Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/button.cpp
9903 views
1
/**************************************************************************/
2
/* button.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 "button.h"
32
33
#include "scene/gui/dialogs.h"
34
35
#include "scene/theme/theme_db.h"
36
37
Size2 Button::get_minimum_size() const {
38
Ref<Texture2D> _icon = icon;
39
if (_icon.is_null() && has_theme_icon(SNAME("icon"))) {
40
_icon = theme_cache.icon;
41
}
42
43
return get_minimum_size_for_text_and_icon("", _icon);
44
}
45
46
void Button::_set_internal_margin(Side p_side, float p_value) {
47
_internal_margin[p_side] = p_value;
48
}
49
50
void Button::_queue_update_size_cache() {
51
}
52
53
String Button::_get_translated_text(const String &p_text) const {
54
return atr(p_text);
55
}
56
57
void Button::_update_theme_item_cache() {
58
Control::_update_theme_item_cache();
59
60
const bool rtl = is_layout_rtl();
61
if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
62
theme_cache.max_style_size = theme_cache.normal_mirrored->get_minimum_size();
63
theme_cache.style_margin_left = theme_cache.normal_mirrored->get_margin(SIDE_LEFT);
64
theme_cache.style_margin_right = theme_cache.normal_mirrored->get_margin(SIDE_RIGHT);
65
theme_cache.style_margin_top = theme_cache.normal_mirrored->get_margin(SIDE_TOP);
66
theme_cache.style_margin_bottom = theme_cache.normal_mirrored->get_margin(SIDE_BOTTOM);
67
} else {
68
theme_cache.max_style_size = theme_cache.normal->get_minimum_size();
69
theme_cache.style_margin_left = theme_cache.normal->get_margin(SIDE_LEFT);
70
theme_cache.style_margin_right = theme_cache.normal->get_margin(SIDE_RIGHT);
71
theme_cache.style_margin_top = theme_cache.normal->get_margin(SIDE_TOP);
72
theme_cache.style_margin_bottom = theme_cache.normal->get_margin(SIDE_BOTTOM);
73
}
74
if (has_theme_stylebox("hover_pressed")) {
75
if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
76
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed_mirrored->get_minimum_size());
77
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed_mirrored->get_margin(SIDE_LEFT));
78
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed_mirrored->get_margin(SIDE_RIGHT));
79
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed_mirrored->get_margin(SIDE_TOP));
80
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed_mirrored->get_margin(SIDE_BOTTOM));
81
} else {
82
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_pressed->get_minimum_size());
83
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_pressed->get_margin(SIDE_LEFT));
84
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_pressed->get_margin(SIDE_RIGHT));
85
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_pressed->get_margin(SIDE_TOP));
86
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_pressed->get_margin(SIDE_BOTTOM));
87
}
88
}
89
if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
90
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed_mirrored->get_minimum_size());
91
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed_mirrored->get_margin(SIDE_LEFT));
92
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed_mirrored->get_margin(SIDE_RIGHT));
93
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed_mirrored->get_margin(SIDE_TOP));
94
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed_mirrored->get_margin(SIDE_BOTTOM));
95
} else {
96
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.pressed->get_minimum_size());
97
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.pressed->get_margin(SIDE_LEFT));
98
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.pressed->get_margin(SIDE_RIGHT));
99
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.pressed->get_margin(SIDE_TOP));
100
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.pressed->get_margin(SIDE_BOTTOM));
101
}
102
if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
103
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover_mirrored->get_minimum_size());
104
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover_mirrored->get_margin(SIDE_LEFT));
105
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover_mirrored->get_margin(SIDE_RIGHT));
106
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover_mirrored->get_margin(SIDE_TOP));
107
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover_mirrored->get_margin(SIDE_BOTTOM));
108
} else {
109
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.hover->get_minimum_size());
110
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.hover->get_margin(SIDE_LEFT));
111
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.hover->get_margin(SIDE_RIGHT));
112
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.hover->get_margin(SIDE_TOP));
113
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.hover->get_margin(SIDE_BOTTOM));
114
}
115
if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
116
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled_mirrored->get_minimum_size());
117
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled_mirrored->get_margin(SIDE_LEFT));
118
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled_mirrored->get_margin(SIDE_RIGHT));
119
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled_mirrored->get_margin(SIDE_TOP));
120
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled_mirrored->get_margin(SIDE_BOTTOM));
121
} else {
122
theme_cache.max_style_size = theme_cache.max_style_size.max(theme_cache.disabled->get_minimum_size());
123
theme_cache.style_margin_left = MAX(theme_cache.style_margin_left, theme_cache.disabled->get_margin(SIDE_LEFT));
124
theme_cache.style_margin_right = MAX(theme_cache.style_margin_right, theme_cache.disabled->get_margin(SIDE_RIGHT));
125
theme_cache.style_margin_top = MAX(theme_cache.style_margin_top, theme_cache.disabled->get_margin(SIDE_TOP));
126
theme_cache.style_margin_bottom = MAX(theme_cache.style_margin_bottom, theme_cache.disabled->get_margin(SIDE_BOTTOM));
127
}
128
theme_cache.max_style_size = theme_cache.max_style_size.max(Vector2(theme_cache.style_margin_left + theme_cache.style_margin_right, theme_cache.style_margin_top + theme_cache.style_margin_bottom));
129
}
130
131
Size2 Button::_get_largest_stylebox_size() const {
132
return theme_cache.max_style_size;
133
}
134
135
Ref<StyleBox> Button::_get_current_stylebox() const {
136
Ref<StyleBox> stylebox = theme_cache.normal;
137
const bool rtl = is_layout_rtl();
138
139
switch (get_draw_mode()) {
140
case DRAW_NORMAL: {
141
if (rtl && has_theme_stylebox(SNAME("normal_mirrored"))) {
142
stylebox = theme_cache.normal_mirrored;
143
} else {
144
stylebox = theme_cache.normal;
145
}
146
} break;
147
148
case DRAW_HOVER_PRESSED: {
149
// Edge case for CheckButton and CheckBox.
150
if (has_theme_stylebox("hover_pressed")) {
151
if (rtl && has_theme_stylebox(SNAME("hover_pressed_mirrored"))) {
152
stylebox = theme_cache.hover_pressed_mirrored;
153
} else {
154
stylebox = theme_cache.hover_pressed;
155
}
156
break;
157
}
158
}
159
[[fallthrough]];
160
case DRAW_PRESSED: {
161
if (rtl && has_theme_stylebox(SNAME("pressed_mirrored"))) {
162
stylebox = theme_cache.pressed_mirrored;
163
} else {
164
stylebox = theme_cache.pressed;
165
}
166
} break;
167
168
case DRAW_HOVER: {
169
if (rtl && has_theme_stylebox(SNAME("hover_mirrored"))) {
170
stylebox = theme_cache.hover_mirrored;
171
} else {
172
stylebox = theme_cache.hover;
173
}
174
} break;
175
176
case DRAW_DISABLED: {
177
if (rtl && has_theme_stylebox(SNAME("disabled_mirrored"))) {
178
stylebox = theme_cache.disabled_mirrored;
179
} else {
180
stylebox = theme_cache.disabled;
181
}
182
} break;
183
}
184
185
return stylebox;
186
}
187
188
void Button::_notification(int p_what) {
189
switch (p_what) {
190
case NOTIFICATION_ACCESSIBILITY_UPDATE: {
191
RID ae = get_accessibility_element();
192
ERR_FAIL_COND(ae.is_null());
193
194
const String &ac_name = get_accessibility_name();
195
if (!xl_text.is_empty() && ac_name.is_empty()) {
196
DisplayServer::get_singleton()->accessibility_update_set_name(ae, xl_text);
197
} else if (!xl_text.is_empty() && !ac_name.is_empty() && ac_name != xl_text) {
198
DisplayServer::get_singleton()->accessibility_update_set_name(ae, ac_name + ": " + xl_text);
199
} else if (xl_text.is_empty() && ac_name.is_empty() && !get_tooltip_text().is_empty()) {
200
DisplayServer::get_singleton()->accessibility_update_set_name(ae, get_tooltip_text()); // Fall back to tooltip.
201
}
202
AcceptDialog *dlg = Object::cast_to<AcceptDialog>(get_parent());
203
if (dlg && dlg->get_ok_button() == this) {
204
DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_DEFAULT_BUTTON);
205
}
206
} break;
207
208
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
209
queue_redraw();
210
} break;
211
212
case NOTIFICATION_TRANSLATION_CHANGED: {
213
xl_text = _get_translated_text(text);
214
_shape();
215
216
update_minimum_size();
217
queue_accessibility_update();
218
queue_redraw();
219
} break;
220
221
case NOTIFICATION_THEME_CHANGED: {
222
_shape();
223
224
update_minimum_size();
225
queue_redraw();
226
} break;
227
228
case NOTIFICATION_RESIZED: {
229
if (autowrap_mode != TextServer::AUTOWRAP_OFF) {
230
_shape();
231
232
update_minimum_size();
233
queue_redraw();
234
}
235
} break;
236
237
case NOTIFICATION_DRAW: {
238
// Reshape and update size min. if text is invalidated by an external source (e.g., oversampling).
239
if (text_buf.is_valid() && !TS->shaped_text_is_ready(text_buf->get_rid())) {
240
_shape();
241
242
update_minimum_size();
243
}
244
245
const RID ci = get_canvas_item();
246
const Size2 size = get_size();
247
248
Ref<StyleBox> style = _get_current_stylebox();
249
// Draws the stylebox in the current state.
250
if (!flat) {
251
style->draw(ci, Rect2(Point2(), size));
252
}
253
254
if (has_focus()) {
255
theme_cache.focus->draw(ci, Rect2(Point2(), size));
256
}
257
258
Ref<Texture2D> _icon = icon;
259
if (_icon.is_null() && has_theme_icon(SNAME("icon"))) {
260
_icon = theme_cache.icon;
261
}
262
263
if (xl_text.is_empty() && _icon.is_null()) {
264
break;
265
}
266
267
const float style_margin_left = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_left : style->get_margin(SIDE_LEFT);
268
const float style_margin_right = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_right : style->get_margin(SIDE_RIGHT);
269
const float style_margin_top = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_top : style->get_margin(SIDE_TOP);
270
const float style_margin_bottom = (theme_cache.align_to_largest_stylebox) ? theme_cache.style_margin_bottom : style->get_margin(SIDE_BOTTOM);
271
272
Size2 drawable_size_remained = size;
273
274
{ // The size after the stelybox is stripped.
275
drawable_size_remained.width -= style_margin_left + style_margin_right;
276
drawable_size_remained.height -= style_margin_top + style_margin_bottom;
277
}
278
279
const int h_separation = MAX(0, theme_cache.h_separation);
280
281
float left_internal_margin_with_h_separation = _internal_margin[SIDE_LEFT];
282
float right_internal_margin_with_h_separation = _internal_margin[SIDE_RIGHT];
283
{ // The width reserved for internal element in derived classes (and h_separation if needed).
284
285
if (_internal_margin[SIDE_LEFT] > 0.0f) {
286
left_internal_margin_with_h_separation += h_separation;
287
}
288
289
if (_internal_margin[SIDE_RIGHT] > 0.0f) {
290
right_internal_margin_with_h_separation += h_separation;
291
}
292
293
drawable_size_remained.width -= left_internal_margin_with_h_separation + right_internal_margin_with_h_separation; // The size after the internal element is stripped.
294
}
295
296
HorizontalAlignment icon_align_rtl_checked = horizontal_icon_alignment;
297
HorizontalAlignment align_rtl_checked = alignment;
298
// Swap icon and text alignment sides if right-to-left layout is set.
299
if (is_layout_rtl()) {
300
if (horizontal_icon_alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
301
icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT;
302
} else if (horizontal_icon_alignment == HORIZONTAL_ALIGNMENT_LEFT) {
303
icon_align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT;
304
}
305
if (alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
306
align_rtl_checked = HORIZONTAL_ALIGNMENT_LEFT;
307
} else if (alignment == HORIZONTAL_ALIGNMENT_LEFT) {
308
align_rtl_checked = HORIZONTAL_ALIGNMENT_RIGHT;
309
}
310
}
311
312
Color font_color;
313
Color icon_modulate_color(1, 1, 1, 1);
314
// Get the font color and icon modulate color in the current state.
315
switch (get_draw_mode()) {
316
case DRAW_NORMAL: {
317
// Focus colors only take precedence over normal state.
318
if (has_focus()) {
319
font_color = theme_cache.font_focus_color;
320
if (has_theme_color(SNAME("icon_focus_color"))) {
321
icon_modulate_color = theme_cache.icon_focus_color;
322
}
323
} else {
324
font_color = theme_cache.font_color;
325
if (has_theme_color(SNAME("icon_normal_color"))) {
326
icon_modulate_color = theme_cache.icon_normal_color;
327
}
328
}
329
} break;
330
case DRAW_HOVER_PRESSED: {
331
font_color = theme_cache.font_hover_pressed_color;
332
if (has_theme_color(SNAME("icon_hover_pressed_color"))) {
333
icon_modulate_color = theme_cache.icon_hover_pressed_color;
334
}
335
336
} break;
337
case DRAW_PRESSED: {
338
if (has_theme_color(SNAME("font_pressed_color"))) {
339
font_color = theme_cache.font_pressed_color;
340
} else {
341
font_color = theme_cache.font_color;
342
}
343
if (has_theme_color(SNAME("icon_pressed_color"))) {
344
icon_modulate_color = theme_cache.icon_pressed_color;
345
}
346
347
} break;
348
case DRAW_HOVER: {
349
font_color = theme_cache.font_hover_color;
350
if (has_theme_color(SNAME("icon_hover_color"))) {
351
icon_modulate_color = theme_cache.icon_hover_color;
352
}
353
354
} break;
355
case DRAW_DISABLED: {
356
font_color = theme_cache.font_disabled_color;
357
if (has_theme_color(SNAME("icon_disabled_color"))) {
358
icon_modulate_color = theme_cache.icon_disabled_color;
359
} else {
360
icon_modulate_color.a = 0.4;
361
}
362
363
} break;
364
}
365
366
const bool is_clipped = clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING || autowrap_mode != TextServer::AUTOWRAP_OFF;
367
const Size2 custom_element_size = drawable_size_remained;
368
369
// Draw the icon.
370
if (_icon.is_valid()) {
371
Size2 icon_size;
372
373
{ // Calculate the drawing size of the icon.
374
icon_size = _icon->get_size();
375
376
if (expand_icon) {
377
const Size2 text_buf_size = text_buf->get_size();
378
Size2 _size = custom_element_size;
379
if (!is_clipped && icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_CENTER && text_buf_size.width > 0.0f) {
380
// If there is not enough space for icon and h_separation, h_separation will occupy the space first,
381
// so the icon's width may be negative. Keep it negative to make it easier to calculate the space
382
// reserved for text later.
383
_size.width -= text_buf_size.width + h_separation;
384
}
385
if (vertical_icon_alignment != VERTICAL_ALIGNMENT_CENTER) {
386
_size.height -= text_buf_size.height;
387
}
388
389
float icon_width = icon_size.width * _size.height / icon_size.height;
390
float icon_height = _size.height;
391
392
if (icon_width > _size.width) {
393
icon_width = _size.width;
394
icon_height = icon_size.height * icon_width / icon_size.width;
395
}
396
397
icon_size = Size2(icon_width, icon_height);
398
}
399
icon_size = _fit_icon_size(icon_size);
400
icon_size = icon_size.round();
401
}
402
403
if (icon_size.width > 0.0f) {
404
// Calculate the drawing position of the icon.
405
Point2 icon_ofs;
406
407
switch (icon_align_rtl_checked) {
408
case HORIZONTAL_ALIGNMENT_CENTER: {
409
icon_ofs.x = (custom_element_size.width - icon_size.width) / 2.0f;
410
}
411
[[fallthrough]];
412
case HORIZONTAL_ALIGNMENT_FILL:
413
case HORIZONTAL_ALIGNMENT_LEFT: {
414
icon_ofs.x += style_margin_left;
415
icon_ofs.x += left_internal_margin_with_h_separation;
416
} break;
417
418
case HORIZONTAL_ALIGNMENT_RIGHT: {
419
icon_ofs.x = size.x - style_margin_right;
420
icon_ofs.x -= right_internal_margin_with_h_separation;
421
icon_ofs.x -= icon_size.width;
422
} break;
423
}
424
425
switch (vertical_icon_alignment) {
426
case VERTICAL_ALIGNMENT_CENTER: {
427
icon_ofs.y = (custom_element_size.height - icon_size.height) / 2.0f;
428
}
429
[[fallthrough]];
430
case VERTICAL_ALIGNMENT_FILL:
431
case VERTICAL_ALIGNMENT_TOP: {
432
icon_ofs.y += style_margin_top;
433
} break;
434
435
case VERTICAL_ALIGNMENT_BOTTOM: {
436
icon_ofs.y = size.y - style_margin_bottom - icon_size.height;
437
} break;
438
}
439
icon_ofs = icon_ofs.floor();
440
441
Rect2 icon_region = Rect2(icon_ofs, icon_size);
442
draw_texture_rect(_icon, icon_region, false, icon_modulate_color);
443
}
444
445
if (!xl_text.is_empty()) {
446
// Update the size after the icon is stripped. Stripping only when the icon alignments are not center.
447
if (icon_align_rtl_checked != HORIZONTAL_ALIGNMENT_CENTER) {
448
// Subtract the space's width occupied by icon and h_separation together.
449
drawable_size_remained.width -= icon_size.width + h_separation;
450
}
451
452
if (vertical_icon_alignment != VERTICAL_ALIGNMENT_CENTER) {
453
drawable_size_remained.height -= icon_size.height;
454
}
455
}
456
}
457
458
// Draw the text.
459
if (!xl_text.is_empty()) {
460
text_buf->set_alignment(align_rtl_checked);
461
462
float text_buf_width = Math::ceil(MAX(1.0f, drawable_size_remained.width)); // The space's width filled by the text_buf.
463
if (autowrap_mode != TextServer::AUTOWRAP_OFF && !Math::is_equal_approx(text_buf_width, text_buf->get_width())) {
464
update_minimum_size();
465
}
466
text_buf->set_width(text_buf_width);
467
468
Point2 text_ofs;
469
470
switch (align_rtl_checked) {
471
case HORIZONTAL_ALIGNMENT_CENTER: {
472
text_ofs.x = (drawable_size_remained.width - text_buf_width) / 2.0f;
473
}
474
[[fallthrough]];
475
case HORIZONTAL_ALIGNMENT_FILL:
476
case HORIZONTAL_ALIGNMENT_LEFT:
477
case HORIZONTAL_ALIGNMENT_RIGHT: {
478
text_ofs.x += style_margin_left;
479
text_ofs.x += left_internal_margin_with_h_separation;
480
if (icon_align_rtl_checked == HORIZONTAL_ALIGNMENT_LEFT) {
481
// Offset by the space's width that occupied by icon and h_separation together.
482
text_ofs.x += custom_element_size.width - drawable_size_remained.width;
483
}
484
} break;
485
}
486
487
text_ofs.y = (drawable_size_remained.height - text_buf->get_size().height) / 2.0f + style_margin_top;
488
if (vertical_icon_alignment == VERTICAL_ALIGNMENT_TOP) {
489
text_ofs.y += custom_element_size.height - drawable_size_remained.height; // Offset by the icon's height.
490
}
491
492
Color font_outline_color = theme_cache.font_outline_color;
493
int outline_size = theme_cache.outline_size;
494
if (outline_size > 0 && font_outline_color.a > 0.0f) {
495
text_buf->draw_outline(ci, text_ofs, outline_size, font_outline_color);
496
}
497
text_buf->draw(ci, text_ofs, font_color);
498
}
499
} break;
500
}
501
}
502
503
Size2 Button::_fit_icon_size(const Size2 &p_size) const {
504
int max_width = theme_cache.icon_max_width;
505
Size2 icon_size = p_size;
506
507
if (max_width > 0 && icon_size.width > max_width) {
508
icon_size.height = icon_size.height * max_width / icon_size.width;
509
icon_size.width = max_width;
510
}
511
512
return icon_size;
513
}
514
515
Size2 Button::get_minimum_size_for_text_and_icon(const String &p_text, Ref<Texture2D> p_icon) const {
516
// Do not include `_internal_margin`, it's already added in the `get_minimum_size` overrides.
517
518
Ref<TextParagraph> paragraph;
519
if (p_text.is_empty()) {
520
paragraph = text_buf;
521
} else {
522
paragraph.instantiate();
523
_shape(paragraph, p_text);
524
}
525
526
Size2 minsize = paragraph->get_size();
527
if (clip_text || overrun_behavior != TextServer::OVERRUN_NO_TRIMMING || autowrap_mode != TextServer::AUTOWRAP_OFF) {
528
minsize.width = 0;
529
}
530
531
if (!expand_icon && p_icon.is_valid()) {
532
Size2 icon_size = _fit_icon_size(p_icon->get_size());
533
if (vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER) {
534
minsize.height = MAX(minsize.height, icon_size.height);
535
} else {
536
minsize.height += icon_size.height;
537
}
538
539
if (horizontal_icon_alignment != HORIZONTAL_ALIGNMENT_CENTER) {
540
minsize.width += icon_size.width;
541
if (!xl_text.is_empty() || !p_text.is_empty()) {
542
minsize.width += MAX(0, theme_cache.h_separation);
543
}
544
} else {
545
minsize.width = MAX(minsize.width, icon_size.width);
546
}
547
}
548
549
if (!xl_text.is_empty() || !p_text.is_empty()) {
550
Ref<Font> font = theme_cache.font;
551
float font_height = font->get_height(theme_cache.font_size);
552
if (vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER) {
553
minsize.height = MAX(font_height, minsize.height);
554
} else {
555
minsize.height += font_height;
556
}
557
}
558
559
return (theme_cache.align_to_largest_stylebox ? _get_largest_stylebox_size() : _get_current_stylebox()->get_minimum_size()) + minsize;
560
}
561
562
void Button::_shape(Ref<TextParagraph> p_paragraph, String p_text) const {
563
if (p_paragraph.is_null()) {
564
p_paragraph = text_buf;
565
}
566
567
if (p_text.is_empty()) {
568
p_text = xl_text;
569
}
570
571
p_paragraph->clear();
572
573
Ref<Font> font = theme_cache.font;
574
int font_size = theme_cache.font_size;
575
if (font.is_null() || font_size == 0) {
576
// Can't shape without a valid font and a non-zero size.
577
return;
578
}
579
580
BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY;
581
switch (autowrap_mode) {
582
case TextServer::AUTOWRAP_WORD_SMART:
583
autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY;
584
break;
585
case TextServer::AUTOWRAP_WORD:
586
autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY;
587
break;
588
case TextServer::AUTOWRAP_ARBITRARY:
589
autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY;
590
break;
591
case TextServer::AUTOWRAP_OFF:
592
break;
593
}
594
autowrap_flags = autowrap_flags | autowrap_flags_trim;
595
p_paragraph->set_break_flags(autowrap_flags);
596
p_paragraph->set_line_spacing(theme_cache.line_spacing);
597
598
if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
599
p_paragraph->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
600
} else {
601
p_paragraph->set_direction((TextServer::Direction)text_direction);
602
}
603
p_paragraph->add_string(p_text, font, font_size, language);
604
p_paragraph->set_text_overrun_behavior(overrun_behavior);
605
}
606
607
void Button::set_text_overrun_behavior(TextServer::OverrunBehavior p_behavior) {
608
if (overrun_behavior != p_behavior) {
609
bool need_update_cache = overrun_behavior == TextServer::OVERRUN_NO_TRIMMING || p_behavior == TextServer::OVERRUN_NO_TRIMMING;
610
overrun_behavior = p_behavior;
611
_shape();
612
613
if (need_update_cache) {
614
_queue_update_size_cache();
615
}
616
queue_redraw();
617
update_minimum_size();
618
}
619
}
620
621
TextServer::OverrunBehavior Button::get_text_overrun_behavior() const {
622
return overrun_behavior;
623
}
624
625
void Button::set_text(const String &p_text) {
626
const String translated_text = _get_translated_text(p_text);
627
if (text == p_text && xl_text == translated_text) {
628
return;
629
}
630
text = p_text;
631
xl_text = translated_text;
632
_shape();
633
634
queue_accessibility_update();
635
queue_redraw();
636
update_minimum_size();
637
}
638
639
String Button::get_text() const {
640
return text;
641
}
642
643
void Button::set_autowrap_mode(TextServer::AutowrapMode p_mode) {
644
if (autowrap_mode != p_mode) {
645
autowrap_mode = p_mode;
646
_shape();
647
queue_redraw();
648
update_minimum_size();
649
}
650
}
651
652
TextServer::AutowrapMode Button::get_autowrap_mode() const {
653
return autowrap_mode;
654
}
655
656
void Button::set_autowrap_trim_flags(BitField<TextServer::LineBreakFlag> p_flags) {
657
if (autowrap_flags_trim != (p_flags & TextServer::BREAK_TRIM_MASK)) {
658
autowrap_flags_trim = p_flags & TextServer::BREAK_TRIM_MASK;
659
_shape();
660
queue_redraw();
661
update_minimum_size();
662
}
663
}
664
665
BitField<TextServer::LineBreakFlag> Button::get_autowrap_trim_flags() const {
666
return autowrap_flags_trim;
667
}
668
669
void Button::set_text_direction(Control::TextDirection p_text_direction) {
670
ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
671
if (text_direction != p_text_direction) {
672
text_direction = p_text_direction;
673
_shape();
674
queue_accessibility_update();
675
queue_redraw();
676
}
677
}
678
679
Control::TextDirection Button::get_text_direction() const {
680
return text_direction;
681
}
682
683
void Button::set_language(const String &p_language) {
684
if (language != p_language) {
685
language = p_language;
686
_shape();
687
queue_accessibility_update();
688
queue_redraw();
689
}
690
}
691
692
String Button::get_language() const {
693
return language;
694
}
695
696
void Button::set_button_icon(const Ref<Texture2D> &p_icon) {
697
if (icon == p_icon) {
698
return;
699
}
700
701
if (icon.is_valid()) {
702
icon->disconnect_changed(callable_mp(this, &Button::_texture_changed));
703
}
704
705
icon = p_icon;
706
707
if (icon.is_valid()) {
708
icon->connect_changed(callable_mp(this, &Button::_texture_changed));
709
}
710
711
queue_redraw();
712
update_minimum_size();
713
}
714
715
void Button::_texture_changed() {
716
queue_redraw();
717
update_minimum_size();
718
}
719
720
Ref<Texture2D> Button::get_button_icon() const {
721
return icon;
722
}
723
724
void Button::set_expand_icon(bool p_enabled) {
725
if (expand_icon != p_enabled) {
726
expand_icon = p_enabled;
727
_queue_update_size_cache();
728
queue_redraw();
729
update_minimum_size();
730
}
731
}
732
733
bool Button::is_expand_icon() const {
734
return expand_icon;
735
}
736
737
void Button::set_flat(bool p_enabled) {
738
if (flat != p_enabled) {
739
flat = p_enabled;
740
queue_redraw();
741
}
742
}
743
744
bool Button::is_flat() const {
745
return flat;
746
}
747
748
void Button::set_clip_text(bool p_enabled) {
749
if (clip_text != p_enabled) {
750
clip_text = p_enabled;
751
752
_queue_update_size_cache();
753
queue_redraw();
754
update_minimum_size();
755
}
756
}
757
758
bool Button::get_clip_text() const {
759
return clip_text;
760
}
761
762
void Button::set_text_alignment(HorizontalAlignment p_alignment) {
763
if (alignment != p_alignment) {
764
alignment = p_alignment;
765
queue_accessibility_update();
766
queue_redraw();
767
}
768
}
769
770
HorizontalAlignment Button::get_text_alignment() const {
771
return alignment;
772
}
773
774
void Button::set_icon_alignment(HorizontalAlignment p_alignment) {
775
if (horizontal_icon_alignment == p_alignment) {
776
return;
777
}
778
779
horizontal_icon_alignment = p_alignment;
780
update_minimum_size();
781
queue_redraw();
782
}
783
784
void Button::set_vertical_icon_alignment(VerticalAlignment p_alignment) {
785
if (vertical_icon_alignment == p_alignment) {
786
return;
787
}
788
bool need_update_cache = vertical_icon_alignment == VERTICAL_ALIGNMENT_CENTER || p_alignment == VERTICAL_ALIGNMENT_CENTER;
789
vertical_icon_alignment = p_alignment;
790
791
if (need_update_cache) {
792
_queue_update_size_cache();
793
}
794
update_minimum_size();
795
queue_redraw();
796
}
797
798
HorizontalAlignment Button::get_icon_alignment() const {
799
return horizontal_icon_alignment;
800
}
801
802
VerticalAlignment Button::get_vertical_icon_alignment() const {
803
return vertical_icon_alignment;
804
}
805
806
void Button::_bind_methods() {
807
ClassDB::bind_method(D_METHOD("set_text", "text"), &Button::set_text);
808
ClassDB::bind_method(D_METHOD("get_text"), &Button::get_text);
809
ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &Button::set_text_overrun_behavior);
810
ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &Button::get_text_overrun_behavior);
811
ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &Button::set_autowrap_mode);
812
ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &Button::get_autowrap_mode);
813
ClassDB::bind_method(D_METHOD("set_autowrap_trim_flags", "autowrap_trim_flags"), &Button::set_autowrap_trim_flags);
814
ClassDB::bind_method(D_METHOD("get_autowrap_trim_flags"), &Button::get_autowrap_trim_flags);
815
ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &Button::set_text_direction);
816
ClassDB::bind_method(D_METHOD("get_text_direction"), &Button::get_text_direction);
817
ClassDB::bind_method(D_METHOD("set_language", "language"), &Button::set_language);
818
ClassDB::bind_method(D_METHOD("get_language"), &Button::get_language);
819
ClassDB::bind_method(D_METHOD("set_button_icon", "texture"), &Button::set_button_icon);
820
ClassDB::bind_method(D_METHOD("get_button_icon"), &Button::get_button_icon);
821
ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &Button::set_flat);
822
ClassDB::bind_method(D_METHOD("is_flat"), &Button::is_flat);
823
ClassDB::bind_method(D_METHOD("set_clip_text", "enabled"), &Button::set_clip_text);
824
ClassDB::bind_method(D_METHOD("get_clip_text"), &Button::get_clip_text);
825
ClassDB::bind_method(D_METHOD("set_text_alignment", "alignment"), &Button::set_text_alignment);
826
ClassDB::bind_method(D_METHOD("get_text_alignment"), &Button::get_text_alignment);
827
ClassDB::bind_method(D_METHOD("set_icon_alignment", "icon_alignment"), &Button::set_icon_alignment);
828
ClassDB::bind_method(D_METHOD("get_icon_alignment"), &Button::get_icon_alignment);
829
ClassDB::bind_method(D_METHOD("set_vertical_icon_alignment", "vertical_icon_alignment"), &Button::set_vertical_icon_alignment);
830
ClassDB::bind_method(D_METHOD("get_vertical_icon_alignment"), &Button::get_vertical_icon_alignment);
831
ClassDB::bind_method(D_METHOD("set_expand_icon", "enabled"), &Button::set_expand_icon);
832
ClassDB::bind_method(D_METHOD("is_expand_icon"), &Button::is_expand_icon);
833
834
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
835
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_button_icon", "get_button_icon");
836
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
837
838
ADD_GROUP("Text Behavior", "");
839
ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_text_alignment", "get_text_alignment");
840
ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis (6+ Characters),Word Ellipsis (6+ Characters),Ellipsis (Always),Word Ellipsis (Always)"), "set_text_overrun_behavior", "get_text_overrun_behavior");
841
ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Off,Arbitrary,Word,Word (Smart)"), "set_autowrap_mode", "get_autowrap_mode");
842
ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_trim_flags", PROPERTY_HINT_FLAGS, vformat("Trim Spaces After Break:%d,Trim Spaces Before Break:%d", TextServer::BREAK_TRIM_START_EDGE_SPACES, TextServer::BREAK_TRIM_END_EDGE_SPACES)), "set_autowrap_trim_flags", "get_autowrap_trim_flags");
843
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_text"), "set_clip_text", "get_clip_text");
844
845
ADD_GROUP("Icon Behavior", "");
846
ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_icon_alignment", "get_icon_alignment");
847
ADD_PROPERTY(PropertyInfo(Variant::INT, "vertical_icon_alignment", PROPERTY_HINT_ENUM, "Top,Center,Bottom"), "set_vertical_icon_alignment", "get_vertical_icon_alignment");
848
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_icon"), "set_expand_icon", "is_expand_icon");
849
850
ADD_GROUP("BiDi", "");
851
ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
852
ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
853
854
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, normal);
855
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, normal_mirrored);
856
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, pressed);
857
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, pressed_mirrored);
858
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover);
859
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover_mirrored);
860
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover_pressed);
861
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, hover_pressed_mirrored);
862
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, disabled);
863
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, disabled_mirrored);
864
BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, Button, focus);
865
866
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_color);
867
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_focus_color);
868
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_pressed_color);
869
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_hover_color);
870
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_hover_pressed_color);
871
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_disabled_color);
872
873
BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, Button, font);
874
BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, Button, font_size);
875
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, outline_size);
876
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, font_outline_color);
877
878
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_normal_color);
879
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_focus_color);
880
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_pressed_color);
881
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_hover_color);
882
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_hover_pressed_color);
883
BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, Button, icon_disabled_color);
884
885
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, Button, icon);
886
887
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, h_separation);
888
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, icon_max_width);
889
890
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, align_to_largest_stylebox);
891
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, Button, line_spacing);
892
}
893
894
Button::Button(const String &p_text) {
895
text_buf.instantiate();
896
text_buf->set_break_flags(TextServer::BREAK_MANDATORY | autowrap_flags_trim);
897
set_mouse_filter(MOUSE_FILTER_STOP);
898
899
set_text(p_text);
900
}
901
902
Button::~Button() {
903
}
904
905