animation_player_editor_plugin.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**************************************************************************/
  2. /* animation_player_editor_plugin.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/animation/animation_library_editor.h"
  32. #include "editor/animation/animation_track_editor.h"
  33. #include "editor/plugins/editor_plugin.h"
  34. #include "scene/animation/animation_player.h"
  35. #include "scene/gui/dialogs.h"
  36. #include "scene/gui/slider.h"
  37. #include "scene/gui/spin_box.h"
  38. #include "scene/gui/texture_button.h"
  39. #include "scene/gui/tree.h"
  40. class AnimationPlayerEditorPlugin;
  41. class ImageTexture;
  42. class AnimationPlayerEditor : public VBoxContainer {
  43. GDCLASS(AnimationPlayerEditor, VBoxContainer);
  44. friend AnimationPlayerEditorPlugin;
  45. AnimationPlayerEditorPlugin *plugin = nullptr;
  46. AnimationMixer *original_node = nullptr; // For pinned mark in SceneTree.
  47. AnimationPlayer *player = nullptr; // For AnimationPlayerEditor, could be dummy.
  48. ObjectID cached_root_node_id;
  49. bool is_dummy = false;
  50. enum {
  51. TOOL_NEW_ANIM,
  52. TOOL_ANIM_LIBRARY,
  53. TOOL_DUPLICATE_ANIM,
  54. TOOL_RENAME_ANIM,
  55. TOOL_EDIT_TRANSITIONS,
  56. TOOL_REMOVE_ANIM,
  57. TOOL_EDIT_RESOURCE
  58. };
  59. enum {
  60. ONION_SKINNING_ENABLE,
  61. ONION_SKINNING_PAST,
  62. ONION_SKINNING_FUTURE,
  63. ONION_SKINNING_1_STEP,
  64. ONION_SKINNING_2_STEPS,
  65. ONION_SKINNING_3_STEPS,
  66. ONION_SKINNING_LAST_STEPS_OPTION = ONION_SKINNING_3_STEPS,
  67. ONION_SKINNING_DIFFERENCES_ONLY,
  68. ONION_SKINNING_FORCE_WHITE_MODULATE,
  69. ONION_SKINNING_INCLUDE_GIZMOS,
  70. };
  71. enum {
  72. ANIM_OPEN,
  73. ANIM_SAVE,
  74. ANIM_SAVE_AS
  75. };
  76. enum {
  77. RESOURCE_LOAD,
  78. RESOURCE_SAVE
  79. };
  80. OptionButton *animation = nullptr;
  81. Button *stop = nullptr;
  82. Button *play = nullptr;
  83. Button *play_from = nullptr;
  84. Button *play_bw = nullptr;
  85. Button *play_bw_from = nullptr;
  86. Button *autoplay = nullptr;
  87. MenuButton *tool_anim = nullptr;
  88. Button *onion_toggle = nullptr;
  89. MenuButton *onion_skinning = nullptr;
  90. Button *pin = nullptr;
  91. SpinBox *frame = nullptr;
  92. LineEdit *scale = nullptr;
  93. LineEdit *name = nullptr;
  94. OptionButton *library = nullptr;
  95. Label *name_title = nullptr;
  96. Ref<Texture2D> stop_icon;
  97. Ref<Texture2D> pause_icon;
  98. Ref<Texture2D> autoplay_icon;
  99. Ref<Texture2D> reset_icon;
  100. Ref<ImageTexture> autoplay_reset_icon;
  101. bool finishing = false;
  102. bool last_active = false;
  103. float timeline_position = 0;
  104. EditorFileDialog *file = nullptr;
  105. ConfirmationDialog *delete_dialog = nullptr;
  106. AnimationLibraryEditor *library_editor = nullptr;
  107. struct BlendEditor {
  108. AcceptDialog *dialog = nullptr;
  109. Tree *tree = nullptr;
  110. OptionButton *next = nullptr;
  111. } blend_editor;
  112. ConfirmationDialog *name_dialog = nullptr;
  113. AcceptDialog *error_dialog = nullptr;
  114. int name_dialog_op = TOOL_NEW_ANIM;
  115. bool updating = false;
  116. bool updating_blends = false;
  117. AnimationTrackEditor *track_editor = nullptr;
  118. static AnimationPlayerEditor *singleton;
  119. // Onion skinning.
  120. struct {
  121. // Settings.
  122. bool enabled = false;
  123. bool past = true;
  124. bool future = false;
  125. uint32_t steps = 1;
  126. bool differences_only = false;
  127. bool force_white_modulate = false;
  128. bool include_gizmos = false;
  129. uint32_t get_capture_count() const {
  130. // 'Differences only' needs a capture of the present.
  131. return (past && future ? 2 * steps : steps) + (differences_only ? 1 : 0);
  132. }
  133. // Rendering.
  134. int64_t last_frame = 0;
  135. int can_overlay = 0;
  136. Size2 capture_size;
  137. LocalVector<RID> captures;
  138. LocalVector<bool> captures_valid;
  139. struct {
  140. RID canvas;
  141. RID canvas_item;
  142. Ref<ShaderMaterial> material;
  143. Ref<Shader> shader;
  144. } capture;
  145. // Cross-call state.
  146. struct {
  147. double anim_player_position = 0.0;
  148. Ref<AnimatedValuesBackup> anim_values_backup;
  149. Rect2 screen_rect;
  150. Dictionary canvas_edit_state;
  151. Dictionary spatial_edit_state;
  152. } temp;
  153. } onion;
  154. void _select_anim_by_name(const String &p_anim);
  155. float _get_editor_step() const;
  156. void _go_to_nearest_keyframe(bool p_backward);
  157. void _play_pressed();
  158. void _play_from_pressed();
  159. void _play_bw_pressed();
  160. void _play_bw_from_pressed();
  161. void _autoplay_pressed();
  162. void _stop_pressed();
  163. void _animation_selected(int p_which);
  164. void _animation_new();
  165. void _animation_rename();
  166. void _animation_name_edited();
  167. void _animation_remove();
  168. void _animation_remove_confirmed();
  169. void _animation_edit();
  170. void _animation_duplicate();
  171. Ref<Animation> _animation_clone(const Ref<Animation> p_anim);
  172. void _animation_resource_edit();
  173. void _scale_changed(const String &p_scale);
  174. void _seek_value_changed(float p_value, bool p_timeline_only = false);
  175. void _blend_editor_next_changed(const int p_idx);
  176. void _edit_animation_blend();
  177. void _update_animation_blend();
  178. void _list_changed();
  179. void _animation_finished(const String &p_name);
  180. void _current_animation_changed(const String &p_name);
  181. void _update_animation();
  182. void _update_player();
  183. void _set_controls_disabled(bool p_disabled);
  184. void _update_animation_list_icons();
  185. void _update_name_dialog_library_dropdown();
  186. void _blend_edited();
  187. void _animation_player_changed(Object *p_pl);
  188. void _animation_libraries_updated();
  189. void _animation_key_editor_seek(float p_pos, bool p_timeline_only = false, bool p_update_position_only = false);
  190. void _animation_key_editor_anim_len_changed(float p_len);
  191. void _animation_update_key_frame();
  192. virtual void shortcut_input(const Ref<InputEvent> &p_ev) override;
  193. void _animation_tool_menu(int p_option);
  194. void _onion_skinning_menu(int p_option);
  195. void _editor_visibility_changed();
  196. bool _are_onion_layers_valid();
  197. void _allocate_onion_layers();
  198. void _free_onion_layers();
  199. void _prepare_onion_layers_1();
  200. void _prepare_onion_layers_2_prolog();
  201. void _prepare_onion_layers_2_step_prepare(int p_step_offset, uint32_t p_capture_idx);
  202. void _prepare_onion_layers_2_step_capture(int p_step_offset, uint32_t p_capture_idx);
  203. void _prepare_onion_layers_2_epilog();
  204. void _start_onion_skinning();
  205. void _stop_onion_skinning();
  206. bool _validate_tracks(const Ref<Animation> p_anim);
  207. void _pin_pressed();
  208. String _get_current() const;
  209. void _ensure_dummy_player();
  210. ~AnimationPlayerEditor();
  211. protected:
  212. void _notification(int p_what);
  213. void _node_removed(Node *p_node);
  214. static void _bind_methods();
  215. public:
  216. AnimationMixer *get_editing_node() const;
  217. AnimationPlayer *get_player() const;
  218. AnimationMixer *fetch_mixer_for_library() const;
  219. Node *get_cached_root_node() const;
  220. static AnimationPlayerEditor *get_singleton() { return singleton; }
  221. bool is_pinned() const { return pin->is_pressed(); }
  222. void unpin() {
  223. pin->set_pressed(false);
  224. _pin_pressed();
  225. }
  226. AnimationTrackEditor *get_track_editor() { return track_editor; }
  227. Dictionary get_state() const;
  228. void set_state(const Dictionary &p_state);
  229. void clear();
  230. void ensure_visibility();
  231. void edit(AnimationMixer *p_node, AnimationPlayer *p_player, bool p_is_dummy);
  232. void forward_force_draw_over_viewport(Control *p_overlay);
  233. AnimationPlayerEditor(AnimationPlayerEditorPlugin *p_plugin);
  234. };
  235. class AnimationPlayerEditorPlugin : public EditorPlugin {
  236. GDCLASS(AnimationPlayerEditorPlugin, EditorPlugin);
  237. friend AnimationPlayerEditor;
  238. AnimationPlayerEditor *anim_editor = nullptr;
  239. AnimationPlayer *player = nullptr;
  240. AnimationPlayer *dummy_player = nullptr;
  241. ObjectID last_mixer;
  242. void _update_dummy_player(AnimationMixer *p_mixer);
  243. void _clear_dummy_player();
  244. protected:
  245. void _notification(int p_what);
  246. void _property_keyed(const String &p_keyed, const Variant &p_value, bool p_advance);
  247. void _transform_key_request(Object *sp, const String &p_sub, const Transform3D &p_key);
  248. void _update_keying();
  249. public:
  250. virtual Dictionary get_state() const override { return anim_editor->get_state(); }
  251. virtual void set_state(const Dictionary &p_state) override { anim_editor->set_state(p_state); }
  252. virtual void clear() override { anim_editor->clear(); }
  253. virtual String get_plugin_name() const override { return "Anim"; }
  254. bool has_main_screen() const override { return false; }
  255. virtual void edit(Object *p_object) override;
  256. virtual bool handles(Object *p_object) const override;
  257. virtual void make_visible(bool p_visible) override;
  258. virtual void forward_canvas_force_draw_over_viewport(Control *p_overlay) override { anim_editor->forward_force_draw_over_viewport(p_overlay); }
  259. virtual void forward_3d_force_draw_over_viewport(Control *p_overlay) override { anim_editor->forward_force_draw_over_viewport(p_overlay); }
  260. AnimationPlayerEditorPlugin();
  261. ~AnimationPlayerEditorPlugin();
  262. };
  263. // AnimationTrackKeyEditEditorPlugin
  264. class EditorInspectorPluginAnimationTrackKeyEdit : public EditorInspectorPlugin {
  265. GDCLASS(EditorInspectorPluginAnimationTrackKeyEdit, EditorInspectorPlugin);
  266. AnimationTrackKeyEditEditor *atk_editor = nullptr;
  267. public:
  268. virtual bool can_handle(Object *p_object) override;
  269. virtual void parse_begin(Object *p_object) override;
  270. };
  271. class AnimationTrackKeyEditEditorPlugin : public EditorPlugin {
  272. GDCLASS(AnimationTrackKeyEditEditorPlugin, EditorPlugin);
  273. EditorInspectorPluginAnimationTrackKeyEdit *atk_plugin = nullptr;
  274. public:
  275. bool has_main_screen() const override { return false; }
  276. virtual bool handles(Object *p_object) const override;
  277. virtual String get_plugin_name() const override { return "AnimationTrackKeyEdit"; }
  278. AnimationTrackKeyEditEditorPlugin();
  279. };
  280. // AnimationMarkerKeyEditEditorPlugin
  281. class EditorInspectorPluginAnimationMarkerKeyEdit : public EditorInspectorPlugin {
  282. GDCLASS(EditorInspectorPluginAnimationMarkerKeyEdit, EditorInspectorPlugin);
  283. AnimationMarkerKeyEditEditor *amk_editor = nullptr;
  284. public:
  285. virtual bool can_handle(Object *p_object) override;
  286. virtual void parse_begin(Object *p_object) override;
  287. };
  288. class AnimationMarkerKeyEditEditorPlugin : public EditorPlugin {
  289. GDCLASS(AnimationMarkerKeyEditEditorPlugin, EditorPlugin);
  290. EditorInspectorPluginAnimationMarkerKeyEdit *amk_plugin = nullptr;
  291. public:
  292. bool has_main_screen() const override { return false; }
  293. virtual bool handles(Object *p_object) const override;
  294. virtual String get_plugin_name() const override { return "AnimationMarkerKeyEdit"; }
  295. AnimationMarkerKeyEditEditorPlugin();
  296. };