animation_track_editor.h 31 KB

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