Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UITextPanel.cs
Go to the documentation of this file.
3using Terraria.UI;
4
6
7public class UITextPanel<T> : UIPanel
8{
9 protected T _text;
10
11 protected float _textScale = 1f;
12
14
15 protected bool _isLarge;
16
17 protected Color _color = Color.White;
18
19 protected bool _drawPanel = true;
20
21 public float TextHAlign = 0.5f;
22
23 public bool HideContents;
24
25 private string _asterisks;
26
27 public bool IsLarge => _isLarge;
28
29 public bool DrawPanel
30 {
31 get
32 {
33 return _drawPanel;
34 }
35 set
36 {
37 _drawPanel = value;
38 }
39 }
40
41 public float TextScale
42 {
43 get
44 {
45 return _textScale;
46 }
47 set
48 {
49 _textScale = value;
50 }
51 }
52
54
55 public string Text
56 {
57 get
58 {
59 if (_text != null)
60 {
61 return _text.ToString();
62 }
63 return "";
64 }
65 }
66
68 {
69 get
70 {
71 return _color;
72 }
73 set
74 {
75 _color = value;
76 }
77 }
78
79 public UITextPanel(T text, float textScale = 1f, bool large = false)
80 {
81 SetText(text, textScale, large);
82 }
83
84 public override void Recalculate()
85 {
87 base.Recalculate();
88 }
89
90 public void SetText(T text)
91 {
93 }
94
95 public virtual void SetText(T text, float textScale, bool large)
96 {
97 Vector2 textSize = new Vector2((large ? FontAssets.DeathText.Value : FontAssets.MouseText.Value).MeasureString(text.ToString()).X, large ? 32f : 16f) * textScale;
98 _text = text;
99 _textScale = textScale;
100 _textSize = textSize;
101 _isLarge = large;
102 MinWidth.Set(textSize.X + PaddingLeft + PaddingRight, 0f);
103 MinHeight.Set(textSize.Y + PaddingTop + PaddingBottom, 0f);
104 }
105
106 protected override void DrawSelf(SpriteBatch spriteBatch)
107 {
108 if (_drawPanel)
109 {
110 base.DrawSelf(spriteBatch);
111 }
112 DrawText(spriteBatch);
113 }
114
115 protected void DrawText(SpriteBatch spriteBatch)
116 {
117 CalculatedStyle innerDimensions = GetInnerDimensions();
118 Vector2 pos = innerDimensions.Position();
119 if (_isLarge)
120 {
121 pos.Y -= 10f * _textScale * _textScale;
122 }
123 else
124 {
125 pos.Y -= 2f * _textScale;
126 }
127 pos.X += (innerDimensions.Width - _textSize.X) * TextHAlign;
128 string text = Text;
129 if (HideContents)
130 {
131 if (_asterisks == null || _asterisks.Length != text.Length)
132 {
133 _asterisks = new string('*', text.Length);
134 }
135 text = _asterisks;
136 }
137 if (_isLarge)
138 {
139 Utils.DrawBorderStringBig(spriteBatch, text, pos, _color, _textScale);
140 }
141 else
142 {
143 Utils.DrawBorderString(spriteBatch, text, pos, _color, _textScale);
144 }
145 }
146}
static Asset< DynamicSpriteFont > DeathText
Definition FontAssets.cs:12
static Asset< DynamicSpriteFont > MouseText
Definition FontAssets.cs:10
void DrawText(SpriteBatch spriteBatch)
override void DrawSelf(SpriteBatch spriteBatch)
UITextPanel(T text, float textScale=1f, bool large=false)
virtual void SetText(T text, float textScale, bool large)
StyleDimension MinWidth
Definition UIElement.cs:35
CalculatedStyle GetInnerDimensions()
Definition UIElement.cs:377
StyleDimension MinHeight
Definition UIElement.cs:37
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
void Set(float pixels, float precent)