animation_bezier_editor.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*************************************************************************/
  2. /* animation_bezier_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_scale.h"
  33. #include "scene/resources/text_line.h"
  34. float AnimationBezierTrackEdit::_bezier_h_to_pixel(float p_h) {
  35. float h = p_h;
  36. h = (h - v_scroll) / v_zoom;
  37. h = (get_size().height / 2) - h;
  38. return h;
  39. }
  40. static _FORCE_INLINE_ Vector2 _bezier_interp(real_t t, const Vector2 &start, const Vector2 &control_1, const Vector2 &control_2, const Vector2 &end) {
  41. /* Formula from Wikipedia article on Bezier curves. */
  42. real_t omt = (1.0 - t);
  43. real_t omt2 = omt * omt;
  44. real_t omt3 = omt2 * omt;
  45. real_t t2 = t * t;
  46. real_t t3 = t2 * t;
  47. return start * omt3 + control_1 * omt2 * t * 3.0 + control_2 * omt * t2 * 3.0 + end * t3;
  48. }
  49. void AnimationBezierTrackEdit::_draw_track(int p_track, const Color &p_color) {
  50. float scale = timeline->get_zoom_scale();
  51. int limit = timeline->get_name_limit();
  52. int right_limit = get_size().width - timeline->get_buttons_width();
  53. //selection may have altered the order of keys
  54. Map<float, int> key_order;
  55. for (int i = 0; i < animation->track_get_key_count(p_track); i++) {
  56. float ofs = animation->track_get_key_time(p_track, i);
  57. if (moving_selection && track == p_track && selection.has(i)) {
  58. ofs += moving_selection_offset.x;
  59. }
  60. key_order[ofs] = i;
  61. }
  62. for (Map<float, int>::Element *E = key_order.front(); E; E = E->next()) {
  63. int i = E->get();
  64. if (!E->next()) {
  65. break;
  66. }
  67. int i_n = E->next()->get();
  68. float offset = animation->track_get_key_time(p_track, i);
  69. float height = animation->bezier_track_get_key_value(p_track, i);
  70. Vector2 out_handle = animation->bezier_track_get_key_out_handle(p_track, i);
  71. if (track == p_track && moving_handle != 0 && moving_handle_key == i) {
  72. out_handle = moving_handle_right;
  73. }
  74. if (moving_selection && track == p_track && selection.has(i)) {
  75. offset += moving_selection_offset.x;
  76. height += moving_selection_offset.y;
  77. }
  78. out_handle += Vector2(offset, height);
  79. float offset_n = animation->track_get_key_time(p_track, i_n);
  80. float height_n = animation->bezier_track_get_key_value(p_track, i_n);
  81. Vector2 in_handle = animation->bezier_track_get_key_in_handle(p_track, i_n);
  82. if (track == p_track && moving_handle != 0 && moving_handle_key == i_n) {
  83. in_handle = moving_handle_left;
  84. }
  85. if (moving_selection && track == p_track && selection.has(i_n)) {
  86. offset_n += moving_selection_offset.x;
  87. height_n += moving_selection_offset.y;
  88. }
  89. in_handle += Vector2(offset_n, height_n);
  90. Vector2 start(offset, height);
  91. Vector2 end(offset_n, height_n);
  92. int from_x = (offset - timeline->get_value()) * scale + limit;
  93. int point_start = from_x;
  94. int to_x = (offset_n - timeline->get_value()) * scale + limit;
  95. int point_end = to_x;
  96. if (from_x > right_limit) { //not visible
  97. continue;
  98. }
  99. if (to_x < limit) { //not visible
  100. continue;
  101. }
  102. from_x = MAX(from_x, limit);
  103. to_x = MIN(to_x, right_limit);
  104. Vector<Vector2> lines;
  105. Vector2 prev_pos;
  106. for (int j = from_x; j <= to_x; j++) {
  107. float t = (j - limit) / scale + timeline->get_value();
  108. float h;
  109. if (j == point_end) {
  110. h = end.y; //make sure it always connects
  111. } else if (j == point_start) {
  112. h = start.y; //make sure it always connects
  113. } else { //custom interpolation, used because it needs to show paths affected by moving the selection or handles
  114. int iterations = 10;
  115. float low = 0;
  116. float high = 1;
  117. float middle;
  118. //narrow high and low as much as possible
  119. for (int k = 0; k < iterations; k++) {
  120. middle = (low + high) / 2;
  121. Vector2 interp = _bezier_interp(middle, start, out_handle, in_handle, end);
  122. if (interp.x < t) {
  123. low = middle;
  124. } else {
  125. high = middle;
  126. }
  127. }
  128. //interpolate the result:
  129. Vector2 low_pos = _bezier_interp(low, start, out_handle, in_handle, end);
  130. Vector2 high_pos = _bezier_interp(high, start, out_handle, in_handle, end);
  131. float c = (t - low_pos.x) / (high_pos.x - low_pos.x);
  132. h = low_pos.lerp(high_pos, c).y;
  133. }
  134. h = _bezier_h_to_pixel(h);
  135. Vector2 pos(j, h);
  136. if (j > from_x) {
  137. lines.push_back(prev_pos);
  138. lines.push_back(pos);
  139. }
  140. prev_pos = pos;
  141. }
  142. if (lines.size() >= 2) {
  143. draw_multiline(lines, p_color);
  144. }
  145. }
  146. }
  147. 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) {
  148. Vector2 from = p_from;
  149. Vector2 to = p_to;
  150. if (from.x == to.x) {
  151. return;
  152. }
  153. if (to.x < from.x) {
  154. SWAP(to, from);
  155. }
  156. if (to.x < p_clip_left) {
  157. return;
  158. }
  159. if (from.x > p_clip_right) {
  160. return;
  161. }
  162. if (to.x > p_clip_right) {
  163. float c = (p_clip_right - from.x) / (to.x - from.x);
  164. to = from.lerp(to, c);
  165. }
  166. if (from.x < p_clip_left) {
  167. float c = (p_clip_left - from.x) / (to.x - from.x);
  168. from = from.lerp(to, c);
  169. }
  170. draw_line(from, to, p_color);
  171. }
  172. void AnimationBezierTrackEdit::_notification(int p_what) {
  173. if (p_what == NOTIFICATION_THEME_CHANGED || p_what == NOTIFICATION_ENTER_TREE) {
  174. bezier_icon = get_theme_icon("KeyBezierPoint", "EditorIcons");
  175. bezier_handle_icon = get_theme_icon("KeyBezierHandle", "EditorIcons");
  176. selected_icon = get_theme_icon("KeyBezierSelected", "EditorIcons");
  177. if (handle_mode_option->get_item_count() == 0) {
  178. handle_mode_option->add_icon_item(get_theme_icon("BezierHandlesFree", "EditorIcons"), TTR("Free"), HANDLE_MODE_FREE);
  179. handle_mode_option->add_icon_item(get_theme_icon("BezierHandlesBalanced", "EditorIcons"), TTR("Balanced"), HANDLE_MODE_BALANCED);
  180. handle_mode_option->add_icon_item(get_theme_icon("BezierHandlesMirror", "EditorIcons"), TTR("Mirror"), HANDLE_MODE_MIRROR);
  181. }
  182. }
  183. if (p_what == NOTIFICATION_RESIZED) {
  184. int right_limit = get_size().width - timeline->get_buttons_width();
  185. int hsep = get_theme_constant("hseparation", "ItemList");
  186. int vsep = get_theme_constant("vseparation", "ItemList");
  187. handle_mode_option->set_position(Vector2(right_limit + hsep, get_size().height - handle_mode_option->get_combined_minimum_size().height - vsep));
  188. handle_mode_option->set_size(Vector2(timeline->get_buttons_width() - hsep * 2, handle_mode_option->get_combined_minimum_size().height));
  189. }
  190. if (p_what == NOTIFICATION_DRAW) {
  191. if (animation.is_null()) {
  192. return;
  193. }
  194. int limit = timeline->get_name_limit();
  195. if (has_focus()) {
  196. Color accent = get_theme_color("accent_color", "Editor");
  197. accent.a *= 0.7;
  198. draw_rect(Rect2(Point2(), get_size()), accent, false);
  199. }
  200. Ref<Font> font = get_theme_font("font", "Label");
  201. int font_size = get_theme_font_size("font_size", "Label");
  202. Color color = get_theme_color("font_color", "Label");
  203. int hsep = get_theme_constant("hseparation", "ItemList");
  204. int vsep = get_theme_constant("vseparation", "ItemList");
  205. Color linecolor = color;
  206. linecolor.a = 0.2;
  207. draw_line(Point2(limit, 0), Point2(limit, get_size().height), linecolor);
  208. int right_limit = get_size().width - timeline->get_buttons_width();
  209. draw_line(Point2(right_limit, 0), Point2(right_limit, get_size().height), linecolor);
  210. Ref<Texture2D> close_icon = get_theme_icon("Close", "EditorIcons");
  211. close_icon_rect.position = Vector2(get_size().width - close_icon->get_width() - hsep, hsep);
  212. close_icon_rect.size = close_icon->get_size();
  213. draw_texture(close_icon, close_icon_rect.position);
  214. String base_path = animation->track_get_path(track);
  215. int end = base_path.find(":");
  216. if (end != -1) {
  217. base_path = base_path.substr(0, end + 1);
  218. }
  219. // NAMES AND ICON
  220. int vofs = vsep;
  221. int margin = 0;
  222. {
  223. NodePath path = animation->track_get_path(track);
  224. Node *node = nullptr;
  225. if (root && root->has_node(path)) {
  226. node = root->get_node(path);
  227. }
  228. String text;
  229. if (node) {
  230. int ofs = 0;
  231. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(node, "Node");
  232. text = node->get_name();
  233. ofs += hsep;
  234. ofs += icon->get_width();
  235. TextLine text_buf = TextLine(text, font, font_size);
  236. text_buf.set_width(limit - ofs - hsep);
  237. int h = MAX(text_buf.get_size().y, icon->get_height());
  238. draw_texture(icon, Point2(ofs, vofs + int(h - icon->get_height()) / 2));
  239. margin = icon->get_width();
  240. Vector2 string_pos = Point2(ofs, vofs + (h - text_buf.get_size().y) / 2 + text_buf.get_line_ascent());
  241. string_pos = string_pos.floor();
  242. text_buf.draw(get_canvas_item(), string_pos, color);
  243. vofs += h + vsep;
  244. }
  245. }
  246. // RELATED TRACKS TITLES
  247. Map<int, Color> subtrack_colors;
  248. subtracks.clear();
  249. for (int i = 0; i < animation->get_track_count(); i++) {
  250. if (animation->track_get_type(i) != Animation::TYPE_BEZIER) {
  251. continue;
  252. }
  253. String path = animation->track_get_path(i);
  254. if (!path.begins_with(base_path)) {
  255. continue; //another node
  256. }
  257. path = path.replace_first(base_path, "");
  258. Color cc = color;
  259. TextLine text_buf = TextLine(path, font, font_size);
  260. text_buf.set_width(limit - margin - hsep);
  261. Rect2 rect = Rect2(margin, vofs, limit - margin - hsep, text_buf.get_size().y + vsep);
  262. if (i != track) {
  263. cc.a *= 0.7;
  264. uint32_t hash = path.hash();
  265. hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
  266. hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
  267. hash = (hash >> 16) ^ hash;
  268. float h = (hash % 65535) / 65536.0;
  269. Color subcolor;
  270. subcolor.set_hsv(h, 0.2, 0.8);
  271. subcolor.a = 0.5;
  272. draw_rect(Rect2(0, vofs + text_buf.get_size().y * 0.1, margin - hsep, text_buf.get_size().y * 0.8), subcolor);
  273. subtrack_colors[i] = subcolor;
  274. subtracks[i] = rect;
  275. } else {
  276. Color ac = get_theme_color("accent_color", "Editor");
  277. ac.a = 0.5;
  278. draw_rect(rect, ac);
  279. }
  280. Vector2 string_pos = Point2(margin, vofs + text_buf.get_line_ascent());
  281. text_buf.draw(get_canvas_item(), string_pos, cc);
  282. vofs += text_buf.get_size().y + vsep;
  283. }
  284. Color accent = get_theme_color("accent_color", "Editor");
  285. { //guides
  286. float min_left_scale = font->get_height(font_size) + vsep;
  287. float scale = (min_left_scale * 2) * v_zoom;
  288. float step = Math::pow(10.0, Math::round(Math::log(scale / 5.0) / Math::log(10.0))) * 5.0;
  289. scale = Math::snapped(scale, step);
  290. while (scale / v_zoom < min_left_scale * 2) {
  291. scale += step;
  292. }
  293. bool first = true;
  294. int prev_iv = 0;
  295. for (int i = font->get_height(font_size); i < get_size().height; i++) {
  296. float ofs = get_size().height / 2 - i;
  297. ofs *= v_zoom;
  298. ofs += v_scroll;
  299. int iv = int(ofs / scale);
  300. if (ofs < 0) {
  301. iv -= 1;
  302. }
  303. if (!first && iv != prev_iv) {
  304. Color lc = linecolor;
  305. lc.a *= 0.5;
  306. draw_line(Point2(limit, i), Point2(right_limit, i), lc);
  307. Color c = color;
  308. c.a *= 0.5;
  309. draw_string(font, Point2(limit + 8, i - 2), TS->format_number(rtos(Math::snapped((iv + 1) * scale, step))), HALIGN_LEFT, -1, font_size, c);
  310. }
  311. first = false;
  312. prev_iv = iv;
  313. }
  314. }
  315. { //draw OTHER curves
  316. float scale = timeline->get_zoom_scale();
  317. Ref<Texture2D> point = get_theme_icon("KeyValue", "EditorIcons");
  318. for (Map<int, Color>::Element *E = subtrack_colors.front(); E; E = E->next()) {
  319. _draw_track(E->key(), E->get());
  320. for (int i = 0; i < animation->track_get_key_count(E->key()); i++) {
  321. float offset = animation->track_get_key_time(E->key(), i);
  322. float value = animation->bezier_track_get_key_value(E->key(), i);
  323. Vector2 pos((offset - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value));
  324. if (pos.x >= limit && pos.x <= right_limit) {
  325. draw_texture(point, pos - point->get_size() / 2, E->get());
  326. }
  327. }
  328. }
  329. //draw edited curve
  330. const Color highlight = get_theme_color("highlight_color", "Editor");
  331. _draw_track(track, highlight);
  332. }
  333. //draw editor handles
  334. {
  335. float scale = timeline->get_zoom_scale();
  336. edit_points.clear();
  337. for (int i = 0; i < animation->track_get_key_count(track); i++) {
  338. float offset = animation->track_get_key_time(track, i);
  339. float value = animation->bezier_track_get_key_value(track, i);
  340. if (moving_selection && selection.has(i)) {
  341. offset += moving_selection_offset.x;
  342. value += moving_selection_offset.y;
  343. }
  344. Vector2 pos((offset - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value));
  345. Vector2 in_vec = animation->bezier_track_get_key_in_handle(track, i);
  346. if (moving_handle != 0 && moving_handle_key == i) {
  347. in_vec = moving_handle_left;
  348. }
  349. Vector2 pos_in = Vector2(((offset + in_vec.x) - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value + in_vec.y));
  350. Vector2 out_vec = animation->bezier_track_get_key_out_handle(track, i);
  351. if (moving_handle != 0 && moving_handle_key == i) {
  352. out_vec = moving_handle_right;
  353. }
  354. Vector2 pos_out = Vector2(((offset + out_vec.x) - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value + out_vec.y));
  355. _draw_line_clipped(pos, pos_in, accent, limit, right_limit);
  356. _draw_line_clipped(pos, pos_out, accent, limit, right_limit);
  357. EditPoint ep;
  358. if (pos.x >= limit && pos.x <= right_limit) {
  359. ep.point_rect.position = (pos - bezier_icon->get_size() / 2).floor();
  360. ep.point_rect.size = bezier_icon->get_size();
  361. if (selection.has(i)) {
  362. draw_texture(selected_icon, ep.point_rect.position);
  363. draw_string(font, ep.point_rect.position + Vector2(8, -font->get_height(font_size) - 4), TTR("Time:") + " " + TS->format_number(rtos(Math::snapped(offset, 0.001))), HALIGN_LEFT, -1, font_size, accent);
  364. draw_string(font, ep.point_rect.position + Vector2(8, -8), TTR("Value:") + " " + TS->format_number(rtos(Math::snapped(value, 0.001))), HALIGN_LEFT, -1, font_size, accent);
  365. } else {
  366. draw_texture(bezier_icon, ep.point_rect.position);
  367. }
  368. ep.point_rect = ep.point_rect.grow(ep.point_rect.size.width * 0.5);
  369. }
  370. if (pos_in.x >= limit && pos_in.x <= right_limit) {
  371. ep.in_rect.position = (pos_in - bezier_handle_icon->get_size() / 2).floor();
  372. ep.in_rect.size = bezier_handle_icon->get_size();
  373. draw_texture(bezier_handle_icon, ep.in_rect.position);
  374. ep.in_rect = ep.in_rect.grow(ep.in_rect.size.width * 0.5);
  375. }
  376. if (pos_out.x >= limit && pos_out.x <= right_limit) {
  377. ep.out_rect.position = (pos_out - bezier_handle_icon->get_size() / 2).floor();
  378. ep.out_rect.size = bezier_handle_icon->get_size();
  379. draw_texture(bezier_handle_icon, ep.out_rect.position);
  380. ep.out_rect = ep.out_rect.grow(ep.out_rect.size.width * 0.5);
  381. }
  382. edit_points.push_back(ep);
  383. }
  384. }
  385. if (box_selecting) {
  386. Color bs = accent;
  387. bs.a *= 0.5;
  388. Vector2 bs_from = box_selection_from;
  389. Vector2 bs_to = box_selection_to;
  390. if (bs_from.x > bs_to.x) {
  391. SWAP(bs_from.x, bs_to.x);
  392. }
  393. if (bs_from.y > bs_to.y) {
  394. SWAP(bs_from.y, bs_to.y);
  395. }
  396. draw_rect(Rect2(bs_from, bs_to - bs_from), bs);
  397. }
  398. }
  399. }
  400. Ref<Animation> AnimationBezierTrackEdit::get_animation() const {
  401. return animation;
  402. }
  403. void AnimationBezierTrackEdit::set_animation_and_track(const Ref<Animation> &p_animation, int p_track) {
  404. animation = p_animation;
  405. track = p_track;
  406. if (is_connected("select_key", Callable(editor, "_key_selected"))) {
  407. disconnect("select_key", Callable(editor, "_key_selected"));
  408. }
  409. if (is_connected("deselect_key", Callable(editor, "_key_deselected"))) {
  410. disconnect("deselect_key", Callable(editor, "_key_deselected"));
  411. }
  412. connect("select_key", Callable(editor, "_key_selected"), varray(p_track), CONNECT_DEFERRED);
  413. connect("deselect_key", Callable(editor, "_key_deselected"), varray(p_track), CONNECT_DEFERRED);
  414. update();
  415. }
  416. Size2 AnimationBezierTrackEdit::get_minimum_size() const {
  417. return Vector2(1, 1);
  418. }
  419. void AnimationBezierTrackEdit::set_undo_redo(UndoRedo *p_undo_redo) {
  420. undo_redo = p_undo_redo;
  421. }
  422. void AnimationBezierTrackEdit::set_timeline(AnimationTimelineEdit *p_timeline) {
  423. timeline = p_timeline;
  424. timeline->connect("zoom_changed", callable_mp(this, &AnimationBezierTrackEdit::_zoom_changed));
  425. }
  426. void AnimationBezierTrackEdit::set_editor(AnimationTrackEditor *p_editor) {
  427. editor = p_editor;
  428. connect("clear_selection", Callable(editor, "_clear_selection"), varray(false));
  429. }
  430. void AnimationBezierTrackEdit::_play_position_draw() {
  431. if (!animation.is_valid() || play_position_pos < 0) {
  432. return;
  433. }
  434. float scale = timeline->get_zoom_scale();
  435. int h = get_size().height;
  436. int px = (-timeline->get_value() + play_position_pos) * scale + timeline->get_name_limit();
  437. if (px >= timeline->get_name_limit() && px < (get_size().width - timeline->get_buttons_width())) {
  438. Color color = get_theme_color("accent_color", "Editor");
  439. play_position->draw_line(Point2(px, 0), Point2(px, h), color, Math::round(2 * EDSCALE));
  440. }
  441. }
  442. void AnimationBezierTrackEdit::set_play_position(float p_pos) {
  443. play_position_pos = p_pos;
  444. play_position->update();
  445. }
  446. void AnimationBezierTrackEdit::update_play_position() {
  447. play_position->update();
  448. }
  449. void AnimationBezierTrackEdit::set_root(Node *p_root) {
  450. root = p_root;
  451. }
  452. void AnimationBezierTrackEdit::_zoom_changed() {
  453. update();
  454. play_position->update();
  455. }
  456. String AnimationBezierTrackEdit::get_tooltip(const Point2 &p_pos) const {
  457. return Control::get_tooltip(p_pos);
  458. }
  459. void AnimationBezierTrackEdit::_clear_selection() {
  460. selection.clear();
  461. emit_signal("clear_selection");
  462. update();
  463. }
  464. void AnimationBezierTrackEdit::_clear_selection_for_anim(const Ref<Animation> &p_anim) {
  465. if (!(animation == p_anim)) {
  466. return;
  467. }
  468. //selection.clear();
  469. _clear_selection();
  470. }
  471. void AnimationBezierTrackEdit::_select_at_anim(const Ref<Animation> &p_anim, int p_track, float p_pos) {
  472. if (!(animation == p_anim)) {
  473. return;
  474. }
  475. int idx = animation->track_find_key(p_track, p_pos, true);
  476. ERR_FAIL_COND(idx < 0);
  477. selection.insert(idx);
  478. emit_signal("select_key", idx, true);
  479. update();
  480. }
  481. void AnimationBezierTrackEdit::_gui_input(const Ref<InputEvent> &p_event) {
  482. if (p_event->is_pressed()) {
  483. if (ED_GET_SHORTCUT("animation_editor/duplicate_selection")->is_shortcut(p_event)) {
  484. duplicate_selection();
  485. accept_event();
  486. }
  487. if (ED_GET_SHORTCUT("animation_editor/delete_selection")->is_shortcut(p_event)) {
  488. delete_selection();
  489. accept_event();
  490. }
  491. }
  492. Ref<InputEventMouseButton> mb = p_event;
  493. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_DOWN) {
  494. float v_zoom_orig = v_zoom;
  495. if (mb->get_command()) {
  496. timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() * 1.05);
  497. } else {
  498. if (v_zoom < 100000) {
  499. v_zoom *= 1.2;
  500. }
  501. }
  502. v_scroll = v_scroll + (mb->get_position().y - get_size().y / 2) * (v_zoom - v_zoom_orig);
  503. update();
  504. }
  505. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_WHEEL_UP) {
  506. float v_zoom_orig = v_zoom;
  507. if (mb->get_command()) {
  508. timeline->get_zoom()->set_value(timeline->get_zoom()->get_value() / 1.05);
  509. } else {
  510. if (v_zoom > 0.000001) {
  511. v_zoom /= 1.2;
  512. }
  513. }
  514. v_scroll = v_scroll + (mb->get_position().y - get_size().y / 2) * (v_zoom - v_zoom_orig);
  515. update();
  516. }
  517. if (mb.is_valid() && mb->get_button_index() == BUTTON_MIDDLE) {
  518. if (mb->is_pressed()) {
  519. int x = mb->get_position().x - timeline->get_name_limit();
  520. panning_timeline_from = x / timeline->get_zoom_scale();
  521. panning_timeline = true;
  522. panning_timeline_at = timeline->get_value();
  523. } else {
  524. panning_timeline = false;
  525. }
  526. }
  527. if (mb.is_valid() && mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
  528. menu_insert_key = mb->get_position();
  529. if (menu_insert_key.x >= timeline->get_name_limit() && menu_insert_key.x <= get_size().width - timeline->get_buttons_width()) {
  530. Vector2 popup_pos = get_global_transform().xform(mb->get_position());
  531. menu->clear();
  532. menu->add_icon_item(bezier_icon, TTR("Insert Key Here"), MENU_KEY_INSERT);
  533. if (selection.size()) {
  534. menu->add_separator();
  535. menu->add_icon_item(get_theme_icon("Duplicate", "EditorIcons"), TTR("Duplicate Selected Key(s)"), MENU_KEY_DUPLICATE);
  536. menu->add_separator();
  537. menu->add_icon_item(get_theme_icon("Remove", "EditorIcons"), TTR("Delete Selected Key(s)"), MENU_KEY_DELETE);
  538. }
  539. menu->set_as_minsize();
  540. menu->set_position(popup_pos);
  541. menu->popup();
  542. }
  543. }
  544. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  545. if (close_icon_rect.has_point(mb->get_position())) {
  546. emit_signal("close_request");
  547. return;
  548. }
  549. for (Map<int, Rect2>::Element *E = subtracks.front(); E; E = E->next()) {
  550. if (E->get().has_point(mb->get_position())) {
  551. set_animation_and_track(animation, E->key());
  552. _clear_selection();
  553. return;
  554. }
  555. }
  556. for (int i = 0; i < edit_points.size(); i++) {
  557. //first check point
  558. //command makes it ignore the main point, so control point editors can be force-edited
  559. //path 2D editing in the 3D and 2D editors works the same way
  560. if (!mb->get_command()) {
  561. if (edit_points[i].point_rect.has_point(mb->get_position())) {
  562. if (mb->get_shift()) {
  563. //add to selection
  564. if (selection.has(i)) {
  565. selection.erase(i);
  566. } else {
  567. selection.insert(i);
  568. }
  569. update();
  570. select_single_attempt = -1;
  571. } else if (selection.has(i)) {
  572. moving_selection_attempt = true;
  573. moving_selection = false;
  574. moving_selection_from_key = i;
  575. moving_selection_offset = Vector2();
  576. select_single_attempt = i;
  577. update();
  578. } else {
  579. moving_selection_attempt = true;
  580. moving_selection = true;
  581. moving_selection_from_key = i;
  582. moving_selection_offset = Vector2();
  583. selection.clear();
  584. selection.insert(i);
  585. update();
  586. }
  587. return;
  588. }
  589. }
  590. if (edit_points[i].in_rect.has_point(mb->get_position())) {
  591. moving_handle = -1;
  592. moving_handle_key = i;
  593. moving_handle_left = animation->bezier_track_get_key_in_handle(track, i);
  594. moving_handle_right = animation->bezier_track_get_key_out_handle(track, i);
  595. update();
  596. return;
  597. }
  598. if (edit_points[i].out_rect.has_point(mb->get_position())) {
  599. moving_handle = 1;
  600. moving_handle_key = i;
  601. moving_handle_left = animation->bezier_track_get_key_in_handle(track, i);
  602. moving_handle_right = animation->bezier_track_get_key_out_handle(track, i);
  603. update();
  604. return;
  605. ;
  606. }
  607. }
  608. //insert new point
  609. if (mb->get_command() && mb->get_position().x >= timeline->get_name_limit() && mb->get_position().x < get_size().width - timeline->get_buttons_width()) {
  610. Array new_point;
  611. new_point.resize(5);
  612. float h = (get_size().height / 2 - mb->get_position().y) * v_zoom + v_scroll;
  613. new_point[0] = h;
  614. new_point[1] = -0.25;
  615. new_point[2] = 0;
  616. new_point[3] = 0.25;
  617. new_point[4] = 0;
  618. float time = ((mb->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value();
  619. while (animation->track_find_key(track, time, true) != -1) {
  620. time += 0.001;
  621. }
  622. undo_redo->create_action(TTR("Add Bezier Point"));
  623. undo_redo->add_do_method(animation.ptr(), "track_insert_key", track, time, new_point);
  624. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", track, time);
  625. undo_redo->commit_action();
  626. //then attempt to move
  627. int index = animation->track_find_key(track, time, true);
  628. ERR_FAIL_COND(index == -1);
  629. _clear_selection();
  630. selection.insert(index);
  631. moving_selection_attempt = true;
  632. moving_selection = false;
  633. moving_selection_from_key = index;
  634. moving_selection_offset = Vector2();
  635. select_single_attempt = -1;
  636. update();
  637. return;
  638. }
  639. //box select
  640. if (mb->get_position().x >= timeline->get_name_limit() && mb->get_position().x < get_size().width - timeline->get_buttons_width()) {
  641. box_selecting_attempt = true;
  642. box_selecting = false;
  643. box_selecting_add = false;
  644. box_selection_from = mb->get_position();
  645. return;
  646. }
  647. }
  648. if (box_selecting_attempt && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  649. if (box_selecting) {
  650. //do actual select
  651. if (!box_selecting_add) {
  652. _clear_selection();
  653. }
  654. Vector2 bs_from = box_selection_from;
  655. Vector2 bs_to = box_selection_to;
  656. if (bs_from.x > bs_to.x) {
  657. SWAP(bs_from.x, bs_to.x);
  658. }
  659. if (bs_from.y > bs_to.y) {
  660. SWAP(bs_from.y, bs_to.y);
  661. }
  662. Rect2 selection_rect(bs_from, bs_to - bs_from);
  663. for (int i = 0; i < edit_points.size(); i++) {
  664. if (edit_points[i].point_rect.intersects(selection_rect)) {
  665. selection.insert(i);
  666. }
  667. }
  668. } else {
  669. _clear_selection(); //clicked and nothing happened, so clear the selection
  670. }
  671. box_selecting_attempt = false;
  672. box_selecting = false;
  673. update();
  674. }
  675. if (moving_handle != 0 && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  676. undo_redo->create_action(TTR("Move Bezier Points"));
  677. undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_in_handle", track, moving_handle_key, moving_handle_left);
  678. undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_out_handle", track, moving_handle_key, moving_handle_right);
  679. undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle", track, moving_handle_key, animation->bezier_track_get_key_in_handle(track, moving_handle_key));
  680. undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle", track, moving_handle_key, animation->bezier_track_get_key_out_handle(track, moving_handle_key));
  681. undo_redo->commit_action();
  682. moving_handle = 0;
  683. update();
  684. }
  685. if (moving_selection_attempt && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  686. if (moving_selection) {
  687. //combit it
  688. undo_redo->create_action(TTR("Move Bezier Points"));
  689. List<AnimMoveRestore> to_restore;
  690. // 1-remove the keys
  691. for (Set<int>::Element *E = selection.back(); E; E = E->prev()) {
  692. undo_redo->add_do_method(animation.ptr(), "track_remove_key", track, E->get());
  693. }
  694. // 2- remove overlapped keys
  695. for (Set<int>::Element *E = selection.back(); E; E = E->prev()) {
  696. float newtime = editor->snap_time(animation->track_get_key_time(track, E->get()) + moving_selection_offset.x);
  697. int idx = animation->track_find_key(track, newtime, true);
  698. if (idx == -1) {
  699. continue;
  700. }
  701. if (selection.has(idx)) {
  702. continue; //already in selection, don't save
  703. }
  704. undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_position", track, newtime);
  705. AnimMoveRestore amr;
  706. amr.key = animation->track_get_key_value(track, idx);
  707. amr.track = track;
  708. amr.time = newtime;
  709. to_restore.push_back(amr);
  710. }
  711. // 3-move the keys (re insert them)
  712. for (Set<int>::Element *E = selection.back(); E; E = E->prev()) {
  713. float newpos = editor->snap_time(animation->track_get_key_time(track, E->get()) + moving_selection_offset.x);
  714. /*
  715. if (newpos<0)
  716. continue; //no add at the beginning
  717. */
  718. Array key = animation->track_get_key_value(track, E->get());
  719. float h = key[0];
  720. h += moving_selection_offset.y;
  721. key[0] = h;
  722. undo_redo->add_do_method(animation.ptr(), "track_insert_key", track, newpos, key, 1);
  723. }
  724. // 4-(undo) remove inserted keys
  725. for (Set<int>::Element *E = selection.back(); E; E = E->prev()) {
  726. float newpos = editor->snap_time(animation->track_get_key_time(track, E->get()) + moving_selection_offset.x);
  727. /*
  728. if (newpos<0)
  729. continue; //no remove what no inserted
  730. */
  731. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", track, newpos);
  732. }
  733. // 5-(undo) reinsert keys
  734. for (Set<int>::Element *E = selection.back(); E; E = E->prev()) {
  735. float oldpos = animation->track_get_key_time(track, E->get());
  736. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track, oldpos, animation->track_get_key_value(track, E->get()), 1);
  737. }
  738. // 6-(undo) reinsert overlapped keys
  739. for (List<AnimMoveRestore>::Element *E = to_restore.front(); E; E = E->next()) {
  740. AnimMoveRestore &amr = E->get();
  741. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, 1);
  742. }
  743. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  744. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  745. // 7-reselect
  746. for (Set<int>::Element *E = selection.back(); E; E = E->prev()) {
  747. float oldpos = animation->track_get_key_time(track, E->get());
  748. float newpos = editor->snap_time(oldpos + moving_selection_offset.x);
  749. undo_redo->add_do_method(this, "_select_at_anim", animation, track, newpos);
  750. undo_redo->add_undo_method(this, "_select_at_anim", animation, track, oldpos);
  751. }
  752. undo_redo->commit_action();
  753. moving_selection = false;
  754. } else if (select_single_attempt != -1) {
  755. selection.clear();
  756. selection.insert(select_single_attempt);
  757. }
  758. moving_selection_attempt = false;
  759. update();
  760. }
  761. Ref<InputEventMouseMotion> mm = p_event;
  762. if (mm.is_valid() && mm->get_button_mask() & BUTTON_MASK_MIDDLE) {
  763. v_scroll += mm->get_relative().y * v_zoom;
  764. if (v_scroll > 100000) {
  765. v_scroll = 100000;
  766. }
  767. if (v_scroll < -100000) {
  768. v_scroll = -100000;
  769. }
  770. int x = mm->get_position().x - timeline->get_name_limit();
  771. float ofs = x / timeline->get_zoom_scale();
  772. float diff = ofs - panning_timeline_from;
  773. timeline->set_value(panning_timeline_at - diff);
  774. update();
  775. }
  776. if (moving_selection_attempt && mm.is_valid()) {
  777. if (!moving_selection) {
  778. moving_selection = true;
  779. select_single_attempt = -1;
  780. }
  781. float y = (get_size().height / 2 - mm->get_position().y) * v_zoom + v_scroll;
  782. float x = editor->snap_time(((mm->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value());
  783. moving_selection_offset = Vector2(x - animation->track_get_key_time(track, moving_selection_from_key), y - animation->bezier_track_get_key_value(track, moving_selection_from_key));
  784. update();
  785. }
  786. if (box_selecting_attempt && mm.is_valid()) {
  787. if (!box_selecting) {
  788. box_selecting = true;
  789. box_selecting_add = mm->get_shift();
  790. }
  791. box_selection_to = mm->get_position();
  792. if (get_local_mouse_position().y < 0) {
  793. //avoid cursor from going too above, so it does not lose focus with viewport
  794. warp_mouse(Vector2(get_local_mouse_position().x, 0));
  795. }
  796. update();
  797. }
  798. if (moving_handle != 0 && mm.is_valid()) {
  799. float y = (get_size().height / 2 - mm->get_position().y) * v_zoom + v_scroll;
  800. float x = ((mm->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value();
  801. Vector2 key_pos = Vector2(animation->track_get_key_time(track, moving_handle_key), animation->bezier_track_get_key_value(track, moving_handle_key));
  802. Vector2 moving_handle_value = Vector2(x, y) - key_pos;
  803. moving_handle_left = animation->bezier_track_get_key_in_handle(track, moving_handle_key);
  804. moving_handle_right = animation->bezier_track_get_key_out_handle(track, moving_handle_key);
  805. if (moving_handle == -1) {
  806. moving_handle_left = moving_handle_value;
  807. if (moving_handle_left.x > 0) {
  808. moving_handle_left.x = 0;
  809. }
  810. if (handle_mode_option->get_selected() == HANDLE_MODE_BALANCED) {
  811. Vector2 scale = Vector2(timeline->get_zoom_scale(), v_zoom);
  812. moving_handle_right = (-(moving_handle_left * scale).normalized() * (moving_handle_right * scale).length()) / scale;
  813. } else if (handle_mode_option->get_selected() == HANDLE_MODE_MIRROR) {
  814. moving_handle_right = -moving_handle_left;
  815. }
  816. }
  817. if (moving_handle == 1) {
  818. moving_handle_right = moving_handle_value;
  819. if (moving_handle_right.x < 0) {
  820. moving_handle_right.x = 0;
  821. }
  822. if (handle_mode_option->get_selected() == HANDLE_MODE_BALANCED) {
  823. Vector2 scale = Vector2(timeline->get_zoom_scale(), v_zoom);
  824. moving_handle_left = (-(moving_handle_right * scale).normalized() * (moving_handle_left * scale).length()) / scale;
  825. } else if (handle_mode_option->get_selected() == HANDLE_MODE_MIRROR) {
  826. moving_handle_left = -moving_handle_right;
  827. }
  828. }
  829. update();
  830. }
  831. }
  832. void AnimationBezierTrackEdit::_menu_selected(int p_index) {
  833. switch (p_index) {
  834. case MENU_KEY_INSERT: {
  835. Array new_point;
  836. new_point.resize(5);
  837. float h = (get_size().height / 2 - menu_insert_key.y) * v_zoom + v_scroll;
  838. new_point[0] = h;
  839. new_point[1] = -0.25;
  840. new_point[2] = 0;
  841. new_point[3] = 0.25;
  842. new_point[4] = 0;
  843. float time = ((menu_insert_key.x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value();
  844. while (animation->track_find_key(track, time, true) != -1) {
  845. time += 0.001;
  846. }
  847. undo_redo->create_action(TTR("Add Bezier Point"));
  848. undo_redo->add_do_method(animation.ptr(), "track_insert_key", track, time, new_point);
  849. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", track, time);
  850. undo_redo->commit_action();
  851. } break;
  852. case MENU_KEY_DUPLICATE: {
  853. duplicate_selection();
  854. } break;
  855. case MENU_KEY_DELETE: {
  856. delete_selection();
  857. } break;
  858. }
  859. }
  860. void AnimationBezierTrackEdit::duplicate_selection() {
  861. if (selection.size() == 0) {
  862. return;
  863. }
  864. float top_time = 1e10;
  865. for (Set<int>::Element *E = selection.back(); E; E = E->prev()) {
  866. float t = animation->track_get_key_time(track, E->get());
  867. if (t < top_time) {
  868. top_time = t;
  869. }
  870. }
  871. undo_redo->create_action(TTR("Anim Duplicate Keys"));
  872. List<Pair<int, float>> new_selection_values;
  873. for (Set<int>::Element *E = selection.back(); E; E = E->prev()) {
  874. float t = animation->track_get_key_time(track, E->get());
  875. float dst_time = t + (timeline->get_play_position() - top_time);
  876. int existing_idx = animation->track_find_key(track, dst_time, true);
  877. undo_redo->add_do_method(animation.ptr(), "track_insert_key", track, dst_time, animation->track_get_key_value(track, E->get()), animation->track_get_key_transition(track, E->get()));
  878. undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_position", track, dst_time);
  879. Pair<int, float> p;
  880. p.first = track;
  881. p.second = dst_time;
  882. new_selection_values.push_back(p);
  883. if (existing_idx != -1) {
  884. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track, dst_time, animation->track_get_key_value(track, existing_idx), animation->track_get_key_transition(track, existing_idx));
  885. }
  886. }
  887. undo_redo->commit_action();
  888. //reselect duplicated
  889. selection.clear();
  890. for (List<Pair<int, float>>::Element *E = new_selection_values.front(); E; E = E->next()) {
  891. int track = E->get().first;
  892. float time = E->get().second;
  893. int existing_idx = animation->track_find_key(track, time, true);
  894. if (existing_idx == -1) {
  895. continue;
  896. }
  897. selection.insert(existing_idx);
  898. }
  899. update();
  900. }
  901. void AnimationBezierTrackEdit::delete_selection() {
  902. if (selection.size()) {
  903. undo_redo->create_action(TTR("Anim Delete Keys"));
  904. for (Set<int>::Element *E = selection.back(); E; E = E->prev()) {
  905. undo_redo->add_do_method(animation.ptr(), "track_remove_key", track, E->get());
  906. undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track, animation->track_get_key_time(track, E->get()), animation->track_get_key_value(track, E->get()), 1);
  907. }
  908. undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
  909. undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
  910. undo_redo->commit_action();
  911. //selection.clear();
  912. }
  913. }
  914. void AnimationBezierTrackEdit::set_block_animation_update_ptr(bool *p_block_ptr) {
  915. block_animation_update_ptr = p_block_ptr;
  916. }
  917. void AnimationBezierTrackEdit::_bind_methods() {
  918. ClassDB::bind_method("_gui_input", &AnimationBezierTrackEdit::_gui_input);
  919. ClassDB::bind_method("_clear_selection", &AnimationBezierTrackEdit::_clear_selection);
  920. ClassDB::bind_method("_clear_selection_for_anim", &AnimationBezierTrackEdit::_clear_selection_for_anim);
  921. ClassDB::bind_method("_select_at_anim", &AnimationBezierTrackEdit::_select_at_anim);
  922. ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::FLOAT, "position"), PropertyInfo(Variant::BOOL, "drag")));
  923. ADD_SIGNAL(MethodInfo("remove_request", PropertyInfo(Variant::INT, "track")));
  924. ADD_SIGNAL(MethodInfo("insert_key", PropertyInfo(Variant::FLOAT, "ofs")));
  925. ADD_SIGNAL(MethodInfo("select_key", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "single")));
  926. ADD_SIGNAL(MethodInfo("deselect_key", PropertyInfo(Variant::INT, "index")));
  927. ADD_SIGNAL(MethodInfo("clear_selection"));
  928. ADD_SIGNAL(MethodInfo("close_request"));
  929. ADD_SIGNAL(MethodInfo("move_selection_begin"));
  930. ADD_SIGNAL(MethodInfo("move_selection", PropertyInfo(Variant::FLOAT, "ofs")));
  931. ADD_SIGNAL(MethodInfo("move_selection_commit"));
  932. ADD_SIGNAL(MethodInfo("move_selection_cancel"));
  933. }
  934. AnimationBezierTrackEdit::AnimationBezierTrackEdit() {
  935. undo_redo = nullptr;
  936. timeline = nullptr;
  937. root = nullptr;
  938. menu = nullptr;
  939. block_animation_update_ptr = nullptr;
  940. moving_selection_attempt = false;
  941. moving_selection = false;
  942. select_single_attempt = -1;
  943. box_selecting = false;
  944. box_selecting_attempt = false;
  945. moving_handle = 0;
  946. play_position_pos = 0;
  947. play_position = memnew(Control);
  948. play_position->set_mouse_filter(MOUSE_FILTER_PASS);
  949. add_child(play_position);
  950. play_position->set_anchors_and_offsets_preset(PRESET_WIDE);
  951. play_position->connect("draw", callable_mp(this, &AnimationBezierTrackEdit::_play_position_draw));
  952. set_focus_mode(FOCUS_CLICK);
  953. v_scroll = 0;
  954. v_zoom = 1;
  955. panning_timeline = false;
  956. set_clip_contents(true);
  957. handle_mode = HANDLE_MODE_FREE;
  958. handle_mode_option = memnew(OptionButton);
  959. add_child(handle_mode_option);
  960. menu = memnew(PopupMenu);
  961. add_child(menu);
  962. menu->connect("id_pressed", callable_mp(this, &AnimationBezierTrackEdit::_menu_selected));
  963. //set_mouse_filter(MOUSE_FILTER_PASS); //scroll has to work too for selection
  964. }