animation_track_editor.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #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/animation/animation_cache.h"
  37. #include "scene/gui/control.h"
  38. #include "scene/gui/file_dialog.h"
  39. #include "scene/gui/menu_button.h"
  40. #include "scene/gui/scroll_bar.h"
  41. #include "scene/gui/slider.h"
  42. #include "scene/gui/spin_box.h"
  43. #include "scene/gui/tab_container.h"
  44. #include "scene/gui/texture_rect.h"
  45. #include "scene/gui/tool_button.h"
  46. #include "scene/resources/animation.h"
  47. #include "scene_tree_editor.h"
  48. class AnimationTrackEdit;
  49. class AnimationTimelineEdit : public Range {
  50. GDCLASS(AnimationTimelineEdit, Range);
  51. Ref<Animation> animation;
  52. AnimationTrackEdit *track_edit;
  53. int name_limit;
  54. Range *zoom;
  55. Range *h_scroll;
  56. float play_position_pos;
  57. HBoxContainer *len_hb;
  58. EditorSpinSlider *length;
  59. ToolButton *loop;
  60. TextureRect *time_icon;
  61. MenuButton *add_track;
  62. Control *play_position; //separate control used to draw so updates for only position changed are much faster
  63. HScrollBar *hscroll;
  64. void _zoom_changed(double);
  65. void _anim_length_changed(double p_new_len);
  66. void _anim_loop_pressed();
  67. void _play_position_draw();
  68. UndoRedo *undo_redo;
  69. Rect2 hsize_rect;
  70. bool editing;
  71. bool use_fps;
  72. bool panning_timeline;
  73. float panning_timeline_from;
  74. float panning_timeline_at;
  75. bool dragging_timeline;
  76. bool dragging_hsize;
  77. float dragging_hsize_from;
  78. float dragging_hsize_at;
  79. void _gui_input(const Ref<InputEvent> &p_event);
  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;
  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. AnimationTimelineEdit();
  102. };
  103. class AnimationTrackEditor;
  104. class AnimationTrackEdit : public Control {
  105. GDCLASS(AnimationTrackEdit, Control);
  106. enum {
  107. MENU_CALL_MODE_CONTINUOUS,
  108. MENU_CALL_MODE_DISCRETE,
  109. MENU_CALL_MODE_TRIGGER,
  110. MENU_CALL_MODE_CAPTURE,
  111. MENU_INTERPOLATION_NEAREST,
  112. MENU_INTERPOLATION_LINEAR,
  113. MENU_INTERPOLATION_CUBIC,
  114. MENU_LOOP_WRAP,
  115. MENU_LOOP_CLAMP,
  116. MENU_KEY_INSERT,
  117. MENU_KEY_DUPLICATE,
  118. MENU_KEY_DELETE
  119. };
  120. AnimationTimelineEdit *timeline;
  121. UndoRedo *undo_redo;
  122. LineEdit *path;
  123. Node *root;
  124. Control *play_position; //separate control used to draw so updates for only position changed are much faster
  125. float play_position_pos;
  126. NodePath node_path;
  127. Ref<Animation> animation;
  128. int track;
  129. Rect2 check_rect;
  130. Rect2 path_rect;
  131. Rect2 update_mode_rect;
  132. Rect2 interp_mode_rect;
  133. Rect2 loop_mode_rect;
  134. Rect2 remove_rect;
  135. Rect2 bezier_edit_rect;
  136. Ref<Texture> type_icon;
  137. Ref<Texture> selected_icon;
  138. PopupMenu *menu;
  139. bool clicking_on_name;
  140. void _zoom_changed();
  141. Ref<Texture> icon_cache;
  142. String path_cache;
  143. void _menu_selected(int p_index);
  144. void _path_entered(const String &p_text);
  145. void _play_position_draw();
  146. bool _is_value_key_valid(const Variant &p_key_value, Variant::Type &r_valid_type) const;
  147. mutable int dropping_at;
  148. float insert_at_pos;
  149. bool moving_selection_attempt;
  150. int select_single_attempt;
  151. bool moving_selection;
  152. float moving_selection_from_ofs;
  153. bool in_group;
  154. AnimationTrackEditor *editor;
  155. protected:
  156. static void _bind_methods();
  157. void _notification(int p_what);
  158. virtual void _gui_input(const Ref<InputEvent> &p_event);
  159. public:
  160. virtual Variant get_drag_data(const Point2 &p_point);
  161. virtual bool can_drop_data(const Point2 &p_point, const Variant &p_data) const;
  162. virtual void drop_data(const Point2 &p_point, const Variant &p_data);
  163. virtual String get_tooltip(const Point2 &p_pos) const;
  164. virtual int get_key_height() const;
  165. virtual Rect2 get_key_rect(int p_index, float p_pixels_sec);
  166. virtual bool is_key_selectable_by_distance() const;
  167. 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);
  168. 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);
  169. virtual void draw_bg(int p_clip_left, int p_clip_right);
  170. virtual void draw_fg(int p_clip_left, int p_clip_right);
  171. //helper
  172. void draw_texture_clipped(const Ref<Texture> &p_texture, const Vector2 &p_pos);
  173. void draw_texture_region_clipped(const Ref<Texture> &p_texture, const Rect2 &p_rect, const Rect2 &p_region);
  174. void draw_rect_clipped(const Rect2 &p_rect, const Color &p_color, bool p_filled = true);
  175. int get_track() const;
  176. Ref<Animation> get_animation() const;
  177. AnimationTimelineEdit *get_timeline() const { return timeline; }
  178. AnimationTrackEditor *get_editor() const { return editor; }
  179. UndoRedo *get_undo_redo() const { return undo_redo; }
  180. NodePath get_path() const;
  181. void set_animation_and_track(const Ref<Animation> &p_animation, int p_track);
  182. virtual Size2 get_minimum_size() const;
  183. void set_undo_redo(UndoRedo *p_undo_redo);
  184. void set_timeline(AnimationTimelineEdit *p_timeline);
  185. void set_editor(AnimationTrackEditor *p_editor);
  186. void set_root(Node *p_root);
  187. void set_play_position(float p_pos);
  188. void update_play_position();
  189. void cancel_drop();
  190. void set_in_group(bool p_enable);
  191. void append_to_selection(const Rect2 &p_box, bool p_deselection);
  192. AnimationTrackEdit();
  193. };
  194. class AnimationTrackEditPlugin : public Reference {
  195. GDCLASS(AnimationTrackEditPlugin, Reference);
  196. public:
  197. 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);
  198. virtual AnimationTrackEdit *create_audio_track_edit();
  199. virtual AnimationTrackEdit *create_animation_track_edit(Object *p_object);
  200. };
  201. class AnimationTrackKeyEdit;
  202. class AnimationMultiTrackKeyEdit;
  203. class AnimationBezierTrackEdit;
  204. class AnimationTrackEditGroup : public Control {
  205. GDCLASS(AnimationTrackEditGroup, Control);
  206. Ref<Texture> icon;
  207. String node_name;
  208. NodePath node;
  209. Node *root;
  210. AnimationTimelineEdit *timeline;
  211. void _zoom_changed();
  212. protected:
  213. static void _bind_methods();
  214. void _notification(int p_what);
  215. public:
  216. void set_type_and_name(const Ref<Texture> &p_type, const String &p_name, const NodePath &p_node);
  217. virtual Size2 get_minimum_size() const;
  218. void set_timeline(AnimationTimelineEdit *p_timeline);
  219. void set_root(Node *p_root);
  220. AnimationTrackEditGroup();
  221. };
  222. class AnimationTrackEditor : public VBoxContainer {
  223. GDCLASS(AnimationTrackEditor, VBoxContainer);
  224. Ref<Animation> animation;
  225. Node *root;
  226. MenuButton *edit;
  227. PanelContainer *main_panel;
  228. HScrollBar *hscroll;
  229. ScrollContainer *scroll;
  230. VBoxContainer *track_vbox;
  231. AnimationBezierTrackEdit *bezier_edit;
  232. Label *info_message;
  233. AnimationTimelineEdit *timeline;
  234. HSlider *zoom;
  235. EditorSpinSlider *step;
  236. TextureRect *zoom_icon;
  237. ToolButton *snap;
  238. OptionButton *snap_mode;
  239. Button *imported_anim_warning;
  240. void _show_imported_anim_warning() const;
  241. void _snap_mode_changed(int p_mode);
  242. Vector<AnimationTrackEdit *> track_edits;
  243. Vector<AnimationTrackEditGroup *> groups;
  244. bool animation_changing_awaiting_update;
  245. void _animation_update();
  246. int _get_track_selected();
  247. void _animation_changed();
  248. void _update_tracks();
  249. void _name_limit_changed();
  250. void _timeline_changed(float p_new_pos, bool p_drag);
  251. void _track_remove_request(int p_track);
  252. void _track_grab_focus(int p_track);
  253. UndoRedo *undo_redo;
  254. void _update_scroll(double);
  255. void _update_step(double p_new_step);
  256. void _update_length(double p_new_len);
  257. void _dropped_track(int p_from_track, int p_to_track);
  258. void _add_track(int p_type);
  259. void _new_track_node_selected(NodePath p_path);
  260. void _new_track_property_selected(String p_name);
  261. void _update_step_spinbox();
  262. PropertySelector *prop_selector;
  263. PropertySelector *method_selector;
  264. SceneTreeDialog *pick_track;
  265. int adding_track_type;
  266. NodePath adding_track_path;
  267. bool keying;
  268. struct InsertData {
  269. Animation::TrackType type;
  270. NodePath path;
  271. int track_idx;
  272. Variant value;
  273. String query;
  274. bool advance;
  275. }; /* insert_data;*/
  276. Label *insert_confirm_text;
  277. CheckBox *insert_confirm_bezier;
  278. CheckBox *insert_confirm_reset;
  279. ConfirmationDialog *insert_confirm;
  280. bool insert_queue;
  281. bool inserting;
  282. bool insert_query;
  283. List<InsertData> insert_data;
  284. uint64_t insert_frame;
  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_delay(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. void _timeline_value_changed(double);
  301. float insert_key_from_track_call_ofs;
  302. int insert_key_from_track_call_track;
  303. void _insert_key_from_track(float p_ofs, int p_track);
  304. void _add_method_key(const String &p_method);
  305. void _clear_selection(bool p_update = false);
  306. void _clear_selection_for_anim(const Ref<Animation> &p_anim);
  307. void _select_at_anim(const Ref<Animation> &p_anim, int p_track, float p_pos);
  308. //selection
  309. struct SelectedKey {
  310. int track;
  311. int key;
  312. bool operator<(const SelectedKey &p_key) const { return track == p_key.track ? key < p_key.key : track < p_key.track; };
  313. };
  314. struct KeyInfo {
  315. float pos;
  316. };
  317. Map<SelectedKey, KeyInfo> selection;
  318. void _key_selected(int p_key, bool p_single, int p_track);
  319. void _key_deselected(int p_key, int p_track);
  320. bool moving_selection;
  321. float moving_selection_offset;
  322. void _move_selection_begin();
  323. void _move_selection(float p_offset);
  324. void _move_selection_commit();
  325. void _move_selection_cancel();
  326. AnimationTrackKeyEdit *key_edit;
  327. AnimationMultiTrackKeyEdit *multi_key_edit;
  328. void _update_key_edit();
  329. void _clear_key_edit();
  330. Control *box_selection;
  331. void _box_selection_draw();
  332. bool box_selecting;
  333. Vector2 box_selecting_from;
  334. Rect2 box_select_rect;
  335. void _scroll_input(const Ref<InputEvent> &p_event);
  336. Vector<Ref<AnimationTrackEditPlugin>> track_edit_plugins;
  337. void _cancel_bezier_edit();
  338. void _bezier_edit(int p_for_track);
  339. ////////////// edit menu stuff
  340. ConfirmationDialog *optimize_dialog;
  341. SpinBox *optimize_linear_error;
  342. SpinBox *optimize_angular_error;
  343. SpinBox *optimize_max_angle;
  344. ConfirmationDialog *cleanup_dialog;
  345. CheckBox *cleanup_keys;
  346. CheckBox *cleanup_tracks;
  347. CheckBox *cleanup_all;
  348. ConfirmationDialog *scale_dialog;
  349. SpinBox *scale;
  350. void _select_all_tracks_for_copy();
  351. void _edit_menu_about_to_show();
  352. void _edit_menu_pressed(int p_option);
  353. int last_menu_track_opt;
  354. void _cleanup_animation(Ref<Animation> p_animation);
  355. void _anim_duplicate_keys(bool transpose);
  356. void _view_group_toggle();
  357. ToolButton *view_group;
  358. ToolButton *selected_filter;
  359. void _selection_changed();
  360. ConfirmationDialog *track_copy_dialog;
  361. Tree *track_copy_select;
  362. struct TrackClipboard {
  363. NodePath full_path;
  364. NodePath base_path;
  365. Animation::TrackType track_type;
  366. Animation::InterpolationType interp_type;
  367. Animation::UpdateMode update_mode;
  368. bool loop_wrap;
  369. bool enabled;
  370. struct Key {
  371. float time;
  372. float transition;
  373. Variant value;
  374. };
  375. Vector<Key> keys;
  376. };
  377. Vector<TrackClipboard> track_clipboard;
  378. void _insert_animation_key(NodePath p_path, const Variant &p_value);
  379. void _pick_track_filter_text_changed(const String &p_text);
  380. void _pick_track_select_recursive(TreeItem *p_item, const String &p_filter, Vector<Node *> &p_select_candidates);
  381. void _pick_track_filter_input(const Ref<InputEvent> &p_ie);
  382. protected:
  383. static void _bind_methods();
  384. void _notification(int p_what);
  385. public:
  386. enum {
  387. EDIT_COPY_TRACKS,
  388. EDIT_COPY_TRACKS_CONFIRM,
  389. EDIT_PASTE_TRACKS,
  390. EDIT_SCALE_SELECTION,
  391. EDIT_SCALE_FROM_CURSOR,
  392. EDIT_SCALE_CONFIRM,
  393. EDIT_DUPLICATE_SELECTION,
  394. EDIT_DUPLICATE_TRANSPOSED,
  395. EDIT_DELETE_SELECTION,
  396. EDIT_GOTO_NEXT_STEP,
  397. EDIT_GOTO_PREV_STEP,
  398. EDIT_APPLY_RESET,
  399. EDIT_OPTIMIZE_ANIMATION,
  400. EDIT_OPTIMIZE_ANIMATION_CONFIRM,
  401. EDIT_CLEAN_UP_ANIMATION,
  402. EDIT_CLEAN_UP_ANIMATION_CONFIRM
  403. };
  404. void add_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin);
  405. void remove_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin);
  406. void set_animation(const Ref<Animation> &p_anim);
  407. Ref<Animation> get_current_animation() const;
  408. void set_root(Node *p_root);
  409. Node *get_root() const;
  410. void update_keying();
  411. bool has_keying() const;
  412. Dictionary get_state() const;
  413. void set_state(const Dictionary &p_state);
  414. void cleanup();
  415. void set_anim_pos(float p_pos);
  416. void insert_node_value_key(Node *p_node, const String &p_property, const Variant &p_value, bool p_only_if_exists = false);
  417. void insert_value_key(const String &p_property, const Variant &p_value, bool p_advance);
  418. void insert_transform_key(Spatial *p_node, const String &p_sub, const Transform &p_xform);
  419. void show_select_node_warning(bool p_show);
  420. bool is_key_selected(int p_track, int p_key) const;
  421. bool is_selection_active() const;
  422. bool is_moving_selection() const;
  423. bool is_snap_enabled() const;
  424. float get_moving_selection_offset() const;
  425. float snap_time(float p_value, bool p_relative = false);
  426. bool is_grouping_tracks();
  427. /** If `p_from_mouse_event` is `true`, handle Shift key presses for precise snapping. */
  428. void goto_prev_step(bool p_from_mouse_event);
  429. /** If `p_from_mouse_event` is `true`, handle Shift key presses for precise snapping. */
  430. void goto_next_step(bool p_from_mouse_event);
  431. MenuButton *get_edit_menu();
  432. AnimationTrackEditor();
  433. ~AnimationTrackEditor();
  434. };
  435. #endif // ANIMATION_TRACK_EDITOR_H