visual_shader_editor_plugin.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 VISUAL_SHADER_EDITOR_PLUGIN_H
  31. #define VISUAL_SHADER_EDITOR_PLUGIN_H
  32. #include "editor/editor_node.h"
  33. #include "editor/editor_plugin.h"
  34. #include "editor/property_editor.h"
  35. #include "scene/gui/button.h"
  36. #include "scene/gui/graph_edit.h"
  37. #include "scene/gui/popup.h"
  38. #include "scene/gui/tree.h"
  39. #include "scene/resources/visual_shader.h"
  40. class VisualShaderNodePlugin : public Reference {
  41. GDCLASS(VisualShaderNodePlugin, Reference);
  42. protected:
  43. static void _bind_methods();
  44. public:
  45. virtual Control *create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node);
  46. };
  47. class VisualShaderEditor : public VBoxContainer {
  48. GDCLASS(VisualShaderEditor, VBoxContainer);
  49. CustomPropertyEditor *property_editor;
  50. int editing_node;
  51. int editing_port;
  52. Ref<VisualShader> visual_shader;
  53. HSplitContainer *main_box;
  54. GraphEdit *graph;
  55. ToolButton *add_node;
  56. ToolButton *preview_shader;
  57. OptionButton *edit_type;
  58. PanelContainer *error_panel;
  59. Label *error_label;
  60. bool pending_update_preview;
  61. bool shader_error;
  62. VBoxContainer *preview_vbox;
  63. TextEdit *preview_text;
  64. Label *error_text;
  65. UndoRedo *undo_redo;
  66. Point2 saved_node_pos;
  67. bool saved_node_pos_dirty;
  68. ConfirmationDialog *members_dialog;
  69. MenuButton *tools;
  70. bool preview_showed;
  71. enum ToolsMenuOptions {
  72. EXPAND_ALL,
  73. COLLAPSE_ALL
  74. };
  75. Tree *members;
  76. AcceptDialog *alert;
  77. LineEdit *node_filter;
  78. RichTextLabel *node_desc;
  79. void _tools_menu_option(int p_idx);
  80. void _show_members_dialog(bool at_mouse_pos);
  81. void _update_graph();
  82. struct AddOption {
  83. String name;
  84. String category;
  85. String sub_category;
  86. String type;
  87. String description;
  88. int sub_func;
  89. String sub_func_str;
  90. Ref<Script> script;
  91. int mode;
  92. int return_type;
  93. int func;
  94. float value;
  95. bool highend;
  96. bool is_custom;
  97. 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) {
  98. name = p_name;
  99. type = p_type;
  100. category = p_category;
  101. sub_category = p_sub_category;
  102. description = p_description;
  103. sub_func = p_sub_func;
  104. return_type = p_return_type;
  105. mode = p_mode;
  106. func = p_func;
  107. value = p_value;
  108. highend = p_highend;
  109. is_custom = false;
  110. }
  111. 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) {
  112. name = p_name;
  113. type = p_type;
  114. category = p_category;
  115. sub_category = p_sub_category;
  116. description = p_description;
  117. sub_func = 0;
  118. sub_func_str = p_sub_func;
  119. return_type = p_return_type;
  120. mode = p_mode;
  121. func = p_func;
  122. value = p_value;
  123. highend = p_highend;
  124. is_custom = false;
  125. }
  126. };
  127. Vector<AddOption> add_options;
  128. int texture_node_option_idx;
  129. int custom_node_option_idx;
  130. List<String> keyword_list;
  131. void _draw_color_over_button(Object *obj, Color p_color);
  132. void _add_custom_node(const String &p_path);
  133. void _add_texture_node(const String &p_path);
  134. VisualShaderNode *_add_node(int p_idx, int p_op_idx = -1);
  135. void _update_options_menu();
  136. void _show_preview_text();
  137. void _update_preview();
  138. String _get_description(int p_idx);
  139. static VisualShaderEditor *singleton;
  140. struct DragOp {
  141. VisualShader::Type type;
  142. int node;
  143. Vector2 from;
  144. Vector2 to;
  145. };
  146. List<DragOp> drag_buffer;
  147. bool drag_dirty = false;
  148. void _node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node);
  149. void _nodes_dragged();
  150. bool updating;
  151. void _connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index);
  152. void _disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index);
  153. void _scroll_changed(const Vector2 &p_scroll);
  154. void _node_selected(Object *p_node);
  155. void _delete_request(int);
  156. void _on_nodes_delete(const Array &p_nodes);
  157. void _node_changed(int p_id);
  158. void _edit_port_default_input(Object *p_button, int p_node, int p_port);
  159. void _port_edited();
  160. int to_node;
  161. int to_slot;
  162. int from_node;
  163. int from_slot;
  164. void _connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position);
  165. void _connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position);
  166. void _line_edit_changed(const String &p_text, Object *line_edit, int p_node_id);
  167. void _line_edit_focus_out(Object *line_edit, int p_node_id);
  168. void _port_name_focus_out(Object *line_edit, int p_node_id, int p_port_id, bool p_output);
  169. void _dup_copy_nodes(int p_type, List<int> &r_nodes, Set<int> &r_excluded);
  170. void _dup_update_excluded(int p_type, Set<int> &r_excluded);
  171. 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);
  172. void _duplicate_nodes();
  173. Vector2 selection_center;
  174. int copy_type; // shader type
  175. List<int> copy_nodes_buffer;
  176. Set<int> copy_nodes_excluded_buffer;
  177. void _clear_buffer();
  178. void _copy_nodes();
  179. void _paste_nodes();
  180. Vector<Ref<VisualShaderNodePlugin>> plugins;
  181. void _mode_selected(int p_id);
  182. void _rebuild();
  183. void _input_select_item(Ref<VisualShaderNodeInput> input, String name);
  184. void _uniform_select_item(Ref<VisualShaderNodeUniformRef> p_uniform, String p_name);
  185. void _add_input_port(int p_node, int p_port, int p_port_type, const String &p_name);
  186. void _remove_input_port(int p_node, int p_port);
  187. void _change_input_port_type(int p_type, int p_node, int p_port);
  188. void _change_input_port_name(const String &p_text, Object *line_edit, int p_node, int p_port);
  189. void _add_output_port(int p_node, int p_port, int p_port_type, const String &p_name);
  190. void _remove_output_port(int p_node, int p_port);
  191. void _change_output_port_type(int p_type, int p_node, int p_port);
  192. void _change_output_port_name(const String &p_text, Object *line_edit, int p_node, int p_port);
  193. void _expression_focus_out(Object *text_edit, int p_node);
  194. void _set_node_size(int p_type, int p_node, const Size2 &p_size);
  195. void _node_resized(const Vector2 &p_new_size, int p_type, int p_node);
  196. void _preview_select_port(int p_node, int p_port);
  197. void _graph_gui_input(const Ref<InputEvent> &p_event);
  198. void _member_filter_changed(const String &p_text);
  199. void _sbox_input(const Ref<InputEvent> &p_ie);
  200. void _member_selected();
  201. void _member_unselected();
  202. void _member_create();
  203. void _member_cancel();
  204. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  205. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  206. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  207. bool _is_available(int p_mode);
  208. void _update_created_node(GraphNode *node);
  209. protected:
  210. void _notification(int p_what);
  211. static void _bind_methods();
  212. public:
  213. void update_custom_nodes();
  214. void add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
  215. void remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
  216. static VisualShaderEditor *get_singleton() { return singleton; }
  217. void clear_custom_types();
  218. 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);
  219. virtual Size2 get_minimum_size() const;
  220. void edit(VisualShader *p_visual_shader);
  221. VisualShaderEditor();
  222. };
  223. class VisualShaderEditorPlugin : public EditorPlugin {
  224. GDCLASS(VisualShaderEditorPlugin, EditorPlugin);
  225. VisualShaderEditor *visual_shader_editor;
  226. EditorNode *editor;
  227. Button *button;
  228. public:
  229. virtual String get_name() const { return "VisualShader"; }
  230. bool has_main_screen() const { return false; }
  231. virtual void edit(Object *p_object);
  232. virtual bool handles(Object *p_object) const;
  233. virtual void make_visible(bool p_visible);
  234. VisualShaderEditorPlugin(EditorNode *p_node);
  235. ~VisualShaderEditorPlugin();
  236. };
  237. class VisualShaderNodePluginDefault : public VisualShaderNodePlugin {
  238. GDCLASS(VisualShaderNodePluginDefault, VisualShaderNodePlugin);
  239. public:
  240. virtual Control *create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node);
  241. };
  242. class EditorPropertyShaderMode : public EditorProperty {
  243. GDCLASS(EditorPropertyShaderMode, EditorProperty);
  244. OptionButton *options;
  245. void _option_selected(int p_which);
  246. protected:
  247. static void _bind_methods();
  248. public:
  249. void setup(const Vector<String> &p_options);
  250. virtual void update_property();
  251. void set_option_button_clip(bool p_enable);
  252. EditorPropertyShaderMode();
  253. };
  254. class EditorInspectorShaderModePlugin : public EditorInspectorPlugin {
  255. GDCLASS(EditorInspectorShaderModePlugin, EditorInspectorPlugin);
  256. public:
  257. virtual bool can_handle(Object *p_object);
  258. virtual void parse_begin(Object *p_object);
  259. 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);
  260. virtual void parse_end();
  261. };
  262. class VisualShaderNodePortPreview : public Control {
  263. GDCLASS(VisualShaderNodePortPreview, Control);
  264. Ref<VisualShader> shader;
  265. VisualShader::Type type;
  266. int node;
  267. int port;
  268. void _shader_changed(); //must regen
  269. protected:
  270. void _notification(int p_what);
  271. static void _bind_methods();
  272. public:
  273. virtual Size2 get_minimum_size() const;
  274. void setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port);
  275. VisualShaderNodePortPreview();
  276. };
  277. class VisualShaderConversionPlugin : public EditorResourceConversionPlugin {
  278. GDCLASS(VisualShaderConversionPlugin, EditorResourceConversionPlugin);
  279. public:
  280. virtual String converts_to() const;
  281. virtual bool handles(const Ref<Resource> &p_resource) const;
  282. virtual Ref<Resource> convert(const Ref<Resource> &p_resource) const;
  283. };
  284. #endif // VISUAL_SHADER_EDITOR_PLUGIN_H