animation_bezier_editor.cpp 74 KB

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