added support for a password character in the gui text box

This commit is contained in:
Dylan Wilson
2017-03-16 20:50:20 +10:00
parent b5656908a5
commit 098799a63d
2 changed files with 12 additions and 5 deletions
@@ -137,13 +137,16 @@ namespace MonoGame.Extended.Gui.Controls
public void Draw(IGuiRenderer renderer, float deltaSeconds)
{
DrawBackground(renderer, deltaSeconds);
DrawText(renderer, deltaSeconds, GetTextInfo(renderer, Text));
}
protected TextInfo GetTextInfo(IGuiRenderer renderer, string text)
{
var font = Font ?? renderer.DefaultFont;
var textSize = font.GetStringRectangle(Text ?? string.Empty, Vector2.Zero).Size.ToVector2();
var textSize = font.GetStringRectangle(text ?? string.Empty, Vector2.Zero).Size.ToVector2();
var textPosition = BoundingRectangle.Center.ToVector2() - textSize * 0.5f;
var textInfo = new TextInfo(Text, font, textPosition, textSize, TextColor, ClippingRectangle);
DrawText(renderer, deltaSeconds, textInfo);
var textInfo = new TextInfo(text, font, textPosition, textSize, TextColor, ClippingRectangle);
return textInfo;
}
protected virtual void DrawBackground(IGuiRenderer renderer, float deltaSeconds)
@@ -23,6 +23,7 @@ namespace MonoGame.Extended.Gui.Controls
}
public int SelectionStart { get; set; }
public char? PasswordCharacter { get; set; }
public override void OnPointerDown(GuiPointerEventArgs args)
{
@@ -75,7 +76,10 @@ namespace MonoGame.Extended.Gui.Controls
protected override void DrawText(IGuiRenderer renderer, float deltaSeconds, TextInfo textInfo)
{
var caretRectangle = textInfo.Font.GetStringRectangle(Text.Substring(0, SelectionStart), textInfo.Position);
if (PasswordCharacter.HasValue)
textInfo = GetTextInfo(renderer, new string(PasswordCharacter.Value, textInfo.Text.Length));
var caretRectangle = textInfo.Font.GetStringRectangle(textInfo.Text.Substring(0, SelectionStart), textInfo.Position);
// TODO: Finish the caret position stuff when it's outside the clipping rectangle
if (caretRectangle.Right > ClippingRectangle.Right)