Debian Codegraph @

scene/resources/animated_texture.cpp

raw symbols SCIP
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 #include "core/os/os.h"
34 #include "servers/rendering/rendering_server.h"
35
36 void AnimatedTexture::_update_proxy() {
37 RWLockRead r(rw_lock);
38
39 float delta;
40 if (prev_ticks == 0) {
41 delta = 0;
42 prev_ticks = OS::get_singleton()->get_ticks_usec();
43 } else {
44 uint64_t ticks = OS::get_singleton()->get_ticks_usec();
45 delta = float(double(ticks - prev_ticks) / 1000000.0);
46 prev_ticks = ticks;
47 }
48
49 time += delta;
50
51 float speed = speed_scale == 0 ? 0 : std::abs(1.0 / speed_scale);
52
53 int iter_max = frame_count;
54 while (iter_max && !pause) {
55 float frame_limit = frames[current_frame].duration * speed;
56
57 if (time > frame_limit) {
58 if (speed_scale > 0.0) {
59 current_frame++;
60 } else {
61 current_frame--;
62 }
63 if (current_frame >= frame_count) {
64 if (one_shot) {
65 current_frame = frame_count - 1;
66 } else {
67 current_frame = 0;
68 }
69 } else if (current_frame < 0) {
70 if (one_shot) {
71 current_frame = 0;
72 } else {
73 current_frame = frame_count - 1;
74 }
75 }
76 time -= frame_limit;
77
78 } else {
79 break;
80 }
81 iter_max--;
82 }
83
84 if (frames[current_frame].texture.is_valid()) {
85 RenderingServer::get_singleton()->texture_proxy_update(proxy, frames[current_frame].texture->get_rid());
86 }
87 }
88
89 void AnimatedTexture::set_frames(int p_frames) {
90 ERR_FAIL_COND(p_frames < 1 || p_frames > MAX_FRAMES);
91
92 RWLockWrite r(rw_lock);
93
94 frame_count = p_frames;
95 }
96
97 int AnimatedTexture::get_frames() const {
98 return frame_count;
99 }
100
101 void AnimatedTexture::set_current_frame(int p_frame) {
102 ERR_FAIL_COND(p_frame < 0 || p_frame >= frame_count);
103
104 RWLockWrite r(rw_lock);
105
106 current_frame = p_frame;
107 time = 0;
108 }
109
110 int AnimatedTexture::get_current_frame() const {
111 return current_frame;
112 }
113
114 void AnimatedTexture::set_pause(bool p_pause) {
115 RWLockWrite r(rw_lock);
116 pause = p_pause;
117 }
118
119 bool AnimatedTexture::get_pause() const {
120 return pause;
121 }
122
123 void AnimatedTexture::set_one_shot(bool p_one_shot) {
124 RWLockWrite r(rw_lock);
125 one_shot = p_one_shot;
126 }
127
128 bool AnimatedTexture::get_one_shot() const {
129 return one_shot;
130 }
131
132 void AnimatedTexture::set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture) {
133 ERR_FAIL_COND(p_texture == this);
134 ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
135
136 RWLockWrite w(rw_lock);
137
138 frames[p_frame].texture = p_texture;
139 }
140
141 Ref<Texture2D> AnimatedTexture::get_frame_texture(int p_frame) const {
142 ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, Ref<Texture2D>());
143
144 RWLockRead r(rw_lock);
145
146 return frames[p_frame].texture;
147 }
148
149 void AnimatedTexture::set_frame_duration(int p_frame, float p_duration) {
150 ERR_FAIL_INDEX(p_frame, MAX_FRAMES);
151
152 RWLockWrite r(rw_lock);
153
154 frames[p_frame].duration = p_duration;
155 }
156
157 float AnimatedTexture::get_frame_duration(int p_frame) const {
158 ERR_FAIL_INDEX_V(p_frame, MAX_FRAMES, 0);
159
160 RWLockRead r(rw_lock);
161
162 return frames[p_frame].duration;
163 }
164
165 void AnimatedTexture::set_speed_scale(float p_scale) {
166 ERR_FAIL_COND(p_scale < -1000 || p_scale >= 1000);
167
168 RWLockWrite r(rw_lock);
169
170 speed_scale = p_scale;
171 }
172
173 float AnimatedTexture::get_speed_scale() const {
174 return speed_scale;
175 }
176
177 int AnimatedTexture::get_width() const {
178 RWLockRead r(rw_lock);
179
180 if (frames[current_frame].texture.is_null()) {
181 return 1;
182 }
183
184 return frames[current_frame].texture->get_width();
185 }
186
187 int AnimatedTexture::get_height() const {
188 RWLockRead r(rw_lock);
189
190 if (frames[current_frame].texture.is_null()) {
191 return 1;
192 }
193
194 return frames[current_frame].texture->get_height();
195 }
196
197 RID AnimatedTexture::get_rid() const {
198 return proxy;
199 }
200
201 bool AnimatedTexture::has_alpha() const {
202 RWLockRead r(rw_lock);
203
204 if (frames[current_frame].texture.is_null()) {
205 return false;
206 }
207
208 return frames[current_frame].texture->has_alpha();
209 }
210
211 Ref<Image> AnimatedTexture::get_image() const {
212 RWLockRead r(rw_lock);
213
214 if (frames[current_frame].texture.is_null()) {
215 return Ref<Image>();
216 }
217
218 return frames[current_frame].texture->get_image();
219 }
220
221 bool AnimatedTexture::is_pixel_opaque(int p_x, int p_y) const {
222 RWLockRead r(rw_lock);
223
224 if (frames[current_frame].texture.is_valid()) {
225 return frames[current_frame].texture->is_pixel_opaque(p_x, p_y);
226 }
227 return true;
228 }
229
230 void AnimatedTexture::_validate_property(PropertyInfo &p_property) const {
231 String prop = p_property.name;
232 if (prop.begins_with("frame_")) {
233 int frame = prop.get_slicec('/', 0).get_slicec('_', 1).to_int();
234 if (frame >= frame_count) {
235 p_property.usage = PROPERTY_USAGE_NONE;
236 }
237 }
238 }
239
240 void AnimatedTexture::_bind_methods() {
241 ClassDB::bind_method(D_METHOD("set_frames", "frames"), &AnimatedTexture::set_frames);
242 ClassDB::bind_method(D_METHOD("get_frames"), &AnimatedTexture::get_frames);
243
244 ClassDB::bind_method(D_METHOD("set_current_frame", "frame"), &AnimatedTexture::set_current_frame);
245 ClassDB::bind_method(D_METHOD("get_current_frame"), &AnimatedTexture::get_current_frame);
246
247 ClassDB::bind_method(D_METHOD("set_pause", "pause"), &AnimatedTexture::set_pause);
248 ClassDB::bind_method(D_METHOD("get_pause"), &AnimatedTexture::get_pause);
249
250 ClassDB::bind_method(D_METHOD("set_one_shot", "one_shot"), &AnimatedTexture::set_one_shot);
251 ClassDB::bind_method(D_METHOD("get_one_shot"), &AnimatedTexture::get_one_shot);
252
253 ClassDB::bind_method(D_METHOD("set_speed_scale", "scale"), &AnimatedTexture::set_speed_scale);
254 ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedTexture::get_speed_scale);
255
256 ClassDB::bind_method(D_METHOD("set_frame_texture", "frame", "texture"), &AnimatedTexture::set_frame_texture);
257 ClassDB::bind_method(D_METHOD("get_frame_texture", "frame"), &AnimatedTexture::get_frame_texture);
258
259 ClassDB::bind_method(D_METHOD("set_frame_duration", "frame", "duration"), &AnimatedTexture::set_frame_duration);
260 ClassDB::bind_method(D_METHOD("get_frame_duration", "frame"), &AnimatedTexture::get_frame_duration);
261
262 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");
263 ADD_PROPERTY(PropertyInfo(Variant::INT, "current_frame", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_frame", "get_current_frame");
264 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "pause"), "set_pause", "get_pause");
265 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "get_one_shot");
266 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-60,60,0.1,or_less,or_greater"), "set_speed_scale", "get_speed_scale");
267
268 for (int i = 0; i < MAX_FRAMES; i++) {
269 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);
270 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);
271 }
272
273 BIND_CONSTANT(MAX_FRAMES);
274 }
275
276 void AnimatedTexture::_finish_non_thread_safe_setup() {
277 RenderingServer::get_singleton()->connect("frame_pre_draw", callable_mp(this, &AnimatedTexture::_update_proxy));
278 }
279
280 AnimatedTexture::AnimatedTexture() {
281 //proxy = RS::get_singleton()->texture_create();
282 proxy_ph = RS::get_singleton()->texture_2d_placeholder_create();
283 proxy = RS::get_singleton()->texture_proxy_create(proxy_ph);
284
285 RenderingServer::get_singleton()->texture_set_force_redraw_if_visible(proxy, true);
286
287 MessageQueue::get_main_singleton()->push_callable(callable_mp(this, &AnimatedTexture::_finish_non_thread_safe_setup));
288 }
289
290 AnimatedTexture::~AnimatedTexture() {
291 ERR_FAIL_NULL(RenderingServer::get_singleton());
292 RS::get_singleton()->free_rid(proxy);
293 RS::get_singleton()->free_rid(proxy_ph);
294 }