animation_track_editor.h 15 KB

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