diff --git a/include/Nazara/Widgets/AbstractLabelWidget.hpp b/include/Nazara/Widgets/AbstractLabelWidget.hpp new file mode 100644 index 000000000..8191940f2 --- /dev/null +++ b/include/Nazara/Widgets/AbstractLabelWidget.hpp @@ -0,0 +1,40 @@ +// Copyright (C) 2024 Jérôme "SirLynix" 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_ABSTRACTLABELWIDGET_HPP +#define NAZARA_WIDGETS_ABSTRACTLABELWIDGET_HPP + +#include +#include + +namespace Nz +{ + class AbstractTextDrawer; + + class NAZARA_WIDGETS_API AbstractLabelWidget : public BaseWidget + { + public: + AbstractLabelWidget(BaseWidget* parent); + AbstractLabelWidget(const AbstractLabelWidget&) = delete; + AbstractLabelWidget(AbstractLabelWidget&&) = delete; + ~AbstractLabelWidget() = default; + + AbstractLabelWidget& operator=(const AbstractLabelWidget&) = delete; + AbstractLabelWidget& operator=(AbstractLabelWidget&&) = delete; + + protected: + std::unique_ptr m_style; + + private: + void OnMouseEnter() override; + void OnMouseExit() override; + void OnRenderLayerUpdated(int baseRenderLayer) override; + }; +} + +#include + +#endif // NAZARA_WIDGETS_ABSTRACTLABELWIDGET_HPP diff --git a/include/Nazara/Widgets/AbstractLabelWidget.inl b/include/Nazara/Widgets/AbstractLabelWidget.inl new file mode 100644 index 000000000..91abd729f --- /dev/null +++ b/include/Nazara/Widgets/AbstractLabelWidget.inl @@ -0,0 +1,11 @@ +// Copyright (C) 2024 Jérôme "SirLynix" 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 + +namespace Nz +{ +} + +#include diff --git a/include/Nazara/Widgets/DefaultWidgetTheme.hpp b/include/Nazara/Widgets/DefaultWidgetTheme.hpp index b1eed5d2c..56e96febb 100644 --- a/include/Nazara/Widgets/DefaultWidgetTheme.hpp +++ b/include/Nazara/Widgets/DefaultWidgetTheme.hpp @@ -25,7 +25,7 @@ namespace Nz std::unique_ptr CreateStyle(ButtonWidget* buttonWidget) const override; std::unique_ptr CreateStyle(CheckboxWidget* checkboxWidget) const override; std::unique_ptr CreateStyle(ImageButtonWidget* imageButtonWidget) const override; - std::unique_ptr CreateStyle(LabelWidget* labelWidget) const override; + std::unique_ptr CreateStyle(AbstractLabelWidget* labelWidget) const override; std::unique_ptr CreateStyle(ProgressBarWidget* progressBarWidget) const override; std::unique_ptr CreateStyle(ScrollAreaWidget* scrollAreaWidget) const override; std::unique_ptr CreateStyle(ScrollbarWidget* scrollbarWidget) const override; diff --git a/include/Nazara/Widgets/LabelWidget.hpp b/include/Nazara/Widgets/LabelWidget.hpp index 5a1e4dd02..90dae57f9 100644 --- a/include/Nazara/Widgets/LabelWidget.hpp +++ b/include/Nazara/Widgets/LabelWidget.hpp @@ -7,19 +7,16 @@ #ifndef NAZARA_WIDGETS_LABELWIDGET_HPP #define NAZARA_WIDGETS_LABELWIDGET_HPP -#include -#include -#include +#include namespace Nz { class AbstractTextDrawer; - class TextSprite; - class NAZARA_WIDGETS_API LabelWidget : public BaseWidget + class NAZARA_WIDGETS_API LabelWidget final : public AbstractLabelWidget { public: - LabelWidget(BaseWidget* parent); + using AbstractLabelWidget::AbstractLabelWidget; LabelWidget(const LabelWidget&) = delete; LabelWidget(LabelWidget&&) = delete; ~LabelWidget() = default; @@ -28,14 +25,6 @@ namespace Nz LabelWidget& operator=(const LabelWidget&) = delete; LabelWidget& operator=(LabelWidget&&) = delete; - - private: - void OnMouseEnter() override; - void OnMouseExit() override; - - void OnRenderLayerUpdated(int baseRenderLayer) override; - - std::unique_ptr m_style; }; } diff --git a/include/Nazara/Widgets/SimpleLabelWidget.hpp b/include/Nazara/Widgets/SimpleLabelWidget.hpp new file mode 100644 index 000000000..016b1f702 --- /dev/null +++ b/include/Nazara/Widgets/SimpleLabelWidget.hpp @@ -0,0 +1,50 @@ +// Copyright (C) 2024 Jérôme "SirLynix" 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_SIMPLELABELWIDGET_HPP +#define NAZARA_WIDGETS_SIMPLELABELWIDGET_HPP + +#include +#include +#include + +namespace Nz +{ + class NAZARA_WIDGETS_API SimpleLabelWidget final : public AbstractLabelWidget + { + public: + using AbstractLabelWidget::AbstractLabelWidget; + SimpleLabelWidget(const SimpleLabelWidget&) = delete; + SimpleLabelWidget(SimpleLabelWidget&&) = delete; + ~SimpleLabelWidget() = default; + + inline void AppendText(std::string_view str); + + inline unsigned int GetCharacterSize() const; + inline const SimpleTextDrawer& GetDrawer() const; + inline const std::string& GetText() const; + inline const Color& GetTextColor() const; + + inline void SetCharacterSize(unsigned int characterSize); + inline void SetText(std::string text); + inline void SetTextColor(const Color& color); + + template void UpdateDrawer(F&& callback); + + SimpleLabelWidget& operator=(const SimpleLabelWidget&) = delete; + SimpleLabelWidget& operator=(SimpleLabelWidget&&) = delete; + + private: + void UpdateText(); + void UpdateTextAndSize(); + + SimpleTextDrawer m_drawer; + }; +} + +#include + +#endif // NAZARA_WIDGETS_SIMPLELABELWIDGET_HPP diff --git a/include/Nazara/Widgets/SimpleLabelWidget.inl b/include/Nazara/Widgets/SimpleLabelWidget.inl new file mode 100644 index 000000000..7b0538d83 --- /dev/null +++ b/include/Nazara/Widgets/SimpleLabelWidget.inl @@ -0,0 +1,70 @@ +// Copyright (C) 2024 Jérôme "SirLynix" 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 + +namespace Nz +{ + inline void SimpleLabelWidget::AppendText(std::string_view str) + { + m_drawer.AppendText(str); + UpdateTextAndSize(); + } + + inline unsigned int SimpleLabelWidget::GetCharacterSize() const + { + return m_drawer.GetCharacterSize(); + } + + inline const SimpleTextDrawer& SimpleLabelWidget::GetDrawer() const + { + return m_drawer; + } + + inline const std::string& SimpleLabelWidget::GetText() const + { + return m_drawer.GetText(); + } + + inline const Color& SimpleLabelWidget::GetTextColor() const + { + return m_drawer.GetTextColor(); + } + + inline void SimpleLabelWidget::SetCharacterSize(unsigned int characterSize) + { + m_drawer.SetCharacterSize(characterSize); + UpdateTextAndSize(); + } + + inline void SimpleLabelWidget::SetText(std::string text) + { + m_drawer.SetText(std::move(text)); + UpdateTextAndSize(); + } + + inline void SimpleLabelWidget::SetTextColor(const Color& color) + { + m_drawer.SetTextColor(color); + UpdateTextAndSize(); + } + + template + void SimpleLabelWidget::UpdateDrawer(F&& callback) + { + using Ret = decltype(callback(m_drawer)); + if constexpr (std::is_void_v) + callback(m_drawer); + else + { + static_assert(std::is_same_v, "callback must either return a boolean or nothing"); + if (!callback(m_drawer)) + return; + } + + UpdateTextAndSize(); + } +} + +#include diff --git a/include/Nazara/Widgets/SimpleWidgetStyles.hpp b/include/Nazara/Widgets/SimpleWidgetStyles.hpp index 626399b4b..0d1d307c0 100644 --- a/include/Nazara/Widgets/SimpleWidgetStyles.hpp +++ b/include/Nazara/Widgets/SimpleWidgetStyles.hpp @@ -154,7 +154,7 @@ namespace Nz class NAZARA_WIDGETS_API SimpleLabelWidgetStyle : public LabelWidgetStyle { public: - SimpleLabelWidgetStyle(LabelWidget* labelWidget, std::shared_ptr material, std::shared_ptr hoveredMaterial = {}); + SimpleLabelWidgetStyle(AbstractLabelWidget* labelWidget, std::shared_ptr material, std::shared_ptr hoveredMaterial = {}); SimpleLabelWidgetStyle(const SimpleLabelWidgetStyle&) = delete; SimpleLabelWidgetStyle(SimpleLabelWidgetStyle&&) = default; ~SimpleLabelWidgetStyle() = default; diff --git a/include/Nazara/Widgets/WidgetTheme.hpp b/include/Nazara/Widgets/WidgetTheme.hpp index 05b3440f0..ba3922b75 100644 --- a/include/Nazara/Widgets/WidgetTheme.hpp +++ b/include/Nazara/Widgets/WidgetTheme.hpp @@ -15,6 +15,7 @@ namespace Nz { + class AbstractLabelWidget; class AbstractTextDrawer; class ButtonWidget; class ButtonWidgetStyle; @@ -22,7 +23,6 @@ namespace Nz class CheckboxWidgetStyle; class ImageButtonWidget; class ImageButtonWidgetStyle; - class LabelWidget; class LabelWidgetStyle; class ProgressBarWidget; class ProgressBarWidgetStyle; @@ -46,7 +46,7 @@ namespace Nz virtual std::unique_ptr CreateStyle(ButtonWidget* buttonWidget) const = 0; virtual std::unique_ptr CreateStyle(CheckboxWidget* checkboxWidget) const = 0; virtual std::unique_ptr CreateStyle(ImageButtonWidget* imageButtonWidget) const = 0; - virtual std::unique_ptr CreateStyle(LabelWidget* labelWidget) const = 0; + virtual std::unique_ptr CreateStyle(AbstractLabelWidget* labelWidget) const = 0; virtual std::unique_ptr CreateStyle(ProgressBarWidget* labelWidget) const = 0; virtual std::unique_ptr CreateStyle(ScrollAreaWidget* scrollareaWidget) const = 0; virtual std::unique_ptr CreateStyle(ScrollbarWidget* scrollbarWidget) const = 0; diff --git a/src/Nazara/Widgets/AbstractLabelWidget.cpp b/src/Nazara/Widgets/AbstractLabelWidget.cpp new file mode 100644 index 000000000..fd6119ca3 --- /dev/null +++ b/src/Nazara/Widgets/AbstractLabelWidget.cpp @@ -0,0 +1,34 @@ +// Copyright (C) 2024 Jérôme "SirLynix" 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 +#include +#include + +namespace Nz +{ + AbstractLabelWidget::AbstractLabelWidget(BaseWidget* parent) : + BaseWidget(parent) + { + m_style = GetTheme()->CreateStyle(this); + SetRenderLayerCount(m_style->GetRenderLayerCount()); + + Layout(); + } + + void AbstractLabelWidget::OnMouseEnter() + { + m_style->OnHoverBegin(); + } + + void AbstractLabelWidget::OnMouseExit() + { + m_style->OnHoverEnd(); + } + + void AbstractLabelWidget::OnRenderLayerUpdated(int baseRenderLayer) + { + m_style->UpdateRenderLayer(baseRenderLayer); + } +} diff --git a/src/Nazara/Widgets/DefaultWidgetTheme.cpp b/src/Nazara/Widgets/DefaultWidgetTheme.cpp index 1ec7f5cf6..2fbc986cb 100644 --- a/src/Nazara/Widgets/DefaultWidgetTheme.cpp +++ b/src/Nazara/Widgets/DefaultWidgetTheme.cpp @@ -218,7 +218,7 @@ namespace Nz return std::make_unique(imageButtonWidget, styleConfig); } - std::unique_ptr DefaultWidgetTheme::CreateStyle(LabelWidget* labelWidget) const + std::unique_ptr DefaultWidgetTheme::CreateStyle(AbstractLabelWidget* labelWidget) const { return std::make_unique(labelWidget, Widgets::Instance()->GetTransparentMaterial()); } diff --git a/src/Nazara/Widgets/LabelWidget.cpp b/src/Nazara/Widgets/LabelWidget.cpp index 509d78345..ec8c0a268 100644 --- a/src/Nazara/Widgets/LabelWidget.cpp +++ b/src/Nazara/Widgets/LabelWidget.cpp @@ -8,15 +8,6 @@ namespace Nz { - LabelWidget::LabelWidget(BaseWidget* parent) : - BaseWidget(parent) - { - m_style = GetTheme()->CreateStyle(this); - SetRenderLayerCount(m_style->GetRenderLayerCount()); - - Layout(); - } - void LabelWidget::UpdateText(const AbstractTextDrawer& drawer) { m_style->UpdateText(drawer); @@ -27,19 +18,4 @@ namespace Nz Layout(); } - - void LabelWidget::OnMouseEnter() - { - m_style->OnHoverBegin(); - } - - void LabelWidget::OnMouseExit() - { - m_style->OnHoverEnd(); - } - - void LabelWidget::OnRenderLayerUpdated(int baseRenderLayer) - { - m_style->UpdateRenderLayer(baseRenderLayer); - } } diff --git a/src/Nazara/Widgets/SimpleLabelWidget.cpp b/src/Nazara/Widgets/SimpleLabelWidget.cpp new file mode 100644 index 000000000..71204b8ca --- /dev/null +++ b/src/Nazara/Widgets/SimpleLabelWidget.cpp @@ -0,0 +1,26 @@ +// Copyright (C) 2024 Jérôme "SirLynix" 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 +#include +#include + +namespace Nz +{ + void SimpleLabelWidget::UpdateText() + { + m_style->UpdateText(m_drawer); + } + + void SimpleLabelWidget::UpdateTextAndSize() + { + m_style->UpdateText(m_drawer); + + Vector2f size(m_drawer.GetBounds().GetLengths()); + SetMinimumSize(size); + SetPreferredSize(size); + + Layout(); + } +} diff --git a/src/Nazara/Widgets/SimpleWidgetStyles.cpp b/src/Nazara/Widgets/SimpleWidgetStyles.cpp index 91015ae3a..b335d5728 100644 --- a/src/Nazara/Widgets/SimpleWidgetStyles.cpp +++ b/src/Nazara/Widgets/SimpleWidgetStyles.cpp @@ -5,11 +5,11 @@ #include #include #include +#include #include #include #include #include -#include #include #include #include @@ -342,7 +342,7 @@ namespace Nz /************************************************************************/ - SimpleLabelWidgetStyle::SimpleLabelWidgetStyle(LabelWidget* labelWidget, std::shared_ptr material, std::shared_ptr hoveredMaterial) : + SimpleLabelWidgetStyle::SimpleLabelWidgetStyle(AbstractLabelWidget* labelWidget, std::shared_ptr material, std::shared_ptr hoveredMaterial) : LabelWidgetStyle(labelWidget, 1), m_hoveredMaterial(std::move(hoveredMaterial)), m_material(std::move(material))