Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/atlas_texture.cpp
9898 views
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 (region.size.width == 0) {
35
if (atlas.is_valid()) {
36
return atlas->get_width();
37
}
38
return 1;
39
} else {
40
return region.size.width + margin.size.width;
41
}
42
}
43
44
int AtlasTexture::get_height() const {
45
if (region.size.height == 0) {
46
if (atlas.is_valid()) {
47
return atlas->get_height();
48
}
49
return 1;
50
} else {
51
return 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
emit_changed();
98
}
99
100
Rect2 AtlasTexture::get_region() const {
101
return region;
102
}
103
104
void AtlasTexture::set_margin(const Rect2 &p_margin) {
105
if (margin == p_margin) {
106
return;
107
}
108
margin = p_margin;
109
emit_changed();
110
}
111
112
Rect2 AtlasTexture::get_margin() const {
113
return margin;
114
}
115
116
void AtlasTexture::set_filter_clip(const bool p_enable) {
117
filter_clip = p_enable;
118
emit_changed();
119
}
120
121
bool AtlasTexture::has_filter_clip() const {
122
return filter_clip;
123
}
124
125
Rect2 AtlasTexture::_get_region_rect() const {
126
Rect2 rc = region;
127
if (atlas.is_valid()) {
128
if (rc.size.width == 0) {
129
rc.size.width = atlas->get_width();
130
}
131
if (rc.size.height == 0) {
132
rc.size.height = atlas->get_height();
133
}
134
}
135
return rc;
136
}
137
138
void AtlasTexture::_bind_methods() {
139
ClassDB::bind_method(D_METHOD("set_atlas", "atlas"), &AtlasTexture::set_atlas);
140
ClassDB::bind_method(D_METHOD("get_atlas"), &AtlasTexture::get_atlas);
141
142
ClassDB::bind_method(D_METHOD("set_region", "region"), &AtlasTexture::set_region);
143
ClassDB::bind_method(D_METHOD("get_region"), &AtlasTexture::get_region);
144
145
ClassDB::bind_method(D_METHOD("set_margin", "margin"), &AtlasTexture::set_margin);
146
ClassDB::bind_method(D_METHOD("get_margin"), &AtlasTexture::get_margin);
147
148
ClassDB::bind_method(D_METHOD("set_filter_clip", "enable"), &AtlasTexture::set_filter_clip);
149
ClassDB::bind_method(D_METHOD("has_filter_clip"), &AtlasTexture::has_filter_clip);
150
151
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "atlas", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_atlas", "get_atlas");
152
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region", PROPERTY_HINT_NONE, "suffix:px"), "set_region", "get_region");
153
ADD_PROPERTY(PropertyInfo(Variant::RECT2, "margin", PROPERTY_HINT_NONE, "suffix:px"), "set_margin", "get_margin");
154
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "filter_clip"), "set_filter_clip", "has_filter_clip");
155
}
156
157
void AtlasTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
158
if (atlas.is_null()) {
159
return;
160
}
161
const Rect2 rc = _get_region_rect();
162
atlas->draw_rect_region(p_canvas_item, Rect2(p_pos + margin.position, rc.size), rc, p_modulate, p_transpose, filter_clip);
163
}
164
165
void AtlasTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
166
if (atlas.is_null()) {
167
return;
168
}
169
170
Rect2 src_rect = Rect2(0, 0, get_width(), get_height());
171
172
Rect2 dr;
173
Rect2 src_c;
174
if (get_rect_region(p_rect, src_rect, dr, src_c)) {
175
atlas->draw_rect_region(p_canvas_item, dr, src_c, p_modulate, p_transpose, filter_clip);
176
}
177
}
178
179
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 {
180
// This might not necessarily work well if using a rect, needs to be fixed properly.
181
if (atlas.is_null()) {
182
return;
183
}
184
185
Rect2 dr;
186
Rect2 src_c;
187
if (get_rect_region(p_rect, p_src_rect, dr, src_c)) {
188
atlas->draw_rect_region(p_canvas_item, dr, src_c, p_modulate, p_transpose, filter_clip);
189
}
190
}
191
192
bool AtlasTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
193
if (atlas.is_null()) {
194
return false;
195
}
196
197
Rect2 src = p_src_rect;
198
if (src.size == Size2()) {
199
src.size = region.size;
200
}
201
if (src.size == Size2() && atlas.is_valid()) {
202
src.size = atlas->get_size();
203
}
204
Vector2 scale = p_rect.size / src.size;
205
206
src.position += (region.position - margin.position);
207
Rect2 src_clipped = _get_region_rect().intersection(src);
208
if (src_clipped.size == Size2()) {
209
return false;
210
}
211
212
Vector2 ofs = (src_clipped.position - src.position);
213
if (scale.x < 0) {
214
ofs.x += (src_clipped.size.x - src.size.x);
215
}
216
if (scale.y < 0) {
217
ofs.y += (src_clipped.size.y - src.size.y);
218
}
219
220
r_rect = Rect2(p_rect.position + ofs * scale, src_clipped.size * scale);
221
r_src_rect = src_clipped;
222
return true;
223
}
224
225
bool AtlasTexture::is_pixel_opaque(int p_x, int p_y) const {
226
if (atlas.is_null()) {
227
return true;
228
}
229
230
int x = p_x + region.position.x - margin.position.x;
231
int y = p_y + region.position.y - margin.position.y;
232
233
// Margin edge may outside of atlas.
234
if (x < 0 || x >= atlas->get_width()) {
235
return false;
236
}
237
if (y < 0 || y >= atlas->get_height()) {
238
return false;
239
}
240
241
return atlas->is_pixel_opaque(x, y);
242
}
243
244
Ref<Image> AtlasTexture::get_image() const {
245
if (atlas.is_null()) {
246
return Ref<Image>();
247
}
248
249
const Ref<Image> &atlas_image = atlas->get_image();
250
if (atlas_image.is_null()) {
251
return Ref<Image>();
252
}
253
254
return atlas_image->get_region(_get_region_rect());
255
}
256
257