Widgets: Add [Rich]TextAreaWidget
This commit is contained in:
510
src/Nazara/Widgets/AbstractTextAreaWidget.cpp
Normal file
510
src/Nazara/Widgets/AbstractTextAreaWidget.cpp
Normal file
@@ -0,0 +1,510 @@
|
||||
// Copyright (C) 2021 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Widgets module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/AbstractTextAreaWidget.hpp>
|
||||
#include <Nazara/Core/StringExt.hpp>
|
||||
#include <Nazara/Core/Unicode.hpp>
|
||||
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
|
||||
#include <Nazara/Utility/Font.hpp>
|
||||
#include <Nazara/Utility/Components/NodeComponent.hpp>
|
||||
#include <Nazara/Widgets/Canvas.hpp>
|
||||
#include <Nazara/Widgets/Widgets.hpp>
|
||||
#include <Nazara/Widgets/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
namespace
|
||||
{
|
||||
constexpr float paddingWidth = 5.f;
|
||||
constexpr float paddingHeight = 3.f;
|
||||
}
|
||||
|
||||
AbstractTextAreaWidget::AbstractTextAreaWidget(BaseWidget* parent) :
|
||||
BaseWidget(parent),
|
||||
m_characterFilter(),
|
||||
m_echoMode(EchoMode::Normal),
|
||||
m_cursorPositionBegin(0U, 0U),
|
||||
m_cursorPositionEnd(0U, 0U),
|
||||
m_isLineWrapEnabled(false),
|
||||
m_isMouseButtonDown(false),
|
||||
m_multiLineEnabled(false),
|
||||
m_readOnly(false),
|
||||
m_tabEnabled(false)
|
||||
{
|
||||
m_textSprite = std::make_shared<TextSprite>(Widgets::Instance()->GetTransparentMaterial());
|
||||
|
||||
auto& registry = GetRegistry();
|
||||
|
||||
m_textEntity = CreateEntity();
|
||||
|
||||
auto& gfxComponent = registry.emplace<GraphicsComponent>(m_textEntity, IsVisible());
|
||||
gfxComponent.AttachRenderable(m_textSprite, GetCanvas()->GetRenderMask());
|
||||
|
||||
auto& textNode = GetRegistry().emplace<NodeComponent>(m_textEntity);
|
||||
textNode.SetParent(this);
|
||||
textNode.SetPosition(paddingWidth, paddingHeight);
|
||||
|
||||
SetCursor(SystemCursor::Text);
|
||||
|
||||
EnableBackground(true);
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::Clear()
|
||||
{
|
||||
AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||
textDrawer.Clear();
|
||||
UpdateTextSprite();
|
||||
|
||||
m_cursorPositionBegin.MakeZero();
|
||||
m_cursorPositionEnd.MakeZero();
|
||||
|
||||
RefreshCursor();
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::EnableLineWrap(bool enable)
|
||||
{
|
||||
if (m_isLineWrapEnabled != enable)
|
||||
{
|
||||
m_isLineWrapEnabled = enable;
|
||||
|
||||
AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||
|
||||
if (enable)
|
||||
textDrawer.SetMaxLineWidth(GetWidth());
|
||||
else
|
||||
textDrawer.SetMaxLineWidth(std::numeric_limits<float>::infinity());
|
||||
|
||||
UpdateTextSprite();
|
||||
}
|
||||
}
|
||||
|
||||
Vector2ui AbstractTextAreaWidget::GetHoveredGlyph(float x, float y) const
|
||||
{
|
||||
const AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||
|
||||
auto& textNode = GetRegistry().get<NodeComponent>(m_textEntity);
|
||||
Vector2f textPosition = Vector2f(textNode.GetPosition(CoordSys::Local));
|
||||
x -= textPosition.x;
|
||||
y -= textPosition.y;
|
||||
|
||||
std::size_t glyphCount = textDrawer.GetGlyphCount();
|
||||
if (glyphCount > 0)
|
||||
{
|
||||
std::size_t lineCount = textDrawer.GetLineCount();
|
||||
std::size_t line = 0U;
|
||||
for (; line < lineCount - 1; ++line)
|
||||
{
|
||||
Rectf lineBounds = textDrawer.GetLine(line).bounds;
|
||||
if (lineBounds.GetMaximum().y > y)
|
||||
break;
|
||||
}
|
||||
|
||||
std::size_t upperLimit = (line != lineCount - 1) ? textDrawer.GetLine(line + 1).glyphIndex : glyphCount + 1;
|
||||
|
||||
std::size_t firstLineGlyph = textDrawer.GetLine(line).glyphIndex;
|
||||
std::size_t i = firstLineGlyph;
|
||||
for (; i < upperLimit - 1; ++i)
|
||||
{
|
||||
Rectf bounds = textDrawer.GetGlyph(i).bounds;
|
||||
if (x < bounds.x + bounds.width * 0.75f)
|
||||
break;
|
||||
}
|
||||
|
||||
return Vector2ui(Vector2<std::size_t>(i - firstLineGlyph, line));
|
||||
}
|
||||
|
||||
return Vector2ui::Zero();
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::Layout()
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
if (m_isLineWrapEnabled)
|
||||
{
|
||||
AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||
|
||||
textDrawer.SetMaxLineWidth(GetWidth());
|
||||
UpdateTextSprite();
|
||||
}
|
||||
|
||||
RefreshCursor();
|
||||
}
|
||||
|
||||
bool AbstractTextAreaWidget::IsFocusable() const
|
||||
{
|
||||
return !m_readOnly;
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::OnFocusLost()
|
||||
{
|
||||
// Hide cursors
|
||||
auto& registry = GetRegistry();
|
||||
for (auto& cursor : m_cursors)
|
||||
registry.get<GraphicsComponent>(cursor.entity).Hide();
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::OnFocusReceived()
|
||||
{
|
||||
if (!m_readOnly)
|
||||
{
|
||||
// Show cursors
|
||||
auto& registry = GetRegistry();
|
||||
for (auto& cursor : m_cursors)
|
||||
registry.get<GraphicsComponent>(cursor.entity).Show();
|
||||
}
|
||||
}
|
||||
|
||||
bool AbstractTextAreaWidget::OnKeyPressed(const WindowEvent::KeyEvent& key)
|
||||
{
|
||||
const AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||
|
||||
switch (key.virtualKey)
|
||||
{
|
||||
case Keyboard::VKey::Backspace:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyBackspace(this, &ignoreDefaultAction);
|
||||
|
||||
std::size_t cursorGlyphEnd = GetGlyphIndex(m_cursorPositionEnd);
|
||||
|
||||
if (ignoreDefaultAction || cursorGlyphEnd == 0)
|
||||
return true;
|
||||
|
||||
// When a text is selected, delete key does the same as delete and leave the character behind it
|
||||
if (HasSelection())
|
||||
EraseSelection();
|
||||
else
|
||||
{
|
||||
MoveCursor(-1);
|
||||
Erase(GetGlyphIndex(m_cursorPositionBegin));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case Keyboard::VKey::Delete:
|
||||
{
|
||||
if (HasSelection())
|
||||
EraseSelection();
|
||||
else
|
||||
Erase(GetGlyphIndex(m_cursorPositionBegin));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case Keyboard::VKey::Down:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyDown(this, &ignoreDefaultAction);
|
||||
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
if (HasSelection())
|
||||
SetCursorPosition(m_cursorPositionEnd);
|
||||
|
||||
MoveCursor({0, 1});
|
||||
return true;
|
||||
}
|
||||
|
||||
case Keyboard::VKey::End:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyEnd(this, &ignoreDefaultAction);
|
||||
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
std::size_t lineCount = textDrawer.GetLineCount();
|
||||
if (key.control && lineCount > 0)
|
||||
SetCursorPosition({ static_cast<unsigned int>(textDrawer.GetLineGlyphCount(lineCount - 1)), static_cast<unsigned int>(lineCount - 1) });
|
||||
else
|
||||
SetCursorPosition({ static_cast<unsigned int>(textDrawer.GetLineGlyphCount(m_cursorPositionEnd.y)), m_cursorPositionEnd.y });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case Keyboard::VKey::Home:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyHome(this, &ignoreDefaultAction);
|
||||
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
SetCursorPosition({ 0U, key.control ? 0U : m_cursorPositionEnd.y });
|
||||
return true;
|
||||
}
|
||||
|
||||
case Keyboard::VKey::Left:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyLeft(this, &ignoreDefaultAction);
|
||||
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
if (HasSelection())
|
||||
SetCursorPosition(m_cursorPositionBegin);
|
||||
else if (key.control)
|
||||
HandleWordCursorMove(true);
|
||||
else
|
||||
MoveCursor(-1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case Keyboard::VKey::Return:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyReturn(this, &ignoreDefaultAction);
|
||||
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
if (!m_multiLineEnabled)
|
||||
break;
|
||||
|
||||
if (HasSelection())
|
||||
EraseSelection();
|
||||
|
||||
Write("\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
case Keyboard::VKey::Right:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyRight(this, &ignoreDefaultAction);
|
||||
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
if (HasSelection())
|
||||
SetCursorPosition(m_cursorPositionEnd);
|
||||
else if (key.control)
|
||||
HandleWordCursorMove(false);
|
||||
else
|
||||
MoveCursor(1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case Keyboard::VKey::Up:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyUp(this, &ignoreDefaultAction);
|
||||
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
if (HasSelection())
|
||||
SetCursorPosition(m_cursorPositionBegin);
|
||||
|
||||
MoveCursor({0, -1});
|
||||
return true;
|
||||
}
|
||||
|
||||
case Keyboard::VKey::Tab:
|
||||
{
|
||||
if (!m_tabEnabled)
|
||||
return false;
|
||||
|
||||
if (HasSelection())
|
||||
HandleSelectionIndentation(!key.shift);
|
||||
else
|
||||
HandleIndentation(!key.shift);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::OnKeyReleased(const WindowEvent::KeyEvent& /*key*/)
|
||||
{
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::OnMouseButtonPress(int x, int y, Mouse::Button button)
|
||||
{
|
||||
if (button == Mouse::Left)
|
||||
{
|
||||
SetFocus();
|
||||
|
||||
Vector2ui hoveredGlyph = GetHoveredGlyph(float(x), float(y));
|
||||
|
||||
// Shift extends selection
|
||||
if (Keyboard::IsKeyPressed(Keyboard::VKey::LShift) || Keyboard::IsKeyPressed(Keyboard::VKey::RShift))
|
||||
SetSelection(hoveredGlyph, m_selectionCursor);
|
||||
else
|
||||
{
|
||||
SetCursorPosition(hoveredGlyph);
|
||||
m_selectionCursor = m_cursorPositionBegin;
|
||||
}
|
||||
|
||||
m_isMouseButtonDown = true;
|
||||
}
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::OnMouseButtonRelease(int, int, Mouse::Button button)
|
||||
{
|
||||
if (button == Mouse::Left)
|
||||
m_isMouseButtonDown = false;
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::OnMouseEnter()
|
||||
{
|
||||
if (!Mouse::IsButtonPressed(Mouse::Left))
|
||||
m_isMouseButtonDown = false;
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::OnMouseMoved(int x, int y, int deltaX, int deltaY)
|
||||
{
|
||||
if (m_isMouseButtonDown)
|
||||
SetSelection(m_selectionCursor, GetHoveredGlyph(float(x), float(y)));
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::OnTextEntered(char32_t character, bool /*repeated*/)
|
||||
{
|
||||
if (m_readOnly)
|
||||
return;
|
||||
|
||||
if (Unicode::GetCategory(character) == Unicode::Category_Other_Control || (m_characterFilter && !m_characterFilter(character)))
|
||||
return;
|
||||
|
||||
if (HasSelection())
|
||||
EraseSelection();
|
||||
|
||||
Write(FromUtf32String(std::u32string_view(&character, 1)));
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::RefreshCursor()
|
||||
{
|
||||
if (m_readOnly)
|
||||
return;
|
||||
|
||||
const AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||
|
||||
auto GetGlyph = [&](const Vector2ui& glyphPosition, std::size_t* glyphIndex) -> const AbstractTextDrawer::Glyph*
|
||||
{
|
||||
if (glyphPosition.y >= textDrawer.GetLineCount())
|
||||
return nullptr;
|
||||
|
||||
const auto& lineInfo = textDrawer.GetLine(glyphPosition.y);
|
||||
|
||||
std::size_t cursorGlyph = GetGlyphIndex({ glyphPosition.x, glyphPosition.y });
|
||||
if (glyphIndex)
|
||||
*glyphIndex = cursorGlyph;
|
||||
|
||||
std::size_t glyphCount = textDrawer.GetGlyphCount();
|
||||
if (glyphCount > 0 && lineInfo.glyphIndex < cursorGlyph)
|
||||
{
|
||||
const auto& glyph = textDrawer.GetGlyph(std::min(cursorGlyph, glyphCount - 1));
|
||||
return &glyph;
|
||||
}
|
||||
else
|
||||
return nullptr;
|
||||
};
|
||||
|
||||
auto& registry = GetRegistry();
|
||||
|
||||
// Move text so that cursor always lies in drawer bounds
|
||||
const auto* lastGlyph = GetGlyph(m_cursorPositionEnd, nullptr);
|
||||
float glyphPos = (lastGlyph) ? lastGlyph->bounds.x : 0.f;
|
||||
float glyphWidth = (lastGlyph) ? lastGlyph->bounds.width : 0.f;
|
||||
|
||||
auto& textNode = registry.get<NodeComponent>(m_textEntity);
|
||||
float textPosition = textNode.GetPosition(CoordSys::Local).x - paddingWidth;
|
||||
float cursorPosition = glyphPos + textPosition;
|
||||
float width = GetWidth();
|
||||
|
||||
if (width <= textDrawer.GetBounds().width)
|
||||
{
|
||||
if (cursorPosition + glyphWidth > width)
|
||||
textNode.Move(width - cursorPosition - glyphWidth, 0.f);
|
||||
else if (cursorPosition - glyphWidth < 0.f)
|
||||
textNode.Move(-cursorPosition + glyphWidth, 0.f);
|
||||
}
|
||||
else
|
||||
textNode.Move(-textPosition, 0.f); //< Reset text position if we have enough room to show everything
|
||||
|
||||
// Create/destroy cursor entities and sprites
|
||||
std::size_t selectionLineCount = m_cursorPositionEnd.y - m_cursorPositionBegin.y + 1;
|
||||
std::size_t oldSpriteCount = m_cursors.size();
|
||||
if (m_cursors.size() < selectionLineCount)
|
||||
{
|
||||
m_cursors.resize(selectionLineCount);
|
||||
for (std::size_t i = oldSpriteCount; i < m_cursors.size(); ++i)
|
||||
{
|
||||
m_cursors[i].sprite = std::make_shared<Sprite>(Widgets::Instance()->GetTransparentMaterial());
|
||||
|
||||
m_cursors[i].entity = CreateEntity();
|
||||
registry.emplace<GraphicsComponent>(m_cursors[i].entity, HasFocus()).AttachRenderable(m_cursors[i].sprite);
|
||||
registry.emplace<NodeComponent>(m_cursors[i].entity).SetParent(textNode);
|
||||
}
|
||||
}
|
||||
else if (m_cursors.size() > selectionLineCount)
|
||||
{
|
||||
for (std::size_t i = selectionLineCount; i < m_cursors.size(); ++i)
|
||||
DestroyEntity(m_cursors[i].entity);
|
||||
|
||||
m_cursors.resize(selectionLineCount);
|
||||
}
|
||||
|
||||
// Resize every cursor sprite
|
||||
for (unsigned int i = m_cursorPositionBegin.y; i <= m_cursorPositionEnd.y; ++i)
|
||||
{
|
||||
const auto& lineInfo = textDrawer.GetLine(i);
|
||||
|
||||
auto& cursor = m_cursors[i - m_cursorPositionBegin.y];
|
||||
if (i == m_cursorPositionBegin.y || i == m_cursorPositionEnd.y)
|
||||
{
|
||||
// Partial selection (or no selection)
|
||||
auto GetGlyphPos = [&](const Vector2ui& glyphPosition)
|
||||
{
|
||||
std::size_t glyphIndex;
|
||||
const auto* glyph = GetGlyph(glyphPosition, &glyphIndex);
|
||||
if (glyph)
|
||||
{
|
||||
float position = glyph->bounds.x;
|
||||
if (glyphIndex >= textDrawer.GetGlyphCount())
|
||||
position += glyph->bounds.width;
|
||||
|
||||
return position;
|
||||
}
|
||||
else
|
||||
return 0.f;
|
||||
};
|
||||
|
||||
float beginX = (i == m_cursorPositionBegin.y) ? GetGlyphPos({ m_cursorPositionBegin.x, i }) : 0.f;
|
||||
float endX = (i == m_cursorPositionEnd.y) ? GetGlyphPos({ m_cursorPositionEnd.x, i }) : lineInfo.bounds.width;
|
||||
float spriteSize = std::max(endX - beginX, 1.f);
|
||||
|
||||
cursor.sprite->SetColor((m_cursorPositionBegin == m_cursorPositionEnd) ? Color::Black : Color(0, 0, 0, 50));
|
||||
cursor.sprite->SetSize(Vector2f(spriteSize, lineInfo.bounds.height));
|
||||
|
||||
registry.get<NodeComponent>(cursor.entity).SetPosition(beginX, lineInfo.bounds.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Full line selection
|
||||
cursor.sprite->SetColor(Color(0, 0, 0, 50));
|
||||
cursor.sprite->SetSize(Vector2f(lineInfo.bounds.width, lineInfo.bounds.height));
|
||||
|
||||
registry.get<NodeComponent>(cursor.entity).SetPosition(0.f, lineInfo.bounds.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AbstractTextAreaWidget::UpdateTextSprite()
|
||||
{
|
||||
m_textSprite->Update(GetTextDrawer());
|
||||
SetPreferredSize(Vector2f(m_textSprite->GetAABB().GetLengths()));
|
||||
}
|
||||
}
|
||||
198
src/Nazara/Widgets/RichTextAreaWidget.cpp
Normal file
198
src/Nazara/Widgets/RichTextAreaWidget.cpp
Normal file
@@ -0,0 +1,198 @@
|
||||
// Copyright (C) 2021 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Widgets module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/RichTextAreaWidget.hpp>
|
||||
#include <Nazara/Widgets/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
RichTextAreaWidget::RichTextAreaWidget(BaseWidget* parent) :
|
||||
AbstractTextAreaWidget(parent)
|
||||
{
|
||||
Layout();
|
||||
}
|
||||
|
||||
void RichTextAreaWidget::AppendText(const std::string& text)
|
||||
{
|
||||
//m_text += text;
|
||||
switch (m_echoMode)
|
||||
{
|
||||
case EchoMode::Normal:
|
||||
m_drawer.AppendText(text);
|
||||
break;
|
||||
|
||||
case EchoMode::Hidden:
|
||||
m_drawer.AppendText(std::string(ComputeCharacterCount(text), '*'));
|
||||
break;
|
||||
|
||||
case EchoMode::HiddenExceptLast:
|
||||
{
|
||||
// TODO
|
||||
/*m_drawer.Clear();
|
||||
std::size_t textLength = m_text.GetLength();
|
||||
if (textLength >= 2)
|
||||
{
|
||||
std::size_t lastCharacterPosition = m_text.GetCharacterPosition(textLength - 2);
|
||||
if (lastCharacterPosition != std::string::npos)
|
||||
m_drawer.AppendText(std::string(textLength - 1, '*'));
|
||||
}
|
||||
|
||||
if (textLength >= 1)
|
||||
m_drawer.AppendText(m_text.SubString(m_text.GetCharacterPosition(textLength - 1)));*/
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateTextSprite();
|
||||
|
||||
//OnTextChanged(this, m_text);
|
||||
}
|
||||
|
||||
void RichTextAreaWidget::Clear()
|
||||
{
|
||||
AbstractTextAreaWidget::Clear();
|
||||
}
|
||||
|
||||
void RichTextAreaWidget::Erase(std::size_t firstGlyph, std::size_t lastGlyph)
|
||||
{
|
||||
if (firstGlyph > lastGlyph)
|
||||
std::swap(firstGlyph, lastGlyph);
|
||||
|
||||
std::size_t textLength = m_drawer.GetGlyphCount();
|
||||
if (firstGlyph > textLength)
|
||||
return;
|
||||
|
||||
std::size_t firstBlock = m_drawer.FindBlock(firstGlyph);
|
||||
std::size_t lastBlock = m_drawer.FindBlock((lastGlyph > 0) ? lastGlyph - 1 : lastGlyph);
|
||||
if (firstBlock == lastBlock)
|
||||
{
|
||||
const std::string& blockText = m_drawer.GetBlockText(firstBlock);
|
||||
std::size_t blockFirstGlyph = m_drawer.GetBlockFirstGlyphIndex(firstBlock);
|
||||
|
||||
std::string newText;
|
||||
if (firstGlyph > blockFirstGlyph)
|
||||
{
|
||||
std::size_t characterPosition = GetCharacterPosition(blockText, firstGlyph - blockFirstGlyph);
|
||||
NazaraAssert(characterPosition != std::string::npos, "Invalid character position");
|
||||
|
||||
newText.append(blockText.substr(0, characterPosition - 1));
|
||||
}
|
||||
|
||||
if (lastGlyph < textLength)
|
||||
newText.append(blockText.substr(GetCharacterPosition(blockText, lastGlyph - blockFirstGlyph)));
|
||||
|
||||
if (!newText.empty())
|
||||
m_drawer.SetBlockText(firstBlock, std::move(newText));
|
||||
else
|
||||
m_drawer.RemoveBlock(firstBlock);
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::string& lastBlockText = m_drawer.GetBlockText(lastBlock);
|
||||
std::size_t lastBlockGlyphIndex = m_drawer.GetBlockFirstGlyphIndex(lastBlock);
|
||||
|
||||
// First, update/delete last block
|
||||
std::size_t lastCharPos = GetCharacterPosition(lastBlockText, lastGlyph - lastBlockGlyphIndex);
|
||||
if (lastCharPos != std::string::npos)
|
||||
{
|
||||
std::string newText = lastBlockText.substr(lastCharPos);
|
||||
if (!newText.empty())
|
||||
m_drawer.SetBlockText(lastBlock, std::move(newText));
|
||||
else
|
||||
m_drawer.RemoveBlock(lastBlock);
|
||||
}
|
||||
|
||||
// And then remove all middle blocks, remove in reverse order because of index shifting
|
||||
assert(lastBlock > 0);
|
||||
for (std::size_t i = lastBlock - 1; i > firstBlock; --i)
|
||||
m_drawer.RemoveBlock(i);
|
||||
|
||||
const std::string& firstBlockText = m_drawer.GetBlockText(firstBlock);
|
||||
std::size_t firstBlockGlyphIndex = m_drawer.GetBlockFirstGlyphIndex(firstBlock);
|
||||
|
||||
// And finally update/delete first block
|
||||
if (firstGlyph > firstBlockGlyphIndex)
|
||||
{
|
||||
std::size_t firstCharPos = GetCharacterPosition(firstBlockText, firstGlyph - firstBlockGlyphIndex - 1);
|
||||
if (firstCharPos != std::string::npos)
|
||||
{
|
||||
std::string newText = firstBlockText.substr(0, firstCharPos);
|
||||
if (!newText.empty())
|
||||
m_drawer.SetBlockText(firstBlock, std::move(newText));
|
||||
else
|
||||
m_drawer.RemoveBlock(firstBlock);
|
||||
}
|
||||
}
|
||||
else
|
||||
m_drawer.RemoveBlock(firstBlock);
|
||||
}
|
||||
|
||||
UpdateDisplayText();
|
||||
}
|
||||
|
||||
void RichTextAreaWidget::Write(const std::string& text, std::size_t glyphPosition)
|
||||
{
|
||||
if (m_drawer.HasBlocks())
|
||||
{
|
||||
auto block = m_drawer.GetBlock(m_drawer.FindBlock((glyphPosition > 0) ? glyphPosition - 1 : glyphPosition));
|
||||
std::size_t firstGlyph = block.GetFirstGlyphIndex();
|
||||
assert(glyphPosition >= firstGlyph);
|
||||
|
||||
std::string blockText = block.GetText();
|
||||
std::size_t characterPosition = GetCharacterPosition(blockText, glyphPosition - firstGlyph);
|
||||
blockText.insert(characterPosition, text);
|
||||
|
||||
block.SetText(blockText);
|
||||
}
|
||||
else
|
||||
m_drawer.AppendText(text);
|
||||
|
||||
SetCursorPosition(glyphPosition + ComputeCharacterCount(text));
|
||||
|
||||
UpdateDisplayText();
|
||||
}
|
||||
|
||||
AbstractTextDrawer& RichTextAreaWidget::GetTextDrawer()
|
||||
{
|
||||
return m_drawer;
|
||||
}
|
||||
|
||||
const AbstractTextDrawer& RichTextAreaWidget::GetTextDrawer() const
|
||||
{
|
||||
return m_drawer;
|
||||
}
|
||||
|
||||
void RichTextAreaWidget::HandleIndentation(bool add)
|
||||
{
|
||||
}
|
||||
|
||||
void RichTextAreaWidget::HandleSelectionIndentation(bool add)
|
||||
{
|
||||
}
|
||||
|
||||
void RichTextAreaWidget::HandleWordCursorMove(bool left)
|
||||
{
|
||||
}
|
||||
|
||||
void RichTextAreaWidget::UpdateDisplayText()
|
||||
{
|
||||
/*m_drawer.Clear();
|
||||
switch (m_echoMode)
|
||||
{
|
||||
case EchoMode_Normal:
|
||||
m_drawer.AppendText(m_text);
|
||||
break;
|
||||
|
||||
case EchoMode_Password:
|
||||
case EchoMode_PasswordExceptLast:
|
||||
m_drawer.AppendText(std::string(m_text.GetLength(), '*'));
|
||||
break;
|
||||
}*/
|
||||
|
||||
UpdateTextSprite();
|
||||
|
||||
SetCursorPosition(m_cursorPositionBegin); //< Refresh cursor position (prevent it from being outside of the text)
|
||||
}
|
||||
}
|
||||
255
src/Nazara/Widgets/TextAreaWidget.cpp
Normal file
255
src/Nazara/Widgets/TextAreaWidget.cpp
Normal file
@@ -0,0 +1,255 @@
|
||||
// Copyright (C) 2021 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Widgets module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/TextAreaWidget.hpp>
|
||||
#include <Nazara/Core/Unicode.hpp>
|
||||
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
|
||||
#include <Nazara/Utility/Font.hpp>
|
||||
#include <Nazara/Utility/Components/NodeComponent.hpp>
|
||||
#include <Nazara/Widgets/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
TextAreaWidget::TextAreaWidget(BaseWidget* parent) :
|
||||
AbstractTextAreaWidget(parent)
|
||||
{
|
||||
SetCharacterSize(GetCharacterSize()); //< Actualize minimum / preferred size
|
||||
|
||||
Layout();
|
||||
}
|
||||
|
||||
void TextAreaWidget::AppendText(const std::string& text)
|
||||
{
|
||||
m_text += text;
|
||||
|
||||
switch (m_echoMode)
|
||||
{
|
||||
case EchoMode::Normal:
|
||||
m_drawer.AppendText(text);
|
||||
break;
|
||||
|
||||
case EchoMode::Hidden:
|
||||
m_drawer.AppendText(std::string(ComputeCharacterCount(text), '*'));
|
||||
break;
|
||||
|
||||
case EchoMode::HiddenExceptLast:
|
||||
{
|
||||
m_drawer.Clear();
|
||||
std::size_t textLength = ComputeCharacterCount(m_text);
|
||||
if (textLength >= 2)
|
||||
{
|
||||
std::size_t lastCharacterPosition = GetCharacterPosition(m_text, textLength - 2);
|
||||
if (lastCharacterPosition != std::string::npos)
|
||||
m_drawer.AppendText(std::string(textLength - 1, '*'));
|
||||
}
|
||||
|
||||
if (textLength >= 1)
|
||||
m_drawer.AppendText(m_text.substr(GetCharacterPosition(m_text, textLength - 1))); //< FIXME: getting last character position could be massively optimized
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateTextSprite();
|
||||
|
||||
OnTextChanged(this, m_text);
|
||||
}
|
||||
|
||||
void TextAreaWidget::Clear()
|
||||
{
|
||||
AbstractTextAreaWidget::Clear();
|
||||
|
||||
m_text.clear();
|
||||
OnTextChanged(this, m_text);
|
||||
}
|
||||
|
||||
void TextAreaWidget::Erase(std::size_t firstGlyph, std::size_t lastGlyph)
|
||||
{
|
||||
if (firstGlyph > lastGlyph)
|
||||
std::swap(firstGlyph, lastGlyph);
|
||||
|
||||
std::size_t textLength = ComputeCharacterCount(m_text);
|
||||
if (firstGlyph > textLength)
|
||||
return;
|
||||
|
||||
std::string newText;
|
||||
if (firstGlyph > 0)
|
||||
{
|
||||
std::size_t characterPosition = GetCharacterPosition(m_text, firstGlyph);
|
||||
NazaraAssert(characterPosition != std::string::npos, "Invalid character position");
|
||||
|
||||
newText.append(m_text.substr(0, characterPosition));
|
||||
}
|
||||
|
||||
if (lastGlyph < textLength)
|
||||
{
|
||||
std::size_t characterPosition = GetCharacterPosition(m_text, lastGlyph);
|
||||
NazaraAssert(characterPosition != std::string::npos, "Invalid character position");
|
||||
|
||||
newText.append(m_text.substr(characterPosition));
|
||||
}
|
||||
|
||||
SetText(newText);
|
||||
}
|
||||
|
||||
void TextAreaWidget::Write(const std::string& text, std::size_t glyphPosition)
|
||||
{
|
||||
if (glyphPosition >= m_drawer.GetGlyphCount())
|
||||
{
|
||||
// It's faster to append than to insert in the middle
|
||||
AppendText(text);
|
||||
SetCursorPosition(m_drawer.GetGlyphCount());
|
||||
}
|
||||
else
|
||||
{
|
||||
m_text.insert(GetCharacterPosition(m_text, glyphPosition), text);
|
||||
SetText(m_text);
|
||||
|
||||
SetCursorPosition(glyphPosition + ComputeCharacterCount(text));
|
||||
}
|
||||
}
|
||||
|
||||
AbstractTextDrawer& TextAreaWidget::GetTextDrawer()
|
||||
{
|
||||
return m_drawer;
|
||||
}
|
||||
|
||||
const AbstractTextDrawer& TextAreaWidget::GetTextDrawer() const
|
||||
{
|
||||
return m_drawer;
|
||||
}
|
||||
|
||||
void TextAreaWidget::HandleIndentation(bool add)
|
||||
{
|
||||
if (add)
|
||||
Write("\t");
|
||||
else
|
||||
{
|
||||
std::size_t currentGlyph = GetGlyphIndex(m_cursorPositionBegin);
|
||||
|
||||
if (currentGlyph > 0 && m_text[GetCharacterPosition(m_text, currentGlyph - 1U)] == '\t') // Check if previous glyph is a tab
|
||||
{
|
||||
Erase(currentGlyph - 1U);
|
||||
|
||||
if (m_cursorPositionBegin.x < static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionBegin.y)))
|
||||
MoveCursor(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextAreaWidget::HandleSelectionIndentation(bool add)
|
||||
{
|
||||
for (unsigned line = m_cursorPositionBegin.y; line <= m_cursorPositionEnd.y; ++line)
|
||||
{
|
||||
const Vector2ui cursorPositionBegin = m_cursorPositionBegin;
|
||||
const Vector2ui cursorPositionEnd = m_cursorPositionEnd;
|
||||
|
||||
if (add)
|
||||
{
|
||||
Write("\t", {0U, line});
|
||||
SetSelection(cursorPositionBegin + (cursorPositionBegin.y == line && cursorPositionBegin.x != 0U ? Vector2ui{ 1U, 0U } : Vector2ui{}),
|
||||
cursorPositionEnd + (cursorPositionEnd.y == line ? Vector2ui{ 1U, 0U } : Vector2ui{}));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (m_drawer.GetLineGlyphCount(line) == 0)
|
||||
continue;
|
||||
|
||||
std::size_t firstGlyph = GetGlyphIndex({ 0U, line });
|
||||
|
||||
if (m_text[GetCharacterPosition(m_text, firstGlyph)] == '\t')
|
||||
{
|
||||
Erase(firstGlyph);
|
||||
SetSelection(cursorPositionBegin - (cursorPositionBegin.y == line && cursorPositionBegin.x != 0U ? Vector2ui{ 1U, 0U } : Vector2ui{}),
|
||||
cursorPositionEnd - (cursorPositionEnd.y == line && cursorPositionEnd.x != 0U ? Vector2ui{ 1U, 0U } : Vector2ui{}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextAreaWidget::HandleWordCursorMove(bool left)
|
||||
{
|
||||
if (left)
|
||||
{
|
||||
std::size_t index = GetGlyphIndex(m_cursorPositionBegin);
|
||||
if (index == 0)
|
||||
return;
|
||||
|
||||
std::size_t spaceIndex = m_text.rfind(' ', index - 2);
|
||||
std::size_t endlIndex = m_text.rfind('\n', index - 1);
|
||||
|
||||
if ((spaceIndex > endlIndex || endlIndex == std::string::npos) && spaceIndex != std::string::npos)
|
||||
SetCursorPosition(spaceIndex + 1);
|
||||
else if (endlIndex != std::string::npos)
|
||||
{
|
||||
if (index == endlIndex + 1)
|
||||
SetCursorPosition(endlIndex);
|
||||
else
|
||||
SetCursorPosition(endlIndex + 1);
|
||||
}
|
||||
else
|
||||
SetCursorPosition({ 0U, m_cursorPositionBegin.y });
|
||||
}
|
||||
else
|
||||
{
|
||||
std::size_t index = GetGlyphIndex(m_cursorPositionEnd);
|
||||
std::size_t spaceIndex = m_text.find(' ', index);
|
||||
std::size_t endlIndex = m_text.find('\n', index);
|
||||
|
||||
if (spaceIndex < endlIndex && spaceIndex != std::string::npos)
|
||||
{
|
||||
if (ComputeCharacterCount(m_text) > spaceIndex)
|
||||
SetCursorPosition(spaceIndex + 1);
|
||||
else
|
||||
SetCursorPosition({ static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionEnd.y)), m_cursorPositionEnd.y });
|
||||
}
|
||||
else if (endlIndex != std::string::npos)
|
||||
{
|
||||
if (index == endlIndex)
|
||||
SetCursorPosition(endlIndex + 1);
|
||||
else
|
||||
SetCursorPosition(endlIndex);
|
||||
}
|
||||
else
|
||||
SetCursorPosition({ static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionEnd.y)), m_cursorPositionEnd.y });
|
||||
}
|
||||
}
|
||||
|
||||
void TextAreaWidget::UpdateDisplayText()
|
||||
{
|
||||
switch (m_echoMode)
|
||||
{
|
||||
case EchoMode::Normal:
|
||||
m_drawer.SetText(m_text);
|
||||
break;
|
||||
|
||||
case EchoMode::Hidden:
|
||||
case EchoMode::HiddenExceptLast:
|
||||
m_drawer.SetText(std::string(ComputeCharacterCount(m_text), '*'));
|
||||
break;
|
||||
}
|
||||
|
||||
UpdateTextSprite();
|
||||
|
||||
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)
|
||||
{
|
||||
const std::shared_ptr<Font>& font = m_drawer.GetFont(i);
|
||||
|
||||
const Font::SizeInfo& sizeInfo = font->GetSizeInfo(m_drawer.GetCharacterSize());
|
||||
lineHeight = std::max(lineHeight, m_drawer.GetLineHeight());
|
||||
spaceAdvance = std::max(spaceAdvance, sizeInfo.spaceAdvance);
|
||||
}
|
||||
|
||||
Vector2f size = { float(spaceAdvance), lineHeight + 5.f };
|
||||
SetMinimumSize(size);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user