visual_shader_editor_plugin.h 26 KB

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