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