Add line wrapping

This commit is contained in:
Lynix
2019-07-05 23:12:12 +02:00
parent b41637c990
commit 0ec1480024
7 changed files with 79 additions and 13 deletions

View File

@@ -32,7 +32,7 @@ namespace Ndk
//virtual TextAreaWidget* Clone() const = 0;
void EnableLineWrap(bool enable = true);
inline void EnableMultiline(bool enable = true);
inline void EnableTabWriting(bool enable = true);
@@ -57,6 +57,7 @@ namespace Ndk
inline bool HasSelection() const;
inline bool IsLineWrapEnabled() const;
inline bool IsMultilineEnabled() const;
inline bool IsReadOnly() const;
inline bool IsTabWritingEnabled() const;
@@ -122,6 +123,7 @@ namespace Ndk
Nz::Vector2ui m_cursorPositionEnd;
Nz::Vector2ui m_selectionCursor;
std::vector<Nz::SpriteRef> m_cursorSprites;
bool m_isLineWrapEnabled;
bool m_isMouseButtonDown;
bool m_multiLineEnabled;
bool m_readOnly;

View File

@@ -123,6 +123,11 @@ namespace Ndk
return m_cursorPositionBegin != m_cursorPositionEnd;
}
inline bool TextAreaWidget::IsLineWrapEnabled() const
{
return m_isLineWrapEnabled;
}
inline bool TextAreaWidget::IsMultilineEnabled() const
{
return m_multiLineEnabled;

View File

@@ -45,6 +45,7 @@ namespace Ndk
// History
m_history = Add<TextAreaWidget>();
m_history->EnableBackground(true);
m_history->EnableLineWrap(true);
m_history->SetReadOnly(true);
m_history->SetBackgroundColor(Nz::Color(80, 80, 160, 128));

View File

@@ -16,6 +16,7 @@ namespace Ndk
m_echoMode(EchoMode_Normal),
m_cursorPositionBegin(0U, 0U),
m_cursorPositionEnd(0U, 0U),
m_isLineWrapEnabled(false),
m_isMouseButtonDown(false),
m_multiLineEnabled(false),
m_readOnly(false),
@@ -74,12 +75,26 @@ namespace Ndk
}
}
m_textSprite->Update(m_drawer);
SetPreferredSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
UpdateTextSprite();
OnTextChanged(this, m_text);
}
void TextAreaWidget::EnableLineWrap(bool enable)
{
if (m_isLineWrapEnabled != enable)
{
m_isLineWrapEnabled = enable;
if (enable)
m_drawer.SetMaxLineWidth(GetWidth());
else
m_drawer.SetMaxLineWidth(std::numeric_limits<float>::infinity());
UpdateTextSprite();
}
}
void TextAreaWidget::Erase(std::size_t firstGlyph, std::size_t lastGlyph)
{
if (firstGlyph > lastGlyph)
@@ -188,6 +203,12 @@ namespace Ndk
{
BaseWidget::Layout();
if (m_isLineWrapEnabled)
{
m_drawer.SetMaxLineWidth(GetWidth());
UpdateTextSprite();
}
RefreshCursor();
}
@@ -607,9 +628,14 @@ namespace Ndk
break;
}
m_textSprite->Update(m_drawer);
SetPreferredSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
UpdateTextSprite();
SetCursorPosition(m_cursorPositionBegin); //< Refresh cursor position (prevent it from being outside of the text)
}
void TextAreaWidget::UpdateTextSprite()
{
m_textSprite->Update(m_drawer);
SetPreferredSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
}
}