animation_bezier_editor.cpp 70 KB

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