Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UITextBox.cs
Go to the documentation of this file.
1using System;
4using Terraria.UI;
5
7
8internal class UITextBox : UITextPanel<string>
9{
10 private int _cursor;
11
12 private int _frameCount;
13
14 private int _maxLength = 20;
15
16 public bool ShowInputTicker = true;
17
18 public bool HideSelf;
19
20 public UITextBox(string text, float textScale = 1f, bool large = false)
21 : base(text, textScale, large)
22 {
23 }
24
25 public void Write(string text)
26 {
27 SetText(base.Text.Insert(_cursor, text));
28 _cursor += text.Length;
29 }
30
31 public override void SetText(string text, float textScale, bool large)
32 {
33 if (text == null)
34 {
35 text = "";
36 }
37 if (text.Length > _maxLength)
38 {
39 text = text.Substring(0, _maxLength);
40 }
41 base.SetText(text, textScale, large);
42 _cursor = Math.Min(base.Text.Length, _cursor);
43 }
44
45 public void SetTextMaxLength(int maxLength)
46 {
47 _maxLength = maxLength;
48 }
49
50 public void Backspace()
51 {
52 if (_cursor != 0)
53 {
54 SetText(base.Text.Substring(0, base.Text.Length - 1));
55 }
56 }
57
58 public void CursorLeft()
59 {
60 if (_cursor != 0)
61 {
62 _cursor--;
63 }
64 }
65
66 public void CursorRight()
67 {
68 if (_cursor < base.Text.Length)
69 {
70 _cursor++;
71 }
72 }
73
74 protected override void DrawSelf(SpriteBatch spriteBatch)
75 {
76 if (HideSelf)
77 {
78 return;
79 }
80 _cursor = base.Text.Length;
81 base.DrawSelf(spriteBatch);
83 if ((_frameCount %= 40) <= 20 && ShowInputTicker)
84 {
85 CalculatedStyle innerDimensions = GetInnerDimensions();
86 Vector2 pos = innerDimensions.Position();
87 Vector2 vector = new Vector2((base.IsLarge ? FontAssets.DeathText.Value : FontAssets.MouseText.Value).MeasureString(base.Text.Substring(0, _cursor)).X, base.IsLarge ? 32f : 16f) * base.TextScale;
88 if (base.IsLarge)
89 {
90 pos.Y -= 8f * base.TextScale;
91 }
92 else
93 {
94 pos.Y -= 2f * base.TextScale;
95 }
96 pos.X += (innerDimensions.Width - base.TextSize.X) * TextHAlign + vector.X - (base.IsLarge ? 8f : 4f) * base.TextScale + 6f;
97 if (base.IsLarge)
98 {
99 Utils.DrawBorderStringBig(spriteBatch, "|", pos, base.TextColor, base.TextScale);
100 }
101 else
102 {
103 Utils.DrawBorderString(spriteBatch, "|", pos, base.TextColor, base.TextScale);
104 }
105 }
106 }
107}
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static Asset< DynamicSpriteFont > DeathText
Definition FontAssets.cs:12
static Asset< DynamicSpriteFont > MouseText
Definition FontAssets.cs:10
override void DrawSelf(SpriteBatch spriteBatch)
Definition UITextBox.cs:74
UITextBox(string text, float textScale=1f, bool large=false)
Definition UITextBox.cs:20
override void SetText(string text, float textScale, bool large)
Definition UITextBox.cs:31
static Vector2 DrawBorderString(SpriteBatch sb, string text, Vector2 pos, Color color, float scale=1f, float anchorx=0f, float anchory=0f, int maxCharactersDisplayed=-1)
Definition Utils.cs:1891
static Vector2 DrawBorderStringBig(SpriteBatch spriteBatch, string text, Vector2 pos, Color color, float scale=1f, float anchorx=0f, float anchory=0f, int maxCharactersDisplayed=-1)
Definition Utils.cs:1903