From 50f4c298e68b8339947b293d2cc8d5a1aaed0a24 Mon Sep 17 00:00:00 2001 From: MewPurPur Date: Thu, 29 Jun 2023 13:48:53 +0200 Subject: [PATCH] Improve material and mesh preview buttons --- editor/editor_themes.cpp | 37 +++++++-- editor/icons/MaterialPreviewCube.svg | 2 +- editor/icons/MaterialPreviewCubeOff.svg | 1 - editor/icons/MaterialPreviewLight1.svg | 2 +- editor/icons/MaterialPreviewLight1Off.svg | 1 - editor/icons/MaterialPreviewLight2.svg | 2 +- editor/icons/MaterialPreviewLight2Off.svg | 1 - editor/icons/MaterialPreviewSphere.svg | 2 +- editor/icons/MaterialPreviewSphereOff.svg | 1 - editor/plugins/material_editor_plugin.cpp | 92 +++++++++++----------- editor/plugins/material_editor_plugin.h | 30 ++++--- editor/plugins/mesh_editor_plugin.cpp | 47 +++++------ editor/plugins/mesh_editor_plugin.h | 15 ++-- editor/plugins/style_box_editor_plugin.cpp | 7 +- 14 files changed, 123 insertions(+), 117 deletions(-) delete mode 100644 editor/icons/MaterialPreviewCubeOff.svg delete mode 100644 editor/icons/MaterialPreviewLight1Off.svg delete mode 100644 editor/icons/MaterialPreviewLight2Off.svg delete mode 100644 editor/icons/MaterialPreviewSphereOff.svg diff --git a/editor/editor_themes.cpp b/editor/editor_themes.cpp index 92c111a418e..92f64c0cd8d 100644 --- a/editor/editor_themes.cpp +++ b/editor/editor_themes.cpp @@ -173,12 +173,6 @@ void EditorColorMap::create() { add_conversion_exception("Sky"); add_conversion_exception("EditorControlAnchor"); add_conversion_exception("DefaultProjectIcon"); - add_conversion_exception("GuiChecked"); - add_conversion_exception("GuiRadioChecked"); - add_conversion_exception("GuiIndeterminate"); - add_conversion_exception("GuiCloseCustomizable"); - add_conversion_exception("GuiGraphNodePort"); - add_conversion_exception("GuiResizer"); add_conversion_exception("ZoomMore"); add_conversion_exception("ZoomLess"); add_conversion_exception("ZoomReset"); @@ -188,6 +182,18 @@ void EditorColorMap::create() { add_conversion_exception("StatusSuccess"); add_conversion_exception("StatusWarning"); add_conversion_exception("OverbrightIndicator"); + add_conversion_exception("MaterialPreviewCube"); + add_conversion_exception("MaterialPreviewSphere"); + add_conversion_exception("MaterialPreviewLight1"); + add_conversion_exception("MaterialPreviewLight2"); + + // GUI + add_conversion_exception("GuiChecked"); + add_conversion_exception("GuiRadioChecked"); + add_conversion_exception("GuiIndeterminate"); + add_conversion_exception("GuiCloseCustomizable"); + add_conversion_exception("GuiGraphNodePort"); + add_conversion_exception("GuiResizer"); add_conversion_exception("GuiMiniCheckerboard"); /// Code Editor. @@ -941,6 +947,25 @@ Ref create_editor_theme(const Ref p_theme) { editor_log_button_pressed->set_border_color(accent_color); theme->set_stylebox("pressed", "EditorLogFilterButton", editor_log_button_pressed); + // Buttons in material previews + const Color dim_light_color = icon_normal_color.darkened(0.24); + const Color dim_light_highlighted_color = icon_normal_color.darkened(0.18); + Ref sb_empty_borderless = make_empty_stylebox(); + + theme->set_type_variation("PreviewLightButton", "Button"); + // When pressed, don't use the accent color tint. When unpressed, dim the icon. + theme->set_color("icon_normal_color", "PreviewLightButton", dim_light_color); + theme->set_color("icon_focus_color", "PreviewLightButton", dim_light_color); + theme->set_color("icon_pressed_color", "PreviewLightButton", icon_normal_color); + theme->set_color("icon_hover_pressed_color", "PreviewLightButton", icon_normal_color); + // Unpressed icon is dim, so use a dim highlight. + theme->set_color("icon_hover_color", "PreviewLightButton", dim_light_highlighted_color); + + theme->set_stylebox("normal", "PreviewLightButton", sb_empty_borderless); + theme->set_stylebox("hover", "PreviewLightButton", sb_empty_borderless); + theme->set_stylebox("focus", "PreviewLightButton", sb_empty_borderless); + theme->set_stylebox("pressed", "PreviewLightButton", sb_empty_borderless); + // ProjectTag { theme->set_type_variation("ProjectTag", "Button"); diff --git a/editor/icons/MaterialPreviewCube.svg b/editor/icons/MaterialPreviewCube.svg index e7e56d02aa8..68d4595bc28 100644 --- a/editor/icons/MaterialPreviewCube.svg +++ b/editor/icons/MaterialPreviewCube.svg @@ -1 +1 @@ - + diff --git a/editor/icons/MaterialPreviewCubeOff.svg b/editor/icons/MaterialPreviewCubeOff.svg deleted file mode 100644 index ccf8a1e8f68..00000000000 --- a/editor/icons/MaterialPreviewCubeOff.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/editor/icons/MaterialPreviewLight1.svg b/editor/icons/MaterialPreviewLight1.svg index 5e0f7539c4b..ef21a4ecd7c 100644 --- a/editor/icons/MaterialPreviewLight1.svg +++ b/editor/icons/MaterialPreviewLight1.svg @@ -1 +1 @@ - + diff --git a/editor/icons/MaterialPreviewLight1Off.svg b/editor/icons/MaterialPreviewLight1Off.svg deleted file mode 100644 index 47f9ee83c48..00000000000 --- a/editor/icons/MaterialPreviewLight1Off.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/editor/icons/MaterialPreviewLight2.svg b/editor/icons/MaterialPreviewLight2.svg index 7370caf17fb..77148648406 100644 --- a/editor/icons/MaterialPreviewLight2.svg +++ b/editor/icons/MaterialPreviewLight2.svg @@ -1 +1 @@ - + diff --git a/editor/icons/MaterialPreviewLight2Off.svg b/editor/icons/MaterialPreviewLight2Off.svg deleted file mode 100644 index 2cbfa4f176c..00000000000 --- a/editor/icons/MaterialPreviewLight2Off.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/editor/icons/MaterialPreviewSphere.svg b/editor/icons/MaterialPreviewSphere.svg index 949d1ef3a77..e3c58fefe38 100644 --- a/editor/icons/MaterialPreviewSphere.svg +++ b/editor/icons/MaterialPreviewSphere.svg @@ -1 +1 @@ - + diff --git a/editor/icons/MaterialPreviewSphereOff.svg b/editor/icons/MaterialPreviewSphereOff.svg deleted file mode 100644 index 5ebd36f85fd..00000000000 --- a/editor/icons/MaterialPreviewSphereOff.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/editor/plugins/material_editor_plugin.cpp b/editor/plugins/material_editor_plugin.cpp index 404711e0742..e24172e7610 100644 --- a/editor/plugins/material_editor_plugin.cpp +++ b/editor/plugins/material_editor_plugin.cpp @@ -39,9 +39,9 @@ #include "scene/3d/light_3d.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/gui/box_container.h" +#include "scene/gui/button.h" #include "scene/gui/color_rect.h" #include "scene/gui/subviewport_container.h" -#include "scene/gui/texture_button.h" #include "scene/main/viewport.h" #include "scene/resources/fog_material.h" #include "scene/resources/particle_process_material.h" @@ -63,15 +63,11 @@ void MaterialEditor::gui_input(const Ref &p_event) { void MaterialEditor::_update_theme_item_cache() { Control::_update_theme_item_cache(); - theme_cache.light_1_on = get_theme_icon(SNAME("MaterialPreviewLight1"), SNAME("EditorIcons")); - theme_cache.light_1_off = get_theme_icon(SNAME("MaterialPreviewLight1Off"), SNAME("EditorIcons")); - theme_cache.light_2_on = get_theme_icon(SNAME("MaterialPreviewLight2"), SNAME("EditorIcons")); - theme_cache.light_2_off = get_theme_icon(SNAME("MaterialPreviewLight2Off"), SNAME("EditorIcons")); + theme_cache.light_1_icon = get_theme_icon(SNAME("MaterialPreviewLight1"), SNAME("EditorIcons")); + theme_cache.light_2_icon = get_theme_icon(SNAME("MaterialPreviewLight2"), SNAME("EditorIcons")); - theme_cache.sphere_on = get_theme_icon(SNAME("MaterialPreviewSphere"), SNAME("EditorIcons")); - theme_cache.sphere_off = get_theme_icon(SNAME("MaterialPreviewSphereOff"), SNAME("EditorIcons")); - theme_cache.box_on = get_theme_icon(SNAME("MaterialPreviewCube"), SNAME("EditorIcons")); - theme_cache.box_off = get_theme_icon(SNAME("MaterialPreviewCubeOff"), SNAME("EditorIcons")); + theme_cache.sphere_icon = get_theme_icon(SNAME("MaterialPreviewSphere"), SNAME("EditorIcons")); + theme_cache.box_icon = get_theme_icon(SNAME("MaterialPreviewCube"), SNAME("EditorIcons")); theme_cache.checkerboard = get_theme_icon(SNAME("Checkerboard"), SNAME("EditorIcons")); } @@ -79,15 +75,11 @@ void MaterialEditor::_update_theme_item_cache() { void MaterialEditor::_notification(int p_what) { switch (p_what) { case NOTIFICATION_THEME_CHANGED: { - light_1_switch->set_texture_normal(theme_cache.light_1_on); - light_1_switch->set_texture_pressed(theme_cache.light_1_off); - light_2_switch->set_texture_normal(theme_cache.light_2_on); - light_2_switch->set_texture_pressed(theme_cache.light_2_off); + light_1_switch->set_icon(theme_cache.light_1_icon); + light_2_switch->set_icon(theme_cache.light_2_icon); - sphere_switch->set_texture_normal(theme_cache.sphere_off); - sphere_switch->set_texture_pressed(theme_cache.sphere_on); - box_switch->set_texture_normal(theme_cache.box_off); - box_switch->set_texture_pressed(theme_cache.box_on); + sphere_switch->set_icon(theme_cache.sphere_icon); + box_switch->set_icon(theme_cache.box_icon); } break; case NOTIFICATION_DRAW: { @@ -135,34 +127,32 @@ void MaterialEditor::edit(Ref p_material, const Ref &p_en _update_rotation(); } -void MaterialEditor::_button_pressed(Node *p_button) { - if (p_button == light_1_switch) { - light1->set_visible(!light_1_switch->is_pressed()); - } +void MaterialEditor::_on_light_1_switch_pressed() { + light1->set_visible(light_1_switch->is_pressed()); +} - if (p_button == light_2_switch) { - light2->set_visible(!light_2_switch->is_pressed()); - } +void MaterialEditor::_on_light_2_switch_pressed() { + light2->set_visible(light_2_switch->is_pressed()); +} - if (p_button == box_switch) { - box_instance->show(); - sphere_instance->hide(); - box_switch->set_pressed(true); - sphere_switch->set_pressed(false); - EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", false); - } +void MaterialEditor::_on_sphere_switch_pressed() { + box_instance->hide(); + sphere_instance->show(); + box_switch->set_pressed(false); + sphere_switch->set_pressed(true); + EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", true); +} - if (p_button == sphere_switch) { - box_instance->hide(); - sphere_instance->show(); - box_switch->set_pressed(false); - sphere_switch->set_pressed(true); - EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", true); - } +void MaterialEditor::_on_box_switch_pressed() { + box_instance->show(); + sphere_instance->hide(); + box_switch->set_pressed(true); + sphere_switch->set_pressed(false); + EditorSettings::get_singleton()->set_project_metadata("inspector_options", "material_preview_on_sphere", false); } MaterialEditor::MaterialEditor() { - // canvas item + // Canvas item vc_2d = memnew(SubViewportContainer); vc_2d->set_stretch(true); @@ -185,7 +175,7 @@ MaterialEditor::MaterialEditor() { layout_2d->set_visible(false); - // spatial + // Spatial vc = memnew(SubViewportContainer); vc->set_stretch(true); @@ -247,32 +237,38 @@ MaterialEditor::MaterialEditor() { VBoxContainer *vb_shape = memnew(VBoxContainer); layout_3d->add_child(vb_shape); - sphere_switch = memnew(TextureButton); + sphere_switch = memnew(Button); + sphere_switch->set_theme_type_variation("PreviewLightButton"); sphere_switch->set_toggle_mode(true); sphere_switch->set_pressed(true); vb_shape->add_child(sphere_switch); - sphere_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed).bind(sphere_switch)); + sphere_switch->connect("pressed", callable_mp(this, &MaterialEditor::_on_sphere_switch_pressed)); - box_switch = memnew(TextureButton); + box_switch = memnew(Button); + box_switch->set_theme_type_variation("PreviewLightButton"); box_switch->set_toggle_mode(true); box_switch->set_pressed(false); vb_shape->add_child(box_switch); - box_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed).bind(box_switch)); + box_switch->connect("pressed", callable_mp(this, &MaterialEditor::_on_box_switch_pressed)); layout_3d->add_spacer(); VBoxContainer *vb_light = memnew(VBoxContainer); layout_3d->add_child(vb_light); - light_1_switch = memnew(TextureButton); + light_1_switch = memnew(Button); + light_1_switch->set_theme_type_variation("PreviewLightButton"); light_1_switch->set_toggle_mode(true); + light_1_switch->set_pressed(true); vb_light->add_child(light_1_switch); - light_1_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed).bind(light_1_switch)); + light_1_switch->connect("pressed", callable_mp(this, &MaterialEditor::_on_light_1_switch_pressed)); - light_2_switch = memnew(TextureButton); + light_2_switch = memnew(Button); + light_2_switch->set_theme_type_variation("PreviewLightButton"); light_2_switch->set_toggle_mode(true); + light_2_switch->set_pressed(true); vb_light->add_child(light_2_switch); - light_2_switch->connect("pressed", callable_mp(this, &MaterialEditor::_button_pressed).bind(light_2_switch)); + light_2_switch->connect("pressed", callable_mp(this, &MaterialEditor::_on_light_2_switch_pressed)); if (EditorSettings::get_singleton()->get_project_metadata("inspector_options", "material_preview_on_sphere", true)) { box_instance->hide(); diff --git a/editor/plugins/material_editor_plugin.h b/editor/plugins/material_editor_plugin.h index ac81bdc7c79..5530592a815 100644 --- a/editor/plugins/material_editor_plugin.h +++ b/editor/plugins/material_editor_plugin.h @@ -44,7 +44,7 @@ class HBoxContainer; class MeshInstance3D; class SubViewport; class SubViewportContainer; -class TextureButton; +class Button; class MaterialEditor : public Control { GDCLASS(MaterialEditor, Control); @@ -71,27 +71,25 @@ class MaterialEditor : public Control { HBoxContainer *layout_3d = nullptr; - TextureButton *sphere_switch = nullptr; - TextureButton *box_switch = nullptr; - - TextureButton *light_1_switch = nullptr; - TextureButton *light_2_switch = nullptr; - Ref material; + Button *sphere_switch = nullptr; + Button *box_switch = nullptr; + Button *light_1_switch = nullptr; + Button *light_2_switch = nullptr; + struct ThemeCache { - Ref light_1_on; - Ref light_1_off; - Ref light_2_on; - Ref light_2_off; - Ref sphere_on; - Ref sphere_off; - Ref box_on; - Ref box_off; + Ref light_1_icon; + Ref light_2_icon; + Ref sphere_icon; + Ref box_icon; Ref checkerboard; } theme_cache; - void _button_pressed(Node *p_button); + void _on_light_1_switch_pressed(); + void _on_light_2_switch_pressed(); + void _on_sphere_switch_pressed(); + void _on_box_switch_pressed(); protected: virtual void _update_theme_item_cache() override; diff --git a/editor/plugins/mesh_editor_plugin.cpp b/editor/plugins/mesh_editor_plugin.cpp index ee555ae8d42..b6be9713706 100644 --- a/editor/plugins/mesh_editor_plugin.cpp +++ b/editor/plugins/mesh_editor_plugin.cpp @@ -32,7 +32,7 @@ #include "core/config/project_settings.h" #include "editor/editor_scale.h" -#include "scene/gui/texture_button.h" +#include "scene/gui/button.h" #include "scene/main/viewport.h" void MeshEditor::gui_input(const Ref &p_event) { @@ -42,11 +42,8 @@ void MeshEditor::gui_input(const Ref &p_event) { if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) { rot_x -= mm->get_relative().y * 0.01; rot_y -= mm->get_relative().x * 0.01; - if (rot_x < -Math_PI / 2) { - rot_x = -Math_PI / 2; - } else if (rot_x > Math_PI / 2) { - rot_x = Math_PI / 2; - } + + rot_x = CLAMP(rot_x, -Math_PI / 2, Math_PI / 2); _update_rotation(); } } @@ -54,19 +51,15 @@ void MeshEditor::gui_input(const Ref &p_event) { void MeshEditor::_update_theme_item_cache() { SubViewportContainer::_update_theme_item_cache(); - theme_cache.light_1_on = get_theme_icon(SNAME("MaterialPreviewLight1"), SNAME("EditorIcons")); - theme_cache.light_1_off = get_theme_icon(SNAME("MaterialPreviewLight1Off"), SNAME("EditorIcons")); - theme_cache.light_2_on = get_theme_icon(SNAME("MaterialPreviewLight2"), SNAME("EditorIcons")); - theme_cache.light_2_off = get_theme_icon(SNAME("MaterialPreviewLight2Off"), SNAME("EditorIcons")); + theme_cache.light_1_icon = get_theme_icon(SNAME("MaterialPreviewLight1"), SNAME("EditorIcons")); + theme_cache.light_2_icon = get_theme_icon(SNAME("MaterialPreviewLight2"), SNAME("EditorIcons")); } void MeshEditor::_notification(int p_what) { switch (p_what) { case NOTIFICATION_THEME_CHANGED: { - light_1_switch->set_texture_normal(theme_cache.light_1_on); - light_1_switch->set_texture_pressed(theme_cache.light_1_off); - light_2_switch->set_texture_normal(theme_cache.light_2_on); - light_2_switch->set_texture_pressed(theme_cache.light_2_off); + light_1_switch->set_icon(theme_cache.light_1_icon); + light_2_switch->set_icon(theme_cache.light_2_icon); } break; } } @@ -100,21 +93,19 @@ void MeshEditor::edit(Ref p_mesh) { } } -void MeshEditor::_button_pressed(Node *p_button) { - if (p_button == light_1_switch) { - light1->set_visible(!light_1_switch->is_pressed()); - } +void MeshEditor::_on_light_1_switch_pressed() { + light1->set_visible(light_1_switch->is_pressed()); +} - if (p_button == light_2_switch) { - light2->set_visible(!light_2_switch->is_pressed()); - } +void MeshEditor::_on_light_2_switch_pressed() { + light2->set_visible(light_2_switch->is_pressed()); } MeshEditor::MeshEditor() { viewport = memnew(SubViewport); Ref world_3d; world_3d.instantiate(); - viewport->set_world_3d(world_3d); //use own world + viewport->set_world_3d(world_3d); // Use own world. add_child(viewport); viewport->set_disable_input(true); viewport->set_msaa_3d(Viewport::MSAA_4X); @@ -154,15 +145,19 @@ MeshEditor::MeshEditor() { VBoxContainer *vb_light = memnew(VBoxContainer); hb->add_child(vb_light); - light_1_switch = memnew(TextureButton); + light_1_switch = memnew(Button); + light_1_switch->set_theme_type_variation("PreviewLightButton"); light_1_switch->set_toggle_mode(true); + light_1_switch->set_pressed(true); vb_light->add_child(light_1_switch); - light_1_switch->connect("pressed", callable_mp(this, &MeshEditor::_button_pressed).bind(light_1_switch)); + light_1_switch->connect("pressed", callable_mp(this, &MeshEditor::_on_light_1_switch_pressed)); - light_2_switch = memnew(TextureButton); + light_2_switch = memnew(Button); + light_2_switch->set_theme_type_variation("PreviewLightButton"); light_2_switch->set_toggle_mode(true); + light_2_switch->set_pressed(true); vb_light->add_child(light_2_switch); - light_2_switch->connect("pressed", callable_mp(this, &MeshEditor::_button_pressed).bind(light_2_switch)); + light_2_switch->connect("pressed", callable_mp(this, &MeshEditor::_on_light_2_switch_pressed)); rot_x = 0; rot_y = 0; diff --git a/editor/plugins/mesh_editor_plugin.h b/editor/plugins/mesh_editor_plugin.h index 335244ffd2f..a8ef476f84c 100644 --- a/editor/plugins/mesh_editor_plugin.h +++ b/editor/plugins/mesh_editor_plugin.h @@ -41,7 +41,7 @@ #include "scene/resources/material.h" class SubViewport; -class TextureButton; +class Button; class MeshEditor : public SubViewportContainer { GDCLASS(MeshEditor, SubViewportContainer); @@ -59,17 +59,16 @@ class MeshEditor : public SubViewportContainer { Ref mesh; - TextureButton *light_1_switch = nullptr; - TextureButton *light_2_switch = nullptr; + Button *light_1_switch = nullptr; + Button *light_2_switch = nullptr; struct ThemeCache { - Ref light_1_on; - Ref light_1_off; - Ref light_2_on; - Ref light_2_off; + Ref light_1_icon; + Ref light_2_icon; } theme_cache; - void _button_pressed(Node *p_button); + void _on_light_1_switch_pressed(); + void _on_light_2_switch_pressed(); void _update_rotation(); protected: diff --git a/editor/plugins/style_box_editor_plugin.cpp b/editor/plugins/style_box_editor_plugin.cpp index c126aec0085..6a64d13e8a9 100644 --- a/editor/plugins/style_box_editor_plugin.cpp +++ b/editor/plugins/style_box_editor_plugin.cpp @@ -109,14 +109,11 @@ StyleBoxPreview::StyleBoxPreview() { set_anchors_and_offsets_preset(PRESET_FULL_RECT); grid_preview = memnew(Button); + // This theme variation works better than the normal theme because there's no focus highlight. + grid_preview->set_theme_type_variation("PreviewLightButton"); grid_preview->set_toggle_mode(true); grid_preview->connect("toggled", callable_mp(this, &StyleBoxPreview::_grid_preview_toggled)); grid_preview->set_pressed(grid_preview_enabled); - grid_preview->set_flat(true); - grid_preview->add_theme_style_override("normal", memnew(StyleBoxEmpty)); - grid_preview->add_theme_style_override("hover", memnew(StyleBoxEmpty)); - grid_preview->add_theme_style_override("focus", memnew(StyleBoxEmpty)); - grid_preview->add_theme_style_override("pressed", memnew(StyleBoxEmpty)); add_child(grid_preview); }