animation_bezier_editor.cpp 39 KB

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