1 /*************************************************************************/
2 /*  shader_graph_editor_plugin.h                                         */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 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 #ifndef SHADER_GRAPH_EDITOR_PLUGIN_H
31 #define SHADER_GRAPH_EDITOR_PLUGIN_H
32 
33 #include "editor/editor_node.h"
34 #include "editor/editor_plugin.h"
35 #include "editor/property_editor.h"
36 #include "scene/gui/button.h"
37 #include "scene/gui/graph_edit.h"
38 #include "scene/gui/popup.h"
39 #include "scene/gui/tree.h"
40 #include "scene/resources/shader.h"
41 #include "scene/resources/shader_graph.h"
42 /**
43 	@author Juan Linietsky <reduzio@gmail.com>
44 */
45 
46 class GraphColorRampEdit : public Control {
47 
48 	OBJ_TYPE(GraphColorRampEdit, Control);
49 
50 	struct Point {
51 
52 		float offset;
53 		Color color;
54 		bool operator<(const Point &p_ponit) const {
55 			return offset < p_ponit.offset;
56 		}
57 	};
58 
59 	PopupPanel *popup;
60 	ColorPicker *picker;
61 
62 	bool grabbing;
63 	int grabbed;
64 	float grabbed_at;
65 	Vector<Point> points;
66 
67 	void _color_changed(const Color &p_color);
68 
69 protected:
70 	void _input_event(const InputEvent &p_event);
71 	void _notification(int p_what);
72 	static void _bind_methods();
73 
74 public:
75 	void set_ramp(const Vector<float> &p_offsets, const Vector<Color> &p_colors);
76 	Vector<float> get_offsets() const;
77 	Vector<Color> get_colors() const;
78 	virtual Size2 get_minimum_size() const;
79 	GraphColorRampEdit();
80 };
81 
82 class GraphCurveMapEdit : public Control {
83 
84 	OBJ_TYPE(GraphCurveMapEdit, Control);
85 
86 	struct Point {
87 
88 		float offset;
89 		float height;
90 		bool operator<(const Point &p_ponit) const {
91 			return offset < p_ponit.offset;
92 		}
93 	};
94 
95 	bool grabbing;
96 	int grabbed;
97 	Vector<Point> points;
98 
99 	void _plot_curve(const Vector2 &p_a, const Vector2 &p_b, const Vector2 &p_c, const Vector2 &p_d);
100 
101 protected:
102 	void _input_event(const InputEvent &p_event);
103 	void _notification(int p_what);
104 	static void _bind_methods();
105 
106 public:
107 	void set_points(const Vector<Vector2> &p_points);
108 	Vector<Vector2> get_points() const;
109 	virtual Size2 get_minimum_size() const;
110 	GraphCurveMapEdit();
111 };
112 
113 class ShaderGraphView : public Control {
114 
115 	OBJ_TYPE(ShaderGraphView, Control);
116 
117 	CustomPropertyEditor *ped_popup;
118 	bool block_update;
119 
120 	Label *status;
121 	GraphEdit *graph_edit;
122 	Ref<ShaderGraph> graph;
123 	int edited_id;
124 	int edited_def;
125 
126 	ShaderGraph::ShaderType type;
127 
128 	void _update_graph();
129 	void _create_node(int p_id);
130 
131 	ToolButton *make_label(String text, Variant::Type v_type = Variant::NIL);
132 	ToolButton *make_editor(String text, GraphNode *gn, int p_id, int param, Variant::Type type, String p_hint = "");
133 
134 	void _connection_request(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot);
135 	void _disconnection_request(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot);
136 
137 	void _node_removed(int p_id);
138 	void _begin_node_move();
139 	void _node_moved(const Vector2 &p_from, const Vector2 &p_to, int p_id);
140 	void _end_node_move();
141 	void _move_node(int p_id, const Vector2 &p_to);
142 	void _duplicate_nodes_request();
143 	void _duplicate_nodes(const Array &p_nodes);
144 	void _delete_nodes_request();
145 
146 	void _default_changed(int p_id, Node *p_button, int p_param, int v_type, String p_hint);
147 
148 	void _scalar_const_changed(double p_value, int p_id);
149 	void _vec_const_changed(double p_value, int p_id, Array p_arr);
150 	void _rgb_const_changed(const Color &p_color, int p_id);
151 	void _xform_const_changed(int p_id, Node *p_button);
152 	void _scalar_op_changed(int p_op, int p_id);
153 	void _vec_op_changed(int p_op, int p_id);
154 	void _vec_scalar_op_changed(int p_op, int p_id);
155 	void _rgb_op_changed(int p_op, int p_id);
156 	void _xform_inv_rev_changed(bool p_enabled, int p_id);
157 	void _scalar_func_changed(int p_func, int p_id);
158 	void _vec_func_changed(int p_func, int p_id);
159 	void _scalar_input_changed(double p_value, int p_id);
160 	void _vec_input_changed(double p_value, int p_id, Array p_arr);
161 	void _xform_input_changed(int p_id, Node *p_button);
162 	void _rgb_input_changed(const Color &p_color, int p_id);
163 	void _tex_input_change(int p_id, Node *p_button);
164 	void _cube_input_change(int p_id);
165 	void _input_name_changed(const String &p_name, int p_id, Node *p_line_edit);
166 	void _tex_edited(int p_id, Node *p_button);
167 	void _cube_edited(int p_id, Node *p_button);
168 	void _variant_edited();
169 	void _comment_edited(int p_id, Node *p_button);
170 	void _color_ramp_changed(int p_id, Node *p_ramp);
171 	void _curve_changed(int p_id, Node *p_curve);
172 	void _sg_updated();
173 	Map<int, GraphNode *> node_map;
174 
175 	Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
176 	bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
177 	void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
178 
179 protected:
180 	void _notification(int p_what);
181 	static void _bind_methods();
182 
183 public:
184 	void add_node(int p_type, const Vector2 &location);
get_graph_edit()185 	GraphEdit *get_graph_edit() { return graph_edit; }
186 	void set_graph(Ref<ShaderGraph> p_graph);
187 
188 	ShaderGraphView(ShaderGraph::ShaderType p_type = ShaderGraph::SHADER_TYPE_FRAGMENT);
189 };
190 
191 class ShaderGraphEditor : public VBoxContainer {
192 
193 	OBJ_TYPE(ShaderGraphEditor, VBoxContainer);
194 
195 	PopupMenu *popup;
196 	TabContainer *tabs;
197 	ShaderGraphView *graph_edits[ShaderGraph::SHADER_TYPE_MAX];
198 	static const char *node_names[ShaderGraph::NODE_TYPE_MAX];
199 	Vector2 next_location;
200 
201 	bool _2d;
202 	void _add_node(int p_type);
203 	void _popup_requested(const Vector2 &p_position);
204 
205 protected:
206 	void _notification(int p_what);
207 	static void _bind_methods();
208 
209 public:
210 	void edit(Ref<ShaderGraph> p_shader);
211 	ShaderGraphEditor(bool p_2d);
212 };
213 
214 class ShaderGraphEditorPlugin : public EditorPlugin {
215 
216 	OBJ_TYPE(ShaderGraphEditorPlugin, EditorPlugin);
217 
218 	bool _2d;
219 	ShaderGraphEditor *shader_editor;
220 	EditorNode *editor;
221 
222 public:
get_name()223 	virtual String get_name() const { return "ShaderGraph"; }
has_main_screen()224 	bool has_main_screen() const { return false; }
225 	virtual void edit(Object *p_node);
226 	virtual bool handles(Object *p_node) const;
227 	virtual void make_visible(bool p_visible);
228 
229 	ShaderGraphEditorPlugin(EditorNode *p_node, bool p_2d);
230 	~ShaderGraphEditorPlugin();
231 };
232 #endif
233