| 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 | if (_get_title_rect().has_point(m->get_position())) { |
| 223 | if (!is_hovering) { |
| 224 | is_hovering = true; |
| 225 | queue_redraw(); |
| 226 | } |
| 227 | } else if (is_hovering) { |
| 228 | is_hovering = false; |
| 229 | queue_redraw(); |
| 230 | } |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) { |
| 235 | set_folded(!folded); |
| 236 | emit_signal(SNAME("folding_changed"), folded); |
| 237 | accept_event(); |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | Ref<InputEventMouseButton> b = p_event; |
| 242 | if (b.is_valid()) { |
| 243 | if (b->get_button_index() == MouseButton::LEFT && b->is_pressed() && _get_title_rect().has_point(b->get_position())) { |
| 244 | set_folded(!folded); |
| 245 | emit_signal(SNAME("folding_changed"), folded); |
| 246 | accept_event(); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | String FoldableContainer::get_tooltip(const Point2 &p_pos) const { |
| 252 | 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)) { |
| 253 | return Control::get_tooltip(p_pos); |
| 254 | } |
| 255 | return String(); |
| 256 | } |
| 257 | |
| 258 | bool FoldableContainer::has_point(const Point2 &p_point) const { |
| 259 | if (folded) { |
| 260 | return _get_title_rect().has_point(p_point); |
| 261 | } |
| 262 | return Control::has_point(p_point); |
| 263 | } |
| 264 | |
| 265 | void FoldableContainer::_notification(int p_what) { |
| 266 | switch (p_what) { |
| 267 | case NOTIFICATION_DRAW: { |
| 268 | RID ci = get_canvas_item(); |
| 269 | Size2 size = get_size(); |
| 270 | int h_separation = _get_h_separation(); |
| 271 | |
| 272 | Ref<StyleBox> title_style = _get_title_style(); |
| 273 | Ref<Texture2D> icon = _get_title_icon(); |
| 274 | |
| 275 | real_t title_controls_width = _get_title_controls_width(); |
| 276 | if (title_controls_width > 0) { |
| 277 | title_controls_width += h_separation; |
| 278 | } |
| 279 | |
| 280 | const Rect2 title_rect = _get_title_rect(); |
| 281 | _draw_flippable_stylebox(title_style, title_rect); |
| 282 | |
| 283 | Size2 title_ms = title_style->get_minimum_size(); |
| 284 | int title_text_width = size.width - title_ms.width; |
| 285 | |
| 286 | int title_style_ofs = (title_position == POSITION_TOP) ? title_style->get_margin(SIDE_TOP) : title_style->get_margin(SIDE_BOTTOM); |
| 287 | Point2 title_text_pos(title_style->get_margin(SIDE_LEFT), title_style_ofs); |
| 288 | title_text_pos.y += MAX((title_minimum_size.height - title_ms.height - text_buf->get_size().height) * 0.5, 0); |
| 289 | |
| 290 | title_text_width -= icon->get_width() + h_separation + title_controls_width; |
| 291 | Point2 icon_pos(0, MAX((title_minimum_size.height - title_ms.height - icon->get_height()) * 0.5, 0) + title_style_ofs); |
| 292 | |
| 293 | bool rtl = is_layout_rtl(); |
| 294 | if (rtl) { |
| 295 | icon_pos.x = size.width - title_style->get_margin(SIDE_RIGHT) - icon->get_width(); |
| 296 | title_text_pos.x += title_controls_width; |
| 297 | } else { |
| 298 | icon_pos.x = title_style->get_margin(SIDE_LEFT); |
| 299 | title_text_pos.x += icon->get_width() + h_separation; |
| 300 | } |
| 301 | icon->draw(ci, title_rect.position + icon_pos); |
| 302 | |
| 303 | Color font_color = folded ? theme_cache.title_collapsed_font_color : theme_cache.title_font_color; |
| 304 | if (is_hovering) { |
| 305 | font_color = theme_cache.title_hovered_font_color; |
| 306 | } |
| 307 | text_buf->set_width(title_text_width); |
| 308 | |
| 309 | if (title_text_width > 0) { |
| 310 | if (theme_cache.title_font_outline_size > 0 && theme_cache.title_font_outline_color.a > 0) { |
| 311 | text_buf->draw_outline(ci, title_rect.position + title_text_pos, theme_cache.title_font_outline_size, theme_cache.title_font_outline_color); |
| 312 | } |
| 313 | text_buf->draw(ci, title_rect.position + title_text_pos, font_color); |
| 314 | } |
| 315 | |
| 316 | if (!folded) { |
| 317 | Rect2 panel_rect( |
| 318 | Point2(0, (title_position == POSITION_TOP) ? title_minimum_size.height : 0), |
| 319 | Size2(size.width, size.height - title_minimum_size.height)); |
| 320 | _draw_flippable_stylebox(theme_cache.panel_style, panel_rect); |
| 321 | } |
| 322 | |
| 323 | if (has_focus(true)) { |
| 324 | Rect2 focus_rect = folded ? title_rect : Rect2(Point2(), size); |
| 325 | _draw_flippable_stylebox(theme_cache.focus_style, focus_rect); |
| 326 | } |
| 327 | } break; |
| 328 | |
| 329 | case NOTIFICATION_SORT_CHILDREN: { |
| 330 | bool rtl = is_layout_rtl(); |
| 331 | const Vector2 size = get_size(); |
| 332 | const Ref<StyleBox> title_style = _get_title_style(); |
| 333 | |
| 334 | uint32_t title_count = title_controls.size(); |
| 335 | if (title_count > 0) { |
| 336 | int h_separation = MAX(theme_cache.h_separation, 0); |
| 337 | real_t offset = 0.0; |
| 338 | if (rtl) { |
| 339 | offset = title_style->get_margin(SIDE_LEFT); |
| 340 | } else { |
| 341 | offset = _get_title_controls_width(); |
| 342 | offset = size.x - title_style->get_margin(SIDE_RIGHT) - offset; |
| 343 | } |
| 344 | |
| 345 | real_t v_center = title_minimum_size.y * 0.5; |
| 346 | if (title_position == POSITION_BOTTOM) { |
| 347 | v_center = size.y - v_center + (title_style->get_margin(SIDE_BOTTOM) - title_style->get_margin(SIDE_TOP)) * 0.5; |
| 348 | } else { |
| 349 | v_center += (title_style->get_margin(SIDE_TOP) - title_style->get_margin(SIDE_BOTTOM)) * 0.5; |
| 350 | } |
| 351 | |
| 352 | for (uint32_t i = 0; i < title_count; i++) { |
| 353 | Control *control = title_controls[rtl ? title_count - i - 1 : i]; |
| 354 | if (!control->is_visible()) { |
| 355 | continue; |
| 356 | } |
| 357 | Rect2 rect(Vector2(), control->get_combined_minimum_size()); |
| 358 | rect.position.x = offset; |
| 359 | rect.position.y = v_center - rect.size.y * 0.5; |
| 360 | fit_child_in_rect(control, rect); |
| 361 | |
| 362 | offset += rect.size.x + h_separation; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | Rect2 inner_rect; |
| 367 | inner_rect.position.x = rtl ? theme_cache.panel_style->get_margin(SIDE_RIGHT) : theme_cache.panel_style->get_margin(SIDE_LEFT); |
| 368 | inner_rect.size.x = size.x - theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.panel_style->get_margin(SIDE_RIGHT); |
| 369 | inner_rect.position.y = theme_cache.panel_style->get_margin(SIDE_TOP); |
| 370 | |
| 371 | 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; |
| 372 | if (title_position == POSITION_TOP) { |
| 373 | inner_rect.position.y += title_minimum_size.y; |
| 374 | } |
| 375 | |
| 376 | for (int i = 0; i < get_child_count(false); i++) { |
| 377 | Control *c = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE); |
| 378 | if (!c) { |
| 379 | continue; |
| 380 | } |
| 381 | c->set_visible(!folded); |
| 382 | |
| 383 | if (!folded) { |
| 384 | fit_child_in_rect(c, inner_rect); |
| 385 | } |
| 386 | } |
| 387 | } break; |
| 388 | |
| 389 | case NOTIFICATION_MOUSE_EXIT: { |
| 390 | if (is_hovering) { |
| 391 | is_hovering = false; |
| 392 | queue_redraw(); |
| 393 | } |
| 394 | } break; |
| 395 | |
| 396 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: |
| 397 | case NOTIFICATION_TRANSLATION_CHANGED: |
| 398 | case NOTIFICATION_THEME_CHANGED: { |
| 399 | _shape(); |
| 400 | update_minimum_size(); |
| 401 | queue_redraw(); |
| 402 | } break; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | real_t FoldableContainer::_get_title_controls_width() const { |
| 407 | real_t width = 0.0; |
| 408 | int visible_controls = 0; |
| 409 | for (const Control *control : title_controls) { |
| 410 | if (control->is_visible()) { |
| 411 | width += control->get_combined_minimum_size().x; |
| 412 | visible_controls++; |
| 413 | } |
| 414 | } |
| 415 | if (visible_controls > 1) { |
| 416 | width += _get_h_separation() * (visible_controls - 1); |
| 417 | } |
| 418 | return width; |
| 419 | } |
| 420 | |
| 421 | Ref<StyleBox> FoldableContainer::_get_title_style() const { |
| 422 | if (is_hovering) { |
| 423 | return folded ? theme_cache.title_collapsed_hover_style : theme_cache.title_hover_style; |
| 424 | } |
| 425 | return folded ? theme_cache.title_collapsed_style : theme_cache.title_style; |
| 426 | } |
| 427 | |
| 428 | Ref<Texture2D> FoldableContainer::_get_title_icon() const { |
| 429 | if (!folded) { |
| 430 | return (title_position == POSITION_TOP) ? theme_cache.expanded_arrow : theme_cache.expanded_arrow_mirrored; |
| 431 | } else if (is_layout_rtl()) { |
| 432 | return theme_cache.folded_arrow_mirrored; |
| 433 | } |
| 434 | return theme_cache.folded_arrow; |
| 435 | } |
| 436 | |
| 437 | Rect2 FoldableContainer::_get_title_rect() const { |
| 438 | return Rect2(0, (title_position == POSITION_TOP) ? 0 : (get_size().height - title_minimum_size.height), get_size().width, title_minimum_size.height); |
| 439 | } |
| 440 | |
| 441 | void FoldableContainer::_update_title_min_size() const { |
| 442 | Ref<StyleBox> title_style = folded ? theme_cache.title_collapsed_style : theme_cache.title_style; |
| 443 | Ref<Texture2D> icon = _get_title_icon(); |
| 444 | Size2 title_ms = title_style->get_minimum_size(); |
| 445 | int h_separation = _get_h_separation(); |
| 446 | |
| 447 | title_minimum_size = title_ms; |
| 448 | title_minimum_size.width += icon->get_width(); |
| 449 | |
| 450 | if (!title.is_empty()) { |
| 451 | title_minimum_size.width += h_separation; |
| 452 | Size2 text_size = text_buf->get_size(); |
| 453 | title_minimum_size.height += MAX(text_size.height, icon->get_height()); |
| 454 | if (overrun_behavior == TextServer::OverrunBehavior::OVERRUN_NO_TRIMMING) { |
| 455 | title_minimum_size.width += text_size.width; |
| 456 | } |
| 457 | } else { |
| 458 | title_minimum_size.height += icon->get_height(); |
| 459 | } |
| 460 | |
| 461 | if (!title_controls.is_empty()) { |
| 462 | real_t controls_height = 0; |
| 463 | int visible_controls = 0; |
| 464 | |
| 465 | for (const Control *control : title_controls) { |
| 466 | if (!control->is_visible()) { |
| 467 | continue; |
| 468 | } |
| 469 | Vector2 size = control->get_combined_minimum_size(); |
| 470 | title_minimum_size.width += size.width; |
| 471 | controls_height = MAX(controls_height, size.height); |
| 472 | visible_controls++; |
| 473 | } |
| 474 | if (visible_controls > 0) { |
| 475 | title_minimum_size.width += h_separation * visible_controls; |
| 476 | } |
| 477 | title_minimum_size.height = MAX(title_minimum_size.height, title_ms.height + controls_height); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | void FoldableContainer::_shape() { |
| 482 | Ref<Font> font = theme_cache.title_font; |
| 483 | int font_size = theme_cache.title_font_size; |
| 484 | if (font.is_null() || font_size == 0) { |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | text_buf->clear(); |
| 489 | text_buf->set_width(-1); |
| 490 | |
| 491 | if (title_text_direction == TEXT_DIRECTION_INHERITED) { |
| 492 | text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR); |
| 493 | } else { |
| 494 | text_buf->set_direction((TextServer::Direction)title_text_direction); |
| 495 | } |
| 496 | text_buf->set_horizontal_alignment(_get_actual_alignment()); |
| 497 | text_buf->set_text_overrun_behavior(overrun_behavior); |
| 498 | const String &lang = language.is_empty() ? _get_locale() : language; |
| 499 | text_buf->add_string(atr(title), font, font_size, lang); |
| 500 | } |
| 501 | |
| 502 | HorizontalAlignment FoldableContainer::_get_actual_alignment() const { |
| 503 | if (is_layout_rtl()) { |
| 504 | if (title_alignment == HORIZONTAL_ALIGNMENT_RIGHT) { |
| 505 | return HORIZONTAL_ALIGNMENT_LEFT; |
| 506 | } else if (title_alignment == HORIZONTAL_ALIGNMENT_LEFT) { |
| 507 | return HORIZONTAL_ALIGNMENT_RIGHT; |
| 508 | } |
| 509 | } |
| 510 | return title_alignment; |
| 511 | } |
| 512 | |
| 513 | void FoldableContainer::_update_group() { |
| 514 | foldable_group->updating_group = true; |
| 515 | for (FoldableContainer *container : foldable_group->containers) { |
| 516 | if (container != this) { |
| 517 | container->set_folded(true); |
| 518 | } |
| 519 | } |
| 520 | foldable_group->updating_group = false; |
| 521 | } |
| 522 | |
| 523 | void FoldableContainer::_draw_flippable_stylebox(const Ref<StyleBox> p_stylebox, const Rect2 &p_rect) { |
| 524 | if (title_position == POSITION_BOTTOM) { |
| 525 | Rect2 rect(-p_rect.position, p_rect.size); |
| 526 | draw_set_transform(Point2(0.0, p_stylebox->get_draw_rect(rect).size.height), 0.0, Size2(1.0, -1.0)); |
| 527 | p_stylebox->draw(get_canvas_item(), rect); |
| 528 | draw_set_transform_matrix(Transform2D()); |
| 529 | } else { |
| 530 | p_stylebox->draw(get_canvas_item(), p_rect); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | void FoldableContainer::_bind_methods() { |
| 535 | ClassDB::bind_method(D_METHOD("fold"), &FoldableContainer::fold); |
| 536 | ClassDB::bind_method(D_METHOD("expand"), &FoldableContainer::expand); |
| 537 | ClassDB::bind_method(D_METHOD("set_folded", "folded"), &FoldableContainer::set_folded); |
| 538 | ClassDB::bind_method(D_METHOD("is_folded"), &FoldableContainer::is_folded); |
| 539 | ClassDB::bind_method(D_METHOD("set_foldable_group", "button_group"), &FoldableContainer::set_foldable_group); |
| 540 | ClassDB::bind_method(D_METHOD("get_foldable_group"), &FoldableContainer::get_foldable_group); |
| 541 | ClassDB::bind_method(D_METHOD("set_title", "text"), &FoldableContainer::set_title); |
| 542 | ClassDB::bind_method(D_METHOD("get_title"), &FoldableContainer::get_title); |
| 543 | ClassDB::bind_method(D_METHOD("set_title_alignment", "alignment"), &FoldableContainer::set_title_alignment); |
| 544 | ClassDB::bind_method(D_METHOD("get_title_alignment"), &FoldableContainer::get_title_alignment); |
| 545 | ClassDB::bind_method(D_METHOD("set_language", "language"), &FoldableContainer::set_language); |
| 546 | ClassDB::bind_method(D_METHOD("get_language"), &FoldableContainer::get_language); |
| 547 | ClassDB::bind_method(D_METHOD("set_title_text_direction", "text_direction"), &FoldableContainer::set_title_text_direction); |
| 548 | ClassDB::bind_method(D_METHOD("get_title_text_direction"), &FoldableContainer::get_title_text_direction); |
| 549 | ClassDB::bind_method(D_METHOD("set_title_text_overrun_behavior", "overrun_behavior"), &FoldableContainer::set_title_text_overrun_behavior); |
| 550 | ClassDB::bind_method(D_METHOD("get_title_text_overrun_behavior"), &FoldableContainer::get_title_text_overrun_behavior); |
| 551 | ClassDB::bind_method(D_METHOD("set_title_position", "title_position"), &FoldableContainer::set_title_position); |
| 552 | ClassDB::bind_method(D_METHOD("get_title_position"), &FoldableContainer::get_title_position); |
| 553 | ClassDB::bind_method(D_METHOD("add_title_bar_control", "control"), &FoldableContainer::add_title_bar_control); |
| 554 | ClassDB::bind_method(D_METHOD("remove_title_bar_control", "control"), &FoldableContainer::remove_title_bar_control); |
| 555 | |
| 556 | ADD_SIGNAL(MethodInfo("folding_changed", PropertyInfo(Variant::BOOL, "is_folded"))); |
| 557 | |
| 558 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "folded"), "set_folded", "is_folded"); |
| 559 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "title"), "set_title", "get_title"); |
| 560 | ADD_PROPERTY(PropertyInfo(Variant::INT, "title_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_title_alignment", "get_title_alignment"); |
| 561 | ADD_PROPERTY(PropertyInfo(Variant::INT, "title_position", PROPERTY_HINT_ENUM, "Top,Bottom"), "set_title_position", "get_title_position"); |
| 562 | 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"); |
| 563 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "foldable_group", PROPERTY_HINT_RESOURCE_TYPE, "FoldableGroup"), "set_foldable_group", "get_foldable_group"); |
| 564 | |
| 565 | ADD_GROUP("BiDi", ""); |
| 566 | 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"); |
| 567 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID), "set_language", "get_language"); |
| 568 | |
| 569 | BIND_ENUM_CONSTANT(POSITION_TOP); |
| 570 | BIND_ENUM_CONSTANT(POSITION_BOTTOM); |
| 571 | |
| 572 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_style, "title_panel"); |
| 573 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_hover_style, "title_hover_panel"); |
| 574 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_style, "title_collapsed_panel"); |
| 575 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_hover_style, "title_collapsed_hover_panel"); |
| 576 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, focus_style, "focus"); |
| 577 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, panel_style, "panel"); |
| 578 | |
| 579 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, FoldableContainer, title_font, "font"); |
| 580 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, FoldableContainer, title_font_size, "font_size"); |
| 581 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, title_font_outline_size, "outline_size"); |
| 582 | |
| 583 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_color, "font_color"); |
| 584 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_hovered_font_color, "hover_font_color"); |
| 585 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_collapsed_font_color, "collapsed_font_color"); |
| 586 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_outline_color, "font_outline_color"); |
| 587 | |
| 588 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow); |
| 589 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow_mirrored); |
| 590 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow); |
| 591 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow_mirrored); |
| 592 | |
| 593 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, h_separation); |
| 594 | } |
| 595 | |
| 596 | FoldableContainer::FoldableContainer(const String &p_text) { |
| 597 | text_buf.instantiate(); |
| 598 | set_title(p_text); |
| 599 | set_focus_mode(FOCUS_ALL); |
| 600 | set_mouse_filter(MOUSE_FILTER_STOP); |
| 601 | } |
| 602 | |
| 603 | FoldableContainer::~FoldableContainer() { |
| 604 | if (foldable_group.is_valid()) { |
| 605 | foldable_group->containers.erase(this); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | FoldableContainer *FoldableGroup::get_expanded_container() const { |
| 610 | for (FoldableContainer *container : containers) { |
| 611 | if (!container->is_folded()) { |
| 612 | return container; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | return nullptr; |
| 617 | } |
| 618 | |
| 619 | void FoldableGroup::set_allow_folding_all(bool p_enabled) { |
| 620 | allow_folding_all = p_enabled; |
| 621 | if (!allow_folding_all && !get_expanded_container() && containers.size() > 0) { |
| 622 | updating_group = true; |
| 623 | (*containers.begin())->set_folded(false); |
| 624 | updating_group = false; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | bool FoldableGroup::is_allow_folding_all() const { |
| 629 | return allow_folding_all; |
| 630 | } |
| 631 | |
| 632 | void FoldableGroup::get_containers(List<FoldableContainer *> *r_containers) const { |
| 633 | for (FoldableContainer *container : containers) { |
| 634 | r_containers->push_back(container); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | TypedArray<FoldableContainer> FoldableGroup::_get_containers() const { |
| 639 | TypedArray<FoldableContainer> foldable_containers; |
| 640 | for (const FoldableContainer *container : containers) { |
| 641 | foldable_containers.push_back(container); |
| 642 | } |
| 643 | |
| 644 | return foldable_containers; |
| 645 | } |
| 646 | |
| 647 | void FoldableGroup::_bind_methods() { |
| 648 | ClassDB::bind_method(D_METHOD("get_expanded_container"), &FoldableGroup::get_expanded_container); |
| 649 | ClassDB::bind_method(D_METHOD("get_containers"), &FoldableGroup::_get_containers); |
| 650 | ClassDB::bind_method(D_METHOD("set_allow_folding_all", "enabled"), &FoldableGroup::set_allow_folding_all); |
| 651 | ClassDB::bind_method(D_METHOD("is_allow_folding_all"), &FoldableGroup::is_allow_folding_all); |
| 652 | |
| 653 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_folding_all"), "set_allow_folding_all", "is_allow_folding_all"); |
| 654 | |
| 655 | ADD_SIGNAL(MethodInfo("expanded", PropertyInfo(Variant::OBJECT, "container", PROPERTY_HINT_RESOURCE_TYPE, "FoldableContainer"))); |
| 656 | } |
| 657 | |
| 658 | FoldableGroup::FoldableGroup() { |
| 659 | set_local_to_scene(true); |
| 660 | } |