From 8013bd5d3b675b13283b382b6b94909daec4cc4f Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 15 Mar 2016 23:02:00 +0100 Subject: [PATCH] Add current work Former-commit-id: 7f1e46e484edbbfd120a7a67ea1c36bee460e517 --- include/Nazara/Utility/RichTextDrawer.hpp | 140 ++++++++++++++++++++++ include/Nazara/Utility/RichTextDrawer.inl | 133 ++++++++++++++++++++ 2 files changed, 273 insertions(+) create mode 100644 include/Nazara/Utility/RichTextDrawer.hpp create mode 100644 include/Nazara/Utility/RichTextDrawer.inl diff --git a/include/Nazara/Utility/RichTextDrawer.hpp b/include/Nazara/Utility/RichTextDrawer.hpp new file mode 100644 index 000000000..65375c13c --- /dev/null +++ b/include/Nazara/Utility/RichTextDrawer.hpp @@ -0,0 +1,140 @@ +// Copyright (C) 2016 Jérôme Leclercq +// 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_RICHTEXTDRAWER_HPP +#define NAZARA_RICHTEXTDRAWER_HPP + +#include +#include +#include +#include +#include +#include + +namespace Nz +{ + class NAZARA_UTILITY_API RichTextDrawer : public AbstractTextDrawer + { + public: + class BlockRef; + + RichTextDrawer(); + RichTextDrawer(const RichTextDrawer& drawer); + RichTextDrawer(RichTextDrawer&& drawer); + virtual ~RichTextDrawer(); + + BlockRef Append(const String& str); + + void Clear(); + + unsigned int GetBlockCharacterSize(std::size_t index) const; + const Color& GetBlockColor(std::size_t index) const; + std::size_t GetBlockCount() const; + const FontRef& GetBlockFont(std::size_t index) const; + UInt32 GetBlockStyle(std::size_t index) const; + const String& GetBlockText(std::size_t index) const; + + unsigned int GetDefaultCharacterSize() const; + const Color& GetDefaultColor() const; + const FontRef& GetDefaultFont() const; + UInt32 GetDefaultStyle() const; + + const Rectui& GetBounds() const override; + Font* GetFont(unsigned int index) const override; + unsigned int GetFontCount() const override; + const Glyph& GetGlyph(unsigned int index) const override; + unsigned int GetGlyphCount() const override; + + void MergeBlocks(); + + void RemoveBlock(std::size_t index); + + void SetBlockCharacterSize(std::size_t index, unsigned int characterSize); + void SetBlockColor(std::size_t index, const Color& color); + void SetBlockFont(std::size_t index, FontRef font); + void SetBlockStyle(std::size_t index, UInt32 style); + void SetBlockText(std::size_t index, const String& str); + + void SetDefaultCharacterSize(unsigned int characterSize); + void SetDefaultColor(const Color& color); + void SetDefaultFont(FontRef font); + void SetDefaultStyle(UInt32 style); + + RichTextDrawer& operator=(const RichTextDrawer& drawer); + RichTextDrawer& operator=(RichTextDrawer&& drawer); + + static RichTextDrawer Draw(const String& str, unsigned int characterSize, UInt32 style = TextStyle_Regular, const Color& color = Color::White); + static RichTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, UInt32 style = TextStyle_Regular, const Color& color = Color::White); + + private: + void ClearGlyphs() const; + void ConnectFontSlots(); + void DisconnectFontSlots(); + void GenerateGlyphs(const String& text) const; + void OnFontAtlasLayerChanged(const Font* font, AbstractImage* oldLayer, AbstractImage* newLayer); + void OnFontInvalidated(const Font* font); + void OnFontRelease(const Font* object); + void UpdateGlyphs() const; + + NazaraSlot(Font, OnFontAtlasChanged, m_atlasChangedSlot); + NazaraSlot(Font, OnFontAtlasLayerChanged, m_atlasLayerChangedSlot); + NazaraSlot(Font, OnFontGlyphCacheCleared, m_glyphCacheClearedSlot); + NazaraSlot(Font, OnFontRelease, m_fontReleaseSlot); + + struct Block + { + Color color; + String text; + UInt32 style; + unsigned int characterSize; + unsigned int fontIndex; + }; + + Color m_defaultColor; + FontRef m_defaultFont; + UInt32 m_defaultStyle; + unsigned int m_defaultCharacterSize; + std::unordered_map m_fonts; + mutable std::vector m_glyphs; + mutable Rectf m_workingBounds; + mutable Rectui m_bounds; + mutable Vector2ui m_drawPos; + mutable bool m_glyphUpdated; + }; + + class RichTextDrawer::BlockRef + { + public: + BlockRef(const BlockRef&) = default; + BlockRef(BlockRef&&) = default; + ~BlockRef() = default; + + inline unsigned int GetCharacterSize() const; + inline Color GetColor() const; + inline const FontRef& GetFont() const; + inline UInt32 GetStyle() const; + inline const String& GetText() const; + + inline void SetCharacterSize(unsigned int size); + inline void SetColor(Color color); + inline void SetFont(FontRef font); + inline void SetStyle(UInt32 style); + inline void SetText(const String& text); + + BlockRef& operator=(const BlockRef&) = default; + BlockRef& operator=(BlockRef&&) = default; + + private: + inline BlockRef(RichTextDrawer& drawer, std::size_t index); + + std::size_t m_blockIndex; + RichTextDrawer& m_drawer; + }; +} + +#include + +#endif // NAZARA_RICHTEXTDRAWER_HPP diff --git a/include/Nazara/Utility/RichTextDrawer.inl b/include/Nazara/Utility/RichTextDrawer.inl new file mode 100644 index 000000000..5af95ec84 --- /dev/null +++ b/include/Nazara/Utility/RichTextDrawer.inl @@ -0,0 +1,133 @@ +// Copyright (C) 2016 Jérôme Leclercq +// This file is part of the "Nazara Engine - Utility module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include + +namespace Nz +{ + /*! + * \class Nz::RichTextDrawer::BlockRef + * \brief Helper class representing a block inside a RichTextDrawer, allowing easier access. + * + * \warning This class is meant for temporary use, moving or destroying the RichTextDrawer or one of its blocks invalidates all BlockRef + */ + + inline RichTextDrawer::BlockRef::BlockRef(RichTextDrawer& drawer, std::size_t index) : + m_blockIndex(index), + m_drawer(drawer) + { + } + + /*! + * Returns the character size used for the characters of the referenced block + * \return The referenced block character size + * + * \see GetColor, GetFont, GetStyle, GetText, SetCharacterSize + */ + inline unsigned int RichTextDrawer::BlockRef::GetCharacterSize() const + { + return m_drawer.GetBlockCharacterSize(m_blockIndex); + } + + /*! + * Returns the color used for the characters of the referenced block + * \return The referenced block color + * + * \see GetCharacterSize, GetFont, GetStyle, GetText, SetColor + */ + inline Color RichTextDrawer::BlockRef::GetColor() const + { + return m_drawer.GetBlockColor(m_blockIndex); + } + + /*! + * Returns the font used for the characters of the referenced block + * \return A reference on the referenced block font + * + * \see GetCharacterSize, GetColor, GetStyle, GetText, SetFont + */ + inline const FontRef& RichTextDrawer::BlockRef::GetFont() const + { + return m_drawer.GetBlockFont(m_blockIndex); + } + + /*! + * Returns the style flags used for the characters of the referenced block + * \return The referenced block style flags (see TextStyleFlags) + * + * \see GetCharacterSize, GetColor, GetFont, GetText, SetStyle + */ + inline UInt32 RichTextDrawer::BlockRef::GetStyle() const + { + return m_drawer.GetBlockStyle(m_blockIndex); + } + + /*! + * Returns the text of the referenced block + * \return The referenced block text + * + * \see GetCharacterSize, GetColor, GetFont, GetStyle, SetText + */ + inline const String& RichTextDrawer::BlockRef::GetText() const + { + return m_drawer.GetBlockText(m_blockIndex); + } + + /*! + * Changes the character size of the referenced block characters + * \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur. + * + * \see GetCharacterSize, SetColor, SetFont, SetStyle, SetText + */ + inline void RichTextDrawer::BlockRef::SetCharacterSize(unsigned int size) + { + m_drawer.SetBlockCharacterSize(m_blockIndex, size); + } + + /*! + * Changes the color of the referenced block characters + * \remark This is the only property that can be changed without forcing a glyph regeneration + * + * \see GetColor, SetCharacterSize, SetFont, SetStyle, SetText + */ + inline void RichTextDrawer::BlockRef::SetColor(Color color) + { + m_drawer.SetBlockColor(m_blockIndex, color); + } + + /*! + * Changes the font of the referenced block characters + * \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur. + * + * \see GetCharacterSize, SetCharacterSize, SetColor, SetStyle, SetText + */ + inline void RichTextDrawer::BlockRef::SetFont(FontRef font) + { + m_drawer.SetBlockFont(m_blockIndex, std::move(font)); + } + + /*! + * Changes the style flags of the referenced block characters + * \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur. + * + * \see GetStyle, SetCharacterSize, SetColor, SetFont, SetText + */ + inline void RichTextDrawer::BlockRef::SetStyle(UInt32 style) + { + m_drawer.SetBlockStyle(m_blockIndex, style); + } + + /*! + * Changes the text of the referenced block + * \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur. + * + * \see GetText, SetCharacterSize, SetColor, SetFont, SetStyle + */ + inline void RichTextDrawer::BlockRef::SetText(const String& text) + { + m_drawer.SetBlockText(m_blockIndex, text); + } +} + +#include