animation_bezier_editor.cpp 38 KB

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