From 127fde5e20777b9ac24807000726ed3fad3d68e8 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Sun, 4 Aug 2024 19:50:00 +0300 Subject: [PATCH] [FileDialog] Rework option controls. --- editor/gui/editor_file_dialog.cpp | 67 +++++++++++++++++++++++-------- editor/gui/editor_file_dialog.h | 5 ++- scene/gui/file_dialog.cpp | 64 ++++++++++++++++++++++------- scene/gui/file_dialog.h | 4 +- 4 files changed, 103 insertions(+), 37 deletions(-) diff --git a/editor/gui/editor_file_dialog.cpp b/editor/gui/editor_file_dialog.cpp index afc6d58d6314..72f64e7b8eff 100644 --- a/editor/gui/editor_file_dialog.cpp +++ b/editor/gui/editor_file_dialog.cpp @@ -42,8 +42,7 @@ #include "editor/filesystem_dock.h" #include "editor/themes/editor_scale.h" #include "scene/gui/center_container.h" -#include "scene/gui/check_box.h" -#include "scene/gui/grid_container.h" +#include "scene/gui/check_button.h" #include "scene/gui/label.h" #include "scene/gui/margin_container.h" #include "scene/gui/option_button.h" @@ -194,6 +193,8 @@ void EditorFileDialog::_update_theme_item_cache() { theme_cache.folder_big_thumbnail = get_editor_theme_icon(SNAME("FolderBigThumb")); theme_cache.file_big_thumbnail = get_editor_theme_icon(SNAME("FileBigThumb")); + theme_cache.check_box_normal = get_theme_stylebox(CoreStringName(normal), "CheckButton"); + theme_cache.progress[0] = get_editor_theme_icon("Progress1"); theme_cache.progress[1] = get_editor_theme_icon("Progress2"); theme_cache.progress[2] = get_editor_theme_icon("Progress3"); @@ -210,6 +211,18 @@ void EditorFileDialog::_notification(int p_what) { case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED: case NOTIFICATION_TRANSLATION_CHANGED: { _update_icons(); + for (int i = 0; i < vbox_options->get_child_count(); i++) { + MarginContainer *mb_option = Object::cast_to(vbox_options->get_child(i)); + if (mb_option) { + if (theme_cache.check_box_normal.is_valid()) { + mb_option->add_theme_constant_override("margin_left", theme_cache.check_box_normal->get_margin(SIDE_LEFT)); + mb_option->add_theme_constant_override("margin_right", theme_cache.check_box_normal->get_margin(SIDE_RIGHT)); + } else { + mb_option->remove_theme_font_override("margin_left"); + mb_option->remove_theme_font_override("margin_right"); + } + } + } invalidate(); } break; @@ -1764,30 +1777,50 @@ void EditorFileDialog::_update_option_controls() { } options_dirty = false; - while (grid_options->get_child_count() > 0) { - Node *child = grid_options->get_child(0); - grid_options->remove_child(child); + while (vbox_options->get_child_count() > 0) { + Node *child = vbox_options->get_child(0); + vbox_options->remove_child(child); child->queue_free(); } selected_options.clear(); for (const EditorFileDialog::Option &opt : options) { - Label *lbl = memnew(Label); - lbl->set_text(opt.name); - grid_options->add_child(lbl); if (opt.values.is_empty()) { - CheckBox *cb = memnew(CheckBox); + CheckButton *cb = memnew(CheckButton); + cb->set_text(opt.name); cb->set_pressed(opt.default_idx); - grid_options->add_child(cb); + vbox_options->add_child(cb); + cb->connect("toggled", callable_mp(this, &EditorFileDialog::_option_changed_checkbox_toggled).bind(opt.name)); selected_options[opt.name] = (bool)opt.default_idx; } else { + MarginContainer *mb_option = memnew(MarginContainer); + if (theme_cache.check_box_normal.is_valid()) { + mb_option->add_theme_constant_override("margin_left", theme_cache.check_box_normal->get_margin(SIDE_LEFT)); + mb_option->add_theme_constant_override("margin_right", theme_cache.check_box_normal->get_margin(SIDE_RIGHT)); + } + vbox_options->add_child(mb_option); + + HBoxContainer *hb_option = memnew(HBoxContainer); + hb_option->set_alignment(BoxContainer::ALIGNMENT_CENTER); + mb_option->add_child(hb_option); + + Label *lbl = memnew(Label); + lbl->set_text(opt.name); + lbl->add_theme_style_override(CoreStringName(normal), memnew(StyleBoxEmpty)); + hb_option->add_child(lbl); + + Control *spacer = memnew(Control); + spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL); + hb_option->add_child(spacer); + OptionButton *ob = memnew(OptionButton); for (const String &val : opt.values) { ob->add_item(val); } ob->select(opt.default_idx); - grid_options->add_child(ob); + hb_option->add_child(ob); + ob->connect(SceneStringName(item_selected), callable_mp(this, &EditorFileDialog::_option_changed_item_selected).bind(opt.name)); selected_options[opt.name] = opt.default_idx; } @@ -2057,11 +2090,11 @@ void EditorFileDialog::add_side_menu(Control *p_menu, const String &p_title) { void EditorFileDialog::_update_side_menu_visibility(bool p_native_dlg) { if (p_native_dlg) { pathhb->set_visible(false); - grid_options->set_visible(false); + vbox_options->set_visible(false); list_hb->set_visible(false); } else { pathhb->set_visible(true); - grid_options->set_visible(true); + vbox_options->set_visible(true); list_hb->set_visible(true); } } @@ -2191,10 +2224,10 @@ EditorFileDialog::EditorFileDialog() { body_hsplit->set_v_size_flags(Control::SIZE_EXPAND_FILL); vbc->add_child(body_hsplit); - grid_options = memnew(GridContainer); - grid_options->set_h_size_flags(Control::SIZE_SHRINK_CENTER); - grid_options->set_columns(2); - vbc->add_child(grid_options); + vbox_options = memnew(VBoxContainer); + vbox_options->set_h_size_flags(Control::SIZE_SHRINK_CENTER); + vbox_options->set_alignment(BoxContainer::ALIGNMENT_CENTER); + vbc->add_child(vbox_options); list_hb = memnew(HSplitContainer); list_hb->set_h_size_flags(Control::SIZE_EXPAND_FILL); diff --git a/editor/gui/editor_file_dialog.h b/editor/gui/editor_file_dialog.h index 6272e27f8260..7d8cd3bf03b0 100644 --- a/editor/gui/editor_file_dialog.h +++ b/editor/gui/editor_file_dialog.h @@ -35,7 +35,6 @@ #include "scene/gui/dialogs.h" #include "scene/property_list_helper.h" -class GridContainer; class DependencyRemoveDialog; class HSplitContainer; class ItemList; @@ -89,7 +88,7 @@ class EditorFileDialog : public ConfirmationDialog { Button *makedir = nullptr; Access access = ACCESS_RESOURCES; - GridContainer *grid_options = nullptr; + VBoxContainer *vbox_options = nullptr; VBoxContainer *vbox = nullptr; FileMode mode = FILE_MODE_SAVE_FILE; bool can_create_dir = false; @@ -177,6 +176,8 @@ class EditorFileDialog : public ConfirmationDialog { Ref folder_big_thumbnail; Ref file_big_thumbnail; + Ref check_box_normal; + Ref progress[8]{}; } theme_cache; diff --git a/scene/gui/file_dialog.cpp b/scene/gui/file_dialog.cpp index 8047369ab1ea..797b13706533 100644 --- a/scene/gui/file_dialog.cpp +++ b/scene/gui/file_dialog.cpp @@ -33,9 +33,9 @@ #include "core/config/project_settings.h" #include "core/os/keyboard.h" #include "core/string/print_string.h" -#include "scene/gui/check_box.h" -#include "scene/gui/grid_container.h" +#include "scene/gui/check_button.h" #include "scene/gui/label.h" +#include "scene/gui/margin_container.h" #include "scene/gui/option_button.h" #include "scene/theme/theme_db.h" @@ -249,6 +249,19 @@ void FileDialog::_notification(int p_what) { makedir->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color); makedir->end_bulk_theme_override(); + Ref sb = get_theme_stylebox(CoreStringName(normal), "CheckButton"); + for (int i = 0; i < vbox_options->get_child_count(); i++) { + MarginContainer *mb_option = Object::cast_to(vbox_options->get_child(i)); + if (mb_option) { + if (sb.is_valid()) { + mb_option->add_theme_constant_override("margin_left", sb->get_margin(SIDE_LEFT)); + mb_option->add_theme_constant_override("margin_right", sb->get_margin(SIDE_RIGHT)); + } else { + mb_option->remove_theme_font_override("margin_left"); + mb_option->remove_theme_font_override("margin_right"); + } + } + } invalidate(); } break; @@ -1128,30 +1141,51 @@ void FileDialog::_update_option_controls() { } options_dirty = false; - while (grid_options->get_child_count() > 0) { - Node *child = grid_options->get_child(0); - grid_options->remove_child(child); + while (vbox_options->get_child_count() > 0) { + Node *child = vbox_options->get_child(0); + vbox_options->remove_child(child); child->queue_free(); } selected_options.clear(); + Ref sb = get_theme_stylebox(CoreStringName(normal), "CheckButton"); for (const FileDialog::Option &opt : options) { - Label *lbl = memnew(Label); - lbl->set_text(opt.name); - grid_options->add_child(lbl); if (opt.values.is_empty()) { - CheckBox *cb = memnew(CheckBox); + CheckButton *cb = memnew(CheckButton); + cb->set_text(opt.name); cb->set_pressed(opt.default_idx); - grid_options->add_child(cb); + vbox_options->add_child(cb); + cb->connect("toggled", callable_mp(this, &FileDialog::_option_changed_checkbox_toggled).bind(opt.name)); selected_options[opt.name] = (bool)opt.default_idx; } else { + MarginContainer *mb_option = memnew(MarginContainer); + if (sb.is_valid()) { + mb_option->add_theme_constant_override("margin_left", sb->get_margin(SIDE_LEFT)); + mb_option->add_theme_constant_override("margin_right", sb->get_margin(SIDE_RIGHT)); + } + vbox_options->add_child(mb_option); + + HBoxContainer *hb_option = memnew(HBoxContainer); + hb_option->set_alignment(BoxContainer::ALIGNMENT_CENTER); + mb_option->add_child(hb_option); + + Label *lbl = memnew(Label); + lbl->set_text(opt.name); + lbl->add_theme_style_override(CoreStringName(normal), memnew(StyleBoxEmpty)); + hb_option->add_child(lbl); + + Control *spacer = memnew(Control); + spacer->set_h_size_flags(Control::SIZE_EXPAND_FILL); + hb_option->add_child(spacer); + OptionButton *ob = memnew(OptionButton); for (const String &val : opt.values) { ob->add_item(val); } ob->select(opt.default_idx); - grid_options->add_child(ob); + hb_option->add_child(ob); + ob->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_option_changed_item_selected).bind(opt.name)); selected_options[opt.name] = opt.default_idx; } @@ -1479,10 +1513,10 @@ FileDialog::FileDialog() { file_box->add_child(filter); vbox->add_child(file_box); - grid_options = memnew(GridContainer); - grid_options->set_h_size_flags(Control::SIZE_SHRINK_CENTER); - grid_options->set_columns(2); - vbox->add_child(grid_options); + vbox_options = memnew(VBoxContainer); + vbox_options->set_h_size_flags(Control::SIZE_SHRINK_CENTER); + vbox_options->set_alignment(BoxContainer::ALIGNMENT_CENTER); + vbox->add_child(vbox_options); dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES); _update_drives(); diff --git a/scene/gui/file_dialog.h b/scene/gui/file_dialog.h index 9680157f0a41..f9a40a25d964 100644 --- a/scene/gui/file_dialog.h +++ b/scene/gui/file_dialog.h @@ -39,8 +39,6 @@ #include "scene/gui/tree.h" #include "scene/property_list_helper.h" -class GridContainer; - class FileDialog : public ConfirmationDialog { GDCLASS(FileDialog, ConfirmationDialog); @@ -73,7 +71,7 @@ class FileDialog : public ConfirmationDialog { Button *makedir = nullptr; Access access = ACCESS_RESOURCES; VBoxContainer *vbox = nullptr; - GridContainer *grid_options = nullptr; + VBoxContainer *vbox_options = nullptr; FileMode mode; LineEdit *dir = nullptr; HBoxContainer *drives_container = nullptr;