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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/*************************************************************************/
/* texture_button.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 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_button.h"
#include "core/typedefs.h"
#include <stdlib.h>
Size2 TextureButton::get_minimum_size() const {
Size2 rscale = Control::get_minimum_size();
if (!expand) {
if (normal.is_null()) {
if (pressed.is_null()) {
if (hover.is_null())
if (click_mask.is_null())
rscale = Size2();
else
rscale = click_mask->get_size();
else
rscale = hover->get_size();
} else
rscale = pressed->get_size();
} else
rscale = normal->get_size();
}
return rscale.abs();
}
bool TextureButton::has_point(const Point2 &p_point) const {
if (click_mask.is_valid()) {
Point2 point = p_point;
Rect2 rect = Rect2();
Size2 mask_size = click_mask->get_size();
if (_tile) {
// if the stretch mode is tile we offset the point to keep it inside the mask size
rect.size = mask_size;
if (_position_rect.has_point(point)) {
int cols = (int)Math::ceil(_position_rect.size.x / mask_size.x);
int rows = (int)Math::ceil(_position_rect.size.y / mask_size.y);
int col = (int)(point.x / mask_size.x) % cols;
int row = (int)(point.y / mask_size.y) % rows;
point.x -= mask_size.x * col;
point.y -= mask_size.y * row;
}
} else {
// we need to transform the point from our scaled / translated image back to our mask image
Point2 ofs = _position_rect.position;
Size2 scale = mask_size / _position_rect.size;
switch (stretch_mode) {
case STRETCH_KEEP_ASPECT_COVERED: {
// if the stretch mode is aspect covered the image uses a texture region so we need to take that into account
float min = MIN(scale.x, scale.y);
scale.x = min;
scale.y = min;
ofs -= _texture_region.position / min;
} break;
default: {
// FIXME: Why a switch if we only handle one enum value?
}
}
// offset and scale the new point position to adjust it to the bitmask size
point -= ofs;
point *= scale;
// finally, we need to check if the point is inside a rectangle with a position >= 0,0 and a size <= mask_size
rect.position = Point2(MAX(0, _texture_region.position.x), MAX(0, _texture_region.position.y));
rect.size = Size2(MIN(mask_size.x, _texture_region.size.x), MIN(mask_size.y, _texture_region.size.y));
}
if (!rect.has_point(point)) {
return false;
}
Point2i p = point;
return click_mask->get_bit(p);
}
return Control::has_point(p_point);
}
void TextureButton::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_DRAW: {
DrawMode draw_mode = get_draw_mode();
Ref<Texture> texdraw;
switch (draw_mode) {
case DRAW_NORMAL: {
if (normal.is_valid())
texdraw = normal;
} break;
case DRAW_HOVER_PRESSED:
case DRAW_PRESSED: {
if (pressed.is_null()) {
if (hover.is_null()) {
if (normal.is_valid())
texdraw = normal;
} else
texdraw = hover;
} else
texdraw = pressed;
} break;
case DRAW_HOVER: {
if (hover.is_null()) {
if (pressed.is_valid() && is_pressed())
texdraw = pressed;
else if (normal.is_valid())
texdraw = normal;
} else
texdraw = hover;
} break;
case DRAW_DISABLED: {
if (disabled.is_null()) {
if (normal.is_valid())
texdraw = normal;
} else
texdraw = disabled;
} break;
}
if (texdraw.is_valid()) {
Point2 ofs;
Size2 size = texdraw->get_size();
_texture_region = Rect2(Point2(), texdraw->get_size());
_tile = false;
if (expand) {
switch (stretch_mode) {
case STRETCH_KEEP:
size = texdraw->get_size();
break;
case STRETCH_SCALE:
size = get_size();
break;
case STRETCH_TILE:
size = get_size();
_tile = true;
break;
case STRETCH_KEEP_CENTERED:
ofs = (get_size() - texdraw->get_size()) / 2;
size = texdraw->get_size();
break;
case STRETCH_KEEP_ASPECT_CENTERED:
case STRETCH_KEEP_ASPECT: {
Size2 _size = get_size();
float tex_width = texdraw->get_width() * _size.height / texdraw->get_height();
float tex_height = _size.height;
if (tex_width > _size.width) {
tex_width = _size.width;
tex_height = texdraw->get_height() * tex_width / texdraw->get_width();
}
if (stretch_mode == STRETCH_KEEP_ASPECT_CENTERED) {
ofs.x = (_size.width - tex_width) / 2;
ofs.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 = texdraw->get_size();
Size2 scaleSize(size.width / tex_size.width, size.height / tex_size.height);
float scale = scaleSize.width > scaleSize.height ? scaleSize.width : scaleSize.height;
Size2 scaledTexSize = tex_size * scale;
Point2 ofs = ((scaledTexSize - size) / scale).abs() / 2.0f;
_texture_region = Rect2(ofs, size / scale);
} break;
}
}
_position_rect = Rect2(ofs, size);
if (_tile)
draw_texture_rect(texdraw, _position_rect, _tile);
else
draw_texture_rect_region(texdraw, _position_rect, _texture_region);
}
if (has_focus() && focused.is_valid()) {
Rect2 drect(Point2(), get_size());
draw_texture_rect(focused, drect, false);
};
} break;
}
}
void TextureButton::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_normal_texture", "texture"), &TextureButton::set_normal_texture);
ClassDB::bind_method(D_METHOD("set_pressed_texture", "texture"), &TextureButton::set_pressed_texture);
ClassDB::bind_method(D_METHOD("set_hover_texture", "texture"), &TextureButton::set_hover_texture);
ClassDB::bind_method(D_METHOD("set_disabled_texture", "texture"), &TextureButton::set_disabled_texture);
ClassDB::bind_method(D_METHOD("set_focused_texture", "texture"), &TextureButton::set_focused_texture);
ClassDB::bind_method(D_METHOD("set_click_mask", "mask"), &TextureButton::set_click_mask);
ClassDB::bind_method(D_METHOD("set_expand", "p_expand"), &TextureButton::set_expand);
ClassDB::bind_method(D_METHOD("set_stretch_mode", "p_mode"), &TextureButton::set_stretch_mode);
ClassDB::bind_method(D_METHOD("get_normal_texture"), &TextureButton::get_normal_texture);
ClassDB::bind_method(D_METHOD("get_pressed_texture"), &TextureButton::get_pressed_texture);
ClassDB::bind_method(D_METHOD("get_hover_texture"), &TextureButton::get_hover_texture);
ClassDB::bind_method(D_METHOD("get_disabled_texture"), &TextureButton::get_disabled_texture);
ClassDB::bind_method(D_METHOD("get_focused_texture"), &TextureButton::get_focused_texture);
ClassDB::bind_method(D_METHOD("get_click_mask"), &TextureButton::get_click_mask);
ClassDB::bind_method(D_METHOD("get_expand"), &TextureButton::get_expand);
ClassDB::bind_method(D_METHOD("get_stretch_mode"), &TextureButton::get_stretch_mode);
ADD_GROUP("Textures", "texture_");
ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_normal", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_normal_texture", "get_normal_texture");
ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_pressed", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_pressed_texture", "get_pressed_texture");
ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_hover", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_hover_texture", "get_hover_texture");
ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_disabled", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_disabled_texture", "get_disabled_texture");
ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_focused", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_focused_texture", "get_focused_texture");
ADD_PROPERTYNZ(PropertyInfo(Variant::OBJECT, "texture_click_mask", PROPERTY_HINT_RESOURCE_TYPE, "BitMap"), "set_click_mask", "get_click_mask");
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "expand", PROPERTY_HINT_RESOURCE_TYPE, "bool"), "set_expand", "get_expand");
ADD_PROPERTYNZ(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");
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 TextureButton::set_normal_texture(const Ref<Texture> &p_normal) {
normal = p_normal;
update();
minimum_size_changed();
}
void TextureButton::set_pressed_texture(const Ref<Texture> &p_pressed) {
pressed = p_pressed;
update();
}
void TextureButton::set_hover_texture(const Ref<Texture> &p_hover) {
hover = p_hover;
update();
}
void TextureButton::set_disabled_texture(const Ref<Texture> &p_disabled) {
disabled = p_disabled;
update();
}
void TextureButton::set_click_mask(const Ref<BitMap> &p_click_mask) {
click_mask = p_click_mask;
update();
}
Ref<Texture> TextureButton::get_normal_texture() const {
return normal;
}
Ref<Texture> TextureButton::get_pressed_texture() const {
return pressed;
}
Ref<Texture> TextureButton::get_hover_texture() const {
return hover;
}
Ref<Texture> TextureButton::get_disabled_texture() const {
return disabled;
}
Ref<BitMap> TextureButton::get_click_mask() const {
return click_mask;
}
Ref<Texture> TextureButton::get_focused_texture() const {
return focused;
};
void TextureButton::set_focused_texture(const Ref<Texture> &p_focused) {
focused = p_focused;
};
bool TextureButton::get_expand() const {
return expand;
}
void TextureButton::set_expand(bool p_expand) {
expand = p_expand;
minimum_size_changed();
update();
}
void TextureButton::set_stretch_mode(StretchMode p_stretch_mode) {
stretch_mode = p_stretch_mode;
update();
}
TextureButton::StretchMode TextureButton::get_stretch_mode() const {
return stretch_mode;
}
TextureButton::TextureButton() {
expand = false;
stretch_mode = STRETCH_SCALE;
_texture_region = Rect2();
_position_rect = Rect2();
_tile = false;
}
|