Skip to content

Commit

Permalink
Add masking for textbox
Browse files Browse the repository at this point in the history
  • Loading branch information
Tharylia committed Oct 2, 2024
1 parent d29cad7 commit 395482f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
25 changes: 19 additions & 6 deletions Blish HUD/Controls/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public bool HideBackground {
set => SetProperty(ref _hideBackground, value);
}

public bool Masked {
get => _masked;
set => SetProperty(ref _masked, value, true);
}

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 +108,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 +136,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

0 comments on commit 395482f

Please sign in to comment.