| 1 | /**************************************************************************/ |
| 2 | /* animated_texture.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "animated_texture.h" |
| 32 | |
| 33 | void AnimatedTexture::_update_proxy() { |
| 34 | RWLockRead r(rw_lock); |
| 35 | |
| 36 | float delta; |
| 37 | if (prev_ticks == 0) { |
| 38 | delta = 0; |
| 39 | prev_ticks = OS::get_singleton()->get_ticks_usec(); |
| 40 | } else { |
| 41 | uint64_t ticks = OS::get_singleton()->get_ticks_usec(); |
| 42 | delta = float(double(ticks - prev_ticks) / 1000000.0); |
| 43 | prev_ticks = ticks; |
| 44 | } |
| 45 | |
| 46 | time += delta; |
| 47 | |
| 48 | float speed = speed_scale == 0 ? 0 : abs(1.0 / speed_scale); |
| 49 | |
| 50 | int iter_max = frame_count; |
| 51 | while (iter_max && !pause) { |
| 52 | float frame_limit = frames[current_frame].duration * speed; |
| 53 | |
| 54 | if (time > frame_limit) { |
| 55 | if (speed_scale > 0.0) { |
| 56 | current_frame++; |
| 57 | } else { |
| 58 | current_frame--; |
| 59 | } |
| 60 | if (current_frame >= frame_count) { |
| 61 | if (one_shot) { |
| 62 | current_frame = frame_count - 1; |
| 63 | } else { |
| 64 | current_frame = 0; |
| 65 | } |
| 66 | } else if (current_frame < 0) { |
| 67 | if (one_shot) { |
| 68 | current_frame = 0; |
| 69 | } else { |
| 70 | current_frame = frame_count - 1; |
| 71 | } |
| 72 | } |
| 73 | time -= frame_limit; |
| 74 | |
| 75 | } else { |
| 76 | break; |
| 77 | } |
| 78 | iter_max--; |
| 79 | } |
| 80 | |
| 81 | if (frames[current_frame].texture.is_valid()) { |
| 82 | RenderingServer::get_singleton()->texture_proxy_update(proxy, frames[current_frame].texture->get_rid()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void AnimatedTexture::set_frames(int p_frames) { |
| 87 | ERR_FAIL_COND(p_frames < 1 || p_frames > MAX_FRAMES); |
| 88 | |
| 89 | RWLockWrite r(rw_lock); |
| 90 | |
| 91 | frame_count = p_frames; |
| 92 | } |
| 93 | |
| 94 | int AnimatedTexture::get_frames() const { |
| 95 | return frame_count; |
| 96 | } |
| 97 | |
| 98 | void AnimatedTexture::set_current_frame(int p_frame) { |
| 99 | ERR_FAIL_COND(p_frame < 0 || p_frame >= frame_count); |
| 100 | |
| 101 | RWLockWrite r(rw_lock); |
| 102 | |
| 103 | current_frame = p_frame; |
| 104 | time = 0; |
| 105 | } |
| 106 | |
| 107 | int AnimatedTexture::get_current_frame() const { |
| 108 | return current_frame; |
| 109 | } |
| 110 | |
| 111 | void AnimatedTexture::set_pause(bool p_pause) { |
| 112 | RWLockWrite r(rw_lock); |
| 113 | pause = p_pause; |
| 114 | } |
| 115 | |
| 116 | bool AnimatedTexture::get_pause() const { |
| 117 | return pause; |
| 118 | } |
| 119 | |
| 120 | void AnimatedTexture::set_one_shot(bool p_one_shot) { |
| 121 | RWLockWrite r(rw_lock); |
| 122 | one_shot = p_one_shot; |
| 123 | } |
| 124 | |
| 125 | bool AnimatedTexture::get_one_shot() const { |
| 126 | return one_shot; |
| 127 | } |
| 128 | |
| 129 | void AnimatedTexture::set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture) { |
| 130 | ERR_FAIL_COND(p_texture == this); |
| 131 | ERR_FAIL_INDEX(p_frame, MAX_FRAMES); |
| 132 | |
| 133 | RWLockWrite w(rw_lock); |
| 134 | |
| 135 | frames[p_frame].texture = p_texture; |
| 136 | } |
| 137 | |
| 138 | Ref<Texture2D> AnimatedTexture::get_frame_texture(int p_frame) const { |
| 139 | ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, Ref<Texture2D>()); |
| 140 | |
| 141 | RWLockRead r(rw_lock); |
| 142 | |
| 143 | return frames[p_frame].texture; |
| 144 | } |
| 145 | |
| 146 | void AnimatedTexture::set_frame_duration(int p_frame, float p_duration) { |
| 147 | ERR_FAIL_INDEX(p_frame, MAX_FRAMES); |
| 148 | |
| 149 | RWLockWrite r(rw_lock); |
| 150 | |
| 151 | frames[p_frame].duration = p_duration; |
| 152 | } |
| 153 | |
| 154 | float AnimatedTexture::get_frame_duration(int p_frame) const { |
| 155 | ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, 0); |
| 156 | |
| 157 | RWLockRead r(rw_lock); |
| 158 | |
| 159 | return frames[p_frame].duration; |
| 160 | } |
| 161 | |
| 162 | void AnimatedTexture::set_speed_scale(float p_scale) { |
| 163 | ERR_FAIL_COND(p_scale < -1000 || p_scale >= 1000); |
| 164 | |
| 165 | RWLockWrite r(rw_lock); |
| 166 | |
| 167 | speed_scale = p_scale; |
| 168 | } |
| 169 | |
| 170 | float AnimatedTexture::get_speed_scale() const { |
| 171 | return speed_scale; |
| 172 | } |
| 173 | |
| 174 | int AnimatedTexture::get_width() const { |
| 175 | RWLockRead r(rw_lock); |
| 176 | |
| 177 | if (!frames[current_frame].texture.is_valid()) { |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | return frames[current_frame].texture->get_width(); |
| 182 | } |
| 183 | |
| 184 | int AnimatedTexture::get_height() const { |
| 185 | RWLockRead r(rw_lock); |
| 186 | |
| 187 | if (!frames[current_frame].texture.is_valid()) { |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | return frames[current_frame].texture->get_height(); |
| 192 | } |
| 193 | |
| 194 | RID AnimatedTexture::get_rid() const { |
| 195 | return proxy; |
| 196 | } |
| 197 | |
| 198 | bool AnimatedTexture::has_alpha() const { |
| 199 | RWLockRead r(rw_lock); |
| 200 | |
| 201 | if (!frames[current_frame].texture.is_valid()) { |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | return frames[current_frame].texture->has_alpha(); |
| 206 | } |
| 207 | |
| 208 | Ref<Image> AnimatedTexture::get_image() const { |
| 209 | RWLockRead r(rw_lock); |
| 210 | |
| 211 | if (!frames[current_frame].texture.is_valid()) { |
| 212 | return Ref<Image>(); |
| 213 | } |
| 214 | |
| 215 | return frames[current_frame].texture->get_image(); |
| 216 | } |
| 217 | |
| 218 | bool AnimatedTexture::is_pixel_opaque(int p_x, int p_y) const { |
| 219 | RWLockRead r(rw_lock); |
| 220 | |
| 221 | if (frames[current_frame].texture.is_valid()) { |
| 222 | return frames[current_frame].texture->is_pixel_opaque(p_x, p_y); |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | void AnimatedTexture::_validate_property(PropertyInfo &p_property) const { |
| 228 | String prop = p_property.name; |
| 229 | if (prop.begins_with("frame_" )) { |
| 230 | int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int(); |
| 231 | if (frame >= frame_count) { |
| 232 | p_property.usage = PROPERTY_USAGE_NONE; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void AnimatedTexture::_bind_methods() { |
| 238 | ClassDB::bind_method(D_METHOD("set_frames" , "frames" ), &AnimatedTexture::set_frames); |
| 239 | ClassDB::bind_method(D_METHOD("get_frames" ), &AnimatedTexture::get_frames); |
| 240 | |
| 241 | ClassDB::bind_method(D_METHOD("set_current_frame" , "frame" ), &AnimatedTexture::set_current_frame); |
| 242 | ClassDB::bind_method(D_METHOD("get_current_frame" ), &AnimatedTexture::get_current_frame); |
| 243 | |
| 244 | ClassDB::bind_method(D_METHOD("set_pause" , "pause" ), &AnimatedTexture::set_pause); |
| 245 | ClassDB::bind_method(D_METHOD("get_pause" ), &AnimatedTexture::get_pause); |
| 246 | |
| 247 | ClassDB::bind_method(D_METHOD("set_one_shot" , "one_shot" ), &AnimatedTexture::set_one_shot); |
| 248 | ClassDB::bind_method(D_METHOD("get_one_shot" ), &AnimatedTexture::get_one_shot); |
| 249 | |
| 250 | ClassDB::bind_method(D_METHOD("set_speed_scale" , "scale" ), &AnimatedTexture::set_speed_scale); |
| 251 | ClassDB::bind_method(D_METHOD("get_speed_scale" ), &AnimatedTexture::get_speed_scale); |
| 252 | |
| 253 | ClassDB::bind_method(D_METHOD("set_frame_texture" , "frame" , "texture" ), &AnimatedTexture::set_frame_texture); |
| 254 | ClassDB::bind_method(D_METHOD("get_frame_texture" , "frame" ), &AnimatedTexture::get_frame_texture); |
| 255 | |
| 256 | ClassDB::bind_method(D_METHOD("set_frame_duration" , "frame" , "duration" ), &AnimatedTexture::set_frame_duration); |
| 257 | ClassDB::bind_method(D_METHOD("get_frame_duration" , "frame" ), &AnimatedTexture::get_frame_duration); |
| 258 | |
| 259 | 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" ); |
| 260 | ADD_PROPERTY(PropertyInfo(Variant::INT, "current_frame" , PROPERTY_HINT_NONE, "" , PROPERTY_USAGE_NONE), "set_current_frame" , "get_current_frame" ); |
| 261 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pause" ), "set_pause" , "get_pause" ); |
| 262 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot" ), "set_one_shot" , "get_one_shot" ); |
| 263 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale" , PROPERTY_HINT_RANGE, "-60,60,0.1,or_less,or_greater" ), "set_speed_scale" , "get_speed_scale" ); |
| 264 | |
| 265 | for (int i = 0; i < MAX_FRAMES; i++) { |
| 266 | ADD_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); |
| 267 | ADD_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); |
| 268 | } |
| 269 | |
| 270 | BIND_CONSTANT(MAX_FRAMES); |
| 271 | } |
| 272 | |
| 273 | AnimatedTexture::AnimatedTexture() { |
| 274 | //proxy = RS::get_singleton()->texture_create(); |
| 275 | proxy_ph = RS::get_singleton()->texture_2d_placeholder_create(); |
| 276 | proxy = RS::get_singleton()->texture_proxy_create(proxy_ph); |
| 277 | |
| 278 | RenderingServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true); |
| 279 | RenderingServer::get_singleton()->connect("frame_pre_draw" , callable_mp(this, &AnimatedTexture::_update_proxy)); |
| 280 | } |
| 281 | |
| 282 | AnimatedTexture::~AnimatedTexture() { |
| 283 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 284 | RS::get_singleton()->free(proxy); |
| 285 | RS::get_singleton()->free(proxy_ph); |
| 286 | } |
| 287 | |