From 1d86d90ca0cb27ca72b9f398280016979b7b531a Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 26 Jan 2020 17:06:39 +0100 Subject: [PATCH] SDK/(Rich)TextAreaWidget: Add character and line spacing offsets --- ChangeLog.md | 1 + .../NDK/Widgets/RichTextAreaWidget.hpp | 6 ++- .../NDK/Widgets/RichTextAreaWidget.inl | 20 ++++++++++ SDK/include/NDK/Widgets/TextAreaWidget.hpp | 7 +++- SDK/include/NDK/Widgets/TextAreaWidget.inl | 33 ++++++++++++++++ SDK/src/NDK/Widgets/TextAreaWidget.cpp | 38 +++++++++---------- 6 files changed, 83 insertions(+), 22 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index c4562182f..a60af1b03 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -321,6 +321,7 @@ Nazara Development Kit: - Fixed crash when pressing up/down key with no history in the console - (Rich)TextAreaWidget text style is now alterable - Added CameraComponent::SetProjectionScale +- Added (Rich)TextAreaWidget character and line spacing offset properties # 0.4: diff --git a/SDK/include/NDK/Widgets/RichTextAreaWidget.hpp b/SDK/include/NDK/Widgets/RichTextAreaWidget.hpp index 280ed9651..a21a233d2 100644 --- a/SDK/include/NDK/Widgets/RichTextAreaWidget.hpp +++ b/SDK/include/NDK/Widgets/RichTextAreaWidget.hpp @@ -27,6 +27,8 @@ namespace Ndk void Erase(std::size_t firstGlyph, std::size_t lastGlyph) override; inline unsigned int GetCharacterSize() const; + inline float GetCharacterSpacingOffset() const; + inline float GetLineSpacingOffset() const; inline const Nz::Color& GetTextColor() const; inline Nz::Font* GetTextFont() const; inline const Nz::Color& GetTextOutlineColor() const; @@ -34,6 +36,8 @@ namespace Ndk inline Nz::TextStyleFlags GetTextStyle() const; inline void SetCharacterSize(unsigned int characterSize); + inline void SetCharacterSpacingOffset(float offset); + inline void SetLineSpacingOffset(float offset); inline void SetTextColor(const Nz::Color& color); inline void SetTextFont(Nz::FontRef font); inline void SetTextOutlineColor(const Nz::Color& color); @@ -53,7 +57,7 @@ namespace Ndk void HandleSelectionIndentation(bool add) override; void HandleWordCursorMove(bool left) override; - void UpdateDisplayText(); + void UpdateDisplayText() override; Nz::RichTextDrawer m_drawer; }; diff --git a/SDK/include/NDK/Widgets/RichTextAreaWidget.inl b/SDK/include/NDK/Widgets/RichTextAreaWidget.inl index 01f2dd7a3..4fd7faa10 100644 --- a/SDK/include/NDK/Widgets/RichTextAreaWidget.inl +++ b/SDK/include/NDK/Widgets/RichTextAreaWidget.inl @@ -11,6 +11,16 @@ namespace Ndk return m_drawer.GetDefaultCharacterSize(); } + inline float RichTextAreaWidget::GetCharacterSpacingOffset() const + { + return m_drawer.GetDefaultCharacterSpacingOffset(); + } + + inline float RichTextAreaWidget::GetLineSpacingOffset() const + { + return m_drawer.GetDefaultLineSpacingOffset(); + } + inline const Nz::Color& RichTextAreaWidget::GetTextColor() const { return m_drawer.GetDefaultColor(); @@ -41,6 +51,16 @@ namespace Ndk m_drawer.SetDefaultCharacterSize(characterSize); } + inline void RichTextAreaWidget::SetCharacterSpacingOffset(float offset) + { + m_drawer.SetDefaultCharacterSpacingOffset(offset); + } + + inline void RichTextAreaWidget::SetLineSpacingOffset(float offset) + { + m_drawer.SetDefaultLineSpacingOffset(offset); + } + inline void RichTextAreaWidget::SetTextColor(const Nz::Color& color) { m_drawer.SetDefaultColor(color); diff --git a/SDK/include/NDK/Widgets/TextAreaWidget.hpp b/SDK/include/NDK/Widgets/TextAreaWidget.hpp index dbbac6dad..8b06e7579 100644 --- a/SDK/include/NDK/Widgets/TextAreaWidget.hpp +++ b/SDK/include/NDK/Widgets/TextAreaWidget.hpp @@ -29,6 +29,8 @@ namespace Ndk inline unsigned int GetCharacterSize() const; inline const Nz::String& GetDisplayText() const; + inline float GetCharacterSpacingOffset() const; + inline float GetLineSpacingOffset() const; inline const Nz::String& GetText() const; inline const Nz::Color& GetTextColor() const; inline Nz::Font* GetTextFont() const; @@ -36,7 +38,9 @@ namespace Ndk inline float GetTextOulineThickness() const; inline Nz::TextStyleFlags GetTextStyle() const; - void SetCharacterSize(unsigned int characterSize); + inline void SetCharacterSize(unsigned int characterSize); + inline void SetCharacterSpacingOffset(float offset); + inline void SetLineSpacingOffset(float offset); inline void SetText(const Nz::String& text); inline void SetTextColor(const Nz::Color& text); inline void SetTextFont(Nz::FontRef font); @@ -61,6 +65,7 @@ namespace Ndk void HandleWordCursorMove(bool left) override; void UpdateDisplayText() override; + void UpdateMinimumSize(); Nz::SimpleTextDrawer m_drawer; Nz::String m_text; diff --git a/SDK/include/NDK/Widgets/TextAreaWidget.inl b/SDK/include/NDK/Widgets/TextAreaWidget.inl index 008cb77fa..f63897420 100644 --- a/SDK/include/NDK/Widgets/TextAreaWidget.inl +++ b/SDK/include/NDK/Widgets/TextAreaWidget.inl @@ -16,6 +16,16 @@ namespace Ndk return m_drawer.GetText(); } + inline float TextAreaWidget::GetCharacterSpacingOffset() const + { + return m_drawer.GetCharacterSpacingOffset(); + } + + inline float TextAreaWidget::GetLineSpacingOffset() const + { + return m_drawer.GetLineSpacingOffset(); + } + inline const Nz::String& TextAreaWidget::GetText() const { return m_text; @@ -46,6 +56,29 @@ namespace Ndk return m_drawer.GetStyle(); } + inline void TextAreaWidget::SetCharacterSize(unsigned int characterSize) + { + m_drawer.SetCharacterSize(characterSize); + + UpdateMinimumSize(); + UpdateDisplayText(); + } + + inline void TextAreaWidget::SetCharacterSpacingOffset(float offset) + { + m_drawer.SetCharacterSpacingOffset(offset); + + UpdateMinimumSize(); + UpdateDisplayText(); + } + + inline void TextAreaWidget::SetLineSpacingOffset(float offset) + { + m_drawer.SetLineSpacingOffset(offset); + + UpdateDisplayText(); + } + inline void TextAreaWidget::SetText(const Nz::String& text) { m_text = text; diff --git a/SDK/src/NDK/Widgets/TextAreaWidget.cpp b/SDK/src/NDK/Widgets/TextAreaWidget.cpp index b3068d0a3..4271f43a1 100644 --- a/SDK/src/NDK/Widgets/TextAreaWidget.cpp +++ b/SDK/src/NDK/Widgets/TextAreaWidget.cpp @@ -91,26 +91,6 @@ namespace Ndk SetText(newText); } - void TextAreaWidget::SetCharacterSize(unsigned int characterSize) - { - m_drawer.SetCharacterSize(characterSize); - - std::size_t fontCount = m_drawer.GetFontCount(); - unsigned int lineHeight = 0; - int spaceAdvance = 0; - for (std::size_t i = 0; i < fontCount; ++i) - { - Nz::Font* font = m_drawer.GetFont(i); - - const Nz::Font::SizeInfo& sizeInfo = font->GetSizeInfo(characterSize); - lineHeight = std::max(lineHeight, sizeInfo.lineHeight); - spaceAdvance = std::max(spaceAdvance, sizeInfo.spaceAdvance); - } - - Nz::Vector2f size = { float(spaceAdvance), float(lineHeight) + 5.f }; - SetMinimumSize(size); - } - void TextAreaWidget::Write(const Nz::String& text, std::size_t glyphPosition) { if (glyphPosition >= m_drawer.GetGlyphCount()) @@ -252,4 +232,22 @@ namespace Ndk SetCursorPosition(m_cursorPositionBegin); //< Refresh cursor position (prevent it from being outside of the text) } + + void TextAreaWidget::UpdateMinimumSize() + { + std::size_t fontCount = m_drawer.GetFontCount(); + float lineHeight = 0; + int spaceAdvance = 0; + for (std::size_t i = 0; i < fontCount; ++i) + { + Nz::Font* font = m_drawer.GetFont(i); + + const Nz::Font::SizeInfo& sizeInfo = font->GetSizeInfo(m_drawer.GetCharacterSize()); + lineHeight = std::max(lineHeight, m_drawer.GetLineHeight()); + spaceAdvance = std::max(spaceAdvance, sizeInfo.spaceAdvance); + } + + Nz::Vector2f size = { float(spaceAdvance), lineHeight + 5.f }; + SetMinimumSize(size); + } }