Widgets: Add widget theme (WIP)

This commit is contained in:
Jérôme Leclercq
2021-11-28 23:04:56 +01:00
parent 8299a5a4bd
commit f7b69e11c2
16 changed files with 447 additions and 163 deletions

View File

@@ -29,7 +29,7 @@ namespace Nz
* This will also register the widget to the canvas owning the top-most widget.
*/
BaseWidget::BaseWidget(BaseWidget* parent) :
BaseWidget()
BaseWidget(parent->GetTheme())
{
NazaraAssert(parent, "Invalid parent");
NazaraAssert(parent->GetCanvas(), "Parent has no canvas");
@@ -46,6 +46,15 @@ namespace Nz
*/
BaseWidget::~BaseWidget()
{
if (m_registry)
{
for (WidgetEntity& entity : m_entities)
{
if (m_registry->valid(entity.handle))
m_registry->destroy(entity.handle);
}
}
UnregisterFromCanvas();
}

View File

@@ -6,7 +6,9 @@
#include <Nazara/Graphics/BasicMaterial.hpp>
#include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/MaterialPass.hpp>
#include <Nazara/Graphics/SlicedSprite.hpp>
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
#include <Nazara/Utility/AbstractTextDrawer.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <Nazara/Widgets/Canvas.hpp>
#include <Nazara/Widgets/Widgets.hpp>
@@ -15,31 +17,20 @@
namespace Nz
{
ButtonWidget::ButtonWidget(BaseWidget* parent) :
BaseWidget(parent),
m_color { 74, 74, 74 },
m_cornerColor { 180, 180, 180 },
m_hoverColor { 128, 128, 128 },
m_hoverCornerColor { 180, 180, 180 },
m_pressColor { 180, 180, 180 },
m_pressCornerColor { 74, 74, 74 }
BaseWidget(parent)
{
entt::registry& registry = GetRegistry();
UInt32 renderMask = GetCanvas()->GetRenderMask();
m_style = GetTheme()->CreateStyle(this);
m_gradientSprite = std::make_shared<Sprite>(Widgets::Instance()->GetOpaqueMaterial());
m_gradientSprite->SetColor(m_color);
m_gradientSprite->SetCornerColor(RectCorner::LeftBottom, m_cornerColor);
m_gradientSprite->SetCornerColor(RectCorner::RightBottom, m_cornerColor);
Layout();
}
m_gradientEntity = CreateEntity();
registry.emplace<NodeComponent>(m_gradientEntity).SetParent(this);
registry.emplace<GraphicsComponent>(m_gradientEntity).AttachRenderable(m_gradientSprite, renderMask);
void ButtonWidget::UpdateText(const AbstractTextDrawer& drawer)
{
m_style->UpdateText(drawer);
m_textSprite = std::make_shared<TextSprite>(Widgets::Instance()->GetTransparentMaterial());
m_textEntity = CreateEntity();
registry.emplace<NodeComponent>(m_textEntity).SetParent(this);
registry.emplace<GraphicsComponent>(m_textEntity).AttachRenderable(m_textSprite, renderMask);
Vector2f size(drawer.GetBounds().GetLengths());
SetMinimumSize(size);
SetPreferredSize(size + Vector2f(20.f, 10.f));
Layout();
}
@@ -47,49 +38,35 @@ namespace Nz
void ButtonWidget::Layout()
{
BaseWidget::Layout();
Vector2f size = GetSize();
m_gradientSprite->SetSize(size);
entt::registry& registry = GetRegistry();
Boxf textBox = m_textSprite->GetAABB();
registry.get<NodeComponent>(m_textEntity).SetPosition(size.x / 2.f - textBox.width / 2.f, size.y / 2.f - textBox.height / 2.f);
m_style->Layout(GetSize());
}
void ButtonWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Mouse::Button button)
{
if (button == Mouse::Left)
{
m_gradientSprite->SetColor(m_pressColor);
m_gradientSprite->SetCornerColor(RectCorner::LeftBottom, m_pressCornerColor);
m_gradientSprite->SetCornerColor(RectCorner::RightBottom, m_pressCornerColor);
}
m_style->OnPress();
}
void ButtonWidget::OnMouseButtonRelease(int /*x*/, int /*y*/, Mouse::Button button)
void ButtonWidget::OnMouseButtonRelease(int x, int y, Mouse::Button button)
{
if (button == Mouse::Left)
{
m_gradientSprite->SetColor(m_hoverColor);
m_gradientSprite->SetCornerColor(RectCorner::LeftBottom, m_hoverCornerColor);
m_gradientSprite->SetCornerColor(RectCorner::RightBottom, m_hoverCornerColor);
m_style->OnRelease();
OnButtonTrigger(this);
// If user clicks inside button and holds it outside, a release mouse button event will be triggered outside of the widget
// we don't want this to trigger the button, so double-check
if (IsInside(x, y))
OnButtonTrigger(this);
}
}
void ButtonWidget::OnMouseEnter()
{
m_gradientSprite->SetColor(m_hoverColor);
m_gradientSprite->SetCornerColor(RectCorner::LeftBottom, m_hoverCornerColor);
m_gradientSprite->SetCornerColor(RectCorner::RightBottom, m_hoverCornerColor);
m_style->OnHoverBegin();
}
void ButtonWidget::OnMouseExit()
{
m_gradientSprite->SetColor(m_color);
m_gradientSprite->SetCornerColor(RectCorner::LeftBottom, m_cornerColor);
m_gradientSprite->SetCornerColor(RectCorner::RightBottom, m_cornerColor);
m_style->OnHoverEnd();
}
}

View File

@@ -3,11 +3,40 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Widgets/Canvas.hpp>
#include <Nazara/Widgets/DefaultWidgetTheme.hpp>
#include <limits>
#include <Nazara/Widgets/Debug.hpp>
namespace Nz
{
Canvas::Canvas(entt::registry& registry, Nz::EventHandler& eventHandler, Nz::CursorControllerHandle cursorController, UInt32 renderMask) :
BaseWidget(std::make_shared<DefaultWidgetTheme>()),
m_renderMask(renderMask),
m_keyboardOwner(InvalidCanvasIndex),
m_hoveredWidget(InvalidCanvasIndex),
m_mouseOwner(InvalidCanvasIndex),
m_registry(registry),
m_cursorController(cursorController)
{
m_canvas = this;
m_widgetParent = nullptr;
// Register ourselves as a widget to handle cursor change
RegisterToCanvas();
// Connect to every meaningful event
m_keyPressedSlot.Connect(eventHandler.OnKeyPressed, this, &Canvas::OnEventKeyPressed);
m_keyReleasedSlot.Connect(eventHandler.OnKeyReleased, this, &Canvas::OnEventKeyReleased);
m_mouseButtonPressedSlot.Connect(eventHandler.OnMouseButtonPressed, this, &Canvas::OnEventMouseButtonPressed);
m_mouseButtonReleasedSlot.Connect(eventHandler.OnMouseButtonReleased, this, &Canvas::OnEventMouseButtonRelease);
m_mouseEnteredSlot.Connect(eventHandler.OnMouseEntered, this, &Canvas::OnEventMouseEntered);
m_mouseLeftSlot.Connect(eventHandler.OnMouseLeft, this, &Canvas::OnEventMouseLeft);
m_mouseMovedSlot.Connect(eventHandler.OnMouseMoved, this, &Canvas::OnEventMouseMoved);
m_mouseWheelMovedSlot.Connect(eventHandler.OnMouseWheelMoved, this, &Canvas::OnEventMouseWheelMoved);
m_textEnteredSlot.Connect(eventHandler.OnTextEntered, this, &Canvas::OnEventTextEntered);
m_textEditedSlot.Connect(eventHandler.OnTextEdited, this, &Canvas::OnEventTextEdited);
}
std::size_t Canvas::RegisterWidget(BaseWidget* widget)
{
WidgetEntry box;

View File

@@ -0,0 +1,121 @@
// Copyright (C) 2021 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Widgets module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Widgets/DefaultWidgetTheme.hpp>
#include <Nazara/Graphics/BasicMaterial.hpp>
#include <Nazara/Graphics/Material.hpp>
#include <Nazara/Graphics/MaterialPass.hpp>
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <Nazara/Widgets/ButtonWidget.hpp>
#include <Nazara/Widgets/Canvas.hpp>
#include <Nazara/Widgets/Widgets.hpp>
#include <Nazara/Widgets/Debug.hpp>
namespace Nz
{
namespace
{
const UInt8 ButtonImage[] = {
#include <Nazara/Widgets/Resources/DefaultStyle/Button.png.h>
};
const UInt8 ButtonPressedImage[] = {
#include <Nazara/Widgets/Resources/DefaultStyle/ButtonPressed.png.h>
};
}
DefaultWidgetTheme::DefaultWidgetTheme()
{
TextureParams texParams;
texParams.renderDevice = Graphics::Instance()->GetRenderDevice();
texParams.loadFormat = PixelFormat::RGBA8_SRGB;
// Button material
{
std::shared_ptr<MaterialPass> buttonMaterialPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
buttonMaterialPass->EnableDepthBuffer(true);
buttonMaterialPass->EnableDepthWrite(false);
m_buttonMaterial = std::make_shared<Material>();
m_buttonMaterial->AddPass("ForwardPass", buttonMaterialPass);
BasicMaterial buttonBasicMat(*buttonMaterialPass);
buttonBasicMat.SetDiffuseMap(Texture::LoadFromMemory(ButtonImage, sizeof(ButtonImage), texParams));
}
// Button (pressed) material
{
std::shared_ptr<MaterialPass> buttonMaterialPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
buttonMaterialPass->EnableDepthBuffer(true);
buttonMaterialPass->EnableDepthWrite(false);
m_pressedButtonMaterial = std::make_shared<Material>();
m_pressedButtonMaterial->AddPass("ForwardPass", buttonMaterialPass);
BasicMaterial buttonBasicMat(*buttonMaterialPass);
buttonBasicMat.SetDiffuseMap(Texture::LoadFromMemory(ButtonPressedImage, sizeof(ButtonPressedImage), texParams));
}
}
std::unique_ptr<ButtonWidgetStyle> DefaultWidgetTheme::CreateStyle(ButtonWidget* buttonWidget) const
{
return std::make_unique<DefaultButtonWidgetStyle>(buttonWidget, m_buttonMaterial, m_pressedButtonMaterial);
}
DefaultButtonWidgetStyle::DefaultButtonWidgetStyle(ButtonWidget* buttonWidget, std::shared_ptr<Material> defaultMaterial, std::shared_ptr<Material> pressedMaterial) :
ButtonWidgetStyle(buttonWidget),
m_defaultMaterial(std::move(defaultMaterial)),
m_pressedMaterial(std::move(pressedMaterial))
{
auto& registry = GetRegistry();
UInt32 renderMask = GetRenderMask();
m_sprite = std::make_shared<SlicedSprite>(m_defaultMaterial);
m_gradientEntity = CreateEntity();
registry.emplace<NodeComponent>(m_gradientEntity).SetParent(buttonWidget);
registry.emplace<GraphicsComponent>(m_gradientEntity).AttachRenderable(m_sprite, renderMask);
m_textSprite = std::make_shared<TextSprite>(Widgets::Instance()->GetTransparentMaterial());
m_textEntity = CreateEntity();
registry.emplace<NodeComponent>(m_textEntity).SetParent(buttonWidget);
registry.emplace<GraphicsComponent>(m_textEntity).AttachRenderable(m_textSprite, renderMask);
}
void DefaultButtonWidgetStyle::Layout(const Vector2f& size)
{
m_sprite->SetSize(size);
entt::registry& registry = GetRegistry();
Boxf textBox = m_textSprite->GetAABB();
registry.get<NodeComponent>(m_textEntity).SetPosition(size.x / 2.f - textBox.width / 2.f, size.y / 2.f - textBox.height / 2.f);
}
void DefaultButtonWidgetStyle::OnHoverBegin()
{
}
void DefaultButtonWidgetStyle::OnHoverEnd()
{
m_sprite->SetMaterial(m_defaultMaterial);
}
void DefaultButtonWidgetStyle::OnPress()
{
m_sprite->SetMaterial(m_pressedMaterial);
}
void DefaultButtonWidgetStyle::OnRelease()
{
m_sprite->SetMaterial(m_defaultMaterial);
}
void DefaultButtonWidgetStyle::UpdateText(const AbstractTextDrawer& drawer)
{
m_textSprite->Update(drawer);
}
}

View File

@@ -0,0 +1,35 @@
// Copyright (C) 2021 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Widgets module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Widgets/WidgetTheme.hpp>
#include <Nazara/Widgets/Canvas.hpp>
#include <Nazara/Widgets/Debug.hpp>
namespace Nz
{
WidgetTheme::~WidgetTheme() = default;
BaseWidgetStyle::~BaseWidgetStyle() = default;
UInt32 BaseWidgetStyle::GetRenderMask() const
{
return m_widgetOwner->GetCanvas()->GetRenderMask();
}
void ButtonWidgetStyle::OnHoverBegin()
{
}
void ButtonWidgetStyle::OnHoverEnd()
{
}
void ButtonWidgetStyle::OnPress()
{
}
void ButtonWidgetStyle::OnRelease()
{
}
}