animation_track_editor.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /**************************************************************************/
  2. /* animation_track_editor.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "editor/editor_data.h"
  32. #include "editor/inspector/editor_properties.h"
  33. #include "editor/inspector/property_selector.h"
  34. #include "scene/3d/node_3d.h"
  35. #include "scene/gui/control.h"
  36. #include "scene/gui/menu_button.h"
  37. #include "scene/gui/scroll_bar.h"
  38. #include "scene/gui/tree.h"
  39. #include "scene/resources/animation.h"
  40. class AnimationMarkerEdit;
  41. class AnimationTrackEditor;
  42. class AnimationTrackEdit;
  43. class CheckBox;
  44. class ColorPickerButton;
  45. class EditorSpinSlider;
  46. class HSlider;
  47. class OptionButton;
  48. class PanelContainer;
  49. class SceneTreeDialog;
  50. class SpinBox;
  51. class TextureRect;
  52. class ViewPanner;
  53. class EditorValidationPanel;
  54. class AnimationTrackKeyEdit : public Object {
  55. GDCLASS(AnimationTrackKeyEdit, Object);
  56. public:
  57. bool setting = false;
  58. bool animation_read_only = false;
  59. Ref<Animation> animation;
  60. int track = -1;
  61. float key_ofs = 0;
  62. Node *root_path = nullptr;
  63. PropertyInfo hint;
  64. NodePath base;
  65. bool use_fps = false;
  66. AnimationTrackEditor *editor = nullptr;
  67. bool _hide_script_from_inspector() { return true; }
  68. bool _hide_metadata_from_inspector() { return true; }
  69. bool _dont_undo_redo() { return true; }
  70. bool _is_read_only() { return animation_read_only; }
  71. void notify_change();
  72. Node *get_root_path();
  73. void set_use_fps(bool p_enable);
  74. protected:
  75. static void _bind_methods();
  76. void _fix_node_path(Variant &value);
  77. void _update_obj(const Ref<Animation> &p_anim);
  78. void _key_ofs_changed(const Ref<Animation> &p_anim, float from, float to);
  79. bool _set(const StringName &p_name, const Variant &p_value);
  80. bool _get(const StringName &p_name, Variant &r_ret) const;
  81. void _get_property_list(List<PropertyInfo> *p_list) const;
  82. };
  83. class AnimationMultiTrackKeyEdit : public Object {
  84. GDCLASS(AnimationMultiTrackKeyEdit, Object);
  85. public:
  86. bool setting = false;
  87. bool animation_read_only = false;
  88. Ref<Animation> animation;
  89. RBMap<int, List<float>> key_ofs_map;
  90. RBMap<int, NodePath> base_map;
  91. PropertyInfo hint;
  92. Node *root_path = nullptr;
  93. bool use_fps = false;
  94. AnimationTrackEditor *editor = nullptr;
  95. bool _hide_script_from_inspector() { return true; }
  96. bool _hide_metadata_from_inspector() { return true; }
  97. bool _dont_undo_redo() { return true; }
  98. bool _is_read_only() { return animation_read_only; }
  99. void notify_change();
  100. Node *get_root_path();
  101. void set_use_fps(bool p_enable);
  102. protected:
  103. static void _bind_methods();
  104. void _fix_node_path(Variant &value, NodePath &base);
  105. void _update_obj(const Ref<Animation> &p_anim);
  106. void _key_ofs_changed(const Ref<Animation> &p_anim, float from, float to);
  107. bool _set(const StringName &p_name, const Variant &p_value);
  108. bool _get(const StringName &p_name, Variant &r_ret) const;
  109. void _get_property_list(List<PropertyInfo> *p_list) const;
  110. };
  111. class AnimationMarkerKeyEdit : public Object {
  112. GDCLASS(AnimationMarkerKeyEdit, Object);
  113. public:
  114. bool animation_read_only = false;
  115. Ref<Animation> animation;
  116. StringName marker_name;
  117. bool use_fps = false;
  118. AnimationMarkerEdit *marker_edit = nullptr;
  119. bool _hide_script_from_inspector() { return true; }
  120. bool _hide_metadata_from_inspector() { return true; }
  121. bool _dont_undo_redo() { return true; }
  122. bool _is_read_only() { return animation_read_only; }
  123. float get_time() const;
  124. protected:
  125. static void _bind_methods();
  126. void _set_marker_name(const StringName &p_name);
  127. bool _set(const StringName &p_name, const Variant &p_value);
  128. bool _get(const StringName &p_name, Variant &r_ret) const;
  129. void _get_property_list(List<PropertyInfo> *p_list) const;
  130. };
  131. class AnimationMultiMarkerKeyEdit : public Object {
  132. GDCLASS(AnimationMultiMarkerKeyEdit, Object);
  133. public:
  134. bool animation_read_only = false;
  135. Ref<Animation> animation;
  136. Vector<StringName> marker_names;
  137. AnimationMarkerEdit *marker_edit = nullptr;
  138. bool _hide_script_from_inspector() { return true; }
  139. bool _hide_metadata_from_inspector() { return true; }
  140. bool _dont_undo_redo() { return true; }
  141. bool _is_read_only() { return animation_read_only; }
  142. protected:
  143. static void _bind_methods();
  144. bool _set(const StringName &p_name, const Variant &p_value);
  145. bool _get(const StringName &p_name, Variant &r_ret) const;
  146. void _get_property_list(List<PropertyInfo> *p_list) const;
  147. };
  148. class AnimationTimelineEdit : public Range {
  149. GDCLASS(AnimationTimelineEdit, Range);
  150. friend class AnimationBezierTrackEdit;
  151. friend class AnimationTrackEditor;
  152. static constexpr float SCROLL_ZOOM_FACTOR_IN = 1.02f; // Zoom factor per mouse scroll in the animation editor when zooming in. The closer to 1.0, the finer the control.
  153. static constexpr float SCROLL_ZOOM_FACTOR_OUT = 0.98f; // Zoom factor when zooming out. Similar to SCROLL_ZOOM_FACTOR_IN but less than 1.0.
  154. Ref<Animation> animation;
  155. bool read_only = false;
  156. AnimationTrackEdit *track_edit = nullptr;
  157. int name_limit = 0;
  158. Range *zoom = nullptr;
  159. Range *h_scroll = nullptr;
  160. float play_position_pos = 0.0f;
  161. HBoxContainer *add_track_hb = nullptr;
  162. HBoxContainer *len_hb = nullptr;
  163. EditorSpinSlider *length = nullptr;
  164. Button *loop = nullptr;
  165. TextureRect *time_icon = nullptr;
  166. MenuButton *add_track = nullptr;
  167. LineEdit *filter_track = nullptr;
  168. Control *play_position = nullptr; //separate control used to draw so updates for only position changed are much faster
  169. HScrollBar *hscroll = nullptr;
  170. void _zoom_changed(double);
  171. void _anim_length_changed(double p_new_len);
  172. void _anim_loop_pressed();
  173. void _play_position_draw();
  174. Rect2 hsize_rect;
  175. bool editing = false;
  176. bool use_fps = false;
  177. Ref<ViewPanner> panner;
  178. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  179. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  180. bool dragging_timeline = false;
  181. bool dragging_hsize = false;
  182. float dragging_hsize_from = 0.0f;
  183. float dragging_hsize_at = 0.0f;
  184. double last_zoom_scale = 1.0;
  185. double hscroll_on_zoom_buffer = -1.0;
  186. Vector2 zoom_scroll_origin;
  187. bool zoom_callback_occurred = false;
  188. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  189. void _track_added(int p_track);
  190. float _get_zoom_scale(double p_zoom_value) const;
  191. void _scroll_to_start();
  192. protected:
  193. static void _bind_methods();
  194. void _notification(int p_what);
  195. public:
  196. int get_name_limit() const;
  197. int get_buttons_width() const;
  198. float get_zoom_scale() const;
  199. virtual Size2 get_minimum_size() const override;
  200. void set_animation(const Ref<Animation> &p_animation, bool p_read_only);
  201. void set_track_edit(AnimationTrackEdit *p_track_edit);
  202. void set_zoom(Range *p_zoom);
  203. Range *get_zoom() const { return zoom; }
  204. void auto_fit();
  205. void set_play_position(float p_pos);
  206. float get_play_position() const;
  207. void update_play_position();
  208. void update_values();
  209. void set_use_fps(bool p_use_fps);
  210. bool is_using_fps() const;
  211. void set_hscroll(HScrollBar *p_hscroll);
  212. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  213. AnimationTimelineEdit();
  214. };
  215. class AnimationMarkerEdit : public Control {
  216. GDCLASS(AnimationMarkerEdit, Control);
  217. friend class AnimationTimelineEdit;
  218. enum {
  219. MENU_KEY_INSERT,
  220. MENU_KEY_RENAME,
  221. MENU_KEY_DELETE,
  222. MENU_KEY_TOGGLE_MARKER_NAMES,
  223. };
  224. AnimationTimelineEdit *timeline = nullptr;
  225. Control *play_position = nullptr; // Separate control used to draw so updates for only position changed are much faster.
  226. float play_position_pos = 0.0f;
  227. HashSet<StringName> selection;
  228. Ref<Animation> animation;
  229. bool read_only = false;
  230. Ref<Texture2D> type_icon;
  231. Ref<Texture2D> selected_icon;
  232. PopupMenu *menu = nullptr;
  233. bool hovered = false;
  234. StringName hovering_marker;
  235. void _zoom_changed();
  236. Ref<Texture2D> icon_cache;
  237. void _menu_selected(int p_index);
  238. void _play_position_draw();
  239. bool _try_select_at_ui_pos(const Point2 &p_pos, bool p_aggregate, bool p_deselectable);
  240. bool _is_ui_pos_in_current_section(const Point2 &p_pos);
  241. float insert_at_pos = 0.0f;
  242. bool moving_selection_attempt = false;
  243. bool moving_selection_effective = false;
  244. float moving_selection_offset = 0.0f;
  245. float moving_selection_pivot = 0.0f;
  246. float moving_selection_mouse_begin_x = 0.0f;
  247. float moving_selection_mouse_begin_y = 0.0f;
  248. StringName select_single_attempt;
  249. bool moving_selection = false;
  250. void _move_selection_begin();
  251. void _move_selection(float p_offset);
  252. void _move_selection_commit();
  253. void _move_selection_cancel();
  254. void _clear_selection_for_anim(const Ref<Animation> &p_anim);
  255. void _select_key(const StringName &p_name, bool is_single = false);
  256. void _deselect_key(const StringName &p_name);
  257. void _insert_marker(float p_ofs);
  258. void _rename_marker(const StringName &p_name);
  259. void _delete_selected_markers();
  260. ConfirmationDialog *marker_insert_confirm = nullptr;
  261. LineEdit *marker_insert_new_name = nullptr;
  262. ColorPickerButton *marker_insert_color = nullptr;
  263. AcceptDialog *marker_insert_error_dialog = nullptr;
  264. float marker_insert_ofs = 0;
  265. ConfirmationDialog *marker_rename_confirm = nullptr;
  266. LineEdit *marker_rename_new_name = nullptr;
  267. StringName marker_rename_prev_name;
  268. AcceptDialog *marker_rename_error_dialog = nullptr;
  269. bool should_show_all_marker_names = false;
  270. ////////////// edit menu stuff
  271. void _marker_insert_confirmed();
  272. void _marker_insert_new_name_changed(const String &p_text);
  273. void _marker_rename_confirmed();
  274. void _marker_rename_new_name_changed(const String &p_text);
  275. AnimationTrackEditor *editor = nullptr;
  276. HBoxContainer *_create_hbox_labeled_control(const String &p_text, Control *p_control) const;
  277. void _update_key_edit();
  278. void _clear_key_edit();
  279. AnimationMarkerKeyEdit *key_edit = nullptr;
  280. AnimationMultiMarkerKeyEdit *multi_key_edit = nullptr;
  281. protected:
  282. static void _bind_methods();
  283. void _notification(int p_what);
  284. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  285. public:
  286. virtual String get_tooltip(const Point2 &p_pos) const override;
  287. virtual int get_key_height() const;
  288. virtual Rect2 get_key_rect(float p_pixels_sec) const;
  289. virtual bool is_key_selectable_by_distance() const;
  290. virtual void draw_key(const StringName &p_name, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right);
  291. virtual void draw_bg(int p_clip_left, int p_clip_right);
  292. virtual void draw_fg(int p_clip_left, int p_clip_right);
  293. Ref<Animation> get_animation() const;
  294. AnimationTimelineEdit *get_timeline() const { return timeline; }
  295. AnimationTrackEditor *get_editor() const { return editor; }
  296. bool is_selection_active() const { return !selection.is_empty(); }
  297. bool is_moving_selection() const { return moving_selection; }
  298. float get_moving_selection_offset() const { return moving_selection_offset; }
  299. void set_animation(const Ref<Animation> &p_animation, bool p_read_only);
  300. virtual Size2 get_minimum_size() const override;
  301. void set_timeline(AnimationTimelineEdit *p_timeline);
  302. void set_editor(AnimationTrackEditor *p_editor);
  303. void set_play_position(float p_pos);
  304. void update_play_position();
  305. void set_use_fps(bool p_use_fps);
  306. PackedStringArray get_selected_section() const;
  307. bool is_marker_selected(const StringName &p_marker) const;
  308. // For use by AnimationTrackEditor.
  309. void _clear_selection(bool p_update);
  310. AnimationMarkerEdit();
  311. };
  312. class AnimationTrackEdit : public Control {
  313. GDCLASS(AnimationTrackEdit, Control);
  314. friend class AnimationTimelineEdit;
  315. enum {
  316. MENU_CALL_MODE_CONTINUOUS,
  317. MENU_CALL_MODE_DISCRETE,
  318. MENU_CALL_MODE_CAPTURE,
  319. MENU_INTERPOLATION_NEAREST,
  320. MENU_INTERPOLATION_LINEAR,
  321. MENU_INTERPOLATION_CUBIC,
  322. MENU_INTERPOLATION_LINEAR_ANGLE,
  323. MENU_INTERPOLATION_CUBIC_ANGLE,
  324. MENU_LOOP_WRAP,
  325. MENU_LOOP_CLAMP,
  326. MENU_KEY_INSERT,
  327. MENU_KEY_DUPLICATE,
  328. MENU_KEY_CUT,
  329. MENU_KEY_COPY,
  330. MENU_KEY_PASTE,
  331. MENU_KEY_ADD_RESET,
  332. MENU_KEY_DELETE,
  333. MENU_KEY_LOOKUP,
  334. MENU_USE_BLEND_ENABLED,
  335. MENU_USE_BLEND_DISABLED,
  336. };
  337. AnimationTimelineEdit *timeline = nullptr;
  338. Popup *path_popup = nullptr;
  339. LineEdit *path = nullptr;
  340. Node *root = nullptr;
  341. Control *play_position = nullptr; //separate control used to draw so updates for only position changed are much faster
  342. float play_position_pos = 0.0f;
  343. NodePath node_path;
  344. Ref<Animation> animation;
  345. bool read_only = false;
  346. int track = 0;
  347. Rect2 check_rect;
  348. Rect2 icon_rect;
  349. Rect2 path_rect;
  350. Rect2 update_mode_rect;
  351. Rect2 interp_mode_rect;
  352. Rect2 loop_wrap_rect;
  353. Rect2 remove_rect;
  354. Ref<Texture2D> type_icon;
  355. Ref<Texture2D> selected_icon;
  356. PopupMenu *menu = nullptr;
  357. bool hovered = false;
  358. bool clicking_on_name = false;
  359. int hovering_key_idx = -1;
  360. void _zoom_changed();
  361. Ref<Texture2D> icon_cache;
  362. String path_cache;
  363. void _menu_selected(int p_index);
  364. void _path_submitted(const String &p_text);
  365. void _play_position_draw();
  366. bool _is_value_key_valid(const Variant &p_key_value, Variant::Type &r_valid_type) const;
  367. bool _try_select_at_ui_pos(const Point2 &p_pos, bool p_aggregate, bool p_deselectable);
  368. int lookup_key_idx = -1;
  369. bool _lookup_key(int p_key_idx) const;
  370. Ref<Texture2D> _get_key_type_icon() const;
  371. mutable int dropping_at = 0;
  372. float insert_at_pos = 0.0f;
  373. bool moving_selection_attempt = false;
  374. bool moving_selection_effective = false;
  375. float moving_selection_pivot = 0.0f;
  376. float moving_selection_mouse_begin_x = 0.0f;
  377. int select_single_attempt = -1;
  378. bool moving_selection = false;
  379. bool command_or_control_pressed = false;
  380. bool in_group = false;
  381. AnimationTrackEditor *editor = nullptr;
  382. protected:
  383. static void _bind_methods();
  384. void _notification(int p_what);
  385. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  386. public:
  387. virtual Variant get_drag_data(const Point2 &p_point) override;
  388. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  389. virtual void drop_data(const Point2 &p_point, const Variant &p_data) override;
  390. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  391. virtual String get_tooltip(const Point2 &p_pos) const override;
  392. virtual int get_key_height() const;
  393. virtual Rect2 get_key_rect(int p_index, float p_pixels_sec);
  394. virtual bool is_key_selectable_by_distance() const;
  395. virtual void draw_key_link(int p_index, float p_pixels_sec, int p_x, int p_next_x, int p_clip_left, int p_clip_right);
  396. virtual void draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right);
  397. virtual void draw_bg(int p_clip_left, int p_clip_right);
  398. virtual void draw_fg(int p_clip_left, int p_clip_right);
  399. //helper
  400. void draw_texture_region_clipped(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_region);
  401. void draw_rect_clipped(const Rect2 &p_rect, const Color &p_color, bool p_filled = true);
  402. int get_track() const;
  403. Ref<Animation> get_animation() const;
  404. AnimationTimelineEdit *get_timeline() const { return timeline; }
  405. AnimationTrackEditor *get_editor() const { return editor; }
  406. NodePath get_path() const;
  407. void set_animation_and_track(const Ref<Animation> &p_animation, int p_track, bool p_read_only);
  408. virtual Size2 get_minimum_size() const override;
  409. void set_timeline(AnimationTimelineEdit *p_timeline);
  410. void set_editor(AnimationTrackEditor *p_editor);
  411. void set_root(Node *p_root);
  412. void set_play_position(float p_pos);
  413. void update_play_position();
  414. void cancel_drop();
  415. void set_in_group(bool p_enable);
  416. void append_to_selection(const Rect2 &p_box, bool p_deselection);
  417. AnimationTrackEdit();
  418. };
  419. class AnimationTrackEditPlugin : public RefCounted {
  420. GDCLASS(AnimationTrackEditPlugin, RefCounted);
  421. public:
  422. virtual AnimationTrackEdit *create_value_track_edit(Object *p_object, Variant::Type p_type, const String &p_property, PropertyHint p_hint, const String &p_hint_string, int p_usage);
  423. virtual AnimationTrackEdit *create_audio_track_edit();
  424. virtual AnimationTrackEdit *create_animation_track_edit(Object *p_object);
  425. };
  426. class AnimationTrackKeyEdit;
  427. class AnimationMultiTrackKeyEdit;
  428. class AnimationBezierTrackEdit;
  429. class AnimationTrackEditGroup : public Control {
  430. GDCLASS(AnimationTrackEditGroup, Control);
  431. Ref<Texture2D> icon;
  432. Vector2 icon_size;
  433. String node_name;
  434. NodePath node;
  435. Node *root = nullptr;
  436. AnimationTimelineEdit *timeline = nullptr;
  437. AnimationTrackEditor *editor = nullptr;
  438. void _zoom_changed();
  439. protected:
  440. void _notification(int p_what);
  441. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  442. public:
  443. void set_type_and_name(const Ref<Texture2D> &p_type, const String &p_name, const NodePath &p_node);
  444. virtual Size2 get_minimum_size() const override;
  445. void set_timeline(AnimationTimelineEdit *p_timeline);
  446. void set_root(Node *p_root);
  447. void set_editor(AnimationTrackEditor *p_editor);
  448. String get_node_name() const;
  449. AnimationTrackEditGroup();
  450. };
  451. class AnimationTrackEditor : public VBoxContainer {
  452. GDCLASS(AnimationTrackEditor, VBoxContainer);
  453. friend class AnimationTimelineEdit;
  454. friend class AnimationBezierTrackEdit;
  455. friend class AnimationMarkerKeyEditEditor;
  456. Ref<Animation> animation;
  457. bool read_only = false;
  458. Node *root = nullptr;
  459. MenuButton *edit = nullptr;
  460. PanelContainer *main_panel = nullptr;
  461. HScrollBar *hscroll = nullptr;
  462. ScrollContainer *scroll = nullptr;
  463. VBoxContainer *track_vbox = nullptr;
  464. AnimationBezierTrackEdit *bezier_edit = nullptr;
  465. VBoxContainer *timeline_vbox = nullptr;
  466. Label *info_message = nullptr;
  467. AnimationTimelineEdit *timeline = nullptr;
  468. AnimationMarkerEdit *marker_edit = nullptr;
  469. HSlider *zoom = nullptr;
  470. EditorSpinSlider *step = nullptr;
  471. Button *fps_compat = nullptr;
  472. Label *nearest_fps_label = nullptr;
  473. TextureRect *zoom_icon = nullptr;
  474. Button *snap_keys = nullptr;
  475. Button *snap_timeline = nullptr;
  476. Button *bezier_edit_icon = nullptr;
  477. OptionButton *snap_mode = nullptr;
  478. Button *auto_fit = nullptr;
  479. Button *auto_fit_bezier = nullptr;
  480. OptionButton *bezier_key_mode = nullptr;
  481. Button *imported_anim_warning = nullptr;
  482. void _show_imported_anim_warning();
  483. Button *dummy_player_warning = nullptr;
  484. void _show_dummy_player_warning();
  485. Button *inactive_player_warning = nullptr;
  486. void _show_inactive_player_warning();
  487. void _snap_mode_changed(int p_mode);
  488. Vector<AnimationTrackEdit *> track_edits;
  489. Vector<AnimationTrackEditGroup *> groups;
  490. bool animation_changing_awaiting_update = false;
  491. void _animation_update(); // Updated by AnimationTrackEditor(this)
  492. int _get_track_selected();
  493. void _animation_changed();
  494. void _update_tracks();
  495. void _redraw_tracks();
  496. void _redraw_groups();
  497. void _check_bezier_exist();
  498. void _name_limit_changed();
  499. void _timeline_changed(float p_new_pos, bool p_timeline_only);
  500. void _track_remove_request(int p_track);
  501. void _animation_track_remove_request(int p_track, Ref<Animation> p_from_animation);
  502. void _track_grab_focus(int p_track);
  503. void _update_scroll(double);
  504. void _update_nearest_fps_label();
  505. void _update_fps_compat_mode(bool p_enabled);
  506. void _update_step(double p_new_step);
  507. void _update_length(double p_new_len);
  508. void _dropped_track(int p_from_track, int p_to_track);
  509. void _add_track(int p_type);
  510. void _new_track_node_selected(NodePath p_path);
  511. void _new_track_property_selected(const String &p_name);
  512. void _update_step_spinbox();
  513. PropertySelector *prop_selector = nullptr;
  514. PropertySelector *method_selector = nullptr;
  515. SceneTreeDialog *pick_track = nullptr;
  516. int adding_track_type = 0;
  517. NodePath adding_track_path;
  518. bool keying = false;
  519. struct InsertData {
  520. Animation::TrackType type;
  521. NodePath path;
  522. int track_idx = 0;
  523. float time = FLT_MAX; // Defaults to current timeline position.
  524. Variant value;
  525. String query;
  526. bool advance = false;
  527. };
  528. Label *insert_confirm_text = nullptr;
  529. CheckBox *insert_confirm_bezier = nullptr;
  530. CheckBox *insert_confirm_reset = nullptr;
  531. ConfirmationDialog *insert_confirm = nullptr;
  532. bool insert_queue = false;
  533. List<InsertData> insert_data;
  534. void _query_insert(const InsertData &p_id);
  535. Ref<Animation> _create_and_get_reset_animation();
  536. void _confirm_insert_list();
  537. struct TrackIndices {
  538. int normal;
  539. int reset;
  540. TrackIndices(const Animation *p_anim = nullptr, const Animation *p_reset_anim = nullptr) {
  541. normal = p_anim ? p_anim->get_track_count() : 0;
  542. reset = p_reset_anim ? p_reset_anim->get_track_count() : 0;
  543. }
  544. };
  545. TrackIndices _confirm_insert(InsertData p_id, TrackIndices p_next_tracks, bool p_reset_wanted, Ref<Animation> p_reset_anim, bool p_create_beziers);
  546. void _insert_track(bool p_reset_wanted, bool p_create_beziers);
  547. void _root_removed();
  548. PropertyInfo _find_hint_for_track(int p_idx, NodePath &r_base_path, Variant *r_current_val = nullptr);
  549. void _scroll_changed(const Vector2 &p_val);
  550. void _v_scroll_changed(float p_val);
  551. void _h_scroll_changed(float p_val);
  552. Ref<ViewPanner> panner;
  553. void _pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event);
  554. void _zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event);
  555. void _timeline_value_changed(double);
  556. float insert_key_from_track_call_ofs = 0.0f;
  557. int insert_key_from_track_call_track = 0;
  558. void _insert_key_from_track(float p_ofs, int p_track);
  559. void _add_method_key(const String &p_method);
  560. void _fetch_value_track_options(const NodePath &p_path, Animation::UpdateMode *r_update_mode, Animation::InterpolationType *r_interpolation_type, bool *r_loop_wrap);
  561. void _clear_selection_for_anim(const Ref<Animation> &p_anim);
  562. void _select_at_anim(const Ref<Animation> &p_anim, int p_track, float p_pos);
  563. //selection
  564. struct SelectedKey {
  565. int track = 0;
  566. int key = 0;
  567. bool operator<(const SelectedKey &p_key) const { return track == p_key.track ? key < p_key.key : track < p_key.track; }
  568. };
  569. struct KeyInfo {
  570. float pos = 0;
  571. };
  572. RBMap<SelectedKey, KeyInfo> selection;
  573. bool moving_selection = false;
  574. float moving_selection_offset = 0.0f;
  575. void _move_selection_begin();
  576. void _move_selection(float p_offset);
  577. void _move_selection_commit();
  578. void _move_selection_cancel();
  579. AnimationTrackKeyEdit *key_edit = nullptr;
  580. AnimationMultiTrackKeyEdit *multi_key_edit = nullptr;
  581. void _update_key_edit();
  582. void _clear_key_edit();
  583. Control *box_selection_container = nullptr;
  584. Control *box_selection = nullptr;
  585. void _box_selection_draw();
  586. bool box_selecting = false;
  587. Vector2 box_selecting_from;
  588. Vector2 box_selecting_to;
  589. Rect2 box_select_rect;
  590. Vector2 prev_scroll_position;
  591. void _scroll_input(const Ref<InputEvent> &p_event);
  592. Vector<Ref<AnimationTrackEditPlugin>> track_edit_plugins;
  593. void _toggle_bezier_edit();
  594. void _cancel_bezier_edit();
  595. void _bezier_edit(int p_for_track);
  596. void _bezier_track_set_key_handle_mode(Animation *p_anim, int p_track, int p_index, Animation::HandleMode p_mode, Animation::HandleSetMode p_set_mode = Animation::HANDLE_SET_MODE_NONE);
  597. void _bezier_track_set_key_handle_mode_at_time(Animation *p_anim, int p_track, float p_time, Animation::HandleMode p_mode, Animation::HandleSetMode p_set_mode = Animation::HANDLE_SET_MODE_NONE);
  598. ////////////// edit menu stuff
  599. ConfirmationDialog *bake_dialog = nullptr;
  600. CheckBox *bake_trs = nullptr;
  601. CheckBox *bake_blendshape = nullptr;
  602. CheckBox *bake_value = nullptr;
  603. SpinBox *bake_fps = nullptr;
  604. ConfirmationDialog *optimize_dialog = nullptr;
  605. SpinBox *optimize_velocity_error = nullptr;
  606. SpinBox *optimize_angular_error = nullptr;
  607. SpinBox *optimize_precision_error = nullptr;
  608. ConfirmationDialog *cleanup_dialog = nullptr;
  609. CheckBox *cleanup_keys_with_trimming_head = nullptr;
  610. CheckBox *cleanup_keys_with_trimming_end = nullptr;
  611. CheckBox *cleanup_keys = nullptr;
  612. CheckBox *cleanup_tracks = nullptr;
  613. CheckBox *cleanup_all = nullptr;
  614. ConfirmationDialog *scale_dialog = nullptr;
  615. SpinBox *scale = nullptr;
  616. ConfirmationDialog *ease_dialog = nullptr;
  617. OptionButton *transition_selection = nullptr;
  618. OptionButton *ease_selection = nullptr;
  619. SpinBox *ease_fps = nullptr;
  620. void _select_all_tracks_for_copy();
  621. void _edit_menu_about_to_popup();
  622. void _edit_menu_pressed(int p_option);
  623. bool scale_from_cursor = false;
  624. void _cleanup_animation(Ref<Animation> p_animation);
  625. void _anim_duplicate_keys(float p_ofs, bool p_ofs_valid, int p_track);
  626. void _anim_copy_keys(bool p_cut);
  627. bool _is_track_compatible(int p_target_track_idx, Variant::Type p_source_value_type, Animation::TrackType p_source_track_type);
  628. void _anim_paste_keys(float p_ofs, bool p_ofs_valid, int p_track);
  629. void _toggle_function_names();
  630. Button *function_name_toggler = nullptr;
  631. void _view_group_toggle();
  632. Button *view_group = nullptr;
  633. Button *selected_filter = nullptr;
  634. Button *alphabetic_sorting = nullptr;
  635. void _auto_fit();
  636. void _auto_fit_bezier();
  637. void _selection_changed();
  638. ConfirmationDialog *track_copy_dialog = nullptr;
  639. Tree *track_copy_select = nullptr;
  640. struct TrackClipboard {
  641. NodePath full_path;
  642. NodePath base_path;
  643. Animation::TrackType track_type = Animation::TYPE_ANIMATION;
  644. Animation::InterpolationType interp_type = Animation::INTERPOLATION_CUBIC_ANGLE;
  645. Animation::UpdateMode update_mode = Animation::UPDATE_CAPTURE;
  646. Animation::LoopMode loop_mode = Animation::LOOP_PINGPONG;
  647. bool loop_wrap = false;
  648. bool enabled = false;
  649. bool use_blend = false;
  650. struct Key {
  651. float time = 0;
  652. float transition = 0;
  653. Variant value;
  654. };
  655. Vector<Key> keys;
  656. };
  657. struct KeyClipboard {
  658. int top_track;
  659. struct Key {
  660. Animation::TrackType track_type;
  661. int track;
  662. float time = 0;
  663. float transition = 0;
  664. Variant value;
  665. };
  666. Vector<Key> keys;
  667. };
  668. Vector<TrackClipboard> track_clipboard;
  669. KeyClipboard key_clipboard;
  670. void _set_key_clipboard(int p_top_track, float p_top_time, RBMap<SelectedKey, KeyInfo> &p_keymap);
  671. void _insert_animation_key(NodePath p_path, const Variant &p_value);
  672. void _pick_track_filter_text_changed(const String &p_newtext);
  673. void _pick_track_select_recursive(TreeItem *p_item, const String &p_filter, Vector<Node *> &p_select_candidates);
  674. double snap_unit;
  675. bool fps_compatible = true;
  676. int nearest_fps = 0;
  677. void _update_snap_unit();
  678. protected:
  679. static void _bind_methods();
  680. void _notification(int p_what);
  681. public:
  682. // Public for use with callable_mp.
  683. void _clear_selection(bool p_update = false);
  684. void _key_selected(int p_key, bool p_single, int p_track);
  685. void _key_deselected(int p_key, int p_track);
  686. enum {
  687. EDIT_COPY_TRACKS,
  688. EDIT_COPY_TRACKS_CONFIRM,
  689. EDIT_PASTE_TRACKS,
  690. EDIT_CUT_KEYS,
  691. EDIT_COPY_KEYS,
  692. EDIT_PASTE_KEYS,
  693. EDIT_SCALE_SELECTION,
  694. EDIT_SCALE_FROM_CURSOR,
  695. EDIT_SCALE_CONFIRM,
  696. EDIT_SET_START_OFFSET,
  697. EDIT_SET_END_OFFSET,
  698. EDIT_EASE_SELECTION,
  699. EDIT_EASE_CONFIRM,
  700. EDIT_DUPLICATE_SELECTED_KEYS,
  701. EDIT_DUPLICATE_SELECTION,
  702. EDIT_DUPLICATE_TRANSPOSED,
  703. EDIT_MOVE_FIRST_SELECTED_KEY_TO_CURSOR,
  704. EDIT_MOVE_LAST_SELECTED_KEY_TO_CURSOR,
  705. EDIT_ADD_RESET_KEY,
  706. EDIT_DELETE_SELECTION,
  707. EDIT_GOTO_NEXT_STEP,
  708. EDIT_GOTO_NEXT_STEP_TIMELINE_ONLY, // Next step without updating animation.
  709. EDIT_GOTO_PREV_STEP,
  710. EDIT_APPLY_RESET,
  711. EDIT_BAKE_ANIMATION,
  712. EDIT_BAKE_ANIMATION_CONFIRM,
  713. EDIT_OPTIMIZE_ANIMATION,
  714. EDIT_OPTIMIZE_ANIMATION_CONFIRM,
  715. EDIT_CLEAN_UP_ANIMATION,
  716. EDIT_CLEAN_UP_ANIMATION_CONFIRM
  717. };
  718. void add_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin);
  719. void remove_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin);
  720. void set_animation(const Ref<Animation> &p_anim, bool p_read_only);
  721. Ref<Animation> get_current_animation() const;
  722. void set_root(Node *p_root);
  723. Node *get_root() const;
  724. void update_keying();
  725. bool has_keying() const;
  726. Dictionary get_state() const;
  727. void set_state(const Dictionary &p_state);
  728. void clear();
  729. void cleanup();
  730. void set_anim_pos(float p_pos);
  731. void insert_node_value_key(Node *p_node, const String &p_property, bool p_only_if_exists = false, bool p_advance = false);
  732. void insert_value_key(const String &p_property, bool p_advance);
  733. void insert_transform_key(Node3D *p_node, const String &p_sub, const Animation::TrackType p_type, const Variant &p_value);
  734. bool has_track(Node3D *p_node, const String &p_sub, const Animation::TrackType p_type);
  735. void make_insert_queue();
  736. void commit_insert_queue();
  737. void show_select_node_warning(bool p_show);
  738. void show_dummy_player_warning(bool p_show);
  739. void show_inactive_player_warning(bool p_show);
  740. bool is_key_selected(int p_track, int p_key) const;
  741. bool is_selection_active() const;
  742. bool is_key_clipboard_active() const;
  743. bool is_moving_selection() const;
  744. bool is_snap_timeline_enabled() const;
  745. bool is_snap_keys_enabled() const;
  746. bool is_bezier_editor_active() const;
  747. bool can_add_reset_key() const;
  748. void _on_filter_updated(const String &p_filter);
  749. float get_moving_selection_offset() const;
  750. float snap_time(float p_value, bool p_relative = false);
  751. float get_snap_unit();
  752. bool is_grouping_tracks();
  753. bool is_sorting_alphabetically();
  754. PackedStringArray get_selected_section() const;
  755. bool is_marker_selected(const StringName &p_marker) const;
  756. bool is_marker_moving_selection() const;
  757. float get_marker_moving_selection_offset() const;
  758. bool is_function_name_pressed();
  759. /** If `p_from_mouse_event` is `true`, handle Shift key presses for precise snapping. */
  760. void goto_prev_step(bool p_from_mouse_event);
  761. /** If `p_from_mouse_event` is `true`, handle Shift key presses for precise snapping. */
  762. void goto_next_step(bool p_from_mouse_event, bool p_timeline_only = false);
  763. MenuButton *get_edit_menu();
  764. AnimationTrackEditor();
  765. ~AnimationTrackEditor();
  766. };
  767. // AnimationTrackKeyEditEditorPlugin
  768. class AnimationTrackKeyEditEditor : public EditorProperty {
  769. GDCLASS(AnimationTrackKeyEditEditor, EditorProperty);
  770. Ref<Animation> animation;
  771. int track = -1;
  772. real_t key_ofs = 0.0;
  773. bool use_fps = false;
  774. EditorSpinSlider *spinner = nullptr;
  775. struct KeyDataCache {
  776. real_t time = 0.0;
  777. float transition = 0.0;
  778. Variant value;
  779. } key_data_cache;
  780. void _time_edit_spun();
  781. void _time_edit_entered();
  782. void _time_edit_exited();
  783. public:
  784. AnimationTrackKeyEditEditor(Ref<Animation> p_animation, int p_track, real_t p_key_ofs, bool p_use_fps);
  785. };
  786. // AnimationMarkerKeyEditEditorPlugin
  787. class AnimationMarkerKeyEditEditor : public EditorProperty {
  788. GDCLASS(AnimationMarkerKeyEditEditor, EditorProperty);
  789. Ref<Animation> animation;
  790. StringName marker_name;
  791. bool use_fps = false;
  792. EditorSpinSlider *spinner = nullptr;
  793. void _time_edit_exited();
  794. public:
  795. AnimationMarkerKeyEditEditor(Ref<Animation> p_animation, const StringName &p_name, bool p_use_fps);
  796. };