visual_script_editor.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*************************************************************************/
  2. /* visual_script_editor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_SCRIPT_EDITOR_H
  31. #define VISUAL_SCRIPT_EDITOR_H
  32. #include "../visual_script.h"
  33. #include "editor/create_dialog.h"
  34. #include "editor/plugins/script_editor_plugin.h"
  35. #include "visual_script_property_selector.h"
  36. class GraphEdit;
  37. class EditorUndoRedoManager;
  38. class VisualScriptEditorSignalEdit;
  39. class VisualScriptEditorVariableEdit;
  40. #ifdef TOOLS_ENABLED
  41. class VisualScriptEditedProperty : public RefCounted {
  42. GDCLASS(VisualScriptEditedProperty, RefCounted);
  43. private:
  44. Variant edited_property;
  45. protected:
  46. static void _bind_methods();
  47. public:
  48. void set_edited_property(Variant p_variant);
  49. Variant get_edited_property() const;
  50. VisualScriptEditedProperty() {}
  51. };
  52. // TODO: Maybe this class should be refactored.
  53. // See https://github.com/godotengine/godot/issues/51913
  54. class VisualScriptEditor : public ScriptEditorBase {
  55. GDCLASS(VisualScriptEditor, ScriptEditorBase);
  56. enum {
  57. TYPE_SEQUENCE = 1000,
  58. INDEX_BASE_SEQUENCE = 1024
  59. };
  60. enum {
  61. EDIT_ADD_NODE,
  62. EDIT_SEPARATOR, // popup menu separator - ignored
  63. EDIT_CUT_NODES,
  64. EDIT_COPY_NODES,
  65. EDIT_PASTE_NODES,
  66. EDIT_DELETE_NODES,
  67. EDIT_DUPLICATE_NODES,
  68. EDIT_CLEAR_COPY_BUFFER,
  69. EDIT_CREATE_FUNCTION,
  70. EDIT_TOGGLE_BREAKPOINT,
  71. EDIT_FIND_NODE_TYPE,
  72. REFRESH_GRAPH,
  73. };
  74. enum PortAction {
  75. CREATE_CALL_SET_GET,
  76. CREATE_ACTION,
  77. };
  78. enum MemberAction {
  79. MEMBER_EDIT,
  80. MEMBER_REMOVE
  81. };
  82. enum MemberType {
  83. MEMBER_FUNCTION,
  84. MEMBER_VARIABLE,
  85. MEMBER_SIGNAL
  86. };
  87. VBoxContainer *members_section = nullptr;
  88. MenuButton *edit_menu = nullptr;
  89. Ref<VisualScript> script;
  90. Button *base_type_select = nullptr;
  91. LineEdit *func_name_box = nullptr;
  92. ScrollContainer *func_input_scroll = nullptr;
  93. VBoxContainer *func_input_vbox = nullptr;
  94. ConfirmationDialog *function_create_dialog = nullptr;
  95. GraphEdit *graph = nullptr;
  96. HBoxContainer *status_bar = nullptr;
  97. Button *toggle_scripts_button = nullptr;
  98. VisualScriptEditorSignalEdit *signal_editor = nullptr;
  99. AcceptDialog *edit_signal_dialog = nullptr;
  100. EditorInspector *edit_signal_edit = nullptr;
  101. VisualScriptPropertySelector *method_select = nullptr;
  102. VisualScriptPropertySelector *new_connect_node_select = nullptr;
  103. VisualScriptPropertySelector *new_virtual_method_select = nullptr;
  104. VisualScriptEditorVariableEdit *variable_editor = nullptr;
  105. AcceptDialog *edit_variable_dialog = nullptr;
  106. EditorInspector *edit_variable_edit = nullptr;
  107. PopupPanel *default_property_editor_popup = nullptr;
  108. EditorProperty *default_property_editor = nullptr;
  109. Ref<VisualScriptEditedProperty> edited_default_property_holder;
  110. Ref<EditorUndoRedoManager> undo_redo;
  111. Tree *members = nullptr;
  112. AcceptDialog *function_name_edit = nullptr;
  113. LineEdit *function_name_box = nullptr;
  114. Label *hint_text = nullptr;
  115. Timer *hint_text_timer = nullptr;
  116. Label *select_func_text = nullptr;
  117. bool updating_graph = false;
  118. void _show_hint(const String &p_hint);
  119. void _hide_timer();
  120. CreateDialog *select_base_type = nullptr;
  121. struct VirtualInMenu {
  122. String name;
  123. Variant::Type ret;
  124. bool ret_variant;
  125. Vector<Pair<Variant::Type, String>> args;
  126. };
  127. HashMap<StringName, Color> node_colors;
  128. HashMap<StringName, Ref<StyleBox>> node_styles;
  129. HashMap<StringName, Variant::Type> base_type_map;
  130. void _update_graph_connections();
  131. void _update_graph(int p_only_id = -1);
  132. bool updating_members = false;
  133. void _update_members();
  134. String _sanitized_variant_text(const StringName &property_name);
  135. StringName selected;
  136. String _validate_name(const String &p_name) const;
  137. struct Clipboard {
  138. HashMap<int, Ref<VisualScriptNode>> nodes;
  139. HashMap<int, Vector2> nodes_positions;
  140. RBSet<VisualScript::SequenceConnection> sequence_connections;
  141. RBSet<VisualScript::DataConnection> data_connections;
  142. };
  143. static Clipboard *clipboard;
  144. PopupMenu *popup_menu = nullptr;
  145. PopupMenu *member_popup = nullptr;
  146. MemberType member_type;
  147. String member_name;
  148. PortAction port_action;
  149. int port_action_node = 0;
  150. int port_action_output = 0;
  151. Vector2 port_action_pos;
  152. int port_action_new_node = 0;
  153. bool saved_pos_dirty = false;
  154. Vector2 mouse_up_position;
  155. void _port_action_menu(int p_option);
  156. void connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id);
  157. NodePath drop_path;
  158. Node *drop_node = nullptr;
  159. Vector2 drop_position;
  160. void _selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting = true);
  161. void connect_seq(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode_new, int new_id);
  162. void _cancel_connect_node();
  163. int _create_new_node_from_name(const String &p_text, const Vector2 &p_point);
  164. void _selected_new_virtual_method(const String &p_text, const String &p_category, const bool p_connecting);
  165. int error_line = -1;
  166. void _node_selected(Node *p_node);
  167. void _center_on_node(int p_id);
  168. void _node_filter_changed(const String &p_text);
  169. void _change_base_type_callback();
  170. void _change_base_type();
  171. void _toggle_tool_script();
  172. void _member_selected();
  173. void _member_edited();
  174. void _begin_node_move();
  175. void _end_node_move();
  176. void _move_node(int p_id, const Vector2 &p_to);
  177. void _get_ends(int p_node, const List<VisualScript::SequenceConnection> &p_seqs, const RBSet<int> &p_selected, RBSet<int> &r_end_nodes);
  178. void _node_moved(Vector2 p_from, Vector2 p_to, int p_id);
  179. void _remove_node(int p_id);
  180. void _graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot);
  181. void _graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot);
  182. void _graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos);
  183. void _node_ports_changed(int p_id);
  184. void _node_create();
  185. void _update_available_nodes();
  186. void _member_button(Object *p_item, int p_column, int p_button, MouseButton p_mouse_button);
  187. void _expression_text_changed(const String &p_text, int p_id);
  188. void _add_input_port(int p_id);
  189. void _add_output_port(int p_id);
  190. void _remove_input_port(int p_id, int p_port);
  191. void _remove_output_port(int p_id, int p_port);
  192. void _change_port_type(int p_select, int p_id, int p_port, bool is_input);
  193. void _update_node_size(int p_id);
  194. void _port_name_focus_out(const Node *p_name_box, int p_id, int p_port, bool is_input);
  195. Vector2 _get_pos_in_graph(Vector2 p_point) const;
  196. Vector2 _get_available_pos(bool p_centered = true, Vector2 p_pos = Vector2()) const;
  197. bool node_has_sequence_connections(int p_id);
  198. void _generic_search(Vector2 pos = Vector2(), bool node_centered = false);
  199. virtual void input(const Ref<InputEvent> &p_event) override;
  200. void _graph_gui_input(const Ref<InputEvent> &p_event);
  201. void _members_gui_input(const Ref<InputEvent> &p_event);
  202. void _fn_name_box_input(const Ref<InputEvent> &p_event);
  203. void _on_fn_name_box_confirmed();
  204. void _rename_function(const String &p_name, const String &p_new_name);
  205. void _create_function_dialog();
  206. void _create_function();
  207. void _add_func_input();
  208. void _remove_func_input(Node *p_node);
  209. void _deselect_input_names();
  210. void _add_node_dialog();
  211. void _node_item_selected();
  212. void _node_item_unselected();
  213. void _on_nodes_copy();
  214. void _on_nodes_paste();
  215. void _on_nodes_delete();
  216. void _on_nodes_duplicate();
  217. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  218. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  219. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  220. int editing_id = 0;
  221. int editing_input = 0;
  222. bool can_swap = false;
  223. int data_disconnect_node = 0;
  224. int data_disconnect_port = 0;
  225. void _default_value_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing);
  226. void _default_value_edited(Node *p_button, int p_id, int p_input_port);
  227. void _menu_option(int p_what);
  228. void _graph_ofs_changed(const Vector2 &p_ofs);
  229. void _comment_node_resized(const Vector2 &p_new_size, int p_node);
  230. void _draw_color_over_button(Object *obj, Color p_color);
  231. void _button_resource_previewed(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, Variant p_ud);
  232. VisualScriptNode::TypeGuess _guess_output_type(int p_port_action_node, int p_port_action_output, RBSet<int> &p_visited_nodes);
  233. void _member_rmb_selected(const Vector2 &p_pos, MouseButton p_button);
  234. void _member_option(int p_option);
  235. void _toggle_scripts_pressed();
  236. protected:
  237. void _notification(int p_what);
  238. static void _bind_methods();
  239. public:
  240. virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
  241. virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
  242. virtual void apply_code() override;
  243. virtual Ref<Resource> get_edited_resource() const override;
  244. virtual void set_edited_resource(const Ref<Resource> &p_res) override;
  245. virtual void enable_editor() override;
  246. virtual Vector<String> get_functions() override;
  247. virtual void reload_text() override;
  248. virtual String get_name() override;
  249. virtual Ref<Texture2D> get_theme_icon() override;
  250. virtual bool is_unsaved() override;
  251. virtual Variant get_edit_state() override;
  252. virtual void set_edit_state(const Variant &p_state) override;
  253. virtual void goto_line(int p_line, bool p_with_error = false) override;
  254. virtual void set_executing_line(int p_line) override;
  255. virtual void clear_executing_line() override;
  256. virtual void trim_trailing_whitespace() override;
  257. virtual void insert_final_newline() override;
  258. virtual void convert_indent_to_spaces() override;
  259. virtual void convert_indent_to_tabs() override;
  260. virtual void ensure_focus() override;
  261. virtual void tag_saved_version() override;
  262. virtual void reload(bool p_soft) override;
  263. virtual PackedInt32Array get_breakpoints() override;
  264. virtual void set_breakpoint(int p_line, bool p_enable) override{};
  265. virtual void clear_breakpoints() override{};
  266. virtual void add_callback(const String &p_function, PackedStringArray p_args) override;
  267. virtual void update_settings() override;
  268. virtual bool show_members_overview() override;
  269. virtual void set_debugger_active(bool p_active) override;
  270. virtual void set_tooltip_request_func(const Callable &p_toolip_callback) override;
  271. virtual Control *get_edit_menu() override;
  272. virtual void clear_edit_menu() override;
  273. virtual void set_find_replace_bar(FindReplaceBar *p_bar) override { p_bar->hide(); }; // Not needed here.
  274. virtual bool can_lose_focus_on_node_selection() override { return false; }
  275. virtual void validate() override;
  276. virtual Control *get_base_editor() const override;
  277. static void register_editor();
  278. static void free_clipboard();
  279. void update_toggle_scripts_button() override;
  280. VisualScriptEditor();
  281. ~VisualScriptEditor();
  282. };
  283. // Singleton
  284. class VisualScriptCustomNodes : public Object {
  285. GDCLASS(VisualScriptCustomNodes, Object);
  286. friend class VisualScriptLanguage;
  287. protected:
  288. static void _bind_methods();
  289. static VisualScriptCustomNodes *singleton;
  290. static HashMap<String, Ref<RefCounted>> custom_nodes;
  291. static Ref<VisualScriptNode> create_node_custom(const String &p_name);
  292. public:
  293. static VisualScriptCustomNodes *get_singleton() { return singleton; }
  294. void add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script);
  295. void remove_custom_node(const String &p_name, const String &p_category);
  296. VisualScriptCustomNodes();
  297. ~VisualScriptCustomNodes();
  298. };
  299. #endif
  300. #endif // VISUAL_SCRIPT_EDITOR_H