diff --git a/doc/classes/FoldableContainer.xml b/doc/classes/FoldableContainer.xml new file mode 100644 index 00000000000..677a65fcf96 --- /dev/null +++ b/doc/classes/FoldableContainer.xml @@ -0,0 +1,144 @@ + + + + A container that can be expanded/collapsed. + + + A container that can be expanded/collapsed, with a title that can be filled with controls, such as buttons. + The title can be positioned at the top or bottom of the container. + The container can be expanded or collapsed by clicking the title or by pressing [code]ui_accept[/code] when focused. + Child control nodes are hidden when the container is collapsed. Ignores non-control children. + Can allow grouping with other FoldableContainers, check [member foldable_group] and [FoldableGroup]. + + + + + + + + + Adds a [Control] that will be placed next to the container's title, obscuring the clickable area. Prime usage is adding [Button] nodes, but it can be any [Control]. + The control will be added as a child of this container and removed from previous parent if necessary. The controls will be placed aligned to the right, with the first added control being the leftmost one. + + + + + + Expands the container and emits [signal folding_changed]. + + + + + + Folds the container and emits [signal folding_changed]. + + + + + + + Removes a [Control] added with [method add_title_bar_control]. The node is not freed automatically, you need to use [method Node.queue_free]. + + + + + + + The [FoldableGroup] associated with the container. + + + If [code]true[/code], the container will becomes folded and will hide all its children. + + + Language code used for text shaping algorithms. If left empty, current locale is used instead. + + + + The Container's title text. + + + Base text writing direction. + + + Defines the behavior of the [FoldableContainer] when the text is longer than the available space. + + + Title's horizontal text alignment as defined in the [enum HorizontalAlignment] enum. + + + Title's position as defined in the [enum TitlePosition] enum. + + + + + + + Emitted when the container is folded/expanded. + + + + + + Makes the title appear at the top of the container. + + + Makes the title appear at the bottom of the container. Also makes all StyleBoxes flipped vertically. + + + + + The title's font color when collapsed. + + + The title's font color when expanded. + + + The title's font outline color. + + + The title's font hover color. + + + The horizontal separation between the title's icon and text, and between title bar controls. + + + The title's font outline size. + + + The title's font. + + + The title's font size. + + + The title's icon used when expanded. + + + The title's icon used when expanded (for bottom title). + + + The title's icon used when folded (for left-to-right layouts). + + + The title's icon used when collapsed (for right-to-left layouts). + + + Background used when [FoldableContainer] has GUI focus. The [theme_item focus] [StyleBox] is displayed [i]over[/i] the base [StyleBox], so a partially transparent [StyleBox] should be used to ensure the base [StyleBox] remains visible. A [StyleBox] that represents an outline or an underline works well for this purpose. To disable the focus visual effect, assign a [StyleBoxEmpty] resource. Note that disabling the focus visual effect will harm keyboard/controller navigation usability, so this is not recommended for accessibility reasons. + + + Default background for the [FoldableContainer]. + + + Background used when the mouse cursor enters the title's area when collapsed. + + + Default background for the [FoldableContainer]'s title when collapsed. + + + Background used when the mouse cursor enters the title's area when expanded. + + + Default background for the [FoldableContainer]'s title when expanded. + + + diff --git a/doc/classes/FoldableGroup.xml b/doc/classes/FoldableGroup.xml new file mode 100644 index 00000000000..f803f0e838f --- /dev/null +++ b/doc/classes/FoldableGroup.xml @@ -0,0 +1,39 @@ + + + + A group of foldable containers that doesn't allow more than one container to be expanded at a time. + + + A group of [FoldableContainer]-derived nodes. Only one container can be expanded at a time. + + + + + + + + Returns an [Array] of [FoldableContainer]s that have this as their FoldableGroup (see [member FoldableContainer.foldable_group]). This is equivalent to [ButtonGroup] but for FoldableContainers. + + + + + + Returns the current expanded container. + + + + + + If [code]true[/code], it is possible to fold all containers in this FoldableGroup. + + + + + + + + Emitted when one of the containers of the group is expanded. + + + + diff --git a/editor/icons/FoldableContainer.svg b/editor/icons/FoldableContainer.svg new file mode 100644 index 00000000000..858174674c9 --- /dev/null +++ b/editor/icons/FoldableContainer.svg @@ -0,0 +1 @@ + diff --git a/editor/icons/GuiArrowUp.svg b/editor/icons/GuiArrowUp.svg new file mode 100644 index 00000000000..84edf2d9478 --- /dev/null +++ b/editor/icons/GuiArrowUp.svg @@ -0,0 +1 @@ + diff --git a/editor/themes/editor_theme_manager.cpp b/editor/themes/editor_theme_manager.cpp index 6c7363de60d..986a0469e74 100644 --- a/editor/themes/editor_theme_manager.cpp +++ b/editor/themes/editor_theme_manager.cpp @@ -1295,6 +1295,40 @@ void EditorThemeManager::_populate_standard_styles(const Ref &p_the // GridContainer. p_theme->set_constant("v_separation", "GridContainer", Math::round(p_config.widget_margin.y - 2 * EDSCALE)); + + // FoldableContainer + + Ref foldable_container_title = make_flat_stylebox(p_config.dark_color_1.darkened(0.125), p_config.base_margin, p_config.base_margin, p_config.base_margin, p_config.base_margin); + foldable_container_title->set_corner_radius(CORNER_BOTTOM_LEFT, 0); + foldable_container_title->set_corner_radius(CORNER_BOTTOM_RIGHT, 0); + p_theme->set_stylebox("title_panel", "FoldableContainer", foldable_container_title); + Ref foldable_container_hover = make_flat_stylebox(p_config.dark_color_1.lerp(p_config.base_color, 0.4), p_config.base_margin, p_config.base_margin, p_config.base_margin, p_config.base_margin); + foldable_container_hover->set_corner_radius(CORNER_BOTTOM_LEFT, 0); + foldable_container_hover->set_corner_radius(CORNER_BOTTOM_RIGHT, 0); + p_theme->set_stylebox("title_hover_panel", "FoldableContainer", foldable_container_hover); + p_theme->set_stylebox("title_collapsed_panel", "FoldableContainer", make_flat_stylebox(p_config.dark_color_1.darkened(0.125), p_config.base_margin, p_config.base_margin, p_config.base_margin, p_config.base_margin)); + p_theme->set_stylebox("title_collapsed_hover_panel", "FoldableContainer", make_flat_stylebox(p_config.dark_color_1.lerp(p_config.base_color, 0.4), p_config.base_margin, p_config.base_margin, p_config.base_margin, p_config.base_margin)); + Ref foldable_container_panel = make_flat_stylebox(p_config.dark_color_1, p_config.base_margin, p_config.base_margin, p_config.base_margin, p_config.base_margin); + foldable_container_panel->set_corner_radius(CORNER_TOP_LEFT, 0); + foldable_container_panel->set_corner_radius(CORNER_TOP_RIGHT, 0); + p_theme->set_stylebox(SceneStringName(panel), "FoldableContainer", foldable_container_panel); + p_theme->set_stylebox("focus", "FoldableContainer", p_config.button_style_focus); + + p_theme->set_font(SceneStringName(font), "FoldableContainer", p_theme->get_font(SceneStringName(font), SNAME("HeaderSmall"))); + p_theme->set_font_size(SceneStringName(font_size), "FoldableContainer", p_theme->get_font_size(SceneStringName(font_size), SNAME("HeaderSmall"))); + + p_theme->set_color(SceneStringName(font_color), "FoldableContainer", p_config.font_color); + p_theme->set_color("hover_font_color", "FoldableContainer", p_config.font_hover_color); + p_theme->set_color("collapsed_font_color", "FoldableContainer", p_config.font_pressed_color); + p_theme->set_color("font_outline_color", "FoldableContainer", p_config.font_outline_color); + + p_theme->set_icon("expanded_arrow", "FoldableContainer", p_theme->get_icon(SNAME("GuiTreeArrowDown"), EditorStringName(EditorIcons))); + p_theme->set_icon("expanded_arrow_mirrored", "FoldableContainer", p_theme->get_icon(SNAME("GuiArrowUp"), EditorStringName(EditorIcons))); + p_theme->set_icon("folded_arrow", "FoldableContainer", p_theme->get_icon(SNAME("GuiTreeArrowRight"), EditorStringName(EditorIcons))); + p_theme->set_icon("folded_arrow_mirrored", "FoldableContainer", p_theme->get_icon(SNAME("GuiTreeArrowLeft"), EditorStringName(EditorIcons))); + + p_theme->set_constant("outline_size", "FoldableContainer", 0); + p_theme->set_constant("h_separation", "FoldableContainer", p_config.separation_margin); } // Window and dialogs. diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp index 907337ed606..b16128d3b20 100644 --- a/modules/mono/editor/bindings_generator.cpp +++ b/modules/mono/editor/bindings_generator.cpp @@ -141,6 +141,7 @@ const Vector prop_allowed_inherited_member_hiding = { "MenuBar.TextDirection", "RichTextLabel.TextDirection", "TextEdit.TextDirection", + "FoldableContainer.TextDirection", "VisualShaderNodeReroute.PortType", // The following instances are uniquely egregious violations, hiding `GetType()` from `object`. // Included for the sake of CI, with the understanding that they *deserve* warnings. diff --git a/scene/gui/foldable_container.cpp b/scene/gui/foldable_container.cpp new file mode 100644 index 00000000000..33489a356a8 --- /dev/null +++ b/scene/gui/foldable_container.cpp @@ -0,0 +1,652 @@ +/**************************************************************************/ +/* foldable_container.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "foldable_container.h" + +#include "scene/resources/text_line.h" +#include "scene/theme/theme_db.h" + +Size2 FoldableContainer::get_minimum_size() const { + _update_title_min_size(); + + if (folded) { + return title_minimum_size; + } + Size2 ms; + + for (int i = 0; i < get_child_count(); i++) { + Control *c = as_sortable_control(get_child(i)); + if (!c) { + continue; + } + ms = ms.max(c->get_combined_minimum_size()); + } + ms += theme_cache.panel_style->get_minimum_size(); + + return Size2(MAX(ms.width, title_minimum_size.width), ms.height + title_minimum_size.height); +} + +void FoldableContainer::fold() { + set_folded(true); + emit_signal(SNAME("folding_changed"), folded); +} + +void FoldableContainer::expand() { + set_folded(false); + emit_signal(SNAME("folding_changed"), folded); +} + +void FoldableContainer::set_folded(bool p_folded) { + if (folded != p_folded) { + if (!changing_group && foldable_group.is_valid()) { + if (!p_folded) { + _update_group(); + foldable_group->emit_signal(SNAME("expanded"), this); + } else if (!foldable_group->updating_group && foldable_group->get_expanded_container() == this && !foldable_group->is_allow_folding_all()) { + return; + } + } + folded = p_folded; + + update_minimum_size(); + queue_sort(); + queue_redraw(); + } +} + +bool FoldableContainer::is_folded() const { + return folded; +} + +void FoldableContainer::set_foldable_group(const Ref &p_group) { + if (foldable_group.is_valid()) { + foldable_group->containers.erase(this); + } + + foldable_group = p_group; + + if (foldable_group.is_valid()) { + changing_group = true; + if (folded && !foldable_group->get_expanded_container() && !foldable_group->is_allow_folding_all()) { + set_folded(false); + } else if (!folded && foldable_group->get_expanded_container()) { + set_folded(true); + } + foldable_group->containers.insert(this); + changing_group = false; + } + + queue_redraw(); +} + +Ref FoldableContainer::get_foldable_group() const { + return foldable_group; +} + +void FoldableContainer::set_text(const String &p_text) { + if (text == p_text) { + return; + } + text = p_text; + _shape(); + update_minimum_size(); + queue_redraw(); +} + +String FoldableContainer::get_text() const { + return text; +} + +void FoldableContainer::set_text_alignment(HorizontalAlignment p_alignment) { + ERR_FAIL_INDEX((int)p_alignment, 3); + text_alignment = p_alignment; + + if (_get_actual_alignment() != text_buf->get_horizontal_alignment()) { + _shape(); + queue_redraw(); + } +} + +HorizontalAlignment FoldableContainer::get_text_alignment() const { + return text_alignment; +} + +void FoldableContainer::set_language(const String &p_language) { + if (language == p_language) { + return; + } + language = p_language; + _shape(); + update_minimum_size(); + queue_redraw(); +} + +String FoldableContainer::get_language() const { + return language; +} + +void FoldableContainer::set_text_direction(TextDirection p_text_direction) { + ERR_FAIL_INDEX(int(p_text_direction), 4); + if (text_direction == p_text_direction) { + return; + } + text_direction = p_text_direction; + _shape(); + queue_redraw(); +} + +Control::TextDirection FoldableContainer::get_text_direction() const { + return text_direction; +} + +void FoldableContainer::set_text_overrun_behavior(TextServer::OverrunBehavior p_overrun_behavior) { + if (overrun_behavior == p_overrun_behavior) { + return; + } + overrun_behavior = p_overrun_behavior; + _shape(); + update_minimum_size(); + queue_redraw(); +} + +TextServer::OverrunBehavior FoldableContainer::get_text_overrun_behavior() const { + return overrun_behavior; +} + +void FoldableContainer::set_title_position(TitlePosition p_title_position) { + ERR_FAIL_INDEX(p_title_position, POSITION_MAX); + if (title_position == p_title_position) { + return; + } + title_position = p_title_position; + queue_redraw(); + queue_sort(); +} + +FoldableContainer::TitlePosition FoldableContainer::get_title_position() const { + return title_position; +} + +void FoldableContainer::add_title_bar_control(Control *p_control) { + ERR_FAIL_NULL(p_control); + if (p_control->get_parent()) { + p_control->get_parent()->remove_child(p_control); + ERR_FAIL_COND_MSG(p_control->get_parent() != nullptr, "Failed to remove control from parent."); + } + add_child(p_control, false, INTERNAL_MODE_FRONT); + title_controls.push_back(p_control); +} + +void FoldableContainer::remove_title_bar_control(Control *p_control) { + ERR_FAIL_NULL(p_control); + + int64_t index = title_controls.find(p_control); + ERR_FAIL_COND_MSG(index == -1, "Can't remove control from title bar."); + + title_controls.remove_at(index); + remove_child(p_control); +} + +void FoldableContainer::gui_input(const Ref &p_event) { + ERR_FAIL_COND(p_event.is_null()); + + Ref m = p_event; + if (m.is_valid()) { + Rect2 title_rect = Rect2(0, (title_position == POSITION_TOP) ? 0 : get_size().height - title_minimum_size.height, get_size().width, title_minimum_size.height); + if (title_rect.has_point(m->get_position())) { + if (!is_hovering) { + is_hovering = true; + queue_redraw(); + } + } else if (is_hovering) { + is_hovering = false; + queue_redraw(); + } + return; + } + + if (p_event->is_action_pressed(SNAME("ui_accept"), false, true)) { + set_folded(!folded); + emit_signal(SNAME("folding_changed"), folded); + accept_event(); + return; + } + + Ref b = p_event; + if (b.is_valid()) { + Rect2 title_rect = Rect2(0, (title_position == POSITION_TOP) ? 0 : get_size().height - title_minimum_size.height, get_size().width, title_minimum_size.height); + if (b->get_button_index() == MouseButton::LEFT && b->is_pressed() && title_rect.has_point(b->get_position())) { + set_folded(!folded); + emit_signal(SNAME("folding_changed"), folded); + accept_event(); + } + } +} + +String FoldableContainer::get_tooltip(const Point2 &p_pos) const { + 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)) { + return Control::get_tooltip(p_pos); + } + return String(); +} + +void FoldableContainer::_notification(int p_what) { + switch (p_what) { + case NOTIFICATION_DRAW: { + RID ci = get_canvas_item(); + Size2 size = get_size(); + int h_separation = _get_h_separation(); + + Ref title_style = _get_title_style(); + Ref icon = _get_title_icon(); + + real_t title_controls_width = _get_title_controls_width(); + if (title_controls_width > 0) { + title_controls_width += h_separation; + } + + Rect2 title_rect( + Point2(0, (title_position == POSITION_TOP) ? 0 : size.height - title_minimum_size.height), + Size2(size.width, title_minimum_size.height)); + _draw_flippable_stylebox(title_style, title_rect); + + Size2 title_ms = title_style->get_minimum_size(); + int title_text_width = size.width - title_ms.width; + + int title_style_ofs = (title_position == POSITION_TOP) ? title_style->get_margin(SIDE_TOP) : title_style->get_margin(SIDE_BOTTOM); + Point2 title_text_pos(title_style->get_margin(SIDE_LEFT), title_style_ofs); + title_text_pos.y += MAX((title_minimum_size.height - title_ms.height - text_buf->get_size().height) * 0.5, 0); + + title_text_width -= icon->get_width() + h_separation + title_controls_width; + Point2 icon_pos(0, MAX((title_minimum_size.height - title_ms.height - icon->get_height()) * 0.5, 0) + title_style_ofs); + + bool rtl = is_layout_rtl(); + if (rtl) { + icon_pos.x = size.width - title_style->get_margin(SIDE_RIGHT) - icon->get_width(); + title_text_pos.x += title_controls_width; + } else { + icon_pos.x = title_style->get_margin(SIDE_LEFT); + title_text_pos.x += icon->get_width() + h_separation; + } + icon->draw(ci, title_rect.position + icon_pos); + + Color font_color = folded ? theme_cache.title_collapsed_font_color : theme_cache.title_font_color; + if (is_hovering) { + font_color = theme_cache.title_hovered_font_color; + } + text_buf->set_width(title_text_width); + + if (title_text_width > 0) { + if (theme_cache.title_font_outline_size > 0 && theme_cache.title_font_outline_color.a > 0) { + text_buf->draw_outline(ci, title_rect.position + title_text_pos, theme_cache.title_font_outline_size, theme_cache.title_font_outline_color); + } + text_buf->draw(ci, title_rect.position + title_text_pos, font_color); + } + + if (!folded) { + Rect2 panel_rect( + Point2(0, (title_position == POSITION_TOP) ? title_minimum_size.height : 0), + Size2(size.width, size.height - title_minimum_size.height)); + _draw_flippable_stylebox(theme_cache.panel_style, panel_rect); + } + + if (has_focus()) { + Rect2 focus_rect = folded ? title_rect : Rect2(Point2(), size); + _draw_flippable_stylebox(theme_cache.focus_style, focus_rect); + } + } break; + + case NOTIFICATION_SORT_CHILDREN: { + bool rtl = is_layout_rtl(); + const Vector2 size = get_size(); + const Ref title_style = _get_title_style(); + + uint32_t title_count = title_controls.size(); + if (title_count > 0) { + int h_separation = MAX(theme_cache.h_separation, 0); + real_t offset = 0.0; + if (rtl) { + offset = title_style->get_margin(SIDE_LEFT); + } else { + offset = _get_title_controls_width(); + offset = size.x - title_style->get_margin(SIDE_RIGHT) - offset; + } + + real_t v_center = title_minimum_size.y * 0.5; + if (title_position == POSITION_BOTTOM) { + v_center = size.y - v_center + (title_style->get_margin(SIDE_BOTTOM) - title_style->get_margin(SIDE_TOP)) * 0.5; + } else { + v_center += (title_style->get_margin(SIDE_TOP) - title_style->get_margin(SIDE_BOTTOM)) * 0.5; + } + + for (uint32_t i = 0; i < title_count; i++) { + Control *control = title_controls[rtl ? title_count - i - 1 : i]; + if (!control->is_visible()) { + continue; + } + Rect2 rect(Vector2(), control->get_combined_minimum_size()); + rect.position.x = offset; + rect.position.y = v_center - rect.size.y * 0.5; + fit_child_in_rect(control, rect); + + offset += rect.size.x + h_separation; + } + } + + Rect2 inner_rect; + inner_rect.position.x = rtl ? theme_cache.panel_style->get_margin(SIDE_RIGHT) : theme_cache.panel_style->get_margin(SIDE_LEFT); + inner_rect.size.x = size.x - theme_cache.panel_style->get_margin(SIDE_LEFT) - theme_cache.panel_style->get_margin(SIDE_RIGHT); + inner_rect.position.y = theme_cache.panel_style->get_margin(SIDE_TOP); + + 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; + if (title_position == POSITION_TOP) { + inner_rect.position.y += title_minimum_size.y; + } + + for (int i = 0; i < get_child_count(false); i++) { + Control *c = as_sortable_control(get_child(i, false), SortableVisibilityMode::IGNORE); + if (!c) { + continue; + } + c->set_visible(!folded); + + if (!folded) { + fit_child_in_rect(c, inner_rect); + } + } + } break; + + case NOTIFICATION_MOUSE_EXIT: { + if (is_hovering) { + is_hovering = false; + queue_redraw(); + } + } break; + + case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: + case NOTIFICATION_TRANSLATION_CHANGED: + case NOTIFICATION_THEME_CHANGED: { + _shape(); + update_minimum_size(); + queue_redraw(); + } break; + } +} + +real_t FoldableContainer::_get_title_controls_width() const { + real_t width = 0.0; + int visible_controls = 0; + for (const Control *control : title_controls) { + if (control->is_visible()) { + width += control->get_combined_minimum_size().x; + visible_controls++; + } + } + if (visible_controls > 1) { + width += _get_h_separation() * (visible_controls - 1); + } + return width; +} + +Ref FoldableContainer::_get_title_style() const { + if (is_hovering) { + return folded ? theme_cache.title_collapsed_hover_style : theme_cache.title_hover_style; + } + return folded ? theme_cache.title_collapsed_style : theme_cache.title_style; +} + +Ref FoldableContainer::_get_title_icon() const { + if (!folded) { + return (title_position == POSITION_TOP) ? theme_cache.expanded_arrow : theme_cache.expanded_arrow_mirrored; + } else if (is_layout_rtl()) { + return theme_cache.folded_arrow_mirrored; + } + return theme_cache.folded_arrow; +} + +void FoldableContainer::_update_title_min_size() const { + Ref title_style = folded ? theme_cache.title_collapsed_style : theme_cache.title_style; + Ref icon = _get_title_icon(); + Size2 title_ms = title_style->get_minimum_size(); + int h_separation = _get_h_separation(); + + title_minimum_size = title_ms; + title_minimum_size.width += icon->get_width(); + + if (!text.is_empty()) { + title_minimum_size.width += h_separation; + Size2 text_size = text_buf->get_size(); + title_minimum_size.height += MAX(text_size.height, icon->get_height()); + if (overrun_behavior == TextServer::OverrunBehavior::OVERRUN_NO_TRIMMING) { + title_minimum_size.width += text_size.width; + } + } else { + title_minimum_size.height += icon->get_height(); + } + + if (!title_controls.is_empty()) { + real_t controls_height = 0; + int visible_controls = 0; + + for (const Control *control : title_controls) { + if (!control->is_visible()) { + continue; + } + Vector2 size = control->get_combined_minimum_size(); + title_minimum_size.width += size.width; + controls_height = MAX(controls_height, size.height); + visible_controls++; + } + if (visible_controls > 0) { + title_minimum_size.width += h_separation * visible_controls; + } + title_minimum_size.height = MAX(title_minimum_size.height, title_ms.height + controls_height); + } +} + +void FoldableContainer::_shape() { + Ref font = theme_cache.title_font; + int font_size = theme_cache.title_font_size; + if (font.is_null() || font_size == 0) { + return; + } + + text_buf->clear(); + text_buf->set_width(-1); + + if (text_direction == TEXT_DIRECTION_INHERITED) { + text_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR); + } else { + text_buf->set_direction((TextServer::Direction)text_direction); + } + text_buf->set_horizontal_alignment(_get_actual_alignment()); + text_buf->set_text_overrun_behavior(overrun_behavior); + text_buf->add_string(atr(text), font, font_size, language); +} + +HorizontalAlignment FoldableContainer::_get_actual_alignment() const { + if (is_layout_rtl()) { + if (text_alignment == HORIZONTAL_ALIGNMENT_RIGHT) { + return HORIZONTAL_ALIGNMENT_LEFT; + } else if (text_alignment == HORIZONTAL_ALIGNMENT_LEFT) { + return HORIZONTAL_ALIGNMENT_RIGHT; + } + } + return text_alignment; +} + +void FoldableContainer::_update_group() { + foldable_group->updating_group = true; + for (FoldableContainer *container : foldable_group->containers) { + if (container != this) { + container->set_folded(true); + } + } + foldable_group->updating_group = false; +} + +void FoldableContainer::_draw_flippable_stylebox(const Ref p_stylebox, const Rect2 &p_rect) { + if (title_position == POSITION_BOTTOM) { + Rect2 rect(-p_rect.position, p_rect.size); + draw_set_transform(Point2(0.0, p_stylebox->get_draw_rect(rect).size.height), 0.0, Size2(1.0, -1.0)); + p_stylebox->draw(get_canvas_item(), rect); + draw_set_transform_matrix(Transform2D()); + } else { + p_stylebox->draw(get_canvas_item(), p_rect); + } +} + +void FoldableContainer::_bind_methods() { + ClassDB::bind_method(D_METHOD("fold"), &FoldableContainer::fold); + ClassDB::bind_method(D_METHOD("expand"), &FoldableContainer::expand); + ClassDB::bind_method(D_METHOD("set_folded", "folded"), &FoldableContainer::set_folded); + ClassDB::bind_method(D_METHOD("is_folded"), &FoldableContainer::is_folded); + ClassDB::bind_method(D_METHOD("set_foldable_group", "button_group"), &FoldableContainer::set_foldable_group); + ClassDB::bind_method(D_METHOD("get_foldable_group"), &FoldableContainer::get_foldable_group); + ClassDB::bind_method(D_METHOD("set_text", "text"), &FoldableContainer::set_text); + ClassDB::bind_method(D_METHOD("get_text"), &FoldableContainer::get_text); + ClassDB::bind_method(D_METHOD("set_title_alignment", "alignment"), &FoldableContainer::set_text_alignment); + ClassDB::bind_method(D_METHOD("get_title_alignment"), &FoldableContainer::get_text_alignment); + ClassDB::bind_method(D_METHOD("set_language", "language"), &FoldableContainer::set_language); + ClassDB::bind_method(D_METHOD("get_language"), &FoldableContainer::get_language); + ClassDB::bind_method(D_METHOD("set_text_direction", "text_direction"), &FoldableContainer::set_text_direction); + ClassDB::bind_method(D_METHOD("get_text_direction"), &FoldableContainer::get_text_direction); + ClassDB::bind_method(D_METHOD("set_text_overrun_behavior", "overrun_behavior"), &FoldableContainer::set_text_overrun_behavior); + ClassDB::bind_method(D_METHOD("get_text_overrun_behavior"), &FoldableContainer::get_text_overrun_behavior); + ClassDB::bind_method(D_METHOD("set_title_position", "title_position"), &FoldableContainer::set_title_position); + ClassDB::bind_method(D_METHOD("get_title_position"), &FoldableContainer::get_title_position); + ClassDB::bind_method(D_METHOD("add_title_bar_control", "control"), &FoldableContainer::add_title_bar_control); + ClassDB::bind_method(D_METHOD("remove_title_bar_control", "control"), &FoldableContainer::remove_title_bar_control); + + ADD_SIGNAL(MethodInfo("folding_changed", PropertyInfo(Variant::BOOL, "is_folded"))); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "folded"), "set_folded", "is_folded"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "title_alignment", PROPERTY_HINT_ENUM, "Left,Center,Right"), "set_title_alignment", "get_title_alignment"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "title_position", PROPERTY_HINT_ENUM, "Top,Bottom"), "set_title_position", "get_title_position"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "text_overrun_behavior", PROPERTY_HINT_ENUM, "Trim Nothing,Trim Characters,Trim Words,Ellipsis,Word Ellipsis"), "set_text_overrun_behavior", "get_text_overrun_behavior"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "foldable_group", PROPERTY_HINT_RESOURCE_TYPE, "FoldableGroup"), "set_foldable_group", "get_foldable_group"); + + ADD_GROUP("BiDi", ""); + ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID), "set_language", "get_language"); + + BIND_ENUM_CONSTANT(POSITION_TOP); + BIND_ENUM_CONSTANT(POSITION_BOTTOM); + + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_style, "title_panel"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_hover_style, "title_hover_panel"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_style, "title_collapsed_panel"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, title_collapsed_hover_style, "title_collapsed_hover_panel"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, focus_style, "focus"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, FoldableContainer, panel_style, "panel"); + + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT, FoldableContainer, title_font, "font"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_FONT_SIZE, FoldableContainer, title_font_size, "font_size"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, title_font_outline_size, "outline_size"); + + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_color, "font_color"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_hovered_font_color, "hover_font_color"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_collapsed_font_color, "collapsed_font_color"); + BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, FoldableContainer, title_font_outline_color, "font_outline_color"); + + BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow); + BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, expanded_arrow_mirrored); + BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow); + BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FoldableContainer, folded_arrow_mirrored); + + BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, FoldableContainer, h_separation); +} + +FoldableContainer::FoldableContainer(const String &p_text) { + text_buf.instantiate(); + set_text(p_text); + set_focus_mode(FOCUS_ALL); + set_mouse_filter(MOUSE_FILTER_STOP); +} + +FoldableContainer::~FoldableContainer() { + if (foldable_group.is_valid()) { + foldable_group->containers.erase(this); + } +} + +FoldableContainer *FoldableGroup::get_expanded_container() const { + for (FoldableContainer *container : containers) { + if (!container->is_folded()) { + return container; + } + } + + return nullptr; +} + +void FoldableGroup::set_allow_folding_all(bool p_enabled) { + allow_folding_all = p_enabled; + if (!allow_folding_all && !get_expanded_container() && containers.size() > 0) { + updating_group = true; + (*containers.begin())->set_folded(false); + updating_group = false; + } +} + +bool FoldableGroup::is_allow_folding_all() const { + return allow_folding_all; +} + +void FoldableGroup::get_containers(List *r_containers) const { + for (FoldableContainer *container : containers) { + r_containers->push_back(container); + } +} + +TypedArray FoldableGroup::_get_containers() const { + TypedArray foldable_containers; + for (const FoldableContainer *container : containers) { + foldable_containers.push_back(container); + } + + return foldable_containers; +} + +void FoldableGroup::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_expanded_container"), &FoldableGroup::get_expanded_container); + ClassDB::bind_method(D_METHOD("get_containers"), &FoldableGroup::_get_containers); + ClassDB::bind_method(D_METHOD("set_allow_folding_all", "enabled"), &FoldableGroup::set_allow_folding_all); + ClassDB::bind_method(D_METHOD("is_allow_folding_all"), &FoldableGroup::is_allow_folding_all); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_folding_all"), "set_allow_folding_all", "is_allow_folding_all"); + + ADD_SIGNAL(MethodInfo("expanded", PropertyInfo(Variant::OBJECT, "container", PROPERTY_HINT_RESOURCE_TYPE, "FoldableContainer"))); +} + +FoldableGroup::FoldableGroup() { + set_local_to_scene(true); +} diff --git a/scene/gui/foldable_container.h b/scene/gui/foldable_container.h new file mode 100644 index 00000000000..40a0b870c12 --- /dev/null +++ b/scene/gui/foldable_container.h @@ -0,0 +1,171 @@ +/**************************************************************************/ +/* foldable_container.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#pragma once + +#include "scene/gui/container.h" + +class FoldableGroup; +class TextLine; + +class FoldableContainer : public Container { + GDCLASS(FoldableContainer, Container); + +public: + enum TitlePosition { + POSITION_TOP, + POSITION_BOTTOM, + POSITION_MAX + }; + +private: + bool folded = false; + String text; + Ref foldable_group; + String language; + TextDirection text_direction = TEXT_DIRECTION_AUTO; + HorizontalAlignment text_alignment = HORIZONTAL_ALIGNMENT_LEFT; + TextServer::OverrunBehavior overrun_behavior = TextServer::OVERRUN_NO_TRIMMING; + TitlePosition title_position = POSITION_TOP; + + Ref text_buf; + bool changing_group = false; + bool is_hovering = false; + mutable Vector2 title_minimum_size; + + LocalVector title_controls; + + struct ThemeCache { + Ref title_style; + Ref title_hover_style; + Ref title_collapsed_style; + Ref title_collapsed_hover_style; + Ref panel_style; + Ref focus_style; + + Color title_font_color; + Color title_hovered_font_color; + Color title_collapsed_font_color; + Color title_font_outline_color; + + Ref title_font; + int title_font_size = 0; + int title_font_outline_size = 0; + + Ref expanded_arrow; + Ref expanded_arrow_mirrored; + Ref folded_arrow; + Ref folded_arrow_mirrored; + + int h_separation = 0; + } theme_cache; + + Ref _get_title_style() const; + Ref _get_title_icon() const; + int _get_h_separation() const { return MAX(theme_cache.h_separation, 0); } + real_t _get_title_controls_width() const; + + void _update_title_min_size() const; + void _shape(); + HorizontalAlignment _get_actual_alignment() const; + void _update_group(); + void _draw_flippable_stylebox(const Ref p_stylebox, const Rect2 &p_rect); + +protected: + virtual void gui_input(const Ref &p_event) override; + virtual String get_tooltip(const Point2 &p_pos) const override; + void _notification(int p_what); + static void _bind_methods(); + +public: + void fold(); + void expand(); + + void set_folded(bool p_folded); + bool is_folded() const; + + void set_foldable_group(const Ref &p_group); + Ref get_foldable_group() const; + + void set_text(const String &p_text); + String get_text() const; + + void set_text_alignment(HorizontalAlignment p_alignment); + HorizontalAlignment get_text_alignment() const; + + void set_text_direction(TextDirection p_text_direction); + TextDirection get_text_direction() const; + + void set_text_overrun_behavior(TextServer::OverrunBehavior p_overrun_behavior); + TextServer::OverrunBehavior get_text_overrun_behavior() const; + + void set_language(const String &p_language); + String get_language() const; + + void set_title_position(TitlePosition p_title_position); + TitlePosition get_title_position() const; + + void add_title_bar_control(Control *p_control); + void remove_title_bar_control(Control *p_control); + + virtual Size2 get_minimum_size() const override; + + virtual Vector get_allowed_size_flags_horizontal() const override { return { SIZE_FILL, SIZE_SHRINK_BEGIN, SIZE_SHRINK_CENTER, SIZE_SHRINK_END }; } + virtual Vector get_allowed_size_flags_vertical() const override { return { SIZE_FILL, SIZE_SHRINK_BEGIN, SIZE_SHRINK_CENTER, SIZE_SHRINK_END }; } + + FoldableContainer(const String &p_text = String()); + ~FoldableContainer(); +}; + +VARIANT_ENUM_CAST(FoldableContainer::TitlePosition); + +class FoldableGroup : public Resource { + GDCLASS(FoldableGroup, Resource); + + friend class FoldableContainer; + + HashSet containers; + bool allow_folding_all = false; + bool updating_group = false; + +protected: + static void _bind_methods(); + +public: + FoldableContainer *get_expanded_container() const; + + void get_containers(List *r_containers) const; + TypedArray _get_containers() const; + + void set_allow_folding_all(bool p_enabled); + bool is_allow_folding_all() const; + + FoldableGroup(); +}; diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index eaa809b1eb8..54fcb34fd91 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -57,6 +57,7 @@ #include "scene/gui/dialogs.h" #include "scene/gui/file_dialog.h" #include "scene/gui/flow_container.h" +#include "scene/gui/foldable_container.h" #include "scene/gui/graph_edit.h" #include "scene/gui/graph_frame.h" #include "scene/gui/graph_node.h" @@ -519,6 +520,9 @@ void register_scene_types() { GDREGISTER_CLASS(GraphFrame); GDREGISTER_CLASS(GraphEdit); + GDREGISTER_CLASS(FoldableGroup); + GDREGISTER_CLASS(FoldableContainer); + OS::get_singleton()->yield(); // may take time to init int swap_cancel_ok = GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/common/swap_cancel_ok", PROPERTY_HINT_ENUM, "Auto,Cancel First,OK First"), 0); diff --git a/scene/theme/default_theme.cpp b/scene/theme/default_theme.cpp index ec1dea39208..f9b21faaa81 100644 --- a/scene/theme/default_theme.cpp +++ b/scene/theme/default_theme.cpp @@ -1259,6 +1259,40 @@ void fill_default_theme(Ref &theme, const Ref &default_font, const theme->set_color("connection_valid_target_tint_color", "GraphEdit", Color(1, 1, 1, 0.4)); theme->set_color("connection_rim_color", "GraphEdit", style_normal_color); + Ref foldable_container_title = make_flat_stylebox(style_pressed_color); + foldable_container_title->set_corner_radius(CORNER_BOTTOM_LEFT, 0); + foldable_container_title->set_corner_radius(CORNER_BOTTOM_RIGHT, 0); + theme->set_stylebox("title_panel", "FoldableContainer", foldable_container_title); + Ref foldable_container_hover = make_flat_stylebox(style_hover_color); + foldable_container_hover->set_corner_radius(CORNER_BOTTOM_LEFT, 0); + foldable_container_hover->set_corner_radius(CORNER_BOTTOM_RIGHT, 0); + theme->set_stylebox("title_hover_panel", "FoldableContainer", foldable_container_hover); + theme->set_stylebox("title_collapsed_panel", "FoldableContainer", make_flat_stylebox(style_pressed_color)); + theme->set_stylebox("title_collapsed_hover_panel", "FoldableContainer", make_flat_stylebox(style_hover_color)); + Ref foldable_container_panel = make_flat_stylebox(style_normal_color); + foldable_container_panel->set_content_margin_all(default_margin); + foldable_container_panel->set_corner_radius(CORNER_TOP_LEFT, 0); + foldable_container_panel->set_corner_radius(CORNER_TOP_RIGHT, 0); + theme->set_stylebox(SceneStringName(panel), "FoldableContainer", foldable_container_panel); + Ref foldable_focus_style = make_flat_stylebox(style_focus_color, default_margin, default_margin, default_margin, default_margin, default_corner_radius, false, 2); + theme->set_stylebox("focus", "FoldableContainer", foldable_focus_style); + + theme->set_font(SceneStringName(font), "FoldableContainer", Ref()); + theme->set_font_size(SceneStringName(font_size), "FoldableContainer", default_font_size); + + theme->set_color(SceneStringName(font_color), "FoldableContainer", control_font_color); + theme->set_color("hover_font_color", "FoldableContainer", control_font_hover_color); + theme->set_color("collapsed_font_color", "FoldableContainer", control_font_pressed_color); + theme->set_color("font_outline_color", "FoldableContainer", Color(1, 1, 1)); + + theme->set_icon("expanded_arrow", "FoldableContainer", icons["arrow_down"]); + theme->set_icon("expanded_arrow_mirrored", "FoldableContainer", icons["arrow_up"]); + theme->set_icon("folded_arrow", "FoldableContainer", icons["arrow_right"]); + theme->set_icon("folded_arrow_mirrored", "FoldableContainer", icons["arrow_left"]); + + theme->set_constant("outline_size", "FoldableContainer", 0); + theme->set_constant("h_separation", "FoldableContainer", Math::round(2 * scale)); + // Visual Node Ports theme->set_constant("port_hotzone_inner_extent", "GraphEdit", 22 * scale); diff --git a/scene/theme/icons/arrow_up.svg b/scene/theme/icons/arrow_up.svg new file mode 100644 index 00000000000..835f861e5b2 --- /dev/null +++ b/scene/theme/icons/arrow_up.svg @@ -0,0 +1 @@ +