animation_track_editor.h 31 KB

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