Path: blob/master/scene/resources/animated_texture.cpp
9896 views
/**************************************************************************/1/* animated_texture.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "animated_texture.h"3132void AnimatedTexture::_update_proxy() {33RWLockRead r(rw_lock);3435float delta;36if (prev_ticks == 0) {37delta = 0;38prev_ticks = OS::get_singleton()->get_ticks_usec();39} else {40uint64_t ticks = OS::get_singleton()->get_ticks_usec();41delta = float(double(ticks - prev_ticks) / 1000000.0);42prev_ticks = ticks;43}4445time += delta;4647float speed = speed_scale == 0 ? 0 : std::abs(1.0 / speed_scale);4849int iter_max = frame_count;50while (iter_max && !pause) {51float frame_limit = frames[current_frame].duration * speed;5253if (time > frame_limit) {54if (speed_scale > 0.0) {55current_frame++;56} else {57current_frame--;58}59if (current_frame >= frame_count) {60if (one_shot) {61current_frame = frame_count - 1;62} else {63current_frame = 0;64}65} else if (current_frame < 0) {66if (one_shot) {67current_frame = 0;68} else {69current_frame = frame_count - 1;70}71}72time -= frame_limit;7374} else {75break;76}77iter_max--;78}7980if (frames[current_frame].texture.is_valid()) {81RenderingServer::get_singleton()->texture_proxy_update(proxy, frames[current_frame].texture->get_rid());82}83}8485void AnimatedTexture::set_frames(int p_frames) {86ERR_FAIL_COND(p_frames < 1 || p_frames > MAX_FRAMES);8788RWLockWrite r(rw_lock);8990frame_count = p_frames;91}9293int AnimatedTexture::get_frames() const {94return frame_count;95}9697void AnimatedTexture::set_current_frame(int p_frame) {98ERR_FAIL_COND(p_frame < 0 || p_frame >= frame_count);99100RWLockWrite r(rw_lock);101102current_frame = p_frame;103time = 0;104}105106int AnimatedTexture::get_current_frame() const {107return current_frame;108}109110void AnimatedTexture::set_pause(bool p_pause) {111RWLockWrite r(rw_lock);112pause = p_pause;113}114115bool AnimatedTexture::get_pause() const {116return pause;117}118119void AnimatedTexture::set_one_shot(bool p_one_shot) {120RWLockWrite r(rw_lock);121one_shot = p_one_shot;122}123124bool AnimatedTexture::get_one_shot() const {125return one_shot;126}127128void AnimatedTexture::set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture) {129ERR_FAIL_COND(p_texture == this);130ERR_FAIL_INDEX(p_frame, MAX_FRAMES);131132RWLockWrite w(rw_lock);133134frames[p_frame].texture = p_texture;135}136137Ref<Texture2D> AnimatedTexture::get_frame_texture(int p_frame) const {138ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, Ref<Texture2D>());139140RWLockRead r(rw_lock);141142return frames[p_frame].texture;143}144145void AnimatedTexture::set_frame_duration(int p_frame, float p_duration) {146ERR_FAIL_INDEX(p_frame, MAX_FRAMES);147148RWLockWrite r(rw_lock);149150frames[p_frame].duration = p_duration;151}152153float AnimatedTexture::get_frame_duration(int p_frame) const {154ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, 0);155156RWLockRead r(rw_lock);157158return frames[p_frame].duration;159}160161void AnimatedTexture::set_speed_scale(float p_scale) {162ERR_FAIL_COND(p_scale < -1000 || p_scale >= 1000);163164RWLockWrite r(rw_lock);165166speed_scale = p_scale;167}168169float AnimatedTexture::get_speed_scale() const {170return speed_scale;171}172173int AnimatedTexture::get_width() const {174RWLockRead r(rw_lock);175176if (frames[current_frame].texture.is_null()) {177return 1;178}179180return frames[current_frame].texture->get_width();181}182183int AnimatedTexture::get_height() const {184RWLockRead r(rw_lock);185186if (frames[current_frame].texture.is_null()) {187return 1;188}189190return frames[current_frame].texture->get_height();191}192193RID AnimatedTexture::get_rid() const {194return proxy;195}196197bool AnimatedTexture::has_alpha() const {198RWLockRead r(rw_lock);199200if (frames[current_frame].texture.is_null()) {201return false;202}203204return frames[current_frame].texture->has_alpha();205}206207Ref<Image> AnimatedTexture::get_image() const {208RWLockRead r(rw_lock);209210if (frames[current_frame].texture.is_null()) {211return Ref<Image>();212}213214return frames[current_frame].texture->get_image();215}216217bool AnimatedTexture::is_pixel_opaque(int p_x, int p_y) const {218RWLockRead r(rw_lock);219220if (frames[current_frame].texture.is_valid()) {221return frames[current_frame].texture->is_pixel_opaque(p_x, p_y);222}223return true;224}225226void AnimatedTexture::_validate_property(PropertyInfo &p_property) const {227String prop = p_property.name;228if (prop.begins_with("frame_")) {229int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();230if (frame >= frame_count) {231p_property.usage = PROPERTY_USAGE_NONE;232}233}234}235236void AnimatedTexture::_bind_methods() {237ClassDB::bind_method(D_METHOD("set_frames", "frames"), &AnimatedTexture::set_frames);238ClassDB::bind_method(D_METHOD("get_frames"), &AnimatedTexture::get_frames);239240ClassDB::bind_method(D_METHOD("set_current_frame", "frame"), &AnimatedTexture::set_current_frame);241ClassDB::bind_method(D_METHOD("get_current_frame"), &AnimatedTexture::get_current_frame);242243ClassDB::bind_method(D_METHOD("set_pause", "pause"), &AnimatedTexture::set_pause);244ClassDB::bind_method(D_METHOD("get_pause"), &AnimatedTexture::get_pause);245246ClassDB::bind_method(D_METHOD("set_one_shot", "one_shot"), &AnimatedTexture::set_one_shot);247ClassDB::bind_method(D_METHOD("get_one_shot"), &AnimatedTexture::get_one_shot);248249ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &AnimatedTexture::set_speed_scale);250ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedTexture::get_speed_scale);251252ClassDB::bind_method(D_METHOD("set_frame_texture", "frame", "texture"), &AnimatedTexture::set_frame_texture);253ClassDB::bind_method(D_METHOD("get_frame_texture", "frame"), &AnimatedTexture::get_frame_texture);254255ClassDB::bind_method(D_METHOD("set_frame_duration", "frame", "duration"), &AnimatedTexture::set_frame_duration);256ClassDB::bind_method(D_METHOD("get_frame_duration", "frame"), &AnimatedTexture::get_frame_duration);257258ADD_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");259ADD_PROPERTY(PropertyInfo(Variant::INT, "current_frame", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_frame", "get_current_frame");260ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pause"), "set_pause", "get_pause");261ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");262ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-60,60,0.1,or_less,or_greater"), "set_speed_scale", "get_speed_scale");263264for (int i = 0; i < MAX_FRAMES; i++) {265ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "frame_" + itos(i) + "/texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_texture", "get_frame_texture", i);266ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "frame_" + itos(i) + "/duration", PROPERTY_HINT_RANGE, "0.0,16.0,0.01,or_greater,suffix:s", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_duration", "get_frame_duration", i);267}268269BIND_CONSTANT(MAX_FRAMES);270}271272void AnimatedTexture::_finish_non_thread_safe_setup() {273RenderingServer::get_singleton()->connect("frame_pre_draw", callable_mp(this, &AnimatedTexture::_update_proxy));274}275276AnimatedTexture::AnimatedTexture() {277//proxy = RS::get_singleton()->texture_create();278proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();279proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);280281RenderingServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true);282283MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &AnimatedTexture::_finish_non_thread_safe_setup));284}285286AnimatedTexture::~AnimatedTexture() {287ERR_FAIL_NULL(RenderingServer::get_singleton());288RS::get_singleton()->free(proxy);289RS::get_singleton()->free(proxy_ph);290}291292293