Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UIText.cs
Go to the documentation of this file.
1using System;
6using Terraria.UI;
8
10
11public class UIText : UIElement
12{
13 private object _text = "";
14
15 private float _textScale = 1f;
16
18
19 private bool _isLarge;
20
22
24
25 private bool _isWrapped;
26
28
29 private string _visibleText;
30
31 private string _lastTextReference;
32
33 public string Text => _text.ToString();
34
35 public float TextOriginX { get; set; }
36
37 public float TextOriginY { get; set; }
38
39 public float WrappedTextBottomPadding { get; set; }
40
41 public bool IsWrapped
42 {
43 get
44 {
45 return _isWrapped;
46 }
47 set
48 {
51 }
52 }
53
55 {
56 get
57 {
58 return _color;
59 }
60 set
61 {
62 _color = value;
63 }
64 }
65
67 {
68 get
69 {
70 return _shadowColor;
71 }
72 set
73 {
75 }
76 }
77
78 public event Action OnInternalTextChange;
79
80 public UIText(string text, float textScale = 1f, bool large = false)
81 {
82 TextOriginX = 0.5f;
83 TextOriginY = 0f;
84 IsWrapped = false;
86 InternalSetText(text, textScale, large);
87 }
88
89 public UIText(LocalizedText text, float textScale = 1f, bool large = false)
90 {
91 TextOriginX = 0.5f;
92 TextOriginY = 0f;
93 IsWrapped = false;
95 InternalSetText(text, textScale, large);
96 }
97
98 public override void Recalculate()
99 {
101 base.Recalculate();
102 }
103
104 public void SetText(string text)
105 {
107 }
108
113
114 public void SetText(string text, float textScale, bool large)
115 {
116 InternalSetText(text, textScale, large);
117 }
118
119 public void SetText(LocalizedText text, float textScale, bool large)
120 {
121 InternalSetText(text, textScale, large);
122 }
123
124 protected override void DrawSelf(SpriteBatch spriteBatch)
125 {
126 base.DrawSelf(spriteBatch);
128 CalculatedStyle innerDimensions = GetInnerDimensions();
129 Vector2 position = innerDimensions.Position();
130 if (_isLarge)
131 {
132 position.Y -= 10f * _textScale;
133 }
134 else
135 {
136 position.Y -= 2f * _textScale;
137 }
138 position.X += (innerDimensions.Width - _textSize.X) * TextOriginX;
139 position.Y += (innerDimensions.Height - _textSize.Y) * TextOriginY;
140 float num = _textScale;
141 if (DynamicallyScaleDownToWidth && _textSize.X > innerDimensions.Width)
142 {
143 num *= innerDimensions.Width / _textSize.X;
144 }
145 DynamicSpriteFont value = (_isLarge ? FontAssets.DeathText : FontAssets.MouseText).Value;
146 Vector2 vector = value.MeasureString(_visibleText);
147 Color baseColor = _shadowColor * ((float)(int)_color.A / 255f);
148 Vector2 origin = new Vector2(0f, 0f) * vector;
149 Vector2 baseScale = new Vector2(num);
150 TextSnippet[] snippets = ChatManager.ParseMessage(_visibleText, _color).ToArray();
152 ChatManager.DrawColorCodedStringShadow(spriteBatch, value, snippets, position, baseColor, 0f, origin, baseScale, -1f, 1.5f);
153 ChatManager.DrawColorCodedString(spriteBatch, value, snippets, position, Color.White, 0f, origin, baseScale, out var _, -1f);
154 }
155
156 private void VerifyTextState()
157 {
158 if ((object)_lastTextReference != Text)
159 {
161 }
162 }
163
164 private void InternalSetText(object text, float textScale, bool large)
165 {
166 DynamicSpriteFont val = (large ? FontAssets.DeathText.Value : FontAssets.MouseText.Value);
167 _text = text;
168 _isLarge = large;
169 _textScale = textScale;
170 _lastTextReference = _text.ToString();
171 if (IsWrapped)
172 {
174 }
175 else
176 {
178 }
179 Vector2 vector = val.MeasureString(_visibleText);
180 Vector2 vector2 = (_textSize = ((!IsWrapped) ? (new Vector2(vector.X, large ? 32f : 16f) * textScale) : (new Vector2(vector.X, vector.Y + WrappedTextBottomPadding) * textScale)));
181 MinWidth.Set(vector2.X + PaddingLeft + PaddingRight, 0f);
182 MinHeight.Set(vector2.Y + PaddingTop + PaddingBottom, 0f);
183 if (this.OnInternalTextChange != null)
184 {
186 }
187 }
188}
string CreateWrappedText(string text, float maxWidth)
static Asset< DynamicSpriteFont > MouseText
Definition FontAssets.cs:10
void SetText(string text, float textScale, bool large)
Definition UIText.cs:114
UIText(string text, float textScale=1f, bool large=false)
Definition UIText.cs:80
void InternalSetText(object text, float textScale, bool large)
Definition UIText.cs:164
override void DrawSelf(SpriteBatch spriteBatch)
Definition UIText.cs:124
UIText(LocalizedText text, float textScale=1f, bool large=false)
Definition UIText.cs:89
void SetText(LocalizedText text)
Definition UIText.cs:109
void SetText(LocalizedText text, float textScale, bool large)
Definition UIText.cs:119
static void DrawColorCodedStringShadow(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, float maxWidth=-1f, float spread=2f)
static Vector2 DrawColorCodedString(SpriteBatch spriteBatch, DynamicSpriteFont font, TextSnippet[] snippets, Vector2 position, Color baseColor, float rotation, Vector2 origin, Vector2 baseScale, out int hoveredSnippet, float maxWidth, bool ignoreColors=false)
static void ConvertNormalSnippets(TextSnippet[] snippets)
static List< TextSnippet > ParseMessage(string text, Color baseColor)
StyleDimension MinWidth
Definition UIElement.cs:35
CalculatedStyle GetInnerDimensions()
Definition UIElement.cs:377
StyleDimension MinHeight
Definition UIElement.cs:37
void Set(float pixels, float precent)