1 | /**************************************************************************/ |
2 | /* animation_bezier_editor.cpp */ |
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 | #include "animation_bezier_editor.h" |
32 | |
33 | #include "editor/editor_node.h" |
34 | #include "editor/editor_scale.h" |
35 | #include "editor/editor_settings.h" |
36 | #include "editor/editor_string_names.h" |
37 | #include "editor/editor_undo_redo_manager.h" |
38 | #include "scene/gui/view_panner.h" |
39 | #include "scene/resources/text_line.h" |
40 | |
41 | #include <limits.h> |
42 | |
43 | float AnimationBezierTrackEdit::_bezier_h_to_pixel(float p_h) { |
44 | float h = p_h; |
45 | h = (h - v_scroll) / v_zoom; |
46 | h = (get_size().height / 2.0) - h; |
47 | return h; |
48 | } |
49 | |
50 | void AnimationBezierTrackEdit::_draw_track(int p_track, const Color &p_color) { |
51 | float scale = timeline->get_zoom_scale(); |
52 | |
53 | int limit = timeline->get_name_limit(); |
54 | int right_limit = get_size().width; |
55 | |
56 | //selection may have altered the order of keys |
57 | RBMap<real_t, int> key_order; |
58 | |
59 | for (int i = 0; i < animation->track_get_key_count(p_track); i++) { |
60 | real_t ofs = animation->track_get_key_time(p_track, i); |
61 | if (moving_selection && selection.has(IntPair(p_track, i))) { |
62 | ofs += moving_selection_offset.x; |
63 | } |
64 | |
65 | key_order[ofs] = i; |
66 | } |
67 | |
68 | for (RBMap<real_t, int>::Element *E = key_order.front(); E; E = E->next()) { |
69 | int i = E->get(); |
70 | |
71 | if (!E->next()) { |
72 | break; |
73 | } |
74 | |
75 | int i_n = E->next()->get(); |
76 | |
77 | float offset = animation->track_get_key_time(p_track, i); |
78 | float height = animation->bezier_track_get_key_value(p_track, i); |
79 | Vector2 out_handle = animation->bezier_track_get_key_out_handle(p_track, i); |
80 | if (p_track == moving_handle_track && (moving_handle == -1 || moving_handle == 1) && moving_handle_key == i) { |
81 | out_handle = moving_handle_right; |
82 | } |
83 | |
84 | if (moving_selection && selection.has(IntPair(p_track, i))) { |
85 | offset += moving_selection_offset.x; |
86 | height += moving_selection_offset.y; |
87 | } |
88 | |
89 | out_handle += Vector2(offset, height); |
90 | |
91 | float offset_n = animation->track_get_key_time(p_track, i_n); |
92 | float height_n = animation->bezier_track_get_key_value(p_track, i_n); |
93 | Vector2 in_handle = animation->bezier_track_get_key_in_handle(p_track, i_n); |
94 | if (p_track == moving_handle_track && (moving_handle == -1 || moving_handle == 1) && moving_handle_key == i_n) { |
95 | in_handle = moving_handle_left; |
96 | } |
97 | |
98 | if (moving_selection && selection.has(IntPair(p_track, i_n))) { |
99 | offset_n += moving_selection_offset.x; |
100 | height_n += moving_selection_offset.y; |
101 | } |
102 | |
103 | in_handle += Vector2(offset_n, height_n); |
104 | |
105 | Vector2 start(offset, height); |
106 | Vector2 end(offset_n, height_n); |
107 | |
108 | int from_x = (offset - timeline->get_value()) * scale + limit; |
109 | int point_start = from_x; |
110 | int to_x = (offset_n - timeline->get_value()) * scale + limit; |
111 | int point_end = to_x; |
112 | |
113 | if (from_x > right_limit) { //not visible |
114 | continue; |
115 | } |
116 | |
117 | if (to_x < limit) { //not visible |
118 | continue; |
119 | } |
120 | |
121 | from_x = MAX(from_x, limit); |
122 | to_x = MIN(to_x, right_limit); |
123 | |
124 | Vector<Vector2> lines; |
125 | |
126 | Vector2 prev_pos; |
127 | |
128 | for (int j = from_x; j <= to_x; j++) { |
129 | float t = (j - limit) / scale + timeline->get_value(); |
130 | |
131 | float h; |
132 | |
133 | if (j == point_end) { |
134 | h = end.y; //make sure it always connects |
135 | } else if (j == point_start) { |
136 | h = start.y; //make sure it always connects |
137 | } else { //custom interpolation, used because it needs to show paths affected by moving the selection or handles |
138 | int iterations = 10; |
139 | float low = 0; |
140 | float high = 1; |
141 | |
142 | //narrow high and low as much as possible |
143 | for (int k = 0; k < iterations; k++) { |
144 | float middle = (low + high) / 2.0; |
145 | |
146 | Vector2 interp = start.bezier_interpolate(out_handle, in_handle, end, middle); |
147 | |
148 | if (interp.x < t) { |
149 | low = middle; |
150 | } else { |
151 | high = middle; |
152 | } |
153 | } |
154 | |
155 | //interpolate the result: |
156 | Vector2 low_pos = start.bezier_interpolate(out_handle, in_handle, end, low); |
157 | Vector2 high_pos = start.bezier_interpolate(out_handle, in_handle, end, high); |
158 | |
159 | float c = (t - low_pos.x) / (high_pos.x - low_pos.x); |
160 | |
161 | h = low_pos.lerp(high_pos, c).y; |
162 | } |
163 | |
164 | h = _bezier_h_to_pixel(h); |
165 | |
166 | Vector2 pos(j, h); |
167 | |
168 | if (j > from_x) { |
169 | lines.push_back(prev_pos); |
170 | lines.push_back(pos); |
171 | } |
172 | prev_pos = pos; |
173 | } |
174 | |
175 | if (lines.size() >= 2) { |
176 | draw_multiline(lines, p_color, Math::round(EDSCALE)); |
177 | } |
178 | } |
179 | } |
180 | |
181 | void AnimationBezierTrackEdit::_draw_line_clipped(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, int p_clip_left, int p_clip_right) { |
182 | Vector2 from = p_from; |
183 | Vector2 to = p_to; |
184 | |
185 | if (from.x == to.x && from.y == to.y) { |
186 | return; |
187 | } |
188 | if (to.x < from.x) { |
189 | SWAP(to, from); |
190 | } |
191 | |
192 | if (to.x < p_clip_left) { |
193 | return; |
194 | } |
195 | |
196 | if (from.x > p_clip_right) { |
197 | return; |
198 | } |
199 | |
200 | if (to.x > p_clip_right) { |
201 | float c = (p_clip_right - from.x) / (to.x - from.x); |
202 | to = from.lerp(to, c); |
203 | } |
204 | |
205 | if (from.x < p_clip_left) { |
206 | float c = (p_clip_left - from.x) / (to.x - from.x); |
207 | from = from.lerp(to, c); |
208 | } |
209 | |
210 | draw_line(from, to, p_color, Math::round(EDSCALE)); |
211 | } |
212 | |
213 | void AnimationBezierTrackEdit::_notification(int p_what) { |
214 | switch (p_what) { |
215 | case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: { |
216 | panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/animation_editors_panning_scheme" ).operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view" ), bool(EDITOR_GET("editors/panning/simple_panning" ))); |
217 | } break; |
218 | |
219 | case NOTIFICATION_ENTER_TREE: { |
220 | panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/animation_editors_panning_scheme" ).operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view" ), bool(EDITOR_GET("editors/panning/simple_panning" ))); |
221 | [[fallthrough]]; |
222 | } |
223 | case NOTIFICATION_THEME_CHANGED: { |
224 | bezier_icon = get_editor_theme_icon(SNAME("KeyBezierPoint" )); |
225 | bezier_handle_icon = get_editor_theme_icon(SNAME("KeyBezierHandle" )); |
226 | selected_icon = get_editor_theme_icon(SNAME("KeyBezierSelected" )); |
227 | } break; |
228 | |
229 | case NOTIFICATION_DRAW: { |
230 | if (animation.is_null()) { |
231 | return; |
232 | } |
233 | |
234 | int limit = timeline->get_name_limit(); |
235 | |
236 | if (has_focus()) { |
237 | Color accent = get_theme_color(SNAME("accent_color" ), EditorStringName(Editor)); |
238 | accent.a *= 0.7; |
239 | draw_rect(Rect2(Point2(), get_size()), accent, false, Math::round(EDSCALE)); |
240 | } |
241 | |
242 | Ref<Font> font = get_theme_font(SNAME("font" ), SNAME("Label" )); |
243 | int font_size = get_theme_font_size(SNAME("font_size" ), SNAME("Label" )); |
244 | Color color = get_theme_color(SNAME("font_color" ), SNAME("Label" )); |
245 | int hsep = get_theme_constant(SNAME("h_separation" ), SNAME("ItemList" )); |
246 | int vsep = get_theme_constant(SNAME("v_separation" ), SNAME("ItemList" )); |
247 | Color linecolor = color; |
248 | linecolor.a = 0.2; |
249 | |
250 | draw_line(Point2(limit, 0), Point2(limit, get_size().height), linecolor, Math::round(EDSCALE)); |
251 | |
252 | int right_limit = get_size().width; |
253 | |
254 | int vofs = vsep; |
255 | int margin = 0; |
256 | |
257 | RBMap<int, Color> subtrack_colors; |
258 | Color selected_track_color; |
259 | subtracks.clear(); |
260 | subtrack_icons.clear(); |
261 | |
262 | RBMap<String, Vector<int>> track_indices; |
263 | int track_count = animation->get_track_count(); |
264 | for (int i = 0; i < track_count; ++i) { |
265 | if (animation->track_get_type(i) != Animation::TrackType::TYPE_BEZIER) { |
266 | continue; |
267 | } |
268 | |
269 | String base_path = animation->track_get_path(i); |
270 | if (is_filtered) { |
271 | if (root && root->has_node(base_path)) { |
272 | Node *node = root->get_node(base_path); |
273 | if (!node) { |
274 | continue; // No node, no filter. |
275 | } |
276 | if (!EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) { |
277 | continue; // Skip track due to not selected. |
278 | } |
279 | } |
280 | } |
281 | |
282 | int end = base_path.find(":" ); |
283 | if (end != -1) { |
284 | base_path = base_path.substr(0, end + 1); |
285 | } |
286 | Vector<int> indices = track_indices.has(base_path) ? track_indices[base_path] : Vector<int>(); |
287 | indices.push_back(i); |
288 | track_indices[base_path] = indices; |
289 | } |
290 | |
291 | for (const KeyValue<String, Vector<int>> &E : track_indices) { |
292 | String base_path = E.key; |
293 | |
294 | Vector<int> tracks = E.value; |
295 | |
296 | // NAMES AND ICON |
297 | { |
298 | NodePath path = animation->track_get_path(tracks[0]); |
299 | |
300 | Node *node = nullptr; |
301 | |
302 | if (root && root->has_node(path)) { |
303 | node = root->get_node(path); |
304 | } |
305 | |
306 | String text; |
307 | |
308 | if (node) { |
309 | int ofs = 0; |
310 | |
311 | Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(node, "Node" ); |
312 | |
313 | text = node->get_name(); |
314 | ofs += hsep; |
315 | |
316 | TextLine text_buf = TextLine(text, font, font_size); |
317 | text_buf.set_width(limit - ofs - icon->get_width() - hsep); |
318 | |
319 | int h = MAX(text_buf.get_size().y, icon->get_height()); |
320 | |
321 | draw_texture(icon, Point2(ofs, vofs + int(h - icon->get_height()) / 2.0)); |
322 | ofs += icon->get_width(); |
323 | |
324 | margin = icon->get_width(); |
325 | |
326 | Vector2 string_pos = Point2(ofs, vofs); |
327 | string_pos = string_pos.floor(); |
328 | text_buf.draw(get_canvas_item(), string_pos, color); |
329 | |
330 | vofs += h + vsep; |
331 | } |
332 | } |
333 | |
334 | Color dc = get_theme_color(SNAME("disabled_font_color" ), EditorStringName(Editor)); |
335 | |
336 | Ref<Texture2D> remove = get_editor_theme_icon(SNAME("Remove" )); |
337 | float remove_hpos = limit - hsep - remove->get_width(); |
338 | |
339 | Ref<Texture2D> lock = get_editor_theme_icon(SNAME("Lock" )); |
340 | Ref<Texture2D> unlock = get_editor_theme_icon(SNAME("Unlock" )); |
341 | float lock_hpos = remove_hpos - hsep - lock->get_width(); |
342 | |
343 | Ref<Texture2D> visibility_visible = get_editor_theme_icon(SNAME("GuiVisibilityVisible" )); |
344 | Ref<Texture2D> visibility_hidden = get_editor_theme_icon(SNAME("GuiVisibilityHidden" )); |
345 | float visibility_hpos = lock_hpos - hsep - visibility_visible->get_width(); |
346 | |
347 | Ref<Texture2D> solo = get_editor_theme_icon(SNAME("AudioBusSolo" )); |
348 | float solo_hpos = visibility_hpos - hsep - solo->get_width(); |
349 | |
350 | float buttons_width = remove->get_width() + lock->get_width() + visibility_visible->get_width() + solo->get_width() + hsep * 3; |
351 | |
352 | for (int i = 0; i < tracks.size(); ++i) { |
353 | // RELATED TRACKS TITLES |
354 | |
355 | int current_track = tracks[i]; |
356 | |
357 | String path = animation->track_get_path(current_track); |
358 | path = path.replace_first(base_path, "" ); |
359 | |
360 | Color cc = color; |
361 | TextLine text_buf = TextLine(path, font, font_size); |
362 | text_buf.set_width(limit - margin - buttons_width); |
363 | |
364 | Rect2 rect = Rect2(margin, vofs, solo_hpos - hsep - solo->get_width(), text_buf.get_size().y + vsep); |
365 | |
366 | cc.a *= 0.7; |
367 | float h; |
368 | if (path.ends_with(":x" )) { |
369 | h = 0; |
370 | } else if (path.ends_with(":y" )) { |
371 | h = 0.33f; |
372 | } else if (path.ends_with(":z" )) { |
373 | h = 0.66f; |
374 | } else { |
375 | uint32_t hash = path.hash(); |
376 | hash = ((hash >> 16) ^ hash) * 0x45d9f3b; |
377 | hash = ((hash >> 16) ^ hash) * 0x45d9f3b; |
378 | hash = (hash >> 16) ^ hash; |
379 | h = (hash % 65535) / 65536.0; |
380 | } |
381 | |
382 | if (current_track != selected_track) { |
383 | Color track_color; |
384 | if (locked_tracks.has(current_track)) { |
385 | track_color.set_hsv(h, 0, 0.4); |
386 | } else { |
387 | track_color.set_hsv(h, 0.2, 0.8); |
388 | } |
389 | track_color.a = 0.5; |
390 | draw_rect(Rect2(0, vofs, margin - hsep, text_buf.get_size().y * 0.8), track_color); |
391 | subtrack_colors[current_track] = track_color; |
392 | |
393 | subtracks[current_track] = rect; |
394 | } else { |
395 | Color ac = get_theme_color(SNAME("accent_color" ), EditorStringName(Editor)); |
396 | ac.a = 0.5; |
397 | draw_rect(rect, ac); |
398 | if (locked_tracks.has(selected_track)) { |
399 | selected_track_color.set_hsv(h, 0.0, 0.4); |
400 | } else { |
401 | selected_track_color.set_hsv(h, 0.8, 0.8); |
402 | } |
403 | } |
404 | |
405 | Vector2 string_pos = Point2(margin, vofs); |
406 | text_buf.draw(get_canvas_item(), string_pos, cc); |
407 | |
408 | float icon_start_height = vofs + rect.size.y / 2.0; |
409 | Rect2 remove_rect = Rect2(remove_hpos, icon_start_height - remove->get_height() / 2.0, remove->get_width(), remove->get_height()); |
410 | if (read_only) { |
411 | draw_texture(remove, remove_rect.position, dc); |
412 | } else { |
413 | draw_texture(remove, remove_rect.position); |
414 | } |
415 | |
416 | Rect2 lock_rect = Rect2(lock_hpos, icon_start_height - lock->get_height() / 2.0, lock->get_width(), lock->get_height()); |
417 | if (locked_tracks.has(current_track)) { |
418 | draw_texture(lock, lock_rect.position); |
419 | } else { |
420 | draw_texture(unlock, lock_rect.position); |
421 | } |
422 | |
423 | Rect2 visible_rect = Rect2(visibility_hpos, icon_start_height - visibility_visible->get_height() / 2.0, visibility_visible->get_width(), visibility_visible->get_height()); |
424 | if (hidden_tracks.has(current_track)) { |
425 | draw_texture(visibility_hidden, visible_rect.position); |
426 | } else { |
427 | draw_texture(visibility_visible, visible_rect.position); |
428 | } |
429 | |
430 | Rect2 solo_rect = Rect2(solo_hpos, icon_start_height - solo->get_height() / 2.0, solo->get_width(), solo->get_height()); |
431 | draw_texture(solo, solo_rect.position); |
432 | |
433 | RBMap<int, Rect2> track_icons; |
434 | track_icons[REMOVE_ICON] = remove_rect; |
435 | track_icons[LOCK_ICON] = lock_rect; |
436 | track_icons[VISIBILITY_ICON] = visible_rect; |
437 | track_icons[SOLO_ICON] = solo_rect; |
438 | |
439 | subtrack_icons[current_track] = track_icons; |
440 | |
441 | vofs += text_buf.get_size().y + vsep; |
442 | } |
443 | } |
444 | |
445 | Color accent = get_theme_color(SNAME("accent_color" ), EditorStringName(Editor)); |
446 | |
447 | { //guides |
448 | float min_left_scale = font->get_height(font_size) + vsep; |
449 | |
450 | float scale = (min_left_scale * 2) * v_zoom; |
451 | float step = Math::pow(10.0, Math::round(Math::log(scale / 5.0) / Math::log(10.0))) * 5.0; |
452 | scale = Math::snapped(scale, step); |
453 | |
454 | while (scale / v_zoom < min_left_scale * 2) { |
455 | scale += step; |
456 | } |
457 | |
458 | bool first = true; |
459 | int prev_iv = 0; |
460 | for (int i = font->get_height(font_size); i < get_size().height; i++) { |
461 | float ofs = get_size().height / 2.0 - i; |
462 | ofs *= v_zoom; |
463 | ofs += v_scroll; |
464 | |
465 | int iv = int(ofs / scale); |
466 | if (ofs < 0) { |
467 | iv -= 1; |
468 | } |
469 | if (!first && iv != prev_iv) { |
470 | Color lc = linecolor; |
471 | lc.a *= 0.5; |
472 | draw_line(Point2(limit, i), Point2(right_limit, i), lc, Math::round(EDSCALE)); |
473 | Color c = color; |
474 | c.a *= 0.5; |
475 | draw_string(font, Point2(limit + 8, i - 2), TS->format_number(rtos(Math::snapped((iv + 1) * scale, step))), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, c); |
476 | } |
477 | |
478 | first = false; |
479 | prev_iv = iv; |
480 | } |
481 | } |
482 | |
483 | { //draw OTHER curves |
484 | |
485 | float scale = timeline->get_zoom_scale(); |
486 | Ref<Texture2D> point = get_editor_theme_icon(SNAME("KeyValue" )); |
487 | for (const KeyValue<int, Color> &E : subtrack_colors) { |
488 | if (hidden_tracks.has(E.key)) { |
489 | continue; |
490 | } |
491 | _draw_track(E.key, E.value); |
492 | |
493 | for (int i = 0; i < animation->track_get_key_count(E.key); i++) { |
494 | float offset = animation->track_get_key_time(E.key, i); |
495 | float value = animation->bezier_track_get_key_value(E.key, i); |
496 | |
497 | Vector2 pos((offset - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value)); |
498 | |
499 | if (pos.x >= limit && pos.x <= right_limit) { |
500 | draw_texture(point, pos - point->get_size() / 2.0, E.value); |
501 | } |
502 | } |
503 | } |
504 | |
505 | if (track_count > 0 && !hidden_tracks.has(selected_track)) { |
506 | //draw edited curve |
507 | _draw_track(selected_track, selected_track_color); |
508 | } |
509 | } |
510 | |
511 | //draw editor handles |
512 | { |
513 | edit_points.clear(); |
514 | float scale = timeline->get_zoom_scale(); |
515 | |
516 | for (int i = 0; i < track_count; ++i) { |
517 | if (animation->track_get_type(i) != Animation::TrackType::TYPE_BEZIER || hidden_tracks.has(i)) { |
518 | continue; |
519 | } |
520 | |
521 | if (hidden_tracks.has(i) || locked_tracks.has(i)) { |
522 | continue; |
523 | } |
524 | |
525 | int key_count = animation->track_get_key_count(i); |
526 | String path = animation->track_get_path(i); |
527 | |
528 | if (is_filtered) { |
529 | if (root && root->has_node(path)) { |
530 | Node *node = root->get_node(path); |
531 | if (!node) { |
532 | continue; // No node, no filter. |
533 | } |
534 | if (!EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) { |
535 | continue; // Skip track due to not selected. |
536 | } |
537 | } |
538 | } |
539 | |
540 | for (int j = 0; j < key_count; ++j) { |
541 | float offset = animation->track_get_key_time(i, j); |
542 | float value = animation->bezier_track_get_key_value(i, j); |
543 | |
544 | if (moving_selection && selection.has(IntPair(i, j))) { |
545 | offset += moving_selection_offset.x; |
546 | value += moving_selection_offset.y; |
547 | } |
548 | |
549 | Vector2 pos((offset - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value)); |
550 | |
551 | Vector2 in_vec = animation->bezier_track_get_key_in_handle(i, j); |
552 | |
553 | if ((moving_handle == 1 || moving_handle == -1) && moving_handle_track == i && moving_handle_key == j) { |
554 | in_vec = moving_handle_left; |
555 | } |
556 | Vector2 pos_in(((offset + in_vec.x) - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value + in_vec.y)); |
557 | |
558 | Vector2 out_vec = animation->bezier_track_get_key_out_handle(i, j); |
559 | |
560 | if ((moving_handle == 1 || moving_handle == -1) && moving_handle_track == i && moving_handle_key == j) { |
561 | out_vec = moving_handle_right; |
562 | } |
563 | |
564 | Vector2 pos_out(((offset + out_vec.x) - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value + out_vec.y)); |
565 | |
566 | if (i == selected_track || selection.has(IntPair(i, j))) { |
567 | _draw_line_clipped(pos, pos_in, accent, limit, right_limit); |
568 | _draw_line_clipped(pos, pos_out, accent, limit, right_limit); |
569 | } |
570 | |
571 | EditPoint ep; |
572 | ep.track = i; |
573 | ep.key = j; |
574 | if (pos.x >= limit && pos.x <= right_limit) { |
575 | ep.point_rect.position = (pos - bezier_icon->get_size() / 2.0).floor(); |
576 | ep.point_rect.size = bezier_icon->get_size(); |
577 | if (selection.has(IntPair(i, j))) { |
578 | draw_texture(selected_icon, ep.point_rect.position); |
579 | draw_string(font, ep.point_rect.position + Vector2(8, -font->get_height(font_size) - 8), TTR("Time:" ) + " " + TS->format_number(rtos(Math::snapped(offset, 0.0001))), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, accent); |
580 | draw_string(font, ep.point_rect.position + Vector2(8, -8), TTR("Value:" ) + " " + TS->format_number(rtos(Math::snapped(value, 0.001))), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, accent); |
581 | } else { |
582 | Color track_color = Color(1, 1, 1, 1); |
583 | if (i != selected_track) { |
584 | track_color = subtrack_colors[i]; |
585 | } |
586 | draw_texture(bezier_icon, ep.point_rect.position, track_color); |
587 | } |
588 | ep.point_rect = ep.point_rect.grow(ep.point_rect.size.width * 0.5); |
589 | } |
590 | ep.point_rect = ep.point_rect.grow(ep.point_rect.size.width * 0.5); |
591 | |
592 | if (i == selected_track || selection.has(IntPair(i, j))) { |
593 | if (animation->bezier_track_get_key_handle_mode(i, j) != Animation::HANDLE_MODE_LINEAR) { |
594 | if (pos_in.x >= limit && pos_in.x <= right_limit) { |
595 | ep.in_rect.position = (pos_in - bezier_handle_icon->get_size() / 2.0).floor(); |
596 | ep.in_rect.size = bezier_handle_icon->get_size(); |
597 | draw_texture(bezier_handle_icon, ep.in_rect.position); |
598 | ep.in_rect = ep.in_rect.grow(ep.in_rect.size.width * 0.5); |
599 | } |
600 | if (pos_out.x >= limit && pos_out.x <= right_limit) { |
601 | ep.out_rect.position = (pos_out - bezier_handle_icon->get_size() / 2.0).floor(); |
602 | ep.out_rect.size = bezier_handle_icon->get_size(); |
603 | draw_texture(bezier_handle_icon, ep.out_rect.position); |
604 | ep.out_rect = ep.out_rect.grow(ep.out_rect.size.width * 0.5); |
605 | } |
606 | } |
607 | } |
608 | if (!locked_tracks.has(i)) { |
609 | edit_points.push_back(ep); |
610 | } |
611 | } |
612 | } |
613 | |
614 | for (int i = 0; i < edit_points.size(); ++i) { |
615 | if (edit_points[i].track == selected_track) { |
616 | EditPoint ep = edit_points[i]; |
617 | edit_points.remove_at(i); |
618 | edit_points.insert(0, ep); |
619 | } |
620 | } |
621 | } |
622 | |
623 | if (box_selecting) { |
624 | Vector2 bs_from = box_selection_from; |
625 | Vector2 bs_to = box_selection_to; |
626 | if (bs_from.x > bs_to.x) { |
627 | SWAP(bs_from.x, bs_to.x); |
628 | } |
629 | if (bs_from.y > bs_to.y) { |
630 | SWAP(bs_from.y, bs_to.y); |
631 | } |
632 | draw_rect( |
633 | Rect2(bs_from, bs_to - bs_from), |
634 | get_theme_color(SNAME("box_selection_fill_color" ), EditorStringName(Editor))); |
635 | draw_rect( |
636 | Rect2(bs_from, bs_to - bs_from), |
637 | get_theme_color(SNAME("box_selection_stroke_color" ), EditorStringName(Editor)), |
638 | false, |
639 | Math::round(EDSCALE)); |
640 | } |
641 | } break; |
642 | } |
643 | } |
644 | |
645 | Ref<Animation> AnimationBezierTrackEdit::get_animation() const { |
646 | return animation; |
647 | } |
648 | |
649 | void AnimationBezierTrackEdit::set_animation_and_track(const Ref<Animation> &p_animation, int p_track, bool p_read_only) { |
650 | animation = p_animation; |
651 | read_only = p_read_only; |
652 | selected_track = p_track; |
653 | queue_redraw(); |
654 | } |
655 | |
656 | Size2 AnimationBezierTrackEdit::get_minimum_size() const { |
657 | return Vector2(1, 1); |
658 | } |
659 | |
660 | void AnimationBezierTrackEdit::set_timeline(AnimationTimelineEdit *p_timeline) { |
661 | timeline = p_timeline; |
662 | timeline->connect("zoom_changed" , callable_mp(this, &AnimationBezierTrackEdit::_zoom_changed)); |
663 | timeline->connect("name_limit_changed" , callable_mp(this, &AnimationBezierTrackEdit::_zoom_changed)); |
664 | } |
665 | |
666 | void AnimationBezierTrackEdit::set_editor(AnimationTrackEditor *p_editor) { |
667 | editor = p_editor; |
668 | connect("clear_selection" , callable_mp(editor, &AnimationTrackEditor::_clear_selection).bind(false)); |
669 | connect("select_key" , callable_mp(editor, &AnimationTrackEditor::_key_selected), CONNECT_DEFERRED); |
670 | } |
671 | |
672 | void AnimationBezierTrackEdit::_play_position_draw() { |
673 | if (!animation.is_valid() || play_position_pos < 0) { |
674 | return; |
675 | } |
676 | |
677 | float scale = timeline->get_zoom_scale(); |
678 | int h = get_size().height; |
679 | |
680 | int limit = timeline->get_name_limit(); |
681 | |
682 | int px = (-timeline->get_value() + play_position_pos) * scale + limit; |
683 | |
684 | if (px >= limit && px < (get_size().width)) { |
685 | Color color = get_theme_color(SNAME("accent_color" ), EditorStringName(Editor)); |
686 | play_position->draw_line(Point2(px, 0), Point2(px, h), color, Math::round(2 * EDSCALE)); |
687 | } |
688 | } |
689 | |
690 | void AnimationBezierTrackEdit::set_play_position(real_t p_pos) { |
691 | play_position_pos = p_pos; |
692 | play_position->queue_redraw(); |
693 | } |
694 | |
695 | void AnimationBezierTrackEdit::update_play_position() { |
696 | play_position->queue_redraw(); |
697 | } |
698 | |
699 | void AnimationBezierTrackEdit::set_root(Node *p_root) { |
700 | root = p_root; |
701 | } |
702 | |
703 | void AnimationBezierTrackEdit::set_filtered(bool p_filtered) { |
704 | is_filtered = p_filtered; |
705 | if (animation == nullptr) { |
706 | return; |
707 | } |
708 | String base_path = animation->track_get_path(selected_track); |
709 | if (is_filtered) { |
710 | if (root && root->has_node(base_path)) { |
711 | Node *node = root->get_node(base_path); |
712 | if (!node || !EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) { |
713 | for (int i = 0; i < animation->get_track_count(); ++i) { |
714 | if (animation->track_get_type(i) != Animation::TrackType::TYPE_BEZIER) { |
715 | continue; |
716 | } |
717 | |
718 | base_path = animation->track_get_path(i); |
719 | if (root && root->has_node(base_path)) { |
720 | node = root->get_node(base_path); |
721 | if (!node) { |
722 | continue; // No node, no filter. |
723 | } |
724 | if (!EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) { |
725 | continue; // Skip track due to not selected. |
726 | } |
727 | |
728 | set_animation_and_track(animation, i, read_only); |
729 | break; |
730 | } |
731 | } |
732 | } |
733 | } |
734 | } |
735 | queue_redraw(); |
736 | } |
737 | |
738 | void AnimationBezierTrackEdit::_zoom_changed() { |
739 | queue_redraw(); |
740 | play_position->queue_redraw(); |
741 | } |
742 | |
743 | void AnimationBezierTrackEdit::_update_locked_tracks_after(int p_track) { |
744 | if (locked_tracks.has(p_track)) { |
745 | locked_tracks.erase(p_track); |
746 | } |
747 | |
748 | Vector<int> updated_locked_tracks; |
749 | for (const int &E : locked_tracks) { |
750 | updated_locked_tracks.push_back(E); |
751 | } |
752 | locked_tracks.clear(); |
753 | for (int i = 0; i < updated_locked_tracks.size(); ++i) { |
754 | if (updated_locked_tracks[i] > p_track) { |
755 | locked_tracks.insert(updated_locked_tracks[i] - 1); |
756 | } else { |
757 | locked_tracks.insert(updated_locked_tracks[i]); |
758 | } |
759 | } |
760 | } |
761 | |
762 | void AnimationBezierTrackEdit::_update_hidden_tracks_after(int p_track) { |
763 | if (hidden_tracks.has(p_track)) { |
764 | hidden_tracks.erase(p_track); |
765 | } |
766 | |
767 | Vector<int> updated_hidden_tracks; |
768 | for (const int &E : hidden_tracks) { |
769 | updated_hidden_tracks.push_back(E); |
770 | } |
771 | hidden_tracks.clear(); |
772 | for (int i = 0; i < updated_hidden_tracks.size(); ++i) { |
773 | if (updated_hidden_tracks[i] > p_track) { |
774 | hidden_tracks.insert(updated_hidden_tracks[i] - 1); |
775 | } else { |
776 | hidden_tracks.insert(updated_hidden_tracks[i]); |
777 | } |
778 | } |
779 | } |
780 | |
781 | String AnimationBezierTrackEdit::get_tooltip(const Point2 &p_pos) const { |
782 | return Control::get_tooltip(p_pos); |
783 | } |
784 | |
785 | void AnimationBezierTrackEdit::_clear_selection() { |
786 | selection.clear(); |
787 | emit_signal(SNAME("clear_selection" )); |
788 | queue_redraw(); |
789 | } |
790 | |
791 | void AnimationBezierTrackEdit::_change_selected_keys_handle_mode(Animation::HandleMode p_mode, bool p_auto) { |
792 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
793 | undo_redo->create_action(TTR("Update Selected Key Handles" )); |
794 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
795 | const IntPair track_key_pair = E->get(); |
796 | undo_redo->add_undo_method(editor, "_bezier_track_set_key_handle_mode" , animation.ptr(), track_key_pair.first, track_key_pair.second, animation->bezier_track_get_key_handle_mode(track_key_pair.first, track_key_pair.second), Animation::HANDLE_SET_MODE_NONE); |
797 | undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle" , track_key_pair.first, track_key_pair.second, animation->bezier_track_get_key_in_handle(track_key_pair.first, track_key_pair.second)); |
798 | undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle" , track_key_pair.first, track_key_pair.second, animation->bezier_track_get_key_out_handle(track_key_pair.first, track_key_pair.second)); |
799 | undo_redo->add_do_method(editor, "_bezier_track_set_key_handle_mode" , animation.ptr(), track_key_pair.first, track_key_pair.second, p_mode, p_auto ? Animation::HANDLE_SET_MODE_AUTO : Animation::HANDLE_SET_MODE_RESET); |
800 | } |
801 | undo_redo->commit_action(); |
802 | } |
803 | |
804 | void AnimationBezierTrackEdit::_clear_selection_for_anim(const Ref<Animation> &p_anim) { |
805 | if (!(animation == p_anim)) { |
806 | return; |
807 | } |
808 | _clear_selection(); |
809 | } |
810 | |
811 | void AnimationBezierTrackEdit::_select_at_anim(const Ref<Animation> &p_anim, int p_track, real_t p_pos) { |
812 | if (!(animation == p_anim)) { |
813 | return; |
814 | } |
815 | |
816 | int idx = animation->track_find_key(p_track, p_pos, Animation::FIND_MODE_APPROX); |
817 | ERR_FAIL_COND(idx < 0); |
818 | |
819 | selection.insert(IntPair(p_track, idx)); |
820 | emit_signal(SNAME("select_key" ), idx, true, p_track); |
821 | queue_redraw(); |
822 | } |
823 | |
824 | void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) { |
825 | ERR_FAIL_COND(p_event.is_null()); |
826 | |
827 | if (panner->gui_input(p_event)) { |
828 | accept_event(); |
829 | return; |
830 | } |
831 | |
832 | if (p_event->is_pressed()) { |
833 | if (ED_GET_SHORTCUT("animation_editor/duplicate_selection" )->matches_event(p_event)) { |
834 | if (!read_only) { |
835 | duplicate_selection(); |
836 | } |
837 | accept_event(); |
838 | } |
839 | |
840 | if (ED_GET_SHORTCUT("animation_editor/delete_selection" )->matches_event(p_event)) { |
841 | if (!read_only) { |
842 | delete_selection(); |
843 | } |
844 | accept_event(); |
845 | } |
846 | } |
847 | |
848 | Ref<InputEventKey> key_press = p_event; |
849 | |
850 | if (key_press.is_valid() && key_press->is_pressed()) { |
851 | if (ED_GET_SHORTCUT("animation_bezier_editor/focus" )->matches_event(p_event)) { |
852 | SelectionSet focused_keys; |
853 | if (selection.is_empty()) { |
854 | for (int i = 0; i < edit_points.size(); ++i) { |
855 | IntPair key_pair = IntPair(edit_points[i].track, edit_points[i].key); |
856 | focused_keys.insert(key_pair); |
857 | } |
858 | } else { |
859 | for (const IntPair &E : selection) { |
860 | focused_keys.insert(E); |
861 | if (E.second > 0) { |
862 | IntPair previous_key = IntPair(E.first, E.second - 1); |
863 | focused_keys.insert(previous_key); |
864 | } |
865 | if (E.second < animation->track_get_key_count(E.first) - 1) { |
866 | IntPair next_key = IntPair(E.first, E.second + 1); |
867 | focused_keys.insert(next_key); |
868 | } |
869 | } |
870 | } |
871 | if (focused_keys.is_empty()) { |
872 | accept_event(); |
873 | return; |
874 | } |
875 | |
876 | real_t minimum_time = INFINITY; |
877 | real_t maximum_time = -INFINITY; |
878 | real_t minimum_value = INFINITY; |
879 | real_t maximum_value = -INFINITY; |
880 | |
881 | for (const IntPair &E : selection) { |
882 | IntPair key_pair = E; |
883 | |
884 | real_t time = animation->track_get_key_time(key_pair.first, key_pair.second); |
885 | real_t value = animation->bezier_track_get_key_value(key_pair.first, key_pair.second); |
886 | |
887 | minimum_time = MIN(time, minimum_time); |
888 | maximum_time = MAX(time, maximum_time); |
889 | minimum_value = MIN(value, minimum_value); |
890 | maximum_value = MAX(value, maximum_value); |
891 | } |
892 | |
893 | float width = get_size().width - timeline->get_name_limit() - timeline->get_buttons_width(); |
894 | float padding = width * 0.1; |
895 | float desired_scale = (width - padding / 2.0) / (maximum_time - minimum_time); |
896 | minimum_time = MAX(0, minimum_time - (padding / 2.0) / desired_scale); |
897 | |
898 | float zv = Math::pow(100 / desired_scale, 0.125f); |
899 | if (zv < 1) { |
900 | zv = Math::pow(desired_scale / 100, 0.125f) - 1; |
901 | zv = 1 - zv; |
902 | } |
903 | float zoom_value = timeline->get_zoom()->get_max() - zv; |
904 | |
905 | if (Math::is_finite(minimum_time) && Math::is_finite(maximum_time) && maximum_time - minimum_time > CMP_EPSILON) { |
906 | timeline->get_zoom()->set_value(zoom_value); |
907 | timeline->call_deferred("set_value" , minimum_time); |
908 | } |
909 | |
910 | if (Math::is_finite(minimum_value) && Math::is_finite(maximum_value)) { |
911 | v_scroll = (maximum_value + minimum_value) / 2.0; |
912 | if (maximum_value - minimum_value > CMP_EPSILON) { |
913 | v_zoom = (maximum_value - minimum_value) / ((get_size().height - timeline->get_size().height) * 0.9); |
914 | } |
915 | } |
916 | |
917 | queue_redraw(); |
918 | accept_event(); |
919 | return; |
920 | } else if (ED_GET_SHORTCUT("animation_bezier_editor/select_all_keys" )->matches_event(p_event)) { |
921 | for (int i = 0; i < edit_points.size(); ++i) { |
922 | selection.insert(IntPair(edit_points[i].track, edit_points[i].key)); |
923 | } |
924 | |
925 | queue_redraw(); |
926 | accept_event(); |
927 | return; |
928 | } else if (ED_GET_SHORTCUT("animation_bezier_editor/deselect_all_keys" )->matches_event(p_event)) { |
929 | selection.clear(); |
930 | |
931 | queue_redraw(); |
932 | accept_event(); |
933 | return; |
934 | } |
935 | } |
936 | |
937 | Ref<InputEventMouseButton> mb = p_event; |
938 | int limit = timeline->get_name_limit(); |
939 | if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) { |
940 | menu_insert_key = mb->get_position(); |
941 | if (menu_insert_key.x >= limit && menu_insert_key.x <= get_size().width) { |
942 | if (!read_only) { |
943 | Vector2 = get_screen_position() + mb->get_position(); |
944 | |
945 | menu->clear(); |
946 | menu->add_icon_item(bezier_icon, TTR("Insert Key Here" ), MENU_KEY_INSERT); |
947 | if (selection.size()) { |
948 | menu->add_separator(); |
949 | menu->add_icon_item(get_editor_theme_icon(SNAME("Duplicate" )), TTR("Duplicate Selected Key(s)" ), MENU_KEY_DUPLICATE); |
950 | menu->add_separator(); |
951 | menu->add_icon_item(get_editor_theme_icon(SNAME("Remove" )), TTR("Delete Selected Key(s)" ), MENU_KEY_DELETE); |
952 | menu->add_separator(); |
953 | menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesFree" )), TTR("Make Handles Free" ), MENU_KEY_SET_HANDLE_FREE); |
954 | menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesLinear" )), TTR("Make Handles Linear" ), MENU_KEY_SET_HANDLE_LINEAR); |
955 | menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesBalanced" )), TTR("Make Handles Balanced" ), MENU_KEY_SET_HANDLE_BALANCED); |
956 | menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesMirror" )), TTR("Make Handles Mirrored" ), MENU_KEY_SET_HANDLE_MIRRORED); |
957 | menu->add_separator(); |
958 | menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesBalanced" )), TTR("Make Handles Balanced (Auto Tangent)" ), MENU_KEY_SET_HANDLE_AUTO_BALANCED); |
959 | menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesMirror" )), TTR("Make Handles Mirrored (Auto Tangent)" ), MENU_KEY_SET_HANDLE_AUTO_MIRRORED); |
960 | } |
961 | |
962 | if (menu->get_item_count()) { |
963 | menu->reset_size(); |
964 | menu->set_position(popup_pos); |
965 | menu->popup(); |
966 | } |
967 | } |
968 | } |
969 | } |
970 | |
971 | if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
972 | for (const KeyValue<int, Rect2> &E : subtracks) { |
973 | if (E.value.has_point(mb->get_position())) { |
974 | if (!locked_tracks.has(E.key) && !hidden_tracks.has(E.key)) { |
975 | set_animation_and_track(animation, E.key, read_only); |
976 | _clear_selection(); |
977 | } |
978 | return; |
979 | } |
980 | } |
981 | |
982 | for (const KeyValue<int, RBMap<int, Rect2>> &E : subtrack_icons) { |
983 | int track = E.key; |
984 | RBMap<int, Rect2> track_icons = E.value; |
985 | for (const KeyValue<int, Rect2> &I : track_icons) { |
986 | if (I.value.has_point(mb->get_position())) { |
987 | if (I.key == REMOVE_ICON) { |
988 | if (!read_only) { |
989 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
990 | undo_redo->create_action("Remove Bezier Track" ); |
991 | |
992 | undo_redo->add_do_method(this, "_update_locked_tracks_after" , track); |
993 | undo_redo->add_do_method(this, "_update_hidden_tracks_after" , track); |
994 | |
995 | undo_redo->add_do_method(animation.ptr(), "remove_track" , track); |
996 | |
997 | undo_redo->add_undo_method(animation.ptr(), "add_track" , Animation::TrackType::TYPE_BEZIER, track); |
998 | undo_redo->add_undo_method(animation.ptr(), "track_set_path" , track, animation->track_get_path(track)); |
999 | |
1000 | for (int i = 0; i < animation->track_get_key_count(track); ++i) { |
1001 | undo_redo->add_undo_method( |
1002 | this, |
1003 | "_bezier_track_insert_key" , |
1004 | track, |
1005 | animation->track_get_key_time(track, i), |
1006 | animation->bezier_track_get_key_value(track, i), |
1007 | animation->bezier_track_get_key_in_handle(track, i), |
1008 | animation->bezier_track_get_key_out_handle(track, i), |
1009 | animation->bezier_track_get_key_handle_mode(track, i)); |
1010 | } |
1011 | |
1012 | undo_redo->commit_action(); |
1013 | |
1014 | selected_track = CLAMP(selected_track, 0, animation->get_track_count() - 1); |
1015 | } |
1016 | return; |
1017 | } else if (I.key == LOCK_ICON) { |
1018 | if (locked_tracks.has(track)) { |
1019 | locked_tracks.erase(track); |
1020 | } else { |
1021 | locked_tracks.insert(track); |
1022 | if (selected_track == track) { |
1023 | for (int i = 0; i < animation->get_track_count(); ++i) { |
1024 | if (!locked_tracks.has(i) && animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) { |
1025 | set_animation_and_track(animation, i, read_only); |
1026 | break; |
1027 | } |
1028 | } |
1029 | } |
1030 | } |
1031 | queue_redraw(); |
1032 | return; |
1033 | } else if (I.key == VISIBILITY_ICON) { |
1034 | if (hidden_tracks.has(track)) { |
1035 | hidden_tracks.erase(track); |
1036 | } else { |
1037 | hidden_tracks.insert(track); |
1038 | if (selected_track == track) { |
1039 | for (int i = 0; i < animation->get_track_count(); ++i) { |
1040 | if (!hidden_tracks.has(i) && animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) { |
1041 | set_animation_and_track(animation, i, read_only); |
1042 | break; |
1043 | } |
1044 | } |
1045 | } |
1046 | } |
1047 | |
1048 | Vector<int> visible_tracks; |
1049 | for (int i = 0; i < animation->get_track_count(); ++i) { |
1050 | if (!hidden_tracks.has(i) && animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) { |
1051 | visible_tracks.push_back(i); |
1052 | } |
1053 | } |
1054 | |
1055 | if (visible_tracks.size() == 1) { |
1056 | solo_track = visible_tracks[0]; |
1057 | } else { |
1058 | solo_track = -1; |
1059 | } |
1060 | |
1061 | queue_redraw(); |
1062 | return; |
1063 | } else if (I.key == SOLO_ICON) { |
1064 | if (solo_track == track) { |
1065 | solo_track = -1; |
1066 | |
1067 | hidden_tracks.clear(); |
1068 | } else { |
1069 | if (hidden_tracks.has(track)) { |
1070 | hidden_tracks.erase(track); |
1071 | } |
1072 | for (int i = 0; i < animation->get_track_count(); ++i) { |
1073 | if (animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) { |
1074 | if (i != track && !hidden_tracks.has(i)) { |
1075 | hidden_tracks.insert(i); |
1076 | } |
1077 | } |
1078 | } |
1079 | |
1080 | set_animation_and_track(animation, track, read_only); |
1081 | solo_track = track; |
1082 | } |
1083 | queue_redraw(); |
1084 | return; |
1085 | } |
1086 | return; |
1087 | } |
1088 | } |
1089 | } |
1090 | |
1091 | for (int i = 0; i < edit_points.size(); i++) { |
1092 | //first check point |
1093 | //command makes it ignore the main point, so control point editors can be force-edited |
1094 | //path 2D editing in the 3D and 2D editors works the same way |
1095 | if (!mb->is_command_or_control_pressed()) { |
1096 | if (edit_points[i].point_rect.has_point(mb->get_position())) { |
1097 | IntPair pair = IntPair(edit_points[i].track, edit_points[i].key); |
1098 | if (mb->is_shift_pressed()) { |
1099 | //add to selection |
1100 | if (selection.has(pair)) { |
1101 | selection.erase(pair); |
1102 | } else { |
1103 | selection.insert(pair); |
1104 | } |
1105 | queue_redraw(); |
1106 | select_single_attempt = IntPair(-1, -1); |
1107 | } else if (selection.has(pair)) { |
1108 | moving_selection_attempt = true; |
1109 | moving_selection = false; |
1110 | moving_selection_from_key = pair.second; |
1111 | moving_selection_from_track = pair.first; |
1112 | moving_handle_track = pair.first; |
1113 | moving_handle_left = animation->bezier_track_get_key_in_handle(pair.first, pair.second); |
1114 | moving_handle_right = animation->bezier_track_get_key_out_handle(pair.first, pair.second); |
1115 | moving_selection_offset = Vector2(); |
1116 | select_single_attempt = pair; |
1117 | queue_redraw(); |
1118 | } else { |
1119 | moving_selection_attempt = true; |
1120 | moving_selection = true; |
1121 | moving_selection_from_key = pair.second; |
1122 | moving_selection_from_track = pair.first; |
1123 | moving_selection_offset = Vector2(); |
1124 | moving_handle_track = pair.first; |
1125 | moving_handle_left = animation->bezier_track_get_key_in_handle(pair.first, pair.second); |
1126 | moving_handle_right = animation->bezier_track_get_key_out_handle(pair.first, pair.second); |
1127 | selection.clear(); |
1128 | selection.insert(pair); |
1129 | set_animation_and_track(animation, pair.first, read_only); |
1130 | } |
1131 | return; |
1132 | } |
1133 | } |
1134 | |
1135 | if (!read_only) { |
1136 | if (edit_points[i].in_rect.has_point(mb->get_position())) { |
1137 | moving_handle = -1; |
1138 | moving_handle_key = edit_points[i].key; |
1139 | moving_handle_track = edit_points[i].track; |
1140 | moving_handle_left = animation->bezier_track_get_key_in_handle(edit_points[i].track, edit_points[i].key); |
1141 | moving_handle_right = animation->bezier_track_get_key_out_handle(edit_points[i].track, edit_points[i].key); |
1142 | queue_redraw(); |
1143 | return; |
1144 | } |
1145 | |
1146 | if (edit_points[i].out_rect.has_point(mb->get_position())) { |
1147 | moving_handle = 1; |
1148 | moving_handle_key = edit_points[i].key; |
1149 | moving_handle_track = edit_points[i].track; |
1150 | moving_handle_left = animation->bezier_track_get_key_in_handle(edit_points[i].track, edit_points[i].key); |
1151 | moving_handle_right = animation->bezier_track_get_key_out_handle(edit_points[i].track, edit_points[i].key); |
1152 | queue_redraw(); |
1153 | return; |
1154 | } |
1155 | } |
1156 | } |
1157 | |
1158 | //insert new point |
1159 | if (mb->get_position().x >= limit && mb->get_position().x < get_size().width && mb->is_command_or_control_pressed()) { |
1160 | Array new_point; |
1161 | new_point.resize(5); |
1162 | |
1163 | float h = (get_size().height / 2.0 - mb->get_position().y) * v_zoom + v_scroll; |
1164 | |
1165 | new_point[0] = h; |
1166 | new_point[1] = -0.25; |
1167 | new_point[2] = 0; |
1168 | new_point[3] = 0.25; |
1169 | new_point[4] = 0; |
1170 | |
1171 | real_t time = ((mb->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value(); |
1172 | while (animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX) != -1) { |
1173 | time += 0.0001; |
1174 | } |
1175 | |
1176 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
1177 | undo_redo->create_action(TTR("Add Bezier Point" )); |
1178 | undo_redo->add_do_method(animation.ptr(), "bezier_track_insert_key" , selected_track, time, new_point); |
1179 | undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time" , selected_track, time); |
1180 | undo_redo->commit_action(); |
1181 | |
1182 | //then attempt to move |
1183 | int index = animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX); |
1184 | ERR_FAIL_COND(index == -1); |
1185 | _clear_selection(); |
1186 | selection.insert(IntPair(selected_track, index)); |
1187 | |
1188 | moving_selection_attempt = true; |
1189 | moving_selection = false; |
1190 | moving_selection_from_key = index; |
1191 | moving_selection_from_track = selected_track; |
1192 | moving_selection_offset = Vector2(); |
1193 | select_single_attempt = IntPair(-1, -1); |
1194 | queue_redraw(); |
1195 | |
1196 | return; |
1197 | } |
1198 | |
1199 | //box select |
1200 | if (mb->get_position().x >= limit && mb->get_position().x < get_size().width) { |
1201 | box_selecting_attempt = true; |
1202 | box_selecting = false; |
1203 | box_selecting_add = false; |
1204 | box_selection_from = mb->get_position(); |
1205 | return; |
1206 | } |
1207 | } |
1208 | |
1209 | if (box_selecting_attempt && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
1210 | if (box_selecting) { |
1211 | //do actual select |
1212 | if (!box_selecting_add) { |
1213 | _clear_selection(); |
1214 | } |
1215 | |
1216 | Vector2 bs_from = box_selection_from; |
1217 | Vector2 bs_to = box_selection_to; |
1218 | if (bs_from.x > bs_to.x) { |
1219 | SWAP(bs_from.x, bs_to.x); |
1220 | } |
1221 | if (bs_from.y > bs_to.y) { |
1222 | SWAP(bs_from.y, bs_to.y); |
1223 | } |
1224 | Rect2 selection_rect(bs_from, bs_to - bs_from); |
1225 | |
1226 | bool track_set = false; |
1227 | for (int i = 0; i < edit_points.size(); i++) { |
1228 | if (edit_points[i].point_rect.intersects(selection_rect)) { |
1229 | selection.insert(IntPair(edit_points[i].track, edit_points[i].key)); |
1230 | if (!track_set) { |
1231 | track_set = true; |
1232 | set_animation_and_track(animation, edit_points[i].track, read_only); |
1233 | } |
1234 | } |
1235 | } |
1236 | } else { |
1237 | _clear_selection(); //clicked and nothing happened, so clear the selection |
1238 | |
1239 | //select by clicking on curve |
1240 | int track_count = animation->get_track_count(); |
1241 | |
1242 | real_t animation_length = animation->get_length(); |
1243 | animation->set_length(real_t(INT_MAX)); //bezier_track_interpolate doesn't find keys if they exist beyond anim length |
1244 | |
1245 | real_t time = ((mb->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value(); |
1246 | |
1247 | for (int i = 0; i < track_count; ++i) { |
1248 | if (animation->track_get_type(i) != Animation::TrackType::TYPE_BEZIER || hidden_tracks.has(i) || locked_tracks.has(i)) { |
1249 | continue; |
1250 | } |
1251 | |
1252 | float track_h = animation->bezier_track_interpolate(i, time); |
1253 | float track_height = _bezier_h_to_pixel(track_h); |
1254 | |
1255 | if (abs(mb->get_position().y - track_height) < 10) { |
1256 | set_animation_and_track(animation, i, read_only); |
1257 | break; |
1258 | } |
1259 | } |
1260 | |
1261 | animation->set_length(animation_length); |
1262 | } |
1263 | |
1264 | box_selecting_attempt = false; |
1265 | box_selecting = false; |
1266 | queue_redraw(); |
1267 | } |
1268 | |
1269 | if (moving_selection_attempt && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
1270 | if (!read_only) { |
1271 | if (moving_selection) { |
1272 | //combit it |
1273 | |
1274 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
1275 | undo_redo->create_action(TTR("Move Bezier Points" )); |
1276 | |
1277 | List<AnimMoveRestore> to_restore; |
1278 | List<Animation::HandleMode> to_restore_handle_modes; |
1279 | // 1-remove the keys |
1280 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
1281 | undo_redo->add_do_method(animation.ptr(), "track_remove_key" , E->get().first, E->get().second); |
1282 | } |
1283 | // 2- remove overlapped keys |
1284 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
1285 | real_t newtime = editor->snap_time(animation->track_get_key_time(E->get().first, E->get().second) + moving_selection_offset.x); |
1286 | |
1287 | int idx = animation->track_find_key(E->get().first, newtime, Animation::FIND_MODE_APPROX); |
1288 | if (idx == -1) { |
1289 | continue; |
1290 | } |
1291 | |
1292 | if (selection.has(IntPair(E->get().first, idx))) { |
1293 | continue; //already in selection, don't save |
1294 | } |
1295 | |
1296 | undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time" , E->get().first, newtime); |
1297 | AnimMoveRestore amr; |
1298 | |
1299 | amr.key = animation->track_get_key_value(E->get().first, idx); |
1300 | amr.track = E->get().first; |
1301 | amr.time = newtime; |
1302 | |
1303 | to_restore.push_back(amr); |
1304 | to_restore_handle_modes.push_back(animation->bezier_track_get_key_handle_mode(E->get().first, idx)); |
1305 | } |
1306 | |
1307 | // 3-move the keys (re insert them) |
1308 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
1309 | real_t newpos = editor->snap_time(animation->track_get_key_time(E->get().first, E->get().second) + moving_selection_offset.x); |
1310 | Array key = animation->track_get_key_value(E->get().first, E->get().second); |
1311 | real_t h = key[0]; |
1312 | h += moving_selection_offset.y; |
1313 | key[0] = h; |
1314 | undo_redo->add_do_method( |
1315 | this, |
1316 | "_bezier_track_insert_key" , |
1317 | E->get().first, |
1318 | newpos, |
1319 | key[0], |
1320 | Vector2(key[1], key[2]), |
1321 | Vector2(key[3], key[4]), |
1322 | animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second)); |
1323 | } |
1324 | |
1325 | // 4-(undo) remove inserted keys |
1326 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
1327 | real_t newpos = editor->snap_time(animation->track_get_key_time(E->get().first, E->get().second) + moving_selection_offset.x); |
1328 | undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time" , E->get().first, newpos); |
1329 | } |
1330 | |
1331 | // 5-(undo) reinsert keys |
1332 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
1333 | real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second); |
1334 | Array key = animation->track_get_key_value(E->get().first, E->get().second); |
1335 | undo_redo->add_undo_method( |
1336 | this, |
1337 | "_bezier_track_insert_key" , |
1338 | E->get().first, |
1339 | oldpos, |
1340 | key[0], |
1341 | Vector2(key[1], key[2]), |
1342 | Vector2(key[3], key[4]), |
1343 | animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second)); |
1344 | } |
1345 | |
1346 | // 6-(undo) reinsert overlapped keys |
1347 | for (int i = 0; i < to_restore.size(); i++) { |
1348 | const AnimMoveRestore &amr = to_restore[i]; |
1349 | Array key = amr.key; |
1350 | undo_redo->add_undo_method(animation.ptr(), "track_insert_key" , amr.track, amr.time, amr.key, 1); |
1351 | undo_redo->add_undo_method( |
1352 | this, |
1353 | "_bezier_track_insert_key" , |
1354 | amr.track, |
1355 | amr.time, |
1356 | key[0], |
1357 | Vector2(key[1], key[2]), |
1358 | Vector2(key[3], key[4]), |
1359 | to_restore_handle_modes[i]); |
1360 | } |
1361 | |
1362 | undo_redo->add_do_method(this, "_clear_selection_for_anim" , animation); |
1363 | undo_redo->add_undo_method(this, "_clear_selection_for_anim" , animation); |
1364 | |
1365 | // 7-reselect |
1366 | |
1367 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
1368 | real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second); |
1369 | real_t newpos = editor->snap_time(oldpos + moving_selection_offset.x); |
1370 | |
1371 | undo_redo->add_do_method(this, "_select_at_anim" , animation, E->get().first, newpos); |
1372 | undo_redo->add_undo_method(this, "_select_at_anim" , animation, E->get().first, oldpos); |
1373 | } |
1374 | |
1375 | undo_redo->commit_action(); |
1376 | |
1377 | moving_selection = false; |
1378 | } else if (select_single_attempt != IntPair(-1, -1)) { |
1379 | selection.clear(); |
1380 | selection.insert(select_single_attempt); |
1381 | set_animation_and_track(animation, select_single_attempt.first, read_only); |
1382 | } |
1383 | |
1384 | moving_selection_attempt = false; |
1385 | queue_redraw(); |
1386 | } |
1387 | } |
1388 | |
1389 | Ref<InputEventMouseMotion> mm = p_event; |
1390 | if (moving_selection_attempt && mm.is_valid()) { |
1391 | if (!moving_selection) { |
1392 | moving_selection = true; |
1393 | select_single_attempt = IntPair(-1, -1); |
1394 | } |
1395 | |
1396 | float y = (get_size().height / 2.0 - mm->get_position().y) * v_zoom + v_scroll; |
1397 | float x = editor->snap_time(((mm->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value()); |
1398 | |
1399 | if (!read_only) { |
1400 | moving_selection_offset = Vector2(x - animation->track_get_key_time(moving_selection_from_track, moving_selection_from_key), y - animation->bezier_track_get_key_value(moving_selection_from_track, moving_selection_from_key)); |
1401 | } |
1402 | |
1403 | additional_moving_handle_lefts.clear(); |
1404 | additional_moving_handle_rights.clear(); |
1405 | |
1406 | queue_redraw(); |
1407 | } |
1408 | |
1409 | if (box_selecting_attempt && mm.is_valid()) { |
1410 | if (!box_selecting) { |
1411 | box_selecting = true; |
1412 | box_selecting_add = mm->is_shift_pressed(); |
1413 | } |
1414 | |
1415 | box_selection_to = mm->get_position(); |
1416 | |
1417 | if (get_local_mouse_position().y < 0) { |
1418 | //avoid cursor from going too above, so it does not lose focus with viewport |
1419 | warp_mouse(Vector2(get_local_mouse_position().x, 0)); |
1420 | } |
1421 | queue_redraw(); |
1422 | } |
1423 | |
1424 | if ((moving_handle == 1 || moving_handle == -1) && mm.is_valid()) { |
1425 | float y = (get_size().height / 2.0 - mm->get_position().y) * v_zoom + v_scroll; |
1426 | float x = editor->snap_time((mm->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value(); |
1427 | |
1428 | Vector2 key_pos = Vector2(animation->track_get_key_time(selected_track, moving_handle_key), animation->bezier_track_get_key_value(selected_track, moving_handle_key)); |
1429 | |
1430 | Vector2 moving_handle_value = Vector2(x, y) - key_pos; |
1431 | |
1432 | moving_handle_left = animation->bezier_track_get_key_in_handle(moving_handle_track, moving_handle_key); |
1433 | moving_handle_right = animation->bezier_track_get_key_out_handle(moving_handle_track, moving_handle_key); |
1434 | |
1435 | if (moving_handle == -1) { |
1436 | moving_handle_left = moving_handle_value; |
1437 | |
1438 | Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key); |
1439 | |
1440 | if (handle_mode == Animation::HANDLE_MODE_BALANCED) { |
1441 | real_t ratio = timeline->get_zoom_scale() * v_zoom; |
1442 | Transform2D xform; |
1443 | xform.set_scale(Vector2(1.0, 1.0 / ratio)); |
1444 | |
1445 | Vector2 vec_out = xform.xform(moving_handle_right); |
1446 | Vector2 vec_in = xform.xform(moving_handle_left); |
1447 | |
1448 | moving_handle_right = xform.affine_inverse().xform(-vec_in.normalized() * vec_out.length()); |
1449 | } else if (handle_mode == Animation::HANDLE_MODE_MIRRORED) { |
1450 | moving_handle_right = -moving_handle_left; |
1451 | } |
1452 | } else if (moving_handle == 1) { |
1453 | moving_handle_right = moving_handle_value; |
1454 | |
1455 | Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key); |
1456 | |
1457 | if (handle_mode == Animation::HANDLE_MODE_BALANCED) { |
1458 | real_t ratio = timeline->get_zoom_scale() * v_zoom; |
1459 | Transform2D xform; |
1460 | xform.set_scale(Vector2(1.0, 1.0 / ratio)); |
1461 | |
1462 | Vector2 vec_in = xform.xform(moving_handle_left); |
1463 | Vector2 vec_out = xform.xform(moving_handle_right); |
1464 | |
1465 | moving_handle_left = xform.affine_inverse().xform(-vec_out.normalized() * vec_in.length()); |
1466 | } else if (handle_mode == Animation::HANDLE_MODE_MIRRORED) { |
1467 | moving_handle_left = -moving_handle_right; |
1468 | } |
1469 | } |
1470 | queue_redraw(); |
1471 | } |
1472 | |
1473 | if ((moving_handle == -1 || moving_handle == 1) && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) { |
1474 | if (!read_only) { |
1475 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
1476 | undo_redo->create_action(TTR("Move Bezier Points" )); |
1477 | if (moving_handle == -1) { |
1478 | real_t ratio = timeline->get_zoom_scale() * v_zoom; |
1479 | undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_in_handle" , moving_handle_track, moving_handle_key, moving_handle_left, ratio); |
1480 | undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle" , moving_handle_track, moving_handle_key, animation->bezier_track_get_key_in_handle(moving_handle_track, moving_handle_key), ratio); |
1481 | } else if (moving_handle == 1) { |
1482 | real_t ratio = timeline->get_zoom_scale() * v_zoom; |
1483 | undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_out_handle" , moving_handle_track, moving_handle_key, moving_handle_right, ratio); |
1484 | undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle" , moving_handle_track, moving_handle_key, animation->bezier_track_get_key_out_handle(moving_handle_track, moving_handle_key), ratio); |
1485 | } |
1486 | undo_redo->commit_action(); |
1487 | moving_handle = 0; |
1488 | queue_redraw(); |
1489 | } |
1490 | } |
1491 | } |
1492 | |
1493 | void AnimationBezierTrackEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) { |
1494 | v_scroll += p_scroll_vec.y * v_zoom; |
1495 | v_scroll = CLAMP(v_scroll, -100000, 100000); |
1496 | timeline->set_value(timeline->get_value() - p_scroll_vec.x / timeline->get_zoom_scale()); |
1497 | queue_redraw(); |
1498 | } |
1499 | |
1500 | void AnimationBezierTrackEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) { |
1501 | const float v_zoom_orig = v_zoom; |
1502 | Ref<InputEventWithModifiers> iewm = p_event; |
1503 | if (iewm.is_valid() && iewm->is_alt_pressed()) { |
1504 | // Alternate zoom (doesn't affect timeline). |
1505 | v_zoom = CLAMP(v_zoom * p_zoom_factor, 0.000001, 100000); |
1506 | } else { |
1507 | timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() / p_zoom_factor); |
1508 | } |
1509 | v_scroll = v_scroll + (p_origin.y - get_size().y / 2.0) * (v_zoom - v_zoom_orig); |
1510 | queue_redraw(); |
1511 | } |
1512 | |
1513 | void AnimationBezierTrackEdit::(int p_index) { |
1514 | switch (p_index) { |
1515 | case MENU_KEY_INSERT: { |
1516 | if (animation->get_track_count() > 0) { |
1517 | Array new_point; |
1518 | new_point.resize(5); |
1519 | |
1520 | float h = (get_size().height / 2.0 - menu_insert_key.y) * v_zoom + v_scroll; |
1521 | |
1522 | new_point[0] = h; |
1523 | new_point[1] = -0.25; |
1524 | new_point[2] = 0; |
1525 | new_point[3] = 0.25; |
1526 | new_point[4] = 0; |
1527 | |
1528 | int limit = timeline->get_name_limit(); |
1529 | |
1530 | real_t time = ((menu_insert_key.x - limit) / timeline->get_zoom_scale()) + timeline->get_value(); |
1531 | |
1532 | while (animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX) != -1) { |
1533 | time += 0.001; |
1534 | } |
1535 | |
1536 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
1537 | undo_redo->create_action(TTR("Add Bezier Point" )); |
1538 | undo_redo->add_do_method(animation.ptr(), "track_insert_key" , selected_track, time, new_point); |
1539 | undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time" , selected_track, time); |
1540 | undo_redo->commit_action(); |
1541 | queue_redraw(); |
1542 | } |
1543 | } break; |
1544 | case MENU_KEY_DUPLICATE: { |
1545 | duplicate_selection(); |
1546 | } break; |
1547 | case MENU_KEY_DELETE: { |
1548 | delete_selection(); |
1549 | } break; |
1550 | case MENU_KEY_SET_HANDLE_FREE: { |
1551 | _change_selected_keys_handle_mode(Animation::HANDLE_MODE_FREE); |
1552 | } break; |
1553 | case MENU_KEY_SET_HANDLE_LINEAR: { |
1554 | _change_selected_keys_handle_mode(Animation::HANDLE_MODE_LINEAR); |
1555 | } break; |
1556 | case MENU_KEY_SET_HANDLE_BALANCED: { |
1557 | _change_selected_keys_handle_mode(Animation::HANDLE_MODE_BALANCED); |
1558 | } break; |
1559 | case MENU_KEY_SET_HANDLE_MIRRORED: { |
1560 | _change_selected_keys_handle_mode(Animation::HANDLE_MODE_MIRRORED); |
1561 | } break; |
1562 | case MENU_KEY_SET_HANDLE_AUTO_BALANCED: { |
1563 | _change_selected_keys_handle_mode(Animation::HANDLE_MODE_BALANCED, true); |
1564 | } break; |
1565 | case MENU_KEY_SET_HANDLE_AUTO_MIRRORED: { |
1566 | _change_selected_keys_handle_mode(Animation::HANDLE_MODE_MIRRORED, true); |
1567 | } break; |
1568 | } |
1569 | } |
1570 | |
1571 | void AnimationBezierTrackEdit::duplicate_selection() { |
1572 | if (selection.size() == 0) { |
1573 | return; |
1574 | } |
1575 | |
1576 | real_t top_time = 1e10; |
1577 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
1578 | real_t t = animation->track_get_key_time(E->get().first, E->get().second); |
1579 | if (t < top_time) { |
1580 | top_time = t; |
1581 | } |
1582 | } |
1583 | |
1584 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
1585 | undo_redo->create_action(TTR("Animation Duplicate Keys" )); |
1586 | |
1587 | List<Pair<int, real_t>> new_selection_values; |
1588 | |
1589 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
1590 | real_t t = animation->track_get_key_time(E->get().first, E->get().second); |
1591 | real_t dst_time = t + (timeline->get_play_position() - top_time); |
1592 | int existing_idx = animation->track_find_key(E->get().first, dst_time, Animation::FIND_MODE_APPROX); |
1593 | |
1594 | undo_redo->add_do_method(animation.ptr(), "track_insert_key" , E->get().first, dst_time, animation->track_get_key_value(E->get().first, E->get().second), animation->track_get_key_transition(E->get().first, E->get().second)); |
1595 | undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time" , E->get().first, dst_time); |
1596 | |
1597 | Pair<int, real_t> p; |
1598 | p.first = E->get().first; |
1599 | p.second = dst_time; |
1600 | new_selection_values.push_back(p); |
1601 | |
1602 | if (existing_idx != -1) { |
1603 | undo_redo->add_undo_method(animation.ptr(), "track_insert_key" , E->get().first, dst_time, animation->track_get_key_value(E->get().first, existing_idx), animation->track_get_key_transition(E->get().first, existing_idx)); |
1604 | } |
1605 | } |
1606 | |
1607 | undo_redo->commit_action(); |
1608 | |
1609 | //reselect duplicated |
1610 | |
1611 | selection.clear(); |
1612 | for (const Pair<int, real_t> &E : new_selection_values) { |
1613 | int track = E.first; |
1614 | real_t time = E.second; |
1615 | |
1616 | int existing_idx = animation->track_find_key(track, time, Animation::FIND_MODE_APPROX); |
1617 | |
1618 | if (existing_idx == -1) { |
1619 | continue; |
1620 | } |
1621 | |
1622 | selection.insert(IntPair(track, existing_idx)); |
1623 | } |
1624 | |
1625 | queue_redraw(); |
1626 | } |
1627 | |
1628 | void AnimationBezierTrackEdit::delete_selection() { |
1629 | if (selection.size()) { |
1630 | EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton(); |
1631 | undo_redo->create_action(TTR("Animation Delete Keys" )); |
1632 | |
1633 | for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) { |
1634 | undo_redo->add_do_method(animation.ptr(), "track_remove_key" , E->get().first, E->get().second); |
1635 | undo_redo->add_undo_method(animation.ptr(), "track_insert_key" , E->get().first, animation->track_get_key_time(E->get().first, E->get().second), animation->track_get_key_value(E->get().first, E->get().second), 1); |
1636 | } |
1637 | undo_redo->add_do_method(this, "_clear_selection_for_anim" , animation); |
1638 | undo_redo->add_undo_method(this, "_clear_selection_for_anim" , animation); |
1639 | undo_redo->commit_action(); |
1640 | |
1641 | //selection.clear(); |
1642 | } |
1643 | } |
1644 | |
1645 | void AnimationBezierTrackEdit::_bezier_track_insert_key(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) { |
1646 | ERR_FAIL_COND(animation.is_null()); |
1647 | int idx = animation->bezier_track_insert_key(p_track, p_time, p_value, p_in_handle, p_out_handle); |
1648 | animation->bezier_track_set_key_handle_mode(p_track, idx, p_handle_mode); |
1649 | } |
1650 | |
1651 | void AnimationBezierTrackEdit::_bind_methods() { |
1652 | ClassDB::bind_method(D_METHOD("_clear_selection" ), &AnimationBezierTrackEdit::_clear_selection); |
1653 | ClassDB::bind_method(D_METHOD("_clear_selection_for_anim" ), &AnimationBezierTrackEdit::_clear_selection_for_anim); |
1654 | ClassDB::bind_method(D_METHOD("_select_at_anim" ), &AnimationBezierTrackEdit::_select_at_anim); |
1655 | ClassDB::bind_method(D_METHOD("_update_hidden_tracks_after" ), &AnimationBezierTrackEdit::_update_hidden_tracks_after); |
1656 | ClassDB::bind_method(D_METHOD("_update_locked_tracks_after" ), &AnimationBezierTrackEdit::_update_locked_tracks_after); |
1657 | ClassDB::bind_method(D_METHOD("_bezier_track_insert_key" ), &AnimationBezierTrackEdit::_bezier_track_insert_key); |
1658 | |
1659 | ADD_SIGNAL(MethodInfo("timeline_changed" , PropertyInfo(Variant::FLOAT, "position" ), PropertyInfo(Variant::BOOL, "drag" ))); |
1660 | ADD_SIGNAL(MethodInfo("remove_request" , PropertyInfo(Variant::INT, "track" ))); |
1661 | ADD_SIGNAL(MethodInfo("insert_key" , PropertyInfo(Variant::FLOAT, "offset" ))); |
1662 | ADD_SIGNAL(MethodInfo("select_key" , PropertyInfo(Variant::INT, "index" ), PropertyInfo(Variant::BOOL, "single" ), PropertyInfo(Variant::INT, "track" ))); |
1663 | ADD_SIGNAL(MethodInfo("clear_selection" )); |
1664 | ADD_SIGNAL(MethodInfo("close_request" )); |
1665 | |
1666 | ADD_SIGNAL(MethodInfo("move_selection_begin" )); |
1667 | ADD_SIGNAL(MethodInfo("move_selection" , PropertyInfo(Variant::FLOAT, "offset" ))); |
1668 | ADD_SIGNAL(MethodInfo("move_selection_commit" )); |
1669 | ADD_SIGNAL(MethodInfo("move_selection_cancel" )); |
1670 | } |
1671 | |
1672 | AnimationBezierTrackEdit::AnimationBezierTrackEdit() { |
1673 | panner.instantiate(); |
1674 | panner->set_callbacks(callable_mp(this, &AnimationBezierTrackEdit::_pan_callback), callable_mp(this, &AnimationBezierTrackEdit::_zoom_callback)); |
1675 | |
1676 | play_position = memnew(Control); |
1677 | play_position->set_mouse_filter(MOUSE_FILTER_PASS); |
1678 | add_child(play_position); |
1679 | play_position->set_anchors_and_offsets_preset(PRESET_FULL_RECT); |
1680 | play_position->connect("draw" , callable_mp(this, &AnimationBezierTrackEdit::_play_position_draw)); |
1681 | set_focus_mode(FOCUS_CLICK); |
1682 | |
1683 | set_clip_contents(true); |
1684 | |
1685 | ED_SHORTCUT("animation_bezier_editor/focus" , TTR("Focus" ), Key::F); |
1686 | ED_SHORTCUT("animation_bezier_editor/select_all_keys" , TTR("Select All Keys" ), KeyModifierMask::CMD_OR_CTRL | Key::A); |
1687 | ED_SHORTCUT("animation_bezier_editor/deselect_all_keys" , TTR("Deselect All Keys" ), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::A); |
1688 | |
1689 | menu = memnew(PopupMenu); |
1690 | add_child(menu); |
1691 | menu->connect("id_pressed" , callable_mp(this, &AnimationBezierTrackEdit::_menu_selected)); |
1692 | } |
1693 | |