Skip to content

Commit

Permalink
Merge pull request #81516 from YuriSizov/editor-theme-access-the-success
Browse files Browse the repository at this point in the history
Fix accessing editor theme items throughout the UI
  • Loading branch information
YuriSizov committed Sep 15, 2023
2 parents 508a758 + 8ecc0c4 commit df6cd37
Show file tree
Hide file tree
Showing 58 changed files with 358 additions and 207 deletions.
7 changes: 7 additions & 0 deletions doc/classes/EditorInterface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@
Returns the editor's [EditorSettings] instance.
</description>
</method>
<method name="get_editor_theme" qualifiers="const">
<return type="Theme" />
<description>
Returns the editor's [Theme].
[b]Note:[/b] When creating custom editor UI, prefer accessing theme items directly from your GUI nodes using the [code]get_theme_*[/code] methods.
</description>
</method>
<method name="get_file_system_dock" qualifiers="const">
<return type="FileSystemDock" />
<description>
Expand Down
6 changes: 3 additions & 3 deletions doc/classes/EditorPlugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,15 @@
# You can use a custom icon:
return preload("res://addons/my_plugin/my_plugin_icon.svg")
# Or use a built-in icon:
return EditorInterface.get_base_control().get_theme_icon("Node", "EditorIcons")
return EditorInterface.get_editor_theme().get_icon("Node", "EditorIcons")
[/gdscript]
[csharp]
public override Texture2D _GetPluginIcon()
{
// You can use a custom icon:
return ResourceLoader.Load&lt;Texture2D&gt;("res://addons/my_plugin/my_plugin_icon.svg");
// Or use a built-in icon:
return EditorInterface.Singleton.GetBaseControl().GetThemeIcon("Node", "EditorIcons");
return EditorInterface.Singleton.GetEditorTheme().GetIcon("Node", "EditorIcons");
}
[/csharp]
[/codeblocks]
Expand Down Expand Up @@ -354,7 +354,7 @@
return "My Super Cool Plugin 3000"

func _get_plugin_icon():
return EditorInterface.get_base_control().get_theme_icon("Node", "EditorIcons")
return EditorInterface.get_editor_theme().get_icon("Node", "EditorIcons")
[/codeblock]
</description>
</method>
Expand Down
9 changes: 2 additions & 7 deletions editor/create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,16 +454,10 @@ void CreateDialog::_sbox_input(const Ref<InputEvent> &p_ie) {
}
}

void CreateDialog::_update_theme() {
search_box->set_right_icon(search_options->get_editor_theme_icon(SNAME("Search")));
favorite->set_icon(search_options->get_editor_theme_icon(SNAME("Favorites")));
}

void CreateDialog::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
connect("confirmed", callable_mp(this, &CreateDialog::_confirmed));
_update_theme();
} break;

case NOTIFICATION_EXIT_TREE: {
Expand All @@ -485,7 +479,8 @@ void CreateDialog::_notification(int p_what) {
favorites->add_theme_constant_override("icon_max_width", icon_width);
recent->set_fixed_icon_size(Size2(icon_width, icon_width));

_update_theme();
search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
favorite->set_icon(get_editor_theme_icon(SNAME("Favorites")));
} break;
}
}
Expand Down
2 changes: 0 additions & 2 deletions editor/create_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ class CreateDialog : public ConfirmationDialog {
bool _is_class_disabled_by_feature_profile(const StringName &p_class) const;
void _load_favorites_and_history();

void _update_theme();

protected:
void _notification(int p_what);
static void _bind_methods();
Expand Down
12 changes: 6 additions & 6 deletions editor/debugger/editor_debugger_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ EditorDebuggerNode::EditorDebuggerNode() {
singleton = this;
}

add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_LEFT));
add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_RIGHT));
add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_LEFT));
add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_RIGHT));

tabs = memnew(TabContainer);
tabs->set_tabs_visible(false);
Expand Down Expand Up @@ -119,7 +119,7 @@ ScriptEditorDebugger *EditorDebuggerNode::_add_debugger() {
if (tabs->get_tab_count() > 1) {
node->clear_style();
tabs->set_tabs_visible(true);
tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
}

if (!debugger_plugins.is_empty()) {
Expand Down Expand Up @@ -284,10 +284,10 @@ void EditorDebuggerNode::_notification(int p_what) {
switch (p_what) {
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
if (tabs->get_tab_count() > 1) {
add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_LEFT));
add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_RIGHT));
add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_LEFT));
add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_RIGHT));

tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
}
} break;

Expand Down
2 changes: 1 addition & 1 deletion editor/editor_autoload_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ EditorAutoloadSettings::EditorAutoloadSettings() {
error_message = memnew(Label);
error_message->hide();
error_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
error_message->add_theme_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
error_message->add_theme_color_override("font_color", EditorNode::get_singleton()->get_editor_theme()->get_color(SNAME("error_color"), EditorStringName(Editor)));
add_child(error_message);

Label *l = memnew(Label);
Expand Down
6 changes: 6 additions & 0 deletions editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "scene/gui/box_container.h"
#include "scene/gui/control.h"
#include "scene/main/window.h"
#include "scene/resources/theme.h"

EditorInterface *EditorInterface::singleton = nullptr;

Expand Down Expand Up @@ -196,6 +197,10 @@ bool EditorInterface::is_plugin_enabled(const String &p_plugin) const {

// Editor GUI.

Ref<Theme> EditorInterface::get_editor_theme() const {
return EditorNode::get_singleton()->get_editor_theme();
}

Control *EditorInterface::get_base_control() const {
return EditorNode::get_singleton()->get_gui_base();
}
Expand Down Expand Up @@ -405,6 +410,7 @@ void EditorInterface::_bind_methods() {

// Editor GUI.

ClassDB::bind_method(D_METHOD("get_editor_theme"), &EditorInterface::get_editor_theme);
ClassDB::bind_method(D_METHOD("get_base_control"), &EditorInterface::get_base_control);
ClassDB::bind_method(D_METHOD("get_editor_main_screen"), &EditorInterface::get_editor_main_screen);
ClassDB::bind_method(D_METHOD("get_script_editor"), &EditorInterface::get_script_editor);
Expand Down
6 changes: 3 additions & 3 deletions editor/editor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Mesh;
class Node;
class ScriptEditor;
class Texture2D;
class Theme;
class VBoxContainer;
class Window;

Expand Down Expand Up @@ -84,11 +85,10 @@ class EditorInterface : public Object {
void set_plugin_enabled(const String &p_plugin, bool p_enabled);
bool is_plugin_enabled(const String &p_plugin) const;

void add_editor_plugin(EditorPlugin *p_plugin);
void remove_editor_plugin(EditorPlugin *p_plugin);

// Editor GUI.

Ref<Theme> get_editor_theme() const;

Control *get_base_control() const;
VBoxContainer *get_editor_main_screen() const;
ScriptEditor *get_script_editor() const;
Expand Down
11 changes: 0 additions & 11 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,6 @@ void EditorNode::_update_theme(bool p_skip_creation) {
scene_root_parent->add_theme_style_override("panel", theme->get_stylebox(SNAME("Content"), EditorStringName(EditorStyles)));
bottom_panel->add_theme_style_override("panel", theme->get_stylebox(SNAME("BottomPanel"), EditorStringName(EditorStyles)));
main_menu->add_theme_style_override("hover", theme->get_stylebox(SNAME("MenuHover"), EditorStringName(EditorStyles)));
prev_scene->set_icon(theme->get_icon(SNAME("PrevScene"), EditorStringName(EditorIcons)));
distraction_free->set_icon(theme->get_icon(SNAME("DistractionFree"), EditorStringName(EditorIcons)));
bottom_panel_raise->set_icon(theme->get_icon(SNAME("ExpandBottomDock"), EditorStringName(EditorIcons)));

Expand Down Expand Up @@ -3782,7 +3781,6 @@ Error EditorNode::load_scene(const String &p_scene, bool p_ignore_broken_deps, b
editor_folding.save_scene_folding(new_scene, lpath);
}

prev_scene->set_disabled(previous_scenes.size() == 0);
opening_prev = false;

EditorDebuggerNode::get_singleton()->update_live_edit_root();
Expand Down Expand Up @@ -7153,15 +7151,6 @@ EditorNode::EditorNode() {
main_menu->add_child(file_menu);
main_menu->set_menu_tooltip(0, TTR("Operations with scene files."));

prev_scene = memnew(Button);
prev_scene->set_flat(true);
prev_scene->set_tooltip_text(TTR("Go to previously opened scene."));
prev_scene->set_disabled(true);
prev_scene->connect("pressed", callable_mp(this, &EditorNode::_menu_option).bind(FILE_OPEN_PREV));
gui_base->add_child(prev_scene);
prev_scene->set_position(Point2(3, 24));
prev_scene->hide();

accept = memnew(AcceptDialog);
accept->set_unparent_when_invisible(true);

Expand Down
1 change: 0 additions & 1 deletion editor/editor_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ class EditorNode : public Node {
PopupMenu *tool_menu = nullptr;
PopupMenu *export_as_menu = nullptr;
Button *export_button = nullptr;
Button *prev_scene = nullptr;
Button *search_button = nullptr;
TextureProgressBar *audio_vu = nullptr;

Expand Down
100 changes: 99 additions & 1 deletion editor/editor_themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions editor/editor_themes.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ class EditorColorMap {
static HashSet<StringName> &get_color_conversion_exceptions() { return color_conversion_exceptions; };
};

class EditorTheme : public Theme {
GDCLASS(EditorTheme, Theme);

static Vector<StringName> editor_theme_types;

public:
virtual Color get_color(const StringName &p_name, const StringName &p_theme_type) const override;
virtual int get_constant(const StringName &p_name, const StringName &p_theme_type) const override;
virtual Ref<Font> get_font(const StringName &p_name, const StringName &p_theme_type) const override;
virtual int get_font_size(const StringName &p_name, const StringName &p_theme_type) const override;
virtual Ref<Texture2D> get_icon(const StringName &p_name, const StringName &p_theme_type) const override;
virtual Ref<StyleBox> get_stylebox(const StringName &p_name, const StringName &p_theme_type) const override;

EditorTheme();
};

Ref<Theme> create_editor_theme(Ref<Theme> p_theme = nullptr);

Ref<Theme> create_custom_theme(Ref<Theme> p_theme = nullptr);
Expand Down
Loading

0 comments on commit df6cd37

Please sign in to comment.