Skip to content

Commit

Permalink
Merge pull request #85254 from TokageItLab/bind-reset-animedit
Browse files Browse the repository at this point in the history
Bind `_reset`/`_restore` in AnimationMixer
  • Loading branch information
akien-mga committed Nov 23, 2023
2 parents 1524972 + f66224d commit bb63963
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
52 changes: 27 additions & 25 deletions scene/animation/animation_mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,6 @@ bool AnimationMixer::is_reset_on_save_enabled() const {
return reset_on_save;
}

#ifdef TOOLS_ENABLED
bool AnimationMixer::can_apply_reset() const {
return has_animation(SceneStringNames::get_singleton()->RESET);
}
Expand Down Expand Up @@ -1929,7 +1928,6 @@ void AnimationMixer::_build_backup_track_cache() {
if (asp) {
t->object->call(SNAME("set_stream"), Ref<AudioStream>());
}
track = memnew(TrackCache); // Make disable this track cache.
} break;
default: {
} // The rest don't matter.
Expand Down Expand Up @@ -1959,29 +1957,6 @@ Ref<AnimatedValuesBackup> AnimationMixer::make_backup() {
return backup;
}

Ref<AnimatedValuesBackup> AnimationMixer::apply_reset(bool p_user_initiated) {
if (!p_user_initiated && dummy) {
return Ref<AnimatedValuesBackup>();
}
ERR_FAIL_COND_V(!can_apply_reset(), Ref<AnimatedValuesBackup>());

Ref<Animation> reset_anim = animation_set[SceneStringNames::get_singleton()->RESET].animation;
ERR_FAIL_COND_V(reset_anim.is_null(), Ref<AnimatedValuesBackup>());

Ref<AnimatedValuesBackup> backup_current = make_backup();
if (p_user_initiated) {
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
ur->create_action(TTR("Animation Apply Reset"));
ur->add_do_method(this, "_reset");
ur->add_undo_method(this, "_restore", backup_current);
ur->commit_action();
} else {
reset();
}

return backup_current;
}

void AnimationMixer::reset() {
ERR_FAIL_COND(!can_apply_reset());

Expand Down Expand Up @@ -2010,6 +1985,30 @@ void AnimationMixer::restore(const Ref<AnimatedValuesBackup> &p_backup) {
track_cache = HashMap<NodePath, AnimationMixer::TrackCache *>();
cache_valid = false;
}

#ifdef TOOLS_ENABLED
Ref<AnimatedValuesBackup> AnimationMixer::apply_reset(bool p_user_initiated) {
if (!p_user_initiated && dummy) {
return Ref<AnimatedValuesBackup>();
}
ERR_FAIL_COND_V(!can_apply_reset(), Ref<AnimatedValuesBackup>());

Ref<Animation> reset_anim = animation_set[SceneStringNames::get_singleton()->RESET].animation;
ERR_FAIL_COND_V(reset_anim.is_null(), Ref<AnimatedValuesBackup>());

Ref<AnimatedValuesBackup> backup_current = make_backup();
if (p_user_initiated) {
EditorUndoRedoManager *ur = EditorUndoRedoManager::get_singleton();
ur->create_action(TTR("Animation Apply Reset"));
ur->add_do_method(this, "_reset");
ur->add_undo_method(this, "_restore", backup_current);
ur->commit_action();
} else {
reset();
}

return backup_current;
}
#endif // TOOLS_ENABLED

/* -------------------------------------------- */
Expand Down Expand Up @@ -2128,6 +2127,9 @@ void AnimationMixer::_bind_methods() {
ADD_SIGNAL(MethodInfo(SNAME("animation_finished"), PropertyInfo(Variant::STRING_NAME, "anim_name")));
ADD_SIGNAL(MethodInfo(SNAME("animation_started"), PropertyInfo(Variant::STRING_NAME, "anim_name")));
ADD_SIGNAL(MethodInfo(SNAME("caches_cleared")));

ClassDB::bind_method(D_METHOD("_reset"), &AnimationMixer::reset);
ClassDB::bind_method(D_METHOD("_restore", "backup"), &AnimationMixer::restore);
}

AnimationMixer::AnimationMixer() {
Expand Down
22 changes: 10 additions & 12 deletions scene/animation/animation_mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@
#include "scene/resources/animation_library.h"
#include "scene/resources/audio_stream_polyphonic.h"

#ifdef TOOLS_ENABLED
class AnimatedValuesBackup;
#endif // TOOLS_ENABLED

class AnimationMixer : public Node {
GDCLASS(AnimationMixer, Node);
#ifdef TOOLS_ENABLED
friend AnimatedValuesBackup;
#ifdef TOOLS_ENABLED
bool editing = false;
bool dummy = false;
#endif // TOOLS_ENABLED
Expand Down Expand Up @@ -364,25 +362,26 @@ class AnimationMixer : public Node {
void set_reset_on_save_enabled(bool p_enabled);
bool is_reset_on_save_enabled() const;

bool can_apply_reset() const;
void _build_backup_track_cache();
Ref<AnimatedValuesBackup> make_backup();
void restore(const Ref<AnimatedValuesBackup> &p_backup);
void reset();

#ifdef TOOLS_ENABLED
Ref<AnimatedValuesBackup> apply_reset(bool p_user_initiated = false);

void set_editing(bool p_editing);
bool is_editing() const;

void set_dummy(bool p_dummy);
bool is_dummy() const;

bool can_apply_reset() const;
void _build_backup_track_cache();
Ref<AnimatedValuesBackup> make_backup();
Ref<AnimatedValuesBackup> apply_reset(bool p_user_initiated = false);
void restore(const Ref<AnimatedValuesBackup> &p_backup);
void reset();
#endif // TOOLS_ENABLED

AnimationMixer();
~AnimationMixer();
};

#ifdef TOOLS_ENABLED
class AnimatedValuesBackup : public RefCounted {
GDCLASS(AnimatedValuesBackup, RefCounted);

Expand All @@ -398,7 +397,6 @@ class AnimatedValuesBackup : public RefCounted {
}
}
};
#endif

VARIANT_ENUM_CAST(AnimationMixer::AnimationCallbackModeProcess);
VARIANT_ENUM_CAST(AnimationMixer::AnimationCallbackModeMethod);
Expand Down

0 comments on commit bb63963

Please sign in to comment.