Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Add support for partial custom editor themes #51648

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/classes/Theme.xml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@
Returns [code]false[/code] if the theme does not have [code]node_type[/code].
</description>
</method>
<method name="merge_with">
<return type="void" />
<argument index="0" name="other" type="Theme" />
<description>
Adds missing and overrides existing definitions with values from the [code]other[/code] [Theme].
[b]Note:[/b] This modifies the current theme. If you want to merge two themes together without modifying either one, create a new empty theme and merge the other two into it one after another.
</description>
</method>
<method name="rename_color">
<return type="void" />
<argument index="0" name="old_name" type="String" />
Expand Down
20 changes: 12 additions & 8 deletions editor/editor_themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,11 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
theme->set_stylebox("panel", "PanelContainer", style_menu);
theme->set_stylebox("MenuPanel", "EditorStyles", style_menu);

// CanvasItem Editor
Ref<StyleBoxFlat> style_canvas_editor_info = make_flat_stylebox(Color(0.0, 0.0, 0.0, 0.2));
style_canvas_editor_info->set_expand_margin_size_all(4 * EDSCALE);
theme->set_stylebox("CanvasItemInfoOverlay", "EditorStyles", style_canvas_editor_info);

// Script Editor
theme->set_stylebox("ScriptEditorPanel", "EditorStyles", make_empty_stylebox(default_margin_size, 0, default_margin_size, default_margin_size));
theme->set_stylebox("ScriptEditor", "EditorStyles", make_empty_stylebox(0, 0, 0, 0));
Expand Down Expand Up @@ -1376,15 +1381,14 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
}

Ref<Theme> create_custom_theme(const Ref<Theme> p_theme) {
Ref<Theme> theme;

const String custom_theme = EditorSettings::get_singleton()->get("interface/theme/custom_theme");
if (custom_theme != "") {
theme = ResourceLoader::load(custom_theme);
}
Ref<Theme> theme = create_editor_theme(p_theme);

if (!theme.is_valid()) {
theme = create_editor_theme(p_theme);
const String custom_theme_path = EditorSettings::get_singleton()->get("interface/theme/custom_theme");
if (custom_theme_path != "") {
Ref<Theme> custom_theme = ResourceLoader::load(custom_theme_path);
if (custom_theme.is_valid()) {
theme->merge_with(custom_theme);
}
}

return theme;
Expand Down
13 changes: 5 additions & 8 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4198,6 +4198,10 @@ void CanvasItemEditor::_notification(int p_what) {
font->set_outline_color(Color(0, 0, 0));
zoom_reset->add_font_override("font", font);
zoom_reset->add_color_override("font_color", Color(1, 1, 1));

info_overlay->get_theme()->set_stylebox("normal", "Label", get_stylebox("CanvasItemInfoOverlay", "EditorStyles"));
warning_child_of_container->add_color_override("font_color", get_color("warning_color", "Editor"));
warning_child_of_container->add_font_override("font", get_font("main", "EditorFonts"));
}

if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
Expand Down Expand Up @@ -5778,20 +5782,13 @@ CanvasItemEditor::CanvasItemEditor(EditorNode *p_editor) {
info_overlay->add_constant_override("separation", 10);
viewport_scrollable->add_child(info_overlay);

// Make sure all labels inside of the container are styled the same.
Theme *info_overlay_theme = memnew(Theme);
info_overlay_theme->copy_default_theme();
info_overlay->set_theme(info_overlay_theme);

StyleBoxFlat *info_overlay_label_stylebox = memnew(StyleBoxFlat);
info_overlay_label_stylebox->set_bg_color(Color(0.0, 0.0, 0.0, 0.2));
info_overlay_label_stylebox->set_expand_margin_size_all(4);
info_overlay_theme->set_stylebox("normal", "Label", info_overlay_label_stylebox);

warning_child_of_container = memnew(Label);
warning_child_of_container->hide();
warning_child_of_container->set_text(TTR("Warning: Children of a container get their position and size determined only by their parent."));
warning_child_of_container->add_color_override("font_color", EditorNode::get_singleton()->get_gui_base()->get_color("warning_color", "Editor"));
warning_child_of_container->add_font_override("font", EditorNode::get_singleton()->get_gui_base()->get_font("main", "EditorFonts"));
add_control_to_info_overlay(warning_child_of_container);

h_scroll = memnew(HScrollBar);
Expand Down
77 changes: 77 additions & 0 deletions scene/resources/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,82 @@ void Theme::copy_theme(const Ref<Theme> &p_other) {
_unfreeze_and_propagate_changes();
}

void Theme::merge_with(const Ref<Theme> &p_other) {
if (p_other.is_null()) {
return;
}

_freeze_change_propagation();

// Colors.
{
const StringName *K = nullptr;
while ((K = p_other->color_map.next(K))) {
const StringName *L = nullptr;
while ((L = p_other->color_map[*K].next(L))) {
set_color(*L, *K, p_other->color_map[*K][*L]);
}
}
}

// Constants.
{
const StringName *K = nullptr;
while ((K = p_other->constant_map.next(K))) {
const StringName *L = nullptr;
while ((L = p_other->constant_map[*K].next(L))) {
set_constant(*L, *K, p_other->constant_map[*K][*L]);
}
}
}

// Fonts.
{
const StringName *K = nullptr;
while ((K = p_other->font_map.next(K))) {
const StringName *L = nullptr;
while ((L = p_other->font_map[*K].next(L))) {
set_font(*L, *K, p_other->font_map[*K][*L]);
}
}
}

// Icons.
{
const StringName *K = nullptr;
while ((K = p_other->icon_map.next(K))) {
const StringName *L = nullptr;
while ((L = p_other->icon_map[*K].next(L))) {
set_icon(*L, *K, p_other->icon_map[*K][*L]);
}
}
}

// Shaders.
{
const StringName *K = nullptr;
while ((K = p_other->shader_map.next(K))) {
const StringName *L = nullptr;
while ((L = p_other->shader_map[*K].next(L))) {
set_shader(*L, *K, p_other->shader_map[*K][*L]);
}
}
}

// Styleboxes.
{
const StringName *K = nullptr;
while ((K = p_other->style_map.next(K))) {
const StringName *L = nullptr;
while ((L = p_other->style_map[*K].next(L))) {
set_stylebox(*L, *K, p_other->style_map[*K][*L]);
}
}
}

_unfreeze_and_propagate_changes();
}

void Theme::get_type_list(List<StringName> *p_list) const {
ERR_FAIL_NULL(p_list);

Expand Down Expand Up @@ -1281,6 +1357,7 @@ void Theme::_bind_methods() {

ClassDB::bind_method("copy_default_theme", &Theme::copy_default_theme);
ClassDB::bind_method(D_METHOD("copy_theme", "other"), &Theme::copy_theme);
ClassDB::bind_method(D_METHOD("merge_with", "other"), &Theme::merge_with);

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "default_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_default_font", "get_default_font");

Expand Down
1 change: 1 addition & 0 deletions scene/resources/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class Theme : public Resource {

void copy_default_theme();
void copy_theme(const Ref<Theme> &p_other);
void merge_with(const Ref<Theme> &p_other);
void clear();

Theme();
Expand Down