Debian Codegraph @

scene/resources/atlas_texture.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* atlas_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 "atlas_texture.h"
32
33 int AtlasTexture::get_width() const {
34 if (rounded_region.size.width == 0) {
35 if (atlas.is_valid()) {
36 return atlas->get_width();
37 }
38 return 1;
39 } else {
40 return rounded_region.size.width + margin.size.width;
41 }
42 }
43
44 int AtlasTexture::get_height() const {
45 if (rounded_region.size.height == 0) {
46 if (atlas.is_valid()) {
47 return atlas->get_height();
48 }
49 return 1;
50 } else {
51 return rounded_region.size.height + margin.size.height;
52 }
53 }
54
55 RID AtlasTexture::get_rid() const {
56 if (atlas.is_valid()) {
57 return atlas->get_rid();
58 }
59
60 return RID();
61 }
62
63 bool AtlasTexture::has_alpha() const {
64 if (atlas.is_valid()) {
65 return atlas->has_alpha();
66 }
67
68 return false;
69 }
70
71 void AtlasTexture::set_atlas(const Ref<Texture2D> &p_atlas) {
72 ERR_FAIL_COND(p_atlas == this);
73 if (atlas == p_atlas) {
74 return;
75 }
76 // Support recursive AtlasTextures.
77 if (Ref<AtlasTexture>(atlas).is_valid()) {
78 atlas->disconnect_changed(callable_mp((Resource *)this, &AtlasTexture::emit_changed));
79 }
80 atlas = p_atlas;
81 if (Ref<AtlasTexture>(atlas).is_valid()) {
82 atlas->connect_changed(callable_mp((Resource *)this, &AtlasTexture::emit_changed));
83 }
84
85 emit_changed();
86 }
87
88 Ref<Texture2D> AtlasTexture::get_atlas() const {
89 return atlas;
90 }
91
92 void AtlasTexture::set_region(const Rect2 &p_region) {
93 if (region == p_region) {
94 return;
95 }
96 region = p_region;
97 rounded_region = Rect2(p_region.position, p_region.size.floor());
98 emit_changed();
99 }
100
101 Rect2 AtlasTexture::get_region() const {
102 return region;
103 }
104
105 void AtlasTexture::set_margin(const Rect2 &p_margin) {
106 if (margin == p_margin) {
107 return;
108 }
109 margin = p_margin;
110 emit_changed();
111 }
112
113 Rect2 AtlasTexture::get_margin() const {
114 return margin;
115 }
116
117 void AtlasTexture::set_filter_clip(const bool p_enable) {
118 filter_clip = p_enable;
119 emit_changed();
120 }
121
122 bool AtlasTexture::has_filter_clip() const {
123 return filter_clip;
124 }
125
126 Rect2 AtlasTexture::_get_region_rect() const {
127 Rect2 rc = rounded_region;
128 if (atlas.is_valid()) {
129 if (rc.size.width == 0) {
130 rc.size.width = atlas->get_width();
131 }
132 if (rc.size.height == 0) {
133 rc.size.height = atlas->get_height();
134 }
135 }
136 return rc;
137 }
138
139 void AtlasTexture::_bind_methods() {
140 ClassDB::bind_method(D_METHOD("set_atlas", "atlas"), &AtlasTexture::set_atlas);
141 ClassDB::bind_method(D_METHOD("get_atlas"), &AtlasTexture::get_atlas);
142
143 ClassDB::bind_method(D_METHOD("set_region", "region"), &AtlasTexture::set_region);
144 ClassDB::bind_method(D_METHOD("get_region"), &AtlasTexture::get_region);
145
146 ClassDB::bind_method(D_METHOD("set_margin", "margin"), &AtlasTexture::set_margin);
147 ClassDB::bind_method(D_METHOD("get_margin"), &AtlasTexture::get_margin);
148
149 ClassDB::bind_method(D_METHOD("set_filter_clip", "enable"), &AtlasTexture::set_filter_clip);
150 ClassDB::bind_method(D_METHOD("has_filter_clip"), &AtlasTexture::has_filter_clip);
151
152 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "atlas", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_atlas", "get_atlas");
153 ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region", PROPERTY_HINT_NONE, "suffix:px"), "set_region", "get_region");
154 ADD_PROPERTY(PropertyInfo(Variant::RECT2, "margin", PROPERTY_HINT_NONE, "suffix:px"), "set_margin", "get_margin");
155 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_clip"), "set_filter_clip", "has_filter_clip");
156 }
157
158 void AtlasTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
159 if (atlas.is_null()) {
160 return;
161 }
162 const Rect2 rc = _get_region_rect();
163 atlas->draw_rect_region(p_canvas_item, Rect2(p_pos + margin.position, rc.size), rc, p_modulate, p_transpose, filter_clip);
164 }
165
166 void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
167 if (atlas.is_null()) {
168 return;
169 }
170
171 Rect2 src_rect = Rect2(0, 0, get_width(), get_height());
172
173 Rect2 dr;
174 Rect2 src_c;
175 if (get_rect_region(p_rect, src_rect, dr, src_c)) {
176 atlas->draw_rect_region(p_canvas_item, dr, src_c, p_modulate, p_transpose, filter_clip);
177 }
178 }
179
180 void AtlasTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
181 // This might not necessarily work well if using a rect, needs to be fixed properly.
182 if (atlas.is_null()) {
183 return;
184 }
185
186 Rect2 dr;
187 Rect2 src_c;
188 if (get_rect_region(p_rect, p_src_rect, dr, src_c)) {
189 atlas->draw_rect_region(p_canvas_item, dr, src_c, p_modulate, p_transpose, filter_clip);
190 }
191 }
192
193 bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
194 if (atlas.is_null()) {
195 return false;
196 }
197
198 Rect2 src = p_src_rect;
199 if (src.size == Size2()) {
200 src.size = rounded_region.size;
201 }
202 if (src.size == Size2() && atlas.is_valid()) {
203 src.size = atlas->get_size();
204 }
205 Vector2 scale = p_rect.size / src.size;
206
207 src.position += (rounded_region.position - margin.position);
208 Rect2 src_clipped = _get_region_rect().intersection(src);
209 if (src_clipped.size == Size2()) {
210 return false;
211 }
212
213 Vector2 ofs = (src_clipped.position - src.position);
214 if (scale.x < 0) {
215 ofs.x += (src_clipped.size.x - src.size.x);
216 }
217 if (scale.y < 0) {
218 ofs.y += (src_clipped.size.y - src.size.y);
219 }
220
221 r_rect = Rect2(p_rect.position + ofs * scale, src_clipped.size * scale);
222 r_src_rect = src_clipped;
223 return true;
224 }
225
226 bool AtlasTexture::is_pixel_opaque(int p_x, int p_y) const {
227 if (atlas.is_null()) {
228 return true;
229 }
230
231 int x = p_x + rounded_region.position.x - margin.position.x;
232 int y = p_y + rounded_region.position.y - margin.position.y;
233
234 // Margin edge may outside of atlas.
235 if (x < 0 || x >= atlas->get_width()) {
236 return false;
237 }
238 if (y < 0 || y >= atlas->get_height()) {
239 return false;
240 }
241
242 return atlas->is_pixel_opaque(x, y);
243 }
244
245 Ref<Image> AtlasTexture::get_image() const {
246 if (atlas.is_null()) {
247 return Ref<Image>();
248 }
249
250 const Ref<Image> &atlas_image = atlas->get_image();
251 if (atlas_image.is_null()) {
252 return Ref<Image>();
253 }
254
255 return atlas_image->get_region(_get_region_rect());
256 }