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

@@ -19,10 +19,13 @@
namespace Nz
{
class BaseWidgetStyle;
class Canvas;
class WidgetTheme;
class NAZARA_WIDGETS_API BaseWidget : public Nz::Node
class NAZARA_WIDGETS_API BaseWidget : public Node
{
friend BaseWidgetStyle;
friend Canvas;
public:
@@ -70,6 +73,7 @@ namespace Nz
inline const Rectf& GetRenderingRect() const;
inline Vector2f GetSize() const;
const std::shared_ptr<WidgetTheme>& GetTheme() const;
inline float GetWidth() const;
inline std::size_t GetWidgetChildCount() const;
@@ -118,6 +122,7 @@ namespace Nz
Rectf GetScissorRect() const;
virtual bool IsFocusable() const;
inline bool IsInside(float x, float y) const;
virtual void OnFocusLost();
virtual void OnFocusReceived();
virtual bool OnKeyPressed(const WindowEvent::KeyEvent& key);
@@ -137,7 +142,7 @@ namespace Nz
virtual void ShowChildren(bool show);
private:
inline BaseWidget();
inline BaseWidget(std::shared_ptr<WidgetTheme> theme);
void DestroyChild(BaseWidget* widget);
void DestroyChildren();
@@ -158,6 +163,7 @@ namespace Nz
std::optional<entt::entity> m_backgroundEntity;
std::size_t m_canvasIndex;
std::shared_ptr<Sprite> m_backgroundSprite;
std::shared_ptr<WidgetTheme> m_theme;
std::vector<WidgetEntity> m_entities;
std::vector<std::unique_ptr<BaseWidget>> m_children;
entt::registry* m_registry;

View File

@@ -10,8 +10,9 @@
namespace Nz
{
inline BaseWidget::BaseWidget() :
inline BaseWidget::BaseWidget(std::shared_ptr<WidgetTheme> theme) :
m_canvasIndex(InvalidCanvasIndex),
m_theme(std::move(theme)),
m_registry(nullptr),
m_canvas(nullptr),
m_backgroundColor(Color(230, 230, 230, 255)),
@@ -164,6 +165,11 @@ namespace Nz
return Vector2f(GetWidth(), GetHeight());
}
inline const std::shared_ptr<WidgetTheme>& BaseWidget::GetTheme() const
{
return m_theme;
}
inline float BaseWidget::GetWidth() const
{
return m_size.x;
@@ -179,6 +185,12 @@ namespace Nz
return Show(false);
}
inline bool BaseWidget::IsInside(float x, float y) const
{
Rectf rect(0.f, 0.f, m_size.x, m_size.y);
return rect.Contains(x, y);
}
inline bool BaseWidget::IsVisible() const
{
return m_visible;

View File

@@ -8,9 +8,10 @@
#define NAZARA_WIDGETS_BUTTONWIDGET_HPP
#include <Nazara/Core/Color.hpp>
#include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Graphics/SlicedSprite.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
#include <Nazara/Widgets/BaseWidget.hpp>
#include <Nazara/Widgets/WidgetTheme.hpp>
namespace Nz
{
@@ -25,18 +26,7 @@ namespace Nz
ButtonWidget(ButtonWidget&&) = default;
~ButtonWidget() = default;
inline const Color& GetColor() const;
inline const Color& GetCornerColor() const;
inline const Color& GetHoverColor() const;
inline const Color& GetHoverCornerColor() const;
inline const Color& GetPressColor() const;
inline const Color& GetPressCornerColor() const;
inline void SetColor(const Color& color, const Color& cornerColor);
inline void SetHoverColor(const Color& color, const Color& cornerColor);
inline void SetPressColor(const Color& color, const Color& cornerColor);
inline void UpdateText(const AbstractTextDrawer& drawer);
void UpdateText(const AbstractTextDrawer& drawer);
ButtonWidget& operator=(const ButtonWidget&) = delete;
ButtonWidget& operator=(ButtonWidget&&) = default;
@@ -51,17 +41,7 @@ namespace Nz
void OnMouseButtonRelease(int x, int y, Mouse::Button button) override;
void OnMouseExit() override;
std::shared_ptr<MaterialPass> m_gradientMaterialPass;
std::shared_ptr<Sprite> m_gradientSprite;
std::shared_ptr<TextSprite> m_textSprite;
entt::entity m_textEntity;
entt::entity m_gradientEntity;
Color m_color;
Color m_cornerColor;
Color m_hoverColor;
Color m_hoverCornerColor;
Color m_pressColor;
Color m_pressCornerColor;
std::unique_ptr<ButtonWidgetStyle> m_style;
};
}

View File

@@ -7,68 +7,6 @@
namespace Nz
{
inline const Color& ButtonWidget::GetColor() const
{
return m_color;
}
inline const Color& ButtonWidget::GetCornerColor() const
{
return m_cornerColor;
}
inline const Color& ButtonWidget::GetHoverColor() const
{
return m_hoverColor;
}
inline const Color& ButtonWidget::GetHoverCornerColor() const
{
return m_hoverCornerColor;
}
inline const Color& ButtonWidget::GetPressColor() const
{
return m_pressColor;
}
inline const Color& ButtonWidget::GetPressCornerColor() const
{
return m_pressCornerColor;
}
inline void ButtonWidget::SetColor(const Color& color, const Color& cornerColor)
{
m_color = color;
m_cornerColor = cornerColor;
m_gradientSprite->SetColor(m_color);
m_gradientSprite->SetCornerColor(RectCorner::LeftBottom, m_cornerColor);
m_gradientSprite->SetCornerColor(RectCorner::RightBottom, m_cornerColor);
}
inline void ButtonWidget::SetHoverColor(const Color& color, const Color& cornerColor)
{
m_hoverColor = color;
m_hoverCornerColor = cornerColor;
}
inline void ButtonWidget::SetPressColor(const Color& color, const Color& cornerColor)
{
m_pressColor = color;
m_pressCornerColor = cornerColor;
}
inline void ButtonWidget::UpdateText(const AbstractTextDrawer& drawer)
{
m_textSprite->Update(drawer);
Vector2f textSize = Vector2f(m_textSprite->GetAABB().GetLengths());
SetMinimumSize(textSize);
SetPreferredSize(textSize + Vector2f(20.f, 10.f));
Layout();
}
}
#include <Nazara/Widgets/DebugOff.hpp>

View File

@@ -20,7 +20,7 @@ namespace Nz
friend BaseWidget;
public:
inline Canvas(entt::registry& registry, EventHandler& eventHandler, CursorControllerHandle cursorController, UInt32 renderMask);
Canvas(entt::registry& registry, EventHandler& eventHandler, CursorControllerHandle cursorController, UInt32 renderMask);
Canvas(const Canvas&) = delete;
Canvas(Canvas&&) = delete;
inline ~Canvas();

View File

@@ -8,33 +8,6 @@
namespace Nz
{
inline Canvas::Canvas(entt::registry& registry, Nz::EventHandler& eventHandler, Nz::CursorControllerHandle cursorController, UInt32 renderMask) :
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);
}
inline Canvas::~Canvas()
{
// Destroy children explicitly because they signal us when getting destroyed, and that can't happen after our own destruction

View File

@@ -0,0 +1,69 @@
// 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
#pragma once
#ifndef NAZARA_WIDGETS_DEFAULTWIDGETTHEME_HPP
#define NAZARA_WIDGETS_DEFAULTWIDGETTHEME_HPP
#include <Nazara/Graphics/SlicedSprite.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
#include <Nazara/Widgets/WidgetTheme.hpp>
#include <entt/entt.hpp>
namespace Nz
{
class DefaultDefaultButtonWidgetStyle;
class NAZARA_WIDGETS_API DefaultWidgetTheme : public WidgetTheme
{
public:
DefaultWidgetTheme();
DefaultWidgetTheme(const DefaultWidgetTheme&) = delete;
DefaultWidgetTheme(DefaultWidgetTheme&&) = default;
~DefaultWidgetTheme() = default;
std::unique_ptr<ButtonWidgetStyle> CreateStyle(ButtonWidget* buttonWidget) const override;
DefaultWidgetTheme& operator=(const DefaultWidgetTheme&) = delete;
DefaultWidgetTheme& operator=(DefaultWidgetTheme&&) = default;
private:
std::shared_ptr<Material> m_buttonMaterial;
std::shared_ptr<Material> m_pressedButtonMaterial;
};
class NAZARA_WIDGETS_API DefaultButtonWidgetStyle : public ButtonWidgetStyle
{
public:
DefaultButtonWidgetStyle(ButtonWidget* buttonWidget, std::shared_ptr<Material> defaultMaterial, std::shared_ptr<Material> pressedMaterial);
DefaultButtonWidgetStyle(const DefaultButtonWidgetStyle&) = delete;
DefaultButtonWidgetStyle(DefaultButtonWidgetStyle&&) = default;
~DefaultButtonWidgetStyle() = default;
void Layout(const Vector2f& size) override;
void OnHoverBegin() override;
void OnHoverEnd() override;
void OnPress() override;
void OnRelease() override;
void UpdateText(const AbstractTextDrawer& drawer) override;
DefaultButtonWidgetStyle& operator=(const DefaultButtonWidgetStyle&) = delete;
DefaultButtonWidgetStyle& operator=(DefaultButtonWidgetStyle&&) = default;
private:
std::shared_ptr<Material> m_defaultMaterial;
std::shared_ptr<Material> m_pressedMaterial;
std::shared_ptr<SlicedSprite> m_sprite;
std::shared_ptr<TextSprite> m_textSprite;
entt::entity m_textEntity;
entt::entity m_gradientEntity;
};
}
#include <Nazara/Widgets/DefaultWidgetTheme.inl>
#endif // NAZARA_WIDGETS_DEFAULTWIDGETTHEME_HPP

View File

@@ -0,0 +1,13 @@
// 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/Widgets/WidgetTheme.hpp>
#include <Nazara/Widgets/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Widgets/DebugOff.hpp>

View File

@@ -0,0 +1,84 @@
// 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
#pragma once
#ifndef NAZARA_WIDGETS_WIDGETTHEME_HPP
#define NAZARA_WIDGETS_WIDGETTHEME_HPP
#include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Widgets/BaseWidget.hpp>
namespace Nz
{
class AbstractTextDrawer;
class ButtonWidget;
class ButtonWidgetStyle;
class NAZARA_WIDGETS_API WidgetTheme
{
public:
WidgetTheme() = default;
WidgetTheme(const WidgetTheme&) = delete;
WidgetTheme(WidgetTheme&&) = default;
virtual ~WidgetTheme();
virtual std::unique_ptr<ButtonWidgetStyle> CreateStyle(ButtonWidget* buttonWidget) const = 0;
WidgetTheme& operator=(const WidgetTheme&) = delete;
WidgetTheme& operator=(WidgetTheme&&) = default;
private:
};
class NAZARA_WIDGETS_API BaseWidgetStyle
{
public:
inline BaseWidgetStyle(BaseWidget* widget);
BaseWidgetStyle(const BaseWidgetStyle&) = delete;
BaseWidgetStyle(BaseWidgetStyle&&) = default;
virtual ~BaseWidgetStyle();
inline entt::entity CreateEntity();
inline void DestroyEntity(entt::entity entity);
inline entt::registry& GetRegistry();
inline const entt::registry& GetRegistry() const;
UInt32 GetRenderMask() const;
BaseWidgetStyle& operator=(const BaseWidgetStyle&) = delete;
BaseWidgetStyle& operator=(BaseWidgetStyle&&) = default;
private:
BaseWidget* m_widgetOwner;
};
class NAZARA_WIDGETS_API ButtonWidgetStyle : public BaseWidgetStyle
{
public:
using BaseWidgetStyle::BaseWidgetStyle;
ButtonWidgetStyle(const ButtonWidgetStyle&) = delete;
ButtonWidgetStyle(ButtonWidgetStyle&&) = default;
~ButtonWidgetStyle() = default;
virtual void Layout(const Vector2f& size) = 0;
virtual void OnHoverBegin();
virtual void OnHoverEnd();
virtual void OnPress();
virtual void OnRelease();
virtual void UpdateText(const AbstractTextDrawer& drawer) = 0;
ButtonWidgetStyle& operator=(const ButtonWidgetStyle&) = delete;
ButtonWidgetStyle& operator=(ButtonWidgetStyle&&) = default;
};
}
#include <Nazara/Widgets/WidgetTheme.inl>
#endif // NAZARA_WIDGETS_WIDGETTHEME_HPP

View File

@@ -0,0 +1,36 @@
// 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/Debug.hpp>
namespace Nz
{
inline BaseWidgetStyle::BaseWidgetStyle(BaseWidget* widget) :
m_widgetOwner(widget)
{
}
inline entt::entity BaseWidgetStyle::CreateEntity()
{
return m_widgetOwner->CreateEntity();
}
inline void BaseWidgetStyle::DestroyEntity(entt::entity entity)
{
m_widgetOwner->DestroyEntity(entity);
}
inline entt::registry& BaseWidgetStyle::GetRegistry()
{
return m_widgetOwner->GetRegistry();
}
inline const entt::registry& BaseWidgetStyle::GetRegistry() const
{
return m_widgetOwner->GetRegistry();
}
}
#include <Nazara/Widgets/DebugOff.hpp>