visual_script_editor.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 VISUALSCRIPT_EDITOR_H
  31. #define VISUALSCRIPT_EDITOR_H
  32. #include "editor/create_dialog.h"
  33. #include "editor/plugins/script_editor_plugin.h"
  34. #include "editor/property_editor.h"
  35. #include "scene/gui/graph_edit.h"
  36. #include "visual_script.h"
  37. #include "visual_script_property_selector.h"
  38. class VisualScriptEditorSignalEdit;
  39. class VisualScriptEditorVariableEdit;
  40. #ifdef TOOLS_ENABLED
  41. class VisualScriptEditor : public ScriptEditorBase {
  42. GDCLASS(VisualScriptEditor, ScriptEditorBase);
  43. enum {
  44. TYPE_SEQUENCE = 1000,
  45. INDEX_BASE_SEQUENCE = 1024
  46. };
  47. enum {
  48. EDIT_DELETE_NODES,
  49. EDIT_TOGGLE_BREAKPOINT,
  50. EDIT_FIND_NODE_TYPE,
  51. EDIT_COPY_NODES,
  52. EDIT_CUT_NODES,
  53. EDIT_PASTE_NODES,
  54. EDIT_CREATE_FUNCTION,
  55. REFRESH_GRAPH
  56. };
  57. enum PortAction {
  58. CREATE_CALL_SET_GET,
  59. CREATE_ACTION,
  60. };
  61. enum MemberAction {
  62. MEMBER_EDIT,
  63. MEMBER_REMOVE
  64. };
  65. enum MemberType {
  66. MEMBER_FUNCTION,
  67. MEMBER_VARIABLE,
  68. MEMBER_SIGNAL
  69. };
  70. VBoxContainer *members_section;
  71. MenuButton *edit_menu;
  72. Ref<VisualScript> script;
  73. Button *base_type_select;
  74. LineEdit *func_name_box;
  75. ScrollContainer *func_input_scroll;
  76. VBoxContainer *func_input_vbox;
  77. ConfirmationDialog *function_create_dialog;
  78. GraphEdit *graph;
  79. VisualScriptEditorSignalEdit *signal_editor;
  80. AcceptDialog *edit_signal_dialog;
  81. EditorInspector *edit_signal_edit;
  82. VisualScriptPropertySelector *method_select;
  83. VisualScriptPropertySelector *new_connect_node_select;
  84. VisualScriptPropertySelector *new_virtual_method_select;
  85. VisualScriptEditorVariableEdit *variable_editor;
  86. AcceptDialog *edit_variable_dialog;
  87. EditorInspector *edit_variable_edit;
  88. CustomPropertyEditor *default_value_edit;
  89. UndoRedo *undo_redo;
  90. Tree *members;
  91. AcceptDialog *function_name_edit;
  92. LineEdit *function_name_box;
  93. Label *hint_text;
  94. Timer *hint_text_timer;
  95. Label *select_func_text;
  96. bool updating_graph;
  97. void _show_hint(const String &p_hint);
  98. void _hide_timer();
  99. CreateDialog *select_base_type;
  100. struct VirtualInMenu {
  101. String name;
  102. Variant::Type ret;
  103. bool ret_variant;
  104. Vector<Pair<Variant::Type, String>> args;
  105. };
  106. HashMap<StringName, Ref<StyleBox>> node_styles;
  107. void _update_graph_connections();
  108. void _update_graph(int p_only_id = -1);
  109. bool updating_members;
  110. void _update_members();
  111. String _sanitized_variant_text(const StringName &property_name);
  112. StringName selected;
  113. String _validate_name(const String &p_name) const;
  114. struct Clipboard {
  115. Map<int, Ref<VisualScriptNode>> nodes;
  116. Map<int, Vector2> nodes_positions;
  117. Set<VisualScript::SequenceConnection> sequence_connections;
  118. Set<VisualScript::DataConnection> data_connections;
  119. };
  120. static Clipboard *clipboard;
  121. PopupMenu *member_popup;
  122. MemberType member_type;
  123. String member_name;
  124. PortAction port_action;
  125. int port_action_node;
  126. int port_action_output;
  127. Vector2 port_action_pos;
  128. int port_action_new_node;
  129. bool saved_pos_dirty;
  130. Vector2 saved_position;
  131. Vector2 mouse_up_position;
  132. void _port_action_menu(int p_option);
  133. void connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id);
  134. void _selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting = true);
  135. void connect_seq(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode_new, int new_id);
  136. void _cancel_connect_node();
  137. int _create_new_node_from_name(const String &p_text, const Vector2 &p_point);
  138. void _selected_new_virtual_method(const String &p_text, const String &p_category, const bool p_connecting);
  139. int error_line;
  140. void _node_selected(Node *p_node);
  141. void _center_on_node(int p_id);
  142. void _node_filter_changed(const String &p_text);
  143. void _change_base_type_callback();
  144. void _change_base_type();
  145. void _toggle_tool_script();
  146. void _member_selected();
  147. void _member_edited();
  148. void _begin_node_move();
  149. void _end_node_move();
  150. void _move_node(int p_id, const Vector2 &p_to);
  151. void _get_ends(int p_node, const List<VisualScript::SequenceConnection> &p_seqs, const Set<int> &p_selected, Set<int> &r_end_nodes);
  152. void _node_moved(Vector2 p_from, Vector2 p_to, int p_id);
  153. void _remove_node(int p_id);
  154. void _graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot);
  155. void _graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot);
  156. void _graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos);
  157. void _node_ports_changed(int p_id);
  158. void _node_create();
  159. void _update_available_nodes();
  160. void _member_button(Object *p_item, int p_column, int p_button);
  161. void _expression_text_changed(const String &p_text, int p_id);
  162. void _add_input_port(int p_id);
  163. void _add_output_port(int p_id);
  164. void _remove_input_port(int p_id, int p_port);
  165. void _remove_output_port(int p_id, int p_port);
  166. void _change_port_type(int p_select, int p_id, int p_port, bool is_input);
  167. void _update_node_size(int p_id);
  168. void _port_name_focus_out(const Node *p_name_box, int p_id, int p_port, bool is_input);
  169. Vector2 _get_available_pos(bool centered = true, Vector2 ofs = Vector2()) const;
  170. bool node_has_sequence_connections(int p_id);
  171. void _generic_search(String p_base_type = "", Vector2 pos = Vector2(), bool node_centered = false);
  172. void _input(const Ref<InputEvent> &p_event);
  173. void _graph_gui_input(const Ref<InputEvent> &p_event);
  174. void _members_gui_input(const Ref<InputEvent> &p_event);
  175. void _fn_name_box_input(const Ref<InputEvent> &p_event);
  176. void _rename_function(const String &p_name, const String &p_new_name);
  177. void _create_function_dialog();
  178. void _create_function();
  179. void _add_func_input();
  180. void _remove_func_input(Node *p_node);
  181. void _deselect_input_names();
  182. void _add_node_dialog();
  183. void _node_item_selected();
  184. void _node_item_unselected();
  185. void _on_nodes_delete();
  186. void _on_nodes_duplicate();
  187. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  188. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  189. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  190. int editing_id;
  191. int editing_input;
  192. bool can_swap;
  193. int data_disconnect_node;
  194. int data_disconnect_port;
  195. void _default_value_changed();
  196. void _default_value_edited(Node *p_button, int p_id, int p_input_port);
  197. void _menu_option(int p_what);
  198. void _graph_ofs_changed(const Vector2 &p_ofs);
  199. void _comment_node_resized(const Vector2 &p_new_size, int p_node);
  200. int selecting_method_id;
  201. void _selected_method(const String &p_method, const String &p_type, const bool p_connecting);
  202. void _draw_color_over_button(Object *obj, Color p_color);
  203. void _button_resource_previewed(const String &p_path, const Ref<Texture2D> &p_preview, const Ref<Texture2D> &p_small_preview, Variant p_ud);
  204. VisualScriptNode::TypeGuess _guess_output_type(int p_port_action_node, int p_port_action_output, Set<int> &p_visited_nodes);
  205. void _member_rmb_selected(const Vector2 &p_pos);
  206. void _member_option(int p_option);
  207. protected:
  208. void _notification(int p_what);
  209. static void _bind_methods();
  210. public:
  211. virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
  212. virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
  213. virtual void apply_code() override;
  214. virtual RES get_edited_resource() const override;
  215. virtual void set_edited_resource(const RES &p_res) override;
  216. virtual void enable_editor() override;
  217. virtual Vector<String> get_functions() override;
  218. virtual void reload_text() override;
  219. virtual String get_name() override;
  220. virtual Ref<Texture2D> get_theme_icon() override;
  221. virtual bool is_unsaved() override;
  222. virtual Variant get_edit_state() override;
  223. virtual void set_edit_state(const Variant &p_state) override;
  224. virtual void goto_line(int p_line, bool p_with_error = false) override;
  225. virtual void set_executing_line(int p_line) override;
  226. virtual void clear_executing_line() override;
  227. virtual void trim_trailing_whitespace() override;
  228. virtual void insert_final_newline() override;
  229. virtual void convert_indent_to_spaces() override;
  230. virtual void convert_indent_to_tabs() override;
  231. virtual void ensure_focus() override;
  232. virtual void tag_saved_version() override;
  233. virtual void reload(bool p_soft) override;
  234. virtual Array get_breakpoints() override;
  235. virtual void add_callback(const String &p_function, PackedStringArray p_args) override;
  236. virtual void update_settings() override;
  237. virtual bool show_members_overview() override;
  238. virtual void set_debugger_active(bool p_active) override;
  239. virtual void set_tooltip_request_func(String p_method, Object *p_obj) override;
  240. virtual Control *get_edit_menu() override;
  241. virtual void clear_edit_menu() override;
  242. virtual bool can_lose_focus_on_node_selection() override { return false; }
  243. virtual void validate() override;
  244. static void register_editor();
  245. static void free_clipboard();
  246. VisualScriptEditor();
  247. ~VisualScriptEditor();
  248. };
  249. // Singleton
  250. class _VisualScriptEditor : public Object {
  251. GDCLASS(_VisualScriptEditor, Object);
  252. friend class VisualScriptLanguage;
  253. protected:
  254. static void _bind_methods();
  255. static _VisualScriptEditor *singleton;
  256. static Map<String, REF> custom_nodes;
  257. static Ref<VisualScriptNode> create_node_custom(const String &p_name);
  258. public:
  259. static _VisualScriptEditor *get_singleton() { return singleton; }
  260. void add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script);
  261. void remove_custom_node(const String &p_name, const String &p_category);
  262. _VisualScriptEditor();
  263. ~_VisualScriptEditor();
  264. };
  265. #endif
  266. #endif // VISUALSCRIPT_EDITOR_H