diff --git a/include/Nazara/Utility/SimpleTextDrawer.hpp b/include/Nazara/Utility/SimpleTextDrawer.hpp index 7533cfd23..6404e72e2 100644 --- a/include/Nazara/Utility/SimpleTextDrawer.hpp +++ b/include/Nazara/Utility/SimpleTextDrawer.hpp @@ -19,19 +19,19 @@ namespace Nz class NAZARA_UTILITY_API SimpleTextDrawer : public AbstractTextDrawer { public: - SimpleTextDrawer(); - SimpleTextDrawer(const SimpleTextDrawer& drawer); - SimpleTextDrawer(SimpleTextDrawer&& drawer); - virtual ~SimpleTextDrawer(); + inline SimpleTextDrawer(); + inline SimpleTextDrawer(const SimpleTextDrawer& drawer); + inline SimpleTextDrawer(SimpleTextDrawer&& drawer); + ~SimpleTextDrawer() = default; - void AppendText(const String& str); + inline void AppendText(const String& str); void Clear() override; const Recti& GetBounds() const override; - unsigned int GetCharacterSize() const; - const Color& GetColor() const; - Font* GetFont() const; + inline unsigned int GetCharacterSize() const; + inline const Color& GetColor() const; + inline Font* GetFont() const; Font* GetFont(std::size_t index) const override; std::size_t GetFontCount() const override; const Glyph& GetGlyph(std::size_t index) const override; @@ -39,42 +39,52 @@ namespace Nz const Line& GetLine(std::size_t index) const override; std::size_t GetLineCount() const override; float GetMaxLineWidth() const override; - const Color& GetOutlineColor() const; - float GetOutlineThickness() const; - TextStyleFlags GetStyle() const; - const String& GetText() const; + inline const Color& GetOutlineColor() const; + inline float GetOutlineThickness() const; + inline TextStyleFlags GetStyle() const; + inline const String& GetText() const; - void SetCharacterSize(unsigned int characterSize); - void SetColor(const Color& color); - void SetFont(Font* font); - void SetMaxLineWidth(float lineWidth) override; - void SetOutlineColor(const Color& color); - void SetOutlineThickness(float thickness); - void SetStyle(TextStyleFlags style); - void SetText(const String& str); + inline void SetCharacterSize(unsigned int characterSize); + inline void SetColor(const Color& color); + inline void SetFont(Font* font); + inline void SetLineSpacingFactor(float factor); + inline void SetMaxLineWidth(float lineWidth) override; + inline void SetOutlineColor(const Color& color); + inline void SetOutlineThickness(float thickness); + inline void SetStyle(TextStyleFlags style); + inline void SetText(const String& str); - SimpleTextDrawer& operator=(const SimpleTextDrawer& drawer); - SimpleTextDrawer& operator=(SimpleTextDrawer&& drawer); + inline SimpleTextDrawer& operator=(const SimpleTextDrawer& drawer); + inline SimpleTextDrawer& operator=(SimpleTextDrawer&& drawer); - static SimpleTextDrawer Draw(const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White); - static SimpleTextDrawer Draw(const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color, float outlineThickness, const Color& outlineColor); - static SimpleTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White); - static SimpleTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color, float outlineThickness, const Color& outlineColor); + static inline SimpleTextDrawer Draw(const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White); + static inline SimpleTextDrawer Draw(const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color, float outlineThickness, const Color& outlineColor); + static inline SimpleTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White); + static inline SimpleTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color, float outlineThickness, const Color& outlineColor); private: - void AppendNewLine() const; + inline void AppendNewLine() const; void AppendNewLine(std::size_t glyphIndex, unsigned int glyphPosition) const; + void ClearGlyphs() const; + void ConnectFontSlots(); void DisconnectFontSlots(); + bool GenerateGlyph(Glyph& glyph, char32_t character, float outlineThickness, bool lineWrap, Nz::Color color, int renderOrder, int* advance) const; void GenerateGlyphs(const String& text) const; + + inline void InvalidateColor(); + inline void InvalidateGlyphs(); + void OnFontAtlasLayerChanged(const Font* font, AbstractImage* oldLayer, AbstractImage* newLayer); void OnFontInvalidated(const Font* font); void OnFontRelease(const Font* object); - bool ShouldLineWrap(float size) const; - void UpdateGlyphColor() const; - void UpdateGlyphs() const; + + inline bool ShouldLineWrap(float size) const; + + inline void UpdateGlyphColor() const; + inline void UpdateGlyphs() const; static constexpr std::size_t InvalidGlyph = std::numeric_limits::max(); @@ -104,4 +114,6 @@ namespace Nz }; } +#include + #endif // NAZARA_SIMPLETEXTDRAWER_HPP diff --git a/include/Nazara/Utility/SimpleTextDrawer.inl b/include/Nazara/Utility/SimpleTextDrawer.inl new file mode 100644 index 000000000..13296c0ae --- /dev/null +++ b/include/Nazara/Utility/SimpleTextDrawer.inl @@ -0,0 +1,317 @@ +// Copyright (C) 2017 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 +#include + +namespace Nz +{ + inline SimpleTextDrawer::SimpleTextDrawer() : + m_color(Color::White), + m_outlineColor(Color::Black), + m_style(TextStyle_Regular), + m_colorUpdated(true), + m_glyphUpdated(true), + m_maxLineWidth(std::numeric_limits::infinity()), + m_outlineThickness(0.f), + m_characterSize(24) + { + SetFont(Font::GetDefault()); + } + + inline SimpleTextDrawer::SimpleTextDrawer(const SimpleTextDrawer& drawer) : + m_color(drawer.m_color), + m_text(drawer.m_text), + m_style(drawer.m_style), + m_colorUpdated(false), + m_glyphUpdated(false), + m_outlineColor(drawer.m_outlineColor), + m_maxLineWidth(drawer.m_maxLineWidth), + m_outlineThickness(drawer.m_outlineThickness), + m_characterSize(drawer.m_characterSize) + { + SetFont(drawer.m_font); + } + + inline SimpleTextDrawer::SimpleTextDrawer(SimpleTextDrawer&& drawer) + { + operator=(std::move(drawer)); + } + + inline void SimpleTextDrawer::AppendText(const String& str) + { + m_text.Append(str); + if (m_glyphUpdated) + GenerateGlyphs(str); + } + + inline unsigned int SimpleTextDrawer::GetCharacterSize() const + { + return m_characterSize; + } + + inline const Color& SimpleTextDrawer::GetColor() const + { + return m_color; + } + + inline Font* SimpleTextDrawer::GetFont() const + { + return m_font; + } + + inline const Color& SimpleTextDrawer::GetOutlineColor() const + { + return m_outlineColor; + } + + inline float SimpleTextDrawer::GetOutlineThickness() const + { + return m_outlineThickness; + } + + inline TextStyleFlags SimpleTextDrawer::GetStyle() const + { + return m_style; + } + + inline const String& SimpleTextDrawer::GetText() const + { + return m_text; + } + + inline void SimpleTextDrawer::SetCharacterSize(unsigned int characterSize) + { + if (m_characterSize != characterSize) + { + m_characterSize = characterSize; + + InvalidateGlyphs(); + } + } + + inline void SimpleTextDrawer::SetColor(const Color& color) + { + if (m_color != color) + { + m_color = color; + + InvalidateColor(); + } + } + + inline void SimpleTextDrawer::SetFont(Font* font) + { + if (m_font != font) + { + m_font = font; + + if (m_font) + ConnectFontSlots(); + else + DisconnectFontSlots(); + + InvalidateGlyphs(); + } + } + + inline void SimpleTextDrawer::SetMaxLineWidth(float lineWidth) + { + if (m_maxLineWidth != lineWidth) + { + NazaraAssert(lineWidth > 0.f, "Max line width must be positive"); + + m_maxLineWidth = lineWidth; + + InvalidateGlyphs(); + } + } + + inline void SimpleTextDrawer::SetOutlineColor(const Color& color) + { + if (m_outlineColor != color) + { + m_outlineColor = color; + + InvalidateColor(); + } + } + + inline void SimpleTextDrawer::SetOutlineThickness(float thickness) + { + if (m_outlineThickness != thickness) + { + NazaraAssert(thickness >= 0.f, "Thickness must be zero or positive"); + + m_outlineThickness = thickness; + + InvalidateGlyphs(); + } + } + + inline void SimpleTextDrawer::SetStyle(TextStyleFlags style) + { + if (m_style != style) + { + m_style = style; + + InvalidateGlyphs(); + } + } + + inline void SimpleTextDrawer::SetText(const String& str) + { + if (m_text != str) + { + m_text = str; + + InvalidateGlyphs(); + } + } + + inline SimpleTextDrawer& SimpleTextDrawer::operator=(const SimpleTextDrawer& drawer) + { + m_characterSize = drawer.m_characterSize; + m_color = drawer.m_color; + m_maxLineWidth = drawer.m_maxLineWidth; + m_outlineColor = drawer.m_outlineColor; + m_outlineThickness = drawer.m_outlineThickness; + m_style = drawer.m_style; + m_text = drawer.m_text; + + SetFont(drawer.m_font); + InvalidateGlyphs(); + + return *this; + } + + inline SimpleTextDrawer& SimpleTextDrawer::operator=(SimpleTextDrawer&& drawer) + { + DisconnectFontSlots(); + + m_bounds = std::move(drawer.m_bounds); + m_colorUpdated = std::move(drawer.m_colorUpdated); + m_characterSize = std::move(drawer.m_characterSize); + m_color = std::move(drawer.m_color); + m_glyphs = std::move(drawer.m_glyphs); + m_glyphUpdated = std::move(drawer.m_glyphUpdated); + m_font = std::move(drawer.m_font); + m_maxLineWidth = drawer.m_maxLineWidth; + m_outlineColor = std::move(drawer.m_outlineColor); + m_outlineThickness = std::move(drawer.m_outlineThickness); + m_style = std::move(drawer.m_style); + m_text = std::move(drawer.m_text); + + // Update slot pointers (TODO: Improve the way of doing this) + ConnectFontSlots(); + drawer.DisconnectFontSlots(); + + return *this; + } + + inline SimpleTextDrawer SimpleTextDrawer::Draw(const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color) + { + SimpleTextDrawer drawer; + drawer.SetCharacterSize(characterSize); + drawer.SetColor(color); + drawer.SetStyle(style); + drawer.SetText(str); + + return drawer; + } + + inline SimpleTextDrawer SimpleTextDrawer::Draw(const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color, float outlineThickness, const Color& outlineColor) + { + SimpleTextDrawer drawer; + drawer.SetCharacterSize(characterSize); + drawer.SetColor(color); + drawer.SetOutlineColor(outlineColor); + drawer.SetOutlineThickness(outlineThickness); + drawer.SetStyle(style); + drawer.SetText(str); + + return drawer; + } + + inline SimpleTextDrawer SimpleTextDrawer::Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color) + { + SimpleTextDrawer drawer; + drawer.SetCharacterSize(characterSize); + drawer.SetColor(color); + drawer.SetFont(font); + drawer.SetStyle(style); + drawer.SetText(str); + + return drawer; + } + + inline SimpleTextDrawer SimpleTextDrawer::Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color, float outlineThickness, const Color& outlineColor) + { + SimpleTextDrawer drawer; + drawer.SetCharacterSize(characterSize); + drawer.SetColor(color); + drawer.SetFont(font); + drawer.SetOutlineColor(outlineColor); + drawer.SetOutlineThickness(outlineThickness); + drawer.SetStyle(style); + drawer.SetText(str); + + return drawer; + } + + inline void SimpleTextDrawer::AppendNewLine() const + { + AppendNewLine(InvalidGlyph, 0); + } + + inline void SimpleTextDrawer::InvalidateColor() + { + m_colorUpdated = false; + } + + inline void SimpleTextDrawer::InvalidateGlyphs() + { + m_glyphUpdated = false; + } + + inline bool SimpleTextDrawer::ShouldLineWrap(float size) const + { + if (m_lines.back().glyphIndex > m_glyphs.size()) + return false; + + return m_lines.back().bounds.GetMaximum().x + size > m_maxLineWidth; + } + + inline void SimpleTextDrawer::UpdateGlyphColor() const + { + if (m_outlineThickness > 0.f) + { + for (std::size_t glyphIndex = 0; glyphIndex < m_glyphs.size(); ++glyphIndex) + { + Glyph& glyph = m_glyphs[glyphIndex]; + if (glyphIndex % 2 == 0) + glyph.color = m_outlineColor; + else + glyph.color = m_color; + } + } + else + { + for (Glyph& glyph : m_glyphs) + glyph.color = m_color; + } + + m_colorUpdated = true; + } + + inline void SimpleTextDrawer::UpdateGlyphs() const + { + NazaraAssert(m_font && m_font->IsValid(), "Invalid font"); + + ClearGlyphs(); + GenerateGlyphs(m_text); + } +} + +#include diff --git a/src/Nazara/Utility/SimpleTextDrawer.cpp b/src/Nazara/Utility/SimpleTextDrawer.cpp index 5f5357dbc..6a9fd3999 100644 --- a/src/Nazara/Utility/SimpleTextDrawer.cpp +++ b/src/Nazara/Utility/SimpleTextDrawer.cpp @@ -9,47 +9,6 @@ namespace Nz { - SimpleTextDrawer::SimpleTextDrawer() : - m_color(Color::White), - m_outlineColor(Color::Black), - m_style(TextStyle_Regular), - m_colorUpdated(true), - m_glyphUpdated(true), - m_maxLineWidth(std::numeric_limits::infinity()), - m_outlineThickness(0.f), - m_characterSize(24) - { - SetFont(Font::GetDefault()); - } - - SimpleTextDrawer::SimpleTextDrawer(const SimpleTextDrawer& drawer) : - m_color(drawer.m_color), - m_text(drawer.m_text), - m_style(drawer.m_style), - m_colorUpdated(false), - m_glyphUpdated(false), - m_outlineColor(drawer.m_outlineColor), - m_maxLineWidth(drawer.m_maxLineWidth), - m_outlineThickness(drawer.m_outlineThickness), - m_characterSize(drawer.m_characterSize) - { - SetFont(drawer.m_font); - } - - SimpleTextDrawer::SimpleTextDrawer(SimpleTextDrawer&& drawer) - { - operator=(std::move(drawer)); - } - - SimpleTextDrawer::~SimpleTextDrawer() = default; - - void SimpleTextDrawer::AppendText(const String& str) - { - m_text.Append(str); - if (m_glyphUpdated) - GenerateGlyphs(str); - } - void SimpleTextDrawer::Clear() { m_text.Clear(true); @@ -64,27 +23,12 @@ namespace Nz return m_bounds; } - unsigned int SimpleTextDrawer::GetCharacterSize() const - { - return m_characterSize; - } - - const Color& SimpleTextDrawer::GetColor() const - { - return m_color; - } - - Font* SimpleTextDrawer::GetFont() const - { - return m_font; - } - Font* SimpleTextDrawer::GetFont(std::size_t index) const { NazaraAssert(index == 0, "Font index out of range"); NazaraUnused(index); - return m_font; + return GetFont(); } std::size_t SimpleTextDrawer::GetFontCount() const @@ -132,188 +76,6 @@ namespace Nz return m_maxLineWidth; } - const Color& SimpleTextDrawer::GetOutlineColor() const - { - return m_outlineColor; - } - - float SimpleTextDrawer::GetOutlineThickness() const - { - return m_outlineThickness; - } - - TextStyleFlags SimpleTextDrawer::GetStyle() const - { - return m_style; - } - - const String& SimpleTextDrawer::GetText() const - { - return m_text; - } - - void SimpleTextDrawer::SetCharacterSize(unsigned int characterSize) - { - m_characterSize = characterSize; - - m_glyphUpdated = false; - } - - void SimpleTextDrawer::SetColor(const Color& color) - { - m_color = color; - - m_colorUpdated = false; - } - - void SimpleTextDrawer::SetFont(Font* font) - { - if (m_font != font) - { - m_font = font; - - if (m_font) - ConnectFontSlots(); - else - DisconnectFontSlots(); - - m_glyphUpdated = false; - } - } - - void SimpleTextDrawer::SetMaxLineWidth(float lineWidth) - { - NazaraAssert(m_maxLineWidth > 0.f, "Max line width must be positive"); - - m_maxLineWidth = lineWidth; - - m_glyphUpdated = false; - } - - void SimpleTextDrawer::SetOutlineColor(const Color& color) - { - m_outlineColor = color; - - m_colorUpdated = false; - } - - void SimpleTextDrawer::SetOutlineThickness(float thickness) - { - NazaraAssert(thickness >= 0.f, "Thickness must be zero or positive"); - - m_outlineThickness = thickness; - - m_glyphUpdated = false; - } - - void SimpleTextDrawer::SetStyle(TextStyleFlags style) - { - m_style = style; - - m_glyphUpdated = false; - } - - void SimpleTextDrawer::SetText(const String& str) - { - m_text = str; - - m_glyphUpdated = false; - } - - SimpleTextDrawer& SimpleTextDrawer::operator=(const SimpleTextDrawer& drawer) - { - m_characterSize = drawer.m_characterSize; - m_color = drawer.m_color; - m_outlineColor = drawer.m_outlineColor; - m_outlineThickness = drawer.m_outlineThickness; - m_style = drawer.m_style; - m_text = drawer.m_text; - - m_colorUpdated = false; - m_glyphUpdated = false; - SetFont(drawer.m_font); - - return *this; - } - - SimpleTextDrawer& SimpleTextDrawer::operator=(SimpleTextDrawer&& drawer) - { - DisconnectFontSlots(); - - m_bounds = std::move(drawer.m_bounds); - m_colorUpdated = std::move(drawer.m_colorUpdated); - m_characterSize = std::move(drawer.m_characterSize); - m_color = std::move(drawer.m_color); - m_glyphs = std::move(drawer.m_glyphs); - m_glyphUpdated = std::move(drawer.m_glyphUpdated); - m_font = std::move(drawer.m_font); - m_maxLineWidth = drawer.m_maxLineWidth; - m_outlineColor = std::move(drawer.m_outlineColor); - m_outlineThickness = std::move(drawer.m_outlineThickness); - m_style = std::move(drawer.m_style); - m_text = std::move(drawer.m_text); - - // Update slot pointers (TODO: Improve the way of doing this) - ConnectFontSlots(); - - return *this; - } - - SimpleTextDrawer SimpleTextDrawer::Draw(const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color) - { - SimpleTextDrawer drawer; - drawer.SetCharacterSize(characterSize); - drawer.SetColor(color); - drawer.SetStyle(style); - drawer.SetText(str); - - return drawer; - } - - SimpleTextDrawer SimpleTextDrawer::Draw(const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color, float outlineThickness, const Color& outlineColor) - { - SimpleTextDrawer drawer; - drawer.SetCharacterSize(characterSize); - drawer.SetColor(color); - drawer.SetOutlineColor(outlineColor); - drawer.SetOutlineThickness(outlineThickness); - drawer.SetStyle(style); - drawer.SetText(str); - - return drawer; - } - - SimpleTextDrawer SimpleTextDrawer::Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color) - { - SimpleTextDrawer drawer; - drawer.SetCharacterSize(characterSize); - drawer.SetColor(color); - drawer.SetFont(font); - drawer.SetStyle(style); - drawer.SetText(str); - - return drawer; - } - - SimpleTextDrawer SimpleTextDrawer::Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style, const Color& color, float outlineThickness, const Color& outlineColor) - { - SimpleTextDrawer drawer; - drawer.SetCharacterSize(characterSize); - drawer.SetColor(color); - drawer.SetFont(font); - drawer.SetOutlineColor(outlineColor); - drawer.SetOutlineThickness(outlineThickness); - drawer.SetStyle(style); - drawer.SetText(str); - - return drawer; - } - - void SimpleTextDrawer::AppendNewLine() const - { - AppendNewLine(InvalidGlyph, 0); - } - void SimpleTextDrawer::AppendNewLine(std::size_t glyphIndex, unsigned int glyphPosition) const { // Ensure we're appending from last line @@ -574,7 +336,7 @@ namespace Nz } #endif - m_glyphUpdated = false; + InvalidateGlyphs(); } void SimpleTextDrawer::OnFontRelease(const Font* font) @@ -592,42 +354,4 @@ namespace Nz SetFont(nullptr); } - - bool SimpleTextDrawer::ShouldLineWrap(float size) const - { - if (m_lines.back().glyphIndex > m_glyphs.size()) - return false; - - return m_lines.back().bounds.GetMaximum().x + size > m_maxLineWidth; - } - - void SimpleTextDrawer::UpdateGlyphColor() const - { - if (m_outlineThickness > 0.f) - { - for (std::size_t glyphIndex = 0; glyphIndex < m_glyphs.size(); ++glyphIndex) - { - Glyph& glyph = m_glyphs[glyphIndex]; - if (glyphIndex % 2 == 0) - glyph.color = m_outlineColor; - else - glyph.color = m_color; - } - } - else - { - for (Glyph& glyph : m_glyphs) - glyph.color = m_color; - } - - m_colorUpdated = true; - } - - void SimpleTextDrawer::UpdateGlyphs() const - { - NazaraAssert(m_font && m_font->IsValid(), "Invalid font"); - - ClearGlyphs(); - GenerateGlyphs(m_text); - } }