Skip to content

Commit e10974b

Browse files
committed
Merge pull request #104578 from KoBeWi/final_interpolasy
Fix final tween value with custom interpolator
2 parents a9316e7 + 8f65797 commit e10974b

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

scene/animation/tween.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,20 @@ Tween::Tween(SceneTree *p_parent_tree) {
542542
valid = true;
543543
}
544544

545+
double PropertyTweener::_get_custom_interpolated_value(const Variant &p_value) {
546+
const Variant *argptr = &p_value;
547+
548+
Variant result;
549+
Callable::CallError ce;
550+
custom_method.callp(&argptr, 1, result, ce);
551+
if (ce.error != Callable::CallError::CALL_OK) {
552+
ERR_FAIL_V_MSG(false, "Error calling custom method from PropertyTweener: " + Variant::get_callable_error_text(custom_method, &argptr, 1, ce) + ".");
553+
} else if (result.get_type() != Variant::FLOAT) {
554+
ERR_FAIL_V_MSG(false, vformat("Wrong return type in PropertyTweener custom method. Expected float, got %s.", Variant::get_type_name(result.get_type())));
555+
}
556+
return result;
557+
}
558+
545559
Ref<PropertyTweener> PropertyTweener::from(const Variant &p_value) {
546560
Ref<Tween> tween = _get_tween();
547561
ERR_FAIL_COND_V(tween.is_null(), nullptr);
@@ -638,25 +652,20 @@ bool PropertyTweener::step(double &r_delta) {
638652
if (time < duration) {
639653
if (custom_method.is_valid()) {
640654
const Variant t = tween->interpolate_variant(0.0, 1.0, time, duration, trans_type, ease_type);
641-
const Variant *argptr = &t;
642-
643-
Variant result;
644-
Callable::CallError ce;
645-
custom_method.callp(&argptr, 1, result, ce);
646-
if (ce.error != Callable::CallError::CALL_OK) {
647-
ERR_FAIL_V_MSG(false, "Error calling custom method from PropertyTweener: " + Variant::get_callable_error_text(custom_method, &argptr, 1, ce) + ".");
648-
} else if (result.get_type() != Variant::FLOAT) {
649-
ERR_FAIL_V_MSG(false, vformat("Wrong return type in PropertyTweener custom method. Expected float, got %s.", Variant::get_type_name(result.get_type())));
650-
}
651-
655+
double result = _get_custom_interpolated_value(t);
652656
target_instance->set_indexed(property, Animation::interpolate_variant(initial_val, final_val, result));
653657
} else {
654658
target_instance->set_indexed(property, tween->interpolate_variant(initial_val, delta_val, time, duration, trans_type, ease_type));
655659
}
656660
r_delta = 0;
657661
return true;
658662
} else {
659-
target_instance->set_indexed(property, final_val);
663+
if (custom_method.is_valid()) {
664+
double final_t = _get_custom_interpolated_value(1.0);
665+
target_instance->set_indexed(property, Animation::interpolate_variant(initial_val, final_val, final_t));
666+
} else {
667+
target_instance->set_indexed(property, final_val);
668+
}
660669
r_delta = elapsed_time - delay - duration;
661670
_finish();
662671
return false;

scene/animation/tween.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ VARIANT_ENUM_CAST(Tween::EaseType);
200200
class PropertyTweener : public Tweener {
201201
GDCLASS(PropertyTweener, Tweener);
202202

203+
double _get_custom_interpolated_value(const Variant &p_value);
204+
203205
public:
204206
Ref<PropertyTweener> from(const Variant &p_value);
205207
Ref<PropertyTweener> from_current();

0 commit comments

Comments
 (0)