Use SVGTextures for editor and default theme icons.

This commit is contained in:
Pāvels Nadtočajevs
2025-04-15 09:35:38 +03:00
parent 9f5048fb36
commit 24bb0d5c4f
5 changed files with 26 additions and 57 deletions

View File

@ -35,6 +35,7 @@
#include "editor/themes/editor_icons.gen.h"
#include "editor/themes/editor_scale.h"
#include "scene/resources/image_texture.h"
#include "scene/resources/svg_texture.h"
#include "modules/svg/image_loader_svg.h"
@ -47,20 +48,8 @@ void editor_configure_icons(bool p_dark_theme) {
}
// See also `generate_icon()` in `scene/theme/default_theme.cpp`.
Ref<ImageTexture> editor_generate_icon(int p_index, float p_scale, float p_saturation, const HashMap<Color, Color> &p_convert_colors = HashMap<Color, Color>()) {
Ref<Image> img = memnew(Image);
// Upsample icon generation only if the editor scale isn't an integer multiplier.
// Generating upsampled icons is slower, and the benefit is hardly visible
// with integer editor scales.
const bool upsample = !Math::is_equal_approx(Math::round(p_scale), p_scale);
Error err = ImageLoaderSVG::create_image_from_string(img, editor_icons_sources[p_index], p_scale, upsample, p_convert_colors);
ERR_FAIL_COND_V_MSG(err != OK, Ref<ImageTexture>(), "Failed generating icon, unsupported or invalid SVG data in editor theme.");
if (p_saturation != 1.0) {
img->adjust_bcs(1.0, 1.0, p_saturation);
}
return ImageTexture::create_from_image(img);
Ref<SVGTexture> editor_generate_icon(int p_index, float p_scale, float p_saturation, const Dictionary &p_convert_colors = Dictionary()) {
return SVGTexture::create_from_string(editor_icons_sources[p_index], p_scale, p_saturation, p_convert_colors);
}
float get_gizmo_handle_scale(const String &p_gizmo_handle_name, float p_gizmo_handle_scale) {
@ -94,8 +83,8 @@ void editor_register_icons(const Ref<Theme> &p_theme, bool p_dark_theme, float p
// And then some icons are completely excluded from the conversion.
// Standard color conversion map.
HashMap<Color, Color> color_conversion_map_light;
HashMap<Color, Color> color_conversion_map_dark;
Dictionary color_conversion_map_light;
Dictionary color_conversion_map_dark;
// Icons by default are set up for the dark theme, so if the theme is light,
// we apply the dark-to-light color conversion map.
for (KeyValue<Color, Color> &E : EditorColorMap::get_color_conversion_map()) {
@ -112,7 +101,7 @@ void editor_register_icons(const Ref<Theme> &p_theme, bool p_dark_theme, float p
color_conversion_map_light[Color::html("#5fff97")] = success_color;
color_conversion_map_light[Color::html("#ffdd65")] = warning_color;
HashMap<Color, Color> color_conversion_map = p_dark_theme ? color_conversion_map_dark : color_conversion_map_light;
Dictionary color_conversion_map = p_dark_theme ? color_conversion_map_dark : color_conversion_map_light;
// The names of the icons used in native menus.
HashSet<StringName> native_menu_icons;
@ -138,7 +127,7 @@ void editor_register_icons(const Ref<Theme> &p_theme, bool p_dark_theme, float p
// Accent color conversion map.
// It is used on some icons (checkbox, radio, toggle, etc.), regardless of the dark
// or light mode.
HashMap<Color, Color> accent_color_map;
Dictionary accent_color_map;
HashSet<StringName> accent_color_icons;
const Color accent_color = p_theme->get_color(SNAME("accent_color"), EditorStringName(Editor));
@ -164,14 +153,14 @@ void editor_register_icons(const Ref<Theme> &p_theme, bool p_dark_theme, float p
saturation = 1.0;
}
Ref<ImageTexture> icon_dark = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_dark);
Ref<ImageTexture> icon_light = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_light);
Ref<SVGTexture> icon_dark = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_dark);
Ref<SVGTexture> icon_light = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), saturation, color_conversion_map_light);
p_theme->set_icon(editor_icon_name + "Dark", EditorStringName(EditorIcons), icon_dark);
p_theme->set_icon(editor_icon_name + "Light", EditorStringName(EditorIcons), icon_light);
p_theme->set_icon(editor_icon_name, EditorStringName(EditorIcons), p_dark_theme ? icon_dark : icon_light);
} else {
Ref<ImageTexture> icon;
Ref<SVGTexture> icon;
if (accent_color_icons.has(editor_icon_name)) {
icon = editor_generate_icon(i, get_gizmo_handle_scale(editor_icon_name, p_gizmo_handle_scale), 1.0, accent_color_map);
} else {
@ -198,7 +187,7 @@ void editor_register_icons(const Ref<Theme> &p_theme, bool p_dark_theme, float p
const float scale = (float)p_thumb_size / 64.0 * EDSCALE;
for (int i = 0; i < editor_bg_thumbs_count; i++) {
const int index = editor_bg_thumbs_indices[i];
Ref<ImageTexture> icon;
Ref<SVGTexture> icon;
if (accent_color_icons.has(editor_icons_names[index])) {
icon = editor_generate_icon(index, scale, 1.0, accent_color_map);
@ -221,7 +210,7 @@ void editor_register_icons(const Ref<Theme> &p_theme, bool p_dark_theme, float p
const float scale = (float)p_thumb_size / 32.0 * EDSCALE;
for (int i = 0; i < editor_md_thumbs_count; i++) {
const int index = editor_md_thumbs_indices[i];
Ref<ImageTexture> icon;
Ref<SVGTexture> icon;
if (accent_color_icons.has(editor_icons_names[index])) {
icon = editor_generate_icon(index, scale, 1.0, accent_color_map);