Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/animation/animation_bezier_editor.h
9896 views
1
/**************************************************************************/
2
/* animation_bezier_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 "animation_track_editor.h"
34
#include "core/templates/hashfuncs.h"
35
36
class ViewPanner;
37
38
class AnimationBezierTrackEdit : public Control {
39
GDCLASS(AnimationBezierTrackEdit, Control);
40
41
enum {
42
MENU_KEY_INSERT,
43
MENU_KEY_DUPLICATE,
44
MENU_KEY_CUT,
45
MENU_KEY_COPY,
46
MENU_KEY_PASTE,
47
MENU_KEY_DELETE,
48
MENU_KEY_SET_HANDLE_FREE,
49
MENU_KEY_SET_HANDLE_LINEAR,
50
MENU_KEY_SET_HANDLE_BALANCED,
51
MENU_KEY_SET_HANDLE_MIRRORED,
52
MENU_KEY_SET_HANDLE_AUTO_BALANCED,
53
MENU_KEY_SET_HANDLE_AUTO_MIRRORED,
54
};
55
56
AnimationTimelineEdit *timeline = nullptr;
57
Node *root = nullptr;
58
Control *play_position = nullptr; //separate control used to draw so updates for only position changed are much faster
59
real_t play_position_pos = 0;
60
61
Ref<Animation> animation;
62
bool read_only = false;
63
int selected_track = 0;
64
65
Vector<Rect2> view_rects;
66
67
Ref<Texture2D> bezier_icon;
68
Ref<Texture2D> bezier_handle_icon;
69
Ref<Texture2D> selected_icon;
70
71
RBMap<int, Rect2> subtracks;
72
73
enum {
74
REMOVE_ICON,
75
LOCK_ICON,
76
SOLO_ICON,
77
VISIBILITY_ICON
78
};
79
80
RBMap<int, RBMap<int, Rect2>> subtrack_icons;
81
HashSet<int> locked_tracks;
82
HashSet<int> hidden_tracks;
83
int solo_track = -1;
84
bool is_filtered = false;
85
86
float track_v_scroll = 0;
87
float track_v_scroll_max = 0;
88
89
float timeline_v_scroll = 0;
90
float timeline_v_zoom = 1;
91
92
PopupMenu *menu = nullptr;
93
94
void _zoom_changed();
95
96
void _update_locked_tracks_after(int p_track);
97
void _update_hidden_tracks_after(int p_track);
98
99
virtual void gui_input(const Ref<InputEvent> &p_event) override;
100
void _menu_selected(int p_index);
101
102
void _play_position_draw();
103
bool _is_track_displayed(int p_track_index);
104
bool _is_track_curves_displayed(int p_track_index);
105
106
Vector2 insert_at_pos;
107
108
typedef Pair<int, int> IntPair;
109
110
bool moving_selection_attempt = false;
111
bool moving_inserted_key = false;
112
Point2 moving_selection_mouse_begin;
113
IntPair select_single_attempt;
114
bool moving_selection = false;
115
int moving_selection_from_key = 0;
116
int moving_selection_from_track = 0;
117
118
Vector2 moving_selection_offset;
119
120
bool box_selecting_attempt = false;
121
bool box_selecting = false;
122
bool box_selecting_add = false;
123
Vector2 box_selection_from;
124
Vector2 box_selection_to;
125
126
Rect2 selection_rect;
127
Rect2 selection_handles_rect;
128
129
bool scaling_selection = false;
130
Vector2i scaling_selection_handles;
131
Vector2 scaling_selection_scale = Vector2(1, 1);
132
Vector2 scaling_selection_offset;
133
Point2 scaling_selection_pivot;
134
135
int moving_handle = 0; //0 no move -1 or +1 out, 2 both (drawing only)
136
int moving_handle_key = 0;
137
int moving_handle_track = 0;
138
Vector2 moving_handle_left;
139
Vector2 moving_handle_right;
140
int moving_handle_mode = 0; // value from Animation::HandleMode
141
142
struct PairHasher {
143
static _FORCE_INLINE_ uint32_t hash(const Pair<int, int> &p_value) {
144
int32_t hash = 23;
145
hash = hash * 31 * hash_one_uint64(p_value.first);
146
hash = hash * 31 * hash_one_uint64(p_value.second);
147
return hash;
148
}
149
};
150
151
HashMap<Pair<int, int>, Vector2, PairHasher> additional_moving_handle_lefts;
152
HashMap<Pair<int, int>, Vector2, PairHasher> additional_moving_handle_rights;
153
154
void _clear_selection();
155
void _clear_selection_for_anim(const Ref<Animation> &p_anim);
156
void _select_at_anim(const Ref<Animation> &p_anim, int p_track, real_t p_pos, bool p_single);
157
bool _try_select_at_ui_pos(const Point2 &p_pos, bool p_aggregate, bool p_deselectable);
158
void _change_selected_keys_handle_mode(Animation::HandleMode p_mode, bool p_auto = false);
159
160
Vector2 menu_insert_key;
161
162
struct AnimMoveRestore {
163
int track = 0;
164
double time = 0;
165
Variant key;
166
real_t transition = 0;
167
};
168
169
AnimationTrackEditor *editor = nullptr;
170
171
struct EditPoint {
172
Rect2 point_rect;
173
Rect2 in_rect;
174
Rect2 out_rect;
175
int track = 0;
176
int key = 0;
177
};
178
179
Vector<EditPoint> edit_points;
180
181
struct PairCompare {
182
bool operator()(const IntPair &lh, const IntPair &rh) {
183
if (lh.first == rh.first) {
184
return lh.second < rh.second;
185
} else {
186
return lh.first < rh.first;
187
}
188
}
189
};
190
191
typedef RBSet<IntPair, PairCompare> SelectionSet;
192
193
SelectionSet selection;
194
195
Ref<ViewPanner> panner;
196
void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
197
void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
198
199
void _draw_line_clipped(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, int p_clip_left, int p_clip_right);
200
void _draw_track(int p_track, const Color &p_color);
201
202
float _bezier_h_to_pixel(float p_h);
203
void _zoom_vertically(real_t p_minimum_value, real_t p_maximum_value);
204
205
protected:
206
static void _bind_methods();
207
void _notification(int p_what);
208
209
public:
210
static float get_bezier_key_value(Array p_bezier_key_array);
211
212
virtual String get_tooltip(const Point2 &p_pos) const override;
213
214
Ref<Animation> get_animation() const;
215
216
void set_animation_and_track(const Ref<Animation> &p_animation, int p_track, bool p_read_only);
217
virtual Size2 get_minimum_size() const override;
218
virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
219
220
void set_timeline(AnimationTimelineEdit *p_timeline);
221
void set_editor(AnimationTrackEditor *p_editor);
222
void set_root(Node *p_root);
223
void set_filtered(bool p_filtered);
224
void auto_fit_vertically();
225
226
void set_play_position(real_t p_pos);
227
void update_play_position();
228
229
void duplicate_selected_keys(real_t p_ofs, bool p_ofs_valid);
230
void copy_selected_keys(bool p_cut);
231
void paste_keys(real_t p_ofs, bool p_ofs_valid);
232
void delete_selection();
233
234
void _bezier_track_insert_key_at_anim(const Ref<Animation> &p_anim, int p_track, double p_time, real_t p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle, const Animation::HandleMode p_handle_mode, Animation::HandleSetMode p_handle_set_mode = Animation::HANDLE_SET_MODE_NONE);
235
236
AnimationBezierTrackEdit();
237
};
238
239