SDK/TextAreaWidget: Add support for line selection

This commit is contained in:
Lynix 2018-04-15 02:39:19 +02:00
parent 9f95a6122b
commit 65c6996ccd
4 changed files with 280 additions and 72 deletions

View File

@ -88,6 +88,7 @@ Nazara Engine:
- Graphics module now register "White2D" and "WhiteCubemap" textures to the TextureLibrary (respectively a 1x1 texture 2D and a 1x1 texture cubemap) - Graphics module now register "White2D" and "WhiteCubemap" textures to the TextureLibrary (respectively a 1x1 texture 2D and a 1x1 texture cubemap)
- Added AbstractTextDrawer::GetLineGlyphCount, which returns the number of glyph part of the line - Added AbstractTextDrawer::GetLineGlyphCount, which returns the number of glyph part of the line
- Fixed Font handling of whitespace glyphs (which were triggering an error) - Fixed Font handling of whitespace glyphs (which were triggering an error)
- ⚠️ Translucent2D pipeline no longer has depth sorting
Nazara Development Kit: Nazara Development Kit:
- Added ImageWidget (#139) - Added ImageWidget (#139)
@ -132,6 +133,8 @@ Nazara Development Kit:
- ⚠️ Rewrote all render queue system, which should be more efficient, take scissor box into account - ⚠️ Rewrote all render queue system, which should be more efficient, take scissor box into account
- ⚠️ All widgets are now bound to a scissor box when rendering - ⚠️ All widgets are now bound to a scissor box when rendering
- Add DebugComponent (a component able to show aabb/obb/collision mesh) - Add DebugComponent (a component able to show aabb/obb/collision mesh)
- ⚠️ TextAreaWidget now support text selection (WIP)
- ⚠️ TextAreaWidget::GetHoveredGlyph now returns a two-dimensional position instead of a single glyph position
# 0.4: # 0.4:

View File

@ -11,6 +11,7 @@
#include <Nazara/Utility/SimpleTextDrawer.hpp> #include <Nazara/Utility/SimpleTextDrawer.hpp>
#include <NDK/BaseWidget.hpp> #include <NDK/BaseWidget.hpp>
#include <NDK/Widgets/Enums.hpp> #include <NDK/Widgets/Enums.hpp>
#include <vector>
namespace Ndk namespace Ndk
{ {
@ -30,15 +31,20 @@ namespace Ndk
inline void EnableMultiline(bool enable = true); inline void EnableMultiline(bool enable = true);
void EraseSelection();
inline unsigned int GetCharacterSize() const; inline unsigned int GetCharacterSize() const;
inline const Nz::Vector2ui& GetCursorPosition() const; inline const Nz::Vector2ui& GetCursorPosition() const;
inline Nz::Vector2ui GetCursorPosition(std::size_t glyphIndex) const;
inline const Nz::String& GetDisplayText() const; inline const Nz::String& GetDisplayText() const;
inline EchoMode GetEchoMode() const; inline EchoMode GetEchoMode() const;
inline std::size_t GetGlyphIndex(const Nz::Vector2ui& cursorPosition); inline std::size_t GetGlyphIndex(const Nz::Vector2ui& cursorPosition);
inline const Nz::String& GetText() const; inline const Nz::String& GetText() const;
inline const Nz::Color& GetTextColor() const; inline const Nz::Color& GetTextColor() const;
std::size_t GetHoveredGlyph(float x, float y) const; Nz::Vector2ui GetHoveredGlyph(float x, float y) const;
inline bool HasSelection() const;
inline bool IsMultilineEnabled() const; inline bool IsMultilineEnabled() const;
inline bool IsReadOnly() const; inline bool IsReadOnly() const;
@ -53,6 +59,7 @@ namespace Ndk
inline void SetCursorPosition(Nz::Vector2ui cursorPosition); inline void SetCursorPosition(Nz::Vector2ui cursorPosition);
inline void SetEchoMode(EchoMode echoMode); inline void SetEchoMode(EchoMode echoMode);
inline void SetReadOnly(bool readOnly = true); inline void SetReadOnly(bool readOnly = true);
inline void SetSelection(Nz::Vector2ui fromPosition, Nz::Vector2ui toPosition);
inline void SetText(const Nz::String& text); inline void SetText(const Nz::String& text);
inline void SetTextColor(const Nz::Color& text); inline void SetTextColor(const Nz::Color& text);
@ -64,6 +71,8 @@ namespace Ndk
NazaraSignal(OnTextAreaCursorMove, const TextAreaWidget* /*textArea*/, std::size_t* /*newCursorPosition*/); NazaraSignal(OnTextAreaCursorMove, const TextAreaWidget* /*textArea*/, std::size_t* /*newCursorPosition*/);
NazaraSignal(OnTextAreaKeyBackspace, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/); NazaraSignal(OnTextAreaKeyBackspace, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
NazaraSignal(OnTextAreaKeyDown, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/); NazaraSignal(OnTextAreaKeyDown, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
NazaraSignal(OnTextAreaKeyEnd, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
NazaraSignal(OnTextAreaKeyHome, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
NazaraSignal(OnTextAreaKeyLeft, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/); NazaraSignal(OnTextAreaKeyLeft, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
NazaraSignal(OnTextAreaKeyReturn, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/); NazaraSignal(OnTextAreaKeyReturn, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
NazaraSignal(OnTextAreaKeyRight, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/); NazaraSignal(OnTextAreaKeyRight, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
@ -79,6 +88,9 @@ namespace Ndk
bool OnKeyPressed(const Nz::WindowEvent::KeyEvent& key) override; bool OnKeyPressed(const Nz::WindowEvent::KeyEvent& key) override;
void OnKeyReleased(const Nz::WindowEvent::KeyEvent& key) override; void OnKeyReleased(const Nz::WindowEvent::KeyEvent& key) override;
void OnMouseButtonPress(int /*x*/, int /*y*/, Nz::Mouse::Button button) override; void OnMouseButtonPress(int /*x*/, int /*y*/, Nz::Mouse::Button button) override;
void OnMouseButtonRelease(int /*x*/, int /*y*/, Nz::Mouse::Button button) override;
void OnMouseEnter() override;
void OnMouseMoved(int x, int y, int deltaX, int deltaY) override;
void OnTextEntered(char32_t character, bool repeated) override; void OnTextEntered(char32_t character, bool repeated) override;
void RefreshCursor(); void RefreshCursor();
@ -88,10 +100,13 @@ namespace Ndk
EntityHandle m_cursorEntity; EntityHandle m_cursorEntity;
EntityHandle m_textEntity; EntityHandle m_textEntity;
Nz::SimpleTextDrawer m_drawer; Nz::SimpleTextDrawer m_drawer;
Nz::SpriteRef m_cursorSprite;
Nz::String m_text; Nz::String m_text;
Nz::TextSpriteRef m_textSprite; Nz::TextSpriteRef m_textSprite;
Nz::Vector2ui m_cursorPosition; Nz::Vector2ui m_cursorPositionBegin;
Nz::Vector2ui m_cursorPositionEnd;
Nz::Vector2ui m_selectionCursor;
std::vector<Nz::SpriteRef> m_cursorSprites;
bool m_isMouseButtonDown;
bool m_multiLineEnabled; bool m_multiLineEnabled;
bool m_readOnly; bool m_readOnly;
}; };

View File

@ -8,7 +8,8 @@ namespace Ndk
{ {
inline void TextAreaWidget::Clear() inline void TextAreaWidget::Clear()
{ {
m_cursorPosition.MakeZero(); m_cursorPositionBegin.MakeZero();
m_cursorPositionEnd.MakeZero();
m_drawer.Clear(); m_drawer.Clear();
m_text.Clear(); m_text.Clear();
m_textSprite->Update(m_drawer); m_textSprite->Update(m_drawer);
@ -29,7 +30,30 @@ namespace Ndk
inline const Nz::Vector2ui& TextAreaWidget::GetCursorPosition() const inline const Nz::Vector2ui& TextAreaWidget::GetCursorPosition() const
{ {
return m_cursorPosition; return m_cursorPositionBegin;
}
Nz::Vector2ui TextAreaWidget::GetCursorPosition(std::size_t glyphIndex) const
{
glyphIndex = std::min(glyphIndex, m_drawer.GetGlyphCount());
std::size_t lineCount = m_drawer.GetLineCount();
std::size_t line = 0U;
for (std::size_t i = line + 1; i < lineCount; ++i)
{
if (m_drawer.GetLine(i).glyphIndex > glyphIndex)
break;
line = i;
}
const auto& lineInfo = m_drawer.GetLine(line);
Nz::Vector2ui cursorPos;
cursorPos.y = static_cast<unsigned int>(line);
cursorPos.x = static_cast<unsigned int>(glyphIndex - lineInfo.glyphIndex);
return cursorPos;
} }
inline const Nz::String& TextAreaWidget::GetDisplayText() const inline const Nz::String& TextAreaWidget::GetDisplayText() const
@ -63,7 +87,12 @@ namespace Ndk
return m_drawer.GetColor(); return m_drawer.GetColor();
} }
inline bool Ndk::TextAreaWidget::IsMultilineEnabled() const inline bool TextAreaWidget::HasSelection() const
{
return m_cursorPositionBegin != m_cursorPositionEnd;
}
inline bool TextAreaWidget::IsMultilineEnabled() const
{ {
return m_multiLineEnabled; return m_multiLineEnabled;
} }
@ -75,7 +104,7 @@ namespace Ndk
inline void TextAreaWidget::MoveCursor(int offset) inline void TextAreaWidget::MoveCursor(int offset)
{ {
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition); std::size_t cursorGlyph = GetGlyphIndex(m_cursorPositionBegin);
if (offset >= 0) if (offset >= 0)
SetCursorPosition(cursorGlyph + static_cast<std::size_t>(offset)); SetCursorPosition(cursorGlyph + static_cast<std::size_t>(offset));
else else
@ -104,7 +133,7 @@ namespace Ndk
} }
}; };
Nz::Vector2ui cursorPosition = m_cursorPosition; Nz::Vector2ui cursorPosition = m_cursorPositionBegin;
cursorPosition.x = ClampOffset(static_cast<unsigned int>(cursorPosition.x), offset.x); cursorPosition.x = ClampOffset(static_cast<unsigned int>(cursorPosition.x), offset.x);
cursorPosition.y = ClampOffset(static_cast<unsigned int>(cursorPosition.y), offset.y); cursorPosition.y = ClampOffset(static_cast<unsigned int>(cursorPosition.y), offset.y);
@ -120,22 +149,8 @@ namespace Ndk
{ {
OnTextAreaCursorMove(this, &glyphIndex); OnTextAreaCursorMove(this, &glyphIndex);
glyphIndex = std::min(glyphIndex, m_drawer.GetGlyphCount()); m_cursorPositionBegin = GetCursorPosition(glyphIndex);
m_cursorPositionEnd = m_cursorPositionBegin;
std::size_t lineCount = m_drawer.GetLineCount();
std::size_t line = 0U;
for (std::size_t i = line + 1; i < lineCount; ++i)
{
if (m_drawer.GetLine(i).glyphIndex > glyphIndex)
break;
line = i;
}
const auto& lineInfo = m_drawer.GetLine(line);
m_cursorPosition.y = static_cast<unsigned int>(line);
m_cursorPosition.x = static_cast<unsigned int>(glyphIndex - lineInfo.glyphIndex);
RefreshCursor(); RefreshCursor();
} }
@ -146,7 +161,7 @@ namespace Ndk
if (cursorPosition.y >= lineCount) if (cursorPosition.y >= lineCount)
cursorPosition.y = static_cast<unsigned int>(lineCount - 1); cursorPosition.y = static_cast<unsigned int>(lineCount - 1);
m_cursorPosition = cursorPosition; m_cursorPositionBegin = cursorPosition;
const auto& lineInfo = m_drawer.GetLine(cursorPosition.y); const auto& lineInfo = m_drawer.GetLine(cursorPosition.y);
if (cursorPosition.y + 1 < lineCount) if (cursorPosition.y + 1 < lineCount)
@ -155,6 +170,8 @@ namespace Ndk
cursorPosition.x = std::min(cursorPosition.x, static_cast<unsigned int>(nextLineInfo.glyphIndex - lineInfo.glyphIndex - 1)); cursorPosition.x = std::min(cursorPosition.x, static_cast<unsigned int>(nextLineInfo.glyphIndex - lineInfo.glyphIndex - 1));
} }
m_cursorPositionEnd = m_cursorPositionBegin;
std::size_t glyphIndex = lineInfo.glyphIndex + cursorPosition.x; std::size_t glyphIndex = lineInfo.glyphIndex + cursorPosition.x;
OnTextAreaCursorMove(this, &glyphIndex); OnTextAreaCursorMove(this, &glyphIndex);
@ -175,6 +192,23 @@ namespace Ndk
m_cursorEntity->Enable(!m_readOnly && HasFocus()); m_cursorEntity->Enable(!m_readOnly && HasFocus());
} }
inline void TextAreaWidget::SetSelection(Nz::Vector2ui fromPosition, Nz::Vector2ui toPosition)
{
///TODO: Check if position are valid
// Ensure begin is before end
if (toPosition.y < fromPosition.y || (toPosition.y == fromPosition.y && toPosition.x < fromPosition.x))
std::swap(fromPosition, toPosition);
if (m_cursorPositionBegin != fromPosition || m_cursorPositionEnd != toPosition)
{
m_cursorPositionBegin = fromPosition;
m_cursorPositionEnd = toPosition;
RefreshCursor();
}
}
inline void TextAreaWidget::SetText(const Nz::String& text) inline void TextAreaWidget::SetText(const Nz::String& text)
{ {
m_text = text; m_text = text;

View File

@ -12,16 +12,14 @@ namespace Ndk
TextAreaWidget::TextAreaWidget(BaseWidget* parent) : TextAreaWidget::TextAreaWidget(BaseWidget* parent) :
BaseWidget(parent), BaseWidget(parent),
m_echoMode(EchoMode_Normal), m_echoMode(EchoMode_Normal),
m_cursorPosition(0U, 0U), m_cursorPositionBegin(0U, 0U),
m_cursorPositionEnd(0U, 0U),
m_isMouseButtonDown(false),
m_multiLineEnabled(false), m_multiLineEnabled(false),
m_readOnly(false) m_readOnly(false)
{ {
m_cursorSprite = Nz::Sprite::New();
m_cursorSprite->SetColor(Nz::Color::Black);
m_cursorSprite->SetSize(1.f, float(m_drawer.GetFont()->GetSizeInfo(m_drawer.GetCharacterSize()).lineHeight));
m_cursorEntity = CreateEntity(true); m_cursorEntity = CreateEntity(true);
m_cursorEntity->AddComponent<GraphicsComponent>().Attach(m_cursorSprite, 10); m_cursorEntity->AddComponent<GraphicsComponent>();
m_cursorEntity->AddComponent<NodeComponent>().SetParent(this); m_cursorEntity->AddComponent<NodeComponent>().SetParent(this);
m_cursorEntity->Enable(false); m_cursorEntity->Enable(false);
@ -72,7 +70,29 @@ namespace Ndk
OnTextChanged(this, m_text); OnTextChanged(this, m_text);
} }
std::size_t TextAreaWidget::GetHoveredGlyph(float x, float y) const void TextAreaWidget::EraseSelection()
{
if (!HasSelection())
return;
std::size_t cursorGlyphBegin = GetGlyphIndex(m_cursorPositionBegin);
std::size_t cursorGlyphEnd = GetGlyphIndex(m_cursorPositionEnd);
std::size_t textLength = m_text.GetLength();
if (cursorGlyphBegin > textLength)
return;
Nz::String newText;
if (cursorGlyphBegin > 0)
newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(cursorGlyphBegin) - 1));
if (cursorGlyphEnd < textLength)
newText.Append(m_text.SubString(m_text.GetCharacterPosition(cursorGlyphEnd)));
SetText(newText);
}
Nz::Vector2ui TextAreaWidget::GetHoveredGlyph(float x, float y) const
{ {
std::size_t glyphCount = m_drawer.GetGlyphCount(); std::size_t glyphCount = m_drawer.GetGlyphCount();
if (glyphCount > 0) if (glyphCount > 0)
@ -88,7 +108,8 @@ namespace Ndk
std::size_t upperLimit = (line != lineCount - 1) ? m_drawer.GetLine(line + 1).glyphIndex : glyphCount + 1; std::size_t upperLimit = (line != lineCount - 1) ? m_drawer.GetLine(line + 1).glyphIndex : glyphCount + 1;
std::size_t i = m_drawer.GetLine(line).glyphIndex; std::size_t firstLineGlyph = m_drawer.GetLine(line).glyphIndex;
std::size_t i = firstLineGlyph;
for (; i < upperLimit - 1; ++i) for (; i < upperLimit - 1; ++i)
{ {
Nz::Rectf bounds = m_drawer.GetGlyph(i).bounds; Nz::Rectf bounds = m_drawer.GetGlyph(i).bounds;
@ -96,10 +117,10 @@ namespace Ndk
break; break;
} }
return i; return Nz::Vector2ui(i - firstLineGlyph, line);
} }
return 0; return Nz::Vector2ui::Zero();
} }
void TextAreaWidget::ResizeToContent() void TextAreaWidget::ResizeToContent()
@ -109,7 +130,7 @@ namespace Ndk
void TextAreaWidget::Write(const Nz::String& text) void TextAreaWidget::Write(const Nz::String& text)
{ {
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition); std::size_t cursorGlyph = GetGlyphIndex(m_cursorPositionBegin);
if (cursorGlyph >= m_drawer.GetGlyphCount()) if (cursorGlyph >= m_drawer.GetGlyphCount())
{ {
@ -156,20 +177,27 @@ namespace Ndk
{ {
case Nz::Keyboard::Delete: case Nz::Keyboard::Delete:
{ {
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition); if (HasSelection())
EraseSelection();
else
{
std::size_t cursorGlyphBegin = GetGlyphIndex(m_cursorPositionBegin);
std::size_t cursorGlyphEnd = GetGlyphIndex(m_cursorPositionEnd);
std::size_t textLength = m_text.GetLength(); std::size_t textLength = m_text.GetLength();
if (cursorGlyph > textLength) if (cursorGlyphBegin > textLength)
return true; return true;
Nz::String newText; Nz::String newText;
if (cursorGlyph > 0) if (cursorGlyphBegin > 0)
newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(cursorGlyph) - 1)); newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(cursorGlyphBegin) - 1));
if (cursorGlyph < textLength) if (cursorGlyphEnd < textLength)
newText.Append(m_text.SubString(m_text.GetCharacterPosition(cursorGlyph + 1))); newText.Append(m_text.SubString(m_text.GetCharacterPosition(cursorGlyphEnd + 1)));
SetText(newText); SetText(newText);
}
return true; return true;
} }
@ -181,10 +209,38 @@ namespace Ndk
if (ignoreDefaultAction) if (ignoreDefaultAction)
return true; return true;
if (HasSelection())
SetCursorPosition(m_cursorPositionEnd);
MoveCursor({0, 1}); MoveCursor({0, 1});
return true; return true;
} }
case Nz::Keyboard::End:
{
bool ignoreDefaultAction = false;
OnTextAreaKeyEnd(this, &ignoreDefaultAction);
if (ignoreDefaultAction)
return true;
const auto& lineInfo = m_drawer.GetLine(m_cursorPositionEnd.y);
SetCursorPosition({ m_drawer.GetLineGlyphCount(m_cursorPositionEnd.y), m_cursorPositionEnd.y });
return true;
}
case Nz::Keyboard::Home:
{
bool ignoreDefaultAction = false;
OnTextAreaKeyHome(this, &ignoreDefaultAction);
if (ignoreDefaultAction)
return true;
SetCursorPosition({ 0U, m_cursorPositionEnd.y });
return true;
}
case Nz::Keyboard::Left: case Nz::Keyboard::Left:
{ {
bool ignoreDefaultAction = false; bool ignoreDefaultAction = false;
@ -193,7 +249,11 @@ namespace Ndk
if (ignoreDefaultAction) if (ignoreDefaultAction)
return true; return true;
if (HasSelection())
SetCursorPosition(m_cursorPositionBegin);
else
MoveCursor(-1); MoveCursor(-1);
return true; return true;
} }
@ -205,7 +265,11 @@ namespace Ndk
if (ignoreDefaultAction) if (ignoreDefaultAction)
return true; return true;
if (HasSelection())
SetCursorPosition(m_cursorPositionEnd);
else
MoveCursor(1); MoveCursor(1);
return true; return true;
} }
@ -217,6 +281,9 @@ namespace Ndk
if (ignoreDefaultAction) if (ignoreDefaultAction)
return true; return true;
if (HasSelection())
SetCursorPosition(m_cursorPositionBegin);
MoveCursor({0, -1}); MoveCursor({0, -1});
return true; return true;
} }
@ -237,7 +304,39 @@ namespace Ndk
SetFocus(); SetFocus();
const Padding& padding = GetPadding(); const Padding& padding = GetPadding();
SetCursorPosition(GetHoveredGlyph(float(x - padding.left), float(y - padding.top))); Nz::Vector2ui hoveredGlyph = GetHoveredGlyph(float(x - padding.left), float(y - padding.top));
// Shift extends selection
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::LShift) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::RShift))
SetSelection(hoveredGlyph, m_selectionCursor);
else
{
SetCursorPosition(hoveredGlyph);
m_selectionCursor = m_cursorPositionBegin;
}
m_isMouseButtonDown = true;
}
}
void TextAreaWidget::OnMouseButtonRelease(int, int, Nz::Mouse::Button button)
{
if (button == Nz::Mouse::Left)
m_isMouseButtonDown = false;
}
void TextAreaWidget::OnMouseEnter()
{
if (!Nz::Mouse::IsButtonPressed(Nz::Mouse::Left))
m_isMouseButtonDown = false;
}
void TextAreaWidget::OnMouseMoved(int x, int y, int deltaX, int deltaY)
{
if (m_isMouseButtonDown)
{
const Padding& padding = GetPadding();
SetSelection(m_selectionCursor, GetHoveredGlyph(float(x - padding.left), float(y - padding.top)));
} }
} }
@ -253,20 +352,30 @@ namespace Ndk
bool ignoreDefaultAction = false; bool ignoreDefaultAction = false;
OnTextAreaKeyBackspace(this, &ignoreDefaultAction); OnTextAreaKeyBackspace(this, &ignoreDefaultAction);
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition); std::size_t cursorGlyphBegin = GetGlyphIndex(m_cursorPositionBegin);
if (ignoreDefaultAction || cursorGlyph == 0) std::size_t cursorGlyphEnd = GetGlyphIndex(m_cursorPositionEnd);
if (ignoreDefaultAction || cursorGlyphEnd == 0)
break; break;
// When a text is selected, delete key does the same as delete and leave the character behind it
if (HasSelection())
EraseSelection();
else
{
Nz::String newText; Nz::String newText;
if (cursorGlyph > 1) if (cursorGlyphBegin > 1)
newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(cursorGlyph - 1) - 1)); newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(cursorGlyphBegin - 1) - 1));
if (cursorGlyph < m_text.GetLength()) if (cursorGlyphEnd < m_text.GetLength())
newText.Append(m_text.SubString(m_text.GetCharacterPosition(cursorGlyph))); newText.Append(m_text.SubString(m_text.GetCharacterPosition(cursorGlyphEnd)));
// Move cursor before setting text (to prevent SetText to move our cursor)
MoveCursor(-1); MoveCursor(-1);
SetText(newText); SetText(newText);
}
break; break;
} }
@ -288,6 +397,9 @@ namespace Ndk
if (Nz::Unicode::GetCategory(character) == Nz::Unicode::Category_Other_Control) if (Nz::Unicode::GetCategory(character) == Nz::Unicode::Category_Other_Control)
break; break;
if (HasSelection())
EraseSelection();
Write(Nz::String::Unicode(character)); Write(Nz::String::Unicode(character));
break; break;
} }
@ -299,8 +411,35 @@ namespace Ndk
if (m_readOnly) if (m_readOnly)
return; return;
const auto& lineInfo = m_drawer.GetLine(m_cursorPosition.y); m_cursorEntity->GetComponent<NodeComponent>().SetPosition(GetContentOrigin());
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition);
std::size_t selectionLineCount = m_cursorPositionEnd.y - m_cursorPositionBegin.y + 1;
std::size_t oldSpriteCount = m_cursorSprites.size();
if (m_cursorSprites.size() != selectionLineCount)
{
m_cursorSprites.resize(m_cursorPositionEnd.y - m_cursorPositionBegin.y + 1);
for (std::size_t i = oldSpriteCount; i < m_cursorSprites.size(); ++i)
{
m_cursorSprites[i] = Nz::Sprite::New();
m_cursorSprites[i]->SetMaterial(Nz::Material::New("Translucent2D"));
}
}
float lineHeight = float(m_drawer.GetFont()->GetSizeInfo(m_drawer.GetCharacterSize()).lineHeight);
GraphicsComponent& gfxComponent = m_cursorEntity->GetComponent<GraphicsComponent>();
gfxComponent.Clear();
for (unsigned int i = m_cursorPositionBegin.y; i <= m_cursorPositionEnd.y; ++i)
{
const auto& lineInfo = m_drawer.GetLine(i);
Nz::SpriteRef& cursorSprite = m_cursorSprites[i - m_cursorPositionBegin.y];
if (i == m_cursorPositionBegin.y || i == m_cursorPositionEnd.y)
{
auto GetGlyphPos = [&](std::size_t localGlyphPos)
{
std::size_t cursorGlyph = GetGlyphIndex({ localGlyphPos, i });
std::size_t glyphCount = m_drawer.GetGlyphCount(); std::size_t glyphCount = m_drawer.GetGlyphCount();
float position; float position;
@ -314,9 +453,26 @@ namespace Ndk
else else
position = 0.f; position = 0.f;
Nz::Vector2f contentOrigin = GetContentOrigin(); return position;
};
m_cursorEntity->GetComponent<NodeComponent>().SetPosition(contentOrigin.x + position, contentOrigin.y + lineInfo.bounds.y); float beginX = (i == m_cursorPositionBegin.y) ? GetGlyphPos(m_cursorPositionBegin.x) : 0.f;
float endX = (i == m_cursorPositionEnd.y) ? GetGlyphPos(m_cursorPositionEnd.x) : lineInfo.bounds.width;
float spriteSize = std::max(endX - beginX, 1.f);
cursorSprite->SetColor((m_cursorPositionBegin == m_cursorPositionEnd) ? Nz::Color::Black : Nz::Color(0, 0, 0, 50));
cursorSprite->SetSize(spriteSize, float(m_drawer.GetFont()->GetSizeInfo(m_drawer.GetCharacterSize()).lineHeight));
gfxComponent.Attach(cursorSprite, Nz::Matrix4f::Translate({ beginX, lineInfo.bounds.y, 0.f }));
}
else
{
cursorSprite->SetColor(Nz::Color(0, 0, 0, 50));
cursorSprite->SetSize(lineInfo.bounds.width, float(m_drawer.GetFont()->GetSizeInfo(m_drawer.GetCharacterSize()).lineHeight));
gfxComponent.Attach(cursorSprite, Nz::Matrix4f::Translate({ 0.f, lineInfo.bounds.y, 0.f }));
}
}
} }
void TextAreaWidget::UpdateDisplayText() void TextAreaWidget::UpdateDisplayText()
@ -335,6 +491,6 @@ namespace Ndk
m_textSprite->Update(m_drawer); m_textSprite->Update(m_drawer);
SetCursorPosition(m_cursorPosition); //< Refresh cursor position (prevent it from being outside of the text) SetCursorPosition(m_cursorPositionBegin); //< Refresh cursor position (prevent it from being outside of the text)
} }
} }