diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2020-12-20 16:35:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-20 16:35:53 +0100 |
commit | bc2ac0bbd685a01569ba6dde4c68adc1ccf37bb8 (patch) | |
tree | 09fe174ea963c2745f8b268454acb5a39dab7de9 /scene | |
parent | e9d12f9e4ea30a6c3f2cf179f104a153d668c2a3 (diff) | |
parent | b7367ac426659e02facc7c828e57aa687cce957c (diff) |
Merge pull request #43115 from RandomShaper/reset_track
Add animation reset track feature
Diffstat (limited to 'scene')
-rw-r--r-- | scene/animation/animation_player.cpp | 84 | ||||
-rw-r--r-- | scene/animation/animation_player.h | 19 | ||||
-rw-r--r-- | scene/resources/animation.cpp | 2 | ||||
-rw-r--r-- | scene/resources/animation.h | 2 |
4 files changed, 85 insertions, 22 deletions
diff --git a/scene/animation/animation_player.cpp b/scene/animation/animation_player.cpp index e9e17148d6..159ccae130 100644 --- a/scene/animation/animation_player.cpp +++ b/scene/animation/animation_player.cpp @@ -36,6 +36,7 @@ #include "servers/audio/audio_stream.h" #ifdef TOOLS_ENABLED +#include "editor/editor_node.h" #include "editor/editor_settings.h" #include "scene/2d/skeleton_2d.h" @@ -53,6 +54,21 @@ void AnimatedValuesBackup::update_skeletons() { } } } + +void AnimatedValuesBackup::restore() const { + for (int i = 0; i < entries.size(); i++) { + const AnimatedValuesBackup::Entry *entry = &entries[i]; + if (entry->bone_idx == -1) { + entry->object->set_indexed(entry->subpath, entry->value); + } else { + Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose(entry->bone_idx, entry->value); + } + } +} + +void AnimatedValuesBackup::_bind_methods() { + ClassDB::bind_method(D_METHOD("restore"), &AnimatedValuesBackup::restore); +} #endif bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) { @@ -1379,6 +1395,14 @@ String AnimationPlayer::get_autoplay() const { return autoplay; } +void AnimationPlayer::set_reset_on_save_enabled(bool p_enabled) { + reset_on_save = p_enabled; +} + +bool AnimationPlayer::is_reset_on_save_enabled() const { + return reset_on_save; +} + void AnimationPlayer::set_animation_process_mode(AnimationProcessMode p_mode) { if (animation_process_mode == p_mode) { return; @@ -1473,15 +1497,15 @@ void AnimationPlayer::get_argument_options(const StringName &p_function, int p_i } #ifdef TOOLS_ENABLED -AnimatedValuesBackup AnimationPlayer::backup_animated_values() { +Ref<AnimatedValuesBackup> AnimationPlayer::backup_animated_values() { + Ref<AnimatedValuesBackup> backup; if (!playback.current.from) { - return AnimatedValuesBackup(); + return backup; } _ensure_node_caches(playback.current.from); - AnimatedValuesBackup backup; - + backup.instance(); for (int i = 0; i < playback.current.from->node_cache.size(); i++) { TrackNodeCache *nc = playback.current.from->node_cache[i]; if (!nc) { @@ -1497,7 +1521,7 @@ AnimatedValuesBackup AnimationPlayer::backup_animated_values() { entry.object = nc->skeleton; entry.bone_idx = nc->bone_idx; entry.value = nc->skeleton->get_bone_pose(nc->bone_idx); - backup.entries.push_back(entry); + backup->entries.push_back(entry); } else { if (nc->spatial) { AnimatedValuesBackup::Entry entry; @@ -1505,7 +1529,7 @@ AnimatedValuesBackup AnimationPlayer::backup_animated_values() { entry.subpath.push_back("transform"); entry.value = nc->spatial->get_transform(); entry.bone_idx = -1; - backup.entries.push_back(entry); + backup->entries.push_back(entry); } else { for (Map<StringName, TrackNodeCache::PropertyAnim>::Element *E = nc->property_anim.front(); E; E = E->next()) { AnimatedValuesBackup::Entry entry; @@ -1515,7 +1539,7 @@ AnimatedValuesBackup AnimationPlayer::backup_animated_values() { entry.value = E->value().object->get_indexed(E->value().subpath, &valid); entry.bone_idx = -1; if (valid) { - backup.entries.push_back(entry); + backup->entries.push_back(entry); } } } @@ -1525,15 +1549,40 @@ AnimatedValuesBackup AnimationPlayer::backup_animated_values() { return backup; } -void AnimationPlayer::restore_animated_values(const AnimatedValuesBackup &p_backup) { - for (int i = 0; i < p_backup.entries.size(); i++) { - const AnimatedValuesBackup::Entry *entry = &p_backup.entries[i]; - if (entry->bone_idx == -1) { - entry->object->set_indexed(entry->subpath, entry->value); - } else { - Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose(entry->bone_idx, entry->value); - } +Ref<AnimatedValuesBackup> AnimationPlayer::apply_reset(bool p_user_initiated) { + ERR_FAIL_COND_V(!can_apply_reset(), Ref<AnimatedValuesBackup>()); + + Ref<Animation> reset_anim = animation_set["RESET"].animation; + ERR_FAIL_COND_V(reset_anim.is_null(), Ref<AnimatedValuesBackup>()); + + Node *root_node = get_node_or_null(root); + ERR_FAIL_COND_V(!root_node, Ref<AnimatedValuesBackup>()); + + AnimationPlayer *aux_player = memnew(AnimationPlayer); + EditorNode::get_singleton()->add_child(aux_player); + aux_player->set_root(aux_player->get_path_to(root_node)); + aux_player->add_animation("RESET", reset_anim); + aux_player->set_assigned_animation("RESET"); + Ref<AnimatedValuesBackup> old_values = aux_player->backup_animated_values(); + aux_player->seek(0.0f, true); + aux_player->queue_delete(); + + if (p_user_initiated) { + Ref<AnimatedValuesBackup> new_values = aux_player->backup_animated_values(); + old_values->restore(); + + UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo(); + ur->create_action(TTR("Anim Apply Reset")); + ur->add_do_method(new_values.ptr(), "restore"); + ur->add_undo_method(old_values.ptr(), "restore"); + ur->commit_action(); } + + return old_values; +} + +bool AnimationPlayer::can_apply_reset() const { + return has_animation("RESET") && playback.assigned != StringName("RESET"); } #endif @@ -1577,6 +1626,9 @@ void AnimationPlayer::_bind_methods() { ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::set_autoplay); ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimationPlayer::get_autoplay); + ClassDB::bind_method(D_METHOD("set_reset_on_save_enabled", "enabled"), &AnimationPlayer::set_reset_on_save_enabled); + ClassDB::bind_method(D_METHOD("is_reset_on_save_enabled"), &AnimationPlayer::is_reset_on_save_enabled); + ClassDB::bind_method(D_METHOD("set_root", "path"), &AnimationPlayer::set_root); ClassDB::bind_method(D_METHOD("get_root"), &AnimationPlayer::get_root); @@ -1600,6 +1652,7 @@ void AnimationPlayer::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ANIMATE_AS_TRIGGER), "set_current_animation", "get_current_animation"); ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "assigned_animation", PROPERTY_HINT_NONE, "", 0), "set_assigned_animation", "get_assigned_animation"); ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_autoplay", "get_autoplay"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_on_save", PROPERTY_HINT_NONE, ""), "set_reset_on_save_enabled", "is_reset_on_save_enabled"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_length", PROPERTY_HINT_NONE, "", 0), "", "get_current_animation_length"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_position", PROPERTY_HINT_NONE, "", 0), "", "get_current_animation_position"); @@ -1631,6 +1684,7 @@ AnimationPlayer::AnimationPlayer() { speed_scale = 1; end_reached = false; end_notify = false; + reset_on_save = true; animation_process_mode = ANIMATION_PROCESS_IDLE; method_call_mode = ANIMATION_METHOD_CALL_DEFERRED; processing = false; diff --git a/scene/animation/animation_player.h b/scene/animation/animation_player.h index dbce5643c7..7f0d5630e1 100644 --- a/scene/animation/animation_player.h +++ b/scene/animation/animation_player.h @@ -37,8 +37,9 @@ #include "scene/resources/animation.h" #ifdef TOOLS_ENABLED -// To save/restore animated values -class AnimatedValuesBackup { +class AnimatedValuesBackup : public Reference { + GDCLASS(AnimatedValuesBackup, Reference); + struct Entry { Object *object; Vector<StringName> subpath; // Unused if bone @@ -49,8 +50,12 @@ class AnimatedValuesBackup { friend class AnimationPlayer; +protected: + static void _bind_methods(); + public: void update_skeletons(); + void restore() const; }; #endif @@ -215,6 +220,7 @@ private: bool end_notify; String autoplay; + bool reset_on_save; AnimationProcessMode animation_process_mode; AnimationMethodCallMode method_call_mode; bool processing; @@ -304,6 +310,9 @@ public: void set_autoplay(const String &p_name); String get_autoplay() const; + void set_reset_on_save_enabled(bool p_enabled); + bool is_reset_on_save_enabled() const; + void set_animation_process_mode(AnimationProcessMode p_mode); AnimationProcessMode get_animation_process_mode() const; @@ -325,9 +334,9 @@ public: void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override; #ifdef TOOLS_ENABLED - // These may be interesting for games, but are too dangerous for general use - AnimatedValuesBackup backup_animated_values(); - void restore_animated_values(const AnimatedValuesBackup &p_backup); + Ref<AnimatedValuesBackup> backup_animated_values(); + Ref<AnimatedValuesBackup> apply_reset(bool p_user_initiated = false); + bool can_apply_reset() const; #endif AnimationPlayer(); diff --git a/scene/resources/animation.cpp b/scene/resources/animation.cpp index a7a02361a9..f4670ca850 100644 --- a/scene/resources/animation.cpp +++ b/scene/resources/animation.cpp @@ -33,8 +33,6 @@ #include "core/math/geometry_3d.h" -#define ANIM_MIN_LENGTH 0.001 - bool Animation::_set(const StringName &p_name, const Variant &p_value) { String name = p_name; diff --git a/scene/resources/animation.h b/scene/resources/animation.h index 060d1fe2d9..650a54ebfc 100644 --- a/scene/resources/animation.h +++ b/scene/resources/animation.h @@ -33,6 +33,8 @@ #include "core/io/resource.h" +#define ANIM_MIN_LENGTH 0.001 + class Animation : public Resource { GDCLASS(Animation, Resource); RES_BASE_EXTENSION("anim"); |