Utility: Add RichTextBuilder

This commit is contained in:
SirLynix 2023-08-24 17:48:00 +02:00
parent f7df4f56b4
commit 4d0565b559
3 changed files with 257 additions and 5 deletions

View File

@ -91,12 +91,15 @@ int main(int argc, char* argv[])
scrollBarWidget->SetPosition(1400.f, 800.f); scrollBarWidget->SetPosition(1400.f, 800.f);
scrollBarWidget->Resize({ 512.f, 256.f }); scrollBarWidget->Resize({ 512.f, 256.f });
/*Nz::TextAreaWidget* textAreaWidget2 = canvas2D.Add<Nz::TextAreaWidget>(); Nz::RichTextAreaWidget* textAreaWidget2 = canvas2D.Add<Nz::RichTextAreaWidget>();
textAreaWidget2->SetPosition(800.f, 700.f); textAreaWidget2->EnableMultiline(true);
textAreaWidget2->SetText("Je suis un autre TextAreaWidget !"); textAreaWidget2->SetPosition(1000.f, 200.f);
textAreaWidget2->Resize(Nz::Vector2f(500.f, textAreaWidget2->GetPreferredHeight()));
textAreaWidget2->SetBackgroundColor(Nz::Color::White()); textAreaWidget2->SetBackgroundColor(Nz::Color::White());
textAreaWidget2->SetTextColor(Nz::Color::Black());*/ textAreaWidget2->SetTextColor(Nz::Color::Black());
Nz::RichTextBuilder builder(textAreaWidget2);
builder << Nz::Color::Blue() << "Rich " << Nz::TextStyle::Bold << "text" << Nz::TextStyle_Regular << builder.CharacterSize(36) << Nz::Color::Black() << "\nAnd a even " << builder.CharacterSize(48) << Nz::Color::Red() << "bigger" << builder.CharacterSize(24) << Nz::Color::Black() << " text";
textAreaWidget2->Resize(Nz::Vector2f(500.f, textAreaWidget2->GetPreferredHeight()));
entt::handle viewer2D = world.CreateEntity(); entt::handle viewer2D = world.CreateEntity();
{ {

View File

@ -0,0 +1,80 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_UTILITY_RICHTEXTBUILDER_HPP
#define NAZARA_UTILITY_RICHTEXTBUILDER_HPP
#include <NazaraUtils/Prerequisites.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/Font.hpp>
#include <memory>
#include <string>
namespace Nz
{
class Font;
template<typename T>
class RichTextBuilder
{
public:
struct CharacterSizeWrapper;
struct CharacterSpacingOffsetWrapper;
struct LineSpacingOffsetWrapper;
struct OutlineColorWrapper;
struct OutlineThicknessWrapper;
RichTextBuilder(T& richText);
RichTextBuilder(T* richText);
RichTextBuilder(const RichTextBuilder&) = delete;
RichTextBuilder(RichTextBuilder&&) = delete;
~RichTextBuilder() = default;
inline RichTextBuilder& AppendText(std::string_view text);
inline RichTextBuilder& SetCharacterSize(unsigned int characterSize);
inline RichTextBuilder& SetCharacterSpacingOffset(float offset);
inline RichTextBuilder& SetLineSpacingOffset(float offset);
inline RichTextBuilder& SetTextColor(const Color& color);
inline RichTextBuilder& SetTextFont(const std::shared_ptr<Font>& font);
inline RichTextBuilder& SetTextOutlineColor(const Color& color);
inline RichTextBuilder& SetTextOutlineThickness(float thickness);
inline RichTextBuilder& SetTextStyle(TextStyleFlags style);
RichTextBuilder& operator=(const RichTextBuilder&) = delete;
RichTextBuilder& operator=(RichTextBuilder&&) = delete;
inline RichTextBuilder& operator<<(const Color& textColor);
inline RichTextBuilder& operator<<(const std::shared_ptr<Font>& font);
inline RichTextBuilder& operator<<(std::string_view str);
inline RichTextBuilder& operator<<(CharacterSizeWrapper characterSize);
inline RichTextBuilder& operator<<(CharacterSpacingOffsetWrapper characterSpacingOffset);
inline RichTextBuilder& operator<<(LineSpacingOffsetWrapper lineSpacing);
inline RichTextBuilder& operator<<(OutlineColorWrapper outlineColor);
inline RichTextBuilder& operator<<(OutlineThicknessWrapper outlineThickness);
inline RichTextBuilder& operator<<(TextStyleFlags textStyle);
static inline CharacterSizeWrapper CharacterSize(unsigned int characterSize);
static inline CharacterSpacingOffsetWrapper CharacterSpacingOffset(unsigned int spacingOffset);
static inline LineSpacingOffsetWrapper LineSpacingOffset(float spacingOffset);
static inline OutlineColorWrapper OutlineColor(const Color& color);
static inline OutlineThicknessWrapper OutlineThickness(float thickness);
struct CharacterSizeWrapper { unsigned int characterSize; };
struct CharacterSpacingOffsetWrapper { float spacingOffset; };
struct LineSpacingOffsetWrapper { float spacingOffset; };
struct OutlineColorWrapper { const Color& color; };
struct OutlineThicknessWrapper { float thickness; };
private:
T& m_richText;
};
}
#include <Nazara/Utility/RichTextBuilder.inl>
#endif // NAZARA_UTILITY_RICHTEXTBUILDER_HPP

View File

@ -0,0 +1,169 @@
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
template<typename T>
RichTextBuilder<T>::RichTextBuilder(T& richText) :
m_richText(richText)
{
}
template<typename T>
RichTextBuilder<T>::RichTextBuilder(T* richText) :
m_richText(*richText)
{
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::AppendText(std::string_view text)
{
m_richText.AppendText(text);
return *this;
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::SetCharacterSize(unsigned int characterSize)
{
m_richText.SetCharacterSize(characterSize);
return *this;
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::SetCharacterSpacingOffset(float offset)
{
m_richText.SetCharacterSpacingOffset(offset);
return *this;
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::SetLineSpacingOffset(float offset)
{
m_richText.SetLineSpacingOffset(offset);
return *this;
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::SetTextColor(const Color& color)
{
m_richText.SetTextColor(color);
return *this;
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::SetTextFont(const std::shared_ptr<Font>& font)
{
m_richText.SetTextFont(font);
return *this;
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::SetTextOutlineColor(const Color& color)
{
m_richText.SetTextOutlineColor(color);
return *this;
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::SetTextOutlineThickness(float thickness)
{
m_richText.SetTextOutlineThickness(thickness);
return *this;
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::SetTextStyle(TextStyleFlags style)
{
m_richText.SetTextStyle(style);
return *this;
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::operator<<(const Color& textColor)
{
return SetTextColor(textColor);
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::operator<<(const std::shared_ptr<Font>& font)
{
return SetTextFont(font);
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::operator<<(std::string_view str)
{
return AppendText(str);
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::operator<<(CharacterSizeWrapper characterSize)
{
return SetCharacterSize(characterSize.characterSize);
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::operator<<(CharacterSpacingOffsetWrapper characterSpacingOffset)
{
return SetCharacterSpacingOffset(characterSpacingOffset.spacingOffset);
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::operator<<(LineSpacingOffsetWrapper lineSpacing)
{
return SetLineSpacingOffset(lineSpacing.spacingOffset);
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::operator<<(OutlineColorWrapper outlineColor)
{
return SetTextOutlineColor(outlineColor.color);
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::operator<<(OutlineThicknessWrapper outlineThickness)
{
return SetTextOutlineThickness(outlineThickness.thickness);
}
template<typename T>
RichTextBuilder<T>& RichTextBuilder<T>::operator<<(TextStyleFlags textStyle)
{
return SetTextStyle(textStyle);
}
template<typename T>
auto RichTextBuilder<T>::CharacterSize(unsigned int characterSize) -> CharacterSizeWrapper
{
return { characterSize };
}
template<typename T>
auto RichTextBuilder<T>::CharacterSpacingOffset(unsigned int spacingOffset) -> CharacterSpacingOffsetWrapper
{
return { spacingOffset };
}
template<typename T>
auto RichTextBuilder<T>::LineSpacingOffset(float spacingOffset) -> LineSpacingOffsetWrapper
{
return { spacingOffset };
}
template<typename T>
auto RichTextBuilder<T>::OutlineColor(const Color& color) -> OutlineColorWrapper
{
return { color };
}
template<typename T>
auto RichTextBuilder<T>::OutlineThickness(float thickness) -> OutlineThicknessWrapper
{
return { thickness };
}
}
#include <Nazara/Utility/DebugOff.hpp>