animation_bezier_editor.cpp 73 KB

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