From 091840bde0806363ed6f3c06bb5c1ce3d7bf0930 Mon Sep 17 00:00:00 2001 From: Edward Paowz Date: Mon, 14 Nov 2022 08:45:45 +0100 Subject: [PATCH 1/2] Added minimal password field support --- include/nanogui/textbox.h | 5 +++++ src/textbox.cpp | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/nanogui/textbox.h b/include/nanogui/textbox.h index b480673b..44621c1c 100644 --- a/include/nanogui/textbox.h +++ b/include/nanogui/textbox.h @@ -73,6 +73,9 @@ class NANOGUI_EXPORT TextBox : public Widget { /// Specify a placeholder text to be displayed while the text box is empty. void set_placeholder(const std::string &placeholder) { m_placeholder = placeholder; } + /// Turn the textbox into a password field. + void set_password_field(bool enable, char mask = '*'); + /// Set the \ref Theme used to draw this widget virtual void set_theme(Theme *theme) override; @@ -113,6 +116,8 @@ class NANOGUI_EXPORT TextBox : public Widget { bool m_editable; bool m_spinnable; bool m_committed; + bool m_password; + char m_password_mask; std::string m_value; std::string m_default_value; Alignment m_alignment; diff --git a/src/textbox.cpp b/src/textbox.cpp index 4f7d1184..5b6e209b 100644 --- a/src/textbox.cpp +++ b/src/textbox.cpp @@ -27,6 +27,8 @@ TextBox::TextBox(Widget *parent, const std::string &value) m_editable(false), m_spinnable(false), m_committed(true), + m_password(false), + m_password_mask('*'), m_value(value), m_default_value(""), m_alignment(Alignment::Center), @@ -211,7 +213,13 @@ void TextBox::draw(NVGcontext* ctx) { draw_pos.x() += m_text_offset; if (m_committed) { - nvgText(ctx, draw_pos.x(), draw_pos.y(), m_value.empty() ? m_placeholder.c_str() : m_value.c_str(), nullptr); + nvgText(ctx, draw_pos.x(), draw_pos.y(), + m_value.empty() + ? m_placeholder.c_str() + : m_password + ? std::string(m_value.length(), m_password_mask ).c_str() + : m_value.c_str() + , nullptr); } else { const int max_glyphs = 1024; NVGglyphPosition glyphs[max_glyphs]; @@ -240,13 +248,24 @@ void TextBox::draw(NVGcontext* ctx) { draw_pos.x() = old_draw_pos.x() + m_text_offset; // draw text with offset - nvgText(ctx, draw_pos.x(), draw_pos.y(), m_value_temp.c_str(), nullptr); - nvgTextBounds(ctx, draw_pos.x(), draw_pos.y(), m_value_temp.c_str(), - nullptr, text_bound); + nvgText(ctx, draw_pos.x(), draw_pos.y(), + m_password + ? std::string(m_value_temp.length(), m_password_mask ).c_str() + : m_value_temp.c_str() + , nullptr); + + nvgTextBounds(ctx, draw_pos.x(), draw_pos.y(), + m_password + ? std::string(m_value_temp.length(), m_password_mask ).c_str() + : m_value_temp.c_str() + ,nullptr, text_bound); // recompute cursor positions nglyphs = nvgTextGlyphPositions(ctx, draw_pos.x(), draw_pos.y(), - m_value_temp.c_str(), nullptr, glyphs, max_glyphs); + m_password + ? std::string(m_value_temp.length(), m_password_mask ).c_str() + : m_value_temp.c_str() + , nullptr, glyphs, max_glyphs); if (m_cursor_pos > -1) { if (m_selection_pos > -1) { @@ -630,4 +649,10 @@ TextBox::SpinArea TextBox::spin_area(const Vector2i & pos) { return SpinArea::None; } +void TextBox::set_password_field(bool enable, char mask) { + m_password = enable; + m_password_mask = mask; +} + + NAMESPACE_END(nanogui) From b3c08b7a71b295743a73d5b74e067e3f26cb0ded Mon Sep 17 00:00:00 2001 From: Edward Paowz Date: Mon, 14 Nov 2022 09:04:55 +0100 Subject: [PATCH 2/2] Added minimal password field support --- include/nanogui/textbox.h | 2 ++ src/textbox.cpp | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/nanogui/textbox.h b/include/nanogui/textbox.h index 44621c1c..a401dc58 100644 --- a/include/nanogui/textbox.h +++ b/include/nanogui/textbox.h @@ -108,6 +108,8 @@ class NANOGUI_EXPORT TextBox : public Widget { int position_to_cursor_index(float posx, float lastx, const NVGglyphPosition *glyphs, int size); + std::string value_or_mask_str(const std::string& str) const; + /// The location (if any) for the spin area. enum class SpinArea { None, Top, Bottom }; SpinArea spin_area(const Vector2i &pos); diff --git a/src/textbox.cpp b/src/textbox.cpp index 5b6e209b..c9adbead 100644 --- a/src/textbox.cpp +++ b/src/textbox.cpp @@ -216,9 +216,7 @@ void TextBox::draw(NVGcontext* ctx) { nvgText(ctx, draw_pos.x(), draw_pos.y(), m_value.empty() ? m_placeholder.c_str() - : m_password - ? std::string(m_value.length(), m_password_mask ).c_str() - : m_value.c_str() + : value_or_mask_str(m_value).c_str() , nullptr); } else { const int max_glyphs = 1024; @@ -249,22 +247,16 @@ void TextBox::draw(NVGcontext* ctx) { // draw text with offset nvgText(ctx, draw_pos.x(), draw_pos.y(), - m_password - ? std::string(m_value_temp.length(), m_password_mask ).c_str() - : m_value_temp.c_str() + value_or_mask_str(m_value_temp).c_str() , nullptr); nvgTextBounds(ctx, draw_pos.x(), draw_pos.y(), - m_password - ? std::string(m_value_temp.length(), m_password_mask ).c_str() - : m_value_temp.c_str() + value_or_mask_str(m_value_temp).c_str() ,nullptr, text_bound); // recompute cursor positions nglyphs = nvgTextGlyphPositions(ctx, draw_pos.x(), draw_pos.y(), - m_password - ? std::string(m_value_temp.length(), m_password_mask ).c_str() - : m_value_temp.c_str() + value_or_mask_str(m_value_temp).c_str() , nullptr, glyphs, max_glyphs); if (m_cursor_pos > -1) { @@ -654,5 +646,11 @@ void TextBox::set_password_field(bool enable, char mask) { m_password_mask = mask; } +std::string TextBox::value_or_mask_str(const std::string& str) const +{ + return m_password + ? std::string(str.length(), m_password_mask).c_str() + : str.c_str(); +} NAMESPACE_END(nanogui)