summaryrefslogtreecommitdiff
path: root/scene/gui/texture_rect.cpp
blob: ebf5ce597eecae9db567b088d00024f1eec0a576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*************************************************************************/
/*  texture_rect.cpp                                                     */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#include "texture_rect.h"
#include "core/core_string_names.h"
#include "servers/rendering_server.h"

void TextureRect::_notification(int p_what) {
	if (p_what == NOTIFICATION_DRAW) {
		if (texture.is_null()) {
			return;
		}

		Size2 size;
		Point2 offset;
		Rect2 region;
		bool tile = false;

		switch (stretch_mode) {
			case STRETCH_SCALE_ON_EXPAND: {
				size = expand ? get_size() : texture->get_size();
			} break;
			case STRETCH_SCALE: {
				size = get_size();
			} break;
			case STRETCH_TILE: {
				size = get_size();
				tile = true;
			} break;
			case STRETCH_KEEP: {
				size = texture->get_size();
			} break;
			case STRETCH_KEEP_CENTERED: {
				offset = (get_size() - texture->get_size()) / 2;
				size = texture->get_size();
			} break;
			case STRETCH_KEEP_ASPECT_CENTERED:
			case STRETCH_KEEP_ASPECT: {
				size = get_size();
				int tex_width = texture->get_width() * size.height / texture->get_height();
				int tex_height = size.height;

				if (tex_width > size.width) {
					tex_width = size.width;
					tex_height = texture->get_height() * tex_width / texture->get_width();
				}

				if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
					offset.x += (size.width - tex_width) / 2;
					offset.y += (size.height - tex_height) / 2;
				}

				size.width = tex_width;
				size.height = tex_height;
			} break;
			case STRETCH_KEEP_ASPECT_COVERED: {
				size = get_size();

				Size2 tex_size = texture->get_size();
				Size2 scale_size(size.width / tex_size.width, size.height / tex_size.height);
				float scale = scale_size.width > scale_size.height ? scale_size.width : scale_size.height;
				Size2 scaled_tex_size = tex_size * scale;

				region.position = ((scaled_tex_size - size) / scale).abs() / 2.0f;
				region.size = size / scale;
			} break;
		}

		Ref<AtlasTexture> p_atlas = texture;

		if (p_atlas.is_valid() && region.has_no_area()) {
			Size2 scale_size(size.width / texture->get_width(), size.height / texture->get_height());

			offset.width += hflip ? p_atlas->get_margin().get_position().width * scale_size.width * 2 : 0;
			offset.height += vflip ? p_atlas->get_margin().get_position().height * scale_size.height * 2 : 0;
		}

		size.width *= hflip ? -1.0f : 1.0f;
		size.height *= vflip ? -1.0f : 1.0f;

		if (region.has_no_area()) {
			draw_texture_rect(texture, Rect2(offset, size), tile);
		} else {
			draw_texture_rect_region(texture, Rect2(offset, size), region);
		}
	}
}

Size2 TextureRect::get_minimum_size() const {
	if (!expand && !texture.is_null()) {
		return texture->get_size();
	} else {
		return Size2();
	}
}

void TextureRect::_bind_methods() {
	ClassDB::bind_method(D_METHOD("set_texture", "texture"), &TextureRect::set_texture);
	ClassDB::bind_method(D_METHOD("get_texture"), &TextureRect::get_texture);
	ClassDB::bind_method(D_METHOD("set_expand", "enable"), &TextureRect::set_expand);
	ClassDB::bind_method(D_METHOD("has_expand"), &TextureRect::has_expand);
	ClassDB::bind_method(D_METHOD("set_flip_h", "enable"), &TextureRect::set_flip_h);
	ClassDB::bind_method(D_METHOD("is_flipped_h"), &TextureRect::is_flipped_h);
	ClassDB::bind_method(D_METHOD("set_flip_v", "enable"), &TextureRect::set_flip_v);
	ClassDB::bind_method(D_METHOD("is_flipped_v"), &TextureRect::is_flipped_v);
	ClassDB::bind_method(D_METHOD("set_stretch_mode", "stretch_mode"), &TextureRect::set_stretch_mode);
	ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureRect::get_stretch_mode);

	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand"), "set_expand", "has_expand");
	ADD_PROPERTY(PropertyInfo(Variant::INT, "stretch_mode", PROPERTY_HINT_ENUM, "Scale On Expand (Compat),Scale,Tile,Keep,Keep Centered,Keep Aspect,Keep Aspect Centered,Keep Aspect Covered"), "set_stretch_mode", "get_stretch_mode");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h");
	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v");

	BIND_ENUM_CONSTANT(STRETCH_SCALE_ON_EXPAND);
	BIND_ENUM_CONSTANT(STRETCH_SCALE);
	BIND_ENUM_CONSTANT(STRETCH_TILE);
	BIND_ENUM_CONSTANT(STRETCH_KEEP);
	BIND_ENUM_CONSTANT(STRETCH_KEEP_CENTERED);
	BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT);
	BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_CENTERED);
	BIND_ENUM_CONSTANT(STRETCH_KEEP_ASPECT_COVERED);
}

void TextureRect::_texture_changed() {
	if (texture.is_valid()) {
		update();
		update_minimum_size();
	}
}

void TextureRect::set_texture(const Ref<Texture2D> &p_tex) {
	if (p_tex == texture) {
		return;
	}

	if (texture.is_valid()) {
		texture->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TextureRect::_texture_changed));
	}

	texture = p_tex;

	if (texture.is_valid()) {
		texture->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &TextureRect::_texture_changed));
	}

	update();
	update_minimum_size();
}

Ref<Texture2D> TextureRect::get_texture() const {
	return texture;
}

void TextureRect::set_expand(bool p_expand) {
	expand = p_expand;
	update();
	update_minimum_size();
}

bool TextureRect::has_expand() const {
	return expand;
}

void TextureRect::set_stretch_mode(StretchMode p_mode) {
	stretch_mode = p_mode;
	update();
}

TextureRect::StretchMode TextureRect::get_stretch_mode() const {
	return stretch_mode;
}

void TextureRect::set_flip_h(bool p_flip) {
	hflip = p_flip;
	update();
}

bool TextureRect::is_flipped_h() const {
	return hflip;
}

void TextureRect::set_flip_v(bool p_flip) {
	vflip = p_flip;
	update();
}

bool TextureRect::is_flipped_v() const {
	return vflip;
}

TextureRect::TextureRect() {
	set_mouse_filter(MOUSE_FILTER_PASS);
}

TextureRect::~TextureRect() {
}