diff options
Diffstat (limited to 'editor/editor_themes.cpp')
-rw-r--r-- | editor/editor_themes.cpp | 100 |
1 files changed, 99 insertions, 1 deletions
diff --git a/editor/editor_themes.cpp b/editor/editor_themes.cpp index 54fd8d63af..f84460ea78 100644 --- a/editor/editor_themes.cpp +++ b/editor/editor_themes.cpp @@ -41,6 +41,7 @@ #include "scene/resources/style_box_flat.h" #include "scene/resources/style_box_line.h" #include "scene/resources/style_box_texture.h" +#include "scene/theme/theme_db.h" #include "modules/modules_enabled.gen.h" // For svg. #ifdef MODULE_SVG_ENABLED @@ -214,6 +215,103 @@ void EditorColorMap::create() { add_conversion_exception("Breakpoint"); } +Vector<StringName> EditorTheme::editor_theme_types; + +// TODO: Refactor these and corresponding Theme methods to use the bool get_xxx(r_value) pattern internally. + +// Keep in sync with Theme::get_color. +Color EditorTheme::get_color(const StringName &p_name, const StringName &p_theme_type) const { + if (color_map.has(p_theme_type) && color_map[p_theme_type].has(p_name)) { + return color_map[p_theme_type][p_name]; + } else { + if (editor_theme_types.has(p_theme_type)) { + WARN_PRINT(vformat("Trying to access a non-existing editor theme color '%s' in '%s'.", p_name, p_theme_type)); + } + return Color(); + } +} + +// Keep in sync with Theme::get_constant. +int EditorTheme::get_constant(const StringName &p_name, const StringName &p_theme_type) const { + if (constant_map.has(p_theme_type) && constant_map[p_theme_type].has(p_name)) { + return constant_map[p_theme_type][p_name]; + } else { + if (editor_theme_types.has(p_theme_type)) { + WARN_PRINT(vformat("Trying to access a non-existing editor theme constant '%s' in '%s'.", p_name, p_theme_type)); + } + return 0; + } +} + +// Keep in sync with Theme::get_font. +Ref<Font> EditorTheme::get_font(const StringName &p_name, const StringName &p_theme_type) const { + if (font_map.has(p_theme_type) && font_map[p_theme_type].has(p_name) && font_map[p_theme_type][p_name].is_valid()) { + return font_map[p_theme_type][p_name]; + } else if (has_default_font()) { + if (editor_theme_types.has(p_theme_type)) { + WARN_PRINT(vformat("Trying to access a non-existing editor theme font '%s' in '%s'.", p_name, p_theme_type)); + } + return default_font; + } else { + if (editor_theme_types.has(p_theme_type)) { + WARN_PRINT(vformat("Trying to access a non-existing editor theme font '%s' in '%s'.", p_name, p_theme_type)); + } + return ThemeDB::get_singleton()->get_fallback_font(); + } +} + +// Keep in sync with Theme::get_font_size. +int EditorTheme::get_font_size(const StringName &p_name, const StringName &p_theme_type) const { + if (font_size_map.has(p_theme_type) && font_size_map[p_theme_type].has(p_name) && (font_size_map[p_theme_type][p_name] > 0)) { + return font_size_map[p_theme_type][p_name]; + } else if (has_default_font_size()) { + if (editor_theme_types.has(p_theme_type)) { + WARN_PRINT(vformat("Trying to access a non-existing editor theme font size '%s' in '%s'.", p_name, p_theme_type)); + } + return default_font_size; + } else { + if (editor_theme_types.has(p_theme_type)) { + WARN_PRINT(vformat("Trying to access a non-existing editor theme font size '%s' in '%s'.", p_name, p_theme_type)); + } + return ThemeDB::get_singleton()->get_fallback_font_size(); + } +} + +// Keep in sync with Theme::get_icon. +Ref<Texture2D> EditorTheme::get_icon(const StringName &p_name, const StringName &p_theme_type) const { + if (icon_map.has(p_theme_type) && icon_map[p_theme_type].has(p_name) && icon_map[p_theme_type][p_name].is_valid()) { + return icon_map[p_theme_type][p_name]; + } else { + if (editor_theme_types.has(p_theme_type)) { + WARN_PRINT(vformat("Trying to access a non-existing editor theme icon '%s' in '%s'.", p_name, p_theme_type)); + } + return ThemeDB::get_singleton()->get_fallback_icon(); + } +} + +// Keep in sync with Theme::get_stylebox. +Ref<StyleBox> EditorTheme::get_stylebox(const StringName &p_name, const StringName &p_theme_type) const { + if (style_map.has(p_theme_type) && style_map[p_theme_type].has(p_name) && style_map[p_theme_type][p_name].is_valid()) { + return style_map[p_theme_type][p_name]; + } else { + if (editor_theme_types.has(p_theme_type)) { + WARN_PRINT(vformat("Trying to access a non-existing editor theme stylebox '%s' in '%s'.", p_name, p_theme_type)); + } + return ThemeDB::get_singleton()->get_fallback_stylebox(); + } +} + +EditorTheme::EditorTheme() { + if (editor_theme_types.is_empty()) { + editor_theme_types.append(EditorStringName(Editor)); + editor_theme_types.append(EditorStringName(EditorFonts)); + editor_theme_types.append(EditorStringName(EditorIcons)); + editor_theme_types.append(EditorStringName(EditorStyles)); + } +} + +// Editor theme generatior. + static Ref<StyleBoxTexture> make_stylebox(Ref<Texture2D> p_texture, float p_left, float p_top, float p_right, float p_bottom, float p_margin_left = -1, float p_margin_top = -1, float p_margin_right = -1, float p_margin_bottom = -1, bool p_draw_center = true) { Ref<StyleBoxTexture> style(memnew(StyleBoxTexture)); style->set_texture(p_texture); @@ -430,7 +528,7 @@ void editor_register_and_generate_icons(Ref<Theme> p_theme, bool p_dark_theme, f Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) { OS::get_singleton()->benchmark_begin_measure("create_editor_theme"); - Ref<Theme> theme = Ref<Theme>(memnew(Theme)); + Ref<EditorTheme> theme = memnew(EditorTheme); // Controls may rely on the scale for their internal drawing logic. theme->set_default_base_scale(EDSCALE); |