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

Improve material and mesh preview buttons #78858

Merged
merged 1 commit into from
Aug 7, 2023
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
37 changes: 31 additions & 6 deletions editor/editor_themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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.
Expand Down Expand Up @@ -941,6 +947,25 @@ Ref<Theme> create_editor_theme(const Ref<Theme> 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<StyleBox> 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");
Expand Down
2 changes: 1 addition & 1 deletion editor/icons/MaterialPreviewCube.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion editor/icons/MaterialPreviewCubeOff.svg

This file was deleted.

2 changes: 1 addition & 1 deletion editor/icons/MaterialPreviewLight1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion editor/icons/MaterialPreviewLight1Off.svg

This file was deleted.

2 changes: 1 addition & 1 deletion editor/icons/MaterialPreviewLight2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion editor/icons/MaterialPreviewLight2Off.svg

This file was deleted.

2 changes: 1 addition & 1 deletion editor/icons/MaterialPreviewSphere.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion editor/icons/MaterialPreviewSphereOff.svg

This file was deleted.

94 changes: 45 additions & 49 deletions editor/plugins/material_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -63,31 +63,23 @@ void MaterialEditor::gui_input(const Ref<InputEvent> &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"));
}

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);

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);
light_1_switch->set_icon(theme_cache.light_1_icon);
light_2_switch->set_icon(theme_cache.light_2_icon);

sphere_switch->set_icon(theme_cache.sphere_icon);
box_switch->set_icon(theme_cache.box_icon);
} break;

case NOTIFICATION_DRAW: {
Expand Down Expand Up @@ -135,34 +127,32 @@ void MaterialEditor::edit(Ref<Material> p_material, const Ref<Environment> &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);
Expand All @@ -185,7 +175,7 @@ MaterialEditor::MaterialEditor() {

layout_2d->set_visible(false);

// spatial
// Spatial

vc = memnew(SubViewportContainer);
vc->set_stretch(true);
Expand Down Expand Up @@ -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();
Expand Down
30 changes: 14 additions & 16 deletions editor/plugins/material_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class HBoxContainer;
class MeshInstance3D;
class SubViewport;
class SubViewportContainer;
class TextureButton;
class Button;

class MaterialEditor : public Control {
GDCLASS(MaterialEditor, Control);
Expand All @@ -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> material;

Button *sphere_switch = nullptr;
Button *box_switch = nullptr;
Button *light_1_switch = nullptr;
Button *light_2_switch = nullptr;

struct ThemeCache {
Ref<Texture2D> light_1_on;
Ref<Texture2D> light_1_off;
Ref<Texture2D> light_2_on;
Ref<Texture2D> light_2_off;
Ref<Texture2D> sphere_on;
Ref<Texture2D> sphere_off;
Ref<Texture2D> box_on;
Ref<Texture2D> box_off;
Ref<Texture2D> light_1_icon;
Ref<Texture2D> light_2_icon;
Ref<Texture2D> sphere_icon;
Ref<Texture2D> box_icon;
Ref<Texture2D> 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;
Expand Down
47 changes: 21 additions & 26 deletions editor/plugins/mesh_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputEvent> &p_event) {
Expand All @@ -42,31 +42,24 @@ void MeshEditor::gui_input(const Ref<InputEvent> &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();
}
}

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;
}
}
Expand Down Expand Up @@ -100,21 +93,19 @@ void MeshEditor::edit(Ref<Mesh> 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<World3D> 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);
Expand Down Expand Up @@ -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;
Expand Down
Loading
Loading