SDK/(Rich)TextAreaWidget: Add character and line spacing offsets

This commit is contained in:
Lynix 2020-01-26 17:06:39 +01:00
parent 8c7301f649
commit 1d86d90ca0
6 changed files with 83 additions and 22 deletions

View File

@ -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:

View File

@ -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;
};

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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);
}
}