diff options
Diffstat (limited to 'scene/animation/tween.cpp')
-rw-r--r-- | scene/animation/tween.cpp | 2113 |
1 files changed, 593 insertions, 1520 deletions
diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp index b4e597f75e..7bf616e602 100644 --- a/scene/animation/tween.cpp +++ b/scene/animation/tween.cpp @@ -30,535 +30,407 @@ #include "tween.h" -void Tween::_add_pending_command(StringName p_key, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5, const Variant &p_arg6, const Variant &p_arg7, const Variant &p_arg8, const Variant &p_arg9, const Variant &p_arg10) { - // Add a new pending command and reference it - pending_commands.push_back(PendingCommand()); - PendingCommand &cmd = pending_commands.back()->get(); - - // Update the command with the target key - cmd.key = p_key; - - // Determine command argument count - int &count = cmd.args; - if (p_arg10.get_type() != Variant::NIL) { - count = 10; - } else if (p_arg9.get_type() != Variant::NIL) { - count = 9; - } else if (p_arg8.get_type() != Variant::NIL) { - count = 8; - } else if (p_arg7.get_type() != Variant::NIL) { - count = 7; - } else if (p_arg6.get_type() != Variant::NIL) { - count = 6; - } else if (p_arg5.get_type() != Variant::NIL) { - count = 5; - } else if (p_arg4.get_type() != Variant::NIL) { - count = 4; - } else if (p_arg3.get_type() != Variant::NIL) { - count = 3; - } else if (p_arg2.get_type() != Variant::NIL) { - count = 2; - } else if (p_arg1.get_type() != Variant::NIL) { - count = 1; - } else { - count = 0; - } +#include "scene/main/node.h" - // Add the specified arguments to the command - if (count > 0) { - cmd.arg[0] = p_arg1; - } - if (count > 1) { - cmd.arg[1] = p_arg2; - } - if (count > 2) { - cmd.arg[2] = p_arg3; - } - if (count > 3) { - cmd.arg[3] = p_arg4; - } - if (count > 4) { - cmd.arg[4] = p_arg5; - } - if (count > 5) { - cmd.arg[5] = p_arg6; - } - if (count > 6) { - cmd.arg[6] = p_arg7; - } - if (count > 7) { - cmd.arg[7] = p_arg8; - } - if (count > 8) { - cmd.arg[8] = p_arg9; +void Tweener::set_tween(Ref<Tween> p_tween) { + tween = p_tween; +} + +void Tweener::_bind_methods() { + ADD_SIGNAL(MethodInfo("finished")); +} + +void Tween::start_tweeners() { + if (tweeners.is_empty()) { + dead = true; + ERR_FAIL_MSG("Tween without commands, aborting."); } - if (count > 9) { - cmd.arg[9] = p_arg10; + + for (List<Ref<Tweener>>::Element *E = tweeners.write[current_step].front(); E; E = E->next()) { + E->get()->start(); } } -void Tween::_process_pending_commands() { - // For each pending command... - for (List<PendingCommand>::Element *E = pending_commands.front(); E; E = E->next()) { - // Get the command - PendingCommand &cmd = E->get(); - Callable::CallError err; - - // Grab all of the arguments for the command - Variant *arg[10] = { - &cmd.arg[0], - &cmd.arg[1], - &cmd.arg[2], - &cmd.arg[3], - &cmd.arg[4], - &cmd.arg[5], - &cmd.arg[6], - &cmd.arg[7], - &cmd.arg[8], - &cmd.arg[9], - }; - - // Execute the command (and retrieve any errors) - this->call(cmd.key, (const Variant **)arg, cmd.args, err); - } +Ref<PropertyTweener> Tween::tween_property(Object *p_target, NodePath p_property, Variant p_to, float p_duration) { + ERR_FAIL_NULL_V(p_target, nullptr); + ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); + ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); - // Clear the pending commands - pending_commands.clear(); + Ref<PropertyTweener> tweener = memnew(PropertyTweener(p_target, p_property, p_to, p_duration)); + append(tweener); + return tweener; } -bool Tween::_set(const StringName &p_name, const Variant &p_value) { - // Set the correct attribute based on the given name - String name = p_name; - if (name == "playback/speed" || name == "speed") { // Backwards compatibility - set_speed_scale(p_value); - return true; +Ref<IntervalTweener> Tween::tween_interval(float p_time) { + ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); + ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); - } else if (name == "playback/active") { - set_active(p_value); - return true; + Ref<IntervalTweener> tweener = memnew(IntervalTweener(p_time)); + append(tweener); + return tweener; +} - } else if (name == "playback/repeat") { - set_repeat(p_value); - return true; - } - return false; +Ref<CallbackTweener> Tween::tween_callback(Callable p_callback) { + ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); + ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); + + Ref<CallbackTweener> tweener = memnew(CallbackTweener(p_callback)); + append(tweener); + return tweener; } -bool Tween::_get(const StringName &p_name, Variant &r_ret) const { - // Get the correct attribute based on the given name - String name = p_name; - if (name == "playback/speed") { // Backwards compatibility - r_ret = speed_scale; - return true; +Ref<MethodTweener> Tween::tween_method(Callable p_callback, float p_from, float p_to, float p_duration) { + ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); + ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); - } else if (name == "playback/active") { - r_ret = is_active(); - return true; + Ref<MethodTweener> tweener = memnew(MethodTweener(p_callback, p_from, p_to, p_duration)); + append(tweener); + return tweener; +} - } else if (name == "playback/repeat") { - r_ret = is_repeat(); - return true; +Ref<Tween> Tween::append(Ref<Tweener> p_tweener) { + ERR_FAIL_COND_V_MSG(invalid, nullptr, "Tween was created outside the scene tree, can't use Tweeners."); + ERR_FAIL_COND_V_MSG(started, nullptr, "Can't append to a Tween that has started. Use stop() first."); + p_tweener->set_tween(this); + + if (parallel_enabled) { + current_step = MAX(current_step, 0); + } else { + current_step++; } - return false; -} - -void Tween::_get_property_list(List<PropertyInfo> *p_list) const { - // Add the property info for the Tween object - p_list->push_back(PropertyInfo(Variant::BOOL, "playback/active", PROPERTY_HINT_NONE, "")); - p_list->push_back(PropertyInfo(Variant::BOOL, "playback/repeat", PROPERTY_HINT_NONE, "")); - p_list->push_back(PropertyInfo(Variant::FLOAT, "playback/speed", PROPERTY_HINT_RANGE, "-64,64,0.01")); -} - -void Tween::_notification(int p_what) { - // What notification did we receive? - switch (p_what) { - case NOTIFICATION_ENTER_TREE: { - // Are we not already active? - if (!is_active()) { - // Make sure that a previous process state was not saved - // Only process if "processing" is set - set_physics_process_internal(false); - set_process_internal(false); - } - } break; + parallel_enabled = default_parallel; - case NOTIFICATION_READY: { - // Do nothing - } break; + tweeners.resize(current_step + 1); + tweeners.write[current_step].push_back(p_tweener); - case NOTIFICATION_INTERNAL_PROCESS: { - // Are we processing during physics time? - if (tween_process_mode == TWEEN_PROCESS_PHYSICS) { - // Do nothing since we aren't aligned with physics when we should be - break; - } + return this; +} - // Should we update? - if (is_active()) { - // Update the tweens - _tween_process(get_process_delta_time()); - } - } break; +void Tween::stop() { + started = false; + running = false; + dead = false; +} - case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { - // Are we processing during 'regular' time? - if (tween_process_mode == TWEEN_PROCESS_IDLE) { - // Do nothing since we would only process during idle time - break; - } +void Tween::pause() { + running = false; +} - // Should we update? - if (is_active()) { - // Update the tweens - _tween_process(get_physics_process_delta_time()); - } - } break; +void Tween::play() { + ERR_FAIL_COND_MSG(invalid, "Tween invalid, can't play."); + ERR_FAIL_COND_MSG(dead, "Can't play finished Tween, use stop() first to reset its state."); + running = true; +} - case NOTIFICATION_EXIT_TREE: { - // We've left the tree. Stop all tweens - stop_all(); - } break; - } +void Tween::kill() { + running = false; // For the sake of is_running(). + dead = true; } -void Tween::_bind_methods() { - // Bind getters and setters - ClassDB::bind_method(D_METHOD("is_active"), &Tween::is_active); - ClassDB::bind_method(D_METHOD("set_active", "active"), &Tween::set_active); +bool Tween::is_running() { + return running; +} - ClassDB::bind_method(D_METHOD("is_repeat"), &Tween::is_repeat); - ClassDB::bind_method(D_METHOD("set_repeat", "repeat"), &Tween::set_repeat); +void Tween::set_valid(bool p_valid) { + invalid = !p_valid; +} - ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &Tween::set_speed_scale); - ClassDB::bind_method(D_METHOD("get_speed_scale"), &Tween::get_speed_scale); - - ClassDB::bind_method(D_METHOD("set_tween_process_mode", "mode"), &Tween::set_tween_process_mode); - ClassDB::bind_method(D_METHOD("get_tween_process_mode"), &Tween::get_tween_process_mode); - - // Bind the various Tween control methods - ClassDB::bind_method(D_METHOD("start"), &Tween::start); - ClassDB::bind_method(D_METHOD("reset", "object", "key"), &Tween::reset, DEFVAL("")); - ClassDB::bind_method(D_METHOD("reset_all"), &Tween::reset_all); - ClassDB::bind_method(D_METHOD("stop", "object", "key"), &Tween::stop, DEFVAL("")); - ClassDB::bind_method(D_METHOD("stop_all"), &Tween::stop_all); - ClassDB::bind_method(D_METHOD("resume", "object", "key"), &Tween::resume, DEFVAL("")); - ClassDB::bind_method(D_METHOD("resume_all"), &Tween::resume_all); - ClassDB::bind_method(D_METHOD("remove", "object", "key"), &Tween::remove, DEFVAL("")); - ClassDB::bind_method(D_METHOD("_remove_by_uid", "uid"), &Tween::_remove_by_uid); - ClassDB::bind_method(D_METHOD("remove_all"), &Tween::remove_all); - ClassDB::bind_method(D_METHOD("seek", "time"), &Tween::seek); - ClassDB::bind_method(D_METHOD("tell"), &Tween::tell); - ClassDB::bind_method(D_METHOD("get_runtime"), &Tween::get_runtime); - - // Bind interpolation and follow methods - ClassDB::bind_method(D_METHOD("interpolate_property", "object", "property", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::interpolate_property, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0)); - ClassDB::bind_method(D_METHOD("interpolate_method", "object", "method", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::interpolate_method, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0)); - ClassDB::bind_method(D_METHOD("interpolate_callback", "object", "duration", "callback", "arg1", "arg2", "arg3", "arg4", "arg5"), &Tween::interpolate_callback, DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant())); - ClassDB::bind_method(D_METHOD("interpolate_deferred_callback", "object", "duration", "callback", "arg1", "arg2", "arg3", "arg4", "arg5"), &Tween::interpolate_deferred_callback, DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant())); - ClassDB::bind_method(D_METHOD("follow_property", "object", "property", "initial_val", "target", "target_property", "duration", "trans_type", "ease_type", "delay"), &Tween::follow_property, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0)); - ClassDB::bind_method(D_METHOD("follow_method", "object", "method", "initial_val", "target", "target_method", "duration", "trans_type", "ease_type", "delay"), &Tween::follow_method, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0)); - ClassDB::bind_method(D_METHOD("targeting_property", "object", "property", "initial", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::targeting_property, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0)); - ClassDB::bind_method(D_METHOD("targeting_method", "object", "method", "initial", "initial_method", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::targeting_method, DEFVAL(TRANS_LINEAR), DEFVAL(EASE_IN_OUT), DEFVAL(0)); - - // Add the Tween signals - ADD_SIGNAL(MethodInfo("tween_started", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key"))); - ADD_SIGNAL(MethodInfo("tween_step", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key"), PropertyInfo(Variant::FLOAT, "elapsed"), PropertyInfo(Variant::OBJECT, "value"))); - ADD_SIGNAL(MethodInfo("tween_completed", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key"))); - ADD_SIGNAL(MethodInfo("tween_all_completed")); - - // Add the properties and tie them to the getters and setters - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "repeat"), "set_repeat", "is_repeat"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_tween_process_mode", "get_tween_process_mode"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale"); - - // Bind Idle vs Physics process - BIND_ENUM_CONSTANT(TWEEN_PROCESS_PHYSICS); - BIND_ENUM_CONSTANT(TWEEN_PROCESS_IDLE); +bool Tween::is_valid() { + return invalid; +} - // Bind the Transition type constants - BIND_ENUM_CONSTANT(TRANS_LINEAR); - BIND_ENUM_CONSTANT(TRANS_SINE); - BIND_ENUM_CONSTANT(TRANS_QUINT); - BIND_ENUM_CONSTANT(TRANS_QUART); - BIND_ENUM_CONSTANT(TRANS_QUAD); - BIND_ENUM_CONSTANT(TRANS_EXPO); - BIND_ENUM_CONSTANT(TRANS_ELASTIC); - BIND_ENUM_CONSTANT(TRANS_CUBIC); - BIND_ENUM_CONSTANT(TRANS_CIRC); - BIND_ENUM_CONSTANT(TRANS_BOUNCE); - BIND_ENUM_CONSTANT(TRANS_BACK); +Ref<Tween> Tween::bind_node(Node *p_node) { + bound_node = p_node->get_instance_id(); + is_bound = true; + return this; +} - // Bind the easing constants - BIND_ENUM_CONSTANT(EASE_IN); - BIND_ENUM_CONSTANT(EASE_OUT); - BIND_ENUM_CONSTANT(EASE_IN_OUT); - BIND_ENUM_CONSTANT(EASE_OUT_IN); +Ref<Tween> Tween::set_process_mode(TweenProcessMode p_mode) { + process_mode = p_mode; + return this; } -Variant Tween::_get_initial_val(const InterpolateData &p_data) const { - // What type of data are we interpolating? - switch (p_data.type) { - case INTER_PROPERTY: - case INTER_METHOD: - case FOLLOW_PROPERTY: - case FOLLOW_METHOD: - // Simply use the given initial value - return p_data.initial_val; - - case TARGETING_PROPERTY: - case TARGETING_METHOD: { - // Get the object that is being targeted - Object *object = ObjectDB::get_instance(p_data.target_id); - ERR_FAIL_COND_V(object == nullptr, p_data.initial_val); - - // Are we targeting a property or a method? - Variant initial_val; - if (p_data.type == TARGETING_PROPERTY) { - // Get the property from the target object - bool valid = false; - initial_val = object->get_indexed(p_data.target_key, &valid); - ERR_FAIL_COND_V(!valid, p_data.initial_val); - } else { - // Call the method and get the initial value from it - Callable::CallError error; - initial_val = object->call(p_data.target_key[0], nullptr, 0, error); - ERR_FAIL_COND_V(error.error != Callable::CallError::CALL_OK, p_data.initial_val); - } - return initial_val; - } +Tween::TweenProcessMode Tween::get_process_mode() { + return process_mode; +} + +Ref<Tween> Tween::set_pause_mode(TweenPauseMode p_mode) { + pause_mode = p_mode; + return this; +} + +Tween::TweenPauseMode Tween::get_pause_mode() { + return pause_mode; +} + +Ref<Tween> Tween::set_parallel(bool p_parallel) { + default_parallel = p_parallel; + parallel_enabled = p_parallel; + return this; +} + +Ref<Tween> Tween::set_loops(int p_loops) { + loops = p_loops; + return this; +} + +Ref<Tween> Tween::set_speed_scale(float p_speed) { + speed_scale = p_speed; + return this; +} + +Ref<Tween> Tween::set_trans(TransitionType p_trans) { + default_transition = p_trans; + return this; +} - case INTER_CALLBACK: - // Callback does not have a special initial value - break; +Tween::TransitionType Tween::get_trans() { + return default_transition; +} + +Ref<Tween> Tween::set_ease(EaseType p_ease) { + default_ease = p_ease; + return this; +} + +Tween::EaseType Tween::get_ease() { + return default_ease; +} + +Ref<Tween> Tween::parallel() { + parallel_enabled = true; + return this; +} + +Ref<Tween> Tween::chain() { + parallel_enabled = false; + return this; +} + +bool Tween::custom_step(float p_delta) { + bool r = running; + running = true; + bool ret = step(p_delta); + running = running && r; // Running might turn false when Tween finished. + return ret; +} + +bool Tween::step(float p_delta) { + ERR_FAIL_COND_V_MSG(tweeners.is_empty(), false, "Tween started, but has no Tweeners."); + + if (dead) { + return false; } - // If we've made it here, just return the delta value as the initial value - return p_data.delta_val; -} - -Variant Tween::_get_final_val(const InterpolateData &p_data) const { - switch (p_data.type) { - case FOLLOW_PROPERTY: - case FOLLOW_METHOD: { - // Get the object that is being followed - Object *target = ObjectDB::get_instance(p_data.target_id); - ERR_FAIL_COND_V(target == nullptr, p_data.initial_val); - - // We want to figure out the final value - Variant final_val; - if (p_data.type == FOLLOW_PROPERTY) { - // Read the property as-is - bool valid = false; - final_val = target->get_indexed(p_data.target_key, &valid); - ERR_FAIL_COND_V(!valid, p_data.initial_val); - } else { - // We're looking at a method. Call the method on the target object - Callable::CallError error; - final_val = target->call(p_data.target_key[0], nullptr, 0, error); - ERR_FAIL_COND_V(error.error != Callable::CallError::CALL_OK, p_data.initial_val); - } - // If we're looking at an INT value, instead convert it to a FLOAT - // This is better for interpolation - if (final_val.get_type() == Variant::INT) { - final_val = final_val.operator real_t(); - } + if (!running) { + return true; + } - return final_val; - } - default: { - // If we're not following a final value/method, use the final value from the data - return p_data.final_val; + if (is_bound) { + Object *bound_instance = ObjectDB::get_instance(bound_node); + if (bound_instance) { + Node *bound_node = Object::cast_to<Node>(bound_instance); + // This can't by anything else than Node, so we can omit checking if casting succeeded. + if (!bound_node->is_inside_tree()) { + return true; + } + } else { + return false; } } -} -Variant &Tween::_get_delta_val(InterpolateData &p_data) { - // What kind of data are we interpolating? - switch (p_data.type) { - case INTER_PROPERTY: - case INTER_METHOD: - // Simply return the given delta value - return p_data.delta_val; - - case FOLLOW_PROPERTY: - case FOLLOW_METHOD: { - // We're following an object, so grab that instance - Object *target = ObjectDB::get_instance(p_data.target_id); - ERR_FAIL_COND_V(target == nullptr, p_data.initial_val); - - // We want to figure out the final value - Variant final_val; - if (p_data.type == FOLLOW_PROPERTY) { - // Read the property as-is - bool valid = false; - final_val = target->get_indexed(p_data.target_key, &valid); - ERR_FAIL_COND_V(!valid, p_data.initial_val); - } else { - // We're looking at a method. Call the method on the target object - Callable::CallError error; - final_val = target->call(p_data.target_key[0], nullptr, 0, error); - ERR_FAIL_COND_V(error.error != Callable::CallError::CALL_OK, p_data.initial_val); - } + if (!started) { + current_step = 0; + loops_done = 0; + start_tweeners(); + started = true; + } - // If we're looking at an INT value, instead convert it to a FLOAT - // This is better for interpolation - if (final_val.get_type() == Variant::INT) { - final_val = final_val.operator real_t(); - } + float rem_delta = p_delta * speed_scale; + bool step_active = false; - // Calculate the delta based on the initial value and the final value - _calc_delta_val(p_data.initial_val, final_val, p_data.delta_val); - return p_data.delta_val; + while (rem_delta > 0 && running) { + float step_delta = rem_delta; + step_active = false; + + for (List<Ref<Tweener>>::Element *E = tweeners.write[current_step].front(); E; E = E->next()) { + // Modified inside Tweener.step(). + float temp_delta = rem_delta; + // Turns to true if any Tweener returns true (i.e. is still not finished). + step_active = E->get()->step(temp_delta) || step_active; + step_delta = MIN(temp_delta, rem_delta); } - case TARGETING_PROPERTY: - case TARGETING_METHOD: { - // Grab the initial value from the data to calculate delta - Variant initial_val = _get_initial_val(p_data); + rem_delta = step_delta; - // If we're looking at an INT value, instead convert it to a FLOAT - // This is better for interpolation - if (initial_val.get_type() == Variant::INT) { - initial_val = initial_val.operator real_t(); - } + if (!step_active) { + emit_signal("step_finished", current_step); + current_step++; - // Calculate the delta based on the initial value and the final value - _calc_delta_val(initial_val, p_data.final_val, p_data.delta_val); - return p_data.delta_val; + if (current_step == tweeners.size()) { + loops_done++; + if (loops_done == loops) { + running = false; + dead = true; + emit_signal("finished"); + } else { + emit_signal("loop_finished", loops_done); + current_step = 0; + start_tweeners(); + } + } else { + start_tweeners(); + } } + } + + return true; +} - case INTER_CALLBACK: - // Callbacks have no special delta - break; +bool Tween::should_pause() { + if (is_bound && pause_mode == TWEEN_PAUSE_BOUND) { + Object *bound_instance = ObjectDB::get_instance(bound_node); + if (bound_instance) { + Node *bound_node = Object::cast_to<Node>(bound_instance); + return !bound_node->can_process(); + } } - // If we've made it here, use the initial value as the delta - return p_data.initial_val; + + return pause_mode != TWEEN_PAUSE_PROCESS; } -Variant Tween::_run_equation(InterpolateData &p_data) { - // Get the initial and delta values from the data - Variant initial_val = _get_initial_val(p_data); - Variant &delta_val = _get_delta_val(p_data); - Variant result; +Variant Tween::interpolate_variant(Variant p_initial_val, Variant p_delta_val, float p_time, float p_duration, TransitionType p_trans, EaseType p_ease) { + ERR_FAIL_INDEX_V(p_trans, TransitionType::TRANS_MAX, Variant()); + ERR_FAIL_INDEX_V(p_ease, EaseType::EASE_MAX, Variant()); +// Helper macro to run equation on sub-elements of the value (e.g. x and y of Vector2). #define APPLY_EQUATION(element) \ - r.element = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, i.element, d.element, p_data.duration); + r.element = run_equation(p_trans, p_ease, p_time, i.element, d.element, p_duration); - // What type of data are we interpolating? - switch (initial_val.get_type()) { - case Variant::BOOL: - // Run the boolean specific equation (checking if it is at least 0.5) - result = (_run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, initial_val, delta_val, p_data.duration)) >= 0.5; - break; + switch (p_initial_val.get_type()) { + case Variant::BOOL: { + return (run_equation(p_trans, p_ease, p_time, p_initial_val, p_delta_val, p_duration)) >= 0.5; + } - case Variant::INT: - // Run the integer specific equation - result = (int)_run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, (int)initial_val, (int)delta_val, p_data.duration); - break; + case Variant::INT: { + return (int)run_equation(p_trans, p_ease, p_time, (int)p_initial_val, (int)p_delta_val, p_duration); + } - case Variant::FLOAT: - // Run the FLOAT specific equation - result = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, (real_t)initial_val, (real_t)delta_val, p_data.duration); - break; + case Variant::FLOAT: { + return run_equation(p_trans, p_ease, p_time, (real_t)p_initial_val, (real_t)p_delta_val, p_duration); + } case Variant::VECTOR2: { - // Get vectors for initial and delta values - Vector2 i = initial_val; - Vector2 d = delta_val; + Vector2 i = p_initial_val; + Vector2 d = p_delta_val; Vector2 r; - // Execute the equation and mutate the r vector - // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(x); APPLY_EQUATION(y); - result = r; - } break; + return r; + } + + case Variant::VECTOR2I: { + Vector2i i = p_initial_val; + Vector2i d = p_delta_val; + Vector2i r; + + APPLY_EQUATION(x); + APPLY_EQUATION(y); + return r; + } case Variant::RECT2: { - // Get the Rect2 for initial and delta value - Rect2 i = initial_val; - Rect2 d = delta_val; + Rect2 i = p_initial_val; + Rect2 d = p_delta_val; Rect2 r; - // Execute the equation for the position and size of Rect2 APPLY_EQUATION(position.x); APPLY_EQUATION(position.y); APPLY_EQUATION(size.x); APPLY_EQUATION(size.y); - result = r; - } break; + return r; + } + + case Variant::RECT2I: { + Rect2i i = p_initial_val; + Rect2i d = p_delta_val; + Rect2i r; + + APPLY_EQUATION(position.x); + APPLY_EQUATION(position.y); + APPLY_EQUATION(size.x); + APPLY_EQUATION(size.y); + return r; + } case Variant::VECTOR3: { - // Get vectors for initial and delta values - Vector3 i = initial_val; - Vector3 d = delta_val; + Vector3 i = p_initial_val; + Vector3 d = p_delta_val; Vector3 r; - // Execute the equation and mutate the r vector - // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(x); APPLY_EQUATION(y); APPLY_EQUATION(z); - result = r; - } break; + return r; + } + + case Variant::VECTOR3I: { + Vector3i i = p_initial_val; + Vector3i d = p_delta_val; + Vector3i r; + + APPLY_EQUATION(x); + APPLY_EQUATION(y); + APPLY_EQUATION(z); + return r; + } case Variant::TRANSFORM2D: { - // Get the transforms for initial and delta values - Transform2D i = initial_val; - Transform2D d = delta_val; + Transform2D i = p_initial_val; + Transform2D d = p_delta_val; Transform2D r; - // Execute the equation on the transforms and mutate the r transform - // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(elements[0][0]); APPLY_EQUATION(elements[0][1]); APPLY_EQUATION(elements[1][0]); APPLY_EQUATION(elements[1][1]); APPLY_EQUATION(elements[2][0]); APPLY_EQUATION(elements[2][1]); - result = r; - } break; + return r; + } case Variant::QUATERNION: { - // Get the quaternian for the initial and delta values - Quaternion i = initial_val; - Quaternion d = delta_val; + Quaternion i = p_initial_val; + Quaternion d = p_delta_val; Quaternion r; - // Execute the equation on the quaternian values and mutate the r quaternian - // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(x); APPLY_EQUATION(y); APPLY_EQUATION(z); APPLY_EQUATION(w); - result = r; - } break; + return r; + } case Variant::AABB: { - // Get the AABB's for the initial and delta values - AABB i = initial_val; - AABB d = delta_val; + AABB i = p_initial_val; + AABB d = p_delta_val; AABB r; - // Execute the equation for the position and size of the AABB's and mutate the r AABB - // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(position.x); APPLY_EQUATION(position.y); APPLY_EQUATION(position.z); APPLY_EQUATION(size.x); APPLY_EQUATION(size.y); APPLY_EQUATION(size.z); - result = r; - } break; + return r; + } case Variant::BASIS: { - // Get the basis for initial and delta values - Basis i = initial_val; - Basis d = delta_val; + Basis i = p_initial_val; + Basis d = p_delta_val; Basis r; - // Execute the equation on all the basis and mutate the r basis - // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(elements[0][0]); APPLY_EQUATION(elements[0][1]); APPLY_EQUATION(elements[0][2]); @@ -568,17 +440,14 @@ Variant Tween::_run_equation(InterpolateData &p_data) { APPLY_EQUATION(elements[2][0]); APPLY_EQUATION(elements[2][1]); APPLY_EQUATION(elements[2][2]); - result = r; - } break; + return r; + } case Variant::TRANSFORM3D: { - // Get the transforms for the initial and delta values - Transform3D i = initial_val; - Transform3D d = delta_val; + Transform3D i = p_initial_val; + Transform3D d = p_delta_val; Transform3D r; - // Execute the equation for each of the transforms and their origin and mutate the r transform - // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(basis.elements[0][0]); APPLY_EQUATION(basis.elements[0][1]); APPLY_EQUATION(basis.elements[0][2]); @@ -591,634 +460,67 @@ Variant Tween::_run_equation(InterpolateData &p_data) { APPLY_EQUATION(origin.x); APPLY_EQUATION(origin.y); APPLY_EQUATION(origin.z); - result = r; - } break; + return r; + } case Variant::COLOR: { - // Get the Color for initial and delta value - Color i = initial_val; - Color d = delta_val; + Color i = p_initial_val; + Color d = p_delta_val; Color r; - // Apply the equation on the Color RGBA, and mutate the r color - // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(r); APPLY_EQUATION(g); APPLY_EQUATION(b); APPLY_EQUATION(a); - result = r; - } break; - - default: { - // If unknown, just return the initial value - result = initial_val; - } break; - }; -#undef APPLY_EQUATION - // Return the result that was computed - return result; -} - -bool Tween::_apply_tween_value(InterpolateData &p_data, Variant &value) { - // Get the object we want to apply the new value to - Object *object = ObjectDB::get_instance(p_data.id); - ERR_FAIL_COND_V(object == nullptr, false); - - // What kind of data are we mutating? - switch (p_data.type) { - case INTER_PROPERTY: - case FOLLOW_PROPERTY: - case TARGETING_PROPERTY: { - // Simply set the property on the object - bool valid = false; - object->set_indexed(p_data.key, value, &valid); - return valid; + return r; } - case INTER_METHOD: - case FOLLOW_METHOD: - case TARGETING_METHOD: { - // We want to call the method on the target object - Callable::CallError error; - - // Do we have a non-nil value passed in? - if (value.get_type() != Variant::NIL) { - // Pass it as an argument to the function call - Variant *arg[1] = { &value }; - object->call(p_data.key[0], (const Variant **)arg, 1, error); - } else { - // Don't pass any argument - object->call(p_data.key[0], nullptr, 0, error); - } - - // Did we get an error from the function call? - return error.error == Callable::CallError::CALL_OK; + default: { + return p_initial_val; } - - case INTER_CALLBACK: - // Nothing to apply for a callback - break; }; - // No issues found! - return true; -} - -void Tween::_tween_process(float p_delta) { - // Process all of the pending commands - _process_pending_commands(); - - // If the scale is 0, make no progress on the tweens - if (speed_scale == 0) { - return; - } - - // Update the delta and whether we are pending an update - p_delta *= speed_scale; - pending_update++; - - // Are we repeating the interpolations? - if (repeat) { - // For each interpolation... - bool repeats_finished = true; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Get the data from it - InterpolateData &data = E->get(); - - // Is not finished? - if (!data.finish) { - // We aren't finished yet, no need to check the rest - repeats_finished = false; - break; - } - } - - // If we are all finished, we can reset all of the tweens - if (repeats_finished) { - reset_all(); - } - } - - // Are all of the tweens complete? - int any_unfinished = 0; - - // For each tween we wish to interpolate... - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Get the data from it - InterpolateData &data = E->get(); - - // Is the data not active or already finished? No need to go any further - if (!data.active || data.finish) { - continue; - } - - // Track if we hit one that isn't finished yet - any_unfinished++; - - // Get the target object for this interpolation - Object *object = ObjectDB::get_instance(data.id); - if (object == nullptr) { - continue; - } - - // Are we still delaying this tween? - bool prev_delaying = data.elapsed <= data.delay; - data.elapsed += p_delta; - if (data.elapsed < data.delay) { - continue; - } else if (prev_delaying) { - // We can apply the tween's value to the data and emit that the tween has started - _apply_tween_value(data, data.initial_val); - emit_signal("tween_started", object, NodePath(Vector<StringName>(), data.key, false)); - } - - // Are we at the end of the tween? - if (data.elapsed > (data.delay + data.duration)) { - // Set the elapsed time to the end and mark this one as finished - data.elapsed = data.delay + data.duration; - data.finish = true; - } - - // Are we interpolating a callback? - if (data.type == INTER_CALLBACK) { - // Is the tween completed? - if (data.finish) { - // Are we calling this callback deferred or immediately? - if (data.call_deferred) { - // Run the deferred function callback, applying the correct number of arguments - switch (data.args) { - case 0: - object->call_deferred(data.key[0]); - break; - case 1: - object->call_deferred(data.key[0], data.arg[0]); - break; - case 2: - object->call_deferred(data.key[0], data.arg[0], data.arg[1]); - break; - case 3: - object->call_deferred(data.key[0], data.arg[0], data.arg[1], data.arg[2]); - break; - case 4: - object->call_deferred(data.key[0], data.arg[0], data.arg[1], data.arg[2], data.arg[3]); - break; - case 5: - object->call_deferred(data.key[0], data.arg[0], data.arg[1], data.arg[2], data.arg[3], data.arg[4]); - break; - } - } else { - // Call the function directly with the arguments - Callable::CallError error; - Variant *arg[5] = { - &data.arg[0], - &data.arg[1], - &data.arg[2], - &data.arg[3], - &data.arg[4], - }; - object->call(data.key[0], (const Variant **)arg, data.args, error); - } - } - } else { - // We can apply the value directly - Variant result = _run_equation(data); - _apply_tween_value(data, result); - - // Emit that the tween has taken a step - emit_signal("tween_step", object, NodePath(Vector<StringName>(), data.key, false), data.elapsed, result); - } - - // Is the tween now finished? - if (data.finish) { - // Set it to the final value directly - Variant final_val = _get_final_val(data); - _apply_tween_value(data, final_val); - - // Mark the tween as completed and emit the signal - data.elapsed = 0; - emit_signal("tween_completed", object, NodePath(Vector<StringName>(), data.key, false)); - - // If we are not repeating the tween, remove it - if (!repeat) { - call_deferred("_remove_by_uid", data.uid); - any_unfinished--; - } - } - } - // One less update left to go - pending_update--; - - // If all tweens are completed, we no longer need to be active - if (any_unfinished == 0) { - set_active(false); - emit_signal("tween_all_completed"); - } -} - -void Tween::set_tween_process_mode(TweenProcessMode p_mode) { - tween_process_mode = p_mode; -} - -Tween::TweenProcessMode Tween::get_tween_process_mode() const { - return tween_process_mode; -} - -bool Tween::is_active() const { - return is_processing_internal() || is_physics_processing_internal(); -} - -void Tween::set_active(bool p_active) { - // Do nothing if it's the same active mode that we currently are - if (is_active() == p_active) { - return; - } - - // Depending on physics or idle, set processing - switch (tween_process_mode) { - case TWEEN_PROCESS_IDLE: - set_process_internal(p_active); - break; - case TWEEN_PROCESS_PHYSICS: - set_physics_process_internal(p_active); - break; - } -} - -bool Tween::is_repeat() const { - return repeat; -} - -void Tween::set_repeat(bool p_repeat) { - repeat = p_repeat; -} - -void Tween::set_speed_scale(float p_speed) { - speed_scale = p_speed; -} - -float Tween::get_speed_scale() const { - return speed_scale; -} - -void Tween::start() { - ERR_FAIL_COND_MSG(!is_inside_tree(), "Tween was not added to the SceneTree!"); - - // Are there any pending updates? - if (pending_update != 0) { - // Start the tweens after deferring - call_deferred("start"); - return; - } - - pending_update++; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - InterpolateData &data = E->get(); - data.active = true; - } - pending_update--; - - // We want to be activated - set_active(true); - - // Don't resume from current position if stop_all() function has been used - if (was_stopped) { - seek(0); - } - was_stopped = false; -} - -void Tween::reset(Object *p_object, StringName p_key) { - // Find all interpolations that use the same object and target string - pending_update++; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Get the target object - InterpolateData &data = E->get(); - Object *object = ObjectDB::get_instance(data.id); - if (object == nullptr) { - continue; - } - - // Do we have the correct object and key? - if (object == p_object && (data.concatenated_key == p_key || p_key == "")) { - // Reset the tween to the initial state - data.elapsed = 0; - data.finish = false; - - // Also apply the initial state if there isn't a delay - if (data.delay == 0) { - _apply_tween_value(data, data.initial_val); - } - } - } - pending_update--; -} - -void Tween::reset_all() { - // Go through all interpolations - pending_update++; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Get the target data and set it back to the initial state - InterpolateData &data = E->get(); - data.elapsed = 0; - data.finish = false; - - // If there isn't a delay, apply the value to the object - if (data.delay == 0) { - _apply_tween_value(data, data.initial_val); - } - } - pending_update--; -} - -void Tween::stop(Object *p_object, StringName p_key) { - // Find the tween that has the given target object and string key - pending_update++; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Get the object the tween is targeting - InterpolateData &data = E->get(); - Object *object = ObjectDB::get_instance(data.id); - if (object == nullptr) { - continue; - } - - // Is this the correct object and does it have the given key? - if (object == p_object && (data.concatenated_key == p_key || p_key == "")) { - // Disable the tween - data.active = false; - } - } - pending_update--; -} - -void Tween::stop_all() { - // We no longer need to be active since all tweens have been stopped - set_active(false); - was_stopped = true; - // For each interpolation... - pending_update++; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Simply set it inactive - InterpolateData &data = E->get(); - data.active = false; - } - pending_update--; -} - -void Tween::resume(Object *p_object, StringName p_key) { - // We need to be activated - // TODO: What if no tween is found?? - set_active(true); - - // Find the tween that uses the given target object and string key - pending_update++; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Grab the object - InterpolateData &data = E->get(); - Object *object = ObjectDB::get_instance(data.id); - if (object == nullptr) { - continue; - } - - // If the object and string key match, activate it - if (object == p_object && (data.concatenated_key == p_key || p_key == "")) { - data.active = true; - } - } - pending_update--; -} - -void Tween::resume_all() { - // Set ourselves active so we can process tweens - // TODO: What if there are no tweens? We get set to active for no reason! - set_active(true); - - // For each interpolation... - pending_update++; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Simply grab it and set it to active - InterpolateData &data = E->get(); - data.active = true; - } - pending_update--; -} - -void Tween::remove(Object *p_object, StringName p_key) { - // If we are still updating, call this function again later - if (pending_update != 0) { - call_deferred("remove", p_object, p_key); - return; - } - - // For each interpolation... - List<List<InterpolateData>::Element *> for_removal; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Get the target object - InterpolateData &data = E->get(); - Object *object = ObjectDB::get_instance(data.id); - if (object == nullptr) { - continue; - } - - // If the target object and string key match, queue it for removal - if (object == p_object && (data.concatenated_key == p_key || p_key == "")) { - for_removal.push_back(E); - } - } - - // For each interpolation we wish to remove... - for (List<List<InterpolateData>::Element *>::Element *E = for_removal.front(); E; E = E->next()) { - // Erase it - interpolates.erase(E->get()); - } -} - -void Tween::_remove_by_uid(int uid) { - // If we are still updating, call this function again later - if (pending_update != 0) { - call_deferred("_remove_by_uid", uid); - return; - } - - // Find the interpolation that matches the given UID - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - if (uid == E->get().uid) { - // It matches, erase it and stop looking - E->erase(); - break; - } - } -} - -void Tween::_push_interpolate_data(InterpolateData &p_data) { - pending_update++; - - // Add the new interpolation - p_data.uid = ++uid; - interpolates.push_back(p_data); - - pending_update--; +#undef APPLY_EQUATION } -void Tween::remove_all() { - // If we are still updating, call this function again later - if (pending_update != 0) { - call_deferred("remove_all"); - return; - } - // We no longer need to be active - set_active(false); - - // Clear out all interpolations and reset the uid - interpolates.clear(); - uid = 0; -} - -void Tween::seek(real_t p_time) { - // Go through each interpolation... - pending_update++; - for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Get the target data - InterpolateData &data = E->get(); - - // Update the elapsed data to be set to the target time - data.elapsed = p_time; - - // Are we at the end? - if (data.elapsed < data.delay) { - // There is still time left to go - data.finish = false; - continue; - } else if (data.elapsed >= (data.delay + data.duration)) { - // We are past the end of it, set the elapsed time to the end and mark as finished - data.elapsed = (data.delay + data.duration); - data.finish = true; - } else { - // We are not finished with this interpolation yet - data.finish = false; +Variant Tween::calculate_delta_value(Variant p_intial_val, Variant p_final_val) { + switch (p_intial_val.get_type()) { + case Variant::BOOL: { + return (int)p_final_val - (int)p_intial_val; } - // If we are a callback, do nothing special - if (data.type == INTER_CALLBACK) { - continue; + case Variant::RECT2: { + Rect2 i = p_intial_val; + Rect2 f = p_final_val; + return Rect2(f.position - i.position, f.size - i.size); } - // Run the equation on the data and apply the value - Variant result = _run_equation(data); - _apply_tween_value(data, result); - } - pending_update--; -} - -real_t Tween::tell() const { - // We want to grab the position of the furthest along tween - pending_update++; - real_t pos = 0.0; - - // For each interpolation... - for (const List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Get the data and figure out if its position is further along than the previous ones - const InterpolateData &data = E->get(); - if (data.elapsed > pos) { - // Save it if so - pos = data.elapsed; + case Variant::RECT2I: { + Rect2i i = p_intial_val; + Rect2i f = p_final_val; + return Rect2i(f.position - i.position, f.size - i.size); } - } - pending_update--; - return pos; -} - -real_t Tween::get_runtime() const { - // If the tween isn't moving, it'll last forever - if (speed_scale == 0) { - return INFINITY; - } - - pending_update++; - - // For each interpolation... - real_t runtime = 0.0; - for (const List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - // Get the tween data and see if it's runtime is greater than the previous tweens - const InterpolateData &data = E->get(); - real_t t = data.delay + data.duration; - if (t > runtime) { - // This is the longest running tween - runtime = t; - } - } - pending_update--; - - // Adjust the runtime for the current speed scale - return runtime / speed_scale; -} - -bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final_val, Variant &p_delta_val) { - // Get the initial, final, and delta values - const Variant &initial_val = p_initial_val; - const Variant &final_val = p_final_val; - Variant &delta_val = p_delta_val; - - // What kind of data are we interpolating? - switch (initial_val.get_type()) { - case Variant::BOOL: - // We'll treat booleans just like integers - case Variant::INT: - // Compute the integer delta - delta_val = (int)final_val - (int)initial_val; - break; - - case Variant::FLOAT: - // Convert to FLOAT and find the delta - delta_val = (real_t)final_val - (real_t)initial_val; - break; - - case Variant::VECTOR2: - // Convert to Vectors and find the delta - delta_val = final_val.operator Vector2() - initial_val.operator Vector2(); - break; - - case Variant::RECT2: { - // Build a new Rect2 and use the new position and sizes to make a delta - Rect2 i = initial_val; - Rect2 f = final_val; - delta_val = Rect2(f.position - i.position, f.size - i.size); - } break; - - case Variant::VECTOR3: - // Convert to Vectors and find the delta - delta_val = final_val.operator Vector3() - initial_val.operator Vector3(); - break; case Variant::TRANSFORM2D: { - // Build a new transform which is the difference between the initial and final values - Transform2D i = initial_val; - Transform2D f = final_val; - Transform2D d = Transform2D(); - d[0][0] = f.elements[0][0] - i.elements[0][0]; - d[0][1] = f.elements[0][1] - i.elements[0][1]; - d[1][0] = f.elements[1][0] - i.elements[1][0]; - d[1][1] = f.elements[1][1] - i.elements[1][1]; - d[2][0] = f.elements[2][0] - i.elements[2][0]; - d[2][1] = f.elements[2][1] - i.elements[2][1]; - delta_val = d; - } break; - - case Variant::QUATERNION: - // Convert to quaternianls and find the delta - delta_val = final_val.operator Quaternion() - initial_val.operator Quaternion(); - break; + Transform2D i = p_intial_val; + Transform2D f = p_final_val; + return Transform2D(f.elements[0][0] - i.elements[0][0], + f.elements[0][1] - i.elements[0][1], + f.elements[1][0] - i.elements[1][0], + f.elements[1][1] - i.elements[1][1], + f.elements[2][0] - i.elements[2][0], + f.elements[2][1] - i.elements[2][1]); + } case Variant::AABB: { - // Build a new AABB and use the new position and sizes to make a delta - AABB i = initial_val; - AABB f = final_val; - delta_val = AABB(f.position - i.position, f.size - i.size); - } break; + AABB i = p_intial_val; + AABB f = p_final_val; + return AABB(f.position - i.position, f.size - i.size); + } case Variant::BASIS: { - // Build a new basis which is the delta between the initial and final values - Basis i = initial_val; - Basis f = final_val; - delta_val = Basis(f.elements[0][0] - i.elements[0][0], + Basis i = p_intial_val; + Basis f = p_final_val; + return Basis(f.elements[0][0] - i.elements[0][0], f.elements[0][1] - i.elements[0][1], f.elements[0][2] - i.elements[0][2], f.elements[1][0] - i.elements[1][0], @@ -1227,14 +529,12 @@ bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final f.elements[2][0] - i.elements[2][0], f.elements[2][1] - i.elements[2][1], f.elements[2][2] - i.elements[2][2]); - } break; + } case Variant::TRANSFORM3D: { - // Build a new transform which is the difference between the initial and final values - Transform3D i = initial_val; - Transform3D f = final_val; - Transform3D d; - d.set(f.basis.elements[0][0] - i.basis.elements[0][0], + Transform3D i = p_intial_val; + Transform3D f = p_final_val; + return Transform3D(f.basis.elements[0][0] - i.basis.elements[0][0], f.basis.elements[0][1] - i.basis.elements[0][1], f.basis.elements[0][2] - i.basis.elements[0][2], f.basis.elements[1][0] - i.basis.elements[1][0], @@ -1246,569 +546,342 @@ bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final f.origin.x - i.origin.x, f.origin.y - i.origin.y, f.origin.z - i.origin.z); - - delta_val = d; - } break; - - case Variant::COLOR: { - // Make a new color which is the difference between each the color's RGBA attributes - Color i = initial_val; - Color f = final_val; - delta_val = Color(f.r - i.r, f.g - i.g, f.b - i.b, f.a - i.a); - } break; + } default: { - static Variant::Type supported_types[] = { - Variant::BOOL, - Variant::INT, - Variant::FLOAT, - Variant::VECTOR2, - Variant::RECT2, - Variant::VECTOR3, - Variant::TRANSFORM2D, - Variant::QUATERNION, - Variant::AABB, - Variant::BASIS, - Variant::TRANSFORM3D, - Variant::COLOR, - }; - - int length = *(&supported_types + 1) - supported_types; - String error_msg = "Invalid parameter type. Supported types are: "; - for (int i = 0; i < length; i++) { - if (i != 0) { - error_msg += ", "; - } - error_msg += Variant::get_type_name(supported_types[i]); - } - error_msg += "."; - ERR_PRINT(error_msg); - return false; + return Variant::evaluate(Variant::OP_SUBTRACT, p_final_val, p_intial_val); } }; - return true; } -void Tween::_build_interpolation(InterpolateType p_interpolation_type, Object *p_object, NodePath *p_property, StringName *p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - // TODO: Add initialization+implementation for remaining interpolation types - // TODO: Fix this method's organization to take advantage of the type - - // Make a new interpolation data - InterpolateData data; - data.active = true; - data.type = p_interpolation_type; - data.finish = false; - data.elapsed = 0; +void Tween::_bind_methods() { + ClassDB::bind_method(D_METHOD("tween_property", "object", "property", "final_val", "duration"), &Tween::tween_property); + ClassDB::bind_method(D_METHOD("tween_interval", "time"), &Tween::tween_interval); + ClassDB::bind_method(D_METHOD("tween_callback", "callback"), &Tween::tween_callback); + ClassDB::bind_method(D_METHOD("tween_method", "method", "from", "to", "duration"), &Tween::tween_method); + + ClassDB::bind_method(D_METHOD("custom_step", "delta"), &Tween::custom_step); + ClassDB::bind_method(D_METHOD("stop"), &Tween::stop); + ClassDB::bind_method(D_METHOD("pause"), &Tween::pause); + ClassDB::bind_method(D_METHOD("play"), &Tween::play); + ClassDB::bind_method(D_METHOD("kill"), &Tween::kill); + + ClassDB::bind_method(D_METHOD("is_running"), &Tween::is_running); + ClassDB::bind_method(D_METHOD("is_valid"), &Tween::is_valid); + ClassDB::bind_method(D_METHOD("bind_node", "node"), &Tween::bind_node); + ClassDB::bind_method(D_METHOD("set_process_mode", "mode"), &Tween::set_process_mode); + ClassDB::bind_method(D_METHOD("set_pause_mode", "mode"), &Tween::set_pause_mode); + + ClassDB::bind_method(D_METHOD("set_parallel", "parallel"), &Tween::set_parallel, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("set_loops", "loops"), &Tween::set_loops, DEFVAL(0)); + ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &Tween::set_speed_scale); + ClassDB::bind_method(D_METHOD("set_trans", "trans"), &Tween::set_trans); + ClassDB::bind_method(D_METHOD("set_ease", "ease"), &Tween::set_ease); - // Validate and apply interpolation data + ClassDB::bind_method(D_METHOD("parallel"), &Tween::parallel); + ClassDB::bind_method(D_METHOD("chain"), &Tween::chain); - // Give it the object - ERR_FAIL_COND_MSG(p_object == nullptr, "Invalid object provided to Tween."); - data.id = p_object->get_instance_id(); + ClassDB::bind_method(D_METHOD("interpolate_value", "trans_type", "ease_type", "elapsed_time", "initial_value", "delta_value", "duration"), &Tween::interpolate_variant); - // Validate the initial and final values - ERR_FAIL_COND_MSG(p_initial_val.get_type() != p_final_val.get_type(), "Initial value type '" + Variant::get_type_name(p_initial_val.get_type()) + "' does not match final value type '" + Variant::get_type_name(p_final_val.get_type()) + "'."); - data.initial_val = p_initial_val; - data.final_val = p_final_val; + ADD_SIGNAL(MethodInfo("step_finished", PropertyInfo(Variant::INT, "idx"))); + ADD_SIGNAL(MethodInfo("loop_finished", PropertyInfo(Variant::INT, "loop_count"))); + ADD_SIGNAL(MethodInfo("finished")); - // Check the Duration - ERR_FAIL_COND_MSG(p_duration < 0, "Only non-negative duration values allowed in Tweens."); - data.duration = p_duration; + BIND_ENUM_CONSTANT(TWEEN_PROCESS_PHYSICS); + BIND_ENUM_CONSTANT(TWEEN_PROCESS_IDLE); - // Tween Delay - ERR_FAIL_COND_MSG(p_delay < 0, "Only non-negative delay values allowed in Tweens."); - data.delay = p_delay; + BIND_ENUM_CONSTANT(TWEEN_PAUSE_BOUND); + BIND_ENUM_CONSTANT(TWEEN_PAUSE_STOP); + BIND_ENUM_CONSTANT(TWEEN_PAUSE_PROCESS); - // Transition type - ERR_FAIL_COND_MSG(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, "Invalid transition type provided to Tween."); - data.trans_type = p_trans_type; + BIND_ENUM_CONSTANT(TRANS_LINEAR); + BIND_ENUM_CONSTANT(TRANS_SINE); + BIND_ENUM_CONSTANT(TRANS_QUINT); + BIND_ENUM_CONSTANT(TRANS_QUART); + BIND_ENUM_CONSTANT(TRANS_QUAD); + BIND_ENUM_CONSTANT(TRANS_EXPO); + BIND_ENUM_CONSTANT(TRANS_ELASTIC); + BIND_ENUM_CONSTANT(TRANS_CUBIC); + BIND_ENUM_CONSTANT(TRANS_CIRC); + BIND_ENUM_CONSTANT(TRANS_BOUNCE); + BIND_ENUM_CONSTANT(TRANS_BACK); - // Easing type - ERR_FAIL_COND_MSG(p_ease_type < 0 || p_ease_type >= EASE_COUNT, "Invalid easing type provided to Tween."); - data.ease_type = p_ease_type; + BIND_ENUM_CONSTANT(EASE_IN); + BIND_ENUM_CONSTANT(EASE_OUT); + BIND_ENUM_CONSTANT(EASE_IN_OUT); + BIND_ENUM_CONSTANT(EASE_OUT_IN); +} - // Is the property defined? - if (p_property) { - // Check that the object actually contains the given property - bool prop_valid = false; - p_object->get_indexed(p_property->get_subnames(), &prop_valid); - ERR_FAIL_COND_MSG(!prop_valid, "Tween target object has no property named: " + p_property->get_concatenated_subnames() + "."); +Ref<PropertyTweener> PropertyTweener::from(Variant p_value) { + initial_val = p_value; + do_continue = false; + return this; +} - data.key = p_property->get_subnames(); - data.concatenated_key = p_property->get_concatenated_subnames(); - } +Ref<PropertyTweener> PropertyTweener::from_current() { + do_continue = false; + return this; +} - // Is the method defined? - if (p_method) { - // Does the object even have the requested method? - ERR_FAIL_COND_MSG(!p_object->has_method(*p_method), "Tween target object has no method named: " + *p_method + "."); +Ref<PropertyTweener> PropertyTweener::as_relative() { + relative = true; + return this; +} - data.key.push_back(*p_method); - data.concatenated_key = *p_method; - } +Ref<PropertyTweener> PropertyTweener::set_trans(Tween::TransitionType p_trans) { + trans_type = p_trans; + return this; +} - // Is there not a valid delta? - if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) { - return; - } +Ref<PropertyTweener> PropertyTweener::set_ease(Tween::EaseType p_ease) { + ease_type = p_ease; + return this; +} - // Add this interpolation to the total - _push_interpolate_data(data); +Ref<PropertyTweener> PropertyTweener::set_delay(float p_delay) { + delay = p_delay; + return this; } -void Tween::interpolate_property(Object *p_object, NodePath p_property, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - // If we are busy updating, call this function again later - if (pending_update != 0) { - _add_pending_command("interpolate_property", p_object, p_property, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); +void PropertyTweener::start() { + elapsed_time = 0; + finished = false; + + Object *target_instance = ObjectDB::get_instance(target); + if (!target_instance) { + WARN_PRINT("Target object freed before starting, aborting Tweener."); return; } - // Check that the target object is valid - ERR_FAIL_COND_MSG(p_object == nullptr, vformat("The Tween \"%s\"'s target node is `null`. Is the node reference correct?", get_name())); - - // Get the property from the node path - p_property = p_property.get_as_property_path(); - - // If no initial value given, grab the initial value from the object - // TODO: Is this documented? This is very useful and removes a lot of clutter from tweens! - if (p_initial_val.get_type() == Variant::NIL) { - p_initial_val = p_object->get_indexed(p_property.get_subnames()); + if (do_continue) { + initial_val = target_instance->get_indexed(property); } - // Convert any integers into REALs as they are better for interpolation - if (p_initial_val.get_type() == Variant::INT) { - p_initial_val = p_initial_val.operator real_t(); - } - if (p_final_val.get_type() == Variant::INT) { - p_final_val = p_final_val.operator real_t(); + if (relative) { + final_val = Variant::evaluate(Variant::Operator::OP_ADD, initial_val, base_final_val); } - // Build the interpolation data - _build_interpolation(INTER_PROPERTY, p_object, &p_property, nullptr, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); + delta_val = tween->calculate_delta_value(initial_val, final_val); } -void Tween::interpolate_method(Object *p_object, StringName p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - // If we are busy updating, call this function again later - if (pending_update != 0) { - _add_pending_command("interpolate_method", p_object, p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); - return; +bool PropertyTweener::step(float &r_delta) { + if (finished) { + // This is needed in case there's a parallel Tweener with longer duration. + return false; } - // Check that the target object is valid - ERR_FAIL_COND_MSG(p_object == nullptr, vformat("The Tween \"%s\"'s target node is `null`. Is the node reference correct?", get_name())); - - // Convert any integers into REALs as they are better for interpolation - if (p_initial_val.get_type() == Variant::INT) { - p_initial_val = p_initial_val.operator real_t(); - } - if (p_final_val.get_type() == Variant::INT) { - p_final_val = p_final_val.operator real_t(); + Object *target_instance = ObjectDB::get_instance(target); + if (!target_instance) { + return false; } + elapsed_time += r_delta; - // Build the interpolation data - _build_interpolation(INTER_METHOD, p_object, nullptr, &p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); -} - -void Tween::interpolate_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE) { - // If we are already updating, call this function again later - if (pending_update != 0) { - _add_pending_command("interpolate_callback", p_object, p_duration, p_callback, p_arg1, p_arg2, p_arg3, p_arg4, p_arg5); - return; + if (elapsed_time < delay) { + r_delta = 0; + return true; } - // Check that the target object is valid - ERR_FAIL_COND(p_object == nullptr); - - // Duration cannot be negative - ERR_FAIL_COND(p_duration < 0); - - // Check whether the object even has the callback - ERR_FAIL_COND_MSG(!p_object->has_method(p_callback), "Object has no callback named: " + p_callback + "."); - - // Build a new InterpolationData - InterpolateData data; - data.active = true; - data.type = INTER_CALLBACK; - data.finish = false; - data.call_deferred = false; - data.elapsed = 0; - - // Give the data it's configuration - data.id = p_object->get_instance_id(); - data.key.push_back(p_callback); - data.concatenated_key = p_callback; - data.duration = p_duration; - data.delay = 0; - - // Add arguments to the interpolation - int args = 0; - if (p_arg5.get_type() != Variant::NIL) { - args = 5; - } else if (p_arg4.get_type() != Variant::NIL) { - args = 4; - } else if (p_arg3.get_type() != Variant::NIL) { - args = 3; - } else if (p_arg2.get_type() != Variant::NIL) { - args = 2; - } else if (p_arg1.get_type() != Variant::NIL) { - args = 1; + float time = MIN(elapsed_time - delay, duration); + target_instance->set_indexed(property, tween->interpolate_variant(initial_val, delta_val, time, duration, trans_type, ease_type)); + + if (time < duration) { + r_delta = 0; + return true; } else { - args = 0; + finished = true; + r_delta = elapsed_time - delay - duration; + emit_signal("finished"); + return false; } - - data.args = args; - data.arg[0] = p_arg1; - data.arg[1] = p_arg2; - data.arg[2] = p_arg3; - data.arg[3] = p_arg4; - data.arg[4] = p_arg5; - - // Add the new interpolation - _push_interpolate_data(data); } -void Tween::interpolate_deferred_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE) { - // If we are already updating, call this function again later - if (pending_update != 0) { - _add_pending_command("interpolate_deferred_callback", p_object, p_duration, p_callback, p_arg1, p_arg2, p_arg3, p_arg4, p_arg5); - return; +void PropertyTweener::set_tween(Ref<Tween> p_tween) { + tween = p_tween; + if (trans_type == Tween::TRANS_MAX) { + trans_type = tween->get_trans(); } - - // Check that the target object is valid - ERR_FAIL_COND(p_object == nullptr); - - // No negative durations allowed - ERR_FAIL_COND(p_duration < 0); - - // Confirm the callback exists on the object - ERR_FAIL_COND_MSG(!p_object->has_method(p_callback), "Object has no callback named: " + p_callback + "."); - - // Create a new InterpolateData for the callback - InterpolateData data; - data.active = true; - data.type = INTER_CALLBACK; - data.finish = false; - data.call_deferred = true; - data.elapsed = 0; - - // Give the data it's configuration - data.id = p_object->get_instance_id(); - data.key.push_back(p_callback); - data.concatenated_key = p_callback; - data.duration = p_duration; - data.delay = 0; - - // Collect arguments for the callback - int args = 0; - if (p_arg5.get_type() != Variant::NIL) { - args = 5; - } else if (p_arg4.get_type() != Variant::NIL) { - args = 4; - } else if (p_arg3.get_type() != Variant::NIL) { - args = 3; - } else if (p_arg2.get_type() != Variant::NIL) { - args = 2; - } else if (p_arg1.get_type() != Variant::NIL) { - args = 1; - } else { - args = 0; + if (ease_type == Tween::EASE_MAX) { + ease_type = tween->get_ease(); } +} - data.args = args; - data.arg[0] = p_arg1; - data.arg[1] = p_arg2; - data.arg[2] = p_arg3; - data.arg[3] = p_arg4; - data.arg[4] = p_arg5; +void PropertyTweener::_bind_methods() { + ClassDB::bind_method(D_METHOD("from", "value"), &PropertyTweener::from); + ClassDB::bind_method(D_METHOD("from_current"), &PropertyTweener::from_current); + 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_delay", "delay"), &PropertyTweener::set_delay); +} - // Add the new interpolation - _push_interpolate_data(data); +PropertyTweener::PropertyTweener(Object *p_target, NodePath p_property, Variant p_to, float p_duration) { + target = p_target->get_instance_id(); + property = p_property.get_as_property_path().get_subnames(); + initial_val = p_target->get_indexed(property); + base_final_val = p_to; + final_val = base_final_val; + duration = p_duration; } -void Tween::follow_property(Object *p_object, NodePath p_property, Variant p_initial_val, Object *p_target, NodePath p_target_property, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - // If we are already updating, call this function again later - if (pending_update != 0) { - _add_pending_command("follow_property", p_object, p_property, p_initial_val, p_target, p_target_property, p_duration, p_trans_type, p_ease_type, p_delay); - return; - } +PropertyTweener::PropertyTweener() { + ERR_FAIL_MSG("Can't create empty PropertyTweener. Use get_tree().tween_property() or tween_property() instead."); +} - // Get the two properties from their paths - p_property = p_property.get_as_property_path(); - p_target_property = p_target_property.get_as_property_path(); +void IntervalTweener::start() { + elapsed_time = 0; + finished = false; +} - // If no initial value is given, grab it from the source object - // TODO: Is this documented? It's really helpful for decluttering tweens - if (p_initial_val.get_type() == Variant::NIL) { - p_initial_val = p_object->get_indexed(p_property.get_subnames()); +bool IntervalTweener::step(float &r_delta) { + if (finished) { + return false; } - // Convert initial INT values to FLOAT as they are better for interpolation - if (p_initial_val.get_type() == Variant::INT) { - p_initial_val = p_initial_val.operator real_t(); + elapsed_time += r_delta; + + if (elapsed_time < duration) { + r_delta = 0; + return true; + } else { + finished = true; + r_delta = elapsed_time - duration; + emit_signal("finished"); + return false; } +} - // Confirm the source and target objects are valid - ERR_FAIL_COND(p_object == nullptr); - ERR_FAIL_COND(p_target == nullptr); +IntervalTweener::IntervalTweener(float p_time) { + duration = p_time; +} - // No negative durations - ERR_FAIL_COND(p_duration < 0); +IntervalTweener::IntervalTweener() { + ERR_FAIL_MSG("Can't create empty IntervalTweener. Use get_tree().tween_interval() instead."); +} - // Ensure transition and easing types are valid - ERR_FAIL_COND(p_trans_type < 0 || p_trans_type >= TRANS_COUNT); - ERR_FAIL_COND(p_ease_type < 0 || p_ease_type >= EASE_COUNT); +Ref<CallbackTweener> CallbackTweener::set_delay(float p_delay) { + delay = p_delay; + return this; +} - // No negative delays - ERR_FAIL_COND(p_delay < 0); +void CallbackTweener::start() { + elapsed_time = 0; + finished = false; +} - // Confirm the source and target objects have the desired properties - bool prop_valid = false; - p_object->get_indexed(p_property.get_subnames(), &prop_valid); - ERR_FAIL_COND(!prop_valid); +bool CallbackTweener::step(float &r_delta) { + if (finished) { + return false; + } - bool target_prop_valid = false; - Variant target_val = p_target->get_indexed(p_target_property.get_subnames(), &target_prop_valid); - ERR_FAIL_COND(!target_prop_valid); + elapsed_time += r_delta; + if (elapsed_time >= delay) { + Variant result; + Callable::CallError ce; + callback.call(nullptr, 0, result, ce); + if (ce.error != Callable::CallError::CALL_OK) { + ERR_FAIL_V_MSG(false, "Error calling method from CallbackTweener: " + Variant::get_call_error_text(this, callback.get_method(), nullptr, 0, ce)); + } - // Convert target INT to FLOAT since it is better for interpolation - if (target_val.get_type() == Variant::INT) { - target_val = target_val.operator real_t(); + finished = true; + r_delta = elapsed_time - delay; + emit_signal("finished"); + return false; } - // Verify that the target value and initial value are the same type - ERR_FAIL_COND(target_val.get_type() != p_initial_val.get_type()); - - // Create a new InterpolateData - InterpolateData data; - data.active = true; - data.type = FOLLOW_PROPERTY; - data.finish = false; - data.elapsed = 0; - - // Give the InterpolateData it's configuration - data.id = p_object->get_instance_id(); - data.key = p_property.get_subnames(); - data.concatenated_key = p_property.get_concatenated_subnames(); - data.initial_val = p_initial_val; - data.target_id = p_target->get_instance_id(); - data.target_key = p_target_property.get_subnames(); - data.duration = p_duration; - data.trans_type = p_trans_type; - data.ease_type = p_ease_type; - data.delay = p_delay; - - // Add the interpolation - _push_interpolate_data(data); -} - -void Tween::follow_method(Object *p_object, StringName p_method, Variant p_initial_val, Object *p_target, StringName p_target_method, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - // If we are currently updating, call this function again later - if (pending_update != 0) { - _add_pending_command("follow_method", p_object, p_method, p_initial_val, p_target, p_target_method, p_duration, p_trans_type, p_ease_type, p_delay); - return; - } - // Convert initial INT values to FLOAT as they are better for interpolation - if (p_initial_val.get_type() == Variant::INT) { - p_initial_val = p_initial_val.operator real_t(); - } + r_delta = 0; + return true; +} - // Verify the source and target objects are valid - ERR_FAIL_COND(p_object == nullptr); - ERR_FAIL_COND(p_target == nullptr); +void CallbackTweener::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_delay", "delay"), &CallbackTweener::set_delay); +} - // No negative durations - ERR_FAIL_COND(p_duration < 0); +CallbackTweener::CallbackTweener(Callable p_callback) { + callback = p_callback; +} - // Ensure that the transition and ease types are valid - ERR_FAIL_COND(p_trans_type < 0 || p_trans_type >= TRANS_COUNT); - ERR_FAIL_COND(p_ease_type < 0 || p_ease_type >= EASE_COUNT); +CallbackTweener::CallbackTweener() { + ERR_FAIL_MSG("Can't create empty CallbackTweener. Use get_tree().tween_callback() instead."); +} - // No negative delays - ERR_FAIL_COND(p_delay < 0); +Ref<MethodTweener> MethodTweener::set_delay(float p_delay) { + delay = p_delay; + return this; +} - // Confirm both objects have the target methods - ERR_FAIL_COND_MSG(!p_object->has_method(p_method), "Object has no method named: " + p_method + "."); - ERR_FAIL_COND_MSG(!p_target->has_method(p_target_method), "Target has no method named: " + p_target_method + "."); +Ref<MethodTweener> MethodTweener::set_trans(Tween::TransitionType p_trans) { + trans_type = p_trans; + return this; +} - // Call the method to get the target value - Callable::CallError error; - Variant target_val = p_target->call(p_target_method, nullptr, 0, error); - ERR_FAIL_COND(error.error != Callable::CallError::CALL_OK); +Ref<MethodTweener> MethodTweener::set_ease(Tween::EaseType p_ease) { + ease_type = p_ease; + return this; +} - // Convert target INT values to FLOAT as they are better for interpolation - if (target_val.get_type() == Variant::INT) { - target_val = target_val.operator real_t(); - } - ERR_FAIL_COND(target_val.get_type() != p_initial_val.get_type()); - - // Make the new InterpolateData for the method follow - InterpolateData data; - data.active = true; - data.type = FOLLOW_METHOD; - data.finish = false; - data.elapsed = 0; - - // Give the data it's configuration - data.id = p_object->get_instance_id(); - data.key.push_back(p_method); - data.concatenated_key = p_method; - data.initial_val = p_initial_val; - data.target_id = p_target->get_instance_id(); - data.target_key.push_back(p_target_method); - data.duration = p_duration; - data.trans_type = p_trans_type; - data.ease_type = p_ease_type; - data.delay = p_delay; - - // Add the new interpolation - _push_interpolate_data(data); -} - -void Tween::targeting_property(Object *p_object, NodePath p_property, Object *p_initial, NodePath p_initial_property, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - // If we are currently updating, call this function again later - if (pending_update != 0) { - _add_pending_command("targeting_property", p_object, p_property, p_initial, p_initial_property, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); - return; - } - // Grab the target property and the target property - p_property = p_property.get_as_property_path(); - p_initial_property = p_initial_property.get_as_property_path(); +void MethodTweener::start() { + elapsed_time = 0; + finished = false; +} - // Convert the initial INT values to FLOAT as they are better for Interpolation - if (p_final_val.get_type() == Variant::INT) { - p_final_val = p_final_val.operator real_t(); +bool MethodTweener::step(float &r_delta) { + if (finished) { + return false; } - // Verify both objects are valid - ERR_FAIL_COND(p_object == nullptr); - ERR_FAIL_COND(p_initial == nullptr); - - // No negative durations - ERR_FAIL_COND(p_duration < 0); - - // Ensure transition and easing types are valid - ERR_FAIL_COND(p_trans_type < 0 || p_trans_type >= TRANS_COUNT); - ERR_FAIL_COND(p_ease_type < 0 || p_ease_type >= EASE_COUNT); - - // No negative delays - ERR_FAIL_COND(p_delay < 0); - - // Ensure the initial and target properties exist on their objects - bool prop_valid = false; - p_object->get_indexed(p_property.get_subnames(), &prop_valid); - ERR_FAIL_COND(!prop_valid); + elapsed_time += r_delta; - bool initial_prop_valid = false; - Variant initial_val = p_initial->get_indexed(p_initial_property.get_subnames(), &initial_prop_valid); - ERR_FAIL_COND(!initial_prop_valid); - - // Convert the initial INT value to FLOAT as it is better for interpolation - if (initial_val.get_type() == Variant::INT) { - initial_val = initial_val.operator real_t(); - } - ERR_FAIL_COND(initial_val.get_type() != p_final_val.get_type()); - - // Build the InterpolateData object - InterpolateData data; - data.active = true; - data.type = TARGETING_PROPERTY; - data.finish = false; - data.elapsed = 0; - - // Give the data it's configuration - data.id = p_object->get_instance_id(); - data.key = p_property.get_subnames(); - data.concatenated_key = p_property.get_concatenated_subnames(); - data.target_id = p_initial->get_instance_id(); - data.target_key = p_initial_property.get_subnames(); - data.initial_val = initial_val; - data.final_val = p_final_val; - data.duration = p_duration; - data.trans_type = p_trans_type; - data.ease_type = p_ease_type; - data.delay = p_delay; - - // Ensure there is a valid delta - if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) { - return; + if (elapsed_time < delay) { + r_delta = 0; + return true; } - // Add the interpolation - _push_interpolate_data(data); -} + float time = MIN(elapsed_time - delay, duration); + Variant current_val = tween->interpolate_variant(initial_val, delta_val, time, duration, trans_type, ease_type); + const Variant **argptr = (const Variant **)alloca(sizeof(Variant *)); + argptr[0] = ¤t_val; -void Tween::targeting_method(Object *p_object, StringName p_method, Object *p_initial, StringName p_initial_method, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - // If we are currently updating, call this function again later - if (pending_update != 0) { - _add_pending_command("targeting_method", p_object, p_method, p_initial, p_initial_method, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); - return; + Variant result; + Callable::CallError ce; + callback.call(argptr, 1, result, ce); + if (ce.error != Callable::CallError::CALL_OK) { + ERR_FAIL_V_MSG(false, "Error calling method from MethodTweener: " + Variant::get_call_error_text(this, callback.get_method(), argptr, 1, ce)); } - // Convert final INT values to FLOAT as they are better for interpolation - if (p_final_val.get_type() == Variant::INT) { - p_final_val = p_final_val.operator real_t(); + if (time < duration) { + r_delta = 0; + return true; + } else { + finished = true; + r_delta = elapsed_time - delay - duration; + emit_signal("finished"); + return false; } +} - // Make sure the given objects are valid - ERR_FAIL_COND(p_object == nullptr); - ERR_FAIL_COND(p_initial == nullptr); - - // No negative durations - ERR_FAIL_COND(p_duration < 0); - - // Ensure transition and easing types are valid - ERR_FAIL_COND(p_trans_type < 0 || p_trans_type >= TRANS_COUNT); - ERR_FAIL_COND(p_ease_type < 0 || p_ease_type >= EASE_COUNT); - - // No negative delays - ERR_FAIL_COND(p_delay < 0); - - // Make sure both objects have the given method - ERR_FAIL_COND_MSG(!p_object->has_method(p_method), "Object has no method named: " + p_method + "."); - ERR_FAIL_COND_MSG(!p_initial->has_method(p_initial_method), "Initial Object has no method named: " + p_initial_method + "."); - - // Call the method to get the initial value - Callable::CallError error; - Variant initial_val = p_initial->call(p_initial_method, nullptr, 0, error); - ERR_FAIL_COND(error.error != Callable::CallError::CALL_OK); - - // Convert initial INT values to FLOAT as they aer better for interpolation - if (initial_val.get_type() == Variant::INT) { - initial_val = initial_val.operator real_t(); +void MethodTweener::set_tween(Ref<Tween> p_tween) { + tween = p_tween; + if (trans_type == Tween::TRANS_MAX) { + trans_type = tween->get_trans(); } - ERR_FAIL_COND(initial_val.get_type() != p_final_val.get_type()); - - // Build the new InterpolateData object - InterpolateData data; - data.active = true; - data.type = TARGETING_METHOD; - data.finish = false; - data.elapsed = 0; - - // Configure the data - data.id = p_object->get_instance_id(); - data.key.push_back(p_method); - data.concatenated_key = p_method; - data.target_id = p_initial->get_instance_id(); - data.target_key.push_back(p_initial_method); - data.initial_val = initial_val; - data.final_val = p_final_val; - data.duration = p_duration; - data.trans_type = p_trans_type; - data.ease_type = p_ease_type; - data.delay = p_delay; - - // Ensure there is a valid delta - if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) { - return; + if (ease_type == Tween::EASE_MAX) { + ease_type = tween->get_ease(); } +} - // Add the interpolation - _push_interpolate_data(data); +void MethodTweener::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_delay", "delay"), &MethodTweener::set_delay); + ClassDB::bind_method(D_METHOD("set_trans", "trans"), &MethodTweener::set_trans); + ClassDB::bind_method(D_METHOD("set_ease", "ease"), &MethodTweener::set_ease); } -Tween::Tween() { +MethodTweener::MethodTweener(Callable p_callback, float p_from, float p_to, float p_duration) { + callback = p_callback; + initial_val = p_from; + delta_val = tween->calculate_delta_value(p_from, p_to); + duration = p_duration; } -Tween::~Tween() { +MethodTweener::MethodTweener() { + ERR_FAIL_MSG("Can't create empty MethodTweener. Use get_tree().tween_method() instead."); } |