Skip to content
0.2.0-dev Download ZIP
108 lines ·3.8 KB Raw
1/**************************************************************************/
2/* animated_texture.h */
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#pragma once
32
33#include "core/os/rw_lock.h"
34#include "scene/resources/texture.h"
35
36class AnimatedTexture : public Texture2D {
37 GDCLASS(AnimatedTexture, Texture2D);
38
39 // Use readers writers lock for this, since its far more times read than written to.
40 RWLock rw_lock;
41
42public:
43 enum {
44 MAX_FRAMES = 256
45 };
46
47private:
48 RID proxy_ph;
49 RID proxy;
50
51 struct Frame {
52 Ref<Texture2D> texture;
53 float duration = 1.0;
54 };
55
56 Frame frames[MAX_FRAMES];
57 int frame_count = 1.0;
58 int current_frame = 0;
59 bool pause = false;
60 bool one_shot = false;
61 float speed_scale = 1.0;
62
63 float time = 0.0;
64
65 uint64_t prev_ticks = 0;
66
67 void _update_proxy();
68 void _finish_non_thread_safe_setup();
69
70protected:
71 static void _bind_methods();
72 void _validate_property(PropertyInfo &p_property) const;
73
74public:
75 void set_frames(int p_frames);
76 int get_frames() const;
77
78 void set_current_frame(int p_frame);
79 int get_current_frame() const;
80
81 void set_pause(bool p_pause);
82 bool get_pause() const;
83
84 void set_one_shot(bool p_one_shot);
85 bool get_one_shot() const;
86
87 void set_frame_texture(int p_frame, const Ref<Texture2D> &p_texture);
88 Ref<Texture2D> get_frame_texture(int p_frame) const;
89
90 void set_frame_duration(int p_frame, float p_duration);
91 float get_frame_duration(int p_frame) const;
92
93 void set_speed_scale(float p_scale);
94 float get_speed_scale() const;
95
96 virtual int get_width() const override;
97 virtual int get_height() const override;
98 virtual RID get_rid() const override;
99
100 virtual bool has_alpha() const override;
101
102 virtual Ref<Image> get_image() const override;
103
104 bool is_pixel_opaque(int p_x, int p_y) const override;
105
106 AnimatedTexture();
107 ~AnimatedTexture();
108};