Skip to content
0.2.0-dev Download ZIP
328 lines ·10.2 KB Raw
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) 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
31#pragma once
32
33#include "editor/animation/animation_tree_editor_plugin.h"
34#include "scene/animation/animation_node_state_machine.h"
35#include "scene/gui/graph_edit.h"
36#include "scene/gui/popup.h"
37
38class ConfirmationDialog;
39class EditorFileDialog;
40class LineEdit;
41class OptionButton;
42class PanelContainer;
43
44class AnimationNodeStateMachineEditor : public AnimationTreeNodeEditorPlugin {
45 GDCLASS(AnimationNodeStateMachineEditor, AnimationTreeNodeEditorPlugin);
46
47 Ref<AnimationNodeStateMachine> state_machine;
48
49 bool read_only = false;
50
51 Button *tool_select = nullptr;
52 Button *tool_create = nullptr;
53 Button *tool_connect = nullptr;
54 Popup *name_edit_popup = nullptr;
55 LineEdit *name_edit = nullptr;
56
57 HBoxContainer *selection_tools_hb = nullptr;
58 Button *tool_erase = nullptr;
59
60 HBoxContainer *transition_tools_hb = nullptr;
61 OptionButton *switch_mode = nullptr;
62 Button *auto_advance = nullptr;
63
64 OptionButton *play_mode = nullptr;
65
66 PanelContainer *panel = nullptr;
67
68 StringName selected_node;
69 HashSet<StringName> selected_nodes;
70
71 HScrollBar *h_scroll = nullptr;
72 VScrollBar *v_scroll = nullptr;
73
74 Control *state_machine_draw = nullptr;
75 Control *state_machine_play_pos = nullptr;
76
77 struct ThemeCache {
78 Ref<StyleBox> panel_style;
79 Ref<StyleBox> error_panel_style;
80 Color error_color;
81
82 Ref<Texture2D> tool_icon_select;
83 Ref<Texture2D> tool_icon_create;
84 Ref<Texture2D> tool_icon_connect;
85 Ref<Texture2D> tool_icon_erase;
86
87 Ref<Texture2D> transition_icon_immediate;
88 Ref<Texture2D> transition_icon_sync;
89 Ref<Texture2D> transition_icon_end;
90
91 Ref<Texture2D> play_icon_start;
92 Ref<Texture2D> play_icon_travel;
93 Ref<Texture2D> play_icon_auto;
94
95 Ref<Texture2D> animation_icon;
96
97 Ref<StyleBox> node_frame;
98 Ref<StyleBox> node_frame_selected;
99 Ref<StyleBox> node_frame_playing;
100 Ref<StyleBox> node_frame_start;
101 Ref<StyleBox> node_frame_end;
102
103 Ref<Font> node_title_font;
104 int node_title_font_size = 0;
105 Color node_title_font_color;
106
107 Ref<Texture2D> play_node;
108 Ref<Texture2D> edit_node;
109
110 Color transition_color;
111 Color transition_disabled_color;
112 Color transition_icon_color;
113 Color transition_icon_disabled_color;
114 Color highlight_color;
115 Color highlight_disabled_color;
116 Color focus_color;
117 Color guideline_color;
118
119 Ref<Texture2D> transition_icons[6]{};
120
121 Color playback_color;
122 Color playback_background_color;
123 } theme_cache;
124
125 bool updating = false;
126
127 static AnimationNodeStateMachineEditor *singleton;
128
129 void _state_machine_gui_input(const Ref<InputEvent> &p_event);
130 void _connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, float p_fade_ratio, bool p_auto_advance, bool p_is_across_group, float p_opacity = 1.0, bool p_endpoint_hovered = false, bool p_endpoint_hovered_start = false);
131
132 void _state_machine_draw();
133
134 void _state_machine_pos_draw_individual(const String &p_name, float p_ratio);
135 void _state_machine_pos_draw_all();
136
137 void _update_graph();
138
139 PopupMenu *menu = nullptr;
140 PopupMenu *connect_menu = nullptr;
141 PopupMenu *state_machine_menu = nullptr;
142 PopupMenu *end_menu = nullptr;
143 PopupMenu *animations_menu = nullptr;
144 Vector<StringName> animations_to_add;
145 Vector<String> nodes_to_connect;
146
147 Vector2 add_node_pos;
148
149 bool box_selecting = false;
150 Point2 box_selecting_from;
151 Point2 box_selecting_to;
152 Rect2 box_selecting_rect;
153 HashSet<StringName> previous_selected;
154
155 bool dragging_selected_attempt = false;
156 bool dragging_selected = false;
157 Vector2 drag_from;
158 Vector2 drag_ofs;
159 StringName snap_x;
160 StringName snap_y;
161
162 bool connecting = false;
163 bool connection_follows_cursor = false;
164 StringName connecting_from;
165 Vector2 connecting_to;
166 StringName connecting_to_node;
167
168 bool reconnecting = false;
169 int hovered_transition_index = -1;
170 bool hovered_transition_start = false;
171 int reconnecting_transition_index = -1;
172 bool reconnecting_transition_start = false;
173 int reconnecting_from_node_rect_index = -1;
174 int reconnecting_to_node_rect_index = -1;
175 Vector2 reconnecting_transition_pos;
176 StringName reconnecting_transition_target;
177
178 void _add_menu_type(int p_index);
179 void _add_animation_type(int p_index);
180 void _connect_to(int p_index);
181 void _reconnect_transition();
182 void _select_transition(const StringName &p_from, const StringName &p_to);
183
184 struct NodeRect {
185 StringName node_name;
186 Rect2 node;
187 Rect2 play;
188 Rect2 name;
189 Rect2 edit;
190 bool can_edit;
191 };
192
193 Vector<NodeRect> node_rects;
194
195 struct TransitionLine {
196 StringName from_node;
197 StringName to_node;
198 Vector2 from;
199 Vector2 to;
200 AnimationNodeStateMachineTransition::SwitchMode mode;
201 StringName advance_condition_name;
202 bool advance_condition_state = false;
203 bool disabled = false;
204 bool auto_advance = false;
205 float width = 0;
206 bool selected;
207 bool travel;
208 float fade_ratio;
209 bool hidden;
210 int transition_index;
211 bool is_across_group = false;
212 };
213
214 Vector<TransitionLine> transition_lines;
215
216 struct NodeUR {
217 StringName name;
218 Ref<AnimationNode> node;
219 Vector2 position;
220 };
221
222 struct TransitionUR {
223 StringName new_from;
224 StringName new_to;
225 StringName old_from;
226 StringName old_to;
227 Ref<AnimationNodeStateMachineTransition> transition;
228 };
229
230 StringName selected_transition_from;
231 StringName selected_transition_to;
232 int selected_transition_index = -1;
233 void _add_transition(const bool p_nested_action = false);
234
235 enum HoveredNodeArea {
236 HOVER_NODE_NONE = -1,
237 HOVER_NODE_PLAY = 0,
238 HOVER_NODE_EDIT = 1,
239 };
240
241 StringName hovered_node_name;
242 HoveredNodeArea hovered_node_area = HOVER_NODE_NONE;
243
244 String prev_name;
245 void _name_edited(const String &p_text);
246 void _name_edited_focus_out();
247 void _open_editor(const String &p_name);
248 void _scroll_changed(double);
249
250 String _get_root_playback_path(String &r_node_directory);
251
252 void _clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect);
253 void _clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect);
254
255 void _erase_selected(const bool p_nested_action = false);
256 void _update_mode();
257 void _open_menu(const Vector2 &p_position);
258 bool _create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path);
259 void _stop_connecting();
260
261 bool last_active = false;
262 StringName last_fading_from_node;
263 StringName last_current_node;
264 Vector<StringName> last_travel_path;
265
266 float fade_from_last_play_pos = 0.0f;
267 float fade_from_current_play_pos = 0.0f;
268 float fade_from_length = 0.0f;
269
270 float last_play_pos = 0.0f;
271 float current_play_pos = 0.0f;
272 float current_length = 0.0f;
273
274 float last_fading_time = 0.0f;
275 float last_fading_pos = 0.0f;
276 float fading_time = 0.0f;
277 float fading_pos = 0.0f;
278
279 EditorFileDialog *open_file = nullptr;
280 Ref<AnimationNode> file_loaded;
281 void _file_opened(const String &p_file);
282
283 enum {
284 MENU_LOAD_FILE = 1000,
285 MENU_PASTE = 1001,
286 MENU_LOAD_FILE_CONFIRM = 1002
287 };
288
289 HashSet<StringName> connected_nodes;
290 void _update_connected_nodes(const StringName &p_node);
291
292 Ref<StyleBox> _adjust_stylebox_opacity(Ref<StyleBox> p_style, float p_opacity);
293
294protected:
295 void _notification(int p_what);
296 static void _bind_methods();
297
298public:
299 static AnimationNodeStateMachineEditor *get_singleton() { return singleton; }
300
301 virtual bool can_edit(const Ref<AnimationNode> &p_node) override;
302 virtual void edit(const Ref<AnimationNode> &p_node) override;
303
304 virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
305 virtual String get_tooltip(const Point2 &p_pos) const override;
306
307 AnimationNodeStateMachineEditor();
308};
309
310class EditorAnimationMultiTransitionEdit : public RefCounted {
311 GDCLASS(EditorAnimationMultiTransitionEdit, RefCounted);
312
313 struct Transition {
314 StringName from;
315 StringName to;
316 Ref<AnimationNodeStateMachineTransition> transition;
317 };
318
319 Vector<Transition> transitions;
320
321protected:
322 bool _set(const StringName &p_name, const Variant &p_property);
323 bool _get(const StringName &p_name, Variant &r_property) const;
324 void _get_property_list(List<PropertyInfo> *p_list) const;
325
326public:
327 void add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition);
328};