Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widget/password field #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/nanogui/textbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -105,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);
Expand All @@ -113,6 +118,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;
Expand Down
33 changes: 28 additions & 5 deletions src/textbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -211,7 +213,11 @@ 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()
: value_or_mask_str(m_value).c_str()
, nullptr);
} else {
const int max_glyphs = 1024;
NVGglyphPosition glyphs[max_glyphs];
Expand Down Expand Up @@ -240,13 +246,18 @@ 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(),
value_or_mask_str(m_value_temp).c_str()
, nullptr);

nvgTextBounds(ctx, draw_pos.x(), draw_pos.y(),
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_value_temp.c_str(), nullptr, glyphs, max_glyphs);
value_or_mask_str(m_value_temp).c_str()
, nullptr, glyphs, max_glyphs);

if (m_cursor_pos > -1) {
if (m_selection_pos > -1) {
Expand Down Expand Up @@ -630,4 +641,16 @@ 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;
}

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)