From fc01e2e7f6914ba3d5fd22fd01e847f2b2387233 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Wed, 6 Sep 2023 16:11:05 +0200 Subject: [PATCH] Add a theme preview switcher to the 2D viewport This commit adds a new View submenu that allows switching between the project theme (default), the editor theme, and the default theme. The last selected option is stored per project and is restored when reloading the project. --- editor/editor_node.cpp | 42 +++++++++++++++++--- editor/editor_node.h | 2 + editor/plugins/canvas_item_editor_plugin.cpp | 30 ++++++++++++++ editor/plugins/canvas_item_editor_plugin.h | 16 ++++++++ 4 files changed, 85 insertions(+), 5 deletions(-) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 6f14a4568a2..2d867ed8c27 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -482,6 +482,10 @@ void EditorNode::_update_theme(bool p_skip_creation) { } } + if (CanvasItemEditor::get_singleton()->get_theme_preview() == CanvasItemEditor::THEME_PREVIEW_EDITOR) { + update_preview_themes(CanvasItemEditor::THEME_PREVIEW_EDITOR); + } + gui_base->add_theme_style_override("panel", theme->get_stylebox(SNAME("Background"), EditorStringName(EditorStyles))); main_vbox->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT, Control::PRESET_MODE_MINSIZE, theme->get_constant(SNAME("window_border_margin"), EditorStringName(Editor))); main_vbox->add_theme_constant_override("separation", theme->get_constant(SNAME("top_bar_separation"), EditorStringName(Editor))); @@ -534,6 +538,36 @@ void EditorNode::_update_theme(bool p_skip_creation) { } } +void EditorNode::update_preview_themes(int p_mode) { + if (!scene_root->is_inside_tree()) { + return; // Too early. + } + + List> preview_themes; + + switch (p_mode) { + case CanvasItemEditor::THEME_PREVIEW_PROJECT: + preview_themes.push_back(ThemeDB::get_singleton()->get_project_theme()); + break; + + case CanvasItemEditor::THEME_PREVIEW_EDITOR: + preview_themes.push_back(get_editor_theme()); + break; + + default: + break; + } + + preview_themes.push_back(ThemeDB::get_singleton()->get_default_theme()); + + ThemeContext *preview_context = ThemeDB::get_singleton()->get_theme_context(scene_root); + if (preview_context) { + preview_context->set_themes(preview_themes); + } else { + ThemeDB::get_singleton()->create_theme_context(scene_root, preview_themes); + } +} + void EditorNode::_notification(int p_what) { switch (p_what) { case NOTIFICATION_PROCESS: { @@ -668,11 +702,9 @@ void EditorNode::_notification(int p_what) { _titlebar_resized(); - // Set up a theme context for the 2D preview viewport using the project theme. - List> preview_themes; - preview_themes.push_back(ThemeDB::get_singleton()->get_project_theme()); - preview_themes.push_back(ThemeDB::get_singleton()->get_default_theme()); - ThemeDB::get_singleton()->create_theme_context(scene_root, preview_themes); + // Set up a theme context for the 2D preview viewport using the stored preview theme. + CanvasItemEditor::ThemePreviewMode theme_preview_mode = (CanvasItemEditor::ThemePreviewMode)(int)EditorSettings::get_singleton()->get_project_metadata("2d_editor", "theme_preview", CanvasItemEditor::THEME_PREVIEW_PROJECT); + update_preview_themes(theme_preview_mode); /* DO NOT LOAD SCENES HERE, WAIT FOR FILE SCANNING AND REIMPORT TO COMPLETE */ } break; diff --git a/editor/editor_node.h b/editor/editor_node.h index 1119f6b896b..a83570b2ea8 100644 --- a/editor/editor_node.h +++ b/editor/editor_node.h @@ -851,6 +851,8 @@ public: void stop_child_process(OS::ProcessID p_pid); Ref get_editor_theme() const { return theme; } + void update_preview_themes(int p_mode); + Ref