Path: blob/master/editor/themes/editor_theme_manager.cpp
20850 views
/**************************************************************************/1/* editor_theme_manager.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "editor_theme_manager.h"3132#include "core/error/error_macros.h"33#include "core/io/resource_loader.h"34#include "editor/editor_string_names.h"35#include "editor/file_system/editor_paths.h"36#include "editor/settings/editor_settings.h"37#include "editor/themes/editor_color_map.h"38#include "editor/themes/editor_fonts.h"39#include "editor/themes/editor_icons.h"40#include "editor/themes/editor_scale.h"41#include "editor/themes/editor_theme.h"42#include "editor/themes/theme_classic.h"43#include "editor/themes/theme_modern.h"44#include "scene/resources/style_box_flat.h"45#include "scene/resources/style_box_line.h"46#include "scene/resources/style_box_texture.h"47#include "scene/resources/texture.h"48#include "scene/scene_string_names.h"49#include "servers/display/display_server.h"5051// Theme configuration.5253uint32_t EditorThemeManager::ThemeConfiguration::hash() {54uint32_t hash = hash_murmur3_one_float(EDSCALE);5556// Basic properties.5758hash = hash_murmur3_one_32(style.hash(), hash);59hash = hash_murmur3_one_32(preset.hash(), hash);60hash = hash_murmur3_one_32(spacing_preset.hash(), hash);6162hash = hash_murmur3_one_32(base_color.to_rgba32(), hash);63hash = hash_murmur3_one_32(accent_color.to_rgba32(), hash);64hash = hash_murmur3_one_float(contrast, hash);65hash = hash_murmur3_one_float(icon_saturation, hash);6667// Extra properties.6869hash = hash_murmur3_one_32(base_spacing, hash);70hash = hash_murmur3_one_32(extra_spacing, hash);71hash = hash_murmur3_one_32(border_width, hash);72hash = hash_murmur3_one_32(corner_radius, hash);7374hash = hash_murmur3_one_32((int)draw_extra_borders, hash);75hash = hash_murmur3_one_float(relationship_line_opacity, hash);76hash = hash_murmur3_one_32(thumb_size, hash);77hash = hash_murmur3_one_32(class_icon_size, hash);78hash = hash_murmur3_one_32((int)enable_touch_optimizations, hash);79hash = hash_murmur3_one_float(gizmo_handle_scale, hash);80hash = hash_murmur3_one_32(inspector_property_height, hash);81hash = hash_murmur3_one_float(subresource_hue_tint, hash);8283hash = hash_murmur3_one_float(default_contrast, hash);8485// Generated properties.8687hash = hash_murmur3_one_32((int)dark_theme, hash);88hash = hash_murmur3_one_32((int)dark_icon_and_font, hash);8990return hash;91}9293uint32_t EditorThemeManager::ThemeConfiguration::hash_fonts() {94uint32_t hash = hash_murmur3_one_float(EDSCALE);9596// TODO: Implement the hash based on what editor_register_fonts() uses.9798return hash;99}100101uint32_t EditorThemeManager::ThemeConfiguration::hash_icons() {102uint32_t hash = hash_murmur3_one_float(EDSCALE);103104hash = hash_murmur3_one_32(accent_color.to_rgba32(), hash);105hash = hash_murmur3_one_float(icon_saturation, hash);106107hash = hash_murmur3_one_32(thumb_size, hash);108hash = hash_murmur3_one_float(gizmo_handle_scale, hash);109110hash = hash_murmur3_one_32((int)dark_icon_and_font, hash);111112return hash;113}114115// Benchmarks.116117int EditorThemeManager::benchmark_run = 0;118119String EditorThemeManager::get_benchmark_key() {120if (benchmark_run == 0) {121return "EditorTheme (Startup)";122}123124return vformat("EditorTheme (Run %d)", benchmark_run);125}126127// Generation helper methods.128129Ref<StyleBoxTexture> EditorThemeManager::make_stylebox(Ref<Texture2D> p_texture, float p_left, float p_top, float p_right, float p_bottom, float p_margin_left, float p_margin_top, float p_margin_right, float p_margin_bottom, bool p_draw_center) {130Ref<StyleBoxTexture> style(memnew(StyleBoxTexture));131style->set_texture(p_texture);132style->set_texture_margin_individual(p_left * EDSCALE, p_top * EDSCALE, p_right * EDSCALE, p_bottom * EDSCALE);133style->set_content_margin_individual((p_left + p_margin_left) * EDSCALE, (p_top + p_margin_top) * EDSCALE, (p_right + p_margin_right) * EDSCALE, (p_bottom + p_margin_bottom) * EDSCALE);134style->set_draw_center(p_draw_center);135return style;136}137138Ref<StyleBoxEmpty> EditorThemeManager::make_empty_stylebox(float p_margin_left, float p_margin_top, float p_margin_right, float p_margin_bottom) {139Ref<StyleBoxEmpty> style(memnew(StyleBoxEmpty));140style->set_content_margin_individual(p_margin_left * EDSCALE, p_margin_top * EDSCALE, p_margin_right * EDSCALE, p_margin_bottom * EDSCALE);141return style;142}143144Ref<StyleBoxFlat> EditorThemeManager::make_flat_stylebox(Color p_color, float p_margin_left, float p_margin_top, float p_margin_right, float p_margin_bottom, int p_corner_width) {145Ref<StyleBoxFlat> style(memnew(StyleBoxFlat));146style->set_bg_color(p_color);147// Adjust level of detail based on the corners' effective sizes.148style->set_corner_detail(Math::ceil(0.8 * p_corner_width * EDSCALE));149style->set_corner_radius_all(p_corner_width * EDSCALE);150style->set_content_margin_individual(p_margin_left * EDSCALE, p_margin_top * EDSCALE, p_margin_right * EDSCALE, p_margin_bottom * EDSCALE);151return style;152}153154Ref<StyleBoxLine> EditorThemeManager::make_line_stylebox(Color p_color, int p_thickness, float p_grow_begin, float p_grow_end, bool p_vertical) {155Ref<StyleBoxLine> style(memnew(StyleBoxLine));156style->set_color(p_color);157style->set_grow_begin(p_grow_begin);158style->set_grow_end(p_grow_end);159style->set_thickness(p_thickness);160style->set_vertical(p_vertical);161return style;162}163164// Theme generation and population routines.165166Ref<EditorTheme> EditorThemeManager::_create_base_theme(const Ref<EditorTheme> &p_old_theme) {167OS::get_singleton()->benchmark_begin_measure(get_benchmark_key(), "Create Base Theme");168169Ref<EditorTheme> theme = memnew(EditorTheme);170ThemeConfiguration config = _create_theme_config();171theme->set_generated_hash(config.hash());172theme->set_generated_fonts_hash(config.hash_fonts());173theme->set_generated_icons_hash(config.hash_icons());174175print_verbose(vformat("EditorTheme: Generating new theme for the config '%d'.", theme->get_generated_hash()));176177bool is_default_style = config.style == "Modern";178if (is_default_style) {179ThemeModern::populate_shared_styles(theme, config);180} else {181ThemeClassic::populate_shared_styles(theme, config);182}183184// Register icons.185{186OS::get_singleton()->benchmark_begin_measure(get_benchmark_key(), "Register Icons");187188// External functions, see editor_icons.cpp.189editor_configure_icons(config.dark_icon_and_font);190191// If settings are comparable to the old theme, then just copy existing icons over.192// Otherwise, regenerate them.193bool keep_old_icons = (p_old_theme.is_valid() && theme->get_generated_icons_hash() == p_old_theme->get_generated_icons_hash());194if (keep_old_icons) {195print_verbose("EditorTheme: Can keep old icons, copying.");196editor_copy_icons(theme, p_old_theme);197} else {198print_verbose("EditorTheme: Generating new icons.");199editor_register_icons(theme, config.dark_icon_and_font, config.icon_saturation, config.thumb_size, config.gizmo_handle_scale);200}201202OS::get_singleton()->benchmark_end_measure(get_benchmark_key(), "Register Icons");203}204205// Register fonts.206{207OS::get_singleton()->benchmark_begin_measure(get_benchmark_key(), "Register Fonts");208209// TODO: Check if existing font definitions from the old theme are usable and copy them.210211// External function, see editor_fonts.cpp.212print_verbose("EditorTheme: Generating new fonts.");213editor_register_fonts(theme);214215OS::get_singleton()->benchmark_end_measure(get_benchmark_key(), "Register Fonts");216}217218// TODO: Check if existing style definitions from the old theme are usable and copy them.219220print_verbose("EditorTheme: Generating new styles.");221222if (is_default_style) {223ThemeModern::populate_standard_styles(theme, config);224ThemeModern::populate_editor_styles(theme, config);225} else {226ThemeClassic::populate_standard_styles(theme, config);227ThemeClassic::populate_editor_styles(theme, config);228}229230_populate_text_editor_styles(theme, config);231_populate_visual_shader_styles(theme, config);232233OS::get_singleton()->benchmark_end_measure(get_benchmark_key(), "Create Base Theme");234return theme;235}236237EditorThemeManager::ThemeConfiguration EditorThemeManager::_create_theme_config() {238ThemeConfiguration config;239240// Basic properties.241242config.style = EDITOR_GET("interface/theme/style");243config.preset = EDITOR_GET("interface/theme/color_preset");244config.spacing_preset = EDITOR_GET("interface/theme/spacing_preset");245246config.base_color = EDITOR_GET("interface/theme/base_color");247config.accent_color = EDITOR_GET("interface/theme/accent_color");248config.contrast = EDITOR_GET("interface/theme/contrast");249config.icon_saturation = EDITOR_GET("interface/theme/icon_saturation");250config.corner_radius = EDITOR_GET("interface/theme/corner_radius");251252// Extra properties.253254config.base_spacing = EDITOR_GET("interface/theme/base_spacing");255config.extra_spacing = EDITOR_GET("interface/theme/additional_spacing");256// Ensure borders are visible when using an editor scale below 100%.257config.border_width = CLAMP((int)EDITOR_GET("interface/theme/border_size"), 0, 2) * MAX(1, EDSCALE);258259config.draw_extra_borders = EDITOR_GET("interface/theme/draw_extra_borders");260config.draw_relationship_lines = EDITOR_GET("interface/theme/draw_relationship_lines");261config.relationship_line_opacity = EDITOR_GET("interface/theme/relationship_line_opacity");262config.thumb_size = EDITOR_GET("filesystem/file_dialog/thumbnail_size");263config.class_icon_size = 16 * EDSCALE;264config.enable_touch_optimizations = EDITOR_GET("interface/touchscreen/enable_touch_optimizations");265config.gizmo_handle_scale = EDITOR_GET("interface/touchscreen/scale_gizmo_handles");266config.subresource_hue_tint = EDITOR_GET("docks/property_editor/subresource_hue_tint");267config.dragging_hover_wait_msec = (float)EDITOR_GET("interface/editor/dragging_hover_wait_seconds") * 1000;268269// Handle theme style.270if (config.preset != "Custom") {271if (config.style == "Classic") {272config.draw_relationship_lines = RELATIONSHIP_ALL;273config.corner_radius = 3;274} else { // Default275config.draw_relationship_lines = config.default_relationship_lines;276config.corner_radius = config.default_corner_radius;277}278279EditorSettings::get_singleton()->set_initial_value("interface/theme/draw_relationship_lines", config.draw_relationship_lines);280EditorSettings::get_singleton()->set_initial_value("interface/theme/corner_radius", config.corner_radius);281282// Enforce values in case they were adjusted or overridden.283EditorSettings::get_singleton()->set_manually("interface/theme/draw_relationship_lines", config.draw_relationship_lines);284EditorSettings::get_singleton()->set_manually("interface/theme/corner_radius", config.corner_radius);285}286287// Handle color preset.288{289const bool follow_system_theme = EDITOR_GET("interface/theme/follow_system_theme");290const bool use_system_accent_color = EDITOR_GET("interface/theme/use_system_accent_color");291DisplayServer *display_server = DisplayServer::get_singleton();292Color system_base_color = display_server->get_base_color();293Color system_accent_color = display_server->get_accent_color();294295if (follow_system_theme) {296String dark_theme = "Default";297String light_theme = "Light";298299config.preset = light_theme; // Assume light theme if we can't detect system theme attributes.300301if (system_base_color == Color(0, 0, 0, 0)) {302if (display_server->is_dark_mode_supported() && display_server->is_dark_mode()) {303config.preset = dark_theme;304}305} else {306if (system_base_color.get_luminance() < 0.5) {307config.preset = dark_theme;308}309}310}311312if (config.preset != "Custom") {313Color preset_accent_color;314Color preset_base_color;315float preset_contrast = config.default_contrast;316bool preset_draw_extra_borders = false;317float preset_icon_saturation = config.default_icon_saturation;318319// A negative contrast rate looks better for light themes, since it better follows the natural order of UI "elevation".320const float light_contrast = -0.06;321322// Please use alphabetical order if you're adding a new color preset here.323if (config.preset == "Black (OLED)") {324preset_accent_color = Color(0.45, 0.75, 1.0);325preset_base_color = Color(0, 0, 0);326// The contrast rate value is irrelevant on a fully black theme.327preset_contrast = 0.0;328preset_draw_extra_borders = true;329} else if (config.preset == "Breeze Dark") {330preset_accent_color = Color(0.239, 0.682, 0.914);331preset_base_color = Color(0.1255, 0.1373, 0.149);332} else if (config.preset == "Godot 2") {333preset_accent_color = Color(0.53, 0.67, 0.89);334preset_base_color = Color(0.24, 0.23, 0.27);335preset_icon_saturation = 1;336} else if (config.preset == "Godot 3") {337preset_accent_color = Color(0.44, 0.73, 0.98);338preset_base_color = Color(0.21, 0.24, 0.29);339preset_icon_saturation = 1;340} else if (config.preset == "Gray") {341preset_accent_color = Color(0.44, 0.73, 0.98);342preset_base_color = Color(0.24, 0.24, 0.24);343} else if (config.preset == "Light") {344preset_accent_color = Color(0.18, 0.50, 1.00);345preset_base_color = Color(0.9, 0.9, 0.9);346preset_contrast = light_contrast;347preset_icon_saturation = 1;348} else if (config.preset == "Solarized (Dark)") {349preset_accent_color = Color(0.15, 0.55, 0.82);350preset_base_color = Color(0.03, 0.21, 0.26);351preset_contrast = 0.23;352} else if (config.preset == "Solarized (Light)") {353preset_accent_color = Color(0.15, 0.55, 0.82);354preset_base_color = Color(0.89, 0.86, 0.79);355preset_contrast = light_contrast;356} else { // Default357preset_accent_color = Color(0.337, 0.62, 1.0);358preset_base_color = Color(0.161, 0.161, 0.161);359}360361config.accent_color = preset_accent_color;362config.base_color = preset_base_color;363config.contrast = preset_contrast;364config.draw_extra_borders = preset_draw_extra_borders;365config.icon_saturation = preset_icon_saturation;366367EditorSettings::get_singleton()->set_initial_value("interface/theme/accent_color", config.accent_color);368EditorSettings::get_singleton()->set_initial_value("interface/theme/base_color", config.base_color);369EditorSettings::get_singleton()->set_initial_value("interface/theme/contrast", config.contrast);370EditorSettings::get_singleton()->set_initial_value("interface/theme/draw_extra_borders", config.draw_extra_borders);371EditorSettings::get_singleton()->set_initial_value("interface/theme/icon_saturation", config.icon_saturation);372}373374if (follow_system_theme && system_base_color != Color(0, 0, 0, 0)) {375config.base_color = system_base_color;376config.preset = "Custom";377}378379if (use_system_accent_color && system_accent_color != Color(0, 0, 0, 0)) {380config.accent_color = system_accent_color;381config.preset = "Custom";382}383384// Enforce values in case they were adjusted or overridden.385EditorSettings::get_singleton()->set_manually("interface/theme/color_preset", config.preset);386EditorSettings::get_singleton()->set_manually("interface/theme/accent_color", config.accent_color);387EditorSettings::get_singleton()->set_manually("interface/theme/base_color", config.base_color);388EditorSettings::get_singleton()->set_manually("interface/theme/contrast", config.contrast);389EditorSettings::get_singleton()->set_manually("interface/theme/draw_extra_borders", config.draw_extra_borders);390EditorSettings::get_singleton()->set_manually("interface/theme/icon_saturation", config.icon_saturation);391}392393// Handle theme spacing preset.394{395if (config.spacing_preset != "Custom") {396int preset_base_spacing = 0;397int preset_extra_spacing = 0;398Size2 preset_dialogs_buttons_min_size;399400if (config.spacing_preset == "Compact") {401preset_base_spacing = 2;402preset_extra_spacing = 2;403preset_dialogs_buttons_min_size = Size2(90, 26);404} else if (config.spacing_preset == "Spacious") {405preset_base_spacing = 6;406preset_extra_spacing = 2;407preset_dialogs_buttons_min_size = Size2(112, 36);408} else { // Default409preset_base_spacing = 4;410preset_extra_spacing = 0;411preset_dialogs_buttons_min_size = Size2(105, 34);412}413414config.base_spacing = preset_base_spacing;415config.extra_spacing = preset_extra_spacing;416config.dialogs_buttons_min_size = preset_dialogs_buttons_min_size;417418EditorSettings::get_singleton()->set_initial_value("interface/theme/base_spacing", config.base_spacing);419EditorSettings::get_singleton()->set_initial_value("interface/theme/additional_spacing", config.extra_spacing);420}421422// Enforce values in case they were adjusted or overridden.423EditorSettings::get_singleton()->set_manually("interface/theme/spacing_preset", config.spacing_preset);424EditorSettings::get_singleton()->set_manually("interface/theme/base_spacing", config.base_spacing);425EditorSettings::get_singleton()->set_manually("interface/theme/additional_spacing", config.extra_spacing);426}427428// Generated properties.429430config.dark_theme = is_dark_theme();431config.dark_icon_and_font = is_dark_icon_and_font();432433config.base_margin = config.base_spacing;434config.increased_margin = config.base_spacing + config.extra_spacing * 0.75;435config.separation_margin = (config.base_spacing + config.extra_spacing / 2) * EDSCALE;436config.popup_margin = config.base_margin * 2.4 * EDSCALE;437// Make sure content doesn't stick to window decorations; this can be fixed in future with layout changes.438config.window_border_margin = MAX(1, config.base_margin * EDSCALE);439config.top_bar_separation = MAX(1, config.base_margin * EDSCALE);440441// Force the v_separation to be even so that the spacing on top and bottom is even.442// If the vsep is odd and cannot be split into 2 even groups (of pixels), then it will be lopsided.443// We add 2 to the vsep to give it some extra spacing which looks a bit more modern (see Windows, for example).444const int separation_base = config.increased_margin + 6;445config.forced_even_separation = separation_base + (separation_base % 2);446447return config;448}449450void _load_text_editor_theme() {451EditorSettings *settings = EditorSettings::get_singleton();452const String theme_name = settings->get_setting("text_editor/theme/color_theme");453454ERR_FAIL_COND(EditorSettings::is_default_text_editor_theme(theme_name.get_file().to_lower()));455456const String theme_path = EditorPaths::get_singleton()->get_text_editor_themes_dir().path_join(theme_name + ".tet");457458Ref<ConfigFile> cf;459cf.instantiate();460Error err = cf->load(theme_path);461ERR_FAIL_COND_MSG(err != OK, vformat("Failed to load text editor theme file '%s': %s", theme_name, error_names[err]));462463const PackedStringArray keys = cf->get_section_keys("color_theme");464465for (const String &key : keys) {466const String setting_key = "text_editor/theme/highlighting/" + key;467// Don't load if it's not an actual setting, or if it isn't a color setting.468if (!settings->has_setting(setting_key) || !key.contains("color")) {469continue;470}471const String val = cf->get_value("color_theme", key);472// Make sure it is actually a color.473if (val.is_valid_html_color()) {474const Color color_value = Color::html(val);475// Change manually to prevent settings_changed spam.476settings->set_initial_value(setting_key, color_value);477settings->set_manually(setting_key, color_value);478}479}480// If it doesn't load a setting just use what is currently loaded.481}482483void EditorThemeManager::_populate_text_editor_styles(const Ref<EditorTheme> &p_theme, ThemeConfiguration &p_config) {484const String text_editor_color_theme = EDITOR_GET("text_editor/theme/color_theme");485const bool is_default_theme = text_editor_color_theme == "Default";486const bool is_godot2_theme = text_editor_color_theme == "Godot 2";487const bool is_custom_theme = text_editor_color_theme == "Custom";488if (is_default_theme || is_godot2_theme || is_custom_theme) {489HashMap<StringName, Color> colors;490if (is_default_theme || is_custom_theme) {491// Adaptive colors for comments and elements with lower relevance.492const Color dim_color = Color(p_config.font_color, 0.5);493const float mono_value = p_config.mono_color.r;494const Color alpha1 = Color(mono_value, mono_value, mono_value, 0.07);495const Color alpha2 = Color(mono_value, mono_value, mono_value, 0.14);496const Color alpha3 = Color(mono_value, mono_value, mono_value, 0.27);497498// Syntax highlight token colors.499colors["text_editor/theme/highlighting/symbol_color"] = p_config.dark_icon_and_font ? Color(0.67, 0.79, 1) : Color(0, 0, 0.61);500colors["text_editor/theme/highlighting/keyword_color"] = p_config.dark_icon_and_font ? Color(1.0, 0.44, 0.52) : Color(0.9, 0.135, 0.51);501colors["text_editor/theme/highlighting/control_flow_keyword_color"] = p_config.dark_icon_and_font ? Color(1.0, 0.55, 0.8) : Color(0.743, 0.12, 0.8);502colors["text_editor/theme/highlighting/base_type_color"] = p_config.dark_icon_and_font ? Color(0.26, 1.0, 0.76) : Color(0, 0.6, 0.2);503colors["text_editor/theme/highlighting/engine_type_color"] = p_config.dark_icon_and_font ? Color(0.56, 1, 0.86) : Color(0.11, 0.55, 0.4);504colors["text_editor/theme/highlighting/user_type_color"] = p_config.dark_icon_and_font ? Color(0.78, 1, 0.93) : Color(0.18, 0.45, 0.4);505colors["text_editor/theme/highlighting/comment_color"] = p_config.dark_icon_and_font ? dim_color : Color(0.08, 0.08, 0.08, 0.5);506colors["text_editor/theme/highlighting/doc_comment_color"] = p_config.dark_icon_and_font ? Color(0.6, 0.7, 0.8, 0.8) : Color(0.15, 0.15, 0.4, 0.7);507colors["text_editor/theme/highlighting/string_color"] = p_config.dark_icon_and_font ? Color(1, 0.93, 0.63) : Color(0.6, 0.42, 0);508colors["text_editor/theme/highlighting/string_placeholder_color"] = p_config.dark_icon_and_font ? Color(1, 0.75, 0.4) : Color(0.93, 0.6, 0.33);509510// Use the brightest background color on a light theme (which generally uses a negative contrast rate).511colors["text_editor/theme/highlighting/background_color"] = p_config.base_color.lerp(Color(0, 0, 0), p_config.contrast * (p_config.dark_icon_and_font ? 1.2 : 1.8)).clamp();512colors["text_editor/theme/highlighting/completion_background_color"] = p_config.base_color.lerp(Color(0, 0, 0), p_config.contrast * 0.3).clamp();513colors["text_editor/theme/highlighting/completion_selected_color"] = alpha1;514colors["text_editor/theme/highlighting/completion_existing_color"] = alpha2;515// Same opacity as the scroll grabber editor icon.516colors["text_editor/theme/highlighting/completion_scroll_color"] = Color(mono_value, mono_value, mono_value, 0.29);517colors["text_editor/theme/highlighting/completion_scroll_hovered_color"] = Color(mono_value, mono_value, mono_value, 0.4);518colors["text_editor/theme/highlighting/completion_font_color"] = p_config.font_color;519colors["text_editor/theme/highlighting/text_color"] = p_config.font_color;520colors["text_editor/theme/highlighting/line_number_color"] = dim_color;521colors["text_editor/theme/highlighting/safe_line_number_color"] = p_config.dark_icon_and_font ? (dim_color * Color(1, 1.2, 1, 1.5)) : Color(0, 0.4, 0, 0.75);522colors["text_editor/theme/highlighting/caret_color"] = p_config.mono_color;523colors["text_editor/theme/highlighting/caret_background_color"] = p_config.mono_color.inverted();524colors["text_editor/theme/highlighting/text_selected_color"] = Color(0, 0, 0, 0);525colors["text_editor/theme/highlighting/selection_color"] = p_config.selection_color;526colors["text_editor/theme/highlighting/brace_mismatch_color"] = p_config.dark_icon_and_font ? p_config.error_color : Color(1, 0.08, 0, 1);527colors["text_editor/theme/highlighting/current_line_color"] = alpha1;528colors["text_editor/theme/highlighting/line_length_guideline_color"] = p_config.dark_icon_and_font ? p_config.base_color : p_config.dark_color_2;529colors["text_editor/theme/highlighting/word_highlighted_color"] = alpha1;530colors["text_editor/theme/highlighting/number_color"] = p_config.dark_icon_and_font ? Color(0.63, 1, 0.88) : Color(0, 0.55, 0.28, 1);531colors["text_editor/theme/highlighting/function_color"] = p_config.dark_icon_and_font ? Color(0.34, 0.7, 1.0) : Color(0, 0.225, 0.9, 1);532colors["text_editor/theme/highlighting/member_variable_color"] = p_config.dark_icon_and_font ? Color(0.34, 0.7, 1.0).lerp(p_config.mono_color, 0.6) : Color(0, 0.4, 0.68, 1);533colors["text_editor/theme/highlighting/mark_color"] = Color(p_config.error_color.r, p_config.error_color.g, p_config.error_color.b, 0.3);534colors["text_editor/theme/highlighting/warning_color"] = Color(p_config.warning_color.r, p_config.warning_color.g, p_config.warning_color.b, 0.15);535colors["text_editor/theme/highlighting/bookmark_color"] = Color(0.08, 0.49, 0.98);536colors["text_editor/theme/highlighting/breakpoint_color"] = p_config.dark_icon_and_font ? p_config.error_color : Color(1, 0.27, 0.2, 1);537colors["text_editor/theme/highlighting/executing_line_color"] = Color(0.98, 0.89, 0.27);538colors["text_editor/theme/highlighting/code_folding_color"] = alpha3;539colors["text_editor/theme/highlighting/folded_code_region_color"] = Color(0.68, 0.46, 0.77, 0.2);540colors["text_editor/theme/highlighting/search_result_color"] = alpha1;541colors["text_editor/theme/highlighting/search_result_border_color"] = p_config.dark_icon_and_font ? Color(0.41, 0.61, 0.91, 0.38) : Color(0, 0.4, 1, 0.38);542543if (p_config.dark_icon_and_font) {544colors["text_editor/theme/highlighting/gdscript/function_definition_color"] = Color(0.4, 0.9, 1.0);545colors["text_editor/theme/highlighting/gdscript/global_function_color"] = Color(0.64, 0.64, 0.96);546colors["text_editor/theme/highlighting/gdscript/node_path_color"] = Color(0.72, 0.77, 0.49);547colors["text_editor/theme/highlighting/gdscript/node_reference_color"] = Color(0.39, 0.76, 0.35);548colors["text_editor/theme/highlighting/gdscript/annotation_color"] = Color(1.0, 0.7, 0.45);549colors["text_editor/theme/highlighting/gdscript/string_name_color"] = Color(1.0, 0.76, 0.65);550colors["text_editor/theme/highlighting/comment_markers/critical_color"] = Color(0.77, 0.35, 0.35);551colors["text_editor/theme/highlighting/comment_markers/warning_color"] = Color(0.72, 0.61, 0.48);552colors["text_editor/theme/highlighting/comment_markers/notice_color"] = Color(0.56, 0.67, 0.51);553} else {554colors["text_editor/theme/highlighting/gdscript/function_definition_color"] = Color(0, 0.6, 0.6);555colors["text_editor/theme/highlighting/gdscript/global_function_color"] = Color(0.36, 0.18, 0.72);556colors["text_editor/theme/highlighting/gdscript/node_path_color"] = Color(0.18, 0.55, 0);557colors["text_editor/theme/highlighting/gdscript/node_reference_color"] = Color(0.0, 0.5, 0);558colors["text_editor/theme/highlighting/gdscript/annotation_color"] = Color(0.8, 0.37, 0);559colors["text_editor/theme/highlighting/gdscript/string_name_color"] = Color(0.8, 0.56, 0.45);560colors["text_editor/theme/highlighting/comment_markers/critical_color"] = Color(0.8, 0.14, 0.14);561colors["text_editor/theme/highlighting/comment_markers/warning_color"] = Color(0.75, 0.39, 0.03);562colors["text_editor/theme/highlighting/comment_markers/notice_color"] = Color(0.24, 0.54, 0.09);563}564} else if (is_godot2_theme) {565colors = EditorSettings::get_godot2_text_editor_theme();566}567EditorSettings *settings = EditorSettings::get_singleton();568for (const KeyValue<StringName, Color> &setting : colors) {569settings->set_initial_value(setting.key, setting.value);570if (is_default_theme || is_godot2_theme) {571settings->set_manually(setting.key, setting.value);572}573}574} else {575// Custom user theme.576_load_text_editor_theme();577}578579// Now theme is loaded, apply it to CodeEdit.580p_theme->set_font(SceneStringName(font), "CodeEdit", p_theme->get_font(SNAME("source"), EditorStringName(EditorFonts)));581p_theme->set_font_size(SceneStringName(font_size), "CodeEdit", p_theme->get_font_size(SNAME("source_size"), EditorStringName(EditorFonts)));582583/* clang-format off */584p_theme->set_icon("tab", "CodeEdit", p_theme->get_icon(SNAME("GuiTab"), EditorStringName(EditorIcons)));585p_theme->set_icon("space", "CodeEdit", p_theme->get_icon(SNAME("GuiSpace"), EditorStringName(EditorIcons)));586p_theme->set_icon("folded", "CodeEdit", p_theme->get_icon(SNAME("CodeFoldedRightArrow"), EditorStringName(EditorIcons)));587p_theme->set_icon("can_fold", "CodeEdit", p_theme->get_icon(SNAME("CodeFoldDownArrow"), EditorStringName(EditorIcons)));588p_theme->set_icon("folded_code_region", "CodeEdit", p_theme->get_icon(SNAME("CodeRegionFoldedRightArrow"), EditorStringName(EditorIcons)));589p_theme->set_icon("can_fold_code_region", "CodeEdit", p_theme->get_icon(SNAME("CodeRegionFoldDownArrow"), EditorStringName(EditorIcons)));590p_theme->set_icon("executing_line", "CodeEdit", p_theme->get_icon(SNAME("TextEditorPlay"), EditorStringName(EditorIcons)));591p_theme->set_icon("breakpoint", "CodeEdit", p_theme->get_icon(SNAME("Breakpoint"), EditorStringName(EditorIcons)));592/* clang-format on */593594p_theme->set_constant("line_spacing", "CodeEdit", EDITOR_GET("text_editor/appearance/whitespace/line_spacing"));595596const Color background_color = EDITOR_GET("text_editor/theme/highlighting/background_color");597Ref<StyleBoxFlat> code_edit_stylebox = make_flat_stylebox(background_color, p_config.widget_margin.x, p_config.widget_margin.y, p_config.widget_margin.x, p_config.widget_margin.y, p_config.corner_radius);598p_theme->set_stylebox(CoreStringName(normal), "CodeEdit", code_edit_stylebox);599p_theme->set_stylebox("read_only", "CodeEdit", code_edit_stylebox);600p_theme->set_stylebox("focus", "CodeEdit", memnew(StyleBoxEmpty));601602/* clang-format off */603p_theme->set_color("completion_background_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/completion_background_color"));604p_theme->set_color("completion_selected_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/completion_selected_color"));605p_theme->set_color("completion_existing_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/completion_existing_color"));606p_theme->set_color("completion_scroll_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/completion_scroll_color"));607p_theme->set_color("completion_scroll_hovered_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/completion_scroll_hovered_color"));608p_theme->set_color(SceneStringName(font_color), "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/text_color"));609p_theme->set_color("line_number_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/line_number_color"));610p_theme->set_color("caret_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/caret_color"));611p_theme->set_color("font_selected_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/text_selected_color"));612p_theme->set_color("selection_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/selection_color"));613p_theme->set_color("brace_mismatch_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/brace_mismatch_color"));614p_theme->set_color("current_line_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/current_line_color"));615p_theme->set_color("line_length_guideline_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/line_length_guideline_color"));616p_theme->set_color("word_highlighted_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/word_highlighted_color"));617p_theme->set_color("bookmark_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/bookmark_color"));618p_theme->set_color("breakpoint_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/breakpoint_color"));619p_theme->set_color("executing_line_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/executing_line_color"));620p_theme->set_color("code_folding_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/code_folding_color"));621p_theme->set_color("folded_code_region_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color"));622p_theme->set_color("search_result_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/search_result_color"));623p_theme->set_color("search_result_border_color", "CodeEdit", EDITOR_GET("text_editor/theme/highlighting/search_result_border_color"));624/* clang-format on */625}626627void EditorThemeManager::_populate_visual_shader_styles(const Ref<EditorTheme> &p_theme, ThemeConfiguration &p_config) {628EditorSettings *ed_settings = EditorSettings::get_singleton();629String visual_shader_color_theme = ed_settings->get("editors/visual_editors/color_theme");630if (visual_shader_color_theme == "Default") {631// Connection type colors632ed_settings->set_initial_value("editors/visual_editors/connection_colors/scalar_color", Color(0.55, 0.55, 0.55), true);633ed_settings->set_initial_value("editors/visual_editors/connection_colors/vector2_color", Color(0.44, 0.43, 0.64), true);634ed_settings->set_initial_value("editors/visual_editors/connection_colors/vector3_color", Color(0.337, 0.314, 0.71), true);635ed_settings->set_initial_value("editors/visual_editors/connection_colors/vector4_color", Color(0.7, 0.65, 0.147), true);636ed_settings->set_initial_value("editors/visual_editors/connection_colors/boolean_color", Color(0.243, 0.612, 0.349), true);637ed_settings->set_initial_value("editors/visual_editors/connection_colors/transform_color", Color(0.71, 0.357, 0.64), true);638ed_settings->set_initial_value("editors/visual_editors/connection_colors/sampler_color", Color(0.659, 0.4, 0.137), true);639640// Node category colors (used for the node headers)641ed_settings->set_initial_value("editors/visual_editors/category_colors/output_color", Color(0.26, 0.10, 0.15), true);642ed_settings->set_initial_value("editors/visual_editors/category_colors/color_color", Color(0.5, 0.5, 0.1), true);643ed_settings->set_initial_value("editors/visual_editors/category_colors/conditional_color", Color(0.208, 0.522, 0.298), true);644ed_settings->set_initial_value("editors/visual_editors/category_colors/input_color", Color(0.502, 0.2, 0.204), true);645ed_settings->set_initial_value("editors/visual_editors/category_colors/scalar_color", Color(0.1, 0.5, 0.6), true);646ed_settings->set_initial_value("editors/visual_editors/category_colors/textures_color", Color(0.5, 0.3, 0.1), true);647ed_settings->set_initial_value("editors/visual_editors/category_colors/transform_color", Color(0.5, 0.3, 0.5), true);648ed_settings->set_initial_value("editors/visual_editors/category_colors/utility_color", Color(0.2, 0.2, 0.2), true);649ed_settings->set_initial_value("editors/visual_editors/category_colors/vector_color", Color(0.2, 0.2, 0.5), true);650ed_settings->set_initial_value("editors/visual_editors/category_colors/special_color", Color(0.098, 0.361, 0.294), true);651ed_settings->set_initial_value("editors/visual_editors/category_colors/particle_color", Color(0.12, 0.358, 0.8), true);652653} else if (visual_shader_color_theme == "Legacy") {654// Connection type colors655ed_settings->set_initial_value("editors/visual_editors/connection_colors/scalar_color", Color(0.38, 0.85, 0.96), true);656ed_settings->set_initial_value("editors/visual_editors/connection_colors/vector2_color", Color(0.74, 0.57, 0.95), true);657ed_settings->set_initial_value("editors/visual_editors/connection_colors/vector3_color", Color(0.84, 0.49, 0.93), true);658ed_settings->set_initial_value("editors/visual_editors/connection_colors/vector4_color", Color(1.0, 0.125, 0.95), true);659ed_settings->set_initial_value("editors/visual_editors/connection_colors/boolean_color", Color(0.55, 0.65, 0.94), true);660ed_settings->set_initial_value("editors/visual_editors/connection_colors/transform_color", Color(0.96, 0.66, 0.43), true);661ed_settings->set_initial_value("editors/visual_editors/connection_colors/sampler_color", Color(1.0, 1.0, 0.0), true);662663// Node category colors (used for the node headers)664Ref<StyleBoxFlat> gn_panel_style = p_theme->get_stylebox(SceneStringName(panel), "GraphNode");665Color gn_bg_color = gn_panel_style->get_bg_color();666ed_settings->set_initial_value("editors/visual_editors/category_colors/output_color", gn_bg_color, true);667ed_settings->set_initial_value("editors/visual_editors/category_colors/color_color", gn_bg_color, true);668ed_settings->set_initial_value("editors/visual_editors/category_colors/conditional_color", gn_bg_color, true);669ed_settings->set_initial_value("editors/visual_editors/category_colors/input_color", gn_bg_color, true);670ed_settings->set_initial_value("editors/visual_editors/category_colors/scalar_color", gn_bg_color, true);671ed_settings->set_initial_value("editors/visual_editors/category_colors/textures_color", gn_bg_color, true);672ed_settings->set_initial_value("editors/visual_editors/category_colors/transform_color", gn_bg_color, true);673ed_settings->set_initial_value("editors/visual_editors/category_colors/utility_color", gn_bg_color, true);674ed_settings->set_initial_value("editors/visual_editors/category_colors/vector_color", gn_bg_color, true);675ed_settings->set_initial_value("editors/visual_editors/category_colors/special_color", gn_bg_color, true);676ed_settings->set_initial_value("editors/visual_editors/category_colors/particle_color", gn_bg_color, true);677}678}679680void EditorThemeManager::_reset_dirty_flag() {681outdated_cache_dirty = true;682}683684// Public interface for theme generation.685686Ref<EditorTheme> EditorThemeManager::generate_theme(const Ref<EditorTheme> &p_old_theme) {687OS::get_singleton()->benchmark_begin_measure(get_benchmark_key(), "Generate Theme");688689Ref<EditorTheme> theme = _create_base_theme(p_old_theme);690691OS::get_singleton()->benchmark_begin_measure(get_benchmark_key(), "Merge Custom Theme");692693const String custom_theme_path = EDITOR_GET("interface/theme/custom_theme");694if (!custom_theme_path.is_empty()) {695Ref<Theme> custom_theme = ResourceLoader::load(custom_theme_path);696if (custom_theme.is_valid()) {697theme->merge_with(custom_theme);698}699}700701OS::get_singleton()->benchmark_end_measure(get_benchmark_key(), "Merge Custom Theme");702703OS::get_singleton()->benchmark_end_measure(get_benchmark_key(), "Generate Theme");704benchmark_run++;705706return theme;707}708709bool EditorThemeManager::is_generated_theme_outdated() {710// This list includes settings used by files in the editor/themes folder.711// Note that the editor scale is purposefully omitted because it cannot be changed712// without a restart, so there is no point regenerating the theme.713714if (outdated_cache_dirty) {715// TODO: We can use this information more intelligently to do partial theme updates and speed things up.716outdated_cache = EditorSettings::get_singleton()->check_changed_settings_in_group("interface/theme") ||717EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/font") ||718EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/main_font") ||719EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/code_font") ||720EditorSettings::get_singleton()->check_changed_settings_in_group("editors/visual_editors") ||721EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/theme") ||722EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/help/help") ||723EditorSettings::get_singleton()->check_changed_settings_in_group("docks/property_editor/subresource_hue_tint") ||724EditorSettings::get_singleton()->check_changed_settings_in_group("filesystem/file_dialog/thumbnail_size") ||725EditorSettings::get_singleton()->check_changed_settings_in_group("run/output/font_size");726727// The outdated flag is relevant at the moment of changing editor settings.728callable_mp_static(&EditorThemeManager::_reset_dirty_flag).call_deferred();729outdated_cache_dirty = false;730}731732return outdated_cache;733}734735bool EditorThemeManager::is_dark_theme() {736Color base_color = EDITOR_GET("interface/theme/base_color");737return base_color.get_luminance() < 0.5;738}739740bool EditorThemeManager::is_dark_icon_and_font() {741int icon_font_color_setting = EDITOR_GET("interface/theme/icon_and_font_color");742if (icon_font_color_setting == ColorMode::AUTO_COLOR) {743return is_dark_theme();744}745746return icon_font_color_setting == ColorMode::LIGHT_COLOR;747}748749void EditorThemeManager::initialize() {750EditorColorMap::create();751EditorTheme::initialize();752}753754void EditorThemeManager::finalize() {755EditorColorMap::finish();756EditorTheme::finalize();757}758759760