visual_shader_editor_plugin.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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_properties.h"
  33. #include "editor/plugins/editor_plugin.h"
  34. #include "editor/plugins/editor_resource_conversion_plugin.h"
  35. #include "editor/plugins/shader/shader_editor.h"
  36. #include "scene/gui/graph_edit.h"
  37. #include "scene/resources/syntax_highlighter.h"
  38. #include "scene/resources/visual_shader.h"
  39. class CodeEdit;
  40. class ColorPicker;
  41. class CurveEditor;
  42. class GraphElement;
  43. class GraphFrame;
  44. class MenuButton;
  45. class PopupPanel;
  46. class RichTextLabel;
  47. class Tree;
  48. class VisualShaderEditor;
  49. class MaterialEditor;
  50. class VisualShaderNodePlugin : public RefCounted {
  51. GDCLASS(VisualShaderNodePlugin, RefCounted);
  52. protected:
  53. VisualShaderEditor *vseditor = nullptr;
  54. protected:
  55. static void _bind_methods();
  56. GDVIRTUAL2RC(Object *, _create_editor, Ref<Resource>, Ref<VisualShaderNode>)
  57. public:
  58. void set_editor(VisualShaderEditor *p_editor);
  59. virtual Control *create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node);
  60. };
  61. class VSGraphNode : public GraphNode {
  62. GDCLASS(VSGraphNode, GraphNode);
  63. protected:
  64. void _draw_port(int p_slot_index, Point2i p_pos, bool p_left, const Color &p_color, const Color &p_rim_color);
  65. virtual void draw_port(int p_slot_index, Point2i p_pos, bool p_left, const Color &p_color) override;
  66. };
  67. class VSRerouteNode : public VSGraphNode {
  68. GDCLASS(VSRerouteNode, GraphNode);
  69. const float FADE_ANIMATION_LENGTH_SEC = 0.3;
  70. float icon_opacity = 0.0;
  71. protected:
  72. void _notification(int p_what);
  73. virtual void draw_port(int p_slot_index, Point2i p_pos, bool p_left, const Color &p_color) override;
  74. public:
  75. VSRerouteNode();
  76. void set_icon_opacity(float p_opacity);
  77. void _on_mouse_entered();
  78. void _on_mouse_exited();
  79. };
  80. class VisualShaderGraphPlugin : public RefCounted {
  81. GDCLASS(VisualShaderGraphPlugin, RefCounted);
  82. private:
  83. VisualShaderEditor *editor = nullptr;
  84. struct InputPort {
  85. Button *default_input_button = nullptr;
  86. };
  87. struct Port {
  88. VisualShaderNode::PortType type = VisualShaderNode::PORT_TYPE_SCALAR;
  89. TextureButton *preview_button = nullptr;
  90. };
  91. struct Link {
  92. VisualShader::Type type = VisualShader::Type::TYPE_MAX;
  93. VisualShaderNode *visual_node = nullptr;
  94. GraphElement *graph_element = nullptr;
  95. bool preview_visible = false;
  96. int preview_pos = 0;
  97. HashMap<int, InputPort> input_ports;
  98. HashMap<int, Port> output_ports;
  99. VBoxContainer *preview_box = nullptr;
  100. LineEdit *parameter_name = nullptr;
  101. CodeEdit *expression_edit = nullptr;
  102. CurveEditor *curve_editors[3] = { nullptr, nullptr, nullptr };
  103. };
  104. Ref<VisualShader> visual_shader;
  105. HashMap<int, Link> links;
  106. List<VisualShader::Connection> connections;
  107. Color vector_expanded_color[4];
  108. // Visual shader specific theme for using MSDF fonts (on GraphNodes) which reduce aliasing at higher zoom levels.
  109. Ref<Theme> vs_msdf_fonts_theme;
  110. protected:
  111. static void _bind_methods();
  112. public:
  113. void set_editor(VisualShaderEditor *p_editor);
  114. void register_shader(VisualShader *p_visual_shader);
  115. void set_connections(const List<VisualShader::Connection> &p_connections);
  116. void register_link(VisualShader::Type p_type, int p_id, VisualShaderNode *p_visual_node, GraphElement *p_graph_element);
  117. void register_output_port(int p_id, int p_port, VisualShaderNode::PortType p_port_type, TextureButton *p_button);
  118. void register_parameter_name(int p_id, LineEdit *p_parameter_name);
  119. void register_default_input_button(int p_node_id, int p_port_id, Button *p_button);
  120. void register_expression_edit(int p_node_id, CodeEdit *p_expression_edit);
  121. void register_curve_editor(int p_node_id, int p_index, CurveEditor *p_curve_editor);
  122. void clear_links();
  123. void set_shader_type(VisualShader::Type p_type);
  124. bool is_preview_visible(int p_id) const;
  125. void update_node(VisualShader::Type p_type, int p_id);
  126. void update_node_deferred(VisualShader::Type p_type, int p_node_id);
  127. void add_node(VisualShader::Type p_type, int p_id, bool p_just_update, bool p_update_frames);
  128. void remove_node(VisualShader::Type p_type, int p_id, bool p_just_update);
  129. void connect_nodes(VisualShader::Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
  130. void disconnect_nodes(VisualShader::Type p_type, int p_from_node, int p_from_port, int p_to_node, int p_to_port);
  131. void show_port_preview(VisualShader::Type p_type, int p_node_id, int p_port_id, bool p_is_valid);
  132. void update_frames(VisualShader::Type p_type, int p_node);
  133. void set_node_position(VisualShader::Type p_type, int p_id, const Vector2 &p_position);
  134. void refresh_node_ports(VisualShader::Type p_type, int p_node);
  135. void set_input_port_default_value(VisualShader::Type p_type, int p_node_id, int p_port_id, const Variant &p_value);
  136. void update_parameter_refs();
  137. void set_parameter_name(VisualShader::Type p_type, int p_node_id, const String &p_name);
  138. void update_curve(int p_node_id);
  139. void update_curve_xyz(int p_node_id);
  140. void set_expression(VisualShader::Type p_type, int p_node_id, const String &p_expression);
  141. void attach_node_to_frame(VisualShader::Type p_type, int p_node_id, int p_frame_id);
  142. void detach_node_from_frame(VisualShader::Type p_type, int p_node_id);
  143. void set_frame_color_enabled(VisualShader::Type p_type, int p_node_id, bool p_enable);
  144. void set_frame_color(VisualShader::Type p_type, int p_node_id, const Color &p_color);
  145. void set_frame_autoshrink_enabled(VisualShader::Type p_type, int p_node_id, bool p_enable);
  146. void update_reroute_nodes();
  147. int get_constant_index(float p_constant) const;
  148. Ref<Script> get_node_script(int p_node_id) const;
  149. void update_theme();
  150. bool is_node_has_parameter_instances_relatively(VisualShader::Type p_type, int p_node) const;
  151. VisualShader::Type get_shader_type() const;
  152. VisualShaderGraphPlugin();
  153. ~VisualShaderGraphPlugin();
  154. };
  155. class VisualShaderEditedProperty : public RefCounted {
  156. GDCLASS(VisualShaderEditedProperty, RefCounted);
  157. private:
  158. Variant edited_property;
  159. protected:
  160. static void _bind_methods();
  161. public:
  162. void set_edited_property(const Variant &p_variant);
  163. Variant get_edited_property() const;
  164. VisualShaderEditedProperty() {}
  165. };
  166. class VisualShaderEditor : public ShaderEditor {
  167. GDCLASS(VisualShaderEditor, ShaderEditor);
  168. friend class VisualShaderGraphPlugin;
  169. PopupPanel *property_editor_popup = nullptr;
  170. EditorProperty *property_editor = nullptr;
  171. int editing_node = -1;
  172. int editing_port = -1;
  173. Ref<VisualShaderEditedProperty> edited_property_holder;
  174. MaterialEditor *material_editor = nullptr;
  175. Ref<VisualShader> visual_shader;
  176. Ref<ShaderMaterial> preview_material;
  177. Ref<Environment> env;
  178. String param_filter_name;
  179. EditorProperty *current_prop = nullptr;
  180. VBoxContainer *shader_preview_vbox = nullptr;
  181. GraphEdit *graph = nullptr;
  182. Button *add_node = nullptr;
  183. MenuButton *varying_button = nullptr;
  184. Button *code_preview_button = nullptr;
  185. Button *shader_preview_button = nullptr;
  186. int last_to_node = -1;
  187. int last_to_port = -1;
  188. Label *info_label = nullptr;
  189. OptionButton *edit_type = nullptr;
  190. OptionButton *edit_type_standard = nullptr;
  191. OptionButton *edit_type_particles = nullptr;
  192. OptionButton *edit_type_sky = nullptr;
  193. OptionButton *edit_type_fog = nullptr;
  194. CheckBox *custom_mode_box = nullptr;
  195. bool custom_mode_enabled = false;
  196. bool pending_update_preview = false;
  197. bool shader_error = false;
  198. AcceptDialog *code_preview_window = nullptr;
  199. VBoxContainer *code_preview_vbox = nullptr;
  200. CodeEdit *preview_text = nullptr;
  201. Ref<CodeHighlighter> syntax_highlighter = nullptr;
  202. PanelContainer *error_panel = nullptr;
  203. Label *error_label = nullptr;
  204. bool pending_custom_scripts_to_delete = false;
  205. List<Ref<Script>> custom_scripts_to_delete;
  206. bool _block_update_options_menu = false;
  207. bool _block_rebuild_shader = false;
  208. Point2 saved_node_pos;
  209. bool saved_node_pos_dirty = false;
  210. ConfirmationDialog *members_dialog = nullptr;
  211. VisualShaderNode::PortType members_input_port_type = VisualShaderNode::PORT_TYPE_MAX;
  212. VisualShaderNode::PortType members_output_port_type = VisualShaderNode::PORT_TYPE_MAX;
  213. PopupMenu *popup_menu = nullptr;
  214. PopupMenu *connection_popup_menu = nullptr;
  215. PopupMenu *constants_submenu = nullptr;
  216. MenuButton *tools = nullptr;
  217. ConfirmationDialog *add_varying_dialog = nullptr;
  218. OptionButton *varying_type = nullptr;
  219. LineEdit *varying_name = nullptr;
  220. OptionButton *varying_mode = nullptr;
  221. Label *varying_error_label = nullptr;
  222. ConfirmationDialog *remove_varying_dialog = nullptr;
  223. Tree *varyings = nullptr;
  224. PopupPanel *frame_title_change_popup = nullptr;
  225. LineEdit *frame_title_change_edit = nullptr;
  226. PopupPanel *frame_tint_color_pick_popup = nullptr;
  227. ColorPicker *frame_tint_color_picker = nullptr;
  228. bool code_preview_first = true;
  229. bool code_preview_showed = false;
  230. bool shader_preview_showed = true;
  231. LineEdit *param_filter = nullptr;
  232. MenuButton *preview_tools = nullptr;
  233. String selected_param_id;
  234. Tree *parameters = nullptr;
  235. HashMap<String, PropertyInfo> parameter_props;
  236. VBoxContainer *param_vbox = nullptr;
  237. VBoxContainer *param_vbox2 = nullptr;
  238. enum ShaderModeFlags {
  239. MODE_FLAGS_SPATIAL_CANVASITEM = 1,
  240. MODE_FLAGS_SKY = 2,
  241. MODE_FLAGS_PARTICLES = 4,
  242. MODE_FLAGS_FOG = 8,
  243. };
  244. int mode = MODE_FLAGS_SPATIAL_CANVASITEM;
  245. enum TypeFlags {
  246. TYPE_FLAGS_VERTEX = 1,
  247. TYPE_FLAGS_FRAGMENT = 2,
  248. TYPE_FLAGS_LIGHT = 4,
  249. };
  250. enum ParticlesTypeFlags {
  251. TYPE_FLAGS_EMIT = 1,
  252. TYPE_FLAGS_PROCESS = 2,
  253. TYPE_FLAGS_COLLIDE = 4,
  254. TYPE_FLAGS_EMIT_CUSTOM = 8,
  255. TYPE_FLAGS_PROCESS_CUSTOM = 16,
  256. };
  257. enum SkyTypeFlags {
  258. TYPE_FLAGS_SKY = 1,
  259. };
  260. enum FogTypeFlags {
  261. TYPE_FLAGS_FOG = 1,
  262. };
  263. enum ToolsMenuOptions {
  264. EXPAND_ALL,
  265. COLLAPSE_ALL
  266. };
  267. enum PreviewToolsMenuOptions {
  268. COPY_PARAMS_FROM_MATERIAL,
  269. PASTE_PARAMS_TO_MATERIAL,
  270. };
  271. #ifdef MINGW_ENABLED
  272. #undef DELETE
  273. #endif
  274. enum NodeMenuOptions {
  275. ADD,
  276. SEPARATOR, // ignore
  277. CUT,
  278. COPY,
  279. PASTE,
  280. DELETE,
  281. DUPLICATE,
  282. CLEAR_COPY_BUFFER,
  283. SEPARATOR2, // ignore
  284. FLOAT_CONSTANTS,
  285. CONVERT_CONSTANTS_TO_PARAMETERS,
  286. CONVERT_PARAMETERS_TO_CONSTANTS,
  287. UNLINK_FROM_PARENT_FRAME,
  288. SEPARATOR3, // ignore
  289. SET_FRAME_TITLE,
  290. ENABLE_FRAME_COLOR,
  291. SET_FRAME_COLOR,
  292. ENABLE_FRAME_AUTOSHRINK,
  293. };
  294. enum ConnectionMenuOptions {
  295. INSERT_NEW_NODE,
  296. INSERT_NEW_REROUTE,
  297. DISCONNECT,
  298. };
  299. enum class VaryingMenuOptions {
  300. ADD,
  301. REMOVE,
  302. };
  303. Tree *members = nullptr;
  304. AcceptDialog *alert = nullptr;
  305. LineEdit *node_filter = nullptr;
  306. RichTextLabel *node_desc = nullptr;
  307. Label *highend_label = nullptr;
  308. void _tools_menu_option(int p_idx);
  309. void _show_members_dialog(bool at_mouse_pos, VisualShaderNode::PortType p_input_port_type = VisualShaderNode::PORT_TYPE_MAX, VisualShaderNode::PortType p_output_port_type = VisualShaderNode::PORT_TYPE_MAX);
  310. void _varying_menu_id_pressed(int p_idx);
  311. void _show_add_varying_dialog();
  312. void _show_remove_varying_dialog();
  313. void _preview_tools_menu_option(int p_idx);
  314. void _clear_preview_param();
  315. void _update_preview_parameter_list();
  316. bool _update_preview_parameter_tree();
  317. void _update_nodes();
  318. void _update_graph();
  319. struct AddOption {
  320. String name;
  321. String category;
  322. String type;
  323. String description;
  324. Vector<Variant> ops;
  325. Ref<Script> script;
  326. int mode = 0;
  327. int return_type = 0;
  328. int func = 0;
  329. bool highend = false;
  330. bool is_custom = false;
  331. bool is_native = false;
  332. int temp_idx = 0;
  333. AddOption(const String &p_name = String(), const String &p_category = String(), const String &p_type = String(), const String &p_description = String(), const Vector<Variant> &p_ops = Vector<Variant>(), int p_return_type = -1, int p_mode = -1, int p_func = -1, bool p_highend = false) {
  334. name = p_name;
  335. type = p_type;
  336. category = p_category;
  337. description = p_description;
  338. ops = p_ops;
  339. return_type = p_return_type;
  340. mode = p_mode;
  341. func = p_func;
  342. highend = p_highend;
  343. }
  344. };
  345. struct _OptionComparator {
  346. _FORCE_INLINE_ bool operator()(const AddOption &a, const AddOption &b) const {
  347. return a.category.count("/") > b.category.count("/") || (a.category + "/" + a.name).naturalnocasecmp_to(b.category + "/" + b.name) < 0;
  348. }
  349. };
  350. Vector<AddOption> add_options;
  351. int cubemap_node_option_idx;
  352. int texture2d_node_option_idx;
  353. int texture2d_array_node_option_idx;
  354. int texture3d_node_option_idx;
  355. int custom_node_option_idx;
  356. int curve_node_option_idx;
  357. int curve_xyz_node_option_idx;
  358. int mesh_emitter_option_idx;
  359. List<String> keyword_list;
  360. List<VisualShaderNodeParameterRef> uniform_refs;
  361. void _draw_color_over_button(Object *p_obj, Color p_color);
  362. void _setup_node(VisualShaderNode *p_node, const Vector<Variant> &p_ops);
  363. void _add_node(int p_idx, const Vector<Variant> &p_ops, const String &p_resource_path = "", int p_node_idx = -1);
  364. void _add_varying(const String &p_name, VisualShader::VaryingMode p_mode, VisualShader::VaryingType p_type);
  365. void _remove_varying(const String &p_name);
  366. void _update_options_menu();
  367. void _set_mode(int p_which);
  368. void _show_preview_text();
  369. void _preview_close_requested();
  370. void _preview_size_changed();
  371. void _update_preview();
  372. void _update_next_previews(int p_node_id);
  373. void _get_next_nodes_recursively(VisualShader::Type p_type, int p_node_id, LocalVector<int> &r_nodes) const;
  374. String _get_description(int p_idx);
  375. void _show_shader_preview();
  376. Vector<int> nodes_link_to_frame_buffer; // Contains the nodes that are requested to be linked to a frame. This is used to perform one Undo/Redo operation for dragging nodes.
  377. int frame_node_id_to_link_to = -1;
  378. struct DragOp {
  379. VisualShader::Type type = VisualShader::Type::TYPE_MAX;
  380. int node = 0;
  381. Vector2 from;
  382. Vector2 to;
  383. };
  384. List<DragOp> drag_buffer;
  385. bool drag_dirty = false;
  386. void _node_dragged(const Vector2 &p_from, const Vector2 &p_to, int p_node);
  387. void _nodes_dragged();
  388. bool updating = false;
  389. void _connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index);
  390. void _disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index);
  391. void _scroll_changed(const Vector2 &p_scroll);
  392. void _node_selected(Object *p_node);
  393. void _delete_nodes(int p_type, const List<int> &p_nodes);
  394. void _delete_node_request(int p_type, int p_node);
  395. void _delete_nodes_request(const TypedArray<StringName> &p_nodes);
  396. void _node_changed(int p_id);
  397. void _nodes_linked_to_frame_request(const TypedArray<StringName> &p_nodes, const StringName &p_frame);
  398. void _frame_rect_changed(const GraphFrame *p_frame, const Rect2 &p_new_rect);
  399. void _edit_port_default_input(Object *p_button, int p_node, int p_port);
  400. void _port_edited(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing);
  401. int to_node = -1;
  402. int to_slot = -1;
  403. int from_node = -1;
  404. int from_slot = -1;
  405. Ref<GraphEdit::Connection> clicked_connection;
  406. bool connection_node_insert_requested = false;
  407. HashSet<int> selected_constants;
  408. HashSet<int> selected_parameters;
  409. int selected_frame = -1;
  410. int selected_float_constant = -1;
  411. void _convert_constants_to_parameters(bool p_vice_versa);
  412. void _detach_nodes_from_frame_request();
  413. void _detach_nodes_from_frame(int p_type, const List<int> &p_nodes);
  414. void _replace_node(VisualShader::Type p_type_id, int p_node_id, const StringName &p_from, const StringName &p_to);
  415. void _update_constant(VisualShader::Type p_type_id, int p_node_id, const Variant &p_var, int p_preview_port);
  416. void _update_parameter(VisualShader::Type p_type_id, int p_node_id, const Variant &p_var, int p_preview_port);
  417. void _unlink_node_from_parent_frame(int p_node_id);
  418. void _connection_drag_ended();
  419. void _connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position);
  420. void _connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position);
  421. bool _check_node_drop_on_connection(const Vector2 &p_position, Ref<GraphEdit::Connection> *r_closest_connection, int *r_node_id = nullptr, int *r_to_port = nullptr);
  422. void _handle_node_drop_on_connection();
  423. void _frame_title_popup_show(const Point2 &p_position, int p_node_id);
  424. void _frame_title_popup_hide();
  425. void _frame_title_popup_focus_out();
  426. void _frame_title_text_changed(const String &p_new_text);
  427. void _frame_title_text_submitted(const String &p_new_text);
  428. void _frame_color_enabled_changed(int p_node_id);
  429. void _frame_color_popup_show(const Point2 &p_position, int p_node_id);
  430. void _frame_color_popup_hide();
  431. void _frame_color_confirm();
  432. void _frame_color_changed(const Color &p_color);
  433. void _frame_autoshrink_enabled_changed(int p_node_id);
  434. void _parameter_line_edit_changed(const String &p_text, int p_node_id);
  435. void _parameter_line_edit_focus_out(Object *p_line_edit, int p_node_id);
  436. void _port_name_focus_out(Object *p_line_edit, int p_node_id, int p_port_id, bool p_output);
  437. struct CopyItem {
  438. int id;
  439. Ref<VisualShaderNode> node;
  440. Vector2 position;
  441. Vector2 size;
  442. String group_inputs;
  443. String group_outputs;
  444. String expression;
  445. bool disabled = false;
  446. };
  447. void _dup_copy_nodes(int p_type, List<CopyItem> &r_nodes, List<VisualShader::Connection> &r_connections);
  448. void _dup_paste_nodes(int p_type, List<CopyItem> &r_items, const List<VisualShader::Connection> &p_connections, const Vector2 &p_offset, bool p_duplicate);
  449. void _duplicate_nodes();
  450. static Vector2 selection_center;
  451. static List<CopyItem> copy_items_buffer;
  452. static List<VisualShader::Connection> copy_connections_buffer;
  453. void _clear_copy_buffer();
  454. void _copy_nodes(bool p_cut);
  455. void _paste_nodes(bool p_use_custom_position = false, const Vector2 &p_custom_position = Vector2());
  456. Vector<Ref<VisualShaderNodePlugin>> plugins;
  457. Ref<VisualShaderGraphPlugin> graph_plugin;
  458. void _mode_selected(int p_id);
  459. void _custom_mode_toggled(bool p_enabled);
  460. void _input_select_item(Ref<VisualShaderNodeInput> p_input, const String &p_name);
  461. void _parameter_ref_select_item(Ref<VisualShaderNodeParameterRef> p_parameter_ref, const String &p_name);
  462. void _varying_select_item(Ref<VisualShaderNodeVarying> p_varying, const String &p_name);
  463. void _float_constant_selected(int p_which);
  464. VisualShader::Type get_current_shader_type() const;
  465. void _add_input_port(int p_node, int p_port, int p_port_type, const String &p_name);
  466. void _remove_input_port(int p_node, int p_port);
  467. void _change_input_port_type(int p_type, int p_node, int p_port);
  468. void _change_input_port_name(const String &p_text, Object *p_line_edit, int p_node, int p_port);
  469. void _add_output_port(int p_node, int p_port, int p_port_type, const String &p_name);
  470. void _remove_output_port(int p_node, int p_port);
  471. void _change_output_port_type(int p_type, int p_node, int p_port);
  472. void _change_output_port_name(const String &p_text, Object *p_line_edit, int p_node, int p_port);
  473. void _expand_output_port(int p_node, int p_port, bool p_expand);
  474. void _expression_focus_out(Object *p_code_edit, int p_node);
  475. void _set_node_size(int p_type, int p_node, const Size2 &p_size);
  476. void _node_resized(const Vector2 &p_new_size, int p_type, int p_node);
  477. void _preview_select_port(int p_node, int p_port);
  478. void _graph_gui_input(const Ref<InputEvent> &p_event);
  479. void _member_filter_changed(const String &p_text);
  480. void _sbox_input(const Ref<InputEvent> &p_event);
  481. void _member_selected();
  482. void _member_create();
  483. void _member_cancel();
  484. void _varying_create();
  485. void _varying_name_changed(const String &p_name);
  486. void _varying_deleted();
  487. void _varying_selected();
  488. void _varying_unselected();
  489. void _update_varying_tree();
  490. void _set_custom_node_option(int p_index, int p_node, int p_op);
  491. Vector2 menu_point;
  492. void _node_menu_id_pressed(int p_idx);
  493. void _connection_menu_id_pressed(int p_idx);
  494. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  495. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  496. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  497. bool _is_available(int p_mode);
  498. void _update_parameters(bool p_update_refs);
  499. void _update_parameter_refs(HashSet<String> &p_names);
  500. void _update_varyings();
  501. void _update_options_menu_deferred();
  502. void _rebuild_shader_deferred();
  503. void _visibility_changed();
  504. void _get_current_mode_limits(int &r_begin_type, int &r_end_type) const;
  505. void _update_custom_script(const Ref<Script> &p_script);
  506. void _script_created(const Ref<Script> &p_script);
  507. void _resource_saved(const Ref<Resource> &p_resource);
  508. void _resource_removed(const Ref<Resource> &p_resource);
  509. void _resources_removed();
  510. void _param_property_changed(const String &p_property, const Variant &p_value, const String &p_field = "", bool p_changing = false);
  511. void _update_current_param();
  512. void _param_filter_changed(const String &p_text);
  513. void _param_selected();
  514. void _param_unselected();
  515. protected:
  516. void _notification(int p_what);
  517. static void _bind_methods();
  518. public:
  519. virtual void edit_shader(const Ref<Shader> &p_shader) override;
  520. virtual void apply_shaders() override;
  521. virtual bool is_unsaved() const override;
  522. virtual void save_external_data(const String &p_str = "") override;
  523. virtual void validate_script() override;
  524. void add_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
  525. void remove_plugin(const Ref<VisualShaderNodePlugin> &p_plugin);
  526. VisualShaderGraphPlugin *get_graph_plugin() { return graph_plugin.ptr(); }
  527. void clear_custom_types();
  528. void add_custom_type(const String &p_name, const String &p_type, const Ref<Script> &p_script, const String &p_description, int p_return_icon_type, const String &p_category, bool p_highend);
  529. Dictionary get_custom_node_data(Ref<VisualShaderNodeCustom> &p_custom_node);
  530. void update_custom_type(const Ref<Resource> &p_resource);
  531. virtual Size2 get_minimum_size() const override;
  532. Ref<VisualShader> get_visual_shader() const { return visual_shader; }
  533. VisualShaderEditor();
  534. };
  535. class VisualShaderNodePluginDefault : public VisualShaderNodePlugin {
  536. GDCLASS(VisualShaderNodePluginDefault, VisualShaderNodePlugin);
  537. public:
  538. virtual Control *create_editor(const Ref<Resource> &p_parent_resource, const Ref<VisualShaderNode> &p_node) override;
  539. };
  540. class EditorPropertyVisualShaderMode : public EditorProperty {
  541. GDCLASS(EditorPropertyVisualShaderMode, EditorProperty);
  542. OptionButton *options = nullptr;
  543. void _option_selected(int p_which);
  544. public:
  545. void setup(const Vector<String> &p_options);
  546. virtual void update_property() override;
  547. void set_option_button_clip(bool p_enable);
  548. EditorPropertyVisualShaderMode();
  549. };
  550. class EditorInspectorVisualShaderModePlugin : public EditorInspectorPlugin {
  551. GDCLASS(EditorInspectorVisualShaderModePlugin, EditorInspectorPlugin);
  552. public:
  553. virtual bool can_handle(Object *p_object) override;
  554. virtual bool parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide = false) override;
  555. };
  556. class VisualShaderNodePortPreview : public Control {
  557. GDCLASS(VisualShaderNodePortPreview, Control);
  558. TextureRect *checkerboard = nullptr;
  559. Ref<VisualShader> shader;
  560. Ref<ShaderMaterial> preview_mat;
  561. VisualShader::Type type = VisualShader::Type::TYPE_MAX;
  562. int node = 0;
  563. int port = 0;
  564. bool is_valid = false;
  565. void _shader_changed(); //must regen
  566. protected:
  567. void _notification(int p_what);
  568. public:
  569. virtual Size2 get_minimum_size() const override;
  570. void setup(const Ref<VisualShader> &p_shader, Ref<ShaderMaterial> &p_preview_material, VisualShader::Type p_type, bool p_has_transparency, int p_node, int p_port, bool p_is_valid);
  571. };
  572. class VisualShaderConversionPlugin : public EditorResourceConversionPlugin {
  573. GDCLASS(VisualShaderConversionPlugin, EditorResourceConversionPlugin);
  574. public:
  575. virtual String converts_to() const override;
  576. virtual bool handles(const Ref<Resource> &p_resource) const override;
  577. virtual Ref<Resource> convert(const Ref<Resource> &p_resource) const override;
  578. };
  579. #endif // VISUAL_SHADER_EDITOR_PLUGIN_H