From 6a7d50403a8c9f82b6db734f777e36e5b0ba37a0 Mon Sep 17 00:00:00 2001 From: Revan Ji Date: Tue, 28 Apr 2020 17:03:36 +0800 Subject: [PATCH] Add set_frame, pause, and oneshot to AnimatedTexture Add API documentation for said changes. (cherry picked from commit f5029e18ca24e775621be7c3f2e38c47069ecc80) --- doc/classes/AnimatedTexture.xml | 10 ++++++- scene/resources/texture.cpp | 50 +++++++++++++++++++++++++++++++-- scene/resources/texture.h | 12 +++++++- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/doc/classes/AnimatedTexture.xml b/doc/classes/AnimatedTexture.xml index f53af6fa5d1..04d2b6e867a 100644 --- a/doc/classes/AnimatedTexture.xml +++ b/doc/classes/AnimatedTexture.xml @@ -61,7 +61,9 @@ - + + Sets the currently visible frame of the texture. + Animation speed in frames per second. This value defines the default time interval between two frames of the animation, and thus the overall duration of the animation loop based on the [member frames] property. A value of 0 means no predefined number of frames per second, the animation will play according to each frame's frame delay (see [method set_frame_delay]). For example, an animation with 8 frames, no frame delay and a [code]fps[/code] value of 2 will run for 4 seconds, with each frame lasting 0.5 seconds. @@ -69,6 +71,12 @@ Number of frames to use in the animation. While you can create the frames independently with [method set_frame_texture], you need to set this value for the animation to take new frames into account. The maximum number of frames is [constant MAX_FRAMES]. + + If [code]true[/code], the animation will only play once and will not loop back to the first frame after reaching the end. Note that reaching the end will not set [member pause] to [code]true[/code]. + + + If [code]true[/code], the animation will pause where it currently is (i.e. at [member current_frame]). The animation will continue from where it was paused when changing this property to [code]false[/code]. + diff --git a/scene/resources/texture.cpp b/scene/resources/texture.cpp index 688bd6877bb..705d91069c0 100644 --- a/scene/resources/texture.cpp +++ b/scene/resources/texture.cpp @@ -1987,15 +1987,20 @@ void AnimatedTexture::_update_proxy() { } int iter_max = frame_count; - while (iter_max) { + while (iter_max && !pause) { float frame_limit = limit + frames[current_frame].delay_sec; if (time > frame_limit) { current_frame++; if (current_frame >= frame_count) { - current_frame = 0; + if (oneshot) { + current_frame = frame_count - 1; + } else { + current_frame = 0; + } } time -= frame_limit; + _change_notify("current_frame"); } else { break; } @@ -2018,6 +2023,33 @@ int AnimatedTexture::get_frames() const { return frame_count; } +void AnimatedTexture::set_current_frame(int p_frame) { + ERR_FAIL_COND(p_frame < 0 || p_frame >= frame_count); + + RWLockWrite r(rw_lock); + + current_frame = p_frame; +} +int AnimatedTexture::get_current_frame() const { + return current_frame; +} + +void AnimatedTexture::set_pause(bool p_pause) { + RWLockWrite r(rw_lock); + pause = p_pause; +} +bool AnimatedTexture::get_pause() const { + return pause; +} + +void AnimatedTexture::set_oneshot(bool p_oneshot) { + RWLockWrite r(rw_lock); + oneshot = p_oneshot; +} +bool AnimatedTexture::get_oneshot() const { + return oneshot; +} + void AnimatedTexture::set_frame_texture(int p_frame, const Ref &p_texture) { ERR_FAIL_COND(p_texture == this); @@ -2141,6 +2173,15 @@ void AnimatedTexture::_bind_methods() { ClassDB::bind_method(D_METHOD("set_frames", "frames"), &AnimatedTexture::set_frames); ClassDB::bind_method(D_METHOD("get_frames"), &AnimatedTexture::get_frames); + ClassDB::bind_method(D_METHOD("set_current_frame", "frame"), &AnimatedTexture::set_current_frame); + ClassDB::bind_method(D_METHOD("get_current_frame"), &AnimatedTexture::get_current_frame); + + ClassDB::bind_method(D_METHOD("set_pause", "pause"), &AnimatedTexture::set_pause); + ClassDB::bind_method(D_METHOD("get_pause"), &AnimatedTexture::get_pause); + + ClassDB::bind_method(D_METHOD("set_oneshot", "oneshot"), &AnimatedTexture::set_oneshot); + ClassDB::bind_method(D_METHOD("get_oneshot"), &AnimatedTexture::get_oneshot); + ClassDB::bind_method(D_METHOD("set_fps", "fps"), &AnimatedTexture::set_fps); ClassDB::bind_method(D_METHOD("get_fps"), &AnimatedTexture::get_fps); @@ -2153,6 +2194,9 @@ void AnimatedTexture::_bind_methods() { ClassDB::bind_method(D_METHOD("_update_proxy"), &AnimatedTexture::_update_proxy); ADD_PROPERTY(PropertyInfo(Variant::INT, "frames", PROPERTY_HINT_RANGE, "1," + itos(MAX_FRAMES), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_frames", "get_frames"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "current_frame", PROPERTY_HINT_NONE, "", 0), "set_current_frame", "get_current_frame"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pause"), "set_pause", "get_pause"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "oneshot"), "set_oneshot", "get_oneshot"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "fps", PROPERTY_HINT_RANGE, "0,1024,0.1"), "set_fps", "get_fps"); for (int i = 0; i < MAX_FRAMES; i++) { @@ -2171,6 +2215,8 @@ AnimatedTexture::AnimatedTexture() { fps = 4; prev_ticks = 0; current_frame = 0; + pause = false; + oneshot = false; VisualServer::get_singleton()->connect("frame_pre_draw", this, "_update_proxy"); #ifndef NO_THREADS diff --git a/scene/resources/texture.h b/scene/resources/texture.h index 3d1bd08d69a..98a0302e47e 100644 --- a/scene/resources/texture.h +++ b/scene/resources/texture.h @@ -695,7 +695,8 @@ private: Frame frames[MAX_FRAMES]; int frame_count; int current_frame; - + bool pause; + bool oneshot; float fps; float time; @@ -712,6 +713,15 @@ public: void set_frames(int p_frames); int get_frames() const; + void set_current_frame(int p_frame); + int get_current_frame() const; + + void set_pause(bool p_pause); + bool get_pause() const; + + void set_oneshot(bool p_oneshot); + bool get_oneshot() const; + void set_frame_texture(int p_frame, const Ref &p_texture); Ref get_frame_texture(int p_frame) const;