Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/animation/animation_state_machine_editor.h
9902 views
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
38
class ConfirmationDialog;
39
class EditorFileDialog;
40
class LineEdit;
41
class OptionButton;
42
class PanelContainer;
43
44
class 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
PanelContainer *error_panel = nullptr;
78
Label *error_label = nullptr;
79
80
struct ThemeCache {
81
Ref<StyleBox> panel_style;
82
Ref<StyleBox> error_panel_style;
83
Color error_color;
84
85
Ref<Texture2D> tool_icon_select;
86
Ref<Texture2D> tool_icon_create;
87
Ref<Texture2D> tool_icon_connect;
88
Ref<Texture2D> tool_icon_erase;
89
90
Ref<Texture2D> transition_icon_immediate;
91
Ref<Texture2D> transition_icon_sync;
92
Ref<Texture2D> transition_icon_end;
93
94
Ref<Texture2D> play_icon_start;
95
Ref<Texture2D> play_icon_travel;
96
Ref<Texture2D> play_icon_auto;
97
98
Ref<Texture2D> animation_icon;
99
100
Ref<StyleBox> node_frame;
101
Ref<StyleBox> node_frame_selected;
102
Ref<StyleBox> node_frame_playing;
103
Ref<StyleBox> node_frame_start;
104
Ref<StyleBox> node_frame_end;
105
106
Ref<Font> node_title_font;
107
int node_title_font_size = 0;
108
Color node_title_font_color;
109
110
Ref<Texture2D> play_node;
111
Ref<Texture2D> edit_node;
112
113
Color transition_color;
114
Color transition_disabled_color;
115
Color transition_icon_color;
116
Color transition_icon_disabled_color;
117
Color highlight_color;
118
Color highlight_disabled_color;
119
Color focus_color;
120
Color guideline_color;
121
122
Ref<Texture2D> transition_icons[6]{};
123
124
Color playback_color;
125
Color playback_background_color;
126
} theme_cache;
127
128
bool updating = false;
129
130
static AnimationNodeStateMachineEditor *singleton;
131
132
void _state_machine_gui_input(const Ref<InputEvent> &p_event);
133
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);
134
135
void _state_machine_draw();
136
137
void _state_machine_pos_draw_individual(const String &p_name, float p_ratio);
138
void _state_machine_pos_draw_all();
139
140
void _update_graph();
141
142
PopupMenu *menu = nullptr;
143
PopupMenu *connect_menu = nullptr;
144
PopupMenu *state_machine_menu = nullptr;
145
PopupMenu *end_menu = nullptr;
146
PopupMenu *animations_menu = nullptr;
147
Vector<String> animations_to_add;
148
Vector<String> nodes_to_connect;
149
150
Vector2 add_node_pos;
151
152
bool box_selecting = false;
153
Point2 box_selecting_from;
154
Point2 box_selecting_to;
155
Rect2 box_selecting_rect;
156
HashSet<StringName> previous_selected;
157
158
bool dragging_selected_attempt = false;
159
bool dragging_selected = false;
160
Vector2 drag_from;
161
Vector2 drag_ofs;
162
StringName snap_x;
163
StringName snap_y;
164
165
bool connecting = false;
166
bool connection_follows_cursor = false;
167
StringName connecting_from;
168
Vector2 connecting_to;
169
StringName connecting_to_node;
170
171
void _add_menu_type(int p_index);
172
void _add_animation_type(int p_index);
173
void _connect_to(int p_index);
174
175
struct NodeRect {
176
StringName node_name;
177
Rect2 node;
178
Rect2 play;
179
Rect2 name;
180
Rect2 edit;
181
bool can_edit;
182
};
183
184
Vector<NodeRect> node_rects;
185
186
struct TransitionLine {
187
StringName from_node;
188
StringName to_node;
189
Vector2 from;
190
Vector2 to;
191
AnimationNodeStateMachineTransition::SwitchMode mode;
192
StringName advance_condition_name;
193
bool advance_condition_state = false;
194
bool disabled = false;
195
bool auto_advance = false;
196
float width = 0;
197
bool selected;
198
bool travel;
199
float fade_ratio;
200
bool hidden;
201
int transition_index;
202
bool is_across_group = false;
203
};
204
205
Vector<TransitionLine> transition_lines;
206
207
struct NodeUR {
208
StringName name;
209
Ref<AnimationNode> node;
210
Vector2 position;
211
};
212
213
struct TransitionUR {
214
StringName new_from;
215
StringName new_to;
216
StringName old_from;
217
StringName old_to;
218
Ref<AnimationNodeStateMachineTransition> transition;
219
};
220
221
StringName selected_transition_from;
222
StringName selected_transition_to;
223
int selected_transition_index = -1;
224
void _add_transition(const bool p_nested_action = false);
225
226
enum HoveredNodeArea {
227
HOVER_NODE_NONE = -1,
228
HOVER_NODE_PLAY = 0,
229
HOVER_NODE_EDIT = 1,
230
};
231
232
StringName hovered_node_name;
233
HoveredNodeArea hovered_node_area = HOVER_NODE_NONE;
234
235
String prev_name;
236
void _name_edited(const String &p_text);
237
void _name_edited_focus_out();
238
void _open_editor(const String &p_name);
239
void _scroll_changed(double);
240
241
String _get_root_playback_path(String &r_node_directory);
242
243
void _clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect);
244
void _clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect);
245
246
void _erase_selected(const bool p_nested_action = false);
247
void _update_mode();
248
void _open_menu(const Vector2 &p_position);
249
bool _create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path);
250
void _stop_connecting();
251
252
bool last_active = false;
253
StringName last_fading_from_node;
254
StringName last_current_node;
255
Vector<StringName> last_travel_path;
256
257
float fade_from_last_play_pos = 0.0f;
258
float fade_from_current_play_pos = 0.0f;
259
float fade_from_length = 0.0f;
260
261
float last_play_pos = 0.0f;
262
float current_play_pos = 0.0f;
263
float current_length = 0.0f;
264
265
float last_fading_time = 0.0f;
266
float last_fading_pos = 0.0f;
267
float fading_time = 0.0f;
268
float fading_pos = 0.0f;
269
270
float error_time = 0.0f;
271
String error_text;
272
273
EditorFileDialog *open_file = nullptr;
274
Ref<AnimationNode> file_loaded;
275
void _file_opened(const String &p_file);
276
277
enum {
278
MENU_LOAD_FILE = 1000,
279
MENU_PASTE = 1001,
280
MENU_LOAD_FILE_CONFIRM = 1002
281
};
282
283
HashSet<StringName> connected_nodes;
284
void _update_connected_nodes(const StringName &p_node);
285
286
Ref<StyleBox> _adjust_stylebox_opacity(Ref<StyleBox> p_style, float p_opacity);
287
288
protected:
289
void _notification(int p_what);
290
static void _bind_methods();
291
292
public:
293
static AnimationNodeStateMachineEditor *get_singleton() { return singleton; }
294
295
virtual bool can_edit(const Ref<AnimationNode> &p_node) override;
296
virtual void edit(const Ref<AnimationNode> &p_node) override;
297
298
virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
299
virtual String get_tooltip(const Point2 &p_pos) const override;
300
301
AnimationNodeStateMachineEditor();
302
};
303
304
class EditorAnimationMultiTransitionEdit : public RefCounted {
305
GDCLASS(EditorAnimationMultiTransitionEdit, RefCounted);
306
307
struct Transition {
308
StringName from;
309
StringName to;
310
Ref<AnimationNodeStateMachineTransition> transition;
311
};
312
313
Vector<Transition> transitions;
314
315
protected:
316
bool _set(const StringName &p_name, const Variant &p_property);
317
bool _get(const StringName &p_name, Variant &r_property) const;
318
void _get_property_list(List<PropertyInfo> *p_list) const;
319
320
public:
321
void add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition);
322
};
323
324