Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UIElement.cs
Go to the documentation of this file.
1using System;
8
9namespace Terraria.UI;
10
11public class UIElement : IComparable
12{
14
16
18
19 public delegate void UIElementAction(UIElement element);
20
21 protected readonly List<UIElement> Elements = new List<UIElement>();
22
24
26
28
30
32
34
36
38
39 private bool _isInitialized;
40
42
43 public bool OverflowHidden;
44
46
47 public float PaddingTop;
48
49 public float PaddingLeft;
50
51 public float PaddingRight;
52
53 public float PaddingBottom;
54
55 public float MarginTop;
56
57 public float MarginLeft;
58
59 public float MarginRight;
60
61 public float MarginBottom;
62
63 public float HAlign;
64
65 public float VAlign;
66
68
70
72
74 {
75 CullMode = CullMode.None,
76 ScissorTestEnable = true
77 };
78
79 public bool UseImmediateMode;
80
82
83 private static int _idCounter = 0;
84
85 public UIElement Parent { get; private set; }
86
87 public int UniqueId { get; private set; }
88
90
91 public bool IsMouseHovering { get; private set; }
92
93 public event MouseEvent OnLeftMouseDown;
94
95 public event MouseEvent OnLeftMouseUp;
96
97 public event MouseEvent OnLeftClick;
98
99 public event MouseEvent OnLeftDoubleClick;
100
101 public event MouseEvent OnRightMouseDown;
102
103 public event MouseEvent OnRightMouseUp;
104
105 public event MouseEvent OnRightClick;
106
107 public event MouseEvent OnRightDoubleClick;
108
109 public event MouseEvent OnMouseOver;
110
111 public event MouseEvent OnMouseOut;
112
114
115 public event ElementEvent OnUpdate;
116
117 public UIElement()
118 {
120 }
121
122 public void SetSnapPoint(string name, int id, Vector2? anchor = null, Vector2? offset = null)
123 {
124 if (!anchor.HasValue)
125 {
126 anchor = new Vector2(0.5f);
127 }
128 if (!offset.HasValue)
129 {
131 }
132 _snapPoint = new SnapPoint(name, id, anchor.Value, offset.Value);
133 }
134
135 public bool GetSnapPoint(out SnapPoint point)
136 {
137 point = _snapPoint;
138 if (_snapPoint != null)
139 {
140 _snapPoint.Calculate(this);
141 }
142 return _snapPoint != null;
143 }
144
146 {
147 action(this);
148 foreach (UIElement element in Elements)
149 {
150 element.ExecuteRecursively(action);
151 }
152 }
153
154 protected virtual void DrawSelf(SpriteBatch spriteBatch)
155 {
156 }
157
158 protected virtual void DrawChildren(SpriteBatch spriteBatch)
159 {
160 foreach (UIElement element in Elements)
161 {
162 element.Draw(spriteBatch);
163 }
164 }
165
166 public void Append(UIElement element)
167 {
168 element.Remove();
169 element.Parent = this;
170 Elements.Add(element);
171 element.Recalculate();
172 }
173
174 public void Remove()
175 {
176 if (Parent != null)
177 {
178 Parent.RemoveChild(this);
179 }
180 }
181
182 public void RemoveChild(UIElement child)
183 {
184 Elements.Remove(child);
185 child.Parent = null;
186 }
187
188 public void RemoveAllChildren()
189 {
190 foreach (UIElement element in Elements)
191 {
192 element.Parent = null;
193 }
194 Elements.Clear();
195 }
196
197 public virtual void Draw(SpriteBatch spriteBatch)
198 {
201 RasterizerState rasterizerState = spriteBatch.GraphicsDevice.RasterizerState;
202 Rectangle scissorRectangle = spriteBatch.GraphicsDevice.ScissorRectangle;
205 {
206 spriteBatch.End();
208 DrawSelf(spriteBatch);
209 spriteBatch.End();
211 }
212 else
213 {
214 DrawSelf(spriteBatch);
215 }
216 if (overflowHidden)
217 {
218 spriteBatch.End();
220 spriteBatch.GraphicsDevice.ScissorRectangle = clippingRectangle;
221 spriteBatch.GraphicsDevice.RasterizerState = OverflowHiddenRasterizerState;
223 }
224 DrawChildren(spriteBatch);
225 if (overflowHidden)
226 {
227 spriteBatch.End();
228 spriteBatch.GraphicsDevice.ScissorRectangle = scissorRectangle;
229 spriteBatch.GraphicsDevice.RasterizerState = rasterizerState;
230 spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, anisotropicClamp, DepthStencilState.None, rasterizerState, null, Main.UIScaleMatrix);
231 }
232 }
233
234 public virtual void Update(GameTime gameTime)
235 {
236 if (this.OnUpdate != null)
237 {
238 this.OnUpdate(this);
239 }
240 foreach (UIElement element in Elements)
241 {
242 element.Update(gameTime);
243 }
244 }
245
247 {
251 position = Vector2.Transform(position, Main.UIScaleMatrix);
252 Rectangle rectangle = new Rectangle((int)vector.X, (int)vector.Y, (int)(position.X - vector.X), (int)(position.Y - vector.Y));
253 int num = (int)((float)Main.screenWidth * Main.UIScale);
254 int num2 = (int)((float)Main.screenHeight * Main.UIScale);
255 rectangle.X = Utils.Clamp(rectangle.X, 0, num);
256 rectangle.Y = Utils.Clamp(rectangle.Y, 0, num2);
257 rectangle.Width = Utils.Clamp(rectangle.Width, 0, num - rectangle.X);
258 rectangle.Height = Utils.Clamp(rectangle.Height, 0, num2 - rectangle.Y);
259 Rectangle scissorRectangle = spriteBatch.GraphicsDevice.ScissorRectangle;
260 int num3 = Utils.Clamp(rectangle.Left, scissorRectangle.Left, scissorRectangle.Right);
261 int num4 = Utils.Clamp(rectangle.Top, scissorRectangle.Top, scissorRectangle.Bottom);
262 int num5 = Utils.Clamp(rectangle.Right, scissorRectangle.Left, scissorRectangle.Right);
263 int num6 = Utils.Clamp(rectangle.Bottom, scissorRectangle.Top, scissorRectangle.Bottom);
264 return new Rectangle(num3, num4, num5 - num3, num6 - num4);
265 }
266
268 {
270 if (GetSnapPoint(out var point))
271 {
272 list.Add(point);
273 }
274 foreach (UIElement element in Elements)
275 {
276 list.AddRange(element.GetSnapPoints());
277 }
278 return list;
279 }
280
281 public virtual void Recalculate()
282 {
284 if (Parent != null && Parent is UIList)
285 {
286 parentDimensions.Height = float.MaxValue;
287 }
289 calculatedStyle.X += MarginLeft;
290 calculatedStyle.Y += MarginTop;
291 calculatedStyle.Width -= MarginLeft + MarginRight;
292 calculatedStyle.Height -= MarginTop + MarginBottom;
294 calculatedStyle.X += PaddingLeft;
295 calculatedStyle.Y += PaddingTop;
296 calculatedStyle.Width -= PaddingLeft + PaddingRight;
297 calculatedStyle.Height -= PaddingTop + PaddingBottom;
300 }
301
303 {
304 CalculatedStyle result = default(CalculatedStyle);
305 result.X = Left.GetValue(parentDimensions.Width) + parentDimensions.X;
306 result.Y = Top.GetValue(parentDimensions.Height) + parentDimensions.Y;
311 result.Width = MathHelper.Clamp(Width.GetValue(parentDimensions.Width), value, value2);
312 result.Height = MathHelper.Clamp(Height.GetValue(parentDimensions.Height), value3, value4);
313 result.Width += MarginLeft + MarginRight;
314 result.Height += MarginTop + MarginBottom;
315 result.X += parentDimensions.Width * HAlign - result.Width * HAlign;
316 result.Y += parentDimensions.Height * VAlign - result.Height * VAlign;
317 return result;
318 }
319
321 {
322 UIElement uIElement = null;
323 for (int num = Elements.Count - 1; num >= 0; num--)
324 {
325 UIElement uIElement2 = Elements[num];
326 if (!uIElement2.IgnoresMouseInteraction && uIElement2.ContainsPoint(point))
327 {
329 break;
330 }
331 }
332 if (uIElement != null)
333 {
334 return uIElement.GetElementAt(point);
335 }
337 {
338 return null;
339 }
340 if (ContainsPoint(point))
341 {
342 return this;
343 }
344 return null;
345 }
346
347 public virtual bool ContainsPoint(Vector2 point)
348 {
349 if (point.X > _dimensions.X && point.Y > _dimensions.Y && point.X < _dimensions.X + _dimensions.Width)
350 {
351 return point.Y < _dimensions.Y + _dimensions.Height;
352 }
353 return false;
354 }
355
357 {
358 return _dimensions.ToRectangle();
359 }
360
361 public void SetPadding(float pixels)
362 {
367 }
368
369 public virtual void RecalculateChildren()
370 {
371 foreach (UIElement element in Elements)
372 {
373 element.Recalculate();
374 }
375 }
376
378 {
379 return _innerDimensions;
380 }
381
383 {
384 return _dimensions;
385 }
386
388 {
389 return _outerDimensions;
390 }
391
392 public void CopyStyle(UIElement element)
393 {
394 Top = element.Top;
395 Left = element.Left;
396 Width = element.Width;
397 Height = element.Height;
399 PaddingLeft = element.PaddingLeft;
400 PaddingRight = element.PaddingRight;
401 PaddingTop = element.PaddingTop;
402 HAlign = element.HAlign;
403 VAlign = element.VAlign;
404 MinWidth = element.MinWidth;
405 MaxWidth = element.MaxWidth;
406 MinHeight = element.MinHeight;
407 MaxHeight = element.MaxHeight;
408 Recalculate();
409 }
410
411 public virtual void LeftMouseDown(UIMouseEvent evt)
412 {
413 if (this.OnLeftMouseDown != null)
414 {
415 this.OnLeftMouseDown(evt, this);
416 }
417 if (Parent != null)
418 {
420 }
421 }
422
423 public virtual void LeftMouseUp(UIMouseEvent evt)
424 {
425 if (this.OnLeftMouseUp != null)
426 {
427 this.OnLeftMouseUp(evt, this);
428 }
429 if (Parent != null)
430 {
432 }
433 }
434
435 public virtual void LeftClick(UIMouseEvent evt)
436 {
437 if (this.OnLeftClick != null)
438 {
439 this.OnLeftClick(evt, this);
440 }
441 if (Parent != null)
442 {
444 }
445 }
446
447 public virtual void LeftDoubleClick(UIMouseEvent evt)
448 {
449 if (this.OnLeftDoubleClick != null)
450 {
451 this.OnLeftDoubleClick(evt, this);
452 }
453 if (Parent != null)
454 {
456 }
457 }
458
459 public virtual void RightMouseDown(UIMouseEvent evt)
460 {
461 if (this.OnRightMouseDown != null)
462 {
463 this.OnRightMouseDown(evt, this);
464 }
465 if (Parent != null)
466 {
468 }
469 }
470
471 public virtual void RightMouseUp(UIMouseEvent evt)
472 {
473 if (this.OnRightMouseUp != null)
474 {
475 this.OnRightMouseUp(evt, this);
476 }
477 if (Parent != null)
478 {
480 }
481 }
482
483 public virtual void RightClick(UIMouseEvent evt)
484 {
485 if (this.OnRightClick != null)
486 {
487 this.OnRightClick(evt, this);
488 }
489 if (Parent != null)
490 {
492 }
493 }
494
495 public virtual void RightDoubleClick(UIMouseEvent evt)
496 {
497 if (this.OnRightDoubleClick != null)
498 {
499 this.OnRightDoubleClick(evt, this);
500 }
501 if (Parent != null)
502 {
504 }
505 }
506
507 public virtual void MouseOver(UIMouseEvent evt)
508 {
509 IsMouseHovering = true;
510 if (this.OnMouseOver != null)
511 {
512 this.OnMouseOver(evt, this);
513 }
514 if (Parent != null)
515 {
517 }
518 }
519
520 public virtual void MouseOut(UIMouseEvent evt)
521 {
522 IsMouseHovering = false;
523 if (this.OnMouseOut != null)
524 {
525 this.OnMouseOut(evt, this);
526 }
527 if (Parent != null)
528 {
530 }
531 }
532
533 public virtual void ScrollWheel(UIScrollWheelEvent evt)
534 {
535 if (this.OnScrollWheel != null)
536 {
537 this.OnScrollWheel(evt, this);
538 }
539 if (Parent != null)
540 {
542 }
543 }
544
545 public void Activate()
546 {
547 if (!_isInitialized)
548 {
549 Initialize();
550 }
551 OnActivate();
552 foreach (UIElement element in Elements)
553 {
554 element.Activate();
555 }
556 }
557
558 public virtual void OnActivate()
559 {
560 }
561
562 [Conditional("DEBUG")]
564 {
565 if (IsMouseHovering)
566 {
567 colorIntensity += 0.1f;
568 }
571 drawer.DrawLine(innerDimensions.Position(), innerDimensions.Position() + new Vector2(innerDimensions.Width, 0f), 2f, color);
572 drawer.DrawLine(innerDimensions.Position() + new Vector2(innerDimensions.Width, 0f), innerDimensions.Position() + new Vector2(innerDimensions.Width, innerDimensions.Height), 2f, color);
573 drawer.DrawLine(innerDimensions.Position() + new Vector2(innerDimensions.Width, innerDimensions.Height), innerDimensions.Position() + new Vector2(0f, innerDimensions.Height), 2f, color);
574 drawer.DrawLine(innerDimensions.Position() + new Vector2(0f, innerDimensions.Height), innerDimensions.Position(), 2f, color);
575 foreach (UIElement element in Elements)
576 {
577 _ = element;
578 }
579 }
580
581 public void Deactivate()
582 {
583 OnDeactivate();
584 foreach (UIElement element in Elements)
585 {
586 element.Deactivate();
587 }
588 }
589
590 public virtual void OnDeactivate()
591 {
592 }
593
594 public void Initialize()
595 {
596 OnInitialize();
597 _isInitialized = true;
598 }
599
600 public virtual void OnInitialize()
601 {
602 }
603
604 public virtual int CompareTo(object obj)
605 {
606 return 0;
607 }
608}
static readonly BlendState AlphaBlend
Definition BlendState.cs:36
static readonly SamplerState AnisotropicClamp
static float Clamp(float value, float min, float max)
Definition MathHelper.cs:46
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
void Add(TKey key, TValue value)
static float UIScale
Definition Main.cs:2624
static Matrix UIScaleMatrix
Definition Main.cs:2619
static Microsoft.Xna.Framework.Color hslToRgb(Vector3 hslVector)
Definition Main.cs:44913
void Calculate(UIElement element)
Definition SnapPoint.cs:27
virtual void Update(GameTime gameTime)
Definition UIElement.cs:234
CalculatedStyle GetOuterDimensions()
Definition UIElement.cs:387
StyleDimension Height
Definition UIElement.cs:29
virtual void MouseOver(UIMouseEvent evt)
Definition UIElement.cs:507
static readonly RasterizerState OverflowHiddenRasterizerState
Definition UIElement.cs:73
StyleDimension MaxWidth
Definition UIElement.cs:31
UIElement GetElementAt(Vector2 point)
Definition UIElement.cs:320
virtual int CompareTo(object obj)
Definition UIElement.cs:604
virtual void ExecuteRecursively(UIElementAction action)
Definition UIElement.cs:145
virtual void RightMouseDown(UIMouseEvent evt)
Definition UIElement.cs:459
StyleDimension MinWidth
Definition UIElement.cs:35
MouseEvent OnRightMouseUp
Definition UIElement.cs:103
void DrawDebugHitbox(BasicDebugDrawer drawer, float colorIntensity=0f)
Definition UIElement.cs:563
ElementEvent OnUpdate
Definition UIElement.cs:115
virtual void Draw(SpriteBatch spriteBatch)
Definition UIElement.cs:197
MouseEvent OnRightDoubleClick
Definition UIElement.cs:107
ScrollWheelEvent OnScrollWheel
Definition UIElement.cs:113
virtual void OnDeactivate()
Definition UIElement.cs:590
void Append(UIElement element)
Definition UIElement.cs:166
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
CalculatedStyle GetInnerDimensions()
Definition UIElement.cs:377
virtual void LeftClick(UIMouseEvent evt)
Definition UIElement.cs:435
StyleDimension Left
Definition UIElement.cs:25
CalculatedStyle _innerDimensions
Definition UIElement.cs:67
MouseEvent OnRightMouseDown
Definition UIElement.cs:101
virtual void MouseOut(UIMouseEvent evt)
Definition UIElement.cs:520
virtual void DrawSelf(SpriteBatch spriteBatch)
Definition UIElement.cs:154
virtual void ScrollWheel(UIScrollWheelEvent evt)
Definition UIElement.cs:533
delegate void UIElementAction(UIElement element)
delegate void ScrollWheelEvent(UIScrollWheelEvent evt, UIElement listeningElement)
virtual void LeftMouseDown(UIMouseEvent evt)
Definition UIElement.cs:411
StyleDimension MaxHeight
Definition UIElement.cs:33
MouseEvent OnLeftMouseDown
Definition UIElement.cs:93
MouseEvent OnLeftDoubleClick
Definition UIElement.cs:99
IEnumerable< UIElement > Children
Definition UIElement.cs:89
void SetSnapPoint(string name, int id, Vector2? anchor=null, Vector2? offset=null)
Definition UIElement.cs:122
virtual void RecalculateChildren()
Definition UIElement.cs:369
CalculatedStyle GetDimensionsBasedOnParentDimensions(CalculatedStyle parentDimensions)
Definition UIElement.cs:302
StyleDimension Width
Definition UIElement.cs:27
virtual bool ContainsPoint(Vector2 point)
Definition UIElement.cs:347
bool GetSnapPoint(out SnapPoint point)
Definition UIElement.cs:135
delegate void ElementEvent(UIElement affectedElement)
virtual void Recalculate()
Definition UIElement.cs:281
StyleDimension MinHeight
Definition UIElement.cs:37
MouseEvent OnRightClick
Definition UIElement.cs:105
virtual void LeftMouseUp(UIMouseEvent evt)
Definition UIElement.cs:423
delegate void MouseEvent(UIMouseEvent evt, UIElement listeningElement)
MouseEvent OnLeftClick
Definition UIElement.cs:97
virtual Rectangle GetViewCullingArea()
Definition UIElement.cs:356
virtual void DrawChildren(SpriteBatch spriteBatch)
Definition UIElement.cs:158
static int _idCounter
Definition UIElement.cs:83
CalculatedStyle _outerDimensions
Definition UIElement.cs:71
MouseEvent OnLeftMouseUp
Definition UIElement.cs:95
CalculatedStyle GetDimensions()
Definition UIElement.cs:382
void RemoveChild(UIElement child)
Definition UIElement.cs:182
readonly List< UIElement > Elements
Definition UIElement.cs:21
virtual void OnActivate()
Definition UIElement.cs:558
void CopyStyle(UIElement element)
Definition UIElement.cs:392
virtual void OnInitialize()
Definition UIElement.cs:600
MouseEvent OnMouseOver
Definition UIElement.cs:109
SamplerState OverrideSamplerState
Definition UIElement.cs:45
virtual List< SnapPoint > GetSnapPoints()
Definition UIElement.cs:267
void SetPadding(float pixels)
Definition UIElement.cs:361
MouseEvent OnMouseOut
Definition UIElement.cs:111
virtual void LeftDoubleClick(UIMouseEvent evt)
Definition UIElement.cs:447
StyleDimension Top
Definition UIElement.cs:23
CalculatedStyle _dimensions
Definition UIElement.cs:69
Rectangle GetClippingRectangle(SpriteBatch spriteBatch)
Definition UIElement.cs:246
static UserInterface ActiveInstance
static Vector2 Transform(Vector2 position, Matrix matrix)
Definition Vector2.cs:317
static StyleDimension Fill
static StyleDimension Empty
float GetValue(float containerSize)