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

Add masking for textboxes #992

Open
wants to merge 2 commits into
base: dev
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
31 changes: 25 additions & 6 deletions Blish HUD/Controls/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public bool HideBackground {
set => SetProperty(ref _hideBackground, value);
}

/// <summary>
/// <inheritdoc cref="TextInputBase._masked"/>
/// </summary>
public bool Masked {
get => _masked;
set => SetProperty(ref _masked, value, true);
}

/// <summary>
/// <inheritdoc cref="TextInputBase._maskingChar"/>
/// </summary>
public char MaskingChar {
get => _maskingChar;
set => SetProperty(ref _maskingChar, value, true);
}

protected virtual void OnEnterPressed(EventArgs e) {
this.Focused = false;

Expand Down Expand Up @@ -98,13 +114,15 @@ private Rectangle CalculateTextRegion() {
}

private Rectangle CalculateHighlightRegion() {
var text = this.DisplayText;

int selectionStart = Math.Min(_selectionStart, _selectionEnd);
int selectionLength = Math.Abs(_selectionStart - _selectionEnd);

if (selectionLength <= 0 || selectionStart + selectionLength > _text.Length) return Rectangle.Empty;
if (selectionLength <= 0 || selectionStart + selectionLength > text.Length) return Rectangle.Empty;

float highlightLeftOffset = MeasureStringWidth(_text.Substring(0, selectionStart));
float highlightWidth = MeasureStringWidth(_text.Substring(selectionStart, selectionLength));
float highlightLeftOffset = MeasureStringWidth(text.Substring(0, selectionStart));
float highlightWidth = MeasureStringWidth(text.Substring(selectionStart, selectionLength));

switch (this.HorizontalAlignment)
{
Expand All @@ -124,14 +142,15 @@ private Rectangle CalculateHighlightRegion() {
}

private Rectangle CalculateCursorRegion() {
float textOffset = MeasureStringWidth(_text.Substring(0, _cursorIndex));
var text = this.DisplayText;
float textOffset = MeasureStringWidth(text.Substring(0, _cursorIndex));

switch (this.HorizontalAlignment) {
case HorizontalAlignment.Center:
textOffset += (this.Width - MeasureStringWidth(_text)) / 2f - TEXT_HORIZONTALPADDING;
textOffset += (this.Width - MeasureStringWidth(text)) / 2f - TEXT_HORIZONTALPADDING;
break;
case HorizontalAlignment.Right:
textOffset += this.Width - MeasureStringWidth(_text) - TEXT_HORIZONTALPADDING * 2;
textOffset += this.Width - MeasureStringWidth(text) - TEXT_HORIZONTALPADDING * 2;
break;
default: break;
}
Expand Down
20 changes: 19 additions & 1 deletion Blish HUD/Controls/TextInputBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public string Text {

protected int _maxLength = int.MaxValue;

public string DisplayText => this._masked
? new string (this._maskingChar, this._text.Length)
: this.Text;

/// <summary>
/// Gets or sets the maximum character length of the control.
/// </summary>
Expand Down Expand Up @@ -190,6 +194,16 @@ public int CursorIndex {
/// </summary>
public int Length => _text.Length;

/// <summary>
/// Gets or sets if the input should be shown as masked. Copying the content will be disabled.
/// </summary>
protected bool _masked;

/// <summary>
/// Gets or sets the character used for the masking.
/// </summary>
protected char _maskingChar = '*';

/// Get state of modifier keys
protected bool IsShiftDown => GameService.Input.Keyboard.ActiveModifiers.HasFlag(ModifierKeys.Shift);
protected bool IsCtrlDown => GameService.Input.Keyboard.ActiveModifiers.HasFlag(ModifierKeys.Ctrl);
Expand Down Expand Up @@ -587,6 +601,8 @@ private void OnGlobalKeyboardKeyStateChanged(object sender, KeyboardEventArgs e)
}

protected virtual void HandleCopy() {
if (this._masked) return;

if (_selectionEnd != _selectionStart) {
int selectStart = Math.Min(_selectionStart, _selectionEnd);
int selectEnd = Math.Max(_selectionStart, _selectionEnd);
Expand All @@ -603,6 +619,8 @@ protected virtual void HandleCopy() {
}

protected virtual void HandleCut() {
if (this._masked) return;

HandleCopy();
DeleteSelection();
}
Expand Down Expand Up @@ -785,7 +803,7 @@ protected void PaintText(SpriteBatch spriteBatch, Rectangle textRegion, Horizont
}

// Draw the text
spriteBatch.DrawStringOnCtrl(this, _text, _font, textRegion, _foreColor, false, false, 0, horizontalAlignment, VerticalAlignment.Top);
spriteBatch.DrawStringOnCtrl(this, this.DisplayText, _font, textRegion, _foreColor, false, false, 0, horizontalAlignment, VerticalAlignment.Top);
}

protected void PaintHighlight(SpriteBatch spriteBatch, Rectangle highlightRegion) {
Expand Down
Loading