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