Widgets: Add CheckboxWidget

This commit is contained in:
Jérôme Leclercq
2021-12-01 10:47:21 +01:00
parent 26d6448076
commit a4c0cc8c34
10 changed files with 395 additions and 2 deletions

View File

@@ -0,0 +1,59 @@
// 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_CHECKBOXWIDGET_HPP
#define NAZARA_WIDGETS_CHECKBOXWIDGET_HPP
#include <Nazara/Core/Color.hpp>
#include <Nazara/Graphics/SlicedSprite.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
#include <Nazara/Widgets/BaseWidget.hpp>
#include <Nazara/Widgets/Enums.hpp>
#include <Nazara/Widgets/WidgetTheme.hpp>
namespace Nz
{
class MaterialPass;
class NAZARA_WIDGETS_API CheckboxWidget : public BaseWidget
{
public:
CheckboxWidget(BaseWidget* parent);
CheckboxWidget(const CheckboxWidget&) = delete;
CheckboxWidget(CheckboxWidget&&) = default;
~CheckboxWidget() = default;
inline void EnableTristate(bool enabled);
inline bool IsTristateEnabled() const;
inline void SetState(bool checkboxState);
void SetState(CheckboxState checkboxState);
inline void SwitchToNextState();
CheckboxWidget& operator=(const CheckboxWidget&) = delete;
CheckboxWidget& operator=(CheckboxWidget&&) = default;
NazaraSignal(OnCheckboxStateUpdate, const CheckboxWidget* /*button*/, CheckboxState /*newState*/);
private:
void Layout() override;
void OnMouseEnter() override;
void OnMouseButtonPress(int x, int y, Mouse::Button button) override;
void OnMouseButtonRelease(int x, int y, Mouse::Button button) override;
void OnMouseExit() override;
std::unique_ptr<CheckboxWidgetStyle> m_style;
CheckboxState m_state;
bool m_isTristateEnabled;
};
}
#include <Nazara/Widgets/CheckboxWidget.inl>
#endif // NAZARA_WIDGETS_CHECKBOXWIDGET_HPP

View File

@@ -0,0 +1,46 @@
// 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/CheckboxWidget.hpp>
#include <Nazara/Widgets/Debug.hpp>
namespace Nz
{
inline void CheckboxWidget::EnableTristate(bool enabled)
{
m_isTristateEnabled = enabled;
}
inline bool CheckboxWidget::IsTristateEnabled() const
{
return m_isTristateEnabled;
}
void CheckboxWidget::SetState(bool checkboxState)
{
return SetState((checkboxState) ? CheckboxState::Checked : CheckboxState::Unchecked);
}
inline void CheckboxWidget::SwitchToNextState()
{
switch (m_state)
{
case CheckboxState::Checked:
SetState(CheckboxState::Unchecked);
break;
case CheckboxState::Unchecked:
{
SetState((m_isTristateEnabled) ? CheckboxState::Tristate : CheckboxState::Checked);
break;
}
case CheckboxState::Tristate:
SetState(CheckboxState::Checked);
break;
}
}
}
#include <Nazara/Widgets/DebugOff.hpp>

View File

@@ -23,6 +23,7 @@ namespace Nz
~DefaultWidgetTheme() = default;
std::unique_ptr<ButtonWidgetStyle> CreateStyle(ButtonWidget* buttonWidget) const override;
std::unique_ptr<CheckboxWidgetStyle> CreateStyle(CheckboxWidget* buttonWidget) const override;
std::unique_ptr<LabelWidgetStyle> CreateStyle(LabelWidget* buttonWidget) const override;
DefaultWidgetTheme& operator=(const DefaultWidgetTheme&) = delete;
@@ -33,6 +34,10 @@ namespace Nz
std::shared_ptr<Material> m_buttonHoveredMaterial;
std::shared_ptr<Material> m_buttonPressedHoveredMaterial;
std::shared_ptr<Material> m_buttonPressedMaterial;
std::shared_ptr<Material> m_checkboxBackgroundMaterial;
std::shared_ptr<Material> m_checkboxBackgroundHoveredMaterial;
std::shared_ptr<Material> m_checkboxCheckMaterial;
std::shared_ptr<Material> m_checkboxTristateMaterial;
};
}

View File

@@ -61,6 +61,50 @@ namespace Nz
bool m_isHovered;
bool m_isPressed;
};
class NAZARA_WIDGETS_API SimpleCheckboxWidgetStyle : public CheckboxWidgetStyle
{
public:
struct StyleConfig;
SimpleCheckboxWidgetStyle(CheckboxWidget* checkboxWidget, StyleConfig config);
SimpleCheckboxWidgetStyle(const SimpleCheckboxWidgetStyle&) = delete;
SimpleCheckboxWidgetStyle(SimpleCheckboxWidgetStyle&&) = default;
~SimpleCheckboxWidgetStyle() = default;
void Layout(const Vector2f& size) override;
void OnHoverBegin() override;
void OnHoverEnd() override;
void OnNewState(CheckboxState newState) override;
SimpleCheckboxWidgetStyle& operator=(const SimpleCheckboxWidgetStyle&) = delete;
SimpleCheckboxWidgetStyle& operator=(SimpleCheckboxWidgetStyle&&) = default;
struct StyleConfig
{
std::shared_ptr<Material> backgroundMaterial;
std::shared_ptr<Material> backgroundHoveredMaterial;
std::shared_ptr<Material> checkMaterial;
std::shared_ptr<Material> tristateMaterial;
float backgroundCornerSize;
float backgroundCornerTexCoords;
};
protected:
virtual void UpdateMaterial(bool hovered);
private:
std::shared_ptr<Material> m_checkMaterial;
std::shared_ptr<Material> m_hoveredMaterial;
std::shared_ptr<Material> m_material;
std::shared_ptr<Material> m_tristateMaterial;
std::shared_ptr<Sprite> m_checkSprite;
std::shared_ptr<SlicedSprite> m_backgroundSprite;
entt::entity m_backgroundEntity;
entt::entity m_checkEntity;
bool m_isHovered;
};
class NAZARA_WIDGETS_API SimpleLabelWidgetStyle : public LabelWidgetStyle
{

View File

@@ -11,12 +11,15 @@
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Widgets/BaseWidget.hpp>
#include <Nazara/Widgets/Enums.hpp>
namespace Nz
{
class AbstractTextDrawer;
class ButtonWidget;
class ButtonWidgetStyle;
class CheckboxWidget;
class CheckboxWidgetStyle;
class LabelWidget;
class LabelWidgetStyle;
@@ -29,6 +32,7 @@ namespace Nz
virtual ~WidgetTheme();
virtual std::unique_ptr<ButtonWidgetStyle> CreateStyle(ButtonWidget* buttonWidget) const = 0;
virtual std::unique_ptr<CheckboxWidgetStyle> CreateStyle(CheckboxWidget* buttonWidget) const = 0;
virtual std::unique_ptr<LabelWidgetStyle> CreateStyle(LabelWidget* buttonWidget) const = 0;
WidgetTheme& operator=(const WidgetTheme&) = delete;
@@ -81,6 +85,26 @@ namespace Nz
ButtonWidgetStyle& operator=(ButtonWidgetStyle&&) = default;
};
class NAZARA_WIDGETS_API CheckboxWidgetStyle : public BaseWidgetStyle
{
public:
using BaseWidgetStyle::BaseWidgetStyle;
CheckboxWidgetStyle(const CheckboxWidgetStyle&) = delete;
CheckboxWidgetStyle(CheckboxWidgetStyle&&) = default;
~CheckboxWidgetStyle() = default;
virtual void Layout(const Vector2f& size) = 0;
virtual void OnHoverBegin();
virtual void OnHoverEnd();
virtual void OnNewState(CheckboxState newState);
virtual void OnPress();
virtual void OnRelease();
CheckboxWidgetStyle& operator=(const CheckboxWidgetStyle&) = delete;
CheckboxWidgetStyle& operator=(CheckboxWidgetStyle&&) = default;
};
class NAZARA_WIDGETS_API LabelWidgetStyle : public BaseWidgetStyle
{
public: