Add get_total_elapsed_time() to Tween

This commit is contained in:
kobewi 2022-03-22 17:39:01 +01:00
parent a30d17fe23
commit e04ae8c8bc
3 changed files with 17 additions and 0 deletions

View File

@ -72,6 +72,13 @@
[b]Note:[/b] The [Tween] will become invalid after finished, but you can call [method stop] after the step, to keep it and reset. [b]Note:[/b] The [Tween] will become invalid after finished, but you can call [method stop] after the step, to keep it and reset.
</description> </description>
</method> </method>
<method name="get_total_elapsed_time" qualifiers="const">
<return type="float" />
<description>
Returns the total time in seconds the [Tween] has been animating (i.e. time since it started, not counting pauses etc.). The time is affected by [method set_speed_scale] and [method stop] will reset it to [code]0[/code].
[b]Note:[/code] As it results from accumulating frame deltas, the time returned after the [Tween] has finished animating will be slightly greater than the actual [Tween] duration.
</description>
</method>
<method name="interpolate_value"> <method name="interpolate_value">
<return type="Variant" /> <return type="Variant" />
<argument index="0" name="initial_value" type="Variant" /> <argument index="0" name="initial_value" type="Variant" />

View File

@ -130,6 +130,7 @@ void Tween::stop() {
started = false; started = false;
running = false; running = false;
dead = false; dead = false;
total_time = 0;
} }
void Tween::pause() { void Tween::pause() {
@ -272,12 +273,14 @@ bool Tween::step(float p_delta) {
ERR_FAIL_COND_V_MSG(tweeners.is_empty(), false, "Tween started, but has no Tweeners."); ERR_FAIL_COND_V_MSG(tweeners.is_empty(), false, "Tween started, but has no Tweeners.");
current_step = 0; current_step = 0;
loops_done = 0; loops_done = 0;
total_time = 0;
start_tweeners(); start_tweeners();
started = true; started = true;
} }
float rem_delta = p_delta * speed_scale; float rem_delta = p_delta * speed_scale;
bool step_active = false; bool step_active = false;
total_time += rem_delta;
while (rem_delta > 0 && running) { while (rem_delta > 0 && running) {
float step_delta = rem_delta; float step_delta = rem_delta;
@ -346,6 +349,10 @@ Node *Tween::get_bound_node() const {
} }
} }
float Tween::get_total_time() const {
return total_time;
}
real_t Tween::run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t p_time, real_t p_initial, real_t p_delta, real_t p_duration) { real_t Tween::run_equation(TransitionType p_trans_type, EaseType p_ease_type, real_t p_time, real_t p_initial, real_t p_delta, real_t p_duration) {
if (p_duration == 0) { if (p_duration == 0) {
// Special case to avoid dividing by 0 in equations. // Special case to avoid dividing by 0 in equations.
@ -624,6 +631,7 @@ void Tween::_bind_methods() {
ClassDB::bind_method(D_METHOD("pause"), &Tween::pause); ClassDB::bind_method(D_METHOD("pause"), &Tween::pause);
ClassDB::bind_method(D_METHOD("play"), &Tween::play); ClassDB::bind_method(D_METHOD("play"), &Tween::play);
ClassDB::bind_method(D_METHOD("kill"), &Tween::kill); ClassDB::bind_method(D_METHOD("kill"), &Tween::kill);
ClassDB::bind_method(D_METHOD("get_total_elapsed_time"), &Tween::get_total_time);
ClassDB::bind_method(D_METHOD("is_running"), &Tween::is_running); 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("is_valid"), &Tween::is_valid);

View File

@ -103,6 +103,7 @@ private:
ObjectID bound_node; ObjectID bound_node;
Vector<List<Ref<Tweener>>> tweeners; Vector<List<Ref<Tweener>>> tweeners;
float total_time = 0;
int current_step = -1; int current_step = -1;
int loops = 1; int loops = 1;
int loops_done = 0; int loops_done = 0;
@ -166,6 +167,7 @@ public:
bool step(float p_delta); bool step(float p_delta);
bool can_process(bool p_tree_paused) const; bool can_process(bool p_tree_paused) const;
Node *get_bound_node() const; Node *get_bound_node() const;
float get_total_time() const;
Tween() {} Tween() {}
}; };