visual_script_editor.h 11 KB

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