1 /*************************************************************************/
2 /*  visual_shader_editor_plugin.h                                        */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #ifndef VISUAL_SHADER_EDITOR_PLUGIN_H
32 #define VISUAL_SHADER_EDITOR_PLUGIN_H
33 
34 #include "editor/editor_node.h"
35 #include "editor/editor_plugin.h"
36 #include "editor/property_editor.h"
37 #include "scene/gui/button.h"
38 #include "scene/gui/graph_edit.h"
39 #include "scene/gui/popup.h"
40 #include "scene/gui/tree.h"
41 #include "scene/resources/visual_shader.h"
42 
43 class VisualShaderNodePlugin : public Reference {
44 
45 	GDCLASS(VisualShaderNodePlugin, Reference);
46 
47 protected:
48 	static void _bind_methods();
49 
50 public:
51 	virtual Control *create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node);
52 };
53 
54 class VisualShaderEditor : public VBoxContainer {
55 
56 	GDCLASS(VisualShaderEditor, VBoxContainer);
57 
58 	CustomPropertyEditor *property_editor;
59 	int editing_node;
60 	int editing_port;
61 
62 	Ref<VisualShader> visual_shader;
63 	HSplitContainer *main_box;
64 	GraphEdit *graph;
65 	ToolButton *add_node;
66 	ToolButton *preview_shader;
67 
68 	OptionButton *edit_type;
69 
70 	PanelContainer *error_panel;
71 	Label *error_label;
72 
73 	bool pending_update_preview;
74 	bool shader_error;
75 	VBoxContainer *preview_vbox;
76 	TextEdit *preview_text;
77 	Label *error_text;
78 
79 	UndoRedo *undo_redo;
80 	Point2 saved_node_pos;
81 	bool saved_node_pos_dirty;
82 
83 	ConfirmationDialog *members_dialog;
84 	MenuButton *tools;
85 
86 	bool preview_showed;
87 
88 	enum ToolsMenuOptions {
89 		EXPAND_ALL,
90 		COLLAPSE_ALL
91 	};
92 
93 	Tree *members;
94 	AcceptDialog *alert;
95 	LineEdit *node_filter;
96 	RichTextLabel *node_desc;
97 
98 	void _tools_menu_option(int p_idx);
99 	void _show_members_dialog(bool at_mouse_pos);
100 
101 	void _update_graph();
102 
103 	struct AddOption {
104 		String name;
105 		String category;
106 		String sub_category;
107 		String type;
108 		String description;
109 		int sub_func;
110 		String sub_func_str;
111 		Ref<Script> script;
112 		int mode;
113 		int return_type;
114 		int func;
115 		float value;
116 		bool highend;
117 		bool is_custom;
118 
119 		AddOption(const String &p_name = String(), const String &p_category = String(), const String &p_sub_category = String(), const String &p_type = String(), const String &p_description = String(), int p_sub_func = -1, int p_return_type = -1, int p_mode = -1, int p_func = -1, float p_value = -1, bool p_highend = false) {
120 			name = p_name;
121 			type = p_type;
122 			category = p_category;
123 			sub_category = p_sub_category;
124 			description = p_description;
125 			sub_func = p_sub_func;
126 			return_type = p_return_type;
127 			mode = p_mode;
128 			func = p_func;
129 			value = p_value;
130 			highend = p_highend;
131 			is_custom = false;
132 		}
133 
134 		AddOption(const String &p_name, const String &p_category, const String &p_sub_category, const String &p_type, const String &p_description, const String &p_sub_func, int p_return_type = -1, int p_mode = -1, int p_func = -1, float p_value = -1, bool p_highend = false) {
135 			name = p_name;
136 			type = p_type;
137 			category = p_category;
138 			sub_category = p_sub_category;
139 			description = p_description;
140 			sub_func = 0;
141 			sub_func_str = p_sub_func;
142 			return_type = p_return_type;
143 			mode = p_mode;
144 			func = p_func;
145 			value = p_value;
146 			highend = p_highend;
147 			is_custom = false;
148 		}
149 	};
150 
151 	Vector<AddOption> add_options;
152 	int texture_node_option_idx;
153 	int custom_node_option_idx;
154 	List<String> keyword_list;
155 
156 	void _draw_color_over_button(Object *obj, Color p_color);
157 
158 	void _add_custom_node(const String &p_path);
159 	void _add_texture_node(const String &p_path);
160 	VisualShaderNode *_add_node(int p_idx, int p_op_idx = -1);
161 	void _update_options_menu();
162 
163 	void _show_preview_text();
164 	void _update_preview();
165 	String _get_description(int p_idx);
166 
167 	static VisualShaderEditor *singleton;
168 
169 	void _node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node);
170 	bool updating;
171 
172 	void _connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index);
173 	void _disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index);
174 
175 	void _scroll_changed(const Vector2 &p_scroll);
176 	void _node_selected(Object *p_node);
177 
178 	void _delete_request(int);
179 	void _on_nodes_delete();
180 
181 	void _removed_from_graph();
182 
183 	void _node_changed(int p_id);
184 
185 	void _edit_port_default_input(Object *p_button, int p_node, int p_port);
186 	void _port_edited();
187 
188 	int to_node;
189 	int to_slot;
190 	int from_node;
191 	int from_slot;
192 
193 	void _connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position);
194 	void _connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position);
195 
196 	void _line_edit_changed(const String &p_text, Object *line_edit, int p_node_id);
197 	void _line_edit_focus_out(Object *line_edit, int p_node_id);
198 
199 	void _port_name_focus_out(Object *line_edit, int p_node_id, int p_port_id, bool p_output);
200 
201 	void _dup_copy_nodes(int p_type, List<int> &r_nodes, Set<int> &r_excluded);
202 	void _dup_update_excluded(int p_type, Set<int> &r_excluded);
203 	void _dup_paste_nodes(int p_type, int p_pasted_type, List<int> &r_nodes, Set<int> &r_excluded, const Vector2 &p_offset, bool p_select);
204 
205 	void _duplicate_nodes();
206 
207 	Vector2 selection_center;
208 	int copy_type; // shader type
209 	List<int> copy_nodes_buffer;
210 	Set<int> copy_nodes_excluded_buffer;
211 
212 	void _clear_buffer();
213 	void _copy_nodes();
214 	void _paste_nodes();
215 
216 	Vector<Ref<VisualShaderNodePlugin> > plugins;
217 
218 	void _mode_selected(int p_id);
219 	void _rebuild();
220 
221 	void _input_select_item(Ref<VisualShaderNodeInput> input, String name);
222 
223 	void _add_input_port(int p_node, int p_port, int p_port_type, const String &p_name);
224 	void _remove_input_port(int p_node, int p_port);
225 	void _change_input_port_type(int p_type, int p_node, int p_port);
226 	void _change_input_port_name(const String &p_text, Object *line_edit, int p_node, int p_port);
227 
228 	void _add_output_port(int p_node, int p_port, int p_port_type, const String &p_name);
229 	void _remove_output_port(int p_node, int p_port);
230 	void _change_output_port_type(int p_type, int p_node, int p_port);
231 	void _change_output_port_name(const String &p_text, Object *line_edit, int p_node, int p_port);
232 
233 	void _expression_focus_out(Object *text_edit, int p_node);
234 
235 	void _set_node_size(int p_type, int p_node, const Size2 &p_size);
236 	void _node_resized(const Vector2 &p_new_size, int p_type, int p_node);
237 
238 	void _preview_select_port(int p_node, int p_port);
239 	void _graph_gui_input(const Ref<InputEvent> &p_event);
240 
241 	void _member_filter_changed(const String &p_text);
242 	void _sbox_input(const Ref<InputEvent> &p_ie);
243 	void _member_selected();
244 	void _member_unselected();
245 	void _member_create();
246 	void _member_cancel();
247 
248 	Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
249 	bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
250 	void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
251 
252 	bool _is_available(int p_mode);
253 	void _update_created_node(GraphNode *node);
254 
255 protected:
256 	void _notification(int p_what);
257 	static void _bind_methods();
258 
259 public:
260 	void update_custom_nodes();
261 	void add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
262 	void remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
263 
get_singleton()264 	static VisualShaderEditor *get_singleton() { return singleton; }
265 
266 	void clear_custom_types();
267 	void add_custom_type(const String &p_name, const Ref<Script> &p_script, const String &p_description, int p_return_icon_type, const String &p_category, const String &p_subcategory);
268 
269 	virtual Size2 get_minimum_size() const;
270 	void edit(VisualShader *p_visual_shader);
271 	VisualShaderEditor();
272 };
273 
274 class VisualShaderEditorPlugin : public EditorPlugin {
275 
276 	GDCLASS(VisualShaderEditorPlugin, EditorPlugin);
277 
278 	VisualShaderEditor *visual_shader_editor;
279 	EditorNode *editor;
280 	Button *button;
281 
282 public:
get_name()283 	virtual String get_name() const { return "VisualShader"; }
has_main_screen()284 	bool has_main_screen() const { return false; }
285 	virtual void edit(Object *p_object);
286 	virtual bool handles(Object *p_object) const;
287 	virtual void make_visible(bool p_visible);
288 
289 	VisualShaderEditorPlugin(EditorNode *p_node);
290 	~VisualShaderEditorPlugin();
291 };
292 
293 class VisualShaderNodePluginDefault : public VisualShaderNodePlugin {
294 
295 	GDCLASS(VisualShaderNodePluginDefault, VisualShaderNodePlugin);
296 
297 public:
298 	virtual Control *create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node);
299 };
300 
301 class EditorPropertyShaderMode : public EditorProperty {
302 	GDCLASS(EditorPropertyShaderMode, EditorProperty);
303 	OptionButton *options;
304 
305 	void _option_selected(int p_which);
306 
307 protected:
308 	static void _bind_methods();
309 
310 public:
311 	void setup(const Vector<String> &p_options);
312 	virtual void update_property();
313 	void set_option_button_clip(bool p_enable);
314 	EditorPropertyShaderMode();
315 };
316 
317 class EditorInspectorShaderModePlugin : public EditorInspectorPlugin {
318 	GDCLASS(EditorInspectorShaderModePlugin, EditorInspectorPlugin);
319 
320 public:
321 	virtual bool can_handle(Object *p_object);
322 	virtual void parse_begin(Object *p_object);
323 	virtual bool parse_property(Object *p_object, Variant::Type p_type, const String &p_path, PropertyHint p_hint, const String &p_hint_text, int p_usage);
324 	virtual void parse_end();
325 };
326 
327 class VisualShaderNodePortPreview : public Control {
328 	GDCLASS(VisualShaderNodePortPreview, Control);
329 	Ref<VisualShader> shader;
330 	VisualShader::Type type;
331 	int node;
332 	int port;
333 	void _shader_changed(); //must regen
334 protected:
335 	void _notification(int p_what);
336 	static void _bind_methods();
337 
338 public:
339 	virtual Size2 get_minimum_size() const;
340 	void setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port);
341 	VisualShaderNodePortPreview();
342 };
343 
344 class VisualShaderConversionPlugin : public EditorResourceConversionPlugin {
345 	GDCLASS(VisualShaderConversionPlugin, EditorResourceConversionPlugin);
346 
347 public:
348 	virtual String converts_to() const;
349 	virtual bool handles(const Ref<Resource> &p_resource) const;
350 	virtual Ref<Resource> convert(const Ref<Resource> &p_resource) const;
351 };
352 
353 #endif // VISUAL_SHADER_EDITOR_PLUGIN_H
354