SDK/TextAreaWidget: Expose cursor

This commit is contained in:
Lynix 2016-12-05 20:39:13 +01:00
parent 6138c02b9b
commit d3e3e9cbe2
3 changed files with 37 additions and 24 deletions

View File

@ -30,13 +30,17 @@ namespace Ndk
//virtual TextAreaWidget* Clone() const = 0; //virtual TextAreaWidget* Clone() const = 0;
inline std::size_t GetCursorPosition() const;
inline std::size_t GetLineCount() const; inline std::size_t GetLineCount() const;
inline const Nz::String& GetText() const; inline const Nz::String& GetText() const;
std::size_t GetHoveredGlyph(float x, float y) const; std::size_t GetHoveredGlyph(float x, float y) const;
inline void MoveCursor(int offset);
void ResizeToContent() override; void ResizeToContent() override;
inline void SetCursorPosition(std::size_t cursorPosition);
void SetText(const Nz::String& text); void SetText(const Nz::String& text);
void Write(const Nz::String& text); void Write(const Nz::String& text);

View File

@ -15,6 +15,11 @@ namespace Ndk
RefreshCursor(); RefreshCursor();
} }
inline std::size_t TextAreaWidget::GetCursorPosition() const
{
return m_cursorPosition;
}
inline std::size_t TextAreaWidget::GetLineCount() const inline std::size_t TextAreaWidget::GetLineCount() const
{ {
return m_drawer.GetLineCount(); return m_drawer.GetLineCount();
@ -24,4 +29,25 @@ namespace Ndk
{ {
return m_drawer.GetText(); return m_drawer.GetText();
} }
inline void TextAreaWidget::MoveCursor(int offset)
{
if (offset >= 0)
SetCursorPosition(m_cursorPosition += static_cast<std::size_t>(offset));
else
{
std::size_t nOffset = static_cast<std::size_t>(-offset);
if (nOffset >= m_cursorPosition)
SetCursorPosition(0);
else
SetCursorPosition(m_cursorPosition - nOffset);
}
}
inline void TextAreaWidget::SetCursorPosition(std::size_t cursorPosition)
{
m_cursorPosition = std::min(cursorPosition, m_drawer.GetGlyphCount());
RefreshCursor();
}
} }

View File

@ -87,7 +87,7 @@ namespace Ndk
if (m_cursorPosition >= m_drawer.GetGlyphCount()) if (m_cursorPosition >= m_drawer.GetGlyphCount())
{ {
AppendText(text); AppendText(text);
m_cursorPosition = m_drawer.GetGlyphCount(); SetCursorPosition(m_drawer.GetGlyphCount());
} }
else else
{ {
@ -95,10 +95,8 @@ namespace Ndk
currentText.Insert(currentText.GetCharacterPosition(m_cursorPosition), text); currentText.Insert(currentText.GetCharacterPosition(m_cursorPosition), text);
SetText(currentText); SetText(currentText);
m_cursorPosition += text.GetLength(); SetCursorPosition(m_cursorPosition + text.GetLength());
} }
RefreshCursor();
} }
void TextAreaWidget::RefreshCursor() void TextAreaWidget::RefreshCursor()
@ -151,21 +149,12 @@ namespace Ndk
} }
case Nz::Keyboard::Left: case Nz::Keyboard::Left:
if (m_cursorPosition > 0) MoveCursor(-1);
m_cursorPosition--;
RefreshCursor();
break; break;
case Nz::Keyboard::Right: case Nz::Keyboard::Right:
{ MoveCursor(1);
std::size_t glyphCount = m_drawer.GetGlyphCount();
if (m_cursorPosition < glyphCount)
m_cursorPosition++;
RefreshCursor();
break; break;
}
} }
} }
@ -184,8 +173,7 @@ namespace Ndk
{ {
GrabKeyboard(); GrabKeyboard();
m_cursorPosition = GetHoveredGlyph(x, y); SetCursorPosition(GetHoveredGlyph(x, y));
RefreshCursor();
} }
} }
@ -213,13 +201,8 @@ namespace Ndk
if (m_cursorPosition < m_drawer.GetGlyphCount()) if (m_cursorPosition < m_drawer.GetGlyphCount())
newText.Append(text.SubString(text.GetCharacterPosition(m_cursorPosition))); newText.Append(text.SubString(text.GetCharacterPosition(m_cursorPosition)));
m_drawer.SetText(newText); SetText(newText);
m_textSprite->Update(m_drawer); MoveCursor(-1);
if (m_cursorPosition > 0)
m_cursorPosition--;
RefreshCursor();
break; break;
} }