animation_bezier_editor.cpp 61 KB

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