animation_track_editor.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*************************************************************************/
  2. /* animation_track_editor.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef ANIMATION_TRACK_EDITOR_H
  31. #define ANIMATION_TRACK_EDITOR_H
  32. #include "editor/editor_data.h"
  33. #include "editor/editor_spin_slider.h"
  34. #include "editor/property_editor.h"
  35. #include "editor/property_selector.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/slider.h"
  40. #include "scene/gui/spin_box.h"
  41. #include "scene/gui/tab_container.h"
  42. #include "scene/gui/texture_rect.h"
  43. #include "scene/resources/animation.h"
  44. #include "scene_tree_editor.h"
  45. class AnimationPlayer;
  46. class AnimationTrackEdit;
  47. class ViewPanner;
  48. class AnimationTimelineEdit : public Range {
  49. GDCLASS(AnimationTimelineEdit, Range);
  50. Ref<Animation> animation;
  51. AnimationTrackEdit *track_edit = nullptr;
  52. int name_limit;
  53. Range *zoom = nullptr;
  54. Range *h_scroll = nullptr;
  55. float play_position_pos;
  56. HBoxContainer *len_hb = nullptr;
  57. EditorSpinSlider *length = nullptr;
  58. Button *loop = nullptr;
  59. TextureRect *time_icon = nullptr;
  60. MenuButton *add_track = nullptr;
  61. Control *play_position = nullptr; //separate control used to draw so updates for only position changed are much faster
  62. HScrollBar *hscroll = nullptr;
  63. void _zoom_changed(double);
  64. void _anim_length_changed(double p_new_len);
  65. void _anim_loop_pressed();
  66. void _play_position_draw();
  67. UndoRedo *undo_redo = nullptr;
  68. Rect2 hsize_rect;
  69. bool editing = false;
  70. bool use_fps = false;
  71. Ref<ViewPanner> panner;
  72. void _scroll_callback(Vector2 p_scroll_vec, bool p_alt);
  73. void _pan_callback(Vector2 p_scroll_vec);
  74. void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt);
  75. bool dragging_timeline = false;
  76. bool dragging_hsize = false;
  77. float dragging_hsize_from;
  78. float dragging_hsize_at;
  79. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  80. void _track_added(int p_track);
  81. protected:
  82. static void _bind_methods();
  83. void _notification(int p_what);
  84. public:
  85. int get_name_limit() const;
  86. int get_buttons_width() const;
  87. float get_zoom_scale() const;
  88. virtual Size2 get_minimum_size() const override;
  89. void set_animation(const Ref<Animation> &p_animation);
  90. void set_track_edit(AnimationTrackEdit *p_track_edit);
  91. void set_zoom(Range *p_zoom);
  92. Range *get_zoom() const { return zoom; }
  93. void set_undo_redo(UndoRedo *p_undo_redo);
  94. void set_play_position(float p_pos);
  95. float get_play_position() const;
  96. void update_play_position();
  97. void update_values();
  98. void set_use_fps(bool p_use_fps);
  99. bool is_using_fps() const;
  100. void set_hscroll(HScrollBar *p_hscroll);
  101. virtual CursorShape get_cursor_shape(const Point2 &p_pos) const override;
  102. AnimationTimelineEdit();
  103. };
  104. class AnimationTrackEditor;
  105. class AnimationTrackEdit : public Control {
  106. GDCLASS(AnimationTrackEdit, Control);
  107. enum {
  108. MENU_CALL_MODE_CONTINUOUS,
  109. MENU_CALL_MODE_DISCRETE,
  110. MENU_CALL_MODE_TRIGGER,
  111. MENU_CALL_MODE_CAPTURE,
  112. MENU_INTERPOLATION_NEAREST,
  113. MENU_INTERPOLATION_LINEAR,
  114. MENU_INTERPOLATION_CUBIC,
  115. MENU_LOOP_WRAP,
  116. MENU_LOOP_CLAMP,
  117. MENU_KEY_INSERT,
  118. MENU_KEY_DUPLICATE,
  119. MENU_KEY_ADD_RESET,
  120. MENU_KEY_DELETE
  121. };
  122. AnimationTimelineEdit *timeline = nullptr;
  123. UndoRedo *undo_redo = nullptr;
  124. Popup *path_popup = nullptr;
  125. LineEdit *path = nullptr;
  126. Node *root = nullptr;
  127. Control *play_position = nullptr; //separate control used to draw so updates for only position changed are much faster
  128. float play_position_pos;
  129. NodePath node_path;
  130. Ref<Animation> animation;
  131. int track;
  132. Rect2 check_rect;
  133. Rect2 path_rect;
  134. Rect2 update_mode_rect;
  135. Rect2 interp_mode_rect;
  136. Rect2 loop_wrap_rect;
  137. Rect2 remove_rect;
  138. Ref<Texture2D> type_icon;
  139. Ref<Texture2D> selected_icon;
  140. PopupMenu *menu = nullptr;
  141. bool clicking_on_name = false;
  142. void _zoom_changed();
  143. Ref<Texture2D> icon_cache;
  144. String path_cache;
  145. void _menu_selected(int p_index);
  146. void _path_submitted(const String &p_text);
  147. void _play_position_draw();
  148. bool _is_value_key_valid(const Variant &p_key_value, Variant::Type &r_valid_type) const;
  149. Ref<Texture2D> _get_key_type_icon() const;
  150. mutable int dropping_at;
  151. float insert_at_pos;
  152. bool moving_selection_attempt = false;
  153. int select_single_attempt;
  154. bool moving_selection = false;
  155. float moving_selection_from_ofs;
  156. bool in_group = false;
  157. AnimationTrackEditor *editor = nullptr;
  158. protected:
  159. static void _bind_methods();
  160. void _notification(int p_what);
  161. virtual void gui_input(const Ref<InputEvent> &p_event) override;
  162. public:
  163. virtual Variant get_drag_data(const Point2 &p_point) override;
  164. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const override;
  165. virtual void drop_data(const Point2 &p_point, const Variant &p_data) override;
  166. virtual String get_tooltip(const Point2 &p_pos) const override;
  167. virtual int get_key_height() const;
  168. virtual Rect2 get_key_rect(int p_index, float p_pixels_sec);
  169. virtual bool is_key_selectable_by_distance() const;
  170. 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);
  171. 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);
  172. virtual void draw_bg(int p_clip_left, int p_clip_right);
  173. virtual void draw_fg(int p_clip_left, int p_clip_right);
  174. //helper
  175. void draw_texture_region_clipped(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_region);
  176. void draw_rect_clipped(const Rect2 &p_rect, const Color &p_color, bool p_filled = true);
  177. int get_track() const;
  178. Ref<Animation> get_animation() const;
  179. AnimationTimelineEdit *get_timeline() const { return timeline; }
  180. AnimationTrackEditor *get_editor() const { return editor; }
  181. UndoRedo *get_undo_redo() const { return undo_redo; }
  182. NodePath get_path() const;
  183. void set_animation_and_track(const Ref<Animation> &p_animation, int p_track);
  184. virtual Size2 get_minimum_size() const override;
  185. void set_undo_redo(UndoRedo *p_undo_redo);
  186. void set_timeline(AnimationTimelineEdit *p_timeline);
  187. void set_editor(AnimationTrackEditor *p_editor);
  188. void set_root(Node *p_root);
  189. void set_play_position(float p_pos);
  190. void update_play_position();
  191. void cancel_drop();
  192. void set_in_group(bool p_enable);
  193. void append_to_selection(const Rect2 &p_box, bool p_deselection);
  194. AnimationTrackEdit();
  195. };
  196. class AnimationTrackEditPlugin : public RefCounted {
  197. GDCLASS(AnimationTrackEditPlugin, RefCounted);
  198. public:
  199. 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);
  200. virtual AnimationTrackEdit *create_audio_track_edit();
  201. virtual AnimationTrackEdit *create_animation_track_edit(Object *p_object);
  202. };
  203. class AnimationTrackKeyEdit;
  204. class AnimationMultiTrackKeyEdit;
  205. class AnimationBezierTrackEdit;
  206. class AnimationTrackEditGroup : public Control {
  207. GDCLASS(AnimationTrackEditGroup, Control);
  208. Ref<Texture2D> icon;
  209. String node_name;
  210. NodePath node;
  211. Node *root = nullptr;
  212. AnimationTimelineEdit *timeline = nullptr;
  213. void _zoom_changed();
  214. protected:
  215. static void _bind_methods();
  216. void _notification(int p_what);
  217. public:
  218. void set_type_and_name(const Ref<Texture2D> &p_type, const String &p_name, const NodePath &p_node);
  219. virtual Size2 get_minimum_size() const override;
  220. void set_timeline(AnimationTimelineEdit *p_timeline);
  221. void set_root(Node *p_root);
  222. AnimationTrackEditGroup();
  223. };
  224. class AnimationTrackEditor : public VBoxContainer {
  225. GDCLASS(AnimationTrackEditor, VBoxContainer);
  226. Ref<Animation> animation;
  227. Node *root = nullptr;
  228. MenuButton *edit = nullptr;
  229. PanelContainer *main_panel = nullptr;
  230. HScrollBar *hscroll = nullptr;
  231. ScrollContainer *scroll = nullptr;
  232. VBoxContainer *track_vbox = nullptr;
  233. AnimationBezierTrackEdit *bezier_edit = nullptr;
  234. Label *info_message = nullptr;
  235. AnimationTimelineEdit *timeline = nullptr;
  236. HSlider *zoom = nullptr;
  237. EditorSpinSlider *step = nullptr;
  238. TextureRect *zoom_icon = nullptr;
  239. Button *snap = nullptr;
  240. Button *bezier_edit_icon = nullptr;
  241. OptionButton *snap_mode = nullptr;
  242. Button *imported_anim_warning = nullptr;
  243. void _show_imported_anim_warning();
  244. void _snap_mode_changed(int p_mode);
  245. Vector<AnimationTrackEdit *> track_edits;
  246. Vector<AnimationTrackEditGroup *> groups;
  247. bool animation_changing_awaiting_update = false;
  248. void _animation_update();
  249. int _get_track_selected();
  250. void _animation_changed();
  251. void _update_tracks();
  252. void _name_limit_changed();
  253. void _timeline_changed(float p_new_pos, bool p_drag, bool p_timeline_only);
  254. void _track_remove_request(int p_track);
  255. void _track_grab_focus(int p_track);
  256. UndoRedo *undo_redo = nullptr;
  257. void _update_scroll(double);
  258. void _update_step(double p_new_step);
  259. void _update_length(double p_new_len);
  260. void _dropped_track(int p_from_track, int p_to_track);
  261. void _add_track(int p_type);
  262. void _new_track_node_selected(NodePath p_path);
  263. void _new_track_property_selected(String p_name);
  264. void _update_step_spinbox();
  265. PropertySelector *prop_selector = nullptr;
  266. PropertySelector *method_selector = nullptr;
  267. SceneTreeDialog *pick_track = nullptr;
  268. int adding_track_type;
  269. NodePath adding_track_path;
  270. bool keying = false;
  271. struct InsertData {
  272. Animation::TrackType type;
  273. NodePath path;
  274. int track_idx = 0;
  275. Variant value;
  276. String query;
  277. bool advance = false;
  278. }; /* insert_data;*/
  279. Label *insert_confirm_text = nullptr;
  280. CheckBox *insert_confirm_bezier = nullptr;
  281. CheckBox *insert_confirm_reset = nullptr;
  282. ConfirmationDialog *insert_confirm = nullptr;
  283. bool insert_queue = false;
  284. List<InsertData> insert_data;
  285. void _query_insert(const InsertData &p_id);
  286. Ref<Animation> _create_and_get_reset_animation();
  287. void _confirm_insert_list();
  288. struct TrackIndices {
  289. int normal;
  290. int reset;
  291. TrackIndices(const Animation *p_anim = nullptr, const Animation *p_reset_anim = nullptr) {
  292. normal = p_anim ? p_anim->get_track_count() : 0;
  293. reset = p_reset_anim ? p_reset_anim->get_track_count() : 0;
  294. }
  295. };
  296. TrackIndices _confirm_insert(InsertData p_id, TrackIndices p_next_tracks, bool p_create_reset, Ref<Animation> p_reset_anim, bool p_create_beziers);
  297. void _insert_track(bool p_create_reset, bool p_create_beziers);
  298. void _root_removed(Node *p_root);
  299. PropertyInfo _find_hint_for_track(int p_idx, NodePath &r_base_path, Variant *r_current_val = nullptr);
  300. Ref<ViewPanner> panner;
  301. void _scroll_callback(Vector2 p_scroll_vec, bool p_alt);
  302. void _pan_callback(Vector2 p_scroll_vec);
  303. void _zoom_callback(Vector2 p_scroll_vec, Vector2 p_origin, bool p_alt);
  304. void _timeline_value_changed(double);
  305. float insert_key_from_track_call_ofs;
  306. int insert_key_from_track_call_track;
  307. void _insert_key_from_track(float p_ofs, int p_track);
  308. void _add_method_key(const String &p_method);
  309. void _clear_selection(bool p_update = false);
  310. void _clear_selection_for_anim(const Ref<Animation> &p_anim);
  311. void _select_at_anim(const Ref<Animation> &p_anim, int p_track, float p_pos);
  312. //selection
  313. struct SelectedKey {
  314. int track = 0;
  315. int key = 0;
  316. bool operator<(const SelectedKey &p_key) const { return track == p_key.track ? key < p_key.key : track < p_key.track; };
  317. };
  318. struct KeyInfo {
  319. float pos = 0;
  320. };
  321. Map<SelectedKey, KeyInfo> selection;
  322. void _key_selected(int p_key, bool p_single, int p_track);
  323. void _key_deselected(int p_key, int p_track);
  324. bool moving_selection = false;
  325. float moving_selection_offset;
  326. void _move_selection_begin();
  327. void _move_selection(float p_offset);
  328. void _move_selection_commit();
  329. void _move_selection_cancel();
  330. AnimationTrackKeyEdit *key_edit = nullptr;
  331. AnimationMultiTrackKeyEdit *multi_key_edit = nullptr;
  332. void _update_key_edit();
  333. void _clear_key_edit();
  334. Control *box_selection = nullptr;
  335. void _box_selection_draw();
  336. bool box_selecting = false;
  337. Vector2 box_selecting_from;
  338. Rect2 box_select_rect;
  339. void _scroll_input(const Ref<InputEvent> &p_event);
  340. Vector<Ref<AnimationTrackEditPlugin>> track_edit_plugins;
  341. void _toggle_bezier_edit();
  342. void _cancel_bezier_edit();
  343. void _bezier_edit(int p_for_track);
  344. ////////////// edit menu stuff
  345. ConfirmationDialog *optimize_dialog = nullptr;
  346. SpinBox *optimize_linear_error = nullptr;
  347. SpinBox *optimize_angular_error = nullptr;
  348. SpinBox *optimize_max_angle = nullptr;
  349. ConfirmationDialog *cleanup_dialog = nullptr;
  350. CheckBox *cleanup_keys = nullptr;
  351. CheckBox *cleanup_tracks = nullptr;
  352. CheckBox *cleanup_all = nullptr;
  353. ConfirmationDialog *scale_dialog = nullptr;
  354. SpinBox *scale = nullptr;
  355. void _select_all_tracks_for_copy();
  356. void _edit_menu_about_to_popup();
  357. void _edit_menu_pressed(int p_option);
  358. int last_menu_track_opt;
  359. void _cleanup_animation(Ref<Animation> p_animation);
  360. void _anim_duplicate_keys(bool transpose);
  361. void _view_group_toggle();
  362. Button *view_group = nullptr;
  363. Button *selected_filter = nullptr;
  364. void _selection_changed();
  365. ConfirmationDialog *track_copy_dialog = nullptr;
  366. Tree *track_copy_select = nullptr;
  367. struct TrackClipboard {
  368. NodePath full_path;
  369. NodePath base_path;
  370. Animation::TrackType track_type = Animation::TrackType::TYPE_ANIMATION;
  371. Animation::InterpolationType interp_type = Animation::InterpolationType::INTERPOLATION_CUBIC;
  372. Animation::UpdateMode update_mode = Animation::UpdateMode::UPDATE_CAPTURE;
  373. Animation::LoopMode loop_mode = Animation::LoopMode::LOOP_LINEAR;
  374. bool loop_wrap = false;
  375. bool enabled = false;
  376. struct Key {
  377. float time = 0;
  378. float transition = 0;
  379. Variant value;
  380. };
  381. Vector<Key> keys;
  382. };
  383. Vector<TrackClipboard> track_clipboard;
  384. void _insert_animation_key(NodePath p_path, const Variant &p_value);
  385. void _pick_track_filter_text_changed(const String &p_newtext);
  386. void _pick_track_select_recursive(TreeItem *p_item, const String &p_filter, Vector<Node *> &p_select_candidates);
  387. void _pick_track_filter_input(const Ref<InputEvent> &p_ie);
  388. protected:
  389. static void _bind_methods();
  390. void _notification(int p_what);
  391. public:
  392. enum {
  393. EDIT_COPY_TRACKS,
  394. EDIT_COPY_TRACKS_CONFIRM,
  395. EDIT_PASTE_TRACKS,
  396. EDIT_SCALE_SELECTION,
  397. EDIT_SCALE_FROM_CURSOR,
  398. EDIT_SCALE_CONFIRM,
  399. EDIT_DUPLICATE_SELECTION,
  400. EDIT_DUPLICATE_TRANSPOSED,
  401. EDIT_ADD_RESET_KEY,
  402. EDIT_DELETE_SELECTION,
  403. EDIT_GOTO_NEXT_STEP,
  404. EDIT_GOTO_PREV_STEP,
  405. EDIT_APPLY_RESET,
  406. EDIT_OPTIMIZE_ANIMATION,
  407. EDIT_OPTIMIZE_ANIMATION_CONFIRM,
  408. EDIT_CLEAN_UP_ANIMATION,
  409. EDIT_CLEAN_UP_ANIMATION_CONFIRM
  410. };
  411. void add_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin);
  412. void remove_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin);
  413. void set_animation(const Ref<Animation> &p_anim);
  414. Ref<Animation> get_current_animation() const;
  415. void set_root(Node *p_root);
  416. Node *get_root() const;
  417. void update_keying();
  418. bool has_keying() const;
  419. Dictionary get_state() const;
  420. void set_state(const Dictionary &p_state);
  421. void cleanup();
  422. void set_anim_pos(float p_pos);
  423. void insert_node_value_key(Node *p_node, const String &p_property, const Variant &p_value, bool p_only_if_exists = false);
  424. void insert_value_key(const String &p_property, const Variant &p_value, bool p_advance);
  425. void insert_transform_key(Node3D *p_node, const String &p_sub, const Animation::TrackType p_type, const Variant p_value);
  426. bool has_track(Node3D *p_node, const String &p_sub, const Animation::TrackType p_type);
  427. void make_insert_queue();
  428. void commit_insert_queue();
  429. void show_select_node_warning(bool p_show);
  430. bool is_key_selected(int p_track, int p_key) const;
  431. bool is_selection_active() const;
  432. bool is_moving_selection() const;
  433. bool is_snap_enabled() const;
  434. float get_moving_selection_offset() const;
  435. float snap_time(float p_value, bool p_relative = false);
  436. bool is_grouping_tracks();
  437. /** If `p_from_mouse_event` is `true`, handle Shift key presses for precise snapping. */
  438. void goto_prev_step(bool p_from_mouse_event);
  439. /** If `p_from_mouse_event` is `true`, handle Shift key presses for precise snapping. */
  440. void goto_next_step(bool p_from_mouse_event);
  441. MenuButton *get_edit_menu();
  442. AnimationTrackEditor();
  443. ~AnimationTrackEditor();
  444. };
  445. #endif // ANIMATION_TRACK_EDITOR_H