Merge branch 'master' of https://github.com/DigitalPulseSoftware/NazaraEngine
This commit is contained in:
commit
3290c497e7
|
|
@ -283,6 +283,9 @@ Nazara Development Kit:
|
||||||
- ⚠️ TextAreaWidget::OnTextAreaCursorMove signal now uses a Vector2ui* position as its second argument (instead of a std::size_t*)
|
- ⚠️ TextAreaWidget::OnTextAreaCursorMove signal now uses a Vector2ui* position as its second argument (instead of a std::size_t*)
|
||||||
- Added TextAreaWidget::OnTextAreaSelection
|
- Added TextAreaWidget::OnTextAreaSelection
|
||||||
- ⚠️ Console class is no longer bound to a LuaState and now has a OnCommand signal
|
- ⚠️ Console class is no longer bound to a LuaState and now has a OnCommand signal
|
||||||
|
- ⚠️ Made AbstractTextAreaWidget which is inherited by TextAreaWidget
|
||||||
|
- ⚠️ Added RichTextAreaWidget
|
||||||
|
- ⚠️ Console now supports text color in history
|
||||||
|
|
||||||
# 0.4:
|
# 0.4:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,10 @@ namespace Nz
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
{
|
{
|
||||||
|
class AbstractTextAreaWidget;
|
||||||
class Console;
|
class Console;
|
||||||
class Entity;
|
class Entity;
|
||||||
|
class RichTextAreaWidget;
|
||||||
class ScrollAreaWidget;
|
class ScrollAreaWidget;
|
||||||
class TextAreaWidget;
|
class TextAreaWidget;
|
||||||
|
|
||||||
|
|
@ -45,7 +47,7 @@ namespace Ndk
|
||||||
void ClearFocus();
|
void ClearFocus();
|
||||||
|
|
||||||
inline unsigned int GetCharacterSize() const;
|
inline unsigned int GetCharacterSize() const;
|
||||||
inline const TextAreaWidget* GetHistory() const;
|
inline const RichTextAreaWidget* GetHistory() const;
|
||||||
inline const TextAreaWidget* GetInput() const;
|
inline const TextAreaWidget* GetInput() const;
|
||||||
inline const Nz::FontRef& GetTextFont() const;
|
inline const Nz::FontRef& GetTextFont() const;
|
||||||
|
|
||||||
|
|
@ -59,7 +61,7 @@ namespace Ndk
|
||||||
NazaraSignal(OnCommand, Console* /*console*/, const Nz::String& /*command*/);
|
NazaraSignal(OnCommand, Console* /*console*/, const Nz::String& /*command*/);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void ExecuteInput(const TextAreaWidget* textArea, bool* ignoreDefaultAction);
|
void ExecuteInput(const AbstractTextAreaWidget* textArea, bool* ignoreDefaultAction);
|
||||||
void Layout() override;
|
void Layout() override;
|
||||||
|
|
||||||
struct Line
|
struct Line
|
||||||
|
|
@ -72,7 +74,7 @@ namespace Ndk
|
||||||
std::vector<Nz::String> m_commandHistory;
|
std::vector<Nz::String> m_commandHistory;
|
||||||
std::vector<Line> m_historyLines;
|
std::vector<Line> m_historyLines;
|
||||||
ScrollAreaWidget* m_historyArea;
|
ScrollAreaWidget* m_historyArea;
|
||||||
TextAreaWidget* m_history;
|
RichTextAreaWidget* m_history;
|
||||||
TextAreaWidget* m_input;
|
TextAreaWidget* m_input;
|
||||||
Nz::FontRef m_defaultFont;
|
Nz::FontRef m_defaultFont;
|
||||||
unsigned int m_characterSize;
|
unsigned int m_characterSize;
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ namespace Ndk
|
||||||
* \return History of the console
|
* \return History of the console
|
||||||
*/
|
*/
|
||||||
|
|
||||||
inline const TextAreaWidget* Console::GetHistory() const
|
inline const RichTextAreaWidget* Console::GetHistory() const
|
||||||
{
|
{
|
||||||
return m_history;
|
return m_history;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#ifndef NDK_WIDGETS_GLOBAL_HPP
|
#ifndef NDK_WIDGETS_GLOBAL_HPP
|
||||||
#define NDK_WIDGETS_GLOBAL_HPP
|
#define NDK_WIDGETS_GLOBAL_HPP
|
||||||
|
|
||||||
|
#include <NDK/Widgets/AbstractTextAreaWidget.hpp>
|
||||||
#include <NDK/Widgets/BoxLayout.hpp>
|
#include <NDK/Widgets/BoxLayout.hpp>
|
||||||
#include <NDK/Widgets/ButtonWidget.hpp>
|
#include <NDK/Widgets/ButtonWidget.hpp>
|
||||||
#include <NDK/Widgets/CheckboxWidget.hpp>
|
#include <NDK/Widgets/CheckboxWidget.hpp>
|
||||||
|
|
@ -12,6 +13,7 @@
|
||||||
#include <NDK/Widgets/ImageWidget.hpp>
|
#include <NDK/Widgets/ImageWidget.hpp>
|
||||||
#include <NDK/Widgets/LabelWidget.hpp>
|
#include <NDK/Widgets/LabelWidget.hpp>
|
||||||
#include <NDK/Widgets/ProgressBarWidget.hpp>
|
#include <NDK/Widgets/ProgressBarWidget.hpp>
|
||||||
|
#include <NDK/Widgets/RichTextAreaWidget.hpp>
|
||||||
#include <NDK/Widgets/ScrollAreaWidget.hpp>
|
#include <NDK/Widgets/ScrollAreaWidget.hpp>
|
||||||
#include <NDK/Widgets/TextAreaWidget.hpp>
|
#include <NDK/Widgets/TextAreaWidget.hpp>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,135 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NDK_WIDGETS_ABSTRACTTEXTAREAWIDGET_HPP
|
||||||
|
#define NDK_WIDGETS_ABSTRACTTEXTAREAWIDGET_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Graphics/TextSprite.hpp>
|
||||||
|
#include <Nazara/Utility/AbstractTextDrawer.hpp>
|
||||||
|
#include <NDK/BaseWidget.hpp>
|
||||||
|
#include <NDK/Widgets/Enums.hpp>
|
||||||
|
#include <functional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
class NDK_API AbstractTextAreaWidget : public BaseWidget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using CharacterFilter = std::function<bool(char32_t)>;
|
||||||
|
|
||||||
|
AbstractTextAreaWidget(BaseWidget* parent);
|
||||||
|
AbstractTextAreaWidget(const AbstractTextAreaWidget&) = delete;
|
||||||
|
AbstractTextAreaWidget(AbstractTextAreaWidget&&) = default;
|
||||||
|
~AbstractTextAreaWidget() = default;
|
||||||
|
|
||||||
|
virtual void Clear();
|
||||||
|
|
||||||
|
//virtual TextAreaWidget* Clone() const = 0;
|
||||||
|
|
||||||
|
void EnableLineWrap(bool enable = true);
|
||||||
|
inline void EnableMultiline(bool enable = true);
|
||||||
|
inline void EnableTabWriting(bool enable = true);
|
||||||
|
|
||||||
|
inline void Erase(std::size_t glyphPosition);
|
||||||
|
virtual void Erase(std::size_t firstGlyph, std::size_t lastGlyph) = 0;
|
||||||
|
inline void EraseSelection();
|
||||||
|
|
||||||
|
inline const CharacterFilter& GetCharacterFilter() const;
|
||||||
|
inline const Nz::Vector2ui& GetCursorPosition() const;
|
||||||
|
inline Nz::Vector2ui GetCursorPosition(std::size_t glyphIndex) const;
|
||||||
|
inline EchoMode GetEchoMode() const;
|
||||||
|
inline std::size_t GetGlyphIndex() const;
|
||||||
|
inline std::size_t GetGlyphIndex(const Nz::Vector2ui& cursorPosition) const;
|
||||||
|
inline const Nz::String& GetText() const;
|
||||||
|
|
||||||
|
Nz::Vector2ui GetHoveredGlyph(float x, float y) const;
|
||||||
|
|
||||||
|
inline bool HasSelection() const;
|
||||||
|
|
||||||
|
inline bool IsLineWrapEnabled() const;
|
||||||
|
inline bool IsMultilineEnabled() const;
|
||||||
|
inline bool IsReadOnly() const;
|
||||||
|
inline bool IsTabWritingEnabled() const;
|
||||||
|
|
||||||
|
inline void MoveCursor(int offset);
|
||||||
|
inline void MoveCursor(const Nz::Vector2i& offset);
|
||||||
|
|
||||||
|
inline Nz::Vector2ui NormalizeCursorPosition(Nz::Vector2ui cursorPosition) const;
|
||||||
|
|
||||||
|
inline void SetCharacterFilter(CharacterFilter filter);
|
||||||
|
inline void SetCursorPosition(std::size_t glyphIndex);
|
||||||
|
inline void SetCursorPosition(Nz::Vector2ui cursorPosition);
|
||||||
|
inline void SetEchoMode(EchoMode echoMode);
|
||||||
|
inline void SetReadOnly(bool readOnly = true);
|
||||||
|
inline void SetSelection(Nz::Vector2ui fromPosition, Nz::Vector2ui toPosition);
|
||||||
|
|
||||||
|
inline void Write(const Nz::String& text);
|
||||||
|
inline void Write(const Nz::String& text, const Nz::Vector2ui& glyphPosition);
|
||||||
|
virtual void Write(const Nz::String& text, std::size_t glyphPosition) = 0;
|
||||||
|
|
||||||
|
AbstractTextAreaWidget& operator=(const AbstractTextAreaWidget&) = delete;
|
||||||
|
AbstractTextAreaWidget& operator=(AbstractTextAreaWidget&&) = default;
|
||||||
|
|
||||||
|
NazaraSignal(OnTextAreaCursorMove, const AbstractTextAreaWidget* /*textArea*/, Nz::Vector2ui* /*newCursorPosition*/);
|
||||||
|
NazaraSignal(OnTextAreaKeyBackspace, const AbstractTextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||||
|
NazaraSignal(OnTextAreaKeyDown, const AbstractTextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||||
|
NazaraSignal(OnTextAreaKeyEnd, const AbstractTextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||||
|
NazaraSignal(OnTextAreaKeyHome, const AbstractTextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||||
|
NazaraSignal(OnTextAreaKeyLeft, const AbstractTextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||||
|
NazaraSignal(OnTextAreaKeyReturn, const AbstractTextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||||
|
NazaraSignal(OnTextAreaKeyRight, const AbstractTextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||||
|
NazaraSignal(OnTextAreaKeyUp, const AbstractTextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||||
|
NazaraSignal(OnTextAreaSelection, const AbstractTextAreaWidget* /*textArea*/, Nz::Vector2ui* /*start*/, Nz::Vector2ui* /*end*/);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual Nz::AbstractTextDrawer& GetTextDrawer() = 0;
|
||||||
|
virtual const Nz::AbstractTextDrawer& GetTextDrawer() const = 0;
|
||||||
|
|
||||||
|
void Layout() override;
|
||||||
|
|
||||||
|
virtual void HandleIndentation(bool add) = 0;
|
||||||
|
virtual void HandleSelectionIndentation(bool add) = 0;
|
||||||
|
virtual void HandleWordCursorMove(bool left) = 0;
|
||||||
|
|
||||||
|
bool IsFocusable() const override;
|
||||||
|
void OnFocusLost() override;
|
||||||
|
void OnFocusReceived() override;
|
||||||
|
bool OnKeyPressed(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 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;
|
||||||
|
|
||||||
|
inline void SetCursorPositionInternal(std::size_t glyphIndex);
|
||||||
|
inline void SetCursorPositionInternal(Nz::Vector2ui cursorPosition);
|
||||||
|
|
||||||
|
void RefreshCursor();
|
||||||
|
virtual void UpdateDisplayText() = 0;
|
||||||
|
void UpdateTextSprite();
|
||||||
|
|
||||||
|
CharacterFilter m_characterFilter;
|
||||||
|
EchoMode m_echoMode;
|
||||||
|
EntityHandle m_cursorEntity;
|
||||||
|
EntityHandle m_textEntity;
|
||||||
|
Nz::TextSpriteRef m_textSprite;
|
||||||
|
Nz::Vector2ui m_cursorPositionBegin;
|
||||||
|
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;
|
||||||
|
bool m_tabEnabled; // writes (Shift+)Tab character if set to true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <NDK/Widgets/AbstractTextAreaWidget.inl>
|
||||||
|
|
||||||
|
#endif // NDK_WIDGETS_ABSTRACTTEXTAREAWIDGET_HPP
|
||||||
|
|
@ -0,0 +1,252 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#include <NDK/Widgets/AbstractTextAreaWidget.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
inline void AbstractTextAreaWidget::EnableMultiline(bool enable)
|
||||||
|
{
|
||||||
|
m_multiLineEnabled = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::EnableTabWriting(bool enable)
|
||||||
|
{
|
||||||
|
m_tabEnabled = enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::Erase(std::size_t glyphPosition)
|
||||||
|
{
|
||||||
|
Erase(glyphPosition, glyphPosition + 1U);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::EraseSelection()
|
||||||
|
{
|
||||||
|
if (!HasSelection())
|
||||||
|
return;
|
||||||
|
|
||||||
|
Erase(GetGlyphIndex(m_cursorPositionBegin), GetGlyphIndex(m_cursorPositionEnd));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const AbstractTextAreaWidget::CharacterFilter& AbstractTextAreaWidget::GetCharacterFilter() const
|
||||||
|
{
|
||||||
|
return m_characterFilter;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const Nz::Vector2ui& AbstractTextAreaWidget::GetCursorPosition() const
|
||||||
|
{
|
||||||
|
return m_cursorPositionBegin;
|
||||||
|
}
|
||||||
|
|
||||||
|
Nz::Vector2ui AbstractTextAreaWidget::GetCursorPosition(std::size_t glyphIndex) const
|
||||||
|
{
|
||||||
|
const Nz::AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||||
|
|
||||||
|
glyphIndex = std::min(glyphIndex, GetTextDrawer().GetGlyphCount());
|
||||||
|
|
||||||
|
std::size_t lineCount = textDrawer.GetLineCount();
|
||||||
|
std::size_t line = 0U;
|
||||||
|
for (std::size_t i = line + 1; i < lineCount; ++i)
|
||||||
|
{
|
||||||
|
if (textDrawer.GetLine(i).glyphIndex > glyphIndex)
|
||||||
|
break;
|
||||||
|
|
||||||
|
line = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto& lineInfo = textDrawer.GetLine(line);
|
||||||
|
|
||||||
|
Nz::Vector2ui cursorPos;
|
||||||
|
cursorPos.y = static_cast<unsigned int>(line);
|
||||||
|
cursorPos.x = static_cast<unsigned int>(glyphIndex - lineInfo.glyphIndex);
|
||||||
|
|
||||||
|
return cursorPos;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline EchoMode AbstractTextAreaWidget::GetEchoMode() const
|
||||||
|
{
|
||||||
|
return m_echoMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::size_t AbstractTextAreaWidget::GetGlyphIndex() const
|
||||||
|
{
|
||||||
|
return GetGlyphIndex(m_cursorPositionBegin);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::size_t AbstractTextAreaWidget::GetGlyphIndex(const Nz::Vector2ui& cursorPosition) const
|
||||||
|
{
|
||||||
|
const Nz::AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||||
|
|
||||||
|
std::size_t glyphIndex = textDrawer.GetLine(cursorPosition.y).glyphIndex + cursorPosition.x;
|
||||||
|
if (textDrawer.GetLineCount() > cursorPosition.y + 1)
|
||||||
|
glyphIndex = std::min(glyphIndex, textDrawer.GetLine(cursorPosition.y + 1).glyphIndex - 1);
|
||||||
|
else
|
||||||
|
glyphIndex = std::min(glyphIndex, textDrawer.GetGlyphCount());
|
||||||
|
|
||||||
|
return glyphIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool AbstractTextAreaWidget::HasSelection() const
|
||||||
|
{
|
||||||
|
return m_cursorPositionBegin != m_cursorPositionEnd;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool AbstractTextAreaWidget::IsLineWrapEnabled() const
|
||||||
|
{
|
||||||
|
return m_isLineWrapEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool AbstractTextAreaWidget::IsMultilineEnabled() const
|
||||||
|
{
|
||||||
|
return m_multiLineEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool AbstractTextAreaWidget::IsTabWritingEnabled() const
|
||||||
|
{
|
||||||
|
return m_tabEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool AbstractTextAreaWidget::IsReadOnly() const
|
||||||
|
{
|
||||||
|
return m_readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::MoveCursor(int offset)
|
||||||
|
{
|
||||||
|
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPositionBegin);
|
||||||
|
if (offset >= 0)
|
||||||
|
SetCursorPosition(cursorGlyph + static_cast<std::size_t>(offset));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::size_t nOffset = static_cast<std::size_t>(-offset);
|
||||||
|
if (nOffset >= cursorGlyph)
|
||||||
|
SetCursorPosition(0);
|
||||||
|
else
|
||||||
|
SetCursorPosition(cursorGlyph - nOffset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::MoveCursor(const Nz::Vector2i& offset)
|
||||||
|
{
|
||||||
|
auto ClampOffset = [] (unsigned int cursorPosition, int cursorOffset) -> unsigned int
|
||||||
|
{
|
||||||
|
if (cursorOffset >= 0)
|
||||||
|
return cursorPosition + cursorOffset;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
unsigned int nOffset = static_cast<unsigned int>(-cursorOffset);
|
||||||
|
if (nOffset >= cursorPosition)
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return cursorPosition - nOffset;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Nz::Vector2ui cursorPosition = m_cursorPositionBegin;
|
||||||
|
cursorPosition.x = ClampOffset(static_cast<unsigned int>(cursorPosition.x), offset.x);
|
||||||
|
cursorPosition.y = ClampOffset(static_cast<unsigned int>(cursorPosition.y), offset.y);
|
||||||
|
|
||||||
|
SetCursorPosition(cursorPosition);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Nz::Vector2ui AbstractTextAreaWidget::NormalizeCursorPosition(Nz::Vector2ui cursorPosition) const
|
||||||
|
{
|
||||||
|
const Nz::AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||||
|
|
||||||
|
std::size_t lineCount = textDrawer.GetLineCount();
|
||||||
|
if (cursorPosition.y >= lineCount)
|
||||||
|
cursorPosition.y = static_cast<unsigned int>(lineCount - 1);
|
||||||
|
|
||||||
|
const auto& lineInfo = textDrawer.GetLine(cursorPosition.y);
|
||||||
|
if (cursorPosition.y + 1 < lineCount)
|
||||||
|
{
|
||||||
|
const auto& nextLineInfo = textDrawer.GetLine(cursorPosition.y + 1);
|
||||||
|
cursorPosition.x = std::min(cursorPosition.x, static_cast<unsigned int>(nextLineInfo.glyphIndex - lineInfo.glyphIndex - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
return cursorPosition;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::SetCharacterFilter(CharacterFilter filter)
|
||||||
|
{
|
||||||
|
m_characterFilter = std::move(filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::SetCursorPosition(std::size_t glyphIndex)
|
||||||
|
{
|
||||||
|
Nz::Vector2ui position = GetCursorPosition(glyphIndex);
|
||||||
|
Nz::Vector2ui newPosition = position;
|
||||||
|
|
||||||
|
OnTextAreaCursorMove(this, &newPosition);
|
||||||
|
|
||||||
|
if (position == newPosition)
|
||||||
|
SetCursorPositionInternal(position);
|
||||||
|
else
|
||||||
|
SetCursorPositionInternal(GetGlyphIndex(newPosition));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::SetCursorPosition(Nz::Vector2ui cursorPosition)
|
||||||
|
{
|
||||||
|
OnTextAreaCursorMove(this, &cursorPosition);
|
||||||
|
|
||||||
|
return SetCursorPositionInternal(NormalizeCursorPosition(cursorPosition));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::SetEchoMode(EchoMode echoMode)
|
||||||
|
{
|
||||||
|
m_echoMode = echoMode;
|
||||||
|
|
||||||
|
UpdateDisplayText();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::SetReadOnly(bool readOnly)
|
||||||
|
{
|
||||||
|
m_readOnly = readOnly;
|
||||||
|
m_cursorEntity->Enable(!m_readOnly && HasFocus());
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::SetSelection(Nz::Vector2ui fromPosition, Nz::Vector2ui toPosition)
|
||||||
|
{
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
OnTextAreaSelection(this, &fromPosition, &toPosition);
|
||||||
|
|
||||||
|
// Ensure begin is before end a second time (in case signal changed it)
|
||||||
|
if (toPosition.y < fromPosition.y || (toPosition.y == fromPosition.y && toPosition.x < fromPosition.x))
|
||||||
|
std::swap(fromPosition, toPosition);
|
||||||
|
|
||||||
|
m_cursorPositionBegin = NormalizeCursorPosition(fromPosition);
|
||||||
|
m_cursorPositionEnd = NormalizeCursorPosition(toPosition);
|
||||||
|
|
||||||
|
RefreshCursor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::Write(const Nz::String& text)
|
||||||
|
{
|
||||||
|
Write(text, GetGlyphIndex(m_cursorPositionBegin));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::Write(const Nz::String& text, const Nz::Vector2ui& glyphPosition)
|
||||||
|
{
|
||||||
|
Write(text, GetGlyphIndex(glyphPosition));
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::SetCursorPositionInternal(std::size_t glyphIndex)
|
||||||
|
{
|
||||||
|
return SetCursorPositionInternal(GetCursorPosition(glyphIndex));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void AbstractTextAreaWidget::SetCursorPositionInternal(Nz::Vector2ui cursorPosition)
|
||||||
|
{
|
||||||
|
m_cursorPositionBegin = cursorPosition;
|
||||||
|
m_cursorPositionEnd = m_cursorPositionBegin;
|
||||||
|
|
||||||
|
RefreshCursor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,58 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NDK_WIDGETS_RICHTEXTAREAWIDGET_HPP
|
||||||
|
#define NDK_WIDGETS_RICHTEXTAREAWIDGET_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Utility/RichTextDrawer.hpp>
|
||||||
|
#include <NDK/Widgets/AbstractTextAreaWidget.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
class NDK_API RichTextAreaWidget : public AbstractTextAreaWidget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RichTextAreaWidget(BaseWidget* parent);
|
||||||
|
RichTextAreaWidget(const RichTextAreaWidget&) = delete;
|
||||||
|
RichTextAreaWidget(RichTextAreaWidget&&) = default;
|
||||||
|
~RichTextAreaWidget() = default;
|
||||||
|
|
||||||
|
void AppendText(const Nz::String& text);
|
||||||
|
|
||||||
|
void Clear() override;
|
||||||
|
|
||||||
|
void Erase(std::size_t firstGlyph, std::size_t lastGlyph) override;
|
||||||
|
|
||||||
|
inline unsigned int GetCharacterSize() const;
|
||||||
|
inline const Nz::Color& GetTextColor() const;
|
||||||
|
inline Nz::Font* GetTextFont() const;
|
||||||
|
|
||||||
|
inline void SetCharacterSize(unsigned int characterSize);
|
||||||
|
inline void SetTextColor(const Nz::Color& color);
|
||||||
|
inline void SetTextFont(Nz::FontRef font);
|
||||||
|
|
||||||
|
void Write(const Nz::String& text, std::size_t glyphPosition) override;
|
||||||
|
|
||||||
|
RichTextAreaWidget& operator=(const RichTextAreaWidget&) = delete;
|
||||||
|
RichTextAreaWidget& operator=(RichTextAreaWidget&&) = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Nz::AbstractTextDrawer& GetTextDrawer() override;
|
||||||
|
const Nz::AbstractTextDrawer& GetTextDrawer() const override;
|
||||||
|
|
||||||
|
void HandleIndentation(bool add) override;
|
||||||
|
void HandleSelectionIndentation(bool add) override;
|
||||||
|
void HandleWordCursorMove(bool left) override;
|
||||||
|
|
||||||
|
void UpdateDisplayText();
|
||||||
|
|
||||||
|
Nz::RichTextDrawer m_drawer;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <NDK/Widgets/RichTextAreaWidget.inl>
|
||||||
|
|
||||||
|
#endif // NDK_WIDGETS_TEXTAREAWIDGET_HPP
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#include <NDK/Widgets/RichTextAreaWidget.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
inline unsigned int RichTextAreaWidget::GetCharacterSize() const
|
||||||
|
{
|
||||||
|
return m_drawer.GetDefaultCharacterSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const Nz::Color& RichTextAreaWidget::GetTextColor() const
|
||||||
|
{
|
||||||
|
return m_drawer.GetDefaultColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Nz::Font* RichTextAreaWidget::GetTextFont() const
|
||||||
|
{
|
||||||
|
return m_drawer.GetDefaultFont();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextAreaWidget::SetCharacterSize(unsigned int characterSize)
|
||||||
|
{
|
||||||
|
m_drawer.SetDefaultCharacterSize(characterSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextAreaWidget::SetTextColor(const Nz::Color& color)
|
||||||
|
{
|
||||||
|
m_drawer.SetDefaultColor(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextAreaWidget::SetTextFont(Nz::FontRef font)
|
||||||
|
{
|
||||||
|
m_drawer.SetDefaultFont(std::move(font));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -7,20 +7,14 @@
|
||||||
#ifndef NDK_WIDGETS_TEXTAREAWIDGET_HPP
|
#ifndef NDK_WIDGETS_TEXTAREAWIDGET_HPP
|
||||||
#define NDK_WIDGETS_TEXTAREAWIDGET_HPP
|
#define NDK_WIDGETS_TEXTAREAWIDGET_HPP
|
||||||
|
|
||||||
#include <Nazara/Graphics/TextSprite.hpp>
|
|
||||||
#include <Nazara/Utility/SimpleTextDrawer.hpp>
|
#include <Nazara/Utility/SimpleTextDrawer.hpp>
|
||||||
#include <NDK/BaseWidget.hpp>
|
#include <NDK/Widgets/AbstractTextAreaWidget.hpp>
|
||||||
#include <NDK/Widgets/Enums.hpp>
|
|
||||||
#include <functional>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
{
|
{
|
||||||
class NDK_API TextAreaWidget : public BaseWidget
|
class NDK_API TextAreaWidget : public AbstractTextAreaWidget
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using CharacterFilter = std::function<bool(char32_t)>;
|
|
||||||
|
|
||||||
TextAreaWidget(BaseWidget* parent);
|
TextAreaWidget(BaseWidget* parent);
|
||||||
TextAreaWidget(const TextAreaWidget&) = delete;
|
TextAreaWidget(const TextAreaWidget&) = delete;
|
||||||
TextAreaWidget(TextAreaWidget&&) = default;
|
TextAreaWidget(TextAreaWidget&&) = default;
|
||||||
|
|
@ -28,115 +22,46 @@ namespace Ndk
|
||||||
|
|
||||||
void AppendText(const Nz::String& text);
|
void AppendText(const Nz::String& text);
|
||||||
|
|
||||||
inline void Clear();
|
void Clear() override;
|
||||||
|
|
||||||
//virtual TextAreaWidget* Clone() const = 0;
|
using AbstractTextAreaWidget::Erase;
|
||||||
|
void Erase(std::size_t firstGlyph, std::size_t lastGlyph) override;
|
||||||
|
|
||||||
void EnableLineWrap(bool enable = true);
|
|
||||||
inline void EnableMultiline(bool enable = true);
|
|
||||||
inline void EnableTabWriting(bool enable = true);
|
|
||||||
|
|
||||||
inline void Erase(std::size_t glyphPosition);
|
|
||||||
void Erase(std::size_t firstGlyph, std::size_t lastGlyph);
|
|
||||||
void EraseSelection();
|
|
||||||
|
|
||||||
inline const CharacterFilter& GetCharacterFilter() const;
|
|
||||||
inline unsigned int GetCharacterSize() const;
|
inline unsigned int GetCharacterSize() 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 std::size_t GetGlyphIndex() const;
|
|
||||||
inline std::size_t GetGlyphIndex(const Nz::Vector2ui& cursorPosition) const;
|
|
||||||
inline const Nz::String& GetText() const;
|
inline const Nz::String& GetText() const;
|
||||||
inline const Nz::Color& GetTextColor() const;
|
inline const Nz::Color& GetTextColor() const;
|
||||||
inline Nz::Font* GetTextFont() const;
|
inline Nz::Font* GetTextFont() const;
|
||||||
inline const Nz::Color& GetTextOulineColor() const;
|
inline const Nz::Color& GetTextOulineColor() const;
|
||||||
inline float GetTextOulineThickness() const;
|
inline float GetTextOulineThickness() const;
|
||||||
|
|
||||||
Nz::Vector2ui GetHoveredGlyph(float x, float y) const;
|
|
||||||
|
|
||||||
inline bool HasSelection() const;
|
|
||||||
|
|
||||||
inline bool IsLineWrapEnabled() const;
|
|
||||||
inline bool IsMultilineEnabled() const;
|
|
||||||
inline bool IsReadOnly() const;
|
|
||||||
inline bool IsTabWritingEnabled() const;
|
|
||||||
|
|
||||||
inline void MoveCursor(int offset);
|
|
||||||
inline void MoveCursor(const Nz::Vector2i& offset);
|
|
||||||
|
|
||||||
inline Nz::Vector2ui NormalizeCursorPosition(Nz::Vector2ui cursorPosition) const;
|
|
||||||
|
|
||||||
inline void SetCharacterFilter(CharacterFilter filter);
|
|
||||||
void SetCharacterSize(unsigned int characterSize);
|
void SetCharacterSize(unsigned int characterSize);
|
||||||
inline void SetCursorPosition(std::size_t glyphIndex);
|
|
||||||
inline void SetCursorPosition(Nz::Vector2ui cursorPosition);
|
|
||||||
inline void SetEchoMode(EchoMode echoMode);
|
|
||||||
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);
|
||||||
inline void SetTextFont(Nz::FontRef font);
|
inline void SetTextFont(Nz::FontRef font);
|
||||||
inline void SetTextOutlineColor(const Nz::Color& color);
|
inline void SetTextOutlineColor(const Nz::Color& color);
|
||||||
inline void SetTextOutlineThickness(float thickness);
|
inline void SetTextOutlineThickness(float thickness);
|
||||||
|
|
||||||
inline void Write(const Nz::String& text);
|
using AbstractTextAreaWidget::Write;
|
||||||
inline void Write(const Nz::String& text, const Nz::Vector2ui& glyphPosition);
|
void Write(const Nz::String& text, std::size_t glyphPosition) override;
|
||||||
void Write(const Nz::String& text, std::size_t glyphPosition);
|
|
||||||
|
|
||||||
TextAreaWidget& operator=(const TextAreaWidget&) = delete;
|
TextAreaWidget& operator=(const TextAreaWidget&) = delete;
|
||||||
TextAreaWidget& operator=(TextAreaWidget&&) = default;
|
TextAreaWidget& operator=(TextAreaWidget&&) = default;
|
||||||
|
|
||||||
NazaraSignal(OnTextAreaCursorMove, const TextAreaWidget* /*textArea*/, Nz::Vector2ui* /*newCursorPosition*/);
|
NazaraSignal(OnTextChanged, const AbstractTextAreaWidget* /*textArea*/, const Nz::String& /*text*/);
|
||||||
NazaraSignal(OnTextAreaKeyBackspace, 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(OnTextAreaKeyReturn, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
|
||||||
NazaraSignal(OnTextAreaKeyRight, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
|
||||||
NazaraSignal(OnTextAreaKeyUp, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
|
||||||
NazaraSignal(OnTextAreaSelection, const TextAreaWidget* /*textArea*/, Nz::Vector2ui* /*start*/, Nz::Vector2ui* /*end*/);
|
|
||||||
NazaraSignal(OnTextChanged, const TextAreaWidget* /*textArea*/, const Nz::String& /*text*/);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Layout() override;
|
Nz::AbstractTextDrawer& GetTextDrawer() override;
|
||||||
|
const Nz::AbstractTextDrawer& GetTextDrawer() const override;
|
||||||
|
|
||||||
bool IsFocusable() const override;
|
void HandleIndentation(bool add) override;
|
||||||
void OnFocusLost() override;
|
void HandleSelectionIndentation(bool add) override;
|
||||||
void OnFocusReceived() override;
|
void HandleWordCursorMove(bool left) override;
|
||||||
bool OnKeyPressed(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 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;
|
|
||||||
|
|
||||||
inline void SetCursorPositionInternal(std::size_t glyphIndex);
|
|
||||||
inline void SetCursorPositionInternal(Nz::Vector2ui cursorPosition);
|
|
||||||
|
|
||||||
void RefreshCursor();
|
|
||||||
void UpdateDisplayText();
|
void UpdateDisplayText();
|
||||||
void UpdateTextSprite();
|
|
||||||
|
|
||||||
CharacterFilter m_characterFilter;
|
|
||||||
EchoMode m_echoMode;
|
|
||||||
EntityHandle m_cursorEntity;
|
|
||||||
EntityHandle m_textEntity;
|
|
||||||
Nz::SimpleTextDrawer m_drawer;
|
Nz::SimpleTextDrawer m_drawer;
|
||||||
Nz::String m_text;
|
Nz::String m_text;
|
||||||
Nz::TextSpriteRef m_textSprite;
|
|
||||||
Nz::Vector2ui m_cursorPositionBegin;
|
|
||||||
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;
|
|
||||||
bool m_tabEnabled; // writes (Shift+)Tab character if set to true
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,98 +6,16 @@
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
{
|
{
|
||||||
inline void TextAreaWidget::Clear()
|
|
||||||
{
|
|
||||||
m_cursorPositionBegin.MakeZero();
|
|
||||||
m_cursorPositionEnd.MakeZero();
|
|
||||||
m_drawer.Clear();
|
|
||||||
m_text.Clear();
|
|
||||||
m_textSprite->Update(m_drawer);
|
|
||||||
SetPreferredSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
|
||||||
|
|
||||||
RefreshCursor();
|
|
||||||
OnTextChanged(this, m_text);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::EnableMultiline(bool enable)
|
|
||||||
{
|
|
||||||
m_multiLineEnabled = enable;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::EnableTabWriting(bool enable)
|
|
||||||
{
|
|
||||||
m_tabEnabled = enable;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::Erase(std::size_t glyphPosition)
|
|
||||||
{
|
|
||||||
Erase(glyphPosition, glyphPosition + 1U);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const TextAreaWidget::CharacterFilter& TextAreaWidget::GetCharacterFilter() const
|
|
||||||
{
|
|
||||||
return m_characterFilter;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline unsigned int TextAreaWidget::GetCharacterSize() const
|
inline unsigned int TextAreaWidget::GetCharacterSize() const
|
||||||
{
|
{
|
||||||
return m_drawer.GetCharacterSize();
|
return m_drawer.GetCharacterSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const Nz::Vector2ui& TextAreaWidget::GetCursorPosition() const
|
|
||||||
{
|
|
||||||
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
|
||||||
{
|
{
|
||||||
return m_drawer.GetText();
|
return m_drawer.GetText();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline EchoMode TextAreaWidget::GetEchoMode() const
|
|
||||||
{
|
|
||||||
return m_echoMode;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::size_t TextAreaWidget::GetGlyphIndex() const
|
|
||||||
{
|
|
||||||
return GetGlyphIndex(m_cursorPositionBegin);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline std::size_t TextAreaWidget::GetGlyphIndex(const Nz::Vector2ui& cursorPosition) const
|
|
||||||
{
|
|
||||||
std::size_t glyphIndex = m_drawer.GetLine(cursorPosition.y).glyphIndex + cursorPosition.x;
|
|
||||||
if (m_drawer.GetLineCount() > cursorPosition.y + 1)
|
|
||||||
glyphIndex = std::min(glyphIndex, m_drawer.GetLine(cursorPosition.y + 1).glyphIndex - 1);
|
|
||||||
else
|
|
||||||
glyphIndex = std::min(glyphIndex, m_drawer.GetGlyphCount());
|
|
||||||
|
|
||||||
return glyphIndex;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const Nz::String& TextAreaWidget::GetText() const
|
inline const Nz::String& TextAreaWidget::GetText() const
|
||||||
{
|
{
|
||||||
return m_text;
|
return m_text;
|
||||||
|
|
@ -123,144 +41,6 @@ namespace Ndk
|
||||||
return m_drawer.GetOutlineThickness();
|
return m_drawer.GetOutlineThickness();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool TextAreaWidget::HasSelection() const
|
|
||||||
{
|
|
||||||
return m_cursorPositionBegin != m_cursorPositionEnd;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool TextAreaWidget::IsLineWrapEnabled() const
|
|
||||||
{
|
|
||||||
return m_isLineWrapEnabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool TextAreaWidget::IsMultilineEnabled() const
|
|
||||||
{
|
|
||||||
return m_multiLineEnabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool TextAreaWidget::IsTabWritingEnabled() const
|
|
||||||
{
|
|
||||||
return m_tabEnabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool TextAreaWidget::IsReadOnly() const
|
|
||||||
{
|
|
||||||
return m_readOnly;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::MoveCursor(int offset)
|
|
||||||
{
|
|
||||||
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPositionBegin);
|
|
||||||
if (offset >= 0)
|
|
||||||
SetCursorPosition(cursorGlyph + static_cast<std::size_t>(offset));
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::size_t nOffset = static_cast<std::size_t>(-offset);
|
|
||||||
if (nOffset >= cursorGlyph)
|
|
||||||
SetCursorPosition(0);
|
|
||||||
else
|
|
||||||
SetCursorPosition(cursorGlyph - nOffset);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::MoveCursor(const Nz::Vector2i& offset)
|
|
||||||
{
|
|
||||||
auto ClampOffset = [] (unsigned int cursorPosition, int cursorOffset) -> unsigned int
|
|
||||||
{
|
|
||||||
if (cursorOffset >= 0)
|
|
||||||
return cursorPosition + cursorOffset;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
unsigned int nOffset = static_cast<unsigned int>(-cursorOffset);
|
|
||||||
if (nOffset >= cursorPosition)
|
|
||||||
return 0;
|
|
||||||
else
|
|
||||||
return cursorPosition - nOffset;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Nz::Vector2ui cursorPosition = m_cursorPositionBegin;
|
|
||||||
cursorPosition.x = ClampOffset(static_cast<unsigned int>(cursorPosition.x), offset.x);
|
|
||||||
cursorPosition.y = ClampOffset(static_cast<unsigned int>(cursorPosition.y), offset.y);
|
|
||||||
|
|
||||||
SetCursorPosition(cursorPosition);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Nz::Vector2ui TextAreaWidget::NormalizeCursorPosition(Nz::Vector2ui cursorPosition) const
|
|
||||||
{
|
|
||||||
std::size_t lineCount = m_drawer.GetLineCount();
|
|
||||||
if (cursorPosition.y >= lineCount)
|
|
||||||
cursorPosition.y = static_cast<unsigned int>(lineCount - 1);
|
|
||||||
|
|
||||||
const auto& lineInfo = m_drawer.GetLine(cursorPosition.y);
|
|
||||||
if (cursorPosition.y + 1 < lineCount)
|
|
||||||
{
|
|
||||||
const auto& nextLineInfo = m_drawer.GetLine(cursorPosition.y + 1);
|
|
||||||
cursorPosition.x = std::min(cursorPosition.x, static_cast<unsigned int>(nextLineInfo.glyphIndex - lineInfo.glyphIndex - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
return cursorPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::SetCharacterFilter(CharacterFilter filter)
|
|
||||||
{
|
|
||||||
m_characterFilter = std::move(filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::SetCursorPosition(std::size_t glyphIndex)
|
|
||||||
{
|
|
||||||
Nz::Vector2ui position = GetCursorPosition(glyphIndex);
|
|
||||||
Nz::Vector2ui newPosition = position;
|
|
||||||
|
|
||||||
OnTextAreaCursorMove(this, &newPosition);
|
|
||||||
|
|
||||||
if (position == newPosition)
|
|
||||||
SetCursorPositionInternal(position);
|
|
||||||
else
|
|
||||||
SetCursorPositionInternal(GetGlyphIndex(newPosition));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::SetCursorPosition(Nz::Vector2ui cursorPosition)
|
|
||||||
{
|
|
||||||
OnTextAreaCursorMove(this, &cursorPosition);
|
|
||||||
|
|
||||||
return SetCursorPositionInternal(NormalizeCursorPosition(cursorPosition));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::SetEchoMode(EchoMode echoMode)
|
|
||||||
{
|
|
||||||
m_echoMode = echoMode;
|
|
||||||
|
|
||||||
UpdateDisplayText();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::SetReadOnly(bool readOnly)
|
|
||||||
{
|
|
||||||
m_readOnly = readOnly;
|
|
||||||
m_cursorEntity->Enable(!m_readOnly && HasFocus());
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::SetSelection(Nz::Vector2ui fromPosition, Nz::Vector2ui toPosition)
|
|
||||||
{
|
|
||||||
// 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)
|
|
||||||
{
|
|
||||||
OnTextAreaSelection(this, &fromPosition, &toPosition);
|
|
||||||
|
|
||||||
// Ensure begin is before end a second time (in case signal changed it)
|
|
||||||
if (toPosition.y < fromPosition.y || (toPosition.y == fromPosition.y && toPosition.x < fromPosition.x))
|
|
||||||
std::swap(fromPosition, toPosition);
|
|
||||||
|
|
||||||
m_cursorPositionBegin = NormalizeCursorPosition(fromPosition);
|
|
||||||
m_cursorPositionEnd = NormalizeCursorPosition(toPosition);
|
|
||||||
|
|
||||||
RefreshCursor();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::SetText(const Nz::String& text)
|
inline void TextAreaWidget::SetText(const Nz::String& text)
|
||||||
{
|
{
|
||||||
m_text = text;
|
m_text = text;
|
||||||
|
|
@ -296,27 +76,4 @@ namespace Ndk
|
||||||
|
|
||||||
UpdateDisplayText();
|
UpdateDisplayText();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void TextAreaWidget::Write(const Nz::String& text)
|
|
||||||
{
|
|
||||||
Write(text, GetGlyphIndex(m_cursorPositionBegin));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::Write(const Nz::String& text, const Nz::Vector2ui& glyphPosition)
|
|
||||||
{
|
|
||||||
Write(text, GetGlyphIndex(glyphPosition));
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextAreaWidget::SetCursorPositionInternal(std::size_t glyphIndex)
|
|
||||||
{
|
|
||||||
return SetCursorPositionInternal(GetCursorPosition(glyphIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void TextAreaWidget::SetCursorPositionInternal(Nz::Vector2ui cursorPosition)
|
|
||||||
{
|
|
||||||
m_cursorPositionBegin = cursorPosition;
|
|
||||||
m_cursorPositionEnd = m_cursorPositionBegin;
|
|
||||||
|
|
||||||
RefreshCursor();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,6 @@
|
||||||
#include <NDK/Widgets.hpp>
|
#include <NDK/Widgets.hpp>
|
||||||
#include <NDK/World.hpp>
|
#include <NDK/World.hpp>
|
||||||
|
|
||||||
///TODO: For now is unable to display different color in the history, it needs a RichTextDrawer to do so
|
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
{
|
{
|
||||||
namespace
|
namespace
|
||||||
|
|
@ -42,7 +40,7 @@ namespace Ndk
|
||||||
m_maxHistoryLines(200)
|
m_maxHistoryLines(200)
|
||||||
{
|
{
|
||||||
// History
|
// History
|
||||||
m_history = Add<TextAreaWidget>();
|
m_history = Add<RichTextAreaWidget>();
|
||||||
m_history->EnableBackground(true);
|
m_history->EnableBackground(true);
|
||||||
m_history->EnableLineWrap(true);
|
m_history->EnableLineWrap(true);
|
||||||
m_history->SetReadOnly(true);
|
m_history->SetReadOnly(true);
|
||||||
|
|
@ -61,25 +59,25 @@ namespace Ndk
|
||||||
// Protect input prefix from erasure/selection
|
// Protect input prefix from erasure/selection
|
||||||
m_input->SetCursorPosition(s_inputPrefixSize);
|
m_input->SetCursorPosition(s_inputPrefixSize);
|
||||||
|
|
||||||
m_input->OnTextAreaCursorMove.Connect([](const TextAreaWidget* textArea, Nz::Vector2ui* newCursorPos)
|
m_input->OnTextAreaCursorMove.Connect([](const AbstractTextAreaWidget* textArea, Nz::Vector2ui* newCursorPos)
|
||||||
{
|
{
|
||||||
newCursorPos->x = std::max(newCursorPos->x, static_cast<unsigned int>(s_inputPrefixSize));
|
newCursorPos->x = std::max(newCursorPos->x, static_cast<unsigned int>(s_inputPrefixSize));
|
||||||
});
|
});
|
||||||
|
|
||||||
m_input->OnTextAreaSelection.Connect([](const TextAreaWidget* textArea, Nz::Vector2ui* start, Nz::Vector2ui* end)
|
m_input->OnTextAreaSelection.Connect([](const AbstractTextAreaWidget* textArea, Nz::Vector2ui* start, Nz::Vector2ui* end)
|
||||||
{
|
{
|
||||||
start->x = std::max(start->x, static_cast<unsigned int>(s_inputPrefixSize));
|
start->x = std::max(start->x, static_cast<unsigned int>(s_inputPrefixSize));
|
||||||
end->x = std::max(end->x, static_cast<unsigned int>(s_inputPrefixSize));
|
end->x = std::max(end->x, static_cast<unsigned int>(s_inputPrefixSize));
|
||||||
});
|
});
|
||||||
|
|
||||||
m_input->OnTextAreaKeyBackspace.Connect([](const TextAreaWidget* textArea, bool* ignoreDefaultAction)
|
m_input->OnTextAreaKeyBackspace.Connect([](const AbstractTextAreaWidget* textArea, bool* ignoreDefaultAction)
|
||||||
{
|
{
|
||||||
if (textArea->GetGlyphIndex() <= s_inputPrefixSize)
|
if (textArea->GetGlyphIndex() <= s_inputPrefixSize)
|
||||||
*ignoreDefaultAction = true;
|
*ignoreDefaultAction = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Handle history
|
// Handle history
|
||||||
m_input->OnTextAreaKeyUp.Connect([&] (const TextAreaWidget* textArea, bool* ignoreDefaultAction)
|
m_input->OnTextAreaKeyUp.Connect([&] (const AbstractTextAreaWidget* textArea, bool* ignoreDefaultAction)
|
||||||
{
|
{
|
||||||
*ignoreDefaultAction = true;
|
*ignoreDefaultAction = true;
|
||||||
|
|
||||||
|
|
@ -89,7 +87,7 @@ namespace Ndk
|
||||||
m_input->SetText(s_inputPrefix + m_commandHistory[m_historyPosition]);
|
m_input->SetText(s_inputPrefix + m_commandHistory[m_historyPosition]);
|
||||||
});
|
});
|
||||||
|
|
||||||
m_input->OnTextAreaKeyDown.Connect([&] (const TextAreaWidget* textArea, bool* ignoreDefaultAction)
|
m_input->OnTextAreaKeyDown.Connect([&] (const AbstractTextAreaWidget* textArea, bool* ignoreDefaultAction)
|
||||||
{
|
{
|
||||||
*ignoreDefaultAction = true;
|
*ignoreDefaultAction = true;
|
||||||
|
|
||||||
|
|
@ -112,6 +110,7 @@ namespace Ndk
|
||||||
m_historyLines.erase(m_historyLines.begin());
|
m_historyLines.erase(m_historyLines.begin());
|
||||||
|
|
||||||
m_historyLines.emplace_back(Line{ color, text });
|
m_historyLines.emplace_back(Line{ color, text });
|
||||||
|
m_history->SetTextColor(color);
|
||||||
m_history->AppendText(text + '\n');
|
m_history->AppendText(text + '\n');
|
||||||
m_history->Resize(m_history->GetPreferredSize());
|
m_history->Resize(m_history->GetPreferredSize());
|
||||||
m_historyArea->Resize(m_historyArea->GetSize());
|
m_historyArea->Resize(m_historyArea->GetSize());
|
||||||
|
|
@ -187,7 +186,7 @@ namespace Ndk
|
||||||
/*!
|
/*!
|
||||||
* \brief Performs this action when an input is added to the console
|
* \brief Performs this action when an input is added to the console
|
||||||
*/
|
*/
|
||||||
void Console::ExecuteInput(const TextAreaWidget* textArea, bool* ignoreDefaultAction)
|
void Console::ExecuteInput(const AbstractTextAreaWidget* textArea, bool* ignoreDefaultAction)
|
||||||
{
|
{
|
||||||
NazaraAssert(textArea == m_input, "Unexpected signal from an other text area");
|
NazaraAssert(textArea == m_input, "Unexpected signal from an other text area");
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,489 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#include <NDK/Widgets/AbstractTextAreaWidget.hpp>
|
||||||
|
#include <Nazara/Core/Unicode.hpp>
|
||||||
|
#include <Nazara/Utility/Font.hpp>
|
||||||
|
#include <NDK/Components/GraphicsComponent.hpp>
|
||||||
|
#include <NDK/Components/NodeComponent.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
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 = Nz::TextSprite::New();
|
||||||
|
|
||||||
|
m_textEntity = CreateEntity();
|
||||||
|
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||||
|
|
||||||
|
auto& textNode = m_textEntity->AddComponent<NodeComponent>();
|
||||||
|
textNode.SetParent(this);
|
||||||
|
textNode.SetPosition(paddingWidth, paddingHeight);
|
||||||
|
|
||||||
|
m_cursorEntity = CreateEntity();
|
||||||
|
m_cursorEntity->AddComponent<GraphicsComponent>();
|
||||||
|
m_cursorEntity->AddComponent<NodeComponent>().SetParent(m_textEntity);
|
||||||
|
m_cursorEntity->GetComponent<NodeComponent>();
|
||||||
|
m_cursorEntity->Enable(false);
|
||||||
|
|
||||||
|
SetCursor(Nz::SystemCursor_Text);
|
||||||
|
|
||||||
|
EnableBackground(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::Clear()
|
||||||
|
{
|
||||||
|
Nz::AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||||
|
|
||||||
|
m_cursorPositionBegin.MakeZero();
|
||||||
|
m_cursorPositionEnd.MakeZero();
|
||||||
|
textDrawer.Clear();
|
||||||
|
m_textSprite->Update(textDrawer);
|
||||||
|
SetPreferredSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||||
|
|
||||||
|
RefreshCursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::EnableLineWrap(bool enable)
|
||||||
|
{
|
||||||
|
if (m_isLineWrapEnabled != enable)
|
||||||
|
{
|
||||||
|
m_isLineWrapEnabled = enable;
|
||||||
|
|
||||||
|
Nz::AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||||
|
|
||||||
|
if (enable)
|
||||||
|
textDrawer.SetMaxLineWidth(GetWidth());
|
||||||
|
else
|
||||||
|
textDrawer.SetMaxLineWidth(std::numeric_limits<float>::infinity());
|
||||||
|
|
||||||
|
UpdateTextSprite();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Nz::Vector2ui AbstractTextAreaWidget::GetHoveredGlyph(float x, float y) const
|
||||||
|
{
|
||||||
|
const Nz::AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||||
|
|
||||||
|
auto& textNode = m_textEntity->GetComponent<Ndk::NodeComponent>();
|
||||||
|
Nz::Vector2f textPosition = Nz::Vector2f(textNode.GetPosition(Nz::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)
|
||||||
|
{
|
||||||
|
Nz::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)
|
||||||
|
{
|
||||||
|
Nz::Rectf bounds = textDrawer.GetGlyph(i).bounds;
|
||||||
|
if (x < bounds.x + bounds.width * 0.75f)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Nz::Vector2ui(Nz::Vector2<std::size_t>(i - firstLineGlyph, line));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Nz::Vector2ui::Zero();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::Layout()
|
||||||
|
{
|
||||||
|
BaseWidget::Layout();
|
||||||
|
|
||||||
|
if (m_isLineWrapEnabled)
|
||||||
|
{
|
||||||
|
Nz::AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||||
|
|
||||||
|
textDrawer.SetMaxLineWidth(GetWidth());
|
||||||
|
UpdateTextSprite();
|
||||||
|
}
|
||||||
|
|
||||||
|
RefreshCursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AbstractTextAreaWidget::IsFocusable() const
|
||||||
|
{
|
||||||
|
return !m_readOnly;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::OnFocusLost()
|
||||||
|
{
|
||||||
|
m_cursorEntity->Disable();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::OnFocusReceived()
|
||||||
|
{
|
||||||
|
if (!m_readOnly)
|
||||||
|
m_cursorEntity->Enable(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AbstractTextAreaWidget::OnKeyPressed(const Nz::WindowEvent::KeyEvent& key)
|
||||||
|
{
|
||||||
|
const Nz::AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||||
|
|
||||||
|
switch (key.code)
|
||||||
|
{
|
||||||
|
case Nz::Keyboard::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 Nz::Keyboard::Delete:
|
||||||
|
{
|
||||||
|
if (HasSelection())
|
||||||
|
EraseSelection();
|
||||||
|
else
|
||||||
|
Erase(GetGlyphIndex(m_cursorPositionBegin));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Nz::Keyboard::Down:
|
||||||
|
{
|
||||||
|
bool ignoreDefaultAction = false;
|
||||||
|
OnTextAreaKeyDown(this, &ignoreDefaultAction);
|
||||||
|
|
||||||
|
if (ignoreDefaultAction)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (HasSelection())
|
||||||
|
SetCursorPosition(m_cursorPositionEnd);
|
||||||
|
|
||||||
|
MoveCursor({0, 1});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Nz::Keyboard::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 Nz::Keyboard::Home:
|
||||||
|
{
|
||||||
|
bool ignoreDefaultAction = false;
|
||||||
|
OnTextAreaKeyHome(this, &ignoreDefaultAction);
|
||||||
|
|
||||||
|
if (ignoreDefaultAction)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
SetCursorPosition({ 0U, key.control ? 0U : m_cursorPositionEnd.y });
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Nz::Keyboard::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 Nz::Keyboard::Return:
|
||||||
|
{
|
||||||
|
bool ignoreDefaultAction = false;
|
||||||
|
OnTextAreaKeyReturn(this, &ignoreDefaultAction);
|
||||||
|
|
||||||
|
if (ignoreDefaultAction)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (!m_multiLineEnabled)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (HasSelection())
|
||||||
|
EraseSelection();
|
||||||
|
|
||||||
|
Write(Nz::String('\n'));
|
||||||
|
return true;;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Nz::Keyboard::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 Nz::Keyboard::Up:
|
||||||
|
{
|
||||||
|
bool ignoreDefaultAction = false;
|
||||||
|
OnTextAreaKeyUp(this, &ignoreDefaultAction);
|
||||||
|
|
||||||
|
if (ignoreDefaultAction)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (HasSelection())
|
||||||
|
SetCursorPosition(m_cursorPositionBegin);
|
||||||
|
|
||||||
|
MoveCursor({0, -1});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
case Nz::Keyboard::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 Nz::WindowEvent::KeyEvent& /*key*/)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::OnMouseButtonPress(int x, int y, Nz::Mouse::Button button)
|
||||||
|
{
|
||||||
|
if (button == Nz::Mouse::Left)
|
||||||
|
{
|
||||||
|
SetFocus();
|
||||||
|
|
||||||
|
Nz::Vector2ui hoveredGlyph = GetHoveredGlyph(float(x), float(y));
|
||||||
|
|
||||||
|
// 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 AbstractTextAreaWidget::OnMouseButtonRelease(int, int, Nz::Mouse::Button button)
|
||||||
|
{
|
||||||
|
if (button == Nz::Mouse::Left)
|
||||||
|
m_isMouseButtonDown = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::OnMouseEnter()
|
||||||
|
{
|
||||||
|
if (!Nz::Mouse::IsButtonPressed(Nz::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 (Nz::Unicode::GetCategory(character) == Nz::Unicode::Category_Other_Control || (m_characterFilter && !m_characterFilter(character)))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (HasSelection())
|
||||||
|
EraseSelection();
|
||||||
|
|
||||||
|
Write(Nz::String::Unicode(character));
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::RefreshCursor()
|
||||||
|
{
|
||||||
|
if (m_readOnly)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const Nz::AbstractTextDrawer& textDrawer = GetTextDrawer();
|
||||||
|
|
||||||
|
auto GetGlyph = [&](const Nz::Vector2ui& glyphPosition, std::size_t* glyphIndex) -> const Nz::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;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Move text so that cursor is always visible
|
||||||
|
const auto* lastGlyph = GetGlyph(m_cursorPositionEnd, nullptr);
|
||||||
|
float glyphPos = (lastGlyph) ? lastGlyph->bounds.x : 0.f;
|
||||||
|
float glyphWidth = (lastGlyph) ? lastGlyph->bounds.width : 0.f;
|
||||||
|
|
||||||
|
auto& node = m_textEntity->GetComponent<Ndk::NodeComponent>();
|
||||||
|
float textPosition = node.GetPosition(Nz::CoordSys_Local).x - paddingWidth;
|
||||||
|
float cursorPosition = glyphPos + textPosition;
|
||||||
|
float width = GetWidth();
|
||||||
|
|
||||||
|
if (width <= textDrawer.GetBounds().width)
|
||||||
|
{
|
||||||
|
if (cursorPosition + glyphWidth > width)
|
||||||
|
node.Move(width - cursorPosition - glyphWidth, 0.f);
|
||||||
|
else if (cursorPosition - glyphWidth < 0.f)
|
||||||
|
node.Move(-cursorPosition + glyphWidth, 0.f);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
node.Move(-textPosition, 0.f); // Reset text position if we have enough room to show everything
|
||||||
|
|
||||||
|
// Show cursor/selection
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GraphicsComponent& gfxComponent = m_cursorEntity->GetComponent<GraphicsComponent>();
|
||||||
|
gfxComponent.Clear();
|
||||||
|
|
||||||
|
for (unsigned int i = m_cursorPositionBegin.y; i <= m_cursorPositionEnd.y; ++i)
|
||||||
|
{
|
||||||
|
const auto& lineInfo = textDrawer.GetLine(i);
|
||||||
|
|
||||||
|
Nz::SpriteRef& cursorSprite = m_cursorSprites[i - m_cursorPositionBegin.y];
|
||||||
|
if (i == m_cursorPositionBegin.y || i == m_cursorPositionEnd.y)
|
||||||
|
{
|
||||||
|
auto GetGlyphPos = [&](const Nz::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);
|
||||||
|
|
||||||
|
cursorSprite->SetColor((m_cursorPositionBegin == m_cursorPositionEnd) ? Nz::Color::Black : Nz::Color(0, 0, 0, 50));
|
||||||
|
cursorSprite->SetSize(spriteSize, lineInfo.bounds.height);
|
||||||
|
|
||||||
|
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, lineInfo.bounds.height);
|
||||||
|
|
||||||
|
gfxComponent.Attach(cursorSprite, Nz::Matrix4f::Translate({ 0.f, lineInfo.bounds.y, 0.f }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void AbstractTextAreaWidget::UpdateTextSprite()
|
||||||
|
{
|
||||||
|
m_textSprite->Update(GetTextDrawer());
|
||||||
|
SetPreferredSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,196 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Development Kit"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||||
|
|
||||||
|
#include <NDK/Widgets/RichTextAreaWidget.hpp>
|
||||||
|
|
||||||
|
namespace Ndk
|
||||||
|
{
|
||||||
|
RichTextAreaWidget::RichTextAreaWidget(BaseWidget* parent) :
|
||||||
|
AbstractTextAreaWidget(parent)
|
||||||
|
{
|
||||||
|
Layout();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextAreaWidget::AppendText(const Nz::String& text)
|
||||||
|
{
|
||||||
|
//m_text += text;
|
||||||
|
switch (m_echoMode)
|
||||||
|
{
|
||||||
|
case EchoMode_Normal:
|
||||||
|
m_drawer.AppendText(text);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EchoMode_Password:
|
||||||
|
m_drawer.AppendText(Nz::String(text.GetLength(), '*'));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EchoMode_PasswordExceptLast:
|
||||||
|
{
|
||||||
|
/*m_drawer.Clear();
|
||||||
|
std::size_t textLength = m_text.GetLength();
|
||||||
|
if (textLength >= 2)
|
||||||
|
{
|
||||||
|
std::size_t lastCharacterPosition = m_text.GetCharacterPosition(textLength - 2);
|
||||||
|
if (lastCharacterPosition != Nz::String::npos)
|
||||||
|
m_drawer.AppendText(Nz::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 Nz::String& blockText = m_drawer.GetBlockText(firstBlock);
|
||||||
|
std::size_t blockFirstGlyph = m_drawer.GetBlockFirstGlyphIndex(firstBlock);
|
||||||
|
|
||||||
|
Nz::String newText;
|
||||||
|
if (firstGlyph > blockFirstGlyph)
|
||||||
|
{
|
||||||
|
std::size_t characterPosition = blockText.GetCharacterPosition(firstGlyph - blockFirstGlyph - 1);
|
||||||
|
NazaraAssert(characterPosition != Nz::String::npos, "Invalid character position");
|
||||||
|
|
||||||
|
newText.Append(blockText.SubString(0, characterPosition));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastGlyph < textLength)
|
||||||
|
newText.Append(blockText.SubString(blockText.GetCharacterPosition(lastGlyph - blockFirstGlyph)));
|
||||||
|
|
||||||
|
if (!newText.IsEmpty())
|
||||||
|
m_drawer.SetBlockText(firstBlock, std::move(newText));
|
||||||
|
else
|
||||||
|
m_drawer.RemoveBlock(firstBlock);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const Nz::String& lastBlockText = m_drawer.GetBlockText(lastBlock);
|
||||||
|
std::size_t lastBlockGlyphIndex = m_drawer.GetBlockFirstGlyphIndex(lastBlock);
|
||||||
|
|
||||||
|
// First, update/delete last block
|
||||||
|
std::size_t lastCharPos = lastBlockText.GetCharacterPosition(lastGlyph - lastBlockGlyphIndex);
|
||||||
|
if (lastCharPos != Nz::String::npos)
|
||||||
|
{
|
||||||
|
Nz::String newText = lastBlockText.SubString(lastCharPos);
|
||||||
|
if (!newText.IsEmpty())
|
||||||
|
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 Nz::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 = firstBlockText.GetCharacterPosition(firstGlyph - firstBlockGlyphIndex - 1);
|
||||||
|
if (firstCharPos != Nz::String::npos)
|
||||||
|
{
|
||||||
|
Nz::String newText = firstBlockText.SubString(0, firstCharPos);
|
||||||
|
if (!newText.IsEmpty())
|
||||||
|
m_drawer.SetBlockText(firstBlock, std::move(newText));
|
||||||
|
else
|
||||||
|
m_drawer.RemoveBlock(firstBlock);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_drawer.RemoveBlock(firstBlock);
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateDisplayText();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextAreaWidget::Write(const Nz::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);
|
||||||
|
|
||||||
|
Nz::String blockText = block.GetText();
|
||||||
|
std::size_t characterPosition = blockText.GetCharacterPosition(glyphPosition - firstGlyph);
|
||||||
|
blockText.Insert(characterPosition, text);
|
||||||
|
|
||||||
|
block.SetText(blockText);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_drawer.AppendText(text);
|
||||||
|
|
||||||
|
SetCursorPosition(glyphPosition + text.GetLength());
|
||||||
|
|
||||||
|
UpdateDisplayText();
|
||||||
|
}
|
||||||
|
|
||||||
|
Nz::AbstractTextDrawer& RichTextAreaWidget::GetTextDrawer()
|
||||||
|
{
|
||||||
|
return m_drawer;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Nz::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(Nz::String(m_text.GetLength(), '*'));
|
||||||
|
break;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
UpdateTextSprite();
|
||||||
|
|
||||||
|
SetCursorPosition(m_cursorPositionBegin); //< Refresh cursor position (prevent it from being outside of the text)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -10,43 +10,11 @@
|
||||||
|
|
||||||
namespace Ndk
|
namespace Ndk
|
||||||
{
|
{
|
||||||
namespace
|
|
||||||
{
|
|
||||||
constexpr float paddingWidth = 5.f;
|
|
||||||
constexpr float paddingHeight = 3.f;
|
|
||||||
}
|
|
||||||
|
|
||||||
TextAreaWidget::TextAreaWidget(BaseWidget* parent) :
|
TextAreaWidget::TextAreaWidget(BaseWidget* parent) :
|
||||||
BaseWidget(parent),
|
AbstractTextAreaWidget(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 = Nz::TextSprite::New();
|
|
||||||
|
|
||||||
m_textEntity = CreateEntity();
|
|
||||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
|
||||||
|
|
||||||
auto& textNode = m_textEntity->AddComponent<NodeComponent>();
|
|
||||||
textNode.SetParent(this);
|
|
||||||
textNode.SetPosition(paddingWidth, paddingHeight);
|
|
||||||
|
|
||||||
m_cursorEntity = CreateEntity();
|
|
||||||
m_cursorEntity->AddComponent<GraphicsComponent>();
|
|
||||||
m_cursorEntity->AddComponent<NodeComponent>().SetParent(m_textEntity);
|
|
||||||
m_cursorEntity->GetComponent<NodeComponent>();
|
|
||||||
m_cursorEntity->Enable(false);
|
|
||||||
|
|
||||||
SetCursor(Nz::SystemCursor_Text);
|
|
||||||
SetCharacterSize(GetCharacterSize()); //< Actualize minimum / preferred size
|
SetCharacterSize(GetCharacterSize()); //< Actualize minimum / preferred size
|
||||||
|
|
||||||
EnableBackground(true);
|
|
||||||
Layout();
|
Layout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,19 +54,12 @@ namespace Ndk
|
||||||
OnTextChanged(this, m_text);
|
OnTextChanged(this, m_text);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextAreaWidget::EnableLineWrap(bool enable)
|
void TextAreaWidget::Clear()
|
||||||
{
|
{
|
||||||
if (m_isLineWrapEnabled != enable)
|
AbstractTextAreaWidget::Clear();
|
||||||
{
|
|
||||||
m_isLineWrapEnabled = enable;
|
|
||||||
|
|
||||||
if (enable)
|
m_text.Clear();
|
||||||
m_drawer.SetMaxLineWidth(GetWidth());
|
OnTextChanged(this, m_text);
|
||||||
else
|
|
||||||
m_drawer.SetMaxLineWidth(std::numeric_limits<float>::infinity());
|
|
||||||
|
|
||||||
UpdateTextSprite();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextAreaWidget::Erase(std::size_t firstGlyph, std::size_t lastGlyph)
|
void TextAreaWidget::Erase(std::size_t firstGlyph, std::size_t lastGlyph)
|
||||||
|
|
@ -130,50 +91,6 @@ namespace Ndk
|
||||||
SetText(newText);
|
SetText(newText);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextAreaWidget::EraseSelection()
|
|
||||||
{
|
|
||||||
if (!HasSelection())
|
|
||||||
return;
|
|
||||||
|
|
||||||
Erase(GetGlyphIndex(m_cursorPositionBegin), GetGlyphIndex(m_cursorPositionEnd));
|
|
||||||
}
|
|
||||||
|
|
||||||
Nz::Vector2ui TextAreaWidget::GetHoveredGlyph(float x, float y) const
|
|
||||||
{
|
|
||||||
auto& textNode = m_textEntity->GetComponent<Ndk::NodeComponent>();
|
|
||||||
Nz::Vector2f textPosition = Nz::Vector2f(textNode.GetPosition(Nz::CoordSys_Local));
|
|
||||||
x -= textPosition.x;
|
|
||||||
y -= textPosition.y;
|
|
||||||
|
|
||||||
std::size_t glyphCount = m_drawer.GetGlyphCount();
|
|
||||||
if (glyphCount > 0)
|
|
||||||
{
|
|
||||||
std::size_t lineCount = m_drawer.GetLineCount();
|
|
||||||
std::size_t line = 0U;
|
|
||||||
for (; line < lineCount - 1; ++line)
|
|
||||||
{
|
|
||||||
Nz::Rectf lineBounds = m_drawer.GetLine(line).bounds;
|
|
||||||
if (lineBounds.GetMaximum().y > y)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::size_t upperLimit = (line != lineCount - 1) ? m_drawer.GetLine(line + 1).glyphIndex : glyphCount + 1;
|
|
||||||
|
|
||||||
std::size_t firstLineGlyph = m_drawer.GetLine(line).glyphIndex;
|
|
||||||
std::size_t i = firstLineGlyph;
|
|
||||||
for (; i < upperLimit - 1; ++i)
|
|
||||||
{
|
|
||||||
Nz::Rectf bounds = m_drawer.GetGlyph(i).bounds;
|
|
||||||
if (x < bounds.x + bounds.width * 0.75f)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Nz::Vector2ui(Nz::Vector2<std::size_t>(i - firstLineGlyph, line));
|
|
||||||
}
|
|
||||||
|
|
||||||
return Nz::Vector2ui::Zero();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextAreaWidget::SetCharacterSize(unsigned int characterSize)
|
void TextAreaWidget::SetCharacterSize(unsigned int characterSize)
|
||||||
{
|
{
|
||||||
m_drawer.SetCharacterSize(characterSize);
|
m_drawer.SetCharacterSize(characterSize);
|
||||||
|
|
@ -198,6 +115,7 @@ namespace Ndk
|
||||||
{
|
{
|
||||||
if (glyphPosition >= m_drawer.GetGlyphCount())
|
if (glyphPosition >= m_drawer.GetGlyphCount())
|
||||||
{
|
{
|
||||||
|
// It's faster to append than to insert in the middle
|
||||||
AppendText(text);
|
AppendText(text);
|
||||||
SetCursorPosition(m_drawer.GetGlyphCount());
|
SetCursorPosition(m_drawer.GetGlyphCount());
|
||||||
}
|
}
|
||||||
|
|
@ -210,142 +128,71 @@ namespace Ndk
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextAreaWidget::Layout()
|
Nz::AbstractTextDrawer& TextAreaWidget::GetTextDrawer()
|
||||||
{
|
{
|
||||||
BaseWidget::Layout();
|
return m_drawer;
|
||||||
|
|
||||||
if (m_isLineWrapEnabled)
|
|
||||||
{
|
|
||||||
m_drawer.SetMaxLineWidth(GetWidth());
|
|
||||||
UpdateTextSprite();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RefreshCursor();
|
const Nz::AbstractTextDrawer& TextAreaWidget::GetTextDrawer() const
|
||||||
|
{
|
||||||
|
return m_drawer;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TextAreaWidget::IsFocusable() const
|
void TextAreaWidget::HandleIndentation(bool add)
|
||||||
{
|
{
|
||||||
return !m_readOnly;
|
if (add)
|
||||||
}
|
Write(Nz::String('\t'));
|
||||||
|
|
||||||
void TextAreaWidget::OnFocusLost()
|
|
||||||
{
|
|
||||||
m_cursorEntity->Disable();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextAreaWidget::OnFocusReceived()
|
|
||||||
{
|
|
||||||
if (!m_readOnly)
|
|
||||||
m_cursorEntity->Enable(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TextAreaWidget::OnKeyPressed(const Nz::WindowEvent::KeyEvent& key)
|
|
||||||
{
|
|
||||||
switch (key.code)
|
|
||||||
{
|
|
||||||
case Nz::Keyboard::Backspace:
|
|
||||||
{
|
|
||||||
bool ignoreDefaultAction = false;
|
|
||||||
OnTextAreaKeyBackspace(this, &ignoreDefaultAction);
|
|
||||||
|
|
||||||
std::size_t cursorGlyphBegin = GetGlyphIndex(m_cursorPositionBegin);
|
|
||||||
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
|
else
|
||||||
{
|
{
|
||||||
Nz::String newText;
|
std::size_t currentGlyph = GetGlyphIndex(m_cursorPositionBegin);
|
||||||
|
|
||||||
if (cursorGlyphBegin > 1)
|
if (currentGlyph > 0 && m_text[m_text.GetCharacterPosition(currentGlyph - 1U)] == '\t') // Check if previous glyph is a tab
|
||||||
newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(cursorGlyphBegin - 1) - 1));
|
{
|
||||||
|
Erase(currentGlyph - 1U);
|
||||||
|
|
||||||
if (cursorGlyphEnd < m_text.GetLength())
|
if (m_cursorPositionBegin.x < static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionBegin.y)))
|
||||||
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);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
void TextAreaWidget::HandleSelectionIndentation(bool add)
|
||||||
}
|
|
||||||
|
|
||||||
case Nz::Keyboard::Delete:
|
|
||||||
{
|
{
|
||||||
if (HasSelection())
|
for (unsigned line = m_cursorPositionBegin.y; line <= m_cursorPositionEnd.y; ++line)
|
||||||
EraseSelection();
|
{
|
||||||
|
const Nz::Vector2ui cursorPositionBegin = m_cursorPositionBegin;
|
||||||
|
const Nz::Vector2ui cursorPositionEnd = m_cursorPositionEnd;
|
||||||
|
|
||||||
|
if (add)
|
||||||
|
{
|
||||||
|
Write(Nz::String('\t'), { 0U, line });
|
||||||
|
SetSelection(cursorPositionBegin + (cursorPositionBegin.y == line && cursorPositionBegin.x != 0U ? Nz::Vector2ui{ 1U, 0U } : Nz::Vector2ui{}),
|
||||||
|
cursorPositionEnd + (cursorPositionEnd.y == line ? Nz::Vector2ui{ 1U, 0U } : Nz::Vector2ui{}));
|
||||||
|
}
|
||||||
else
|
else
|
||||||
Erase(GetGlyphIndex(m_cursorPositionBegin));
|
{
|
||||||
|
if (m_drawer.GetLineGlyphCount(line) == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
return true;
|
std::size_t firstGlyph = GetGlyphIndex({ 0U, line });
|
||||||
|
|
||||||
|
if (m_text[m_text.GetCharacterPosition(firstGlyph)] == '\t')
|
||||||
|
{
|
||||||
|
Erase(firstGlyph);
|
||||||
|
SetSelection(cursorPositionBegin - (cursorPositionBegin.y == line && cursorPositionBegin.x != 0U ? Nz::Vector2ui{ 1U, 0U } : Nz::Vector2ui{}),
|
||||||
|
cursorPositionEnd - (cursorPositionEnd.y == line && cursorPositionEnd.x != 0U ? Nz::Vector2ui{ 1U, 0U } : Nz::Vector2ui{}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
case Nz::Keyboard::Down:
|
void TextAreaWidget::HandleWordCursorMove(bool left)
|
||||||
{
|
{
|
||||||
bool ignoreDefaultAction = false;
|
if (left)
|
||||||
OnTextAreaKeyDown(this, &ignoreDefaultAction);
|
|
||||||
|
|
||||||
if (ignoreDefaultAction)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (HasSelection())
|
|
||||||
SetCursorPosition(m_cursorPositionEnd);
|
|
||||||
|
|
||||||
MoveCursor({0, 1});
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Nz::Keyboard::End:
|
|
||||||
{
|
|
||||||
bool ignoreDefaultAction = false;
|
|
||||||
OnTextAreaKeyEnd(this, &ignoreDefaultAction);
|
|
||||||
|
|
||||||
if (ignoreDefaultAction)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
std::size_t lineCount = m_drawer.GetLineCount();
|
|
||||||
if (key.control && lineCount > 0)
|
|
||||||
SetCursorPosition({ static_cast<unsigned int>(m_drawer.GetLineGlyphCount(lineCount - 1)), static_cast<unsigned int>(lineCount - 1) });
|
|
||||||
else
|
|
||||||
SetCursorPosition({ static_cast<unsigned int>(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, key.control ? 0U : m_cursorPositionEnd.y });
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Nz::Keyboard::Left:
|
|
||||||
{
|
|
||||||
bool ignoreDefaultAction = false;
|
|
||||||
OnTextAreaKeyLeft(this, &ignoreDefaultAction);
|
|
||||||
|
|
||||||
if (ignoreDefaultAction)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (HasSelection())
|
|
||||||
SetCursorPosition(m_cursorPositionBegin);
|
|
||||||
else if (key.control)
|
|
||||||
{
|
{
|
||||||
std::size_t index = GetGlyphIndex(m_cursorPositionBegin);
|
std::size_t index = GetGlyphIndex(m_cursorPositionBegin);
|
||||||
|
|
||||||
if (index == 0)
|
if (index == 0)
|
||||||
return true;
|
return;
|
||||||
|
|
||||||
std::size_t spaceIndex = m_text.FindLast(' ', index - 2);
|
std::size_t spaceIndex = m_text.FindLast(' ', index - 2);
|
||||||
std::size_t endlIndex = m_text.FindLast('\n', index - 1);
|
std::size_t endlIndex = m_text.FindLast('\n', index - 1);
|
||||||
|
|
@ -363,40 +210,6 @@ namespace Ndk
|
||||||
SetCursorPosition({ 0U, m_cursorPositionBegin.y });
|
SetCursorPosition({ 0U, m_cursorPositionBegin.y });
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
MoveCursor(-1);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Nz::Keyboard::Return:
|
|
||||||
{
|
|
||||||
bool ignoreDefaultAction = false;
|
|
||||||
OnTextAreaKeyReturn(this, &ignoreDefaultAction);
|
|
||||||
|
|
||||||
if (ignoreDefaultAction)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (!m_multiLineEnabled)
|
|
||||||
break;
|
|
||||||
|
|
||||||
if (HasSelection())
|
|
||||||
EraseSelection();
|
|
||||||
|
|
||||||
Write(Nz::String('\n'));
|
|
||||||
return true;;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Nz::Keyboard::Right:
|
|
||||||
{
|
|
||||||
bool ignoreDefaultAction = false;
|
|
||||||
OnTextAreaKeyRight(this, &ignoreDefaultAction);
|
|
||||||
|
|
||||||
if (ignoreDefaultAction)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (HasSelection())
|
|
||||||
SetCursorPosition(m_cursorPositionEnd);
|
|
||||||
else if (key.control)
|
|
||||||
{
|
{
|
||||||
std::size_t index = GetGlyphIndex(m_cursorPositionEnd);
|
std::size_t index = GetGlyphIndex(m_cursorPositionEnd);
|
||||||
std::size_t spaceIndex = m_text.Find(' ', index);
|
std::size_t spaceIndex = m_text.Find(' ', index);
|
||||||
|
|
@ -419,244 +232,6 @@ namespace Ndk
|
||||||
else
|
else
|
||||||
SetCursorPosition({ static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionEnd.y)), m_cursorPositionEnd.y });
|
SetCursorPosition({ static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionEnd.y)), m_cursorPositionEnd.y });
|
||||||
}
|
}
|
||||||
else
|
|
||||||
MoveCursor(1);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Nz::Keyboard::Up:
|
|
||||||
{
|
|
||||||
bool ignoreDefaultAction = false;
|
|
||||||
OnTextAreaKeyUp(this, &ignoreDefaultAction);
|
|
||||||
|
|
||||||
if (ignoreDefaultAction)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (HasSelection())
|
|
||||||
SetCursorPosition(m_cursorPositionBegin);
|
|
||||||
|
|
||||||
MoveCursor({0, -1});
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Nz::Keyboard::Tab:
|
|
||||||
{
|
|
||||||
if (!m_tabEnabled)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (HasSelection())
|
|
||||||
{
|
|
||||||
for(unsigned line = m_cursorPositionBegin.y; line <= m_cursorPositionEnd.y; ++line)
|
|
||||||
{
|
|
||||||
const Nz::Vector2ui cursorPositionBegin = m_cursorPositionBegin;
|
|
||||||
const Nz::Vector2ui cursorPositionEnd = m_cursorPositionEnd;
|
|
||||||
|
|
||||||
if (key.shift)
|
|
||||||
{
|
|
||||||
if (m_drawer.GetLineGlyphCount(line) == 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
std::size_t firstGlyph = GetGlyphIndex({ 0U, line });
|
|
||||||
|
|
||||||
if (m_text[m_text.GetCharacterPosition(firstGlyph)] == '\t')
|
|
||||||
{
|
|
||||||
Erase(firstGlyph);
|
|
||||||
SetSelection(cursorPositionBegin - (cursorPositionBegin.y == line && cursorPositionBegin.x != 0U ? Nz::Vector2ui { 1U, 0U } : Nz::Vector2ui {}),
|
|
||||||
cursorPositionEnd - (cursorPositionEnd.y == line && cursorPositionEnd.x != 0U ? Nz::Vector2ui { 1U, 0U } : Nz::Vector2ui {}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Write(Nz::String('\t'), { 0U, line });
|
|
||||||
SetSelection(cursorPositionBegin + (cursorPositionBegin.y == line && cursorPositionBegin.x != 0U ? Nz::Vector2ui { 1U, 0U } : Nz::Vector2ui {}),
|
|
||||||
cursorPositionEnd + (cursorPositionEnd.y == line ? Nz::Vector2ui { 1U, 0U } : Nz::Vector2ui {}));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (key.shift)
|
|
||||||
{
|
|
||||||
std::size_t currentGlyph = GetGlyphIndex(m_cursorPositionBegin);
|
|
||||||
|
|
||||||
if (currentGlyph > 0 && m_text[m_text.GetCharacterPosition(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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
Write(Nz::String('\t'));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextAreaWidget::OnKeyReleased(const Nz::WindowEvent::KeyEvent& /*key*/)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextAreaWidget::OnMouseButtonPress(int x, int y, Nz::Mouse::Button button)
|
|
||||||
{
|
|
||||||
if (button == Nz::Mouse::Left)
|
|
||||||
{
|
|
||||||
SetFocus();
|
|
||||||
|
|
||||||
Nz::Vector2ui hoveredGlyph = GetHoveredGlyph(float(x), float(y));
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
SetSelection(m_selectionCursor, GetHoveredGlyph(float(x), float(y)));
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextAreaWidget::OnTextEntered(char32_t character, bool /*repeated*/)
|
|
||||||
{
|
|
||||||
if (m_readOnly)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (Nz::Unicode::GetCategory(character) == Nz::Unicode::Category_Other_Control || (m_characterFilter && !m_characterFilter(character)))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (HasSelection())
|
|
||||||
EraseSelection();
|
|
||||||
|
|
||||||
Write(Nz::String::Unicode(character));
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextAreaWidget::RefreshCursor()
|
|
||||||
{
|
|
||||||
if (m_readOnly)
|
|
||||||
return;
|
|
||||||
|
|
||||||
auto GetGlyph = [&](const Nz::Vector2ui& glyphPosition, std::size_t* glyphIndex) -> const Nz::AbstractTextDrawer::Glyph*
|
|
||||||
{
|
|
||||||
const auto& lineInfo = m_drawer.GetLine(glyphPosition.y);
|
|
||||||
|
|
||||||
std::size_t cursorGlyph = GetGlyphIndex({ glyphPosition.x, glyphPosition.y });
|
|
||||||
if (glyphIndex)
|
|
||||||
*glyphIndex = cursorGlyph;
|
|
||||||
|
|
||||||
std::size_t glyphCount = m_drawer.GetGlyphCount();
|
|
||||||
if (glyphCount > 0 && lineInfo.glyphIndex < cursorGlyph)
|
|
||||||
{
|
|
||||||
const auto& glyph = m_drawer.GetGlyph(std::min(cursorGlyph, glyphCount - 1));
|
|
||||||
return &glyph;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return nullptr;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Move text so that cursor is always visible
|
|
||||||
const auto* lastGlyph = GetGlyph(m_cursorPositionEnd, nullptr);
|
|
||||||
float glyphPos = (lastGlyph) ? lastGlyph->bounds.x : 0.f;
|
|
||||||
float glyphWidth = (lastGlyph) ? lastGlyph->bounds.width : 0.f;
|
|
||||||
|
|
||||||
auto& node = m_textEntity->GetComponent<Ndk::NodeComponent>();
|
|
||||||
float textPosition = node.GetPosition(Nz::CoordSys_Local).x - paddingWidth;
|
|
||||||
float cursorPosition = glyphPos + textPosition;
|
|
||||||
float width = GetWidth();
|
|
||||||
|
|
||||||
if (width <= m_drawer.GetBounds().width)
|
|
||||||
{
|
|
||||||
if (cursorPosition + glyphWidth > width)
|
|
||||||
node.Move(width - cursorPosition - glyphWidth, 0.f);
|
|
||||||
else if (cursorPosition - glyphWidth < 0.f)
|
|
||||||
node.Move(-cursorPosition + glyphWidth, 0.f);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
node.Move(-textPosition, 0.f); // Reset text position if we have enough room to show everything
|
|
||||||
|
|
||||||
// Show cursor/selection
|
|
||||||
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 = [&](const Nz::Vector2ui& glyphPosition)
|
|
||||||
{
|
|
||||||
std::size_t glyphIndex;
|
|
||||||
const auto* glyph = GetGlyph(glyphPosition, &glyphIndex);
|
|
||||||
if (glyph)
|
|
||||||
{
|
|
||||||
float position = glyph->bounds.x;
|
|
||||||
if (glyphIndex >= m_drawer.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);
|
|
||||||
|
|
||||||
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()
|
||||||
|
|
@ -677,10 +252,4 @@ namespace Ndk
|
||||||
|
|
||||||
SetCursorPosition(m_cursorPositionBegin); //< 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextAreaWidget::UpdateTextSprite()
|
|
||||||
{
|
|
||||||
m_textSprite->Update(m_drawer);
|
|
||||||
SetPreferredSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,7 @@
|
||||||
#include <Nazara/Utility/MeshData.hpp>
|
#include <Nazara/Utility/MeshData.hpp>
|
||||||
#include <Nazara/Utility/Node.hpp>
|
#include <Nazara/Utility/Node.hpp>
|
||||||
#include <Nazara/Utility/PixelFormat.hpp>
|
#include <Nazara/Utility/PixelFormat.hpp>
|
||||||
|
#include <Nazara/Utility/RichTextDrawer.hpp>
|
||||||
#include <Nazara/Utility/Sequence.hpp>
|
#include <Nazara/Utility/Sequence.hpp>
|
||||||
#include <Nazara/Utility/SimpleTextDrawer.hpp>
|
#include <Nazara/Utility/SimpleTextDrawer.hpp>
|
||||||
#include <Nazara/Utility/SkeletalMesh.hpp>
|
#include <Nazara/Utility/SkeletalMesh.hpp>
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,8 @@ namespace Nz
|
||||||
AbstractTextDrawer() = default;
|
AbstractTextDrawer() = default;
|
||||||
virtual ~AbstractTextDrawer();
|
virtual ~AbstractTextDrawer();
|
||||||
|
|
||||||
|
virtual void Clear() = 0;
|
||||||
|
|
||||||
virtual const Recti& GetBounds() const = 0;
|
virtual const Recti& GetBounds() const = 0;
|
||||||
virtual Font* GetFont(std::size_t index) const = 0;
|
virtual Font* GetFont(std::size_t index) const = 0;
|
||||||
virtual std::size_t GetFontCount() const = 0;
|
virtual std::size_t GetFontCount() const = 0;
|
||||||
|
|
@ -35,6 +37,9 @@ namespace Nz
|
||||||
virtual const Line& GetLine(std::size_t index) const = 0;
|
virtual const Line& GetLine(std::size_t index) const = 0;
|
||||||
virtual std::size_t GetLineCount() const = 0;
|
virtual std::size_t GetLineCount() const = 0;
|
||||||
inline std::size_t GetLineGlyphCount(std::size_t index) const;
|
inline std::size_t GetLineGlyphCount(std::size_t index) const;
|
||||||
|
virtual float GetMaxLineWidth() const = 0;
|
||||||
|
|
||||||
|
virtual void SetMaxLineWidth(float lineWidth) = 0;
|
||||||
|
|
||||||
struct Glyph
|
struct Glyph
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,176 @@
|
||||||
|
// Copyright (C) 2016 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Engine - Utility module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_RICHTEXTDRAWER_HPP
|
||||||
|
#define NAZARA_RICHTEXTDRAWER_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Core/String.hpp>
|
||||||
|
#include <Nazara/Utility/AbstractTextDrawer.hpp>
|
||||||
|
#include <Nazara/Utility/Enums.hpp>
|
||||||
|
#include <Nazara/Utility/Font.hpp>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
class NAZARA_UTILITY_API RichTextDrawer : public AbstractTextDrawer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class BlockRef;
|
||||||
|
|
||||||
|
RichTextDrawer();
|
||||||
|
RichTextDrawer(const RichTextDrawer& drawer);
|
||||||
|
RichTextDrawer(RichTextDrawer&& drawer);
|
||||||
|
~RichTextDrawer();
|
||||||
|
|
||||||
|
BlockRef AppendText(const String& str, bool forceNewBlock = false);
|
||||||
|
|
||||||
|
void Clear() override;
|
||||||
|
|
||||||
|
inline std::size_t FindBlock(std::size_t glyphIndex) const;
|
||||||
|
|
||||||
|
inline unsigned int GetBlockCharacterSize(std::size_t index) const;
|
||||||
|
inline const Color& GetBlockColor(std::size_t index) const;
|
||||||
|
inline std::size_t GetBlockCount() const;
|
||||||
|
inline std::size_t GetBlockFirstGlyphIndex(std::size_t index) const;
|
||||||
|
inline const FontRef& GetBlockFont(std::size_t index) const;
|
||||||
|
inline TextStyleFlags GetBlockStyle(std::size_t index) const;
|
||||||
|
inline const String& GetBlockText(std::size_t index) const;
|
||||||
|
|
||||||
|
inline BlockRef GetBlock(std::size_t index);
|
||||||
|
const Recti& GetBounds() const override;
|
||||||
|
inline unsigned int GetDefaultCharacterSize() const;
|
||||||
|
inline const Color& GetDefaultColor() const;
|
||||||
|
inline const FontRef& GetDefaultFont() const;
|
||||||
|
inline TextStyleFlags GetDefaultStyle() const;
|
||||||
|
Font* GetFont(std::size_t index) const override;
|
||||||
|
std::size_t GetFontCount() const override;
|
||||||
|
const Glyph& GetGlyph(std::size_t index) const override;
|
||||||
|
std::size_t GetGlyphCount() const override;
|
||||||
|
const Line& GetLine(std::size_t index) const override;
|
||||||
|
std::size_t GetLineCount() const override;
|
||||||
|
float GetMaxLineWidth() const override;
|
||||||
|
|
||||||
|
inline bool HasBlocks() const;
|
||||||
|
|
||||||
|
void MergeBlocks();
|
||||||
|
|
||||||
|
void RemoveBlock(std::size_t index);
|
||||||
|
|
||||||
|
inline void SetBlockCharacterSize(std::size_t index, unsigned int characterSize);
|
||||||
|
inline void SetBlockColor(std::size_t index, const Color& color);
|
||||||
|
inline void SetBlockFont(std::size_t index, FontRef font);
|
||||||
|
inline void SetBlockStyle(std::size_t index, TextStyleFlags style);
|
||||||
|
inline void SetBlockText(std::size_t index, String str);
|
||||||
|
|
||||||
|
inline void SetDefaultCharacterSize(unsigned int characterSize);
|
||||||
|
inline void SetDefaultColor(const Color& color);
|
||||||
|
inline void SetDefaultFont(const FontRef& font);
|
||||||
|
inline void SetDefaultStyle(TextStyleFlags style);
|
||||||
|
|
||||||
|
void SetMaxLineWidth(float lineWidth) override;
|
||||||
|
|
||||||
|
RichTextDrawer& operator=(const RichTextDrawer& drawer);
|
||||||
|
RichTextDrawer& operator=(RichTextDrawer&& drawer);
|
||||||
|
|
||||||
|
static constexpr std::size_t InvalidBlockIndex = std::numeric_limits<std::size_t>::max();
|
||||||
|
|
||||||
|
//static RichTextDrawer Draw(const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White);
|
||||||
|
//static RichTextDrawer Draw(Font* font, const String& str, unsigned int characterSize, TextStyleFlags style = TextStyle_Regular, const Color& color = Color::White);
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct Block;
|
||||||
|
|
||||||
|
inline void AppendNewLine(const Font* font, unsigned int characterSize) const;
|
||||||
|
inline void ClearGlyphs() const;
|
||||||
|
inline void ConnectFontSlots();
|
||||||
|
inline void DisconnectFontSlots();
|
||||||
|
bool GenerateGlyph(Glyph& glyph, char32_t character, float outlineThickness, bool lineWrap, const Font* font, const Color& color, TextStyleFlags style, unsigned int characterSize, int renderOrder, int* advance) const;
|
||||||
|
void GenerateGlyphs(const Font* font, const Color& color, TextStyleFlags style, unsigned int characterSize, const Color& outlineColor, float outlineThickness, const String& text) const;
|
||||||
|
inline std::size_t HandleFontAddition(const FontRef& font);
|
||||||
|
inline void ReleaseFont(std::size_t fontIndex);
|
||||||
|
|
||||||
|
inline void InvalidateGlyphs();
|
||||||
|
|
||||||
|
void OnFontAtlasLayerChanged(const Font* font, AbstractImage* oldLayer, AbstractImage* newLayer);
|
||||||
|
void OnFontInvalidated(const Font* font);
|
||||||
|
void OnFontRelease(const Font* object);
|
||||||
|
|
||||||
|
void UpdateGlyphs() const;
|
||||||
|
|
||||||
|
struct Block
|
||||||
|
{
|
||||||
|
std::size_t fontIndex;
|
||||||
|
std::size_t glyphIndex;
|
||||||
|
Color color;
|
||||||
|
String text;
|
||||||
|
TextStyleFlags style;
|
||||||
|
unsigned int characterSize;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FontData
|
||||||
|
{
|
||||||
|
FontRef font;
|
||||||
|
std::size_t useCount = 0;
|
||||||
|
|
||||||
|
NazaraSlot(Font, OnFontAtlasChanged, atlasChangedSlot);
|
||||||
|
NazaraSlot(Font, OnFontAtlasLayerChanged, atlasLayerChangedSlot);
|
||||||
|
NazaraSlot(Font, OnFontGlyphCacheCleared, glyphCacheClearedSlot);
|
||||||
|
NazaraSlot(Font, OnFontRelease, fontReleaseSlot);
|
||||||
|
};
|
||||||
|
|
||||||
|
Color m_defaultColor;
|
||||||
|
TextStyleFlags m_defaultStyle;
|
||||||
|
FontRef m_defaultFont;
|
||||||
|
std::unordered_map<FontRef, std::size_t> m_fontIndexes;
|
||||||
|
std::vector<Block> m_blocks;
|
||||||
|
std::vector<FontData> m_fonts;
|
||||||
|
mutable std::vector<Glyph> m_glyphs;
|
||||||
|
mutable std::vector<Line> m_lines;
|
||||||
|
mutable Rectf m_workingBounds;
|
||||||
|
mutable Recti m_bounds;
|
||||||
|
mutable Vector2ui m_drawPos;
|
||||||
|
mutable bool m_glyphUpdated;
|
||||||
|
float m_maxLineWidth;
|
||||||
|
unsigned int m_defaultCharacterSize;
|
||||||
|
};
|
||||||
|
|
||||||
|
class RichTextDrawer::BlockRef
|
||||||
|
{
|
||||||
|
friend RichTextDrawer;
|
||||||
|
|
||||||
|
public:
|
||||||
|
BlockRef(const BlockRef&) = default;
|
||||||
|
BlockRef(BlockRef&&) = default;
|
||||||
|
~BlockRef() = default;
|
||||||
|
|
||||||
|
inline unsigned int GetCharacterSize() const;
|
||||||
|
inline Color GetColor() const;
|
||||||
|
inline std::size_t GetFirstGlyphIndex() const;
|
||||||
|
inline const FontRef& GetFont() const;
|
||||||
|
inline TextStyleFlags GetStyle() const;
|
||||||
|
inline const String& GetText() const;
|
||||||
|
|
||||||
|
inline void SetCharacterSize(unsigned int size);
|
||||||
|
inline void SetColor(Color color);
|
||||||
|
inline void SetFont(FontRef font);
|
||||||
|
inline void SetStyle(TextStyleFlags style);
|
||||||
|
inline void SetText(const String& text);
|
||||||
|
|
||||||
|
BlockRef& operator=(const BlockRef&) = default;
|
||||||
|
BlockRef& operator=(BlockRef&&) = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
inline BlockRef(RichTextDrawer& drawer, std::size_t index);
|
||||||
|
|
||||||
|
std::size_t m_blockIndex;
|
||||||
|
RichTextDrawer& m_drawer;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Utility/RichTextDrawer.inl>
|
||||||
|
|
||||||
|
#endif // NAZARA_RICHTEXTDRAWER_HPP
|
||||||
|
|
@ -0,0 +1,429 @@
|
||||||
|
// Copyright (C) 2016 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Engine - Utility module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Utility/RichTextDrawer.hpp>
|
||||||
|
#include <Nazara/Utility/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
inline std::size_t RichTextDrawer::FindBlock(std::size_t glyphIndex) const
|
||||||
|
{
|
||||||
|
auto it = m_blocks.begin();
|
||||||
|
for (; it != m_blocks.end(); ++it)
|
||||||
|
{
|
||||||
|
if (it->glyphIndex > glyphIndex)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(it != m_blocks.begin());
|
||||||
|
return std::distance(m_blocks.begin(), it) - 1;
|
||||||
|
/*
|
||||||
|
// Binary search
|
||||||
|
std::size_t count = m_blocks.size();
|
||||||
|
std::size_t step;
|
||||||
|
|
||||||
|
std::size_t i = InvalidBlockIndex;
|
||||||
|
std::size_t first = 0;
|
||||||
|
std::size_t last = count;
|
||||||
|
while (count > 0)
|
||||||
|
{
|
||||||
|
i = first;
|
||||||
|
step = count / 2;
|
||||||
|
|
||||||
|
i += step;
|
||||||
|
|
||||||
|
if (m_blocks[i].glyphIndex < glyphIndex)
|
||||||
|
{
|
||||||
|
first = ++i;
|
||||||
|
count -= step + 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
count = step;
|
||||||
|
}
|
||||||
|
|
||||||
|
return i;*/
|
||||||
|
}
|
||||||
|
|
||||||
|
inline auto RichTextDrawer::GetBlock(std::size_t index) -> BlockRef
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
return BlockRef(*this, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline unsigned int RichTextDrawer::GetBlockCharacterSize(std::size_t index) const
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
return m_blocks[index].characterSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const Color& RichTextDrawer::GetBlockColor(std::size_t index) const
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
return m_blocks[index].color;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::size_t RichTextDrawer::GetBlockCount() const
|
||||||
|
{
|
||||||
|
return m_blocks.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::size_t RichTextDrawer::GetBlockFirstGlyphIndex(std::size_t index) const
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
return m_blocks[index].glyphIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const FontRef& RichTextDrawer::GetBlockFont(std::size_t index) const
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
std::size_t fontIndex = m_blocks[index].fontIndex;
|
||||||
|
assert(fontIndex < m_fonts.size());
|
||||||
|
return m_fonts[fontIndex].font;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TextStyleFlags RichTextDrawer::GetBlockStyle(std::size_t index) const
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
return m_blocks[index].style;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const String& RichTextDrawer::GetBlockText(std::size_t index) const
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
return m_blocks[index].text;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline unsigned int RichTextDrawer::GetDefaultCharacterSize() const
|
||||||
|
{
|
||||||
|
return m_defaultCharacterSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const Color& RichTextDrawer::GetDefaultColor() const
|
||||||
|
{
|
||||||
|
return m_defaultColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const FontRef& RichTextDrawer::GetDefaultFont() const
|
||||||
|
{
|
||||||
|
return m_defaultFont;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline TextStyleFlags RichTextDrawer::GetDefaultStyle() const
|
||||||
|
{
|
||||||
|
return m_defaultStyle;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::AppendNewLine(const Font* font, unsigned int characterSize) const
|
||||||
|
{
|
||||||
|
// Ensure we're appending from last line
|
||||||
|
Line& lastLine = m_lines.back();
|
||||||
|
|
||||||
|
const Font::SizeInfo& sizeInfo = font->GetSizeInfo(characterSize);
|
||||||
|
|
||||||
|
unsigned int previousDrawPos = m_drawPos.x;
|
||||||
|
|
||||||
|
// Reset cursor
|
||||||
|
m_drawPos.x = 0;
|
||||||
|
m_drawPos.y += sizeInfo.lineHeight;
|
||||||
|
|
||||||
|
m_workingBounds.ExtendTo(lastLine.bounds);
|
||||||
|
m_lines.emplace_back(Line{ Rectf(0.f, float(sizeInfo.lineHeight * m_lines.size()), 0.f, float(sizeInfo.lineHeight)), m_glyphs.size() + 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::ClearGlyphs() const
|
||||||
|
{
|
||||||
|
m_bounds.MakeZero();
|
||||||
|
m_lines.clear();
|
||||||
|
m_glyphs.clear();
|
||||||
|
m_glyphUpdated = true;
|
||||||
|
m_workingBounds.MakeZero(); //< Compute bounds as float to speedup bounds computation (as casting between floats and integers is costly)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::ConnectFontSlots()
|
||||||
|
{
|
||||||
|
for (auto& fontData : m_fonts)
|
||||||
|
{
|
||||||
|
fontData.atlasChangedSlot.Connect(fontData.font->OnFontAtlasChanged, this, &RichTextDrawer::OnFontInvalidated);
|
||||||
|
fontData.atlasLayerChangedSlot.Connect(fontData.font->OnFontAtlasLayerChanged, this, &RichTextDrawer::OnFontAtlasLayerChanged);
|
||||||
|
fontData.fontReleaseSlot.Connect(fontData.font->OnFontDestroy, this, &RichTextDrawer::OnFontRelease);
|
||||||
|
fontData.glyphCacheClearedSlot.Connect(fontData.font->OnFontGlyphCacheCleared, this, &RichTextDrawer::OnFontInvalidated);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::DisconnectFontSlots()
|
||||||
|
{
|
||||||
|
for (auto& fontData : m_fonts)
|
||||||
|
{
|
||||||
|
fontData.atlasChangedSlot.Disconnect();
|
||||||
|
fontData.atlasLayerChangedSlot.Disconnect();
|
||||||
|
fontData.fontReleaseSlot.Disconnect();
|
||||||
|
fontData.glyphCacheClearedSlot.Disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::size_t RichTextDrawer::HandleFontAddition(const FontRef& font)
|
||||||
|
{
|
||||||
|
auto it = m_fontIndexes.find(font);
|
||||||
|
if (it == m_fontIndexes.end())
|
||||||
|
{
|
||||||
|
std::size_t fontIndex = m_fonts.size();
|
||||||
|
m_fonts.emplace_back();
|
||||||
|
auto& fontData = m_fonts.back();
|
||||||
|
fontData.font = font;
|
||||||
|
fontData.atlasChangedSlot.Connect(font->OnFontAtlasChanged, this, &RichTextDrawer::OnFontInvalidated);
|
||||||
|
fontData.atlasLayerChangedSlot.Connect(font->OnFontAtlasLayerChanged, this, &RichTextDrawer::OnFontAtlasLayerChanged);
|
||||||
|
fontData.fontReleaseSlot.Connect(font->OnFontDestroy, this, &RichTextDrawer::OnFontRelease);
|
||||||
|
fontData.glyphCacheClearedSlot.Connect(font->OnFontGlyphCacheCleared, this, &RichTextDrawer::OnFontInvalidated);
|
||||||
|
|
||||||
|
it = m_fontIndexes.emplace(font, fontIndex).first;
|
||||||
|
}
|
||||||
|
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::ReleaseFont(std::size_t fontIndex)
|
||||||
|
{
|
||||||
|
assert(fontIndex < m_fonts.size());
|
||||||
|
|
||||||
|
FontData& fontData = m_fonts[fontIndex];
|
||||||
|
assert(fontData.useCount > 0);
|
||||||
|
|
||||||
|
if (--fontData.useCount == 0)
|
||||||
|
{
|
||||||
|
// Shift font indexes
|
||||||
|
m_fontIndexes.erase(fontData.font);
|
||||||
|
for (auto it = m_fontIndexes.begin(); it != m_fontIndexes.end(); ++it)
|
||||||
|
{
|
||||||
|
if (it->second > fontIndex)
|
||||||
|
it->second--;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_fonts.erase(m_fonts.begin() + fontIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool RichTextDrawer::HasBlocks() const
|
||||||
|
{
|
||||||
|
return !m_blocks.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::SetBlockCharacterSize(std::size_t index, unsigned int characterSize)
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
m_blocks[index].characterSize = characterSize;
|
||||||
|
|
||||||
|
InvalidateGlyphs();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::SetBlockColor(std::size_t index, const Color& color)
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
m_blocks[index].color = color;
|
||||||
|
|
||||||
|
InvalidateGlyphs();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::SetBlockFont(std::size_t index, FontRef font)
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
std::size_t fontIndex = HandleFontAddition(font);
|
||||||
|
std::size_t oldFontIndex = m_blocks[index].fontIndex;
|
||||||
|
|
||||||
|
if (oldFontIndex != fontIndex)
|
||||||
|
{
|
||||||
|
ReleaseFont(oldFontIndex);
|
||||||
|
|
||||||
|
m_fonts[fontIndex].useCount++;
|
||||||
|
m_blocks[index].fontIndex = fontIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::SetBlockStyle(std::size_t index, TextStyleFlags style)
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
m_blocks[index].style = style;
|
||||||
|
|
||||||
|
InvalidateGlyphs();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::SetBlockText(std::size_t index, String str)
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
|
||||||
|
std::size_t previousLength = m_blocks[index].text.GetLength();
|
||||||
|
|
||||||
|
m_blocks[index].text = std::move(str);
|
||||||
|
|
||||||
|
std::size_t newLength = m_blocks[index].text.GetLength();
|
||||||
|
if (newLength != previousLength)
|
||||||
|
{
|
||||||
|
std::size_t delta = newLength - previousLength; //< Underflow allowed
|
||||||
|
for (std::size_t i = index + 1; i < m_blocks.size(); ++i)
|
||||||
|
m_blocks[i].glyphIndex += delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
InvalidateGlyphs();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::SetDefaultCharacterSize(unsigned int characterSize)
|
||||||
|
{
|
||||||
|
m_defaultCharacterSize = characterSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::SetDefaultColor(const Color& color)
|
||||||
|
{
|
||||||
|
m_defaultColor = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::SetDefaultFont(const FontRef& font)
|
||||||
|
{
|
||||||
|
m_defaultFont = font;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::SetDefaultStyle(TextStyleFlags style)
|
||||||
|
{
|
||||||
|
m_defaultStyle = style;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RichTextDrawer::InvalidateGlyphs()
|
||||||
|
{
|
||||||
|
m_glyphUpdated = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \class Nz::RichTextDrawer::BlockRef
|
||||||
|
* \brief Helper class representing a block inside a RichTextDrawer, allowing easier access.
|
||||||
|
*
|
||||||
|
* \warning This class is meant for temporary use, moving or destroying the RichTextDrawer or one of its blocks invalidates all BlockRef
|
||||||
|
*/
|
||||||
|
|
||||||
|
inline RichTextDrawer::BlockRef::BlockRef(RichTextDrawer& drawer, std::size_t index) :
|
||||||
|
m_blockIndex(index),
|
||||||
|
m_drawer(drawer)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Returns the character size used for the characters of the referenced block
|
||||||
|
* \return The referenced block character size
|
||||||
|
*
|
||||||
|
* \see GetColor, GetFont, GetStyle, GetText, SetCharacterSize
|
||||||
|
*/
|
||||||
|
inline unsigned int RichTextDrawer::BlockRef::GetCharacterSize() const
|
||||||
|
{
|
||||||
|
return m_drawer.GetBlockCharacterSize(m_blockIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Returns the color used for the characters of the referenced block
|
||||||
|
* \return The referenced block color
|
||||||
|
*
|
||||||
|
* \see GetCharacterSize, GetFont, GetStyle, GetText, SetColor
|
||||||
|
*/
|
||||||
|
inline Color RichTextDrawer::BlockRef::GetColor() const
|
||||||
|
{
|
||||||
|
return m_drawer.GetBlockColor(m_blockIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Returns the font used for the characters of the referenced block
|
||||||
|
* \return A reference on the referenced block font
|
||||||
|
*
|
||||||
|
* \see GetCharacterSize, GetColor, GetStyle, GetText, SetFont
|
||||||
|
*/
|
||||||
|
inline const FontRef& RichTextDrawer::BlockRef::GetFont() const
|
||||||
|
{
|
||||||
|
return m_drawer.GetBlockFont(m_blockIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Returns the style flags used for the characters of the referenced block
|
||||||
|
* \return The referenced block style flags (see TextStyleFlags)
|
||||||
|
*
|
||||||
|
* \see GetCharacterSize, GetColor, GetFont, GetText, SetStyle
|
||||||
|
*/
|
||||||
|
inline TextStyleFlags RichTextDrawer::BlockRef::GetStyle() const
|
||||||
|
{
|
||||||
|
return m_drawer.GetBlockStyle(m_blockIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Returns the first glyph index at which starts the referenced block
|
||||||
|
* \return The first glyph index concerned by this block
|
||||||
|
*
|
||||||
|
* \see GetText
|
||||||
|
*/
|
||||||
|
inline std::size_t RichTextDrawer::BlockRef::GetFirstGlyphIndex() const
|
||||||
|
{
|
||||||
|
return m_drawer.GetBlockFirstGlyphIndex(m_blockIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Returns the text of the referenced block
|
||||||
|
* \return The referenced block text
|
||||||
|
*
|
||||||
|
* \see GetCharacterSize, GetColor, GetFont, GetStyle, SetText
|
||||||
|
*/
|
||||||
|
inline const String& RichTextDrawer::BlockRef::GetText() const
|
||||||
|
{
|
||||||
|
return m_drawer.GetBlockText(m_blockIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Changes the character size of the referenced block characters
|
||||||
|
* \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur.
|
||||||
|
*
|
||||||
|
* \see GetCharacterSize, SetColor, SetFont, SetStyle, SetText
|
||||||
|
*/
|
||||||
|
inline void RichTextDrawer::BlockRef::SetCharacterSize(unsigned int size)
|
||||||
|
{
|
||||||
|
m_drawer.SetBlockCharacterSize(m_blockIndex, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Changes the color of the referenced block characters
|
||||||
|
* \remark This is the only property that can be changed without forcing a glyph regeneration
|
||||||
|
*
|
||||||
|
* \see GetColor, SetCharacterSize, SetFont, SetStyle, SetText
|
||||||
|
*/
|
||||||
|
inline void RichTextDrawer::BlockRef::SetColor(Color color)
|
||||||
|
{
|
||||||
|
m_drawer.SetBlockColor(m_blockIndex, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Changes the font of the referenced block characters
|
||||||
|
* \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur.
|
||||||
|
*
|
||||||
|
* \see GetCharacterSize, SetCharacterSize, SetColor, SetStyle, SetText
|
||||||
|
*/
|
||||||
|
inline void RichTextDrawer::BlockRef::SetFont(FontRef font)
|
||||||
|
{
|
||||||
|
m_drawer.SetBlockFont(m_blockIndex, std::move(font));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Changes the style flags of the referenced block characters
|
||||||
|
* \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur.
|
||||||
|
*
|
||||||
|
* \see GetStyle, SetCharacterSize, SetColor, SetFont, SetText
|
||||||
|
*/
|
||||||
|
inline void RichTextDrawer::BlockRef::SetStyle(TextStyleFlags style)
|
||||||
|
{
|
||||||
|
m_drawer.SetBlockStyle(m_blockIndex, style);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Changes the text of the referenced block
|
||||||
|
* \remark This invalidates the drawer and will force a (complete or partial, depending on the block index) glyph regeneration to occur.
|
||||||
|
*
|
||||||
|
* \see GetText, SetCharacterSize, SetColor, SetFont, SetStyle
|
||||||
|
*/
|
||||||
|
inline void RichTextDrawer::BlockRef::SetText(const String& text)
|
||||||
|
{
|
||||||
|
m_drawer.SetBlockText(m_blockIndex, text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Utility/DebugOff.hpp>
|
||||||
|
|
@ -26,7 +26,7 @@ namespace Nz
|
||||||
|
|
||||||
void AppendText(const String& str);
|
void AppendText(const String& str);
|
||||||
|
|
||||||
void Clear();
|
void Clear() override;
|
||||||
|
|
||||||
const Recti& GetBounds() const override;
|
const Recti& GetBounds() const override;
|
||||||
unsigned int GetCharacterSize() const;
|
unsigned int GetCharacterSize() const;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,510 @@
|
||||||
|
// Copyright (C) 2017 Jérôme Leclercq
|
||||||
|
// This file is part of the "Nazara Engine - Utility module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Utility/RichTextDrawer.hpp>
|
||||||
|
#include <limits>
|
||||||
|
#include <memory>
|
||||||
|
#include <Nazara/Utility/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
RichTextDrawer::RichTextDrawer() :
|
||||||
|
m_defaultColor(Color::White),
|
||||||
|
//m_outlineColor(Color::Black),
|
||||||
|
m_defaultStyle(TextStyle_Regular),
|
||||||
|
m_glyphUpdated(false),
|
||||||
|
//m_maxLineWidth(std::numeric_limits<float>::infinity()),
|
||||||
|
//m_outlineThickness(0.f),
|
||||||
|
m_defaultCharacterSize(24)
|
||||||
|
{
|
||||||
|
SetDefaultFont(Font::GetDefault());
|
||||||
|
}
|
||||||
|
|
||||||
|
RichTextDrawer::RichTextDrawer(const RichTextDrawer& drawer) :
|
||||||
|
m_defaultColor(drawer.m_defaultColor),
|
||||||
|
m_defaultStyle(drawer.m_defaultStyle),
|
||||||
|
m_fontIndexes(drawer.m_fontIndexes),
|
||||||
|
m_blocks(drawer.m_blocks),
|
||||||
|
m_glyphUpdated(false),
|
||||||
|
//m_outlineColor(drawer.m_outlineColor),
|
||||||
|
//m_maxLineWidth(drawer.m_maxLineWidth),
|
||||||
|
//m_outlineThickness(drawer.m_outlineThickness),
|
||||||
|
m_defaultCharacterSize(drawer.m_defaultCharacterSize)
|
||||||
|
{
|
||||||
|
m_fonts.resize(drawer.m_fonts.size());
|
||||||
|
for (std::size_t i = 0; i < m_fonts.size(); ++i)
|
||||||
|
{
|
||||||
|
m_fonts[i].font = drawer.m_fonts[i].font;
|
||||||
|
m_fonts[i].useCount = drawer.m_fonts[i].useCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetDefaultFont(drawer.m_defaultFont);
|
||||||
|
|
||||||
|
ConnectFontSlots();
|
||||||
|
}
|
||||||
|
|
||||||
|
RichTextDrawer::RichTextDrawer(RichTextDrawer&& drawer)
|
||||||
|
{
|
||||||
|
operator=(std::move(drawer));
|
||||||
|
}
|
||||||
|
|
||||||
|
RichTextDrawer::~RichTextDrawer() = default;
|
||||||
|
|
||||||
|
auto RichTextDrawer::AppendText(const String& str, bool forceNewBlock) -> BlockRef
|
||||||
|
{
|
||||||
|
NazaraAssert(!str.IsEmpty(), "String cannot be empty");
|
||||||
|
|
||||||
|
std::size_t defaultFontIndex = HandleFontAddition(m_defaultFont);
|
||||||
|
|
||||||
|
auto HasDefaultProperties = [&](const Block& block)
|
||||||
|
{
|
||||||
|
return block.characterSize == m_defaultCharacterSize &&
|
||||||
|
block.color == m_defaultColor &&
|
||||||
|
block.fontIndex == defaultFontIndex &&
|
||||||
|
block.style == m_defaultStyle;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check if last block has the same property as default, else create a new block
|
||||||
|
if (forceNewBlock || m_blocks.empty() || !HasDefaultProperties(m_blocks.back()))
|
||||||
|
{
|
||||||
|
std::size_t glyphIndex;
|
||||||
|
if (!m_blocks.empty())
|
||||||
|
{
|
||||||
|
Block& lastBlock = m_blocks.back();
|
||||||
|
glyphIndex = lastBlock.glyphIndex + lastBlock.text.GetLength();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
glyphIndex = 0;
|
||||||
|
|
||||||
|
m_blocks.emplace_back();
|
||||||
|
Block& newBlock = m_blocks.back();
|
||||||
|
newBlock.characterSize = m_defaultCharacterSize;
|
||||||
|
newBlock.color = m_defaultColor;
|
||||||
|
newBlock.fontIndex = defaultFontIndex;
|
||||||
|
newBlock.glyphIndex = glyphIndex;
|
||||||
|
newBlock.style = m_defaultStyle;
|
||||||
|
newBlock.text = str;
|
||||||
|
|
||||||
|
assert(newBlock.fontIndex < m_fonts.size());
|
||||||
|
m_fonts[newBlock.fontIndex].useCount++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_blocks.back().text += str;
|
||||||
|
|
||||||
|
InvalidateGlyphs();
|
||||||
|
|
||||||
|
return BlockRef(*this, m_blocks.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextDrawer::Clear()
|
||||||
|
{
|
||||||
|
m_fontIndexes.clear();
|
||||||
|
m_blocks.clear();
|
||||||
|
m_fonts.clear();
|
||||||
|
m_glyphs.clear();
|
||||||
|
ClearGlyphs();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Recti& RichTextDrawer::GetBounds() const
|
||||||
|
{
|
||||||
|
if (!m_glyphUpdated)
|
||||||
|
UpdateGlyphs();
|
||||||
|
|
||||||
|
return m_bounds;
|
||||||
|
}
|
||||||
|
|
||||||
|
Font* RichTextDrawer::GetFont(std::size_t index) const
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_fonts.size(), "Font index out of range");
|
||||||
|
|
||||||
|
return m_fonts[index].font;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t RichTextDrawer::GetFontCount() const
|
||||||
|
{
|
||||||
|
return m_fonts.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const AbstractTextDrawer::Glyph& RichTextDrawer::GetGlyph(std::size_t index) const
|
||||||
|
{
|
||||||
|
if (!m_glyphUpdated)
|
||||||
|
UpdateGlyphs();
|
||||||
|
|
||||||
|
return m_glyphs[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t RichTextDrawer::GetGlyphCount() const
|
||||||
|
{
|
||||||
|
if (!m_glyphUpdated)
|
||||||
|
UpdateGlyphs();
|
||||||
|
|
||||||
|
return m_glyphs.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
const AbstractTextDrawer::Line& RichTextDrawer::GetLine(std::size_t index) const
|
||||||
|
{
|
||||||
|
if (!m_glyphUpdated)
|
||||||
|
UpdateGlyphs();
|
||||||
|
|
||||||
|
NazaraAssert(index < m_lines.size(), "Line index out of range");
|
||||||
|
return m_lines[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t RichTextDrawer::GetLineCount() const
|
||||||
|
{
|
||||||
|
if (!m_glyphUpdated)
|
||||||
|
UpdateGlyphs();
|
||||||
|
|
||||||
|
return m_lines.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
float RichTextDrawer::GetMaxLineWidth() const
|
||||||
|
{
|
||||||
|
return m_maxLineWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextDrawer::MergeBlocks()
|
||||||
|
{
|
||||||
|
auto TestBlockProperties = [](const Block& lhs, const Block& rhs)
|
||||||
|
{
|
||||||
|
return lhs.characterSize == rhs.characterSize &&
|
||||||
|
lhs.color == rhs.color &&
|
||||||
|
lhs.fontIndex == rhs.fontIndex &&
|
||||||
|
lhs.style == rhs.style;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::size_t previousBlockIndex = 0;
|
||||||
|
for (std::size_t i = 1; i < m_blocks.size(); ++i)
|
||||||
|
{
|
||||||
|
if (TestBlockProperties(m_blocks[previousBlockIndex], m_blocks[i]))
|
||||||
|
{
|
||||||
|
RemoveBlock(i);
|
||||||
|
--i;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
previousBlockIndex = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextDrawer::RemoveBlock(std::size_t index)
|
||||||
|
{
|
||||||
|
NazaraAssert(index < m_blocks.size(), "Invalid block index");
|
||||||
|
|
||||||
|
std::size_t textLength = m_blocks[index].text.GetLength();
|
||||||
|
|
||||||
|
ReleaseFont(m_blocks[index].fontIndex);
|
||||||
|
m_blocks.erase(m_blocks.begin() + index);
|
||||||
|
|
||||||
|
for (std::size_t i = index; i < m_blocks.size(); ++i)
|
||||||
|
{
|
||||||
|
assert(m_blocks[i].glyphIndex > textLength);
|
||||||
|
m_blocks[i].glyphIndex -= textLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
InvalidateGlyphs();
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextDrawer::SetMaxLineWidth(float lineWidth)
|
||||||
|
{
|
||||||
|
m_maxLineWidth = lineWidth;
|
||||||
|
|
||||||
|
//TODO: Implement max line width
|
||||||
|
}
|
||||||
|
|
||||||
|
RichTextDrawer& RichTextDrawer::operator=(const RichTextDrawer& drawer)
|
||||||
|
{
|
||||||
|
DisconnectFontSlots();
|
||||||
|
|
||||||
|
m_blocks = drawer.m_blocks;
|
||||||
|
m_defaultCharacterSize = drawer.m_defaultCharacterSize;
|
||||||
|
m_defaultColor = drawer.m_defaultColor;
|
||||||
|
m_defaultFont = drawer.m_defaultFont;
|
||||||
|
m_defaultStyle = drawer.m_defaultStyle;
|
||||||
|
m_fontIndexes = drawer.m_fontIndexes;
|
||||||
|
|
||||||
|
m_fonts.resize(drawer.m_fonts.size());
|
||||||
|
for (std::size_t i = 0; i < m_fonts.size(); ++i)
|
||||||
|
{
|
||||||
|
m_fonts[i].font = drawer.m_fonts[i].font;
|
||||||
|
m_fonts[i].useCount = drawer.m_fonts[i].useCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
ConnectFontSlots();
|
||||||
|
InvalidateGlyphs();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
RichTextDrawer& RichTextDrawer::operator=(RichTextDrawer&& drawer)
|
||||||
|
{
|
||||||
|
m_blocks = std::move(drawer.m_blocks);
|
||||||
|
m_bounds = std::move(m_bounds);
|
||||||
|
m_defaultCharacterSize = std::move(drawer.m_defaultCharacterSize);
|
||||||
|
m_defaultColor = std::move(drawer.m_defaultColor);
|
||||||
|
m_defaultFont = std::move(drawer.m_defaultFont);
|
||||||
|
m_defaultStyle = std::move(drawer.m_defaultStyle);
|
||||||
|
m_drawPos = std::move(m_drawPos);
|
||||||
|
m_fontIndexes = std::move(drawer.m_fontIndexes);
|
||||||
|
m_fonts = std::move(drawer.m_fonts);
|
||||||
|
m_glyphs = std::move(m_glyphs);
|
||||||
|
m_lines = std::move(m_lines);
|
||||||
|
m_glyphUpdated = std::move(m_glyphUpdated);
|
||||||
|
m_workingBounds = std::move(m_workingBounds);
|
||||||
|
|
||||||
|
drawer.DisconnectFontSlots();
|
||||||
|
ConnectFontSlots();
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RichTextDrawer::GenerateGlyph(Glyph& glyph, char32_t character, float outlineThickness, bool lineWrap, const Font* font, const Color& color, TextStyleFlags style, unsigned int characterSize, int renderOrder, int* advance) const
|
||||||
|
{
|
||||||
|
const Font::Glyph& fontGlyph = font->GetGlyph(characterSize, style, outlineThickness, character);
|
||||||
|
if (fontGlyph.valid && fontGlyph.fauxOutlineThickness <= 0.f)
|
||||||
|
{
|
||||||
|
glyph.atlas = font->GetAtlas()->GetLayer(fontGlyph.layerIndex);
|
||||||
|
glyph.atlasRect = fontGlyph.atlasRect;
|
||||||
|
glyph.color = color;
|
||||||
|
glyph.flipped = fontGlyph.flipped;
|
||||||
|
glyph.renderOrder = renderOrder;
|
||||||
|
|
||||||
|
glyph.bounds.Set(fontGlyph.aabb);
|
||||||
|
|
||||||
|
//if (lineWrap && ShouldLineWrap(glyph, glyph.bounds.width))
|
||||||
|
// AppendNewLine(m_lastSeparatorGlyph, m_lastSeparatorPosition);
|
||||||
|
|
||||||
|
glyph.bounds.x += m_drawPos.x;
|
||||||
|
glyph.bounds.y += m_drawPos.y;
|
||||||
|
|
||||||
|
// Faux bold and faux outline thickness are not supported
|
||||||
|
|
||||||
|
// We "lean" the glyph to simulate italics style
|
||||||
|
float italic = (fontGlyph.requireFauxItalic) ? 0.208f : 0.f;
|
||||||
|
float italicTop = italic * glyph.bounds.y;
|
||||||
|
float italicBottom = italic * glyph.bounds.GetMaximum().y;
|
||||||
|
|
||||||
|
glyph.corners[0].Set(glyph.bounds.x - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
|
||||||
|
glyph.corners[1].Set(glyph.bounds.x + glyph.bounds.width - italicTop - outlineThickness, glyph.bounds.y - outlineThickness);
|
||||||
|
glyph.corners[2].Set(glyph.bounds.x - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
|
||||||
|
glyph.corners[3].Set(glyph.bounds.x + glyph.bounds.width - italicBottom - outlineThickness, glyph.bounds.y + glyph.bounds.height - outlineThickness);
|
||||||
|
|
||||||
|
if (advance)
|
||||||
|
*advance = fontGlyph.advance;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
void RichTextDrawer::GenerateGlyphs(const Font* font, const Color& color, TextStyleFlags style, unsigned int characterSize, const Color& outlineColor, float outlineThickness, const String& text) const
|
||||||
|
{
|
||||||
|
if (text.IsEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
///TODO: Allow iteration on Unicode characters without allocating any buffer
|
||||||
|
std::u32string characters = text.GetUtf32String();
|
||||||
|
if (characters.empty())
|
||||||
|
{
|
||||||
|
NazaraError("Invalid character set");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char32_t previousCharacter = 0;
|
||||||
|
|
||||||
|
const Font::SizeInfo& sizeInfo = font->GetSizeInfo(characterSize);
|
||||||
|
|
||||||
|
float heightDifference = sizeInfo.lineHeight - m_lines.back().bounds.height;
|
||||||
|
if (heightDifference > 0.f)
|
||||||
|
{
|
||||||
|
for (std::size_t glyphIndex = m_lines.back().glyphIndex; glyphIndex < m_glyphs.size(); ++glyphIndex)
|
||||||
|
{
|
||||||
|
Glyph& glyph = m_glyphs[glyphIndex];
|
||||||
|
glyph.bounds.y += heightDifference;
|
||||||
|
|
||||||
|
for (auto& corner : glyph.corners)
|
||||||
|
corner.y += heightDifference;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_drawPos.y += heightDifference;
|
||||||
|
m_lines.back().bounds.height += heightDifference;
|
||||||
|
}
|
||||||
|
/*if (firstFont.font)
|
||||||
|
m_lines.emplace_back(Line{ Rectf(0.f, 0.f, 0.f, float(font->GetSizeInfo(firstBlock.characterSize).lineHeight)), 0 });
|
||||||
|
else
|
||||||
|
m_lines.emplace_back(Line{ Rectf::Zero(), 0 });*/
|
||||||
|
|
||||||
|
m_glyphs.reserve(m_glyphs.size() + characters.size() * ((outlineThickness > 0.f) ? 2 : 1));
|
||||||
|
for (char32_t character : characters)
|
||||||
|
{
|
||||||
|
if (previousCharacter != 0)
|
||||||
|
m_drawPos.x += font->GetKerning(characterSize, previousCharacter, character);
|
||||||
|
|
||||||
|
previousCharacter = character;
|
||||||
|
|
||||||
|
bool whitespace = true;
|
||||||
|
int advance = 0;
|
||||||
|
switch (character)
|
||||||
|
{
|
||||||
|
case ' ':
|
||||||
|
case '\n':
|
||||||
|
advance = sizeInfo.spaceAdvance;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case '\t':
|
||||||
|
advance = sizeInfo.spaceAdvance * 4;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
whitespace = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Glyph glyph;
|
||||||
|
if (!whitespace)
|
||||||
|
{
|
||||||
|
if (!GenerateGlyph(glyph, character, 0.f, true, font, color, style, characterSize, 0, &advance))
|
||||||
|
continue; // Glyph failed to load, just skip it (can't do much)
|
||||||
|
|
||||||
|
if (outlineThickness > 0.f)
|
||||||
|
{
|
||||||
|
Glyph outlineGlyph;
|
||||||
|
if (GenerateGlyph(outlineGlyph, character, outlineThickness, false, font, outlineColor, style, characterSize, -1, nullptr))
|
||||||
|
{
|
||||||
|
m_glyphs.push_back(outlineGlyph);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
float glyphAdvance = advance;
|
||||||
|
|
||||||
|
//if (ShouldLineWrap(glyph, glyphAdvance))
|
||||||
|
// AppendNewLine(m_lastSeparatorGlyph, m_lastSeparatorPosition);
|
||||||
|
|
||||||
|
glyph.atlas = nullptr;
|
||||||
|
glyph.bounds.Set(float(m_drawPos.x), m_lines.back().bounds.y, glyphAdvance, float(sizeInfo.lineHeight));
|
||||||
|
|
||||||
|
glyph.corners[0].Set(glyph.bounds.GetCorner(RectCorner_LeftTop));
|
||||||
|
glyph.corners[1].Set(glyph.bounds.GetCorner(RectCorner_RightTop));
|
||||||
|
glyph.corners[2].Set(glyph.bounds.GetCorner(RectCorner_LeftBottom));
|
||||||
|
glyph.corners[3].Set(glyph.bounds.GetCorner(RectCorner_RightBottom));
|
||||||
|
}
|
||||||
|
|
||||||
|
m_lines.back().bounds.ExtendTo(glyph.bounds);
|
||||||
|
|
||||||
|
switch (character)
|
||||||
|
{
|
||||||
|
case '\n':
|
||||||
|
{
|
||||||
|
AppendNewLine(font, characterSize);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
m_drawPos.x += advance;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*if (whitespace)
|
||||||
|
{
|
||||||
|
m_lastSeparatorGlyph = m_glyphs.size();
|
||||||
|
m_lastSeparatorPosition = m_drawPos.x;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
m_glyphs.push_back(glyph);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_workingBounds.ExtendTo(m_lines.back().bounds);
|
||||||
|
|
||||||
|
m_bounds.Set(Rectf(std::floor(m_workingBounds.x), std::floor(m_workingBounds.y), std::ceil(m_workingBounds.width), std::ceil(m_workingBounds.height)));
|
||||||
|
|
||||||
|
m_glyphUpdated = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextDrawer::OnFontAtlasLayerChanged(const Font* font, AbstractImage* oldLayer, AbstractImage* newLayer)
|
||||||
|
{
|
||||||
|
NazaraUnused(font);
|
||||||
|
|
||||||
|
#ifdef NAZARA_DEBUG
|
||||||
|
auto it = std::find_if(m_fonts.begin(), m_fonts.end(), [font](const auto& fontData) { return fontData.font == font; });
|
||||||
|
if (it == m_fonts.end())
|
||||||
|
{
|
||||||
|
NazaraInternalError("Not listening to " + String::Pointer(font));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Update atlas layer pointer
|
||||||
|
// Note: This can happen while updating
|
||||||
|
for (Glyph& glyph : m_glyphs)
|
||||||
|
{
|
||||||
|
if (glyph.atlas == oldLayer)
|
||||||
|
glyph.atlas = newLayer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextDrawer::OnFontInvalidated(const Font* font)
|
||||||
|
{
|
||||||
|
NazaraUnused(font);
|
||||||
|
|
||||||
|
#ifdef NAZARA_DEBUG
|
||||||
|
auto it = std::find_if(m_fonts.begin(), m_fonts.end(), [font](const auto& fontData) { return fontData.font == font; });
|
||||||
|
if (it == m_fonts.end())
|
||||||
|
{
|
||||||
|
NazaraInternalError("Not listening to " + String::Pointer(font));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
m_glyphUpdated = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextDrawer::OnFontRelease(const Font* font)
|
||||||
|
{
|
||||||
|
NazaraUnused(font);
|
||||||
|
NazaraUnused(font);
|
||||||
|
|
||||||
|
#ifdef NAZARA_DEBUG
|
||||||
|
auto it = std::find_if(m_fonts.begin(), m_fonts.end(), [font](const auto& fontData) { return fontData.font == font; });
|
||||||
|
if (it == m_fonts.end())
|
||||||
|
{
|
||||||
|
NazaraInternalError("Not listening to " + String::Pointer(font));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//SetFont(nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void RichTextDrawer::UpdateGlyphs() const
|
||||||
|
{
|
||||||
|
ClearGlyphs();
|
||||||
|
|
||||||
|
if (!m_blocks.empty())
|
||||||
|
{
|
||||||
|
const Block& firstBlock = m_blocks.front();
|
||||||
|
|
||||||
|
assert(firstBlock.fontIndex < m_fonts.size());
|
||||||
|
const auto& firstFont = m_fonts[firstBlock.fontIndex];
|
||||||
|
|
||||||
|
if (firstFont.font)
|
||||||
|
m_lines.emplace_back(Line{ Rectf(0.f, 0.f, 0.f, float(firstFont.font->GetSizeInfo(firstBlock.characterSize).lineHeight)), 0 });
|
||||||
|
else
|
||||||
|
m_lines.emplace_back(Line{ Rectf::Zero(), 0 });
|
||||||
|
|
||||||
|
m_drawPos.Set(0, firstBlock.characterSize);
|
||||||
|
|
||||||
|
for (const Block& block : m_blocks)
|
||||||
|
{
|
||||||
|
assert(block.fontIndex < m_fonts.size());
|
||||||
|
const auto& fontData = m_fonts[block.fontIndex];
|
||||||
|
|
||||||
|
GenerateGlyphs(fontData.font, block.color, block.style, block.characterSize, block.color, 0.f, block.text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
m_lines.emplace_back(Line{ Rectf::Zero(), 0 }); //< Ensure there's always a line
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -452,7 +452,7 @@ namespace Nz
|
||||||
|
|
||||||
const Font::SizeInfo& sizeInfo = m_font->GetSizeInfo(m_characterSize);
|
const Font::SizeInfo& sizeInfo = m_font->GetSizeInfo(m_characterSize);
|
||||||
|
|
||||||
m_glyphs.reserve(m_glyphs.size() + characters.size() * (m_outlineThickness > 0.f) ? 2 : 1);
|
m_glyphs.reserve(m_glyphs.size() + characters.size() * ((m_outlineThickness > 0.f) ? 2 : 1));
|
||||||
for (char32_t character : characters)
|
for (char32_t character : characters)
|
||||||
{
|
{
|
||||||
if (m_previousCharacter != 0)
|
if (m_previousCharacter != 0)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue