Sdk/TextAreaWidget: Add support for EchoMode
This commit is contained in:
parent
4a238f825a
commit
bb6e032b60
|
|
@ -16,6 +16,7 @@ Nazara Development Kit:
|
|||
- ⚠️ Removed TextAreaWidget::GetLineCount
|
||||
- Fix World movement which could cause crashes when updating systems
|
||||
- Fix crash occuring sometimes on keyboard event
|
||||
- Add support for EchoMode to TextAreaWidget (which allows to setup password text area)
|
||||
|
||||
# 0.4:
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,15 @@ namespace Ndk
|
|||
|
||||
CheckboxState_Max = CheckboxState_Unchecked
|
||||
};
|
||||
|
||||
enum EchoMode
|
||||
{
|
||||
EchoMode_Normal,
|
||||
EchoMode_Password,
|
||||
EchoMode_PasswordExceptLast,
|
||||
|
||||
EchoMode_Max = EchoMode_PasswordExceptLast
|
||||
};
|
||||
}
|
||||
|
||||
#endif // NAZARA_ENUMS_SDK_HPP
|
||||
|
|
|
|||
|
|
@ -7,10 +7,10 @@
|
|||
#ifndef NDK_WIDGETS_TEXTAREAWIDGET_HPP
|
||||
#define NDK_WIDGETS_TEXTAREAWIDGET_HPP
|
||||
|
||||
#include <NDK/Prerequesites.hpp>
|
||||
#include <NDK/BaseWidget.hpp>
|
||||
#include <Nazara/Utility/SimpleTextDrawer.hpp>
|
||||
#include <Nazara/Graphics/TextSprite.hpp>
|
||||
#include <Nazara/Utility/SimpleTextDrawer.hpp>
|
||||
#include <NDK/BaseWidget.hpp>
|
||||
#include <NDK/Widgets/Enums.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
|
|
@ -30,9 +30,11 @@ namespace Ndk
|
|||
|
||||
inline void EnableMultiline(bool enable = true);
|
||||
|
||||
inline unsigned int GetCharacterSize() const;
|
||||
inline const Nz::Vector2ui& GetCursorPosition() const;
|
||||
inline const Nz::String& GetDisplayText() const;
|
||||
inline EchoMode GetEchoMode() const;
|
||||
inline std::size_t GetGlyphUnderCursor() const;
|
||||
inline std::size_t GetLineCount() const;
|
||||
inline const Nz::String& GetText() const;
|
||||
inline const Nz::Color& GetTextColor() const;
|
||||
|
||||
|
|
@ -46,8 +48,10 @@ namespace Ndk
|
|||
|
||||
void ResizeToContent() override;
|
||||
|
||||
inline 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 SetText(const Nz::String& text);
|
||||
inline void SetTextColor(const Nz::Color& text);
|
||||
|
|
@ -77,11 +81,14 @@ namespace Ndk
|
|||
void OnTextEntered(char32_t character, bool repeated) override;
|
||||
|
||||
void RefreshCursor();
|
||||
void UpdateDisplayText();
|
||||
|
||||
EchoMode m_echoMode;
|
||||
EntityHandle m_cursorEntity;
|
||||
EntityHandle m_textEntity;
|
||||
Nz::SimpleTextDrawer m_drawer;
|
||||
Nz::SpriteRef m_cursorSprite;
|
||||
Nz::String m_text;
|
||||
Nz::TextSpriteRef m_textSprite;
|
||||
Nz::Vector2ui m_cursorPosition;
|
||||
std::size_t m_cursorGlyph;
|
||||
|
|
|
|||
|
|
@ -20,24 +20,34 @@ namespace Ndk
|
|||
m_multiLineEnabled = enable;
|
||||
}
|
||||
|
||||
inline unsigned int TextAreaWidget::GetCharacterSize() const
|
||||
{
|
||||
return m_drawer.GetCharacterSize();
|
||||
}
|
||||
|
||||
inline const Nz::Vector2ui& TextAreaWidget::GetCursorPosition() const
|
||||
{
|
||||
return m_cursorPosition;
|
||||
}
|
||||
|
||||
inline const Nz::String & TextAreaWidget::GetDisplayText() const
|
||||
{
|
||||
return m_drawer.GetText();
|
||||
}
|
||||
|
||||
inline EchoMode TextAreaWidget::GetEchoMode() const
|
||||
{
|
||||
return m_echoMode;
|
||||
}
|
||||
|
||||
inline std::size_t Ndk::TextAreaWidget::GetGlyphUnderCursor() const
|
||||
{
|
||||
return m_cursorGlyph;
|
||||
}
|
||||
|
||||
inline std::size_t TextAreaWidget::GetLineCount() const
|
||||
{
|
||||
return m_drawer.GetLineCount();
|
||||
}
|
||||
|
||||
inline const Nz::String& TextAreaWidget::GetText() const
|
||||
{
|
||||
return m_drawer.GetText();
|
||||
return m_text;
|
||||
}
|
||||
|
||||
inline const Nz::Color& TextAreaWidget::GetTextColor() const
|
||||
|
|
@ -92,6 +102,11 @@ namespace Ndk
|
|||
SetCursorPosition(cursorPosition);
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::SetCharacterSize(unsigned int characterSize)
|
||||
{
|
||||
m_drawer.SetCharacterSize(characterSize);
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::SetCursorPosition(std::size_t glyphIndex)
|
||||
{
|
||||
OnTextAreaCursorMove(this, &glyphIndex);
|
||||
|
|
@ -110,8 +125,8 @@ namespace Ndk
|
|||
|
||||
const auto& lineInfo = m_drawer.GetLine(line);
|
||||
|
||||
m_cursorPosition.y = line;
|
||||
m_cursorPosition.x = m_cursorGlyph - lineInfo.glyphIndex;
|
||||
m_cursorPosition.y = static_cast<unsigned int>(line);
|
||||
m_cursorPosition.x = static_cast<unsigned int>(m_cursorGlyph - lineInfo.glyphIndex);
|
||||
|
||||
RefreshCursor();
|
||||
}
|
||||
|
|
@ -140,6 +155,13 @@ namespace Ndk
|
|||
RefreshCursor();
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::SetEchoMode(EchoMode echoMode)
|
||||
{
|
||||
m_echoMode = echoMode;
|
||||
|
||||
UpdateDisplayText();
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::SetReadOnly(bool readOnly)
|
||||
{
|
||||
m_readOnly = readOnly;
|
||||
|
|
@ -149,11 +171,9 @@ namespace Ndk
|
|||
|
||||
inline void TextAreaWidget::SetText(const Nz::String& text)
|
||||
{
|
||||
m_drawer.SetText(text);
|
||||
m_text = text;
|
||||
|
||||
m_textSprite->Update(m_drawer);
|
||||
|
||||
SetCursorPosition(m_cursorPosition); //< Refresh cursor position (prevent it from being outside of the text)
|
||||
UpdateDisplayText();
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::SetTextColor(const Nz::Color& text)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ namespace Ndk
|
|||
{
|
||||
TextAreaWidget::TextAreaWidget(BaseWidget* parent) :
|
||||
BaseWidget(parent),
|
||||
m_echoMode(EchoMode_Normal),
|
||||
m_cursorPosition(0U, 0U),
|
||||
m_cursorGlyph(0),
|
||||
m_multiLineEnabled(false),
|
||||
|
|
@ -38,7 +39,34 @@ namespace Ndk
|
|||
|
||||
void TextAreaWidget::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;
|
||||
}
|
||||
}
|
||||
|
||||
m_textSprite->Update(m_drawer);
|
||||
}
|
||||
|
|
@ -87,9 +115,8 @@ namespace Ndk
|
|||
}
|
||||
else
|
||||
{
|
||||
Nz::String currentText = m_drawer.GetText();
|
||||
currentText.Insert(currentText.GetCharacterPosition(m_cursorGlyph), text);
|
||||
SetText(currentText);
|
||||
m_text.Insert(m_text.GetCharacterPosition(m_cursorGlyph), text);
|
||||
SetText(m_text);
|
||||
|
||||
SetCursorPosition(m_cursorGlyph + text.GetLength());
|
||||
}
|
||||
|
|
@ -110,17 +137,14 @@ namespace Ndk
|
|||
{
|
||||
case Nz::Keyboard::Delete:
|
||||
{
|
||||
const Nz::String& text = m_drawer.GetText();
|
||||
|
||||
Nz::String newText;
|
||||
if (m_cursorGlyph > 0)
|
||||
newText.Append(text.SubString(0, text.GetCharacterPosition(m_cursorGlyph) - 1));
|
||||
newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(m_cursorGlyph) - 1));
|
||||
|
||||
if (m_cursorGlyph < m_drawer.GetGlyphCount())
|
||||
newText.Append(text.SubString(text.GetCharacterPosition(m_cursorGlyph + 1)));
|
||||
if (m_cursorGlyph < m_text.GetLength())
|
||||
newText.Append(m_text.SubString(m_text.GetCharacterPosition(m_cursorGlyph + 1)));
|
||||
|
||||
m_drawer.SetText(newText);
|
||||
m_textSprite->Update(m_drawer);
|
||||
SetText(newText);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -220,14 +244,12 @@ namespace Ndk
|
|||
if (ignoreDefaultAction)
|
||||
break;
|
||||
|
||||
const Nz::String& text = m_drawer.GetText();
|
||||
|
||||
Nz::String newText;
|
||||
if (m_cursorGlyph > 1)
|
||||
newText.Append(text.SubString(0, text.GetCharacterPosition(m_cursorGlyph - 1) - 1));
|
||||
if (m_cursorGlyph > 0)
|
||||
newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(m_cursorGlyph - 1) - 1));
|
||||
|
||||
if (m_cursorGlyph < m_drawer.GetGlyphCount())
|
||||
newText.Append(text.SubString(text.GetCharacterPosition(m_cursorGlyph)));
|
||||
if (m_cursorGlyph < m_text.GetLength())
|
||||
newText.Append(m_text.SubString(m_text.GetCharacterPosition(m_cursorGlyph + 1)));
|
||||
|
||||
MoveCursor({-1, 0});
|
||||
SetText(newText);
|
||||
|
|
@ -278,4 +300,23 @@ namespace Ndk
|
|||
|
||||
m_cursorEntity->GetComponent<NodeComponent>().SetPosition(contentOrigin.x + position, contentOrigin.y + lineInfo.bounds.y);
|
||||
}
|
||||
|
||||
void TextAreaWidget::UpdateDisplayText()
|
||||
{
|
||||
switch (m_echoMode)
|
||||
{
|
||||
case EchoMode_Normal:
|
||||
m_drawer.SetText(m_text);
|
||||
break;
|
||||
|
||||
case EchoMode_Password:
|
||||
case EchoMode_PasswordExceptLast:
|
||||
m_drawer.SetText(Nz::String(m_text.GetLength(), '*'));
|
||||
break;
|
||||
}
|
||||
|
||||
m_textSprite->Update(m_drawer);
|
||||
|
||||
SetCursorPosition(m_cursorPosition); //< Refresh cursor position (prevent it from being outside of the text)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue