From 1522762dc986c130ad63cbf854514d730788a4cf Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Fri, 31 Mar 2023 21:17:59 +0200 Subject: [PATCH] Make icons of scripted and custom classes fit the editor UI Also: - Add an option to limit the icon size in PopupMenu. This is similar to how this works in Tree and TreeItem. - Add the same option to TabBar. - Add a theme constant for Tree, PopupMenu, Button, and TabBar to apply this limit on the control level. Co-authored-by: Daylily-Zeleen --- doc/classes/Button.xml | 5 +- doc/classes/PopupMenu.xml | 18 ++++ doc/classes/TabBar.xml | 22 ++++- doc/classes/TabContainer.xml | 3 + doc/classes/Tree.xml | 3 + doc/classes/TreeItem.xml | 4 +- editor/create_dialog.cpp | 5 + editor/editor_data.cpp | 18 ++-- editor/editor_data.h | 2 +- editor/editor_help.cpp | 4 +- editor/editor_inspector.cpp | 10 +- editor/editor_node.cpp | 8 +- editor/editor_path.cpp | 9 +- editor/editor_resource_picker.cpp | 2 + editor/editor_themes.cpp | 2 + editor/scene_tree_dock.cpp | 35 ++++--- editor/scene_tree_editor.cpp | 2 + scene/gui/button.cpp | 23 ++++- scene/gui/button.h | 3 + scene/gui/popup_menu.cpp | 96 +++++++++++++++++-- scene/gui/popup_menu.h | 5 + scene/gui/tab_bar.cpp | 74 ++++++++++++-- scene/gui/tab_bar.h | 7 ++ scene/gui/tab_container.cpp | 2 + scene/gui/tab_container.h | 1 + scene/gui/tree.cpp | 54 ++++++----- scene/gui/tree.h | 6 +- .../resources/default_theme/default_theme.cpp | 5 + 28 files changed, 339 insertions(+), 89 deletions(-) diff --git a/doc/classes/Button.xml b/doc/classes/Button.xml index 9fce014125f..ec2447dbbc1 100644 --- a/doc/classes/Button.xml +++ b/doc/classes/Button.xml @@ -48,7 +48,7 @@ When this property is enabled, text that is too large to fit the button is clipped, when disabled the Button will always be wide enough to hold the text. - When enabled, the button's icon will expand/shrink to fit the button's size while keeping its aspect. + When enabled, the button's icon will expand/shrink to fit the button's size while keeping its aspect. See also [theme_item icon_max_width]. Flat buttons don't display decoration. @@ -116,6 +116,9 @@ The horizontal space between [Button]'s icon and text. Negative values will be treated as [code]0[/code] when used. + + The maximum allowed width of the [Button]'s icon. This limit is applied on top of the default size of the icon, or its expanded size if [member expand_icon] is [code]true[/code]. The height is adjusted according to the icon's ratio. + The size of the text outline. [b]Note:[/b] If using a font with [member FontFile.multichannel_signed_distance_field] enabled, its [member FontFile.msdf_pixel_range] must be set to at least [i]twice[/i] the value of [theme_item outline_size] for outline rendering to look correct. Otherwise, the outline may appear to be cut off earlier than intended. diff --git a/doc/classes/PopupMenu.xml b/doc/classes/PopupMenu.xml index 6787b5d20a7..6996061bd8d 100644 --- a/doc/classes/PopupMenu.xml +++ b/doc/classes/PopupMenu.xml @@ -202,6 +202,13 @@ Returns the icon of the item at the given [param index]. + + + + + Returns the maximum allowed width of the icon for the item at the given [param index]. + + @@ -397,6 +404,14 @@ Replaces the [Texture2D] icon of the item at the given [param index]. + + + + + + Sets the maximum allowed width of the icon for the item at the given [param index]. This limit is applied on top of the default size of the icon and on top of [theme_item icon_max_width]. The height is adjusted according to the icon's ratio. + + @@ -573,6 +588,9 @@ The horizontal space between the item's elements. + + The maximum allowed width of the item's icon. This limit is applied on top of the default size of the icon, but before the value set with [method set_item_icon_max_width]. The height is adjusted according to the icon's ratio. + Width of the single indentation level. diff --git a/doc/classes/TabBar.xml b/doc/classes/TabBar.xml index ae6d589339c..7211fc21378 100644 --- a/doc/classes/TabBar.xml +++ b/doc/classes/TabBar.xml @@ -46,14 +46,21 @@ - Returns the [Texture2D] for the right button of the tab at index [param tab_idx] or [code]null[/code] if the button has no [Texture2D]. + Returns the icon for the right button of the tab at index [param tab_idx] or [code]null[/code] if the right button has no icon. - Returns the [Texture2D] for the tab at index [param tab_idx] or [code]null[/code] if the tab has no [Texture2D]. + Returns the icon for the tab at index [param tab_idx] or [code]null[/code] if the tab has no icon. + + + + + + + Returns the maximum allowed width of the icon for the tab at index [param tab_idx]. @@ -158,6 +165,14 @@ Sets an [param icon] for the tab at index [param tab_idx]. + + + + + + Sets the maximum allowed width of the icon for the tab at index [param tab_idx]. This limit is applied on top of the default size of the icon and on top of [theme_item icon_max_width]. The height is adjusted according to the icon's ratio. + + @@ -323,6 +338,9 @@ The horizontal separation between the elements inside tabs. + + The maximum allowed width of the tab's icon. This limit is applied on top of the default size of the icon, but before the value set with [method set_tab_icon_max_width]. The height is adjusted according to the icon's ratio. + The size of the tab text outline. [b]Note:[/b] If using a font with [member FontFile.multichannel_signed_distance_field] enabled, its [member FontFile.msdf_pixel_range] must be set to at least [i]twice[/i] the value of [theme_item outline_size] for outline rendering to look correct. Otherwise, the outline may appear to be cut off earlier than intended. diff --git a/doc/classes/TabContainer.xml b/doc/classes/TabContainer.xml index c34336353f0..b0dcb932dc4 100644 --- a/doc/classes/TabContainer.xml +++ b/doc/classes/TabContainer.xml @@ -209,6 +209,9 @@ Font color of the other, unselected tabs. + + The maximum allowed width of the tab's icon. This limit is applied on top of the default size of the icon, but before the value set with [method TabBar.set_tab_icon_max_width]. The height is adjusted according to the icon's ratio. + Space between tab's name and its icon. diff --git a/doc/classes/Tree.xml b/doc/classes/Tree.xml index 95778f86c45..d09f61b8c87 100644 --- a/doc/classes/Tree.xml +++ b/doc/classes/Tree.xml @@ -518,6 +518,9 @@ The horizontal space between item cells. This is also used as the margin at the start of an item when folding is disabled. + + The maximum allowed width of the icon in item's cells. This limit is applied on top of the default size of the icon, but before the value set with [method TreeItem.set_icon_max_width]. The height is adjusted according to the icon's ratio. + The horizontal margin at the start of an item. This is used when folding is enabled for the item. diff --git a/doc/classes/TreeItem.xml b/doc/classes/TreeItem.xml index 3ce434e0699..49b4622aed0 100644 --- a/doc/classes/TreeItem.xml +++ b/doc/classes/TreeItem.xml @@ -183,7 +183,7 @@ - Returns the column's icon's maximum width. + Returns the maximum allowed width of the icon in the given [param column]. @@ -545,7 +545,7 @@ - Sets the given column's icon's maximum width. + Sets the maximum allowed width of the icon in the given [param column]. This limit is applied on top of the default size of the icon and on top of [theme_item Tree.icon_max_width]. The height is adjusted according to the icon's ratio. diff --git a/editor/create_dialog.cpp b/editor/create_dialog.cpp index ee47ee57296..aaf106a1c75 100644 --- a/editor/create_dialog.cpp +++ b/editor/create_dialog.cpp @@ -462,6 +462,11 @@ void CreateDialog::_notification(int p_what) { } break; case NOTIFICATION_THEME_CHANGED: { + const int icon_width = get_theme_constant(SNAME("class_icon_size"), SNAME("Editor")); + search_options->add_theme_constant_override("icon_max_width", icon_width); + favorites->add_theme_constant_override("icon_max_width", icon_width); + recent->set_fixed_icon_size(Size2(icon_width, icon_width)); + _update_theme(); } break; } diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp index d2af7879d27..2d095d2dc77 100644 --- a/editor/editor_data.cpp +++ b/editor/editor_data.cpp @@ -1030,13 +1030,11 @@ void EditorData::script_class_load_icon_paths() { } } -Ref EditorData::_load_script_icon(const String &p_path) const { - if (p_path.length()) { - Ref img = memnew(Image); - Error err = ImageLoader::load_image(p_path, img); - if (err == OK) { - img->resize(16 * EDSCALE, 16 * EDSCALE, Image::INTERPOLATE_LANCZOS); - return ImageTexture::create_from_image(img); +Ref EditorData::_load_script_icon(const String &p_path) const { + if (!p_path.is_empty() && ResourceLoader::exists(p_path)) { + Ref icon = ResourceLoader::load(p_path); + if (icon.is_valid()) { + return icon; } } return nullptr; @@ -1051,9 +1049,9 @@ Ref EditorData::get_script_icon(const Ref