Debian Codegraph @

scene/gui/texture_rect.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* texture_rect.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 "texture_rect.h"
32
33 void TextureRect::_notification(int p_what) {
34 switch (p_what) {
35 case NOTIFICATION_DRAW: {
36 if (texture.is_null()) {
37 return;
38 }
39
40 Size2 size;
41 Point2 offset;
42 Rect2 region;
43 bool tile = false;
44
45 switch (stretch_mode) {
46 case STRETCH_SCALE: {
47 size = get_size();
48 } break;
49 case STRETCH_TILE: {
50 size = get_size();
51 tile = true;
52 } break;
53 case STRETCH_KEEP: {
54 size = texture->get_size();
55 } break;
56 case STRETCH_KEEP_CENTERED: {
57 offset = (get_size() - texture->get_size()) / 2;
58 size = texture->get_size();
59 } break;
60 case STRETCH_KEEP_ASPECT_CENTERED:
61 case STRETCH_KEEP_ASPECT: {
62 size = get_size();
63 int tex_width = texture->get_width() * size.height / texture->get_height();
64 int tex_height = size.height;
65
66 if (tex_width > size.width) {
67 tex_width = size.width;
68 tex_height = texture->get_height() * tex_width / texture->get_width();
69 }
70
71 if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
72 offset.x += (size.width - tex_width) / 2;
73 offset.y += (size.height - tex_height) / 2;
74 }
75
76 size.width = tex_width;
77 size.height = tex_height;
78 } break;
79 case STRETCH_KEEP_ASPECT_COVERED: {
80 size = get_size();
81
82 Size2 tex_size = texture->get_size();
83 Size2 scale_size(size.width / tex_size.width, size.height / tex_size.height);
84 float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height;
85 Size2 scaled_tex_size = tex_size * scale;
86
87 region.position = ((scaled_tex_size - size) / scale).abs() / 2.0f;
88 region.size = size / scale;
89 } break;
90 }
91
92 size.width *= hflip ? -1.0f : 1.0f;
93 size.height *= vflip ? -1.0f : 1.0f;
94
95 if (region.has_area()) {
96 draw_texture_rect_region(texture, Rect2(offset, size), region);
97 } else {
98 draw_texture_rect(texture, Rect2(offset, size), tile);
99 }
100 } break;
101 case NOTIFICATION_RESIZED: {
102 update_minimum_size();
103 } break;
104 }
105 }
106
107 Size2 TextureRect::get_minimum_size() const {
108 if (texture.is_valid()) {
109 switch (expand_mode) {
110 case EXPAND_KEEP_SIZE: {
111 return texture->get_size();
112 } break;
113 case EXPAND_IGNORE_SIZE: {
114 return Size2();
115 } break;
116 case EXPAND_FIT_WIDTH: {
117 return Size2(get_size().y, 0);
118 } break;
119 case EXPAND_FIT_WIDTH_PROPORTIONAL: {
120 real_t ratio = real_t(texture->get_width()) / texture->get_height();
121 return Size2(get_size().y * ratio, 0);
122 } break;
123 case EXPAND_FIT_HEIGHT: {
124 return Size2(0, get_size().x);
125 } break;
126 case EXPAND_FIT_HEIGHT_PROPORTIONAL: {
127 real_t ratio = real_t(texture->get_height()) / texture->get_width();
128 return Size2(0, get_size().x * ratio);
129 } break;
130 }
131 }
132 return Size2();
133 }
134
135 void TextureRect::_bind_methods() {
136 ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture);
137 ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture);
138 ClassDB::bind_method(D_METHOD("set_expand_mode", "expand_mode"), &TextureRect::set_expand_mode);
139 ClassDB::bind_method(D_METHOD("get_expand_mode"), &TextureRect::get_expand_mode);
140 ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureRect::set_flip_h);
141 ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureRect::is_flipped_h);
142 ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureRect::set_flip_v);
143 ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureRect::is_flipped_v);
144 ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode);
145 ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);
146
147 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
148 ADD_PROPERTY(PropertyInfo(Variant::INT, "expand_mode", PROPERTY_HINT_ENUM, "Keep Size,Ignore Size,Fit Width,Fit Width Proportional,Fit Height,Fit Height Proportional"), "set_expand_mode", "get_expand_mode");
149 ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode");
150 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
151 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");
152
153 BIND_ENUM_CONSTANT(EXPAND_KEEP_SIZE);
154 BIND_ENUM_CONSTANT(EXPAND_IGNORE_SIZE);
155 BIND_ENUM_CONSTANT(EXPAND_FIT_WIDTH);
156 BIND_ENUM_CONSTANT(EXPAND_FIT_WIDTH_PROPORTIONAL);
157 BIND_ENUM_CONSTANT(EXPAND_FIT_HEIGHT);
158 BIND_ENUM_CONSTANT(EXPAND_FIT_HEIGHT_PROPORTIONAL);
159
160 BIND_ENUM_CONSTANT(STRETCH_SCALE);
161 BIND_ENUM_CONSTANT(STRETCH_TILE);
162 BIND_ENUM_CONSTANT(STRETCH_KEEP);
163 BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
164 BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
165 BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
166 BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
167 }
168
169 #ifndef DISABLE_DEPRECATED
170 bool TextureRect::_set(const StringName &p_name, const Variant &p_value) {
171 if ((p_name == SNAME("expand") || p_name == SNAME("ignore_texture_size")) && p_value.operator bool()) {
172 expand_mode = EXPAND_IGNORE_SIZE;
173 return true;
174 }
175 return false;
176 }
177 #endif
178
179 void TextureRect::_texture_changed() {
180 queue_redraw();
181 update_minimum_size();
182 }
183
184 void TextureRect::set_texture(const Ref<Texture2D> &p_tex) {
185 if (p_tex == texture) {
186 return;
187 }
188
189 if (texture.is_valid()) {
190 texture->disconnect_changed(callable_mp(this, &TextureRect::_texture_changed));
191 }
192
193 texture = p_tex;
194
195 if (texture.is_valid()) {
196 texture->connect_changed(callable_mp(this, &TextureRect::_texture_changed));
197 }
198
199 queue_redraw();
200 update_minimum_size();
201 }
202
203 Ref<Texture2D> TextureRect::get_texture() const {
204 return texture;
205 }
206
207 void TextureRect::set_expand_mode(ExpandMode p_mode) {
208 if (expand_mode == p_mode) {
209 return;
210 }
211
212 expand_mode = p_mode;
213 queue_redraw();
214 update_minimum_size();
215 }
216
217 TextureRect::ExpandMode TextureRect::get_expand_mode() const {
218 return expand_mode;
219 }
220
221 void TextureRect::set_stretch_mode(StretchMode p_mode) {
222 if (stretch_mode == p_mode) {
223 return;
224 }
225
226 stretch_mode = p_mode;
227 queue_redraw();
228 }
229
230 TextureRect::StretchMode TextureRect::get_stretch_mode() const {
231 return stretch_mode;
232 }
233
234 void TextureRect::set_flip_h(bool p_flip) {
235 if (hflip == p_flip) {
236 return;
237 }
238
239 hflip = p_flip;
240 queue_redraw();
241 }
242
243 bool TextureRect::is_flipped_h() const {
244 return hflip;
245 }
246
247 void TextureRect::set_flip_v(bool p_flip) {
248 if (vflip == p_flip) {
249 return;
250 }
251
252 vflip = p_flip;
253 queue_redraw();
254 }
255
256 bool TextureRect::is_flipped_v() const {
257 return vflip;
258 }
259
260 TextureRect::TextureRect() {
261 set_mouse_filter(MOUSE_FILTER_PASS);
262 }
263
264 TextureRect::~TextureRect() {
265 }