Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/foldable_container.cpp
9902 views
1
/**************************************************************************/
2
/* foldable_container.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 "foldable_container.h"
32
33
#include "scene/resources/text_line.h"
34
#include "scene/theme/theme_db.h"
35
36
Size2 FoldableContainer::get_minimum_size() const {
37
_update_title_min_size();
38
39
if (folded) {
40
return title_minimum_size;
41
}
42
Size2 ms;
43
44
for (int i = 0; i < get_child_count(); i++) {
45
Control *c = as_sortable_control(get_child(i));
46
if (!c) {
47
continue;
48
}
49
ms = ms.max(c->get_combined_minimum_size());
50
}
51
ms += theme_cache.panel_style->get_minimum_size();
52
53
return Size2(MAX(ms.width, title_minimum_size.width), ms.height + title_minimum_size.height);
54
}
55
56
void FoldableContainer::fold() {
57
set_folded(true);
58
emit_signal(SNAME("folding_changed"), folded);
59
}
60
61
void FoldableContainer::expand() {
62
set_folded(false);
63
emit_signal(SNAME("folding_changed"), folded);
64
}
65
66
void FoldableContainer::set_folded(bool p_folded) {
67
if (folded != p_folded) {
68
if (!changing_group && foldable_group.is_valid()) {
69
if (!p_folded) {
70
_update_group();
71
foldable_group->emit_signal(SNAME("expanded"), this);
72
} else if (!foldable_group->updating_group && foldable_group->get_expanded_container() == this && !foldable_group->is_allow_folding_all()) {
73
return;
74
}
75
}
76
folded = p_folded;
77
78
update_minimum_size();
79
queue_sort();
80
queue_redraw();
81
}
82
}
83
84
bool FoldableContainer::is_folded() const {
85
return folded;
86
}
87
88
void FoldableContainer::set_foldable_group(const Ref<FoldableGroup> &p_group) {
89
if (foldable_group.is_valid()) {
90
foldable_group->containers.erase(this);
91
}
92
93
foldable_group = p_group;
94
95
if (foldable_group.is_valid()) {
96
changing_group = true;
97
if (folded && !foldable_group->get_expanded_container() && !foldable_group->is_allow_folding_all()) {
98
set_folded(false);
99
} else if (!folded && foldable_group->get_expanded_container()) {
100
set_folded(true);
101
}
102
foldable_group->containers.insert(this);
103
changing_group = false;
104
}
105
106
queue_redraw();
107
}
108
109
Ref<FoldableGroup> FoldableContainer::get_foldable_group() const {
110
return foldable_group;
111
}
112
113
void FoldableContainer::set_title(const String &p_text) {
114
if (title == p_text) {
115
return;
116
}
117
title = p_text;
118
_shape();
119
update_minimum_size();
120
queue_redraw();
121
}
122
123
String FoldableContainer::get_title() const {
124
return title;
125
}
126
127
void FoldableContainer::set_title_alignment(HorizontalAlignment p_alignment) {
128
ERR_FAIL_INDEX((int)p_alignment, 3);
129
title_alignment = p_alignment;
130
131
if (_get_actual_alignment() != text_buf->get_horizontal_alignment()) {
132
_shape();
133
queue_redraw();
134
}
135
}
136
137
HorizontalAlignment FoldableContainer::get_title_alignment() const {
138
return title_alignment;
139
}
140
141
void FoldableContainer::set_language(const String &p_language) {
142
if (language == p_language) {
143
return;
144
}
145
language = p_language;
146
_shape();
147
update_minimum_size();
148
queue_redraw();
149
}
150
151
String FoldableContainer::get_language() const {
152
return language;
153
}
154
155
void FoldableContainer::set_title_text_direction(TextDirection p_text_direction) {
156
ERR_FAIL_INDEX(int(p_text_direction), 4);
157
if (title_text_direction == p_text_direction) {
158
return;
159
}
160
title_text_direction = p_text_direction;
161
_shape();
162
queue_redraw();
163
}
164
165
Control::TextDirection FoldableContainer::get_title_text_direction() const {
166
return title_text_direction;
167
}
168
169
void FoldableContainer::set_title_text_overrun_behavior(TextServer::OverrunBehavior p_overrun_behavior) {
170
if (overrun_behavior == p_overrun_behavior) {
171
return;
172
}
173
overrun_behavior = p_overrun_behavior;
174
_shape();
175
update_minimum_size();
176
queue_redraw();
177
}
178
179
TextServer::OverrunBehavior FoldableContainer::get_title_text_overrun_behavior() const {
180
return overrun_behavior;
181
}
182
183
void FoldableContainer::set_title_position(TitlePosition p_title_position) {
184
ERR_FAIL_INDEX(p_title_position, POSITION_MAX);
185
if (title_position == p_title_position) {
186
return;
187
}
188
title_position = p_title_position;
189
queue_redraw();
190
queue_sort();
191
}
192
193
FoldableContainer::TitlePosition FoldableContainer::get_title_position() const {
194
return title_position;
195
}
196
197
void FoldableContainer::add_title_bar_control(Control *p_control) {
198
ERR_FAIL_NULL(p_control);
199
if (p_control->get_parent()) {
200
p_control->get_parent()->remove_child(p_control);
201
ERR_FAIL_COND_MSG(p_control->get_parent() != nullptr, "Failed to remove control from parent.");
202
}
203
add_child(p_control, false, INTERNAL_MODE_FRONT);
204
title_controls.push_back(p_control);
205
}
206
207
void FoldableContainer::remove_title_bar_control(Control *p_control) {
208
ERR_FAIL_NULL(p_control);
209
210
int64_t index = title_controls.find(p_control);
211
ERR_FAIL_COND_MSG(index == -1, "Can't remove control from title bar.");
212
213
title_controls.remove_at(index);
214
remove_child(p_control);
215
}
216
217
void FoldableContainer::gui_input(const Ref<InputEvent> &p_event) {
218
ERR_FAIL_COND(p_event.is_null());
219
220
Ref<InputEventMouseMotion> m = p_event;
221
if (m.is_valid()) {
222
Rect2 title_rect = Rect2(0, (title_position == POSITION_TOP) ? 0 : get_size().height - title_minimum_size.height, get_size().width, title_minimum_size.height);
223
if (title_rect.has_point(m->get_position())) {
224
if (!is_hovering) {
225
is_hovering = true;
226
queue_redraw();
227
}
228
} else if (is_hovering) {
229
is_hovering = false;
230
queue_redraw();
231
}
232
return;
233
}
234
235
if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) {
236
set_folded(!folded);
237
emit_signal(SNAME("folding_changed"), folded);
238
accept_event();
239
return;
240
}
241
242
Ref<InputEventMouseButton> b = p_event;
243
if (b.is_valid()) {
244
Rect2 title_rect = Rect2(0, (title_position == POSITION_TOP) ? 0 : get_size().height - title_minimum_size.height, get_size().width, title_minimum_size.height);
245
if (b->get_button_index() == MouseButton::LEFT && b->is_pressed() && title_rect.has_point(b->get_position())) {
246
set_folded(!folded);
247
emit_signal(SNAME("folding_changed"), folded);
248
accept_event();
249
}
250
}
251
}
252
253
String FoldableContainer::get_tooltip(const Point2 &p_pos) const {
254
if (Rect2(0, (title_position == POSITION_TOP) ? 0 : get_size().height - title_minimum_size.height, get_size().width, title_minimum_size.height).has_point(p_pos)) {
255
return Control::get_tooltip(p_pos);
256
}
257
return String();
258
}
259
260
void FoldableContainer::_notification(int p_what) {
261
switch (p_what) {
262
case NOTIFICATION_DRAW: {
263
RID ci = get_canvas_item();
264
Size2 size = get_size();
265
int h_separation = _get_h_separation();
266
267
Ref<StyleBox> title_style = _get_title_style();
268
Ref<Texture2D> icon = _get_title_icon();
269
270
real_t title_controls_width = _get_title_controls_width();
271
if (title_controls_width > 0) {
272
title_controls_width += h_separation;
273
}
274
275
Rect2 title_rect(
276
Point2(0, (title_position == POSITION_TOP) ? 0 : size.height - title_minimum_size.height),
277
Size2(size.width, title_minimum_size.height));
278
_draw_flippable_stylebox(title_style, title_rect);
279
280
Size2 title_ms = title_style->get_minimum_size();
281
int title_text_width = size.width - title_ms.width;
282
283
int title_style_ofs = (title_position == POSITION_TOP) ? title_style->get_margin(SIDE_TOP) : title_style->get_margin(SIDE_BOTTOM);
284
Point2 title_text_pos(title_style->get_margin(SIDE_LEFT), title_style_ofs);
285
title_text_pos.y += MAX((title_minimum_size.height - title_ms.height - text_buf->get_size().height) * 0.5, 0);
286
287
title_text_width -= icon->get_width() + h_separation + title_controls_width;
288
Point2 icon_pos(0, MAX((title_minimum_size.height - title_ms.height - icon->get_height()) * 0.5, 0) + title_style_ofs);
289
290
bool rtl = is_layout_rtl();
291
if (rtl) {
292
icon_pos.x = size.width - title_style->get_margin(SIDE_RIGHT) - icon->get_width();
293
title_text_pos.x += title_controls_width;
294
} else {
295
icon_pos.x = title_style->get_margin(SIDE_LEFT);
296
title_text_pos.x += icon->get_width() + h_separation;
297
}
298
icon->draw(ci, title_rect.position + icon_pos);
299
300
Color font_color = folded ? theme_cache.title_collapsed_font_color : theme_cache.title_font_color;
301
if (is_hovering) {
302
font_color = theme_cache.title_hovered_font_color;
303
}
304
text_buf->set_width(title_text_width);
305
306
if (title_text_width > 0) {
307
if (theme_cache.title_font_outline_size > 0 && theme_cache.title_font_outline_color.a > 0) {
308
text_buf->draw_outline(ci, title_rect.position + title_text_pos, theme_cache.title_font_outline_size, theme_cache.title_font_outline_color);
309
}
310
text_buf->draw(ci, title_rect.position + title_text_pos, font_color);
311
}
312
313
if (!folded) {
314
Rect2 panel_rect(
315
Point2(0, (title_position == POSITION_TOP) ? title_minimum_size.height : 0),
316
Size2(size.width, size.height - title_minimum_size.height));
317
_draw_flippable_stylebox(theme_cache.panel_style, panel_rect);
318
}
319
320
if (has_focus()) {
321
Rect2 focus_rect = folded ? title_rect : Rect2(Point2(), size);
322
_draw_flippable_stylebox(theme_cache.focus_style, focus_rect);
323
}
324
} break;
325
326
case NOTIFICATION_SORT_CHILDREN: {
327
bool rtl = is_layout_rtl();
328
const Vector2 size = get_size();
329
const Ref<StyleBox> title_style = _get_title_style();
330
331
uint32_t title_count = title_controls.size();
332
if (title_count > 0) {
333
int h_separation = MAX(theme_cache.h_separation, 0);
334
real_t offset = 0.0;
335
if (rtl) {
336
offset = title_style->get_margin(SIDE_LEFT);
337
} else {
338
offset = _get_title_controls_width();
339
offset = size.x - title_style->get_margin(SIDE_RIGHT) - offset;
340
}
341
342
real_t v_center = title_minimum_size.y * 0.5;
343
if (title_position == POSITION_BOTTOM) {
344
v_center = size.y - v_center + (title_style->get_margin(SIDE_BOTTOM) - title_style->get_margin(SIDE_TOP)) * 0.5;
345
} else {
346
v_center += (title_style->get_margin(SIDE_TOP) - title_style->get_margin(SIDE_BOTTOM)) * 0.5;
347
}
348
349
for (uint32_t i = 0; i < title_count; i++) {
350
Control *control = title_controls[rtl ? title_count - i - 1 : i];
351
if (!control->is_visible()) {
352
continue;
353
}
354
Rect2 rect(Vector2(), control->get_combined_minimum_size());
355
rect.position.x = offset;
356
rect.position.y = v_center - rect.size.y * 0.5;
357
fit_child_in_rect(control, rect);
358
359
offset += rect.size.x + h_separation;
360
}
361
}
362
363
Rect2 inner_rect;
364
inner_rect.position.x = rtl ? theme_cache.panel_style->get_margin(SIDE_RIGHT) : theme_cache.panel_style->get_margin(SIDE_LEFT);
365
inner_rect.size.x = size.x - theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.panel_style->get_margin(SIDE_RIGHT);
366
inner_rect.position.y = theme_cache.panel_style->get_margin(SIDE_TOP);
367
368
inner_rect.size.y = size.y - theme_cache.panel_style->get_margin(SIDE_TOP) - theme_cache.panel_style->get_margin(SIDE_BOTTOM) - title_minimum_size.y;
369
if (title_position == POSITION_TOP) {
370
inner_rect.position.y += title_minimum_size.y;
371
}
372
373
for (int i = 0; i < get_child_count(false); i++) {
374
Control *c = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE);
375
if (!c) {
376
continue;
377
}
378
c->set_visible(!folded);
379
380
if (!folded) {
381
fit_child_in_rect(c, inner_rect);
382
}
383
}
384
} break;
385
386
case NOTIFICATION_MOUSE_EXIT: {
387
if (is_hovering) {
388
is_hovering = false;
389
queue_redraw();
390
}
391
} break;
392
393
case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
394
case NOTIFICATION_TRANSLATION_CHANGED:
395
case NOTIFICATION_THEME_CHANGED: {
396
_shape();
397
update_minimum_size();
398
queue_redraw();
399
} break;
400
}
401
}
402
403
real_t FoldableContainer::_get_title_controls_width() const {
404
real_t width = 0.0;
405
int visible_controls = 0;
406
for (const Control *control : title_controls) {
407
if (control->is_visible()) {
408
width += control->get_combined_minimum_size().x;
409
visible_controls++;
410
}
411
}
412
if (visible_controls > 1) {
413
width += _get_h_separation() * (visible_controls - 1);
414
}
415
return width;
416
}
417
418
Ref<StyleBox> FoldableContainer::_get_title_style() const {
419
if (is_hovering) {
420
return folded ? theme_cache.title_collapsed_hover_style : theme_cache.title_hover_style;
421
}
422
return folded ? theme_cache.title_collapsed_style : theme_cache.title_style;
423
}
424
425
Ref<Texture2D> FoldableContainer::_get_title_icon() const {
426
if (!folded) {
427
return (title_position == POSITION_TOP) ? theme_cache.expanded_arrow : theme_cache.expanded_arrow_mirrored;
428
} else if (is_layout_rtl()) {
429
return theme_cache.folded_arrow_mirrored;
430
}
431
return theme_cache.folded_arrow;
432
}
433
434
void FoldableContainer::_update_title_min_size() const {
435
Ref<StyleBox> title_style = folded ? theme_cache.title_collapsed_style : theme_cache.title_style;
436
Ref<Texture2D> icon = _get_title_icon();
437
Size2 title_ms = title_style->get_minimum_size();
438
int h_separation = _get_h_separation();
439
440
title_minimum_size = title_ms;
441
title_minimum_size.width += icon->get_width();
442
443
if (!title.is_empty()) {
444
title_minimum_size.width += h_separation;
445
Size2 text_size = text_buf->get_size();
446
title_minimum_size.height += MAX(text_size.height, icon->get_height());
447
if (overrun_behavior == TextServer::OverrunBehavior::OVERRUN_NO_TRIMMING) {
448
title_minimum_size.width += text_size.width;
449
}
450
} else {
451
title_minimum_size.height += icon->get_height();
452
}
453
454
if (!title_controls.is_empty()) {
455
real_t controls_height = 0;
456
int visible_controls = 0;
457
458
for (const Control *control : title_controls) {
459
if (!control->is_visible()) {
460
continue;
461
}
462
Vector2 size = control->get_combined_minimum_size();
463
title_minimum_size.width += size.width;
464
controls_height = MAX(controls_height, size.height);
465
visible_controls++;
466
}
467
if (visible_controls > 0) {
468
title_minimum_size.width += h_separation * visible_controls;
469
}
470
title_minimum_size.height = MAX(title_minimum_size.height, title_ms.height + controls_height);
471
}
472
}
473
474
void FoldableContainer::_shape() {
475
Ref<Font> font = theme_cache.title_font;
476
int font_size = theme_cache.title_font_size;
477
if (font.is_null() || font_size == 0) {
478
return;
479
}
480
481
text_buf->clear();
482
text_buf->set_width(-1);
483
484
if (title_text_direction == TEXT_DIRECTION_INHERITED) {
485
text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
486
} else {
487
text_buf->set_direction((TextServer::Direction)title_text_direction);
488
}
489
text_buf->set_horizontal_alignment(_get_actual_alignment());
490
text_buf->set_text_overrun_behavior(overrun_behavior);
491
text_buf->add_string(atr(title), font, font_size, language);
492
}
493
494
HorizontalAlignment FoldableContainer::_get_actual_alignment() const {
495
if (is_layout_rtl()) {
496
if (title_alignment == HORIZONTAL_ALIGNMENT_RIGHT) {
497
return HORIZONTAL_ALIGNMENT_LEFT;
498
} else if (title_alignment == HORIZONTAL_ALIGNMENT_LEFT) {
499
return HORIZONTAL_ALIGNMENT_RIGHT;
500
}
501
}
502
return title_alignment;
503
}
504
505
void FoldableContainer::_update_group() {
506
foldable_group->updating_group = true;
507
for (FoldableContainer *container : foldable_group->containers) {
508
if (container != this) {
509
container->set_folded(true);
510
}
511
}
512
foldable_group->updating_group = false;
513
}
514
515
void FoldableContainer::_draw_flippable_stylebox(const Ref<StyleBox> p_stylebox, const Rect2 &p_rect) {
516
if (title_position == POSITION_BOTTOM) {
517
Rect2 rect(-p_rect.position, p_rect.size);
518
draw_set_transform(Point2(0.0, p_stylebox->get_draw_rect(rect).size.height), 0.0, Size2(1.0, -1.0));
519
p_stylebox->draw(get_canvas_item(), rect);
520
draw_set_transform_matrix(Transform2D());
521
} else {
522
p_stylebox->draw(get_canvas_item(), p_rect);
523
}
524
}
525
526
void FoldableContainer::_bind_methods() {
527
ClassDB::bind_method(D_METHOD("fold"), &FoldableContainer::fold);
528
ClassDB::bind_method(D_METHOD("expand"), &FoldableContainer::expand);
529
ClassDB::bind_method(D_METHOD("set_folded", "folded"), &FoldableContainer::set_folded);
530
ClassDB::bind_method(D_METHOD("is_folded"), &FoldableContainer::is_folded);
531
ClassDB::bind_method(D_METHOD("set_foldable_group", "button_group"), &FoldableContainer::set_foldable_group);
532
ClassDB::bind_method(D_METHOD("get_foldable_group"), &FoldableContainer::get_foldable_group);
533
ClassDB::bind_method(D_METHOD("set_title", "text"), &FoldableContainer::set_title);
534
ClassDB::bind_method(D_METHOD("get_title"), &FoldableContainer::get_title);
535
ClassDB::bind_method(D_METHOD("set_title_alignment", "alignment"), &FoldableContainer::set_title_alignment);
536
ClassDB::bind_method(D_METHOD("get_title_alignment"), &FoldableContainer::get_title_alignment);
537
ClassDB::bind_method(D_METHOD("set_language", "language"), &FoldableContainer::set_language);
538
ClassDB::bind_method(D_METHOD("get_language"), &FoldableContainer::get_language);
539
ClassDB::bind_method(D_METHOD("set_title_text_direction", "text_direction"), &FoldableContainer::set_title_text_direction);
540
ClassDB::bind_method(D_METHOD("get_title_text_direction"), &FoldableContainer::get_title_text_direction);
541
ClassDB::bind_method(D_METHOD("set_title_text_overrun_behavior", "overrun_behavior"), &FoldableContainer::set_title_text_overrun_behavior);
542
ClassDB::bind_method(D_METHOD("get_title_text_overrun_behavior"), &FoldableContainer::get_title_text_overrun_behavior);
543
ClassDB::bind_method(D_METHOD("set_title_position", "title_position"), &FoldableContainer::set_title_position);
544
ClassDB::bind_method(D_METHOD("get_title_position"), &FoldableContainer::get_title_position);
545
ClassDB::bind_method(D_METHOD("add_title_bar_control", "control"), &FoldableContainer::add_title_bar_control);
546
ClassDB::bind_method(D_METHOD("remove_title_bar_control", "control"), &FoldableContainer::remove_title_bar_control);
547
548
ADD_SIGNAL(MethodInfo("folding_changed", PropertyInfo(Variant::BOOL, "is_folded")));
549
550
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "folded"), "set_folded", "is_folded");
551
ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title");
552
ADD_PROPERTY(PropertyInfo(Variant::INT, "title_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_title_alignment", "get_title_alignment");
553
ADD_PROPERTY(PropertyInfo(Variant::INT, "title_position", PROPERTY_HINT_ENUM, "Top,Bottom"), "set_title_position", "get_title_position");
554
ADD_PROPERTY(PropertyInfo(Variant::INT, "title_text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis,Word Ellipsis"), "set_title_text_overrun_behavior", "get_title_text_overrun_behavior");
555
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "foldable_group", PROPERTY_HINT_RESOURCE_TYPE, "FoldableGroup"), "set_foldable_group", "get_foldable_group");
556
557
ADD_GROUP("BiDi", "");
558
ADD_PROPERTY(PropertyInfo(Variant::INT, "title_text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_title_text_direction", "get_title_text_direction");
559
ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID), "set_language", "get_language");
560
561
BIND_ENUM_CONSTANT(POSITION_TOP);
562
BIND_ENUM_CONSTANT(POSITION_BOTTOM);
563
564
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_style, "title_panel");
565
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_hover_style, "title_hover_panel");
566
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_style, "title_collapsed_panel");
567
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_hover_style, "title_collapsed_hover_panel");
568
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, focus_style, "focus");
569
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, panel_style, "panel");
570
571
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, FoldableContainer, title_font, "font");
572
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, FoldableContainer, title_font_size, "font_size");
573
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, title_font_outline_size, "outline_size");
574
575
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_color, "font_color");
576
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_hovered_font_color, "hover_font_color");
577
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_collapsed_font_color, "collapsed_font_color");
578
BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_outline_color, "font_outline_color");
579
580
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow);
581
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow_mirrored);
582
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow);
583
BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow_mirrored);
584
585
BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, h_separation);
586
}
587
588
FoldableContainer::FoldableContainer(const String &p_text) {
589
text_buf.instantiate();
590
set_title(p_text);
591
set_focus_mode(FOCUS_ALL);
592
set_mouse_filter(MOUSE_FILTER_STOP);
593
}
594
595
FoldableContainer::~FoldableContainer() {
596
if (foldable_group.is_valid()) {
597
foldable_group->containers.erase(this);
598
}
599
}
600
601
FoldableContainer *FoldableGroup::get_expanded_container() const {
602
for (FoldableContainer *container : containers) {
603
if (!container->is_folded()) {
604
return container;
605
}
606
}
607
608
return nullptr;
609
}
610
611
void FoldableGroup::set_allow_folding_all(bool p_enabled) {
612
allow_folding_all = p_enabled;
613
if (!allow_folding_all && !get_expanded_container() && containers.size() > 0) {
614
updating_group = true;
615
(*containers.begin())->set_folded(false);
616
updating_group = false;
617
}
618
}
619
620
bool FoldableGroup::is_allow_folding_all() const {
621
return allow_folding_all;
622
}
623
624
void FoldableGroup::get_containers(List<FoldableContainer *> *r_containers) const {
625
for (FoldableContainer *container : containers) {
626
r_containers->push_back(container);
627
}
628
}
629
630
TypedArray<FoldableContainer> FoldableGroup::_get_containers() const {
631
TypedArray<FoldableContainer> foldable_containers;
632
for (const FoldableContainer *container : containers) {
633
foldable_containers.push_back(container);
634
}
635
636
return foldable_containers;
637
}
638
639
void FoldableGroup::_bind_methods() {
640
ClassDB::bind_method(D_METHOD("get_expanded_container"), &FoldableGroup::get_expanded_container);
641
ClassDB::bind_method(D_METHOD("get_containers"), &FoldableGroup::_get_containers);
642
ClassDB::bind_method(D_METHOD("set_allow_folding_all", "enabled"), &FoldableGroup::set_allow_folding_all);
643
ClassDB::bind_method(D_METHOD("is_allow_folding_all"), &FoldableGroup::is_allow_folding_all);
644
645
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_folding_all"), "set_allow_folding_all", "is_allow_folding_all");
646
647
ADD_SIGNAL(MethodInfo("expanded", PropertyInfo(Variant::OBJECT, "container", PROPERTY_HINT_RESOURCE_TYPE, "FoldableContainer")));
648
}
649
650
FoldableGroup::FoldableGroup() {
651
set_local_to_scene(true);
652
}
653
654