animation_bezier_editor.cpp 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377
  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. #include "animation_bezier_editor.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/gui/editor_spin_slider.h"
  36. #include "editor/plugins/animation_player_editor_plugin.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "scene/gui/view_panner.h"
  39. #include "scene/resources/text_line.h"
  40. #include <climits>
  41. float AnimationBezierTrackEdit::_bezier_h_to_pixel(float p_h) {
  42. float h = p_h;
  43. h = (h - timeline_v_scroll) / timeline_v_zoom;
  44. h = (get_size().height / 2.0) - h;
  45. return h;
  46. }
  47. void AnimationBezierTrackEdit::_draw_track(int p_track, const Color &p_color) {
  48. float scale = timeline->get_zoom_scale();
  49. int limit = timeline->get_name_limit();
  50. int right_limit = get_size().width;
  51. // Selection may have altered the order of keys.
  52. RBMap<real_t, int> key_order;
  53. for (int i = 0; i < animation->track_get_key_count(p_track); i++) {
  54. real_t ofs = animation->track_get_key_time(p_track, i);
  55. if (selection.has(IntPair(p_track, i))) {
  56. if (moving_selection) {
  57. ofs += moving_selection_offset.x;
  58. } else if (scaling_selection) {
  59. ofs += -scaling_selection_offset.x + (ofs - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
  60. }
  61. }
  62. key_order[ofs] = i;
  63. }
  64. for (RBMap<real_t, int>::Element *E = key_order.front(); E; E = E->next()) {
  65. int i = E->get();
  66. if (!E->next()) {
  67. break;
  68. }
  69. int i_n = E->next()->get();
  70. float offset = animation->track_get_key_time(p_track, i);
  71. float height = animation->bezier_track_get_key_value(p_track, i);
  72. Vector2 out_handle = animation->bezier_track_get_key_out_handle(p_track, i);
  73. if (p_track == moving_handle_track && (moving_handle == -1 || moving_handle == 1) && moving_handle_key == i) {
  74. out_handle = moving_handle_right;
  75. }
  76. if (selection.has(IntPair(p_track, i))) {
  77. if (moving_selection) {
  78. offset += moving_selection_offset.x;
  79. height += moving_selection_offset.y;
  80. } else if (scaling_selection) {
  81. offset += -scaling_selection_offset.x + (offset - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
  82. height += -scaling_selection_offset.y + (height - scaling_selection_pivot.y) * (scaling_selection_scale.y - 1);
  83. }
  84. }
  85. out_handle += Vector2(offset, height);
  86. float offset_n = animation->track_get_key_time(p_track, i_n);
  87. float height_n = animation->bezier_track_get_key_value(p_track, i_n);
  88. Vector2 in_handle = animation->bezier_track_get_key_in_handle(p_track, i_n);
  89. if (p_track == moving_handle_track && (moving_handle == -1 || moving_handle == 1) && moving_handle_key == i_n) {
  90. in_handle = moving_handle_left;
  91. }
  92. if (selection.has(IntPair(p_track, i_n))) {
  93. if (moving_selection) {
  94. offset_n += moving_selection_offset.x;
  95. height_n += moving_selection_offset.y;
  96. } else if (scaling_selection) {
  97. offset_n += -scaling_selection_offset.x + (offset_n - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
  98. height_n += -scaling_selection_offset.y + (height_n - scaling_selection_pivot.y) * (scaling_selection_scale.y - 1);
  99. }
  100. }
  101. in_handle += Vector2(offset_n, height_n);
  102. Vector2 start(offset, height);
  103. Vector2 end(offset_n, height_n);
  104. int from_x = (offset - timeline->get_value()) * scale + limit;
  105. int point_start = from_x;
  106. int to_x = (offset_n - timeline->get_value()) * scale + limit;
  107. int point_end = to_x;
  108. if (from_x > right_limit) { // Not visible.
  109. continue;
  110. }
  111. if (to_x < limit) { // Not visible.
  112. continue;
  113. }
  114. from_x = MAX(from_x, limit);
  115. to_x = MIN(to_x, right_limit);
  116. Vector<Vector2> lines;
  117. Vector2 prev_pos;
  118. for (int j = from_x; j <= to_x; j++) {
  119. float t = (j - limit) / scale + timeline->get_value();
  120. float h;
  121. if (j == point_end) {
  122. h = end.y; // Make sure it always connects.
  123. } else if (j == point_start) {
  124. h = start.y; // Make sure it always connects.
  125. } else { // Custom interpolation, used because it needs to show paths affected by moving the selection or handles.
  126. int iterations = 10;
  127. float low = 0;
  128. float high = 1;
  129. // Narrow high and low as much as possible.
  130. for (int k = 0; k < iterations; k++) {
  131. float middle = (low + high) / 2.0;
  132. Vector2 interp = start.bezier_interpolate(out_handle, in_handle, end, middle);
  133. if (interp.x < t) {
  134. low = middle;
  135. } else {
  136. high = middle;
  137. }
  138. }
  139. // Interpolate the result.
  140. Vector2 low_pos = start.bezier_interpolate(out_handle, in_handle, end, low);
  141. Vector2 high_pos = start.bezier_interpolate(out_handle, in_handle, end, high);
  142. float c = (t - low_pos.x) / (high_pos.x - low_pos.x);
  143. h = low_pos.lerp(high_pos, c).y;
  144. }
  145. h = _bezier_h_to_pixel(h);
  146. Vector2 pos(j, h);
  147. if (j > from_x) {
  148. lines.push_back(prev_pos);
  149. lines.push_back(pos);
  150. }
  151. prev_pos = pos;
  152. }
  153. if (lines.size() >= 2) {
  154. draw_multiline(lines, p_color, Math::round(EDSCALE), true);
  155. }
  156. }
  157. }
  158. 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) {
  159. Vector2 from = p_from;
  160. Vector2 to = p_to;
  161. if (from.x == to.x && from.y == to.y) {
  162. return;
  163. }
  164. if (to.x < from.x) {
  165. SWAP(to, from);
  166. }
  167. if (to.x < p_clip_left) {
  168. return;
  169. }
  170. if (from.x > p_clip_right) {
  171. return;
  172. }
  173. if (to.x > p_clip_right) {
  174. float c = (p_clip_right - from.x) / (to.x - from.x);
  175. to = from.lerp(to, c);
  176. }
  177. if (from.x < p_clip_left) {
  178. float c = (p_clip_left - from.x) / (to.x - from.x);
  179. from = from.lerp(to, c);
  180. }
  181. draw_line(from, to, p_color, Math::round(EDSCALE), true);
  182. }
  183. void AnimationBezierTrackEdit::_notification(int p_what) {
  184. switch (p_what) {
  185. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  186. if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
  187. 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")));
  188. panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
  189. }
  190. } break;
  191. case NOTIFICATION_ENTER_TREE: {
  192. 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")));
  193. panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
  194. } break;
  195. case NOTIFICATION_THEME_CHANGED: {
  196. bezier_icon = get_editor_theme_icon(SNAME("KeyBezierPoint"));
  197. bezier_handle_icon = get_editor_theme_icon(SNAME("KeyBezierHandle"));
  198. selected_icon = get_editor_theme_icon(SNAME("KeyBezierSelected"));
  199. } break;
  200. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  201. RID ae = get_accessibility_element();
  202. ERR_FAIL_COND(ae.is_null());
  203. //TODO
  204. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
  205. DisplayServer::get_singleton()->accessibility_update_set_value(ae, TTR(vformat("The %s is not accessible at this time.", "Animation bezier track editor")));
  206. } break;
  207. case NOTIFICATION_DRAW: {
  208. if (animation.is_null()) {
  209. return;
  210. }
  211. int limit = timeline->get_name_limit();
  212. const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  213. const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  214. const Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  215. const Color h_line_color = get_theme_color(SNAME("h_line_color"), SNAME("AnimationBezierTrackEdit"));
  216. const Color v_line_color = get_theme_color(SNAME("v_line_color"), SNAME("AnimationBezierTrackEdit"));
  217. const Color focus_color = get_theme_color(SNAME("focus_color"), SNAME("AnimationBezierTrackEdit"));
  218. const Color track_focus_color = get_theme_color(SNAME("track_focus_color"), SNAME("AnimationBezierTrackEdit"));
  219. const int h_separation = get_theme_constant(SNAME("h_separation"), SNAME("AnimationBezierTrackEdit"));
  220. const int v_separation = get_theme_constant(SNAME("h_separation"), SNAME("AnimationBezierTrackEdit"));
  221. if (has_focus()) {
  222. draw_rect(Rect2(Point2(), get_size()), focus_color, false, Math::round(EDSCALE));
  223. }
  224. draw_line(Point2(limit, 0), Point2(limit, get_size().height), v_line_color, Math::round(EDSCALE));
  225. int right_limit = get_size().width;
  226. track_v_scroll_max = v_separation;
  227. int vofs = v_separation + track_v_scroll;
  228. int margin = 0;
  229. RBMap<int, Color> subtrack_colors;
  230. Color selected_track_color;
  231. subtracks.clear();
  232. subtrack_icons.clear();
  233. RBMap<String, Vector<int>> track_indices;
  234. int track_count = animation->get_track_count();
  235. for (int i = 0; i < track_count; ++i) {
  236. if (!_is_track_displayed(i)) {
  237. continue;
  238. }
  239. String base_path = animation->track_get_path(i);
  240. int end = base_path.find_char(':');
  241. if (end != -1) {
  242. base_path = base_path.substr(0, end + 1);
  243. }
  244. Vector<int> indices = track_indices.has(base_path) ? track_indices[base_path] : Vector<int>();
  245. indices.push_back(i);
  246. track_indices[base_path] = indices;
  247. }
  248. for (const KeyValue<String, Vector<int>> &E : track_indices) {
  249. String base_path = E.key;
  250. Vector<int> tracks = E.value;
  251. // Names and icon.
  252. {
  253. NodePath path = animation->track_get_path(tracks[0]);
  254. Node *node = nullptr;
  255. if (root && root->has_node(path)) {
  256. node = root->get_node(path);
  257. }
  258. String text;
  259. if (node) {
  260. int ofs = 0;
  261. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(node, "Node");
  262. text = node->get_name();
  263. ofs += h_separation;
  264. TextLine text_buf = TextLine(text, font, font_size);
  265. text_buf.set_width(limit - ofs - icon->get_width() - h_separation);
  266. int h = MAX(text_buf.get_size().y, icon->get_height());
  267. draw_texture(icon, Point2(ofs, vofs + int(h - icon->get_height()) / 2.0));
  268. ofs += icon->get_width() + h_separation;
  269. margin = icon->get_width();
  270. Vector2 string_pos = Point2(ofs, vofs);
  271. string_pos = string_pos.floor();
  272. text_buf.draw(get_canvas_item(), string_pos, color);
  273. vofs += h + v_separation;
  274. track_v_scroll_max += h + v_separation;
  275. }
  276. }
  277. const Color dc = get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
  278. Ref<Texture2D> remove = get_editor_theme_icon(SNAME("Remove"));
  279. float remove_hpos = limit - h_separation - remove->get_width();
  280. Ref<Texture2D> lock = get_editor_theme_icon(SNAME("Lock"));
  281. Ref<Texture2D> unlock = get_editor_theme_icon(SNAME("Unlock"));
  282. float lock_hpos = remove_hpos - h_separation - lock->get_width();
  283. Ref<Texture2D> visibility_visible = get_editor_theme_icon(SNAME("GuiVisibilityVisible"));
  284. Ref<Texture2D> visibility_hidden = get_editor_theme_icon(SNAME("GuiVisibilityHidden"));
  285. float visibility_hpos = lock_hpos - h_separation - visibility_visible->get_width();
  286. Ref<Texture2D> solo = get_editor_theme_icon(SNAME("AudioBusSolo"));
  287. float solo_hpos = visibility_hpos - h_separation - solo->get_width();
  288. float buttons_width = remove->get_width() + lock->get_width() + visibility_visible->get_width() + solo->get_width() + h_separation * 3;
  289. for (int i = 0; i < tracks.size(); ++i) {
  290. // Related track titles.
  291. int current_track = tracks[i];
  292. String path = animation->track_get_path(current_track);
  293. path = path.replace_first(base_path, "");
  294. Color cc = color;
  295. TextLine text_buf = TextLine(path, font, font_size);
  296. text_buf.set_width(limit - margin - buttons_width - h_separation * 2);
  297. Rect2 rect = Rect2(margin, vofs, solo_hpos - h_separation - solo->get_width(), text_buf.get_size().y + v_separation);
  298. cc.a *= 0.7;
  299. float h;
  300. if (path.ends_with(":x")) {
  301. h = 0;
  302. } else if (path.ends_with(":y")) {
  303. h = 0.33f;
  304. } else if (path.ends_with(":z")) {
  305. h = 0.66f;
  306. } else {
  307. uint32_t hash = path.hash();
  308. hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
  309. hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
  310. hash = (hash >> 16) ^ hash;
  311. h = (hash % 65535) / 65536.0;
  312. }
  313. if (current_track != selected_track) {
  314. Color track_color;
  315. if (locked_tracks.has(current_track)) {
  316. track_color.set_hsv(h, 0, 0.4);
  317. } else {
  318. track_color.set_hsv(h, 0.2, 0.8);
  319. }
  320. track_color.a = 0.5;
  321. draw_rect(Rect2(0, vofs, margin - h_separation, text_buf.get_size().y * 0.8), track_color);
  322. subtrack_colors[current_track] = track_color;
  323. subtracks[current_track] = rect;
  324. } else {
  325. draw_rect(rect, track_focus_color);
  326. if (locked_tracks.has(selected_track)) {
  327. selected_track_color.set_hsv(h, 0.0, 0.4);
  328. } else {
  329. selected_track_color.set_hsv(h, 0.8, 0.8);
  330. }
  331. }
  332. Vector2 string_pos = Point2(margin + h_separation, vofs);
  333. text_buf.draw(get_canvas_item(), string_pos, cc);
  334. float icon_start_height = vofs + rect.size.y / 2.0;
  335. Rect2 remove_rect = Rect2(remove_hpos, icon_start_height - remove->get_height() / 2.0, remove->get_width(), remove->get_height());
  336. if (read_only) {
  337. draw_texture(remove, remove_rect.position, dc);
  338. } else {
  339. draw_texture(remove, remove_rect.position);
  340. }
  341. Rect2 lock_rect = Rect2(lock_hpos, icon_start_height - lock->get_height() / 2.0, lock->get_width(), lock->get_height());
  342. if (locked_tracks.has(current_track)) {
  343. draw_texture(lock, lock_rect.position);
  344. } else {
  345. draw_texture(unlock, lock_rect.position);
  346. }
  347. Rect2 visible_rect = Rect2(visibility_hpos, icon_start_height - visibility_visible->get_height() / 2.0, visibility_visible->get_width(), visibility_visible->get_height());
  348. if (hidden_tracks.has(current_track)) {
  349. draw_texture(visibility_hidden, visible_rect.position);
  350. } else {
  351. draw_texture(visibility_visible, visible_rect.position);
  352. }
  353. Rect2 solo_rect = Rect2(solo_hpos, icon_start_height - solo->get_height() / 2.0, solo->get_width(), solo->get_height());
  354. draw_texture(solo, solo_rect.position);
  355. RBMap<int, Rect2> track_icons;
  356. track_icons[REMOVE_ICON] = remove_rect;
  357. track_icons[LOCK_ICON] = lock_rect;
  358. track_icons[VISIBILITY_ICON] = visible_rect;
  359. track_icons[SOLO_ICON] = solo_rect;
  360. subtrack_icons[current_track] = track_icons;
  361. vofs += text_buf.get_size().y + v_separation;
  362. track_v_scroll_max += text_buf.get_size().y + v_separation;
  363. }
  364. }
  365. const Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  366. // Guides.
  367. {
  368. float min_left_scale = font->get_height(font_size) + v_separation;
  369. float scale = (min_left_scale * 2) * timeline_v_zoom;
  370. float step = Math::pow(10.0, Math::round(Math::log(scale / 5.0) / Math::log(10.0))) * 5.0;
  371. scale = Math::snapped(scale, step);
  372. while (scale / timeline_v_zoom < min_left_scale * 2) {
  373. scale += step;
  374. }
  375. bool first = true;
  376. int prev_iv = 0;
  377. for (int i = font->get_height(font_size); i < get_size().height; i++) {
  378. float ofs = get_size().height / 2.0 - i;
  379. ofs *= timeline_v_zoom;
  380. ofs += timeline_v_scroll;
  381. int iv = int(ofs / scale);
  382. if (ofs < 0) {
  383. iv -= 1;
  384. }
  385. if (!first && iv != prev_iv) {
  386. Color lc = h_line_color;
  387. lc.a *= 0.5;
  388. draw_line(Point2(limit, i), Point2(right_limit, i), lc, Math::round(EDSCALE));
  389. Color c = color;
  390. c.a *= 0.5;
  391. 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);
  392. }
  393. first = false;
  394. prev_iv = iv;
  395. }
  396. }
  397. // Draw other curves.
  398. {
  399. float scale = timeline->get_zoom_scale();
  400. Ref<Texture2D> point = get_editor_theme_icon(SNAME("KeyValue"));
  401. for (const KeyValue<int, Color> &E : subtrack_colors) {
  402. if (hidden_tracks.has(E.key)) {
  403. continue;
  404. }
  405. _draw_track(E.key, E.value);
  406. for (int i = 0; i < animation->track_get_key_count(E.key); i++) {
  407. float offset = animation->track_get_key_time(E.key, i);
  408. float value = animation->bezier_track_get_key_value(E.key, i);
  409. Vector2 pos((offset - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value));
  410. if (pos.x >= limit && pos.x <= right_limit) {
  411. draw_texture(point, pos - point->get_size() / 2.0, E.value);
  412. }
  413. }
  414. }
  415. if (track_count > 0 && !hidden_tracks.has(selected_track)) {
  416. // Draw edited curve.
  417. _draw_track(selected_track, selected_track_color);
  418. }
  419. }
  420. const bool draw_selection_handles = selection.size() > 1;
  421. LocalVector<Point2> selected_pos;
  422. // Draw editor handles.
  423. {
  424. edit_points.clear();
  425. float scale = timeline->get_zoom_scale();
  426. for (int i = 0; i < track_count; ++i) {
  427. bool draw_track = _is_track_curves_displayed(i) && !locked_tracks.has(i);
  428. if (!draw_selection_handles && !draw_track) {
  429. continue;
  430. }
  431. int key_count = animation->track_get_key_count(i);
  432. for (int j = 0; j < key_count; ++j) {
  433. float offset = animation->track_get_key_time(i, j);
  434. float value = animation->bezier_track_get_key_value(i, j);
  435. bool is_selected = selection.has(IntPair(i, j));
  436. if (is_selected) {
  437. if (moving_selection) {
  438. offset += moving_selection_offset.x;
  439. value += moving_selection_offset.y;
  440. } else if (scaling_selection) {
  441. offset += -scaling_selection_offset.x + (offset - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
  442. value += -scaling_selection_offset.y + (value - scaling_selection_pivot.y) * (scaling_selection_scale.y - 1);
  443. }
  444. }
  445. Vector2 pos((offset - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value));
  446. if (draw_selection_handles && is_selected) {
  447. selected_pos.push_back(pos);
  448. }
  449. if (!draw_track) {
  450. continue;
  451. }
  452. Vector2 in_vec = animation->bezier_track_get_key_in_handle(i, j);
  453. if ((moving_handle == 1 || moving_handle == -1) && moving_handle_track == i && moving_handle_key == j) {
  454. in_vec = moving_handle_left;
  455. }
  456. Vector2 pos_in(((offset + in_vec.x) - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value + in_vec.y));
  457. Vector2 out_vec = animation->bezier_track_get_key_out_handle(i, j);
  458. if ((moving_handle == 1 || moving_handle == -1) && moving_handle_track == i && moving_handle_key == j) {
  459. out_vec = moving_handle_right;
  460. }
  461. Vector2 pos_out(((offset + out_vec.x) - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value + out_vec.y));
  462. if (i == selected_track || is_selected) {
  463. _draw_line_clipped(pos, pos_in, accent, limit, right_limit);
  464. _draw_line_clipped(pos, pos_out, accent, limit, right_limit);
  465. }
  466. EditPoint ep;
  467. ep.track = i;
  468. ep.key = j;
  469. if (pos.x >= limit && pos.x <= right_limit) {
  470. ep.point_rect.position = (pos - bezier_icon->get_size() / 2.0).floor();
  471. ep.point_rect.size = bezier_icon->get_size();
  472. if (is_selected) {
  473. draw_texture(selected_icon, ep.point_rect.position);
  474. 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);
  475. 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);
  476. } else {
  477. Color track_color = Color(1, 1, 1, 1);
  478. if (i != selected_track) {
  479. track_color = subtrack_colors[i];
  480. }
  481. draw_texture(bezier_icon, ep.point_rect.position, track_color);
  482. }
  483. ep.point_rect = ep.point_rect.grow(ep.point_rect.size.width * 0.5);
  484. }
  485. ep.point_rect = ep.point_rect.grow(ep.point_rect.size.width * 0.5);
  486. if (i == selected_track || is_selected) {
  487. if (animation->bezier_track_get_key_handle_mode(i, j) != Animation::HANDLE_MODE_LINEAR) {
  488. if (pos_in.x >= limit && pos_in.x <= right_limit) {
  489. ep.in_rect.position = (pos_in - bezier_handle_icon->get_size() / 2.0).floor();
  490. ep.in_rect.size = bezier_handle_icon->get_size();
  491. draw_texture(bezier_handle_icon, ep.in_rect.position);
  492. ep.in_rect = ep.in_rect.grow(ep.in_rect.size.width * 0.5);
  493. }
  494. if (pos_out.x >= limit && pos_out.x <= right_limit) {
  495. ep.out_rect.position = (pos_out - bezier_handle_icon->get_size() / 2.0).floor();
  496. ep.out_rect.size = bezier_handle_icon->get_size();
  497. draw_texture(bezier_handle_icon, ep.out_rect.position);
  498. ep.out_rect = ep.out_rect.grow(ep.out_rect.size.width * 0.5);
  499. }
  500. }
  501. }
  502. if (!locked_tracks.has(i)) {
  503. edit_points.push_back(ep);
  504. }
  505. }
  506. }
  507. for (int i = 0; i < edit_points.size(); ++i) {
  508. if (edit_points[i].track == selected_track) {
  509. EditPoint ep = edit_points[i];
  510. edit_points.remove_at(i);
  511. edit_points.insert(0, ep);
  512. }
  513. }
  514. }
  515. selection_rect = Rect2();
  516. selection_handles_rect = Rect2();
  517. // Draw scale handles.
  518. if (draw_selection_handles) {
  519. selection_rect.position = selected_pos[0];
  520. selected_pos.remove_at(0);
  521. for (const Point2 &pos : selected_pos) {
  522. selection_rect = selection_rect.expand(pos);
  523. }
  524. const int outer_ofs = Math::round(12 * EDSCALE);
  525. const int inner_ofs = Math::round(outer_ofs / 2.0);
  526. // Draw horizontal handles.
  527. if (selection_rect.size.height > CMP_EPSILON) {
  528. _draw_line_clipped(selection_rect.position - Vector2(inner_ofs, inner_ofs), selection_rect.position + Vector2(selection_rect.size.width + inner_ofs, -inner_ofs), accent, limit, right_limit);
  529. _draw_line_clipped(selection_rect.position + Vector2(-inner_ofs, selection_rect.size.height + inner_ofs), selection_rect.position + selection_rect.size + Vector2(inner_ofs, inner_ofs), accent, limit, right_limit);
  530. }
  531. // Draw vertical handles.
  532. if (selection_rect.size.width > CMP_EPSILON) {
  533. _draw_line_clipped(selection_rect.position - Vector2(inner_ofs, inner_ofs), selection_rect.position + Vector2(-inner_ofs, selection_rect.size.height + inner_ofs), accent, limit, right_limit);
  534. _draw_line_clipped(selection_rect.position + Vector2(selection_rect.size.width + inner_ofs, -inner_ofs), selection_rect.position + selection_rect.size + Vector2(inner_ofs, inner_ofs), accent, limit, right_limit);
  535. }
  536. selection_handles_rect.position = selection_rect.position - Vector2(outer_ofs, outer_ofs);
  537. selection_handles_rect.size = selection_rect.size + Vector2(outer_ofs, outer_ofs) * 2;
  538. }
  539. if (box_selecting) {
  540. Vector2 bs_from = box_selection_from;
  541. Vector2 bs_to = box_selection_to;
  542. if (bs_from.x > bs_to.x) {
  543. SWAP(bs_from.x, bs_to.x);
  544. }
  545. if (bs_from.y > bs_to.y) {
  546. SWAP(bs_from.y, bs_to.y);
  547. }
  548. draw_rect(
  549. Rect2(bs_from, bs_to - bs_from),
  550. get_theme_color(SNAME("box_selection_fill_color"), EditorStringName(Editor)));
  551. draw_rect(
  552. Rect2(bs_from, bs_to - bs_from),
  553. get_theme_color(SNAME("box_selection_stroke_color"), EditorStringName(Editor)),
  554. false,
  555. Math::round(EDSCALE));
  556. }
  557. } break;
  558. }
  559. }
  560. // Check if a track is displayed in the bezier editor (track type = bezier and track not filtered).
  561. bool AnimationBezierTrackEdit::_is_track_displayed(int p_track_index) {
  562. if (animation->track_get_type(p_track_index) != Animation::TrackType::TYPE_BEZIER) {
  563. return false;
  564. }
  565. if (is_filtered) {
  566. String path = animation->track_get_path(p_track_index);
  567. if (root && root->has_node(path)) {
  568. Node *node = root->get_node(path);
  569. if (!node) {
  570. return false; // No node, no filter.
  571. }
  572. if (!EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
  573. return false; // Skip track due to not selected.
  574. }
  575. }
  576. }
  577. return true;
  578. }
  579. // Check if the curves for a track are displayed in the editor (not hidden). Includes the check on the track visibility.
  580. bool AnimationBezierTrackEdit::_is_track_curves_displayed(int p_track_index) {
  581. // Is the track is visible in the editor?
  582. if (!_is_track_displayed(p_track_index)) {
  583. return false;
  584. }
  585. // And curves visible?
  586. if (hidden_tracks.has(p_track_index)) {
  587. return false;
  588. }
  589. return true;
  590. }
  591. Ref<Animation> AnimationBezierTrackEdit::get_animation() const {
  592. return animation;
  593. }
  594. void AnimationBezierTrackEdit::set_animation_and_track(const Ref<Animation> &p_animation, int p_track, bool p_read_only) {
  595. animation = p_animation;
  596. read_only = p_read_only;
  597. selected_track = p_track;
  598. queue_redraw();
  599. }
  600. Size2 AnimationBezierTrackEdit::get_minimum_size() const {
  601. return Vector2(1, 1);
  602. }
  603. Control::CursorShape AnimationBezierTrackEdit::get_cursor_shape(const Point2 &p_pos) const {
  604. // Box selecting or moving a handle
  605. if (box_selecting || Math::abs(moving_handle) == 1) {
  606. return get_default_cursor_shape();
  607. }
  608. // Hovering a handle
  609. if (!read_only) {
  610. for (const EditPoint &edit_point : edit_points) {
  611. if (edit_point.in_rect.has_point(p_pos) || edit_point.out_rect.has_point(p_pos)) {
  612. return get_default_cursor_shape();
  613. }
  614. }
  615. }
  616. // Currently box scaling
  617. if (scaling_selection) {
  618. if (scaling_selection_handles == Vector2i(1, 1) || scaling_selection_handles == Vector2i(-1, -1)) {
  619. return CURSOR_FDIAGSIZE;
  620. } else if (scaling_selection_handles == Vector2i(1, -1) || scaling_selection_handles == Vector2i(-1, 1)) {
  621. return CURSOR_BDIAGSIZE;
  622. } else if (abs(scaling_selection_handles.x) == 1) {
  623. return CURSOR_HSIZE;
  624. } else if (abs(scaling_selection_handles.y) == 1) {
  625. return CURSOR_VSIZE;
  626. }
  627. }
  628. // Hovering the scaling box
  629. const Vector2i rel_pos = p_pos - selection_rect.position;
  630. if (selection_handles_rect.has_point(p_pos)) {
  631. if ((rel_pos.x < 0 && rel_pos.y < 0) || (rel_pos.x > selection_rect.size.width && rel_pos.y > selection_rect.size.height)) {
  632. return CURSOR_FDIAGSIZE;
  633. } else if ((rel_pos.x < 0 && rel_pos.y > selection_rect.size.height) || (rel_pos.x > selection_rect.size.width && rel_pos.y < 0)) {
  634. return CURSOR_BDIAGSIZE;
  635. } else if (rel_pos.x < 0 || rel_pos.x > selection_rect.size.width) {
  636. return CURSOR_HSIZE;
  637. } else if (rel_pos.y < 0 || rel_pos.y > selection_rect.size.height) {
  638. return CURSOR_VSIZE;
  639. }
  640. return CURSOR_MOVE;
  641. }
  642. return get_default_cursor_shape();
  643. }
  644. void AnimationBezierTrackEdit::set_timeline(AnimationTimelineEdit *p_timeline) {
  645. timeline = p_timeline;
  646. timeline->connect("zoom_changed", callable_mp(this, &AnimationBezierTrackEdit::_zoom_changed));
  647. timeline->connect("name_limit_changed", callable_mp(this, &AnimationBezierTrackEdit::_zoom_changed));
  648. }
  649. void AnimationBezierTrackEdit::set_editor(AnimationTrackEditor *p_editor) {
  650. editor = p_editor;
  651. connect("clear_selection", callable_mp(editor, &AnimationTrackEditor::_clear_selection).bind(false));
  652. connect("select_key", callable_mp(editor, &AnimationTrackEditor::_key_selected), CONNECT_DEFERRED);
  653. connect("deselect_key", callable_mp(editor, &AnimationTrackEditor::_key_deselected), CONNECT_DEFERRED);
  654. }
  655. void AnimationBezierTrackEdit::_play_position_draw() {
  656. if (animation.is_null() || play_position_pos < 0) {
  657. return;
  658. }
  659. float scale = timeline->get_zoom_scale();
  660. int h = get_size().height;
  661. int limit = timeline->get_name_limit();
  662. int px = (-timeline->get_value() + play_position_pos) * scale + limit;
  663. if (px >= limit && px < (get_size().width)) {
  664. const Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  665. play_position->draw_line(Point2(px, 0), Point2(px, h), color, Math::round(2 * EDSCALE));
  666. }
  667. }
  668. void AnimationBezierTrackEdit::set_play_position(real_t p_pos) {
  669. play_position_pos = p_pos;
  670. play_position->queue_redraw();
  671. }
  672. void AnimationBezierTrackEdit::update_play_position() {
  673. play_position->queue_redraw();
  674. }
  675. void AnimationBezierTrackEdit::set_root(Node *p_root) {
  676. root = p_root;
  677. }
  678. void AnimationBezierTrackEdit::set_filtered(bool p_filtered) {
  679. is_filtered = p_filtered;
  680. if (animation.is_null()) {
  681. return;
  682. }
  683. String base_path = animation->track_get_path(selected_track);
  684. if (is_filtered) {
  685. if (root && root->has_node(base_path)) {
  686. Node *node = root->get_node(base_path);
  687. if (!node || !EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
  688. for (int i = 0; i < animation->get_track_count(); ++i) {
  689. if (animation->track_get_type(i) != Animation::TrackType::TYPE_BEZIER) {
  690. continue;
  691. }
  692. base_path = animation->track_get_path(i);
  693. if (root && root->has_node(base_path)) {
  694. node = root->get_node(base_path);
  695. if (!node) {
  696. continue; // No node, no filter.
  697. }
  698. if (!EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
  699. continue; // Skip track due to not selected.
  700. }
  701. set_animation_and_track(animation, i, read_only);
  702. break;
  703. }
  704. }
  705. }
  706. }
  707. }
  708. queue_redraw();
  709. }
  710. void AnimationBezierTrackEdit::auto_fit_vertically() {
  711. int track_count = animation->get_track_count();
  712. real_t minimum_value = Math::INF;
  713. real_t maximum_value = -Math::INF;
  714. int nb_track_visible = 0;
  715. for (int i = 0; i < track_count; ++i) {
  716. if (!_is_track_curves_displayed(i) || locked_tracks.has(i)) {
  717. continue;
  718. }
  719. int key_count = animation->track_get_key_count(i);
  720. for (int j = 0; j < key_count; ++j) {
  721. real_t value = animation->bezier_track_get_key_value(i, j);
  722. minimum_value = MIN(value, minimum_value);
  723. maximum_value = MAX(value, maximum_value);
  724. // We also want to includes the handles...
  725. Vector2 in_vec = animation->bezier_track_get_key_in_handle(i, j);
  726. Vector2 out_vec = animation->bezier_track_get_key_out_handle(i, j);
  727. minimum_value = MIN(value + in_vec.y, minimum_value);
  728. maximum_value = MAX(value + in_vec.y, maximum_value);
  729. minimum_value = MIN(value + out_vec.y, minimum_value);
  730. maximum_value = MAX(value + out_vec.y, maximum_value);
  731. }
  732. nb_track_visible++;
  733. }
  734. if (nb_track_visible == 0) {
  735. // No visible track... we will not adjust the vertical zoom
  736. return;
  737. }
  738. if (Math::is_finite(minimum_value) && Math::is_finite(maximum_value)) {
  739. _zoom_vertically(minimum_value, maximum_value);
  740. queue_redraw();
  741. }
  742. }
  743. void AnimationBezierTrackEdit::_zoom_vertically(real_t p_minimum_value, real_t p_maximum_value) {
  744. real_t target_height = p_maximum_value - p_minimum_value;
  745. if (target_height <= CMP_EPSILON) {
  746. timeline_v_scroll = p_maximum_value;
  747. return;
  748. }
  749. timeline_v_scroll = (p_maximum_value + p_minimum_value) / 2.0;
  750. timeline_v_zoom = target_height / ((get_size().height - timeline->get_size().height) * 0.9);
  751. }
  752. void AnimationBezierTrackEdit::_zoom_changed() {
  753. queue_redraw();
  754. play_position->queue_redraw();
  755. }
  756. void AnimationBezierTrackEdit::_update_locked_tracks_after(int p_track) {
  757. if (locked_tracks.has(p_track)) {
  758. locked_tracks.erase(p_track);
  759. }
  760. Vector<int> updated_locked_tracks;
  761. for (const int &E : locked_tracks) {
  762. updated_locked_tracks.push_back(E);
  763. }
  764. locked_tracks.clear();
  765. for (int i = 0; i < updated_locked_tracks.size(); ++i) {
  766. if (updated_locked_tracks[i] > p_track) {
  767. locked_tracks.insert(updated_locked_tracks[i] - 1);
  768. } else {
  769. locked_tracks.insert(updated_locked_tracks[i]);
  770. }
  771. }
  772. }
  773. void AnimationBezierTrackEdit::_update_hidden_tracks_after(int p_track) {
  774. if (hidden_tracks.has(p_track)) {
  775. hidden_tracks.erase(p_track);
  776. }
  777. Vector<int> updated_hidden_tracks;
  778. for (const int &E : hidden_tracks) {
  779. updated_hidden_tracks.push_back(E);
  780. }
  781. hidden_tracks.clear();
  782. for (int i = 0; i < updated_hidden_tracks.size(); ++i) {
  783. if (updated_hidden_tracks[i] > p_track) {
  784. hidden_tracks.insert(updated_hidden_tracks[i] - 1);
  785. } else {
  786. hidden_tracks.insert(updated_hidden_tracks[i]);
  787. }
  788. }
  789. }
  790. String AnimationBezierTrackEdit::get_tooltip(const Point2 &p_pos) const {
  791. return Control::get_tooltip(p_pos);
  792. }
  793. void AnimationBezierTrackEdit::_clear_selection() {
  794. selection.clear();
  795. emit_signal(SNAME("clear_selection"));
  796. queue_redraw();
  797. }
  798. void AnimationBezierTrackEdit::_change_selected_keys_handle_mode(Animation::HandleMode p_mode, bool p_auto) {
  799. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  800. undo_redo->create_action(TTR("Update Selected Key Handles"), UndoRedo::MERGE_DISABLE, animation.ptr());
  801. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  802. const IntPair track_key_pair = E->get();
  803. 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);
  804. 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));
  805. 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));
  806. 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);
  807. }
  808. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  809. if (ape) {
  810. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  811. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  812. }
  813. undo_redo->commit_action();
  814. }
  815. void AnimationBezierTrackEdit::_clear_selection_for_anim(const Ref<Animation> &p_anim) {
  816. if (!(animation == p_anim) || !is_visible()) {
  817. return;
  818. }
  819. _clear_selection();
  820. }
  821. void AnimationBezierTrackEdit::_select_at_anim(const Ref<Animation> &p_anim, int p_track, real_t p_pos, bool p_single) {
  822. if (!(animation == p_anim) || !is_visible()) {
  823. return;
  824. }
  825. int idx = animation->track_find_key(p_track, p_pos, Animation::FIND_MODE_APPROX);
  826. ERR_FAIL_COND(idx < 0);
  827. selection.insert(IntPair(p_track, idx));
  828. emit_signal(SNAME("select_key"), idx, p_single, p_track);
  829. queue_redraw();
  830. }
  831. void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
  832. ERR_FAIL_COND(p_event.is_null());
  833. if (panner->gui_input(p_event)) {
  834. accept_event();
  835. return;
  836. }
  837. if (p_event->is_pressed()) {
  838. if (ED_IS_SHORTCUT("animation_editor/duplicate_selected_keys", p_event)) {
  839. if (!read_only) {
  840. duplicate_selected_keys(-1.0, false);
  841. }
  842. accept_event();
  843. }
  844. if (ED_IS_SHORTCUT("animation_editor/cut_selected_keys", p_event)) {
  845. if (!read_only) {
  846. copy_selected_keys(true);
  847. }
  848. accept_event();
  849. }
  850. if (ED_IS_SHORTCUT("animation_editor/copy_selected_keys", p_event)) {
  851. if (!read_only) {
  852. copy_selected_keys(false);
  853. }
  854. accept_event();
  855. }
  856. if (ED_IS_SHORTCUT("animation_editor/paste_keys", p_event)) {
  857. if (!read_only) {
  858. paste_keys(-1.0, false);
  859. }
  860. accept_event();
  861. }
  862. if (ED_IS_SHORTCUT("animation_editor/delete_selection", p_event)) {
  863. if (!read_only) {
  864. delete_selection();
  865. }
  866. accept_event();
  867. }
  868. }
  869. Ref<InputEventKey> key_press = p_event;
  870. if (key_press.is_valid() && key_press->is_pressed()) {
  871. if (ED_IS_SHORTCUT("animation_bezier_editor/focus", p_event)) {
  872. SelectionSet focused_keys;
  873. if (selection.is_empty()) {
  874. for (int i = 0; i < edit_points.size(); ++i) {
  875. IntPair key_pair = IntPair(edit_points[i].track, edit_points[i].key);
  876. focused_keys.insert(key_pair);
  877. }
  878. } else {
  879. for (const IntPair &E : selection) {
  880. focused_keys.insert(E);
  881. if (E.second > 0) {
  882. IntPair previous_key = IntPair(E.first, E.second - 1);
  883. focused_keys.insert(previous_key);
  884. }
  885. if (E.second < animation->track_get_key_count(E.first) - 1) {
  886. IntPair next_key = IntPair(E.first, E.second + 1);
  887. focused_keys.insert(next_key);
  888. }
  889. }
  890. }
  891. if (focused_keys.is_empty()) {
  892. accept_event();
  893. return;
  894. }
  895. real_t minimum_time = Math::INF;
  896. real_t maximum_time = -Math::INF;
  897. real_t minimum_value = Math::INF;
  898. real_t maximum_value = -Math::INF;
  899. for (const IntPair &E : focused_keys) {
  900. IntPair key_pair = E;
  901. real_t time = animation->track_get_key_time(key_pair.first, key_pair.second);
  902. real_t value = animation->bezier_track_get_key_value(key_pair.first, key_pair.second);
  903. minimum_time = MIN(time, minimum_time);
  904. maximum_time = MAX(time, maximum_time);
  905. minimum_value = MIN(value, minimum_value);
  906. maximum_value = MAX(value, maximum_value);
  907. }
  908. float width = get_size().width - timeline->get_name_limit() - timeline->get_buttons_width();
  909. float padding = width * 0.1;
  910. float desired_scale = (width - padding / 2.0) / (maximum_time - minimum_time);
  911. minimum_time = MAX(0, minimum_time - (padding / 2.0) / desired_scale);
  912. float zv = Math::pow(100 / desired_scale, 0.125f);
  913. if (zv < 1) {
  914. zv = Math::pow(desired_scale / 100, 0.125f) - 1;
  915. zv = 1 - zv;
  916. }
  917. float zoom_value = timeline->get_zoom()->get_max() - zv;
  918. if (Math::is_finite(minimum_time) && Math::is_finite(maximum_time) && maximum_time - minimum_time > CMP_EPSILON) {
  919. timeline->get_zoom()->set_value(zoom_value);
  920. callable_mp((Range *)timeline, &Range::set_value).call_deferred(minimum_time);
  921. }
  922. if (Math::is_finite(minimum_value) && Math::is_finite(maximum_value)) {
  923. _zoom_vertically(minimum_value, maximum_value);
  924. }
  925. queue_redraw();
  926. accept_event();
  927. return;
  928. } else if (ED_IS_SHORTCUT("animation_bezier_editor/select_all_keys", p_event)) {
  929. for (int i = 0; i < edit_points.size(); ++i) {
  930. _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), i == 0);
  931. }
  932. queue_redraw();
  933. accept_event();
  934. return;
  935. } else if (ED_IS_SHORTCUT("animation_bezier_editor/deselect_all_keys", p_event)) {
  936. selection.clear();
  937. emit_signal(SNAME("clear_selection"));
  938. queue_redraw();
  939. accept_event();
  940. return;
  941. }
  942. }
  943. Ref<InputEventMouseButton> mb = p_event;
  944. int limit = timeline->get_name_limit();
  945. if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  946. menu_insert_key = mb->get_position();
  947. if (menu_insert_key.x >= limit && menu_insert_key.x <= get_size().width) {
  948. if (!read_only) {
  949. Vector2 popup_pos = get_screen_position() + mb->get_position();
  950. bool selected = _try_select_at_ui_pos(mb->get_position(), mb->is_shift_pressed(), false);
  951. menu->clear();
  952. menu->add_icon_item(bezier_icon, TTR("Insert Key Here"), MENU_KEY_INSERT);
  953. if (selected || selection.size()) {
  954. menu->add_separator();
  955. menu->add_icon_item(get_editor_theme_icon(SNAME("Duplicate")), TTR("Duplicate Selected Key(s)"), MENU_KEY_DUPLICATE);
  956. menu->add_icon_item(get_editor_theme_icon(SNAME("ActionCut")), TTR("Cut Selected Key(s)"), MENU_KEY_CUT);
  957. menu->add_icon_item(get_editor_theme_icon(SNAME("ActionCopy")), TTR("Copy Selected Key(s)"), MENU_KEY_COPY);
  958. }
  959. if (editor->is_key_clipboard_active()) {
  960. menu->add_icon_item(get_editor_theme_icon(SNAME("ActionPaste")), TTR("Paste Key(s)"), MENU_KEY_PASTE);
  961. }
  962. if (selected || selection.size()) {
  963. menu->add_separator();
  964. menu->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Delete Selected Key(s)"), MENU_KEY_DELETE);
  965. menu->add_separator();
  966. menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesFree")), TTR("Make Handles Free"), MENU_KEY_SET_HANDLE_FREE);
  967. menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesLinear")), TTR("Make Handles Linear"), MENU_KEY_SET_HANDLE_LINEAR);
  968. menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesBalanced")), TTR("Make Handles Balanced"), MENU_KEY_SET_HANDLE_BALANCED);
  969. menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesMirror")), TTR("Make Handles Mirrored"), MENU_KEY_SET_HANDLE_MIRRORED);
  970. menu->add_separator();
  971. menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesBalanced")), TTR("Make Handles Balanced (Auto Tangent)"), MENU_KEY_SET_HANDLE_AUTO_BALANCED);
  972. menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesMirror")), TTR("Make Handles Mirrored (Auto Tangent)"), MENU_KEY_SET_HANDLE_AUTO_MIRRORED);
  973. }
  974. if (menu->get_item_count()) {
  975. menu->reset_size();
  976. menu->set_position(popup_pos);
  977. menu->popup();
  978. }
  979. }
  980. }
  981. }
  982. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  983. Point2 pos = mb->get_position();
  984. bool no_mod_key_pressed = !mb->is_alt_pressed() && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed();
  985. if (mb->is_double_click() && !moving_selection && no_mod_key_pressed) {
  986. int x = pos.x - timeline->get_name_limit();
  987. float ofs = x / timeline->get_zoom_scale() + timeline->get_value();
  988. emit_signal(SNAME("timeline_changed"), ofs, false);
  989. }
  990. for (const KeyValue<int, Rect2> &E : subtracks) {
  991. if (E.value.has_point(mb->get_position())) {
  992. if (!locked_tracks.has(E.key) && !hidden_tracks.has(E.key)) {
  993. set_animation_and_track(animation, E.key, read_only);
  994. _clear_selection();
  995. }
  996. return;
  997. }
  998. }
  999. for (const KeyValue<int, RBMap<int, Rect2>> &E : subtrack_icons) {
  1000. int track = E.key;
  1001. RBMap<int, Rect2> track_icons = E.value;
  1002. for (const KeyValue<int, Rect2> &I : track_icons) {
  1003. if (I.value.has_point(mb->get_position())) {
  1004. if (I.key == REMOVE_ICON) {
  1005. if (!read_only) {
  1006. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1007. undo_redo->create_action("Remove Bezier Track", UndoRedo::MERGE_DISABLE, animation.ptr());
  1008. undo_redo->add_do_method(this, "_update_locked_tracks_after", track);
  1009. undo_redo->add_do_method(this, "_update_hidden_tracks_after", track);
  1010. undo_redo->add_do_method(animation.ptr(), "remove_track", track);
  1011. undo_redo->add_undo_method(animation.ptr(), "add_track", Animation::TrackType::TYPE_BEZIER, track);
  1012. undo_redo->add_undo_method(animation.ptr(), "track_set_path", track, animation->track_get_path(track));
  1013. for (int i = 0; i < animation->track_get_key_count(track); ++i) {
  1014. undo_redo->add_undo_method(
  1015. this,
  1016. "_bezier_track_insert_key_at_anim",
  1017. animation,
  1018. track,
  1019. animation->track_get_key_time(track, i),
  1020. animation->bezier_track_get_key_value(track, i),
  1021. animation->bezier_track_get_key_in_handle(track, i),
  1022. animation->bezier_track_get_key_out_handle(track, i),
  1023. animation->bezier_track_get_key_handle_mode(track, i));
  1024. }
  1025. undo_redo->commit_action();
  1026. selected_track = CLAMP(selected_track, 0, animation->get_track_count() - 1);
  1027. }
  1028. return;
  1029. } else if (I.key == LOCK_ICON) {
  1030. if (locked_tracks.has(track)) {
  1031. locked_tracks.erase(track);
  1032. } else {
  1033. locked_tracks.insert(track);
  1034. if (selected_track == track) {
  1035. for (int i = 0; i < animation->get_track_count(); ++i) {
  1036. if (!locked_tracks.has(i) && animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) {
  1037. set_animation_and_track(animation, i, read_only);
  1038. break;
  1039. }
  1040. }
  1041. }
  1042. }
  1043. queue_redraw();
  1044. return;
  1045. } else if (I.key == VISIBILITY_ICON) {
  1046. if (hidden_tracks.has(track)) {
  1047. hidden_tracks.erase(track);
  1048. } else {
  1049. hidden_tracks.insert(track);
  1050. if (selected_track == track) {
  1051. for (int i = 0; i < animation->get_track_count(); ++i) {
  1052. if (!hidden_tracks.has(i) && animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) {
  1053. set_animation_and_track(animation, i, read_only);
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. }
  1059. Vector<int> visible_tracks;
  1060. for (int i = 0; i < animation->get_track_count(); ++i) {
  1061. if (!hidden_tracks.has(i) && animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) {
  1062. visible_tracks.push_back(i);
  1063. }
  1064. }
  1065. if (visible_tracks.size() == 1) {
  1066. solo_track = visible_tracks[0];
  1067. } else {
  1068. solo_track = -1;
  1069. }
  1070. queue_redraw();
  1071. return;
  1072. } else if (I.key == SOLO_ICON) {
  1073. if (solo_track == track) {
  1074. solo_track = -1;
  1075. hidden_tracks.clear();
  1076. } else {
  1077. if (hidden_tracks.has(track)) {
  1078. hidden_tracks.erase(track);
  1079. }
  1080. for (int i = 0; i < animation->get_track_count(); ++i) {
  1081. if (animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) {
  1082. if (i != track && !hidden_tracks.has(i)) {
  1083. hidden_tracks.insert(i);
  1084. }
  1085. }
  1086. }
  1087. set_animation_and_track(animation, track, read_only);
  1088. solo_track = track;
  1089. }
  1090. queue_redraw();
  1091. return;
  1092. }
  1093. return;
  1094. }
  1095. }
  1096. }
  1097. // Check this first, to allow manipulating key handles while ignoring keyframes before scaling/moving.
  1098. bool inside_selection_handles_rect = !read_only && selection_handles_rect.has_point(mb->get_position());
  1099. // First, check keyframe.
  1100. // Command/Control makes it ignore the keyframe, so control point editors can be force-edited.
  1101. if (!inside_selection_handles_rect && !mb->is_command_or_control_pressed()) {
  1102. if (_try_select_at_ui_pos(mb->get_position(), mb->is_shift_pressed(), true)) {
  1103. return;
  1104. }
  1105. }
  1106. // Second, check key handles.
  1107. for (int i = 0; i < edit_points.size(); i++) {
  1108. if (!read_only) {
  1109. if (edit_points[i].in_rect.has_point(mb->get_position())) {
  1110. moving_handle = -1;
  1111. moving_handle_key = edit_points[i].key;
  1112. moving_handle_track = edit_points[i].track;
  1113. moving_handle_left = animation->bezier_track_get_key_in_handle(edit_points[i].track, edit_points[i].key);
  1114. moving_handle_right = animation->bezier_track_get_key_out_handle(edit_points[i].track, edit_points[i].key);
  1115. queue_redraw();
  1116. return;
  1117. }
  1118. if (edit_points[i].out_rect.has_point(mb->get_position())) {
  1119. moving_handle = 1;
  1120. moving_handle_key = edit_points[i].key;
  1121. moving_handle_track = edit_points[i].track;
  1122. moving_handle_left = animation->bezier_track_get_key_in_handle(edit_points[i].track, edit_points[i].key);
  1123. moving_handle_right = animation->bezier_track_get_key_out_handle(edit_points[i].track, edit_points[i].key);
  1124. queue_redraw();
  1125. return;
  1126. }
  1127. }
  1128. }
  1129. // Box scaling/movement.
  1130. if (inside_selection_handles_rect) {
  1131. const Vector2i rel_pos = mb->get_position() - selection_rect.position;
  1132. scaling_selection_handles = Vector2i();
  1133. // Check which scaling handles are available.
  1134. if (selection_rect.size.width > CMP_EPSILON) {
  1135. if (rel_pos.x <= 0) {
  1136. scaling_selection_handles.x = -1;
  1137. } else if (rel_pos.x >= selection_rect.size.width) {
  1138. scaling_selection_handles.x = 1;
  1139. }
  1140. }
  1141. if (selection_rect.size.height > CMP_EPSILON) {
  1142. if (rel_pos.y <= 0) {
  1143. scaling_selection_handles.y = -1;
  1144. } else if (rel_pos.y >= selection_rect.size.height) {
  1145. scaling_selection_handles.y = 1;
  1146. }
  1147. }
  1148. if (scaling_selection_handles != Vector2i()) {
  1149. scaling_selection = true;
  1150. const float time = ((selection_rect.position.x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1151. const float h = (get_size().height / 2.0 - selection_rect.position.y) * timeline_v_zoom + timeline_v_scroll;
  1152. scaling_selection_pivot = Point2(time, h);
  1153. return;
  1154. }
  1155. // If not scaling, that means we're moving.
  1156. moving_selection_attempt = true;
  1157. moving_selection = false;
  1158. moving_selection_mouse_begin = mb->get_position();
  1159. // The pivot will be from the mouse click location, not a specific key.
  1160. moving_selection_from_key = -1;
  1161. moving_selection_from_track = selected_track;
  1162. moving_selection_offset = Vector2();
  1163. select_single_attempt = IntPair(-1, -1);
  1164. return;
  1165. }
  1166. // Insert new point.
  1167. if (mb->get_position().x >= limit && mb->get_position().x < get_size().width && mb->is_command_or_control_pressed()) {
  1168. float h = (get_size().height / 2.0 - mb->get_position().y) * timeline_v_zoom + timeline_v_scroll;
  1169. Array new_point = animation->make_default_bezier_key(h);
  1170. real_t time = ((mb->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1171. while (animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX) != -1) {
  1172. time += 0.0001;
  1173. }
  1174. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1175. undo_redo->create_action(TTR("Add Bezier Point"));
  1176. undo_redo->add_do_method(animation.ptr(), "bezier_track_insert_key", selected_track, time, new_point[0], Vector2(new_point[1], new_point[2]), Vector2(new_point[3], new_point[4]));
  1177. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", selected_track, time);
  1178. undo_redo->commit_action();
  1179. // Then attempt to move.
  1180. int index = animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX);
  1181. ERR_FAIL_COND(index == -1);
  1182. _clear_selection();
  1183. _select_at_anim(animation, selected_track, animation->track_get_key_time(selected_track, index), true);
  1184. moving_selection_attempt = true;
  1185. moving_selection = false;
  1186. moving_selection_mouse_begin = mb->get_position();
  1187. moving_selection_from_key = index;
  1188. moving_selection_from_track = selected_track;
  1189. moving_selection_offset = Vector2();
  1190. select_single_attempt = IntPair(-1, -1);
  1191. queue_redraw();
  1192. return;
  1193. }
  1194. // Box select.
  1195. if (mb->get_position().x >= limit && mb->get_position().x < get_size().width) {
  1196. box_selecting_attempt = true;
  1197. box_selecting = false;
  1198. box_selecting_add = false;
  1199. box_selection_from = mb->get_position();
  1200. return;
  1201. }
  1202. }
  1203. if (box_selecting_attempt && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1204. if (box_selecting) {
  1205. // Do actual select.
  1206. if (!box_selecting_add) {
  1207. _clear_selection();
  1208. }
  1209. Vector2 bs_from = box_selection_from;
  1210. Vector2 bs_to = box_selection_to;
  1211. if (bs_from.x > bs_to.x) {
  1212. SWAP(bs_from.x, bs_to.x);
  1213. }
  1214. if (bs_from.y > bs_to.y) {
  1215. SWAP(bs_from.y, bs_to.y);
  1216. }
  1217. Rect2 rect(bs_from, bs_to - bs_from);
  1218. bool track_set = false;
  1219. int j = 0;
  1220. for (int i = 0; i < edit_points.size(); i++) {
  1221. if (edit_points[i].point_rect.intersects(rect)) {
  1222. _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), j == 0 && !box_selecting_add);
  1223. if (!track_set) {
  1224. track_set = true;
  1225. set_animation_and_track(animation, edit_points[i].track, read_only);
  1226. }
  1227. j++;
  1228. }
  1229. }
  1230. } else {
  1231. _clear_selection(); // Clicked and nothing happened, so clear the selection.
  1232. // Select by clicking on curve.
  1233. int track_count = animation->get_track_count();
  1234. real_t animation_length = animation->get_length();
  1235. animation->set_length(real_t(INT_MAX)); // bezier_track_interpolate doesn't find keys if they exist beyond anim length.
  1236. real_t time = ((mb->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1237. for (int i = 0; i < track_count; ++i) {
  1238. if (animation->track_get_type(i) != Animation::TrackType::TYPE_BEZIER || hidden_tracks.has(i) || locked_tracks.has(i)) {
  1239. continue;
  1240. }
  1241. float track_h = animation->bezier_track_interpolate(i, time);
  1242. float track_height = _bezier_h_to_pixel(track_h);
  1243. if (std::abs(mb->get_position().y - track_height) < 10) {
  1244. set_animation_and_track(animation, i, read_only);
  1245. break;
  1246. }
  1247. }
  1248. animation->set_length(animation_length);
  1249. }
  1250. box_selecting_attempt = false;
  1251. box_selecting = false;
  1252. queue_redraw();
  1253. }
  1254. if (moving_selection_attempt && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1255. if (!read_only) {
  1256. if (moving_selection && (std::abs(moving_selection_offset.x) > CMP_EPSILON || std::abs(moving_selection_offset.y) > CMP_EPSILON)) {
  1257. // Commit it.
  1258. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1259. undo_redo->create_action(TTR("Move Bezier Points"));
  1260. List<AnimMoveRestore> to_restore;
  1261. List<Animation::HandleMode> to_restore_handle_modes;
  1262. // 1 - Remove the keys.
  1263. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1264. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->get().first, E->get().second);
  1265. }
  1266. // 2 - Remove overlapped keys.
  1267. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1268. real_t newtime = animation->track_get_key_time(E->get().first, E->get().second) + moving_selection_offset.x;
  1269. int idx = animation->track_find_key(E->get().first, newtime, Animation::FIND_MODE_APPROX);
  1270. if (idx == -1) {
  1271. continue;
  1272. }
  1273. if (selection.has(IntPair(E->get().first, idx))) {
  1274. continue; // Already in selection, don't save.
  1275. }
  1276. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", E->get().first, newtime);
  1277. AnimMoveRestore amr;
  1278. amr.key = animation->track_get_key_value(E->get().first, idx);
  1279. amr.track = E->get().first;
  1280. amr.time = newtime;
  1281. to_restore.push_back(amr);
  1282. to_restore_handle_modes.push_back(animation->bezier_track_get_key_handle_mode(E->get().first, idx));
  1283. }
  1284. // 3 - Move the keys (re-insert them).
  1285. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1286. real_t newpos = animation->track_get_key_time(E->get().first, E->get().second) + moving_selection_offset.x;
  1287. Array key = animation->track_get_key_value(E->get().first, E->get().second);
  1288. real_t h = key[0];
  1289. h += moving_selection_offset.y;
  1290. key[0] = h;
  1291. undo_redo->add_do_method(
  1292. this,
  1293. "_bezier_track_insert_key_at_anim",
  1294. animation,
  1295. E->get().first,
  1296. newpos,
  1297. key[0],
  1298. Vector2(key[1], key[2]),
  1299. Vector2(key[3], key[4]),
  1300. animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second));
  1301. }
  1302. // 4 - (undo) Remove inserted keys.
  1303. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1304. real_t newpos = animation->track_get_key_time(E->get().first, E->get().second) + moving_selection_offset.x;
  1305. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", E->get().first, newpos);
  1306. }
  1307. // 5 - (undo) Reinsert keys.
  1308. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1309. real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second);
  1310. Array key = animation->track_get_key_value(E->get().first, E->get().second);
  1311. undo_redo->add_undo_method(
  1312. this,
  1313. "_bezier_track_insert_key_at_anim",
  1314. animation,
  1315. E->get().first,
  1316. oldpos,
  1317. key[0],
  1318. Vector2(key[1], key[2]),
  1319. Vector2(key[3], key[4]),
  1320. animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second));
  1321. }
  1322. // 6 - (undo) Reinsert overlapped keys.
  1323. List<AnimMoveRestore>::ConstIterator restore_itr = to_restore.begin();
  1324. List<Animation::HandleMode>::ConstIterator handle_itr = to_restore_handle_modes.begin();
  1325. for (; restore_itr != to_restore.end() && handle_itr != to_restore_handle_modes.end(); ++restore_itr, ++handle_itr) {
  1326. const AnimMoveRestore &amr = *restore_itr;
  1327. Array key = amr.key;
  1328. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, 1);
  1329. undo_redo->add_undo_method(
  1330. this,
  1331. "_bezier_track_insert_key_at_anim",
  1332. animation,
  1333. amr.track,
  1334. amr.time,
  1335. key[0],
  1336. Vector2(key[1], key[2]),
  1337. Vector2(key[3], key[4]),
  1338. *handle_itr);
  1339. }
  1340. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1341. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1342. // 7 - Reselect.
  1343. int i = 0;
  1344. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1345. real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second);
  1346. real_t newpos = oldpos + moving_selection_offset.x;
  1347. undo_redo->add_do_method(this, "_select_at_anim", animation, E->get().first, newpos, i == 0);
  1348. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->get().first, oldpos, i == 0);
  1349. i++;
  1350. }
  1351. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1352. if (ape) {
  1353. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1354. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1355. }
  1356. undo_redo->commit_action();
  1357. } else if (select_single_attempt != IntPair(-1, -1)) {
  1358. selection.clear();
  1359. set_animation_and_track(animation, select_single_attempt.first, read_only);
  1360. _select_at_anim(animation, select_single_attempt.first, animation->track_get_key_time(select_single_attempt.first, select_single_attempt.second), true);
  1361. }
  1362. moving_selection = false;
  1363. moving_selection_attempt = false;
  1364. moving_selection_mouse_begin = Point2();
  1365. queue_redraw();
  1366. }
  1367. }
  1368. if (scaling_selection && mb.is_valid() && !read_only && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1369. if (std::abs(scaling_selection_scale.x - 1) > CMP_EPSILON || std::abs(scaling_selection_scale.y - 1) > CMP_EPSILON) {
  1370. // Scale it.
  1371. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1372. undo_redo->create_action(TTR("Scale Bezier Points"));
  1373. List<AnimMoveRestore> to_restore;
  1374. List<Animation::HandleMode> to_restore_handle_modes;
  1375. // 1 - Remove the keys.
  1376. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1377. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->get().first, E->get().second);
  1378. }
  1379. // 2 - Remove overlapped keys.
  1380. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1381. real_t newtime = animation->track_get_key_time(E->get().first, E->get().second);
  1382. newtime += -scaling_selection_offset.x + (newtime - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
  1383. int idx = animation->track_find_key(E->get().first, newtime, Animation::FIND_MODE_APPROX);
  1384. if (idx == -1) {
  1385. continue;
  1386. }
  1387. if (selection.has(IntPair(E->get().first, idx))) {
  1388. continue; // Already in selection, don't save.
  1389. }
  1390. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", E->get().first, newtime);
  1391. AnimMoveRestore amr;
  1392. amr.key = animation->track_get_key_value(E->get().first, idx);
  1393. amr.track = E->get().first;
  1394. amr.time = newtime;
  1395. to_restore.push_back(amr);
  1396. to_restore_handle_modes.push_back(animation->bezier_track_get_key_handle_mode(E->get().first, idx));
  1397. }
  1398. // 3 - Scale the keys (re-insert them).
  1399. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1400. real_t newpos = animation->track_get_key_time(E->get().first, E->get().second);
  1401. newpos += -scaling_selection_offset.x + (newpos - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
  1402. Array key = animation->track_get_key_value(E->get().first, E->get().second);
  1403. real_t h = key[0];
  1404. h += -scaling_selection_offset.y + (h - scaling_selection_pivot.y) * (scaling_selection_scale.y - 1);
  1405. key[0] = h;
  1406. undo_redo->add_do_method(
  1407. this,
  1408. "_bezier_track_insert_key_at_anim",
  1409. animation,
  1410. E->get().first,
  1411. newpos,
  1412. key[0],
  1413. Vector2(key[1], key[2]),
  1414. Vector2(key[3], key[4]),
  1415. animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second));
  1416. }
  1417. // 4 - (undo) Remove inserted keys.
  1418. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1419. real_t newpos = animation->track_get_key_time(E->get().first, E->get().second);
  1420. newpos += -scaling_selection_offset.x + (newpos - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
  1421. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", E->get().first, newpos);
  1422. }
  1423. // 5 - (undo) Reinsert keys.
  1424. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1425. real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second);
  1426. Array key = animation->track_get_key_value(E->get().first, E->get().second);
  1427. undo_redo->add_undo_method(
  1428. this,
  1429. "_bezier_track_insert_key_at_anim",
  1430. animation,
  1431. E->get().first,
  1432. oldpos,
  1433. key[0],
  1434. Vector2(key[1], key[2]),
  1435. Vector2(key[3], key[4]),
  1436. animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second));
  1437. }
  1438. // 6 - (undo) Reinsert overlapped keys.
  1439. List<AnimMoveRestore>::ConstIterator restore_itr = to_restore.begin();
  1440. List<Animation::HandleMode>::ConstIterator handle_itr = to_restore_handle_modes.begin();
  1441. for (; restore_itr != to_restore.end() && handle_itr != to_restore_handle_modes.end(); ++restore_itr, ++handle_itr) {
  1442. const AnimMoveRestore &amr = *restore_itr;
  1443. Array key = amr.key;
  1444. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, 1);
  1445. undo_redo->add_undo_method(
  1446. this,
  1447. "_bezier_track_insert_key_at_anim",
  1448. animation,
  1449. amr.track,
  1450. amr.time,
  1451. key[0],
  1452. Vector2(key[1], key[2]),
  1453. Vector2(key[3], key[4]),
  1454. *handle_itr);
  1455. }
  1456. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1457. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1458. // 7 - Reselect.
  1459. int i = 0;
  1460. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1461. real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second);
  1462. real_t newpos = animation->track_get_key_time(E->get().first, E->get().second);
  1463. newpos += -scaling_selection_offset.x + (newpos - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
  1464. undo_redo->add_do_method(this, "_select_at_anim", animation, E->get().first, newpos, i == 0);
  1465. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->get().first, oldpos, i == 0);
  1466. i++;
  1467. }
  1468. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1469. if (ape) {
  1470. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1471. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1472. }
  1473. undo_redo->commit_action();
  1474. }
  1475. scaling_selection = false;
  1476. scaling_selection_scale = Vector2(1, 1);
  1477. scaling_selection_offset = Vector2();
  1478. queue_redraw();
  1479. }
  1480. Ref<InputEventMouseMotion> mm = p_event;
  1481. if (moving_selection_attempt && mm.is_valid()) {
  1482. if (!moving_selection) {
  1483. moving_selection = true;
  1484. select_single_attempt = IntPair(-1, -1);
  1485. }
  1486. if (!read_only) {
  1487. float y = (get_size().height / 2.0 - mm->get_position().y) * timeline_v_zoom + timeline_v_scroll;
  1488. float moving_selection_begin_time = ((moving_selection_mouse_begin.x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1489. float new_time = ((mm->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1490. float moving_selection_pivot = moving_selection_from_key != -1 ? animation->track_get_key_time(moving_selection_from_track, moving_selection_from_key) : 0;
  1491. float time_delta = new_time - moving_selection_begin_time;
  1492. float snapped_time = editor->snap_time(moving_selection_pivot + time_delta);
  1493. float time_offset = 0.0;
  1494. if (std::abs(moving_selection_offset.x) > CMP_EPSILON || (snapped_time > moving_selection_pivot && time_delta > CMP_EPSILON) || (snapped_time < moving_selection_pivot && time_delta < -CMP_EPSILON)) {
  1495. time_offset = snapped_time - moving_selection_pivot;
  1496. }
  1497. float moving_selection_begin_value;
  1498. if (moving_selection_from_key == -1) {
  1499. moving_selection_begin_value = (get_size().height / 2.0 - moving_selection_mouse_begin.y) * timeline_v_zoom + timeline_v_scroll;
  1500. } else {
  1501. moving_selection_begin_value = animation->bezier_track_get_key_value(moving_selection_from_track, moving_selection_from_key);
  1502. }
  1503. float y_offset = y - moving_selection_begin_value;
  1504. moving_selection_offset = Vector2(time_offset, y_offset);
  1505. }
  1506. additional_moving_handle_lefts.clear();
  1507. additional_moving_handle_rights.clear();
  1508. queue_redraw();
  1509. }
  1510. if (box_selecting_attempt && mm.is_valid()) {
  1511. if (!box_selecting) {
  1512. box_selecting = true;
  1513. box_selecting_add = mm->is_shift_pressed();
  1514. }
  1515. box_selection_to = mm->get_position();
  1516. queue_redraw();
  1517. }
  1518. if (scaling_selection && mm.is_valid() && !read_only) {
  1519. Point2 mp = mm->get_position();
  1520. const int handle_length = Math::round((selection_handles_rect.size.width - selection_rect.size.width) / 4.0);
  1521. Point2 rel_pos;
  1522. // Calculate the scale according with the distance between the mouse's position (adjusted so that the cursor appears inside the handles)
  1523. // and the opposite end of the `selection_rect`.
  1524. if (scaling_selection_handles.x != 0) {
  1525. if (scaling_selection_handles.x == 1) { // Right Handle
  1526. const int handle_adjust = Math::round(mp.x - (scaling_selection_scale.x >= 0 ? selection_rect.position.x : (selection_rect.position.x + selection_rect.size.width)));
  1527. mp.x -= MIN(Math::abs(handle_adjust), handle_length) * scaling_selection_handles.x * SIGN(handle_adjust);
  1528. if (editor->is_snap_keys_enabled()) {
  1529. mp.x = editor->snap_time((mp.x - limit) / timeline->get_zoom_scale(), true) + timeline->get_value();
  1530. mp.x = (mp.x - timeline->get_value()) * timeline->get_zoom_scale() + limit;
  1531. }
  1532. rel_pos.x = scaling_selection_scale.x >= 0 ? (mp.x - selection_rect.position.x) : selection_rect.position.x + selection_rect.size.width - mp.x;
  1533. } else { // Left Handle
  1534. const int handle_adjust = Math::round((scaling_selection_scale.x >= 0 ? (selection_rect.position.x + selection_rect.size.width) : selection_rect.position.x) - mp.x);
  1535. mp.x -= MIN(Math::abs(handle_adjust), handle_length) * scaling_selection_handles.x * SIGN(handle_adjust);
  1536. const float x = editor->snap_time((mp.x - limit) / timeline->get_zoom_scale(), true) + timeline->get_value();
  1537. if (editor->is_snap_keys_enabled()) {
  1538. mp.x = (x - timeline->get_value()) * timeline->get_zoom_scale() + limit;
  1539. }
  1540. rel_pos.x = scaling_selection_scale.x >= 0 ? (selection_rect.position.x + selection_rect.size.width - mp.x) : (mp.x - selection_rect.position.x);
  1541. scaling_selection_offset.x = scaling_selection_pivot.x - x;
  1542. }
  1543. scaling_selection_scale.x *= rel_pos.x / selection_rect.size.width;
  1544. if (scaling_selection_scale.x == 0) {
  1545. scaling_selection_scale.x = CMP_EPSILON;
  1546. }
  1547. }
  1548. if (scaling_selection_handles.y != 0) {
  1549. if (scaling_selection_handles.y == 1) { // Bottom Handle
  1550. const int handle_adjust = Math::round(mp.y - (scaling_selection_scale.y >= 0 ? selection_rect.position.y : (selection_rect.position.y + selection_rect.size.height)));
  1551. mp.y -= MIN(Math::abs(handle_adjust), handle_length) * scaling_selection_handles.y * SIGN(handle_adjust);
  1552. if (scaling_selection_scale.y >= 0) {
  1553. rel_pos.y = mp.y - selection_rect.position.y;
  1554. } else {
  1555. rel_pos.y = selection_rect.position.y + selection_rect.size.height - mp.y;
  1556. }
  1557. } else { // Top Handle
  1558. const int handle_adjust = Math::round((scaling_selection_scale.y >= 0 ? (selection_rect.position.y + selection_rect.size.height) : selection_rect.position.y) - mp.y);
  1559. mp.y -= MIN(Math::abs(handle_adjust), handle_length) * scaling_selection_handles.y * SIGN(handle_adjust);
  1560. if (scaling_selection_scale.y >= 0) {
  1561. rel_pos.y = selection_rect.position.y + selection_rect.size.height - mp.y;
  1562. } else {
  1563. rel_pos.y = mp.y - selection_rect.position.y;
  1564. }
  1565. const float h = (get_size().height / 2.0 - mp.y) * timeline_v_zoom + timeline_v_scroll;
  1566. scaling_selection_offset.y = scaling_selection_pivot.y - h;
  1567. }
  1568. scaling_selection_scale.y *= rel_pos.y / selection_rect.size.height;
  1569. if (scaling_selection_scale.y == 0) {
  1570. scaling_selection_scale.y = CMP_EPSILON;
  1571. }
  1572. }
  1573. queue_redraw();
  1574. }
  1575. if ((moving_handle == 1 || moving_handle == -1) && mm.is_valid()) {
  1576. float y = (get_size().height / 2.0 - mm->get_position().y) * timeline_v_zoom + timeline_v_scroll;
  1577. float x = editor->snap_time((mm->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1578. Vector2 key_pos = Vector2(animation->track_get_key_time(moving_handle_track, moving_handle_key), animation->bezier_track_get_key_value(moving_handle_track, moving_handle_key));
  1579. Vector2 moving_handle_value = Vector2(x, y) - key_pos;
  1580. moving_handle_left = animation->bezier_track_get_key_in_handle(moving_handle_track, moving_handle_key);
  1581. moving_handle_right = animation->bezier_track_get_key_out_handle(moving_handle_track, moving_handle_key);
  1582. if (moving_handle == -1) {
  1583. moving_handle_left = moving_handle_value;
  1584. Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key);
  1585. if (handle_mode == Animation::HANDLE_MODE_BALANCED) {
  1586. real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
  1587. Transform2D xform;
  1588. xform.set_scale(Vector2(1.0, 1.0 / ratio));
  1589. Vector2 vec_out = xform.xform(moving_handle_right);
  1590. Vector2 vec_in = xform.xform(moving_handle_left);
  1591. moving_handle_right = xform.affine_inverse().xform(-vec_in.normalized() * vec_out.length());
  1592. } else if (handle_mode == Animation::HANDLE_MODE_MIRRORED) {
  1593. moving_handle_right = -moving_handle_left;
  1594. }
  1595. } else if (moving_handle == 1) {
  1596. moving_handle_right = moving_handle_value;
  1597. Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key);
  1598. if (handle_mode == Animation::HANDLE_MODE_BALANCED) {
  1599. real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
  1600. Transform2D xform;
  1601. xform.set_scale(Vector2(1.0, 1.0 / ratio));
  1602. Vector2 vec_in = xform.xform(moving_handle_left);
  1603. Vector2 vec_out = xform.xform(moving_handle_right);
  1604. moving_handle_left = xform.affine_inverse().xform(-vec_out.normalized() * vec_in.length());
  1605. } else if (handle_mode == Animation::HANDLE_MODE_MIRRORED) {
  1606. moving_handle_left = -moving_handle_right;
  1607. }
  1608. }
  1609. queue_redraw();
  1610. }
  1611. if ((moving_handle == -1 || moving_handle == 1) && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1612. if (!read_only) {
  1613. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1614. undo_redo->create_action(TTR("Move Bezier Points"));
  1615. if (moving_handle == -1) {
  1616. real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
  1617. undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_in_handle", moving_handle_track, moving_handle_key, moving_handle_left, ratio);
  1618. 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);
  1619. } else if (moving_handle == 1) {
  1620. real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
  1621. undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_out_handle", moving_handle_track, moving_handle_key, moving_handle_right, ratio);
  1622. 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);
  1623. }
  1624. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1625. if (ape) {
  1626. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1627. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1628. }
  1629. undo_redo->commit_action();
  1630. moving_handle = 0;
  1631. queue_redraw();
  1632. }
  1633. }
  1634. }
  1635. bool AnimationBezierTrackEdit::_try_select_at_ui_pos(const Point2 &p_pos, bool p_aggregate, bool p_deselectable) {
  1636. for (int i = 0; i < edit_points.size(); i++) {
  1637. // Path 2D editing in the 3D and 2D editors works the same way. (?)
  1638. if (edit_points[i].point_rect.has_point(p_pos)) {
  1639. IntPair pair = IntPair(edit_points[i].track, edit_points[i].key);
  1640. if (p_aggregate) {
  1641. // Add to selection.
  1642. if (selection.has(pair)) {
  1643. if (p_deselectable) {
  1644. selection.erase(pair);
  1645. emit_signal(SNAME("deselect_key"), edit_points[i].key, edit_points[i].track);
  1646. }
  1647. } else {
  1648. _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), false);
  1649. }
  1650. queue_redraw();
  1651. select_single_attempt = IntPair(-1, -1);
  1652. } else {
  1653. if (p_deselectable) {
  1654. moving_selection_attempt = true;
  1655. moving_selection_from_key = pair.second;
  1656. moving_selection_from_track = pair.first;
  1657. moving_selection_mouse_begin = p_pos;
  1658. moving_selection_offset = Vector2();
  1659. moving_handle_track = pair.first;
  1660. moving_handle_left = animation->bezier_track_get_key_in_handle(pair.first, pair.second);
  1661. moving_handle_right = animation->bezier_track_get_key_out_handle(pair.first, pair.second);
  1662. if (selection.has(pair)) {
  1663. moving_selection = false;
  1664. } else {
  1665. moving_selection = true;
  1666. }
  1667. select_single_attempt = pair;
  1668. }
  1669. set_animation_and_track(animation, pair.first, read_only);
  1670. if (!selection.has(pair)) {
  1671. selection.clear();
  1672. _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), true);
  1673. }
  1674. }
  1675. return true;
  1676. }
  1677. }
  1678. return false;
  1679. }
  1680. void AnimationBezierTrackEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1681. Ref<InputEventMouseMotion> mm = p_event;
  1682. if (mm.is_valid()) {
  1683. if (mm->get_position().x > timeline->get_name_limit()) {
  1684. timeline_v_scroll += p_scroll_vec.y * timeline_v_zoom;
  1685. timeline_v_scroll = CLAMP(timeline_v_scroll, -100000, 100000);
  1686. timeline->set_value(timeline->get_value() - p_scroll_vec.x / timeline->get_zoom_scale());
  1687. } else {
  1688. track_v_scroll += p_scroll_vec.y;
  1689. if (track_v_scroll < -track_v_scroll_max) {
  1690. track_v_scroll = -track_v_scroll_max;
  1691. } else if (track_v_scroll > 0) {
  1692. track_v_scroll = 0;
  1693. }
  1694. }
  1695. queue_redraw();
  1696. }
  1697. }
  1698. void AnimationBezierTrackEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1699. const float v_zoom_orig = timeline_v_zoom;
  1700. Ref<InputEventWithModifiers> iewm = p_event;
  1701. if (iewm.is_valid() && iewm->is_alt_pressed()) {
  1702. // Alternate zoom (doesn't affect timeline).
  1703. timeline_v_zoom = CLAMP(timeline_v_zoom / p_zoom_factor, 0.000001, 100000);
  1704. } else {
  1705. float zoom_factor = p_zoom_factor > 1.0 ? AnimationTimelineEdit::SCROLL_ZOOM_FACTOR_IN : AnimationTimelineEdit::SCROLL_ZOOM_FACTOR_OUT;
  1706. timeline->_zoom_callback(zoom_factor, p_origin, p_event);
  1707. }
  1708. timeline_v_scroll = timeline_v_scroll + (p_origin.y - get_size().y / 2.0) * (timeline_v_zoom - v_zoom_orig);
  1709. queue_redraw();
  1710. }
  1711. float AnimationBezierTrackEdit::get_bezier_key_value(Array p_bezier_key_array) {
  1712. return p_bezier_key_array[0];
  1713. }
  1714. void AnimationBezierTrackEdit::_menu_selected(int p_index) {
  1715. int limit = timeline->get_name_limit();
  1716. real_t time = ((menu_insert_key.x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1717. switch (p_index) {
  1718. case MENU_KEY_INSERT: {
  1719. if (animation->get_track_count() > 0) {
  1720. if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
  1721. time = editor->snap_time(time);
  1722. }
  1723. while (animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX) != -1) {
  1724. time += 0.001;
  1725. }
  1726. float h = (get_size().height / 2.0 - menu_insert_key.y) * timeline_v_zoom + timeline_v_scroll;
  1727. Array new_point = animation->make_default_bezier_key(h);
  1728. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1729. undo_redo->create_action(TTR("Add Bezier Point"));
  1730. undo_redo->add_do_method(animation.ptr(), "track_insert_key", selected_track, time, new_point);
  1731. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1732. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", selected_track, time);
  1733. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1734. if (ape) {
  1735. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1736. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1737. }
  1738. undo_redo->commit_action();
  1739. queue_redraw();
  1740. }
  1741. } break;
  1742. case MENU_KEY_DUPLICATE: {
  1743. duplicate_selected_keys(time, true);
  1744. } break;
  1745. case MENU_KEY_DELETE: {
  1746. delete_selection();
  1747. } break;
  1748. case MENU_KEY_CUT: {
  1749. copy_selected_keys(true);
  1750. } break;
  1751. case MENU_KEY_COPY: {
  1752. copy_selected_keys(false);
  1753. } break;
  1754. case MENU_KEY_PASTE: {
  1755. paste_keys(time, true);
  1756. } break;
  1757. case MENU_KEY_SET_HANDLE_FREE: {
  1758. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_FREE);
  1759. } break;
  1760. case MENU_KEY_SET_HANDLE_LINEAR: {
  1761. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_LINEAR);
  1762. } break;
  1763. case MENU_KEY_SET_HANDLE_BALANCED: {
  1764. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_BALANCED);
  1765. } break;
  1766. case MENU_KEY_SET_HANDLE_MIRRORED: {
  1767. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_MIRRORED);
  1768. } break;
  1769. case MENU_KEY_SET_HANDLE_AUTO_BALANCED: {
  1770. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_BALANCED, true);
  1771. } break;
  1772. case MENU_KEY_SET_HANDLE_AUTO_MIRRORED: {
  1773. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_MIRRORED, true);
  1774. } break;
  1775. }
  1776. }
  1777. void AnimationBezierTrackEdit::duplicate_selected_keys(real_t p_ofs, bool p_ofs_valid) {
  1778. if (selection.is_empty()) {
  1779. return;
  1780. }
  1781. real_t top_time = 1e10;
  1782. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1783. real_t t = animation->track_get_key_time(E->get().first, E->get().second);
  1784. if (t < top_time) {
  1785. top_time = t;
  1786. }
  1787. }
  1788. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1789. undo_redo->create_action(TTR("Animation Duplicate Keys"));
  1790. List<Pair<int, real_t>> new_selection_values;
  1791. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1792. real_t t = animation->track_get_key_time(E->get().first, E->get().second);
  1793. real_t insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
  1794. if (p_ofs_valid) {
  1795. if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
  1796. insert_pos = editor->snap_time(insert_pos);
  1797. }
  1798. }
  1799. real_t dst_time = t + (insert_pos - top_time);
  1800. int existing_idx = animation->track_find_key(E->get().first, dst_time, Animation::FIND_MODE_APPROX);
  1801. 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));
  1802. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", E->get().first, dst_time);
  1803. Pair<int, real_t> p;
  1804. p.first = E->get().first;
  1805. p.second = dst_time;
  1806. new_selection_values.push_back(p);
  1807. if (existing_idx != -1) {
  1808. 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));
  1809. }
  1810. }
  1811. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1812. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1813. // Reselect duplicated.
  1814. int i = 0;
  1815. for (const Pair<int, real_t> &E : new_selection_values) {
  1816. undo_redo->add_do_method(this, "_select_at_anim", animation, E.first, E.second, i == 0);
  1817. i++;
  1818. }
  1819. i = 0;
  1820. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1821. real_t time = animation->track_get_key_time(E->get().first, E->get().second);
  1822. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->get().first, time, i == 0);
  1823. i++;
  1824. }
  1825. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1826. if (ape) {
  1827. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1828. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1829. }
  1830. undo_redo->add_do_method(this, "queue_redraw");
  1831. undo_redo->add_undo_method(this, "queue_redraw");
  1832. undo_redo->commit_action();
  1833. }
  1834. void AnimationBezierTrackEdit::copy_selected_keys(bool p_cut) {
  1835. if (selection.is_empty()) {
  1836. return;
  1837. }
  1838. float top_time = 1e10;
  1839. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1840. float t = animation->track_get_key_time(E->get().first, E->get().second);
  1841. if (t < top_time) {
  1842. top_time = t;
  1843. }
  1844. }
  1845. RBMap<AnimationTrackEditor::SelectedKey, AnimationTrackEditor::KeyInfo> keys;
  1846. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1847. AnimationTrackEditor::SelectedKey sk;
  1848. AnimationTrackEditor::KeyInfo ki;
  1849. sk.track = E->get().first;
  1850. sk.key = E->get().second;
  1851. ki.pos = animation->track_get_key_time(E->get().first, E->get().second);
  1852. keys.insert(sk, ki);
  1853. }
  1854. editor->_set_key_clipboard(selected_track, top_time, keys);
  1855. if (p_cut) {
  1856. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1857. undo_redo->create_action(TTR("Animation Cut Keys"), UndoRedo::MERGE_DISABLE, animation.ptr());
  1858. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1859. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1860. int i = 0;
  1861. for (RBMap<AnimationTrackEditor::SelectedKey, AnimationTrackEditor::KeyInfo>::Element *E = keys.back(); E; E = E->prev()) {
  1862. int track_idx = E->key().track;
  1863. int key_idx = E->key().key;
  1864. float time = E->value().pos;
  1865. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", track_idx, time);
  1866. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track_idx, time, animation->track_get_key_value(track_idx, key_idx), animation->track_get_key_transition(track_idx, key_idx));
  1867. undo_redo->add_undo_method(this, "_select_at_anim", animation, track_idx, time, i == 0);
  1868. i++;
  1869. }
  1870. i = 0;
  1871. for (RBMap<AnimationTrackEditor::SelectedKey, AnimationTrackEditor::KeyInfo>::Element *E = keys.back(); E; E = E->prev()) {
  1872. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, E->value().pos, i == 0);
  1873. i++;
  1874. }
  1875. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1876. if (ape) {
  1877. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1878. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1879. }
  1880. undo_redo->add_do_method(this, "queue_redraw");
  1881. undo_redo->add_undo_method(this, "queue_redraw");
  1882. undo_redo->commit_action();
  1883. }
  1884. }
  1885. void AnimationBezierTrackEdit::paste_keys(real_t p_ofs, bool p_ofs_valid) {
  1886. if (editor->is_key_clipboard_active() && animation.is_valid() && (selected_track >= 0 && selected_track < animation->get_track_count())) {
  1887. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1888. undo_redo->create_action(TTR("Animation Paste Keys"));
  1889. bool same_track = true;
  1890. bool all_compatible = true;
  1891. for (int i = 0; i < editor->key_clipboard.keys.size(); i++) {
  1892. const AnimationTrackEditor::KeyClipboard::Key key = editor->key_clipboard.keys[i];
  1893. if (key.track != 0) {
  1894. same_track = false;
  1895. break;
  1896. }
  1897. if (!editor->_is_track_compatible(selected_track, key.value.get_type(), key.track_type)) {
  1898. all_compatible = false;
  1899. break;
  1900. }
  1901. }
  1902. ERR_FAIL_COND_MSG(!all_compatible, "Paste failed: Not all animation keys were compatible with their target tracks");
  1903. if (!same_track) {
  1904. WARN_PRINT("Pasted animation keys from multiple tracks into single Bezier track");
  1905. }
  1906. List<Pair<int, float>> new_selection_values;
  1907. for (int i = 0; i < editor->key_clipboard.keys.size(); i++) {
  1908. const AnimationTrackEditor::KeyClipboard::Key key = editor->key_clipboard.keys[i];
  1909. float insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
  1910. if (p_ofs_valid) {
  1911. if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
  1912. insert_pos = editor->snap_time(insert_pos);
  1913. }
  1914. }
  1915. float dst_time = key.time + insert_pos;
  1916. int existing_idx = animation->track_find_key(selected_track, dst_time, Animation::FIND_MODE_APPROX);
  1917. Variant value = key.value;
  1918. if (key.track_type != Animation::TYPE_BEZIER) {
  1919. value = animation->make_default_bezier_key(key.value);
  1920. }
  1921. undo_redo->add_do_method(animation.ptr(), "track_insert_key", selected_track, dst_time, value, key.transition);
  1922. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", selected_track, dst_time);
  1923. Pair<int, float> p;
  1924. p.first = selected_track;
  1925. p.second = dst_time;
  1926. new_selection_values.push_back(p);
  1927. if (existing_idx != -1) {
  1928. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", selected_track, dst_time, animation->track_get_key_value(selected_track, existing_idx), animation->track_get_key_transition(selected_track, existing_idx));
  1929. }
  1930. }
  1931. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1932. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1933. // Reselect pasted.
  1934. int i = 0;
  1935. for (const Pair<int, float> &E : new_selection_values) {
  1936. undo_redo->add_do_method(this, "_select_at_anim", animation, E.first, E.second, i == 0);
  1937. i++;
  1938. }
  1939. i = 0;
  1940. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1941. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->get().first, animation->track_get_key_time(E->get().first, E->get().second), i == 0);
  1942. i++;
  1943. }
  1944. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1945. if (ape) {
  1946. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1947. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1948. }
  1949. undo_redo->add_do_method(this, "queue_redraw");
  1950. undo_redo->add_undo_method(this, "queue_redraw");
  1951. undo_redo->commit_action();
  1952. }
  1953. }
  1954. void AnimationBezierTrackEdit::delete_selection() {
  1955. if (selection.size()) {
  1956. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1957. undo_redo->create_action(TTR("Animation Delete Keys"));
  1958. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1959. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->get().first, E->get().second);
  1960. 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);
  1961. }
  1962. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1963. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1964. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1965. if (ape) {
  1966. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1967. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1968. }
  1969. undo_redo->commit_action();
  1970. //selection.clear();
  1971. }
  1972. }
  1973. void AnimationBezierTrackEdit::_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) {
  1974. int idx = p_anim->bezier_track_insert_key(p_track, p_time, p_value, p_in_handle, p_out_handle);
  1975. p_anim->bezier_track_set_key_handle_mode(p_track, idx, p_handle_mode);
  1976. }
  1977. void AnimationBezierTrackEdit::_bind_methods() {
  1978. ClassDB::bind_method(D_METHOD("_clear_selection"), &AnimationBezierTrackEdit::_clear_selection);
  1979. ClassDB::bind_method(D_METHOD("_clear_selection_for_anim"), &AnimationBezierTrackEdit::_clear_selection_for_anim);
  1980. ClassDB::bind_method(D_METHOD("_select_at_anim"), &AnimationBezierTrackEdit::_select_at_anim);
  1981. ClassDB::bind_method(D_METHOD("_update_hidden_tracks_after"), &AnimationBezierTrackEdit::_update_hidden_tracks_after);
  1982. ClassDB::bind_method(D_METHOD("_update_locked_tracks_after"), &AnimationBezierTrackEdit::_update_locked_tracks_after);
  1983. ClassDB::bind_method(D_METHOD("_bezier_track_insert_key_at_anim"), &AnimationBezierTrackEdit::_bezier_track_insert_key_at_anim);
  1984. ADD_SIGNAL(MethodInfo("select_key", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "single"), PropertyInfo(Variant::INT, "track")));
  1985. ADD_SIGNAL(MethodInfo("deselect_key", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::INT, "track")));
  1986. ADD_SIGNAL(MethodInfo("clear_selection"));
  1987. ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::FLOAT, "position"), PropertyInfo(Variant::BOOL, "timeline_only")));
  1988. }
  1989. AnimationBezierTrackEdit::AnimationBezierTrackEdit() {
  1990. panner.instantiate();
  1991. panner->set_callbacks(callable_mp(this, &AnimationBezierTrackEdit::_pan_callback), callable_mp(this, &AnimationBezierTrackEdit::_zoom_callback));
  1992. play_position = memnew(Control);
  1993. play_position->set_mouse_filter(MOUSE_FILTER_PASS);
  1994. add_child(play_position);
  1995. play_position->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  1996. play_position->connect(SceneStringName(draw), callable_mp(this, &AnimationBezierTrackEdit::_play_position_draw));
  1997. set_focus_mode(FOCUS_CLICK);
  1998. set_clip_contents(true);
  1999. ED_SHORTCUT("animation_bezier_editor/focus", TTRC("Focus"), Key::F);
  2000. ED_SHORTCUT("animation_bezier_editor/select_all_keys", TTRC("Select All Keys"), KeyModifierMask::CMD_OR_CTRL | Key::A);
  2001. ED_SHORTCUT("animation_bezier_editor/deselect_all_keys", TTRC("Deselect All Keys"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::A);
  2002. menu = memnew(PopupMenu);
  2003. add_child(menu);
  2004. menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationBezierTrackEdit::_menu_selected));
  2005. }