1 /*************************************************************************/
2 /*  animation_state_machine_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 ANIMATION_STATE_MACHINE_EDITOR_H
32 #define ANIMATION_STATE_MACHINE_EDITOR_H
33 
34 #include "editor/editor_node.h"
35 #include "editor/editor_plugin.h"
36 #include "editor/plugins/animation_tree_editor_plugin.h"
37 #include "editor/property_editor.h"
38 #include "scene/animation/animation_node_state_machine.h"
39 #include "scene/gui/button.h"
40 #include "scene/gui/graph_edit.h"
41 #include "scene/gui/popup.h"
42 #include "scene/gui/tree.h"
43 
44 class AnimationNodeStateMachineEditor : public AnimationTreeNodeEditorPlugin {
45 
46 	GDCLASS(AnimationNodeStateMachineEditor, AnimationTreeNodeEditorPlugin);
47 
48 	Ref<AnimationNodeStateMachine> state_machine;
49 
50 	ToolButton *tool_select;
51 	ToolButton *tool_create;
52 	ToolButton *tool_connect;
53 	LineEdit *name_edit;
54 
55 	HBoxContainer *tool_erase_hb;
56 	ToolButton *tool_erase;
57 	ToolButton *tool_autoplay;
58 	ToolButton *tool_end;
59 
60 	OptionButton *transition_mode;
61 	OptionButton *play_mode;
62 
63 	PanelContainer *panel;
64 
65 	StringName selected_node;
66 
67 	HScrollBar *h_scroll;
68 	VScrollBar *v_scroll;
69 
70 	Control *state_machine_draw;
71 	Control *state_machine_play_pos;
72 
73 	PanelContainer *error_panel;
74 	Label *error_label;
75 
76 	bool updating;
77 
78 	UndoRedo *undo_redo;
79 
80 	static AnimationNodeStateMachineEditor *singleton;
81 
82 	void _state_machine_gui_input(const Ref<InputEvent> &p_event);
83 	void _connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, bool p_auto_advance);
84 	void _state_machine_draw();
85 	void _state_machine_pos_draw();
86 
87 	void _update_graph();
88 
89 	PopupMenu *menu;
90 	PopupMenu *animations_menu;
91 	Vector<String> animations_to_add;
92 
93 	Vector2 add_node_pos;
94 
95 	bool dragging_selected_attempt;
96 	bool dragging_selected;
97 	Vector2 drag_from;
98 	Vector2 drag_ofs;
99 	StringName snap_x;
100 	StringName snap_y;
101 
102 	bool connecting;
103 	StringName connecting_from;
104 	Vector2 connecting_to;
105 	StringName connecting_to_node;
106 
107 	void _add_menu_type(int p_index);
108 	void _add_animation_type(int p_index);
109 
110 	void _removed_from_graph();
111 
112 	struct NodeRect {
113 		StringName node_name;
114 		Rect2 node;
115 		Rect2 play;
116 		Rect2 name;
117 		Rect2 edit;
118 	};
119 
120 	Vector<NodeRect> node_rects;
121 
122 	struct TransitionLine {
123 		StringName from_node;
124 		StringName to_node;
125 		Vector2 from;
126 		Vector2 to;
127 		AnimationNodeStateMachineTransition::SwitchMode mode;
128 		StringName advance_condition_name;
129 		bool advance_condition_state;
130 		bool disabled;
131 		bool auto_advance;
132 		float width;
133 	};
134 
135 	Vector<TransitionLine> transition_lines;
136 
137 	StringName selected_transition_from;
138 	StringName selected_transition_to;
139 
140 	bool over_text;
141 	StringName over_node;
142 	int over_node_what;
143 
144 	String prev_name;
145 	void _name_edited(const String &p_text);
146 	void _name_edited_focus_out();
147 	void _open_editor(const String &p_name);
148 	void _scroll_changed(double);
149 
150 	void _clip_src_line_to_rect(Vector2 &r_from, Vector2 &r_to, const Rect2 &p_rect);
151 	void _clip_dst_line_to_rect(Vector2 &r_from, Vector2 &r_to, const Rect2 &p_rect);
152 
153 	void _erase_selected();
154 	void _update_mode();
155 	void _autoplay_selected();
156 	void _end_selected();
157 
158 	bool last_active;
159 	StringName last_blend_from_node;
160 	StringName last_current_node;
161 	Vector<StringName> last_travel_path;
162 	float last_play_pos;
163 	float play_pos;
164 	float current_length;
165 
166 	float error_time;
167 	String error_text;
168 
169 	EditorFileDialog *open_file;
170 	Ref<AnimationNode> file_loaded;
171 	void _file_opened(const String &p_file);
172 
173 	enum {
174 		MENU_LOAD_FILE = 1000,
175 		MENU_PASTE = 1001,
176 		MENU_LOAD_FILE_CONFIRM = 1002
177 	};
178 
179 protected:
180 	void _notification(int p_what);
181 	static void _bind_methods();
182 
183 public:
get_singleton()184 	static AnimationNodeStateMachineEditor *get_singleton() { return singleton; }
185 	virtual bool can_edit(const Ref<AnimationNode> &p_node);
186 	virtual void edit(const Ref<AnimationNode> &p_node);
187 	AnimationNodeStateMachineEditor();
188 };
189 
190 #endif // ANIMATION_STATE_MACHINE_EDITOR_H
191