visual_shader_editor_plugin.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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-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 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<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. GraphEdit *graph;
  54. MenuButton *add_node;
  55. OptionButton *edit_type;
  56. PanelContainer *error_panel;
  57. Label *error_label;
  58. UndoRedo *undo_redo;
  59. void _update_graph();
  60. struct AddOption {
  61. String name;
  62. String category;
  63. String type;
  64. Ref<Script> script;
  65. AddOption(const String &p_name = String(), const String &p_category = String(), const String &p_type = String()) {
  66. name = p_name;
  67. type = p_type;
  68. category = p_category;
  69. }
  70. };
  71. Vector<AddOption> add_options;
  72. void _draw_color_over_button(Object *obj, Color p_color);
  73. void _add_node(int p_idx);
  74. void _update_options_menu();
  75. static VisualShaderEditor *singleton;
  76. void _node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node);
  77. bool updating;
  78. void _connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index);
  79. void _disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index);
  80. void _scroll_changed(const Vector2 &p_scroll);
  81. void _node_selected(Object *p_node);
  82. void _delete_request(int);
  83. void _removed_from_graph();
  84. void _node_changed(int p_id);
  85. void _edit_port_default_input(Object *p_button, int p_node, int p_port);
  86. void _port_edited();
  87. void _connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position);
  88. void _line_edit_changed(const String &p_text, Object *line_edit, int p_node_id);
  89. void _line_edit_focus_out(Object *line_edit, int p_node_id);
  90. void _duplicate_nodes();
  91. Vector<Ref<VisualShaderNodePlugin> > plugins;
  92. void _mode_selected(int p_id);
  93. void _input_select_item(Ref<VisualShaderNodeInput> input, String name);
  94. void _preview_select_port(int p_node, int p_port);
  95. void _input(const Ref<InputEvent> p_event);
  96. protected:
  97. void _notification(int p_what);
  98. static void _bind_methods();
  99. public:
  100. void add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
  101. void remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
  102. static VisualShaderEditor *get_singleton() { return singleton; }
  103. void add_custom_type(const String &p_name, const String &p_category, const Ref<Script> &p_script);
  104. void remove_custom_type(const Ref<Script> &p_script);
  105. virtual Size2 get_minimum_size() const;
  106. void edit(VisualShader *p_visual_shader);
  107. VisualShaderEditor();
  108. };
  109. class VisualShaderEditorPlugin : public EditorPlugin {
  110. GDCLASS(VisualShaderEditorPlugin, EditorPlugin);
  111. VisualShaderEditor *visual_shader_editor;
  112. EditorNode *editor;
  113. Button *button;
  114. public:
  115. virtual String get_name() const { return "VisualShader"; }
  116. bool has_main_screen() const { return false; }
  117. virtual void edit(Object *p_object);
  118. virtual bool handles(Object *p_object) const;
  119. virtual void make_visible(bool p_visible);
  120. VisualShaderEditorPlugin(EditorNode *p_node);
  121. ~VisualShaderEditorPlugin();
  122. };
  123. class VisualShaderNodePluginDefault : public VisualShaderNodePlugin {
  124. GDCLASS(VisualShaderNodePluginDefault, VisualShaderNodePlugin)
  125. public:
  126. virtual Control *create_editor(const Ref<VisualShaderNode> &p_node);
  127. };
  128. class EditorPropertyShaderMode : public EditorProperty {
  129. GDCLASS(EditorPropertyShaderMode, EditorProperty)
  130. OptionButton *options;
  131. void _option_selected(int p_which);
  132. protected:
  133. static void _bind_methods();
  134. public:
  135. void setup(const Vector<String> &p_options);
  136. virtual void update_property();
  137. void set_option_button_clip(bool p_enable);
  138. EditorPropertyShaderMode();
  139. };
  140. class EditorInspectorShaderModePlugin : public EditorInspectorPlugin {
  141. GDCLASS(EditorInspectorShaderModePlugin, EditorInspectorPlugin)
  142. public:
  143. virtual bool can_handle(Object *p_object);
  144. virtual void parse_begin(Object *p_object);
  145. 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);
  146. virtual void parse_end();
  147. };
  148. class VisualShaderNodePortPreview : public Control {
  149. GDCLASS(VisualShaderNodePortPreview, Control)
  150. Ref<VisualShader> shader;
  151. VisualShader::Type type;
  152. int node;
  153. int port;
  154. void _shader_changed(); //must regen
  155. protected:
  156. void _notification(int p_what);
  157. static void _bind_methods();
  158. public:
  159. virtual Size2 get_minimum_size() const;
  160. void setup(const Ref<VisualShader> &p_shader, VisualShader::Type p_type, int p_node, int p_port);
  161. VisualShaderNodePortPreview();
  162. };
  163. #endif // VISUAL_SHADER_EDITOR_PLUGIN_H