animation_bezier_editor.cpp 94 KB

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