visual_script_editor.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /**************************************************************************/
  2. /* visual_script_editor.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_SCRIPT_EDITOR_H
  31. #define VISUAL_SCRIPT_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. PopupDialog *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. StringName edited_func;
  108. StringName default_func;
  109. void _update_graph_connections();
  110. void _update_graph(int p_only_id = -1);
  111. bool updating_members;
  112. void _update_members();
  113. String _sanitized_variant_text(const StringName &property_name);
  114. StringName selected;
  115. String _validate_name(const String &p_name) const;
  116. struct Clipboard {
  117. Map<int, Ref<VisualScriptNode>> nodes;
  118. Map<int, Vector2> nodes_positions;
  119. Set<VisualScript::SequenceConnection> sequence_connections;
  120. Set<VisualScript::DataConnection> data_connections;
  121. };
  122. static Clipboard *clipboard;
  123. PopupMenu *member_popup;
  124. MemberType member_type;
  125. String member_name;
  126. PortAction port_action;
  127. int port_action_node;
  128. int port_action_output;
  129. Vector2 port_action_pos;
  130. int port_action_new_node;
  131. bool saved_pos_dirty;
  132. Vector2 saved_position;
  133. Vector2 mouse_up_position;
  134. void _port_action_menu(int p_option, const StringName &p_func);
  135. NodePath drop_path;
  136. Node *drop_node = nullptr;
  137. Vector2 drop_position;
  138. void connect_data(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode, int new_id);
  139. void _selected_connect_node(const String &p_text, const String &p_category, const bool p_connecting = true);
  140. void connect_seq(Ref<VisualScriptNode> vnode_old, Ref<VisualScriptNode> vnode_new, int new_id);
  141. void _cancel_connect_node();
  142. int _create_new_node_from_name(const String &p_text, const Vector2 &p_point, const StringName &p_func = StringName());
  143. void _selected_new_virtual_method(const String &p_text, const String &p_category, const bool p_connecting);
  144. int error_line;
  145. void _node_selected(Node *p_node);
  146. void _center_on_node(const StringName &p_func, int p_id);
  147. void _node_filter_changed(const String &p_text);
  148. void _change_base_type_callback();
  149. void _change_base_type();
  150. void _toggle_tool_script();
  151. void _member_selected();
  152. void _member_edited();
  153. void _begin_node_move();
  154. void _end_node_move();
  155. void _move_node(const StringName &p_func, int p_id, const Vector2 &p_to);
  156. void _get_ends(int p_node, const List<VisualScript::SequenceConnection> &p_seqs, const Set<int> &p_selected, Set<int> &r_end_nodes);
  157. void _node_moved(Vector2 p_from, Vector2 p_to, int p_id);
  158. void _remove_node(int p_id);
  159. void _graph_connected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot);
  160. void _graph_disconnected(const String &p_from, int p_from_slot, const String &p_to, int p_to_slot);
  161. void _graph_connect_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_pos);
  162. void _node_ports_changed(const String &p_func, int p_id);
  163. void _node_create();
  164. void _update_available_nodes();
  165. void _member_button(Object *p_item, int p_column, int p_button);
  166. void _expression_text_changed(const String &p_text, int p_id);
  167. void _add_input_port(int p_id);
  168. void _add_output_port(int p_id);
  169. void _remove_input_port(int p_id, int p_port);
  170. void _remove_output_port(int p_id, int p_port);
  171. void _change_port_type(int p_select, int p_id, int p_port, bool is_input);
  172. void _update_node_size(int p_id);
  173. void _port_name_focus_out(const Node *p_name_box, int p_id, int p_port, bool is_input);
  174. Vector2 _get_pos_in_graph(Vector2 p_point) const;
  175. Vector2 _get_available_pos(bool p_centered = true, Vector2 p_pos = Vector2()) const;
  176. StringName _get_function_of_node(int p_id) const;
  177. void _move_nodes_with_rescan(const StringName &p_func_from, const StringName &p_func_to, int p_id);
  178. bool node_has_sequence_connections(const StringName &p_func, int p_id);
  179. void _generic_search(String p_base_type = "", Vector2 pos = Vector2(), bool node_centered = false);
  180. void _input(const Ref<InputEvent> &p_event);
  181. void _graph_gui_input(const Ref<InputEvent> &p_event);
  182. void _members_gui_input(const Ref<InputEvent> &p_event);
  183. void _fn_name_box_input(const Ref<InputEvent> &p_event);
  184. void _rename_function(const String &p_name, const String &p_new_name);
  185. void _create_function_dialog();
  186. void _create_function();
  187. void _add_func_input();
  188. void _remove_func_input(Node *p_node);
  189. void _deselect_input_names();
  190. void _add_node_dialog();
  191. void _node_item_selected();
  192. void _node_item_unselected();
  193. void _on_nodes_copy();
  194. void _on_nodes_paste();
  195. void _on_nodes_delete(const Array &p_nodes);
  196. void _on_nodes_duplicate();
  197. Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
  198. bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
  199. void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
  200. int editing_id;
  201. int editing_input;
  202. bool can_swap;
  203. int data_disconnect_node;
  204. int data_disconnect_port;
  205. void _default_value_changed();
  206. void _default_value_edited(Node *p_button, int p_id, int p_input_port);
  207. void _menu_option(int p_what);
  208. void _graph_ofs_changed(const Vector2 &p_ofs);
  209. void _comment_node_resized(const Vector2 &p_new_size, int p_node);
  210. int selecting_method_id;
  211. void _selected_method(const String &p_method, const String &p_type, const bool p_connecting);
  212. void _draw_color_over_button(Object *obj, Color p_color);
  213. void _button_resource_previewed(const String &p_path, const Ref<Texture> &p_preview, const Ref<Texture> &p_small_preview, Variant p_ud);
  214. VisualScriptNode::TypeGuess _guess_output_type(int p_port_action_node, int p_port_action_output, Set<int> &p_visited_nodes);
  215. void _member_rmb_selected(const Vector2 &p_pos);
  216. void _member_option(int p_option);
  217. protected:
  218. void _notification(int p_what);
  219. static void _bind_methods();
  220. public:
  221. virtual void add_syntax_highlighter(SyntaxHighlighter *p_highlighter);
  222. virtual void set_syntax_highlighter(SyntaxHighlighter *p_highlighter);
  223. virtual void apply_code();
  224. virtual RES get_edited_resource() const;
  225. virtual void set_edited_resource(const RES &p_res);
  226. virtual void enable_editor();
  227. virtual Vector<String> get_functions();
  228. virtual void reload_text();
  229. virtual String get_name();
  230. virtual Ref<Texture> get_icon();
  231. virtual bool is_unsaved();
  232. virtual Variant get_edit_state();
  233. virtual void set_edit_state(const Variant &p_state);
  234. virtual void goto_line(int p_line, bool p_with_error = false);
  235. virtual void set_executing_line(int p_line);
  236. virtual void clear_executing_line();
  237. virtual void trim_trailing_whitespace();
  238. virtual void insert_final_newline();
  239. virtual void convert_indent_to_spaces();
  240. virtual void convert_indent_to_tabs();
  241. virtual void ensure_focus();
  242. virtual void tag_saved_version();
  243. virtual void reload(bool p_soft);
  244. virtual void get_breakpoints(List<int> *p_breakpoints);
  245. virtual void add_callback(const String &p_function, PoolStringArray p_args);
  246. virtual void update_settings();
  247. virtual bool show_members_overview();
  248. virtual void set_debugger_active(bool p_active);
  249. virtual void set_tooltip_request_func(String p_method, Object *p_obj);
  250. virtual Control *get_edit_menu();
  251. virtual void clear_edit_menu();
  252. virtual bool can_lose_focus_on_node_selection() { return false; }
  253. virtual void validate();
  254. static void register_editor();
  255. static void free_clipboard();
  256. VisualScriptEditor();
  257. ~VisualScriptEditor();
  258. };
  259. // Singleton
  260. class _VisualScriptEditor : public Object {
  261. GDCLASS(_VisualScriptEditor, Object);
  262. friend class VisualScriptLanguage;
  263. protected:
  264. static void _bind_methods();
  265. static _VisualScriptEditor *singleton;
  266. static Map<String, RefPtr> custom_nodes;
  267. static Ref<VisualScriptNode> create_node_custom(const String &p_name);
  268. public:
  269. static _VisualScriptEditor *get_singleton() { return singleton; }
  270. void add_custom_node(const String &p_name, const String &p_category, const Ref<Script> &p_script);
  271. void remove_custom_node(const String &p_name, const String &p_category);
  272. _VisualScriptEditor();
  273. ~_VisualScriptEditor();
  274. };
  275. #endif
  276. #endif // VISUAL_SCRIPT_EDITOR_H