Path: blob/master/scene/resources/animated_texture.cpp
20984 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"3132#include "core/os/os.h"33#include "servers/rendering/rendering_server.h"3435void AnimatedTexture::_update_proxy() {36RWLockRead r(rw_lock);3738float delta;39if (prev_ticks == 0) {40delta = 0;41prev_ticks = OS::get_singleton()->get_ticks_usec();42} else {43uint64_t ticks = OS::get_singleton()->get_ticks_usec();44delta = float(double(ticks - prev_ticks) / 1000000.0);45prev_ticks = ticks;46}4748time += delta;4950float speed = speed_scale == 0 ? 0 : std::abs(1.0 / speed_scale);5152int iter_max = frame_count;53while (iter_max && !pause) {54float frame_limit = frames[current_frame].duration * speed;5556if (time > frame_limit) {57if (speed_scale > 0.0) {58current_frame++;59} else {60current_frame--;61}62if (current_frame >= frame_count) {63if (one_shot) {64current_frame = frame_count - 1;65} else {66current_frame = 0;67}68} else if (current_frame < 0) {69if (one_shot) {70current_frame = 0;71} else {72current_frame = frame_count - 1;73}74}75time -= frame_limit;7677} else {78break;79}80iter_max--;81}8283if (frames[current_frame].texture.is_valid()) {84RenderingServer::get_singleton()->texture_proxy_update(proxy, frames[current_frame].texture->get_rid());85}86}8788void AnimatedTexture::set_frames(int p_frames) {89ERR_FAIL_COND(p_frames < 1 || p_frames > MAX_FRAMES);9091RWLockWrite r(rw_lock);9293frame_count = p_frames;94}9596int AnimatedTexture::get_frames() const {97return frame_count;98}99100void AnimatedTexture::set_current_frame(int p_frame) {101ERR_FAIL_COND(p_frame < 0 || p_frame >= frame_count);102103RWLockWrite r(rw_lock);104105current_frame = p_frame;106time = 0;107}108109int AnimatedTexture::get_current_frame() const {110return current_frame;111}112113void AnimatedTexture::set_pause(bool p_pause) {114RWLockWrite r(rw_lock);115pause = p_pause;116}117118bool AnimatedTexture::get_pause() const {119return pause;120}121122void AnimatedTexture::set_one_shot(bool p_one_shot) {123RWLockWrite r(rw_lock);124one_shot = p_one_shot;125}126127bool AnimatedTexture::get_one_shot() const {128return one_shot;129}130131void AnimatedTexture::set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture) {132ERR_FAIL_COND(p_texture == this);133ERR_FAIL_INDEX(p_frame, MAX_FRAMES);134135RWLockWrite w(rw_lock);136137frames[p_frame].texture = p_texture;138}139140Ref<Texture2D> AnimatedTexture::get_frame_texture(int p_frame) const {141ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, Ref<Texture2D>());142143RWLockRead r(rw_lock);144145return frames[p_frame].texture;146}147148void AnimatedTexture::set_frame_duration(int p_frame, float p_duration) {149ERR_FAIL_INDEX(p_frame, MAX_FRAMES);150151RWLockWrite r(rw_lock);152153frames[p_frame].duration = p_duration;154}155156float AnimatedTexture::get_frame_duration(int p_frame) const {157ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, 0);158159RWLockRead r(rw_lock);160161return frames[p_frame].duration;162}163164void AnimatedTexture::set_speed_scale(float p_scale) {165ERR_FAIL_COND(p_scale < -1000 || p_scale >= 1000);166167RWLockWrite r(rw_lock);168169speed_scale = p_scale;170}171172float AnimatedTexture::get_speed_scale() const {173return speed_scale;174}175176int AnimatedTexture::get_width() const {177RWLockRead r(rw_lock);178179if (frames[current_frame].texture.is_null()) {180return 1;181}182183return frames[current_frame].texture->get_width();184}185186int AnimatedTexture::get_height() const {187RWLockRead r(rw_lock);188189if (frames[current_frame].texture.is_null()) {190return 1;191}192193return frames[current_frame].texture->get_height();194}195196RID AnimatedTexture::get_rid() const {197return proxy;198}199200bool AnimatedTexture::has_alpha() const {201RWLockRead r(rw_lock);202203if (frames[current_frame].texture.is_null()) {204return false;205}206207return frames[current_frame].texture->has_alpha();208}209210Ref<Image> AnimatedTexture::get_image() const {211RWLockRead r(rw_lock);212213if (frames[current_frame].texture.is_null()) {214return Ref<Image>();215}216217return frames[current_frame].texture->get_image();218}219220bool AnimatedTexture::is_pixel_opaque(int p_x, int p_y) const {221RWLockRead r(rw_lock);222223if (frames[current_frame].texture.is_valid()) {224return frames[current_frame].texture->is_pixel_opaque(p_x, p_y);225}226return true;227}228229void AnimatedTexture::_validate_property(PropertyInfo &p_property) const {230String prop = p_property.name;231if (prop.begins_with("frame_")) {232int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();233if (frame >= frame_count) {234p_property.usage = PROPERTY_USAGE_NONE;235}236}237}238239void AnimatedTexture::_bind_methods() {240ClassDB::bind_method(D_METHOD("set_frames", "frames"), &AnimatedTexture::set_frames);241ClassDB::bind_method(D_METHOD("get_frames"), &AnimatedTexture::get_frames);242243ClassDB::bind_method(D_METHOD("set_current_frame", "frame"), &AnimatedTexture::set_current_frame);244ClassDB::bind_method(D_METHOD("get_current_frame"), &AnimatedTexture::get_current_frame);245246ClassDB::bind_method(D_METHOD("set_pause", "pause"), &AnimatedTexture::set_pause);247ClassDB::bind_method(D_METHOD("get_pause"), &AnimatedTexture::get_pause);248249ClassDB::bind_method(D_METHOD("set_one_shot", "one_shot"), &AnimatedTexture::set_one_shot);250ClassDB::bind_method(D_METHOD("get_one_shot"), &AnimatedTexture::get_one_shot);251252ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &AnimatedTexture::set_speed_scale);253ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedTexture::get_speed_scale);254255ClassDB::bind_method(D_METHOD("set_frame_texture", "frame", "texture"), &AnimatedTexture::set_frame_texture);256ClassDB::bind_method(D_METHOD("get_frame_texture", "frame"), &AnimatedTexture::get_frame_texture);257258ClassDB::bind_method(D_METHOD("set_frame_duration", "frame", "duration"), &AnimatedTexture::set_frame_duration);259ClassDB::bind_method(D_METHOD("get_frame_duration", "frame"), &AnimatedTexture::get_frame_duration);260261ADD_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");262ADD_PROPERTY(PropertyInfo(Variant::INT, "current_frame", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_frame", "get_current_frame");263ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pause"), "set_pause", "get_pause");264ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");265ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-60,60,0.1,or_less,or_greater"), "set_speed_scale", "get_speed_scale");266267for (int i = 0; i < MAX_FRAMES; i++) {268ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "frame_" + itos(i) + "/texture", PROPERTY_HINT_RESOURCE_TYPE, Texture2D::get_class_static(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_INTERNAL), "set_frame_texture", "get_frame_texture", i);269ADD_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);270}271272BIND_CONSTANT(MAX_FRAMES);273}274275void AnimatedTexture::_finish_non_thread_safe_setup() {276RenderingServer::get_singleton()->connect("frame_pre_draw", callable_mp(this, &AnimatedTexture::_update_proxy));277}278279AnimatedTexture::AnimatedTexture() {280//proxy = RS::get_singleton()->texture_create();281proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();282proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);283284RenderingServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true);285286MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &AnimatedTexture::_finish_non_thread_safe_setup));287}288289AnimatedTexture::~AnimatedTexture() {290ERR_FAIL_NULL(RenderingServer::get_singleton());291RS::get_singleton()->free_rid(proxy);292RS::get_singleton()->free_rid(proxy_ph);293}294295296