Skip to content

Commit

Permalink
Merge pull request #65192 from YuriSizov/control-customizable-cache-p2
Browse files Browse the repository at this point in the history
  • Loading branch information
akien-mga authored Sep 1, 2022
2 parents dcd7456 + 15fd025 commit 8c7be63
Show file tree
Hide file tree
Showing 12 changed files with 371 additions and 188 deletions.
6 changes: 6 additions & 0 deletions doc/classes/AcceptDialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
</signal>
</signals>
<theme_items>
<theme_item name="button_margin" data_type="constant" type="int" default="32">
Offset that is applied to the content of the window on the bottom, effectively moving the button row.
</theme_item>
<theme_item name="margin" data_type="constant" type="int" default="8">
Offset that is applied to the content of the window on top, left, and right.
</theme_item>
<theme_item name="panel" data_type="style" type="StyleBox">
Panel that fills up the background of the window.
</theme_item>
Expand Down
31 changes: 18 additions & 13 deletions scene/gui/dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ void AcceptDialog::_parent_focused() {
}
}

void AcceptDialog::_update_theme_item_cache() {
Window::_update_theme_item_cache();

theme_cache.panel_style = get_theme_stylebox(SNAME("panel"));
theme_cache.margin = get_theme_constant(SNAME("margin"));
theme_cache.button_margin = get_theme_constant(SNAME("button_margin"));
}

void AcceptDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_VISIBILITY_CHANGED: {
Expand All @@ -69,7 +77,10 @@ void AcceptDialog::_notification(int p_what) {
} break;

case NOTIFICATION_THEME_CHANGED: {
bg->add_theme_style_override("panel", bg->get_theme_stylebox(SNAME("panel"), SNAME("AcceptDialog")));
bg->add_theme_style_override("panel", theme_cache.panel_style);

label->set_begin(Point2(theme_cache.margin, theme_cache.margin));
label->set_end(Point2(-theme_cache.margin, -theme_cache.button_margin - 10));
} break;

case NOTIFICATION_EXIT_TREE: {
Expand Down Expand Up @@ -185,12 +196,12 @@ void AcceptDialog::_update_child_rects() {
if (label->get_text().is_empty()) {
label_size.height = 0;
}
int margin = hbc->get_theme_constant(SNAME("margin"), SNAME("Dialogs"));

Size2 size = get_size();
Size2 hminsize = hbc->get_combined_minimum_size();

Vector2 cpos(margin, margin + label_size.height);
Vector2 csize(size.x - margin * 2, size.y - margin * 3 - hminsize.y - label_size.height);
Vector2 cpos(theme_cache.margin, theme_cache.margin + label_size.height);
Vector2 csize(size.x - theme_cache.margin * 2, size.y - theme_cache.margin * 3 - hminsize.y - label_size.height);

for (int i = 0; i < get_child_count(); i++) {
Control *c = Object::cast_to<Control>(get_child(i));
Expand All @@ -206,7 +217,7 @@ void AcceptDialog::_update_child_rects() {
c->set_size(csize);
}

cpos.y += csize.y + margin;
cpos.y += csize.y + theme_cache.margin;
csize.y = hminsize.y;

hbc->set_position(cpos);
Expand All @@ -217,7 +228,6 @@ void AcceptDialog::_update_child_rects() {
}

Size2 AcceptDialog::_get_contents_minimum_size() const {
int margin = hbc->get_theme_constant(SNAME("margin"), SNAME("Dialogs"));
Size2 minsize = label->get_combined_minimum_size();

for (int i = 0; i < get_child_count(); i++) {
Expand All @@ -238,8 +248,8 @@ Size2 AcceptDialog::_get_contents_minimum_size() const {
Size2 hminsize = hbc->get_combined_minimum_size();
minsize.x = MAX(hminsize.x, minsize.x);
minsize.y += hminsize.y;
minsize.x += margin * 2;
minsize.y += margin * 3; //one as separation between hbc and child
minsize.x += theme_cache.margin * 2;
minsize.y += theme_cache.margin * 3; //one as separation between hbc and child

Size2 wmsize = get_min_size();
minsize.x = MAX(wmsize.x, minsize.x);
Expand Down Expand Up @@ -350,14 +360,9 @@ AcceptDialog::AcceptDialog() {

hbc = memnew(HBoxContainer);

int margin = hbc->get_theme_constant(SNAME("margin"), SNAME("Dialogs"));
int button_margin = hbc->get_theme_constant(SNAME("button_margin"), SNAME("Dialogs"));

label = memnew(Label);
label->set_anchor(SIDE_RIGHT, Control::ANCHOR_END);
label->set_anchor(SIDE_BOTTOM, Control::ANCHOR_END);
label->set_begin(Point2(margin, margin));
label->set_end(Point2(-margin, -button_margin - 10));
add_child(label, false, INTERNAL_MODE_FRONT);

add_child(hbc, false, INTERNAL_MODE_FRONT);
Expand Down
7 changes: 7 additions & 0 deletions scene/gui/dialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class AcceptDialog : public Window {
bool hide_on_ok = true;
bool close_on_escape = true;

struct ThemeCache {
Ref<StyleBox> panel_style;
int margin = 0;
int button_margin = 0;
} theme_cache;

void _custom_action(const String &p_action);
void _update_child_rects();

Expand All @@ -62,6 +68,7 @@ class AcceptDialog : public Window {

protected:
virtual Size2 _get_contents_minimum_size() const override;
virtual void _update_theme_item_cache() override;

void _notification(int p_what);
static void _bind_methods();
Expand Down
107 changes: 58 additions & 49 deletions scene/gui/file_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,26 @@ VBoxContainer *FileDialog::get_vbox() {
return vbox;
}

void FileDialog::_theme_changed() {
Color font_color = vbox->get_theme_color(SNAME("font_color"), SNAME("Button"));
Color font_hover_color = vbox->get_theme_color(SNAME("font_hover_color"), SNAME("Button"));
Color font_focus_color = vbox->get_theme_color(SNAME("font_focus_color"), SNAME("Button"));
Color font_pressed_color = vbox->get_theme_color(SNAME("font_pressed_color"), SNAME("Button"));

dir_up->add_theme_color_override("icon_normal_color", font_color);
dir_up->add_theme_color_override("icon_hover_color", font_hover_color);
dir_up->add_theme_color_override("icon_focus_color", font_focus_color);
dir_up->add_theme_color_override("icon_pressed_color", font_pressed_color);

dir_prev->add_theme_color_override("icon_color_normal", font_color);
dir_prev->add_theme_color_override("icon_color_hover", font_hover_color);
dir_prev->add_theme_color_override("icon_focus_color", font_focus_color);
dir_prev->add_theme_color_override("icon_color_pressed", font_pressed_color);

dir_next->add_theme_color_override("icon_color_normal", font_color);
dir_next->add_theme_color_override("icon_color_hover", font_hover_color);
dir_next->add_theme_color_override("icon_focus_color", font_focus_color);
dir_next->add_theme_color_override("icon_color_pressed", font_pressed_color);

refresh->add_theme_color_override("icon_normal_color", font_color);
refresh->add_theme_color_override("icon_hover_color", font_hover_color);
refresh->add_theme_color_override("icon_focus_color", font_focus_color);
refresh->add_theme_color_override("icon_pressed_color", font_pressed_color);

show_hidden->add_theme_color_override("icon_normal_color", font_color);
show_hidden->add_theme_color_override("icon_hover_color", font_hover_color);
show_hidden->add_theme_color_override("icon_focus_color", font_focus_color);
show_hidden->add_theme_color_override("icon_pressed_color", font_pressed_color);
void FileDialog::_update_theme_item_cache() {
ConfirmationDialog::_update_theme_item_cache();

theme_cache.parent_folder = get_theme_icon(SNAME("parent_folder"));
theme_cache.forward_folder = get_theme_icon(SNAME("forward_folder"));
theme_cache.back_folder = get_theme_icon(SNAME("back_folder"));
theme_cache.reload = get_theme_icon(SNAME("reload"));
theme_cache.toggle_hidden = get_theme_icon(SNAME("toggle_hidden"));
theme_cache.folder = get_theme_icon(SNAME("folder"));
theme_cache.file = get_theme_icon(SNAME("file"));

theme_cache.folder_icon_modulate = get_theme_color(SNAME("folder_icon_modulate"));
theme_cache.file_icon_modulate = get_theme_color(SNAME("file_icon_modulate"));
theme_cache.files_disabled = get_theme_color(SNAME("files_disabled"));

// TODO: Define own colors?
theme_cache.icon_normal_color = get_theme_color(SNAME("font_color"), SNAME("Button"));
theme_cache.icon_hover_color = get_theme_color(SNAME("font_hover_color"), SNAME("Button"));
theme_cache.icon_focus_color = get_theme_color(SNAME("font_focus_color"), SNAME("Button"));
theme_cache.icon_pressed_color = get_theme_color(SNAME("font_pressed_color"), SNAME("Button"));
}

void FileDialog::_notification(int p_what) {
Expand All @@ -99,18 +89,42 @@ void FileDialog::_notification(int p_what) {
}
} break;

case NOTIFICATION_ENTER_TREE: {
dir_up->set_icon(vbox->get_theme_icon(SNAME("parent_folder"), SNAME("FileDialog")));
case NOTIFICATION_THEME_CHANGED: {
dir_up->set_icon(theme_cache.parent_folder);
if (vbox->is_layout_rtl()) {
dir_prev->set_icon(vbox->get_theme_icon(SNAME("forward_folder"), SNAME("FileDialog")));
dir_next->set_icon(vbox->get_theme_icon(SNAME("back_folder"), SNAME("FileDialog")));
dir_prev->set_icon(theme_cache.forward_folder);
dir_next->set_icon(theme_cache.back_folder);
} else {
dir_prev->set_icon(vbox->get_theme_icon(SNAME("back_folder"), SNAME("FileDialog")));
dir_next->set_icon(vbox->get_theme_icon(SNAME("forward_folder"), SNAME("FileDialog")));
dir_prev->set_icon(theme_cache.back_folder);
dir_next->set_icon(theme_cache.forward_folder);
}
refresh->set_icon(vbox->get_theme_icon(SNAME("reload"), SNAME("FileDialog")));
show_hidden->set_icon(vbox->get_theme_icon(SNAME("toggle_hidden"), SNAME("FileDialog")));
_theme_changed();
refresh->set_icon(theme_cache.reload);
show_hidden->set_icon(theme_cache.toggle_hidden);

dir_up->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
dir_up->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
dir_up->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
dir_up->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);

dir_prev->add_theme_color_override("icon_color_normal", theme_cache.icon_normal_color);
dir_prev->add_theme_color_override("icon_color_hover", theme_cache.icon_hover_color);
dir_prev->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
dir_prev->add_theme_color_override("icon_color_pressed", theme_cache.icon_pressed_color);

dir_next->add_theme_color_override("icon_color_normal", theme_cache.icon_normal_color);
dir_next->add_theme_color_override("icon_color_hover", theme_cache.icon_hover_color);
dir_next->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
dir_next->add_theme_color_override("icon_color_pressed", theme_cache.icon_pressed_color);

refresh->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
refresh->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
refresh->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
refresh->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);

show_hidden->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
show_hidden->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
show_hidden->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
show_hidden->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
} break;

case NOTIFICATION_TRANSLATION_CHANGED: {
Expand Down Expand Up @@ -506,10 +520,6 @@ void FileDialog::update_file_list() {
}

TreeItem *root = tree->create_item();
Ref<Texture2D> folder = vbox->get_theme_icon(SNAME("folder"), SNAME("FileDialog"));
Ref<Texture2D> file_icon = vbox->get_theme_icon(SNAME("file"), SNAME("FileDialog"));
const Color folder_color = vbox->get_theme_color(SNAME("folder_icon_modulate"), SNAME("FileDialog"));
const Color file_color = vbox->get_theme_color(SNAME("file_icon_modulate"), SNAME("FileDialog"));
List<String> files;
List<String> dirs;

Expand Down Expand Up @@ -541,8 +551,8 @@ void FileDialog::update_file_list() {
String &dir_name = dirs.front()->get();
TreeItem *ti = tree->create_item(root);
ti->set_text(0, dir_name);
ti->set_icon(0, folder);
ti->set_icon_modulate(0, folder_color);
ti->set_icon(0, theme_cache.folder);
ti->set_icon_modulate(0, theme_cache.folder_icon_modulate);

Dictionary d;
d["name"] = dir_name;
Expand Down Expand Up @@ -601,12 +611,12 @@ void FileDialog::update_file_list() {
Ref<Texture2D> icon = get_icon_func(base_dir.path_join(files.front()->get()));
ti->set_icon(0, icon);
} else {
ti->set_icon(0, file_icon);
ti->set_icon(0, theme_cache.file);
}
ti->set_icon_modulate(0, file_color);
ti->set_icon_modulate(0, theme_cache.file_icon_modulate);

if (mode == FILE_MODE_OPEN_DIR) {
ti->set_custom_color(0, vbox->get_theme_color(SNAME("files_disabled"), SNAME("FileDialog")));
ti->set_custom_color(0, theme_cache.files_disabled);
ti->set_selectable(0, false);
}
Dictionary d;
Expand Down Expand Up @@ -1006,7 +1016,6 @@ FileDialog::FileDialog() {

vbox = memnew(VBoxContainer);
add_child(vbox, false, INTERNAL_MODE_FRONT);
vbox->connect("theme_changed", callable_mp(this, &FileDialog::_theme_changed));

mode = FILE_MODE_SAVE_FILE;
set_title(TTRC("Save a File"));
Expand Down
21 changes: 20 additions & 1 deletion scene/gui/file_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,25 @@ class FileDialog : public ConfirmationDialog {

bool invalidated = true;

struct ThemeCache {
Ref<Texture2D> parent_folder;
Ref<Texture2D> forward_folder;
Ref<Texture2D> back_folder;
Ref<Texture2D> reload;
Ref<Texture2D> toggle_hidden;
Ref<Texture2D> folder;
Ref<Texture2D> file;

Color folder_icon_modulate;
Color file_icon_modulate;
Color files_disabled;

Color icon_normal_color;
Color icon_hover_color;
Color icon_focus_color;
Color icon_pressed_color;
} theme_cache;

void update_dir();
void update_file_name();
void update_file_list();
Expand Down Expand Up @@ -143,7 +162,7 @@ class FileDialog : public ConfirmationDialog {
virtual void _post_popup() override;

protected:
void _theme_changed();
virtual void _update_theme_item_cache() override;

void _notification(int p_what);
static void _bind_methods();
Expand Down
30 changes: 17 additions & 13 deletions scene/gui/popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ void Popup::_deinitialize_visible_parents() {
}
}

void Popup::_update_theme_item_cache() {
Window::_update_theme_item_cache();

theme_cache.panel_style = get_theme_stylebox(SNAME("panel"));
}

void Popup::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_VISIBILITY_CHANGED: {
Expand Down Expand Up @@ -186,8 +192,6 @@ Popup::~Popup() {
}

Size2 PopupPanel::_get_contents_minimum_size() const {
Ref<StyleBox> p = get_theme_stylebox(SNAME("panel"), get_class_name());

Size2 ms;

for (int i = 0; i < get_child_count(); i++) {
Expand All @@ -205,14 +209,12 @@ Size2 PopupPanel::_get_contents_minimum_size() const {
ms.y = MAX(cms.y, ms.y);
}

return ms + p->get_minimum_size();
return ms + theme_cache.panel_style->get_minimum_size();
}

void PopupPanel::_update_child_rects() {
Ref<StyleBox> p = get_theme_stylebox(SNAME("panel"), get_class_name());

Vector2 cpos(p->get_offset());
Vector2 csize(get_size() - p->get_minimum_size());
Vector2 cpos(theme_cache.panel_style->get_offset());
Vector2 csize(get_size() - theme_cache.panel_style->get_minimum_size());

for (int i = 0; i < get_child_count(); i++) {
Control *c = Object::cast_to<Control>(get_child(i));
Expand All @@ -234,15 +236,17 @@ void PopupPanel::_update_child_rects() {
}
}

void PopupPanel::_update_theme_item_cache() {
Popup::_update_theme_item_cache();

theme_cache.panel_style = get_theme_stylebox(SNAME("panel"));
}

void PopupPanel::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY:
case NOTIFICATION_THEME_CHANGED: {
panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), get_class_name()));
} break;

case NOTIFICATION_ENTER_TREE:
case NOTIFICATION_READY: {
panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), get_class_name()));
panel->add_theme_style_override("panel", theme_cache.panel_style);
_update_child_rects();
} break;

Expand Down
Loading

0 comments on commit 8c7be63

Please sign in to comment.