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