animation_bezier_editor.cpp 60 KB

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