Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UserInterface.cs
Go to the documentation of this file.
1using System;
7
8namespace Terraria.UI;
9
10public class UserInterface
11{
13
14 private class InputPointerCache
15 {
16 public double LastTimeDown;
17
18 public bool WasDown;
19
21
23
24 public MouseElementEvent MouseDownEvent;
25
26 public MouseElementEvent MouseUpEvent;
27
28 public MouseElementEvent ClickEvent;
29
30 public MouseElementEvent DoubleClickEvent;
31
32 public void Clear()
33 {
34 LastClicked = null;
35 LastDown = null;
36 LastTimeDown = 0.0;
37 }
38 }
39
40 private const double DOUBLE_CLICK_TIME = 500.0;
41
42 private const double STATE_CHANGE_CLICK_DISABLE_TIME = 200.0;
43
44 private const int MAX_HISTORY_SIZE = 32;
45
46 private const int HISTORY_PRUNE_SIZE = 4;
47
49
51
53 {
54 MouseDownEvent = delegate(UIElement element, UIMouseEvent evt)
55 {
56 element.LeftMouseDown(evt);
57 },
58 MouseUpEvent = delegate(UIElement element, UIMouseEvent evt)
59 {
60 element.LeftMouseUp(evt);
61 },
62 ClickEvent = delegate(UIElement element, UIMouseEvent evt)
63 {
64 element.LeftClick(evt);
65 },
66 DoubleClickEvent = delegate(UIElement element, UIMouseEvent evt)
67 {
68 element.LeftDoubleClick(evt);
69 }
70 };
71
73 {
74 MouseDownEvent = delegate(UIElement element, UIMouseEvent evt)
75 {
76 element.RightMouseDown(evt);
77 },
78 MouseUpEvent = delegate(UIElement element, UIMouseEvent evt)
79 {
80 element.RightMouseUp(evt);
81 },
82 ClickEvent = delegate(UIElement element, UIMouseEvent evt)
83 {
84 element.RightClick(evt);
85 },
86 DoubleClickEvent = delegate(UIElement element, UIMouseEvent evt)
87 {
88 element.RightDoubleClick(evt);
89 }
90 };
91
93
95
97
98 private bool _isStateDirty;
99
100 public bool IsVisible;
101
103
105
106 public void ClearPointers()
107 {
110 }
111
112 public void ResetLasts()
113 {
114 if (_lastElementHover != null)
115 {
117 }
119 _lastElementHover = null;
120 }
121
122 public void EscapeElements()
123 {
124 ResetLasts();
125 }
126
128 {
129 ActiveInstance = this;
130 }
131
132 public void Use()
133 {
134 if (ActiveInstance != this)
135 {
136 ActiveInstance = this;
137 Recalculate();
138 }
139 else
140 {
141 ActiveInstance = this;
142 }
143 }
144
146 {
147 LeftMouse.WasDown = Main.mouseLeft;
148 RightMouse.WasDown = Main.mouseRight;
149 }
150
166
167 private void GetMousePosition()
168 {
170 }
171
172 public void Update(GameTime time)
173 {
174 if (_currentState == null)
175 {
176 return;
177 }
179 UIElement uIElement = (Main.hasFocus ? _currentState.GetElementAt(MousePosition) : null);
180 _clickDisabledTimeRemaining = Math.Max(0.0, _clickDisabledTimeRemaining - time.ElapsedGameTime.TotalMilliseconds);
181 bool num = _clickDisabledTimeRemaining > 0.0;
183 {
184 if (_lastElementHover != null)
185 {
187 }
190 }
191 if (!num)
192 {
195 }
197 {
199 PlayerInput.ScrollWheelDeltaForUI = 0;
200 }
201 if (_currentState != null)
202 {
203 _currentState.Update(time);
204 }
205 }
206
208 {
209 if (isDown && !cache.WasDown && mouseElement != null)
210 {
211 cache.LastDown = mouseElement;
213 if (cache.LastClicked == mouseElement && time.TotalGameTime.TotalMilliseconds - cache.LastTimeDown < 500.0)
214 {
216 cache.LastClicked = null;
217 }
218 cache.LastTimeDown = time.TotalGameTime.TotalMilliseconds;
219 }
220 else if (!isDown && cache.WasDown && cache.LastDown != null)
221 {
222 UIElement lastDown = cache.LastDown;
223 if (lastDown.ContainsPoint(MousePosition))
224 {
226 cache.LastClicked = cache.LastDown;
227 }
228 cache.MouseUpEvent(lastDown, new UIMouseEvent(lastDown, MousePosition));
229 cache.LastDown = null;
230 }
231 cache.WasDown = isDown;
232 }
233
234 public void Draw(SpriteBatch spriteBatch, GameTime time)
235 {
236 Use();
237 if (_currentState != null)
238 {
239 if (_isStateDirty)
240 {
242 _isStateDirty = false;
243 }
244 _currentState.Draw(spriteBatch);
245 }
246 }
247
249 {
251 }
252
253 public void SetState(UIState state)
254 {
255 if (state == _currentState)
256 {
257 return;
258 }
259 if (state != null)
260 {
262 }
263 if (_currentState != null)
264 {
265 if (_lastElementHover != null)
266 {
268 }
270 }
272 ResetState();
273 if (state != null)
274 {
275 _isStateDirty = true;
276 state.Activate();
277 state.Recalculate();
278 }
279 }
280
281 public void GoBack()
282 {
283 if (_history.Count >= 2)
284 {
285 UIState state = _history[_history.Count - 2];
286 _history.RemoveRange(_history.Count - 2, 2);
288 }
289 }
290
292 {
294 if (_history.Count > 32)
295 {
296 _history.RemoveRange(0, 4);
297 }
298 }
299
300 public void Recalculate()
301 {
302 if (_currentState != null)
303 {
305 }
306 }
307
313
314 internal void RefreshState()
315 {
316 if (_currentState != null)
317 {
319 }
320 ResetState();
323 }
324
326 {
327 if (IsVisible && _lastElementHover != null)
328 {
329 return !(_lastElementHover is UIState);
330 }
331 return false;
332 }
333}
void Add(TKey key, TValue value)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static bool hasFocus
Definition Main.cs:1781
static bool mouseRight
Definition Main.cs:616
static bool dedServ
Definition Main.cs:1226
static float UIScale
Definition Main.cs:2624
static int mouseY
Definition Main.cs:606
static int mouseX
Definition Main.cs:604
static bool mouseLeft
Definition Main.cs:614
virtual void Update(GameTime gameTime)
Definition UIElement.cs:234
UIElement GetElementAt(Vector2 point)
Definition UIElement.cs:320
virtual void RightMouseDown(UIMouseEvent evt)
Definition UIElement.cs:459
virtual void Draw(SpriteBatch spriteBatch)
Definition UIElement.cs:197
virtual void RightDoubleClick(UIMouseEvent evt)
Definition UIElement.cs:495
virtual void RightMouseUp(UIMouseEvent evt)
Definition UIElement.cs:471
virtual void RightClick(UIMouseEvent evt)
Definition UIElement.cs:483
virtual void LeftClick(UIMouseEvent evt)
Definition UIElement.cs:435
virtual void MouseOut(UIMouseEvent evt)
Definition UIElement.cs:520
virtual void LeftMouseDown(UIMouseEvent evt)
Definition UIElement.cs:411
virtual void Recalculate()
Definition UIElement.cs:281
virtual void LeftMouseUp(UIMouseEvent evt)
Definition UIElement.cs:423
virtual void LeftDoubleClick(UIMouseEvent evt)
Definition UIElement.cs:447
void Update(GameTime time)
void SetState(UIState state)
void DrawDebugHitbox(BasicDebugDrawer drawer)
const double STATE_CHANGE_CLICK_DISABLE_TIME
void AddToHistory(UIState state)
void Draw(SpriteBatch spriteBatch, GameTime time)
InputPointerCache RightMouse
static UserInterface ActiveInstance
void HandleClick(InputPointerCache cache, GameTime time, bool isDown, UIElement mouseElement)
delegate void MouseElementEvent(UIElement element, UIMouseEvent evt)
CalculatedStyle GetDimensions()
InputPointerCache LeftMouse