diff options
author | kobewi <kobewi4e@gmail.com> | 2023-09-25 15:56:04 +0200 |
---|---|---|
committer | kobewi <kobewi4e@gmail.com> | 2023-10-12 16:41:51 +0200 |
commit | 58f4984d607d89a07f1e44cdce99b17424864b4b (patch) | |
tree | ffa131d4dd28f73fcc8358f59baec63f8b9f4446 /scene/animation/tween.cpp | |
parent | b1371806ad3907c009458ea939bd4b810f9deb21 (diff) | |
download | redot-engine-58f4984d607d89a07f1e44cdce99b17424864b4b.tar.gz |
Add set_custom_interpolator() to PropertyTweener
Diffstat (limited to 'scene/animation/tween.cpp')
-rw-r--r-- | scene/animation/tween.cpp | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp index c778129eb6..8db6be7d12 100644 --- a/scene/animation/tween.cpp +++ b/scene/animation/tween.cpp @@ -531,6 +531,11 @@ Ref<PropertyTweener> PropertyTweener::set_ease(Tween::EaseType p_ease) { return this; } +Ref<PropertyTweener> PropertyTweener::set_custom_interpolator(const Callable &p_method) { + custom_method = p_method; + return this; +} + Ref<PropertyTweener> PropertyTweener::set_delay(double p_delay) { delay = p_delay; return this; @@ -581,7 +586,23 @@ bool PropertyTweener::step(double &r_delta) { double time = MIN(elapsed_time - delay, duration); if (time < duration) { - target_instance->set_indexed(property, tween->interpolate_variant(initial_val, delta_val, time, duration, trans_type, ease_type)); + if (custom_method.is_valid()) { + const Variant t = tween->interpolate_variant(0.0, 1.0, time, duration, trans_type, ease_type); + const Variant *argptr = &t; + + Variant result; + Callable::CallError ce; + custom_method.callp(&argptr, 1, result, ce); + if (ce.error != Callable::CallError::CALL_OK) { + ERR_FAIL_V_MSG(false, "Error calling custom method from PropertyTweener: " + Variant::get_callable_error_text(custom_method, &argptr, 1, ce) + "."); + } else if (result.get_type() != Variant::FLOAT) { + ERR_FAIL_V_MSG(false, vformat("Wrong return type in PropertyTweener custom method. Expected float, got %s.", Variant::get_type_name(result.get_type()))); + } + + target_instance->set_indexed(property, Animation::interpolate_variant(initial_val, final_val, result)); + } else { + target_instance->set_indexed(property, tween->interpolate_variant(initial_val, delta_val, time, duration, trans_type, ease_type)); + } r_delta = 0; return true; } else { @@ -609,6 +630,7 @@ void PropertyTweener::_bind_methods() { ClassDB::bind_method(D_METHOD("as_relative"), &PropertyTweener::as_relative); ClassDB::bind_method(D_METHOD("set_trans", "trans"), &PropertyTweener::set_trans); ClassDB::bind_method(D_METHOD("set_ease", "ease"), &PropertyTweener::set_ease); + ClassDB::bind_method(D_METHOD("set_custom_interpolator", "interpolator_method"), &PropertyTweener::set_custom_interpolator); ClassDB::bind_method(D_METHOD("set_delay", "delay"), &PropertyTweener::set_delay); } @@ -685,7 +707,7 @@ bool CallbackTweener::step(double &r_delta) { Callable::CallError ce; callback.callp(nullptr, 0, result, ce); if (ce.error != Callable::CallError::CALL_OK) { - ERR_FAIL_V_MSG(false, "Error calling method from CallbackTweener: " + Variant::get_callable_error_text(callback, nullptr, 0, ce)); + ERR_FAIL_V_MSG(false, "Error calling method from CallbackTweener: " + Variant::get_callable_error_text(callback, nullptr, 0, ce) + "."); } finished = true; @@ -765,7 +787,7 @@ bool MethodTweener::step(double &r_delta) { Callable::CallError ce; callback.callp(argptr, 1, result, ce); if (ce.error != Callable::CallError::CALL_OK) { - ERR_FAIL_V_MSG(false, "Error calling method from MethodTweener: " + Variant::get_callable_error_text(callback, argptr, 1, ce)); + ERR_FAIL_V_MSG(false, "Error calling method from MethodTweener: " + Variant::get_callable_error_text(callback, argptr, 1, ce) + "."); } if (time < duration) { |