From 395482f1142c11dd78211a7215f0504c66746964 Mon Sep 17 00:00:00 2001 From: Tharylia <41393569+Tharylia@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:01:19 +0200 Subject: [PATCH 1/2] Add masking for textbox --- Blish HUD/Controls/TextBox.cs | 25 +++++++++++++++++++------ Blish HUD/Controls/TextInputBase.cs | 20 +++++++++++++++++++- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Blish HUD/Controls/TextBox.cs b/Blish HUD/Controls/TextBox.cs index fbc5bed99..fa4263cb7 100644 --- a/Blish HUD/Controls/TextBox.cs +++ b/Blish HUD/Controls/TextBox.cs @@ -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; @@ -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) { @@ -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; } diff --git a/Blish HUD/Controls/TextInputBase.cs b/Blish HUD/Controls/TextInputBase.cs index 432727a4e..9ffede872 100644 --- a/Blish HUD/Controls/TextInputBase.cs +++ b/Blish HUD/Controls/TextInputBase.cs @@ -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; + /// /// Gets or sets the maximum character length of the control. /// @@ -190,6 +194,16 @@ public int CursorIndex { /// public int Length => _text.Length; + /// + /// Gets or sets if the input should be shown as masked. Copying the content will be disabled. + /// + protected bool _masked; + + /// + /// Gets or sets the character used for the masking. + /// + 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); @@ -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); @@ -603,6 +619,8 @@ protected virtual void HandleCopy() { } protected virtual void HandleCut() { + if (this._masked) return; + HandleCopy(); DeleteSelection(); } @@ -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) { From 0c21a20f69dfd6a279df2101615a8b72e9bdddea Mon Sep 17 00:00:00 2001 From: Tharylia <41393569+Tharylia@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:09:42 +0200 Subject: [PATCH 2/2] Inherit documentation from base class --- Blish HUD/Controls/TextBox.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Blish HUD/Controls/TextBox.cs b/Blish HUD/Controls/TextBox.cs index fa4263cb7..6aa34d0ea 100644 --- a/Blish HUD/Controls/TextBox.cs +++ b/Blish HUD/Controls/TextBox.cs @@ -38,11 +38,17 @@ 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);