From ec795269ad3fb414043126cff6077e2c5e735e61 Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 18 Jan 2017 23:38:29 +0100 Subject: [PATCH] Sdk/TextAreaWidget: Add signals (allowing for controlling the cursor/keys) --- SDK/include/NDK/Widgets/TextAreaWidget.hpp | 8 ++++ SDK/include/NDK/Widgets/TextAreaWidget.inl | 4 ++ SDK/src/NDK/Widgets/TextAreaWidget.cpp | 55 +++++++++++++++++++++- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/SDK/include/NDK/Widgets/TextAreaWidget.hpp b/SDK/include/NDK/Widgets/TextAreaWidget.hpp index 311a1a840..6581a9965 100644 --- a/SDK/include/NDK/Widgets/TextAreaWidget.hpp +++ b/SDK/include/NDK/Widgets/TextAreaWidget.hpp @@ -56,6 +56,14 @@ namespace Ndk TextAreaWidget& operator=(const TextAreaWidget&) = delete; TextAreaWidget& operator=(TextAreaWidget&&) = default; + NazaraSignal(OnTextAreaCursorMove, const TextAreaWidget* /*textArea*/, std::size_t* /*newCursorPosition*/); + NazaraSignal(OnTextAreaKeyBackspace, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/); + NazaraSignal(OnTextAreaKeyDown, 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*/); + private: void Layout() override; diff --git a/SDK/include/NDK/Widgets/TextAreaWidget.inl b/SDK/include/NDK/Widgets/TextAreaWidget.inl index 711f8de8c..4ddc6d114 100644 --- a/SDK/include/NDK/Widgets/TextAreaWidget.inl +++ b/SDK/include/NDK/Widgets/TextAreaWidget.inl @@ -66,6 +66,8 @@ namespace Ndk inline void TextAreaWidget::SetCursorPosition(std::size_t cursorPosition) { + OnTextAreaCursorMove(this, &cursorPosition); + m_cursorPosition = std::min(cursorPosition, m_drawer.GetGlyphCount()); RefreshCursor(); @@ -83,6 +85,8 @@ namespace Ndk m_drawer.SetText(text); m_textSprite->Update(m_drawer); + + SetCursorPosition(m_cursorPosition); //< Refresh cursor position (prevent it from being outside of the text) } inline void TextAreaWidget::SetTextColor(const Nz::Color& text) diff --git a/SDK/src/NDK/Widgets/TextAreaWidget.cpp b/SDK/src/NDK/Widgets/TextAreaWidget.cpp index 089f8a380..99e450949 100644 --- a/SDK/src/NDK/Widgets/TextAreaWidget.cpp +++ b/SDK/src/NDK/Widgets/TextAreaWidget.cpp @@ -123,13 +123,53 @@ namespace Ndk break; } + case Nz::Keyboard::Down: + { + bool ignoreDefaultAction = false; + OnTextAreaKeyDown(this, &ignoreDefaultAction); + + if (ignoreDefaultAction) + break; + + //TODO + break; + } + case Nz::Keyboard::Left: + { + bool ignoreDefaultAction = false; + OnTextAreaKeyLeft(this, &ignoreDefaultAction); + + if (ignoreDefaultAction) + break; + MoveCursor(-1); break; + } case Nz::Keyboard::Right: + { + bool ignoreDefaultAction = false; + OnTextAreaKeyRight(this, &ignoreDefaultAction); + + if (ignoreDefaultAction) + break; + MoveCursor(1); break; + } + + case Nz::Keyboard::Up: + { + bool ignoreDefaultAction = false; + OnTextAreaKeyUp(this, &ignoreDefaultAction); + + if (ignoreDefaultAction) + break; + + //TODO + break; + } } } @@ -170,6 +210,12 @@ namespace Ndk { case '\b': { + bool ignoreDefaultAction = false; + OnTextAreaKeyBackspace(this, &ignoreDefaultAction); + + if (ignoreDefaultAction) + break; + const Nz::String& text = m_drawer.GetText(); Nz::String newText; @@ -179,18 +225,23 @@ namespace Ndk if (m_cursorPosition < m_drawer.GetGlyphCount()) newText.Append(text.SubString(text.GetCharacterPosition(m_cursorPosition))); - SetText(newText); MoveCursor(-1); + SetText(newText); break; } case '\r': case '\n': - if (!m_multiLineEnabled) + { + bool ignoreDefaultAction = false; + OnTextAreaKeyReturn(this, &ignoreDefaultAction); + + if (ignoreDefaultAction || !m_multiLineEnabled) break; Write(Nz::String('\n')); break; + } default: {