animation_bezier_editor.cpp 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  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. }
  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. const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  191. const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  192. const Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  193. const Color h_line_color = get_theme_color(SNAME("h_line_color"), SNAME("AnimationBezierTrackEdit"));
  194. const Color v_line_color = get_theme_color(SNAME("v_line_color"), SNAME("AnimationBezierTrackEdit"));
  195. const Color focus_color = get_theme_color(SNAME("focus_color"), SNAME("AnimationBezierTrackEdit"));
  196. const Color track_focus_color = get_theme_color(SNAME("track_focus_color"), SNAME("AnimationBezierTrackEdit"));
  197. const int h_separation = get_theme_constant(SNAME("h_separation"), SNAME("AnimationBezierTrackEdit"));
  198. const int v_separation = get_theme_constant(SNAME("h_separation"), SNAME("AnimationBezierTrackEdit"));
  199. if (has_focus()) {
  200. draw_rect(Rect2(Point2(), get_size()), focus_color, false, Math::round(EDSCALE));
  201. }
  202. draw_line(Point2(limit, 0), Point2(limit, get_size().height), v_line_color, Math::round(EDSCALE));
  203. int right_limit = get_size().width;
  204. track_v_scroll_max = v_separation;
  205. int vofs = v_separation + 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_char(':');
  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 += h_separation;
  242. TextLine text_buf = TextLine(text, font, font_size);
  243. text_buf.set_width(limit - ofs - icon->get_width() - h_separation);
  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() + h_separation;
  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 + v_separation;
  252. track_v_scroll_max += h + v_separation;
  253. }
  254. }
  255. const 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 - h_separation - 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 - h_separation - 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 - h_separation - visibility_visible->get_width();
  264. Ref<Texture2D> solo = get_editor_theme_icon(SNAME("AudioBusSolo"));
  265. float solo_hpos = visibility_hpos - h_separation - solo->get_width();
  266. float buttons_width = remove->get_width() + lock->get_width() + visibility_visible->get_width() + solo->get_width() + h_separation * 3;
  267. for (int i = 0; i < tracks.size(); ++i) {
  268. // Related track 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 - h_separation * 2);
  275. Rect2 rect = Rect2(margin, vofs, solo_hpos - h_separation - solo->get_width(), text_buf.get_size().y + v_separation);
  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 - h_separation, text_buf.get_size().y * 0.8), track_color);
  300. subtrack_colors[current_track] = track_color;
  301. subtracks[current_track] = rect;
  302. } else {
  303. draw_rect(rect, track_focus_color);
  304. if (locked_tracks.has(selected_track)) {
  305. selected_track_color.set_hsv(h, 0.0, 0.4);
  306. } else {
  307. selected_track_color.set_hsv(h, 0.8, 0.8);
  308. }
  309. }
  310. Vector2 string_pos = Point2(margin + h_separation, vofs);
  311. text_buf.draw(get_canvas_item(), string_pos, cc);
  312. float icon_start_height = vofs + rect.size.y / 2.0;
  313. Rect2 remove_rect = Rect2(remove_hpos, icon_start_height - remove->get_height() / 2.0, remove->get_width(), remove->get_height());
  314. if (read_only) {
  315. draw_texture(remove, remove_rect.position, dc);
  316. } else {
  317. draw_texture(remove, remove_rect.position);
  318. }
  319. Rect2 lock_rect = Rect2(lock_hpos, icon_start_height - lock->get_height() / 2.0, lock->get_width(), lock->get_height());
  320. if (locked_tracks.has(current_track)) {
  321. draw_texture(lock, lock_rect.position);
  322. } else {
  323. draw_texture(unlock, lock_rect.position);
  324. }
  325. Rect2 visible_rect = Rect2(visibility_hpos, icon_start_height - visibility_visible->get_height() / 2.0, visibility_visible->get_width(), visibility_visible->get_height());
  326. if (hidden_tracks.has(current_track)) {
  327. draw_texture(visibility_hidden, visible_rect.position);
  328. } else {
  329. draw_texture(visibility_visible, visible_rect.position);
  330. }
  331. Rect2 solo_rect = Rect2(solo_hpos, icon_start_height - solo->get_height() / 2.0, solo->get_width(), solo->get_height());
  332. draw_texture(solo, solo_rect.position);
  333. RBMap<int, Rect2> track_icons;
  334. track_icons[REMOVE_ICON] = remove_rect;
  335. track_icons[LOCK_ICON] = lock_rect;
  336. track_icons[VISIBILITY_ICON] = visible_rect;
  337. track_icons[SOLO_ICON] = solo_rect;
  338. subtrack_icons[current_track] = track_icons;
  339. vofs += text_buf.get_size().y + v_separation;
  340. track_v_scroll_max += text_buf.get_size().y + v_separation;
  341. }
  342. }
  343. const Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  344. // Guides.
  345. {
  346. float min_left_scale = font->get_height(font_size) + v_separation;
  347. float scale = (min_left_scale * 2) * timeline_v_zoom;
  348. float step = Math::pow(10.0, Math::round(Math::log(scale / 5.0) / Math::log(10.0))) * 5.0;
  349. scale = Math::snapped(scale, step);
  350. while (scale / timeline_v_zoom < min_left_scale * 2) {
  351. scale += step;
  352. }
  353. bool first = true;
  354. int prev_iv = 0;
  355. for (int i = font->get_height(font_size); i < get_size().height; i++) {
  356. float ofs = get_size().height / 2.0 - i;
  357. ofs *= timeline_v_zoom;
  358. ofs += timeline_v_scroll;
  359. int iv = int(ofs / scale);
  360. if (ofs < 0) {
  361. iv -= 1;
  362. }
  363. if (!first && iv != prev_iv) {
  364. Color lc = h_line_color;
  365. lc.a *= 0.5;
  366. draw_line(Point2(limit, i), Point2(right_limit, i), lc, Math::round(EDSCALE));
  367. Color c = color;
  368. c.a *= 0.5;
  369. 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);
  370. }
  371. first = false;
  372. prev_iv = iv;
  373. }
  374. }
  375. // Draw other curves.
  376. {
  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. const 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.is_null()) {
  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. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1204. if (ape) {
  1205. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1206. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1207. }
  1208. undo_redo->commit_action();
  1209. } else if (select_single_attempt != IntPair(-1, -1)) {
  1210. selection.clear();
  1211. set_animation_and_track(animation, select_single_attempt.first, read_only);
  1212. _select_at_anim(animation, select_single_attempt.first, animation->track_get_key_time(select_single_attempt.first, select_single_attempt.second), true);
  1213. }
  1214. moving_selection = false;
  1215. moving_selection_attempt = false;
  1216. moving_selection_mouse_begin_x = 0.0;
  1217. queue_redraw();
  1218. }
  1219. }
  1220. Ref<InputEventMouseMotion> mm = p_event;
  1221. if (moving_selection_attempt && mm.is_valid()) {
  1222. if (!moving_selection) {
  1223. moving_selection = true;
  1224. select_single_attempt = IntPair(-1, -1);
  1225. }
  1226. if (!read_only) {
  1227. float y = (get_size().height / 2.0 - mm->get_position().y) * timeline_v_zoom + timeline_v_scroll;
  1228. float moving_selection_begin_time = ((moving_selection_mouse_begin_x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1229. float new_time = ((mm->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1230. float moving_selection_pivot = animation->track_get_key_time(moving_selection_from_track, moving_selection_from_key);
  1231. float time_delta = new_time - moving_selection_begin_time;
  1232. float snapped_time = editor->snap_time(moving_selection_pivot + time_delta);
  1233. float time_offset = 0.0;
  1234. 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)) {
  1235. time_offset = snapped_time - moving_selection_pivot;
  1236. }
  1237. float moving_selection_begin_value = animation->bezier_track_get_key_value(moving_selection_from_track, moving_selection_from_key);
  1238. float y_offset = y - moving_selection_begin_value;
  1239. moving_selection_offset = Vector2(time_offset, y_offset);
  1240. }
  1241. additional_moving_handle_lefts.clear();
  1242. additional_moving_handle_rights.clear();
  1243. queue_redraw();
  1244. }
  1245. if (box_selecting_attempt && mm.is_valid()) {
  1246. if (!box_selecting) {
  1247. box_selecting = true;
  1248. box_selecting_add = mm->is_shift_pressed();
  1249. }
  1250. box_selection_to = mm->get_position();
  1251. queue_redraw();
  1252. }
  1253. if ((moving_handle == 1 || moving_handle == -1) && mm.is_valid()) {
  1254. float y = (get_size().height / 2.0 - mm->get_position().y) * timeline_v_zoom + timeline_v_scroll;
  1255. float x = editor->snap_time((mm->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value();
  1256. 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));
  1257. Vector2 moving_handle_value = Vector2(x, y) - key_pos;
  1258. moving_handle_left = animation->bezier_track_get_key_in_handle(moving_handle_track, moving_handle_key);
  1259. moving_handle_right = animation->bezier_track_get_key_out_handle(moving_handle_track, moving_handle_key);
  1260. if (moving_handle == -1) {
  1261. moving_handle_left = moving_handle_value;
  1262. Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key);
  1263. if (handle_mode == Animation::HANDLE_MODE_BALANCED) {
  1264. real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
  1265. Transform2D xform;
  1266. xform.set_scale(Vector2(1.0, 1.0 / ratio));
  1267. Vector2 vec_out = xform.xform(moving_handle_right);
  1268. Vector2 vec_in = xform.xform(moving_handle_left);
  1269. moving_handle_right = xform.affine_inverse().xform(-vec_in.normalized() * vec_out.length());
  1270. } else if (handle_mode == Animation::HANDLE_MODE_MIRRORED) {
  1271. moving_handle_right = -moving_handle_left;
  1272. }
  1273. } else if (moving_handle == 1) {
  1274. moving_handle_right = moving_handle_value;
  1275. Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key);
  1276. if (handle_mode == Animation::HANDLE_MODE_BALANCED) {
  1277. real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
  1278. Transform2D xform;
  1279. xform.set_scale(Vector2(1.0, 1.0 / ratio));
  1280. Vector2 vec_in = xform.xform(moving_handle_left);
  1281. Vector2 vec_out = xform.xform(moving_handle_right);
  1282. moving_handle_left = xform.affine_inverse().xform(-vec_out.normalized() * vec_in.length());
  1283. } else if (handle_mode == Animation::HANDLE_MODE_MIRRORED) {
  1284. moving_handle_left = -moving_handle_right;
  1285. }
  1286. }
  1287. queue_redraw();
  1288. }
  1289. if ((moving_handle == -1 || moving_handle == 1) && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  1290. if (!read_only) {
  1291. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1292. undo_redo->create_action(TTR("Move Bezier Points"));
  1293. if (moving_handle == -1) {
  1294. real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
  1295. undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_in_handle", moving_handle_track, moving_handle_key, moving_handle_left, ratio);
  1296. 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);
  1297. } else if (moving_handle == 1) {
  1298. real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
  1299. undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_out_handle", moving_handle_track, moving_handle_key, moving_handle_right, ratio);
  1300. 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);
  1301. }
  1302. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1303. if (ape) {
  1304. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1305. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1306. }
  1307. undo_redo->commit_action();
  1308. moving_handle = 0;
  1309. queue_redraw();
  1310. }
  1311. }
  1312. }
  1313. bool AnimationBezierTrackEdit::_try_select_at_ui_pos(const Point2 &p_pos, bool p_aggregate, bool p_deselectable) {
  1314. for (int i = 0; i < edit_points.size(); i++) {
  1315. // Path 2D editing in the 3D and 2D editors works the same way. (?)
  1316. if (edit_points[i].point_rect.has_point(p_pos)) {
  1317. IntPair pair = IntPair(edit_points[i].track, edit_points[i].key);
  1318. if (p_aggregate) {
  1319. // Add to selection.
  1320. if (selection.has(pair)) {
  1321. if (p_deselectable) {
  1322. selection.erase(pair);
  1323. emit_signal(SNAME("deselect_key"), edit_points[i].key, edit_points[i].track);
  1324. }
  1325. } else {
  1326. _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), false);
  1327. }
  1328. queue_redraw();
  1329. select_single_attempt = IntPair(-1, -1);
  1330. } else {
  1331. if (p_deselectable) {
  1332. moving_selection_attempt = true;
  1333. moving_selection_from_key = pair.second;
  1334. moving_selection_from_track = pair.first;
  1335. moving_selection_mouse_begin_x = p_pos.x;
  1336. moving_selection_offset = Vector2();
  1337. moving_handle_track = pair.first;
  1338. moving_handle_left = animation->bezier_track_get_key_in_handle(pair.first, pair.second);
  1339. moving_handle_right = animation->bezier_track_get_key_out_handle(pair.first, pair.second);
  1340. if (selection.has(pair)) {
  1341. moving_selection = false;
  1342. } else {
  1343. moving_selection = true;
  1344. }
  1345. select_single_attempt = pair;
  1346. }
  1347. set_animation_and_track(animation, pair.first, read_only);
  1348. if (!selection.has(pair)) {
  1349. selection.clear();
  1350. _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), true);
  1351. }
  1352. }
  1353. return true;
  1354. }
  1355. }
  1356. return false;
  1357. }
  1358. void AnimationBezierTrackEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
  1359. Ref<InputEventMouseMotion> mm = p_event;
  1360. if (mm.is_valid()) {
  1361. if (mm->get_position().x > timeline->get_name_limit()) {
  1362. timeline_v_scroll += p_scroll_vec.y * timeline_v_zoom;
  1363. timeline_v_scroll = CLAMP(timeline_v_scroll, -100000, 100000);
  1364. timeline->set_value(timeline->get_value() - p_scroll_vec.x / timeline->get_zoom_scale());
  1365. } else {
  1366. track_v_scroll += p_scroll_vec.y;
  1367. if (track_v_scroll < -track_v_scroll_max) {
  1368. track_v_scroll = -track_v_scroll_max;
  1369. } else if (track_v_scroll > 0) {
  1370. track_v_scroll = 0;
  1371. }
  1372. }
  1373. queue_redraw();
  1374. }
  1375. }
  1376. void AnimationBezierTrackEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
  1377. const float v_zoom_orig = timeline_v_zoom;
  1378. Ref<InputEventWithModifiers> iewm = p_event;
  1379. if (iewm.is_valid() && iewm->is_alt_pressed()) {
  1380. // Alternate zoom (doesn't affect timeline).
  1381. timeline_v_zoom = CLAMP(timeline_v_zoom / p_zoom_factor, 0.000001, 100000);
  1382. } else {
  1383. float zoom_factor = p_zoom_factor > 1.0 ? AnimationTimelineEdit::SCROLL_ZOOM_FACTOR_IN : AnimationTimelineEdit::SCROLL_ZOOM_FACTOR_OUT;
  1384. timeline->_zoom_callback(zoom_factor, p_origin, p_event);
  1385. }
  1386. timeline_v_scroll = timeline_v_scroll + (p_origin.y - get_size().y / 2.0) * (timeline_v_zoom - v_zoom_orig);
  1387. queue_redraw();
  1388. }
  1389. float AnimationBezierTrackEdit::get_bezier_key_value(Array p_bezier_key_array) {
  1390. return p_bezier_key_array[0];
  1391. }
  1392. void AnimationBezierTrackEdit::_menu_selected(int p_index) {
  1393. int limit = timeline->get_name_limit();
  1394. real_t time = ((menu_insert_key.x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
  1395. switch (p_index) {
  1396. case MENU_KEY_INSERT: {
  1397. if (animation->get_track_count() > 0) {
  1398. if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
  1399. time = editor->snap_time(time);
  1400. }
  1401. while (animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX) != -1) {
  1402. time += 0.001;
  1403. }
  1404. float h = (get_size().height / 2.0 - menu_insert_key.y) * timeline_v_zoom + timeline_v_scroll;
  1405. Array new_point = animation->make_default_bezier_key(h);
  1406. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1407. undo_redo->create_action(TTR("Add Bezier Point"));
  1408. undo_redo->add_do_method(animation.ptr(), "track_insert_key", selected_track, time, new_point);
  1409. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1410. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", selected_track, time);
  1411. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1412. if (ape) {
  1413. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1414. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1415. }
  1416. undo_redo->commit_action();
  1417. queue_redraw();
  1418. }
  1419. } break;
  1420. case MENU_KEY_DUPLICATE: {
  1421. duplicate_selected_keys(time, true);
  1422. } break;
  1423. case MENU_KEY_DELETE: {
  1424. delete_selection();
  1425. } break;
  1426. case MENU_KEY_CUT: {
  1427. copy_selected_keys(true);
  1428. } break;
  1429. case MENU_KEY_COPY: {
  1430. copy_selected_keys(false);
  1431. } break;
  1432. case MENU_KEY_PASTE: {
  1433. paste_keys(time, true);
  1434. } break;
  1435. case MENU_KEY_SET_HANDLE_FREE: {
  1436. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_FREE);
  1437. } break;
  1438. case MENU_KEY_SET_HANDLE_LINEAR: {
  1439. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_LINEAR);
  1440. } break;
  1441. case MENU_KEY_SET_HANDLE_BALANCED: {
  1442. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_BALANCED);
  1443. } break;
  1444. case MENU_KEY_SET_HANDLE_MIRRORED: {
  1445. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_MIRRORED);
  1446. } break;
  1447. case MENU_KEY_SET_HANDLE_AUTO_BALANCED: {
  1448. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_BALANCED, true);
  1449. } break;
  1450. case MENU_KEY_SET_HANDLE_AUTO_MIRRORED: {
  1451. _change_selected_keys_handle_mode(Animation::HANDLE_MODE_MIRRORED, true);
  1452. } break;
  1453. }
  1454. }
  1455. void AnimationBezierTrackEdit::duplicate_selected_keys(real_t p_ofs, bool p_ofs_valid) {
  1456. if (selection.size() == 0) {
  1457. return;
  1458. }
  1459. real_t top_time = 1e10;
  1460. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1461. real_t t = animation->track_get_key_time(E->get().first, E->get().second);
  1462. if (t < top_time) {
  1463. top_time = t;
  1464. }
  1465. }
  1466. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1467. undo_redo->create_action(TTR("Animation Duplicate Keys"));
  1468. List<Pair<int, real_t>> new_selection_values;
  1469. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1470. real_t t = animation->track_get_key_time(E->get().first, E->get().second);
  1471. real_t insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
  1472. if (p_ofs_valid) {
  1473. if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
  1474. insert_pos = editor->snap_time(insert_pos);
  1475. }
  1476. }
  1477. real_t dst_time = t + (insert_pos - top_time);
  1478. int existing_idx = animation->track_find_key(E->get().first, dst_time, Animation::FIND_MODE_APPROX);
  1479. 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));
  1480. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", E->get().first, dst_time);
  1481. Pair<int, real_t> p;
  1482. p.first = E->get().first;
  1483. p.second = dst_time;
  1484. new_selection_values.push_back(p);
  1485. if (existing_idx != -1) {
  1486. 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));
  1487. }
  1488. }
  1489. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1490. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1491. // Reselect duplicated.
  1492. int i = 0;
  1493. for (const Pair<int, real_t> &E : new_selection_values) {
  1494. undo_redo->add_do_method(this, "_select_at_anim", animation, E.first, E.second, i == 0);
  1495. i++;
  1496. }
  1497. i = 0;
  1498. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1499. real_t time = animation->track_get_key_time(E->get().first, E->get().second);
  1500. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->get().first, time, i == 0);
  1501. i++;
  1502. }
  1503. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1504. if (ape) {
  1505. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1506. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1507. }
  1508. undo_redo->add_do_method(this, "queue_redraw");
  1509. undo_redo->add_undo_method(this, "queue_redraw");
  1510. undo_redo->commit_action();
  1511. }
  1512. void AnimationBezierTrackEdit::copy_selected_keys(bool p_cut) {
  1513. if (selection.is_empty()) {
  1514. return;
  1515. }
  1516. float top_time = 1e10;
  1517. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1518. float t = animation->track_get_key_time(E->get().first, E->get().second);
  1519. if (t < top_time) {
  1520. top_time = t;
  1521. }
  1522. }
  1523. RBMap<AnimationTrackEditor::SelectedKey, AnimationTrackEditor::KeyInfo> keys;
  1524. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1525. AnimationTrackEditor::SelectedKey sk;
  1526. AnimationTrackEditor::KeyInfo ki;
  1527. sk.track = E->get().first;
  1528. sk.key = E->get().second;
  1529. ki.pos = animation->track_get_key_time(E->get().first, E->get().second);
  1530. keys.insert(sk, ki);
  1531. }
  1532. editor->_set_key_clipboard(selected_track, top_time, keys);
  1533. if (p_cut) {
  1534. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1535. undo_redo->create_action(TTR("Animation Cut Keys"), UndoRedo::MERGE_DISABLE, animation.ptr());
  1536. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1537. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1538. int i = 0;
  1539. for (RBMap<AnimationTrackEditor::SelectedKey, AnimationTrackEditor::KeyInfo>::Element *E = keys.back(); E; E = E->prev()) {
  1540. int track_idx = E->key().track;
  1541. int key_idx = E->key().key;
  1542. float time = E->value().pos;
  1543. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", track_idx, time);
  1544. 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));
  1545. undo_redo->add_undo_method(this, "_select_at_anim", animation, track_idx, time, i == 0);
  1546. i++;
  1547. }
  1548. i = 0;
  1549. for (RBMap<AnimationTrackEditor::SelectedKey, AnimationTrackEditor::KeyInfo>::Element *E = keys.back(); E; E = E->prev()) {
  1550. undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, E->value().pos, i == 0);
  1551. i++;
  1552. }
  1553. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1554. if (ape) {
  1555. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1556. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1557. }
  1558. undo_redo->add_do_method(this, "queue_redraw");
  1559. undo_redo->add_undo_method(this, "queue_redraw");
  1560. undo_redo->commit_action();
  1561. }
  1562. }
  1563. void AnimationBezierTrackEdit::paste_keys(real_t p_ofs, bool p_ofs_valid) {
  1564. if (editor->is_key_clipboard_active() && animation.is_valid() && (selected_track >= 0 && selected_track < animation->get_track_count())) {
  1565. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1566. undo_redo->create_action(TTR("Animation Paste Keys"));
  1567. bool same_track = true;
  1568. bool all_compatible = true;
  1569. for (int i = 0; i < editor->key_clipboard.keys.size(); i++) {
  1570. const AnimationTrackEditor::KeyClipboard::Key key = editor->key_clipboard.keys[i];
  1571. if (key.track != 0) {
  1572. same_track = false;
  1573. break;
  1574. }
  1575. if (!editor->_is_track_compatible(selected_track, key.value.get_type(), key.track_type)) {
  1576. all_compatible = false;
  1577. break;
  1578. }
  1579. }
  1580. ERR_FAIL_COND_MSG(!all_compatible, "Paste failed: Not all animation keys were compatible with their target tracks");
  1581. if (!same_track) {
  1582. WARN_PRINT("Pasted animation keys from multiple tracks into single Bezier track");
  1583. }
  1584. List<Pair<int, float>> new_selection_values;
  1585. for (int i = 0; i < editor->key_clipboard.keys.size(); i++) {
  1586. const AnimationTrackEditor::KeyClipboard::Key key = editor->key_clipboard.keys[i];
  1587. float insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
  1588. if (p_ofs_valid) {
  1589. if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
  1590. insert_pos = editor->snap_time(insert_pos);
  1591. }
  1592. }
  1593. float dst_time = key.time + insert_pos;
  1594. int existing_idx = animation->track_find_key(selected_track, dst_time, Animation::FIND_MODE_APPROX);
  1595. Variant value = key.value;
  1596. if (key.track_type != Animation::TYPE_BEZIER) {
  1597. value = animation->make_default_bezier_key(key.value);
  1598. }
  1599. undo_redo->add_do_method(animation.ptr(), "track_insert_key", selected_track, dst_time, value, key.transition);
  1600. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", selected_track, dst_time);
  1601. Pair<int, float> p;
  1602. p.first = selected_track;
  1603. p.second = dst_time;
  1604. new_selection_values.push_back(p);
  1605. if (existing_idx != -1) {
  1606. 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));
  1607. }
  1608. }
  1609. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1610. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1611. // Reselect pasted.
  1612. int i = 0;
  1613. for (const Pair<int, float> &E : new_selection_values) {
  1614. undo_redo->add_do_method(this, "_select_at_anim", animation, E.first, E.second, i == 0);
  1615. i++;
  1616. }
  1617. i = 0;
  1618. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1619. 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);
  1620. i++;
  1621. }
  1622. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1623. if (ape) {
  1624. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1625. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1626. }
  1627. undo_redo->add_do_method(this, "queue_redraw");
  1628. undo_redo->add_undo_method(this, "queue_redraw");
  1629. undo_redo->commit_action();
  1630. }
  1631. }
  1632. void AnimationBezierTrackEdit::delete_selection() {
  1633. if (selection.size()) {
  1634. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  1635. undo_redo->create_action(TTR("Animation Delete Keys"));
  1636. for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
  1637. undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->get().first, E->get().second);
  1638. 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);
  1639. }
  1640. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  1641. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  1642. AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
  1643. if (ape) {
  1644. undo_redo->add_do_method(ape, "_animation_update_key_frame");
  1645. undo_redo->add_undo_method(ape, "_animation_update_key_frame");
  1646. }
  1647. undo_redo->commit_action();
  1648. //selection.clear();
  1649. }
  1650. }
  1651. 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) {
  1652. int idx = p_anim->bezier_track_insert_key(p_track, p_time, p_value, p_in_handle, p_out_handle);
  1653. p_anim->bezier_track_set_key_handle_mode(p_track, idx, p_handle_mode);
  1654. }
  1655. void AnimationBezierTrackEdit::_bind_methods() {
  1656. ClassDB::bind_method(D_METHOD("_clear_selection"), &AnimationBezierTrackEdit::_clear_selection);
  1657. ClassDB::bind_method(D_METHOD("_clear_selection_for_anim"), &AnimationBezierTrackEdit::_clear_selection_for_anim);
  1658. ClassDB::bind_method(D_METHOD("_select_at_anim"), &AnimationBezierTrackEdit::_select_at_anim);
  1659. ClassDB::bind_method(D_METHOD("_update_hidden_tracks_after"), &AnimationBezierTrackEdit::_update_hidden_tracks_after);
  1660. ClassDB::bind_method(D_METHOD("_update_locked_tracks_after"), &AnimationBezierTrackEdit::_update_locked_tracks_after);
  1661. ClassDB::bind_method(D_METHOD("_bezier_track_insert_key_at_anim"), &AnimationBezierTrackEdit::_bezier_track_insert_key_at_anim);
  1662. ADD_SIGNAL(MethodInfo("select_key", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "single"), PropertyInfo(Variant::INT, "track")));
  1663. ADD_SIGNAL(MethodInfo("deselect_key", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::INT, "track")));
  1664. ADD_SIGNAL(MethodInfo("clear_selection"));
  1665. }
  1666. AnimationBezierTrackEdit::AnimationBezierTrackEdit() {
  1667. panner.instantiate();
  1668. panner->set_callbacks(callable_mp(this, &AnimationBezierTrackEdit::_pan_callback), callable_mp(this, &AnimationBezierTrackEdit::_zoom_callback));
  1669. play_position = memnew(Control);
  1670. play_position->set_mouse_filter(MOUSE_FILTER_PASS);
  1671. add_child(play_position);
  1672. play_position->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  1673. play_position->connect(SceneStringName(draw), callable_mp(this, &AnimationBezierTrackEdit::_play_position_draw));
  1674. set_focus_mode(FOCUS_CLICK);
  1675. set_clip_contents(true);
  1676. ED_SHORTCUT("animation_bezier_editor/focus", TTR("Focus"), Key::F);
  1677. ED_SHORTCUT("animation_bezier_editor/select_all_keys", TTR("Select All Keys"), KeyModifierMask::CMD_OR_CTRL | Key::A);
  1678. ED_SHORTCUT("animation_bezier_editor/deselect_all_keys", TTR("Deselect All Keys"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::A);
  1679. menu = memnew(PopupMenu);
  1680. add_child(menu);
  1681. menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationBezierTrackEdit::_menu_selected));
  1682. }