Client LuaCsForBarotrauma
Widget.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
7 
8 namespace Barotrauma
9 {
10  public enum WidgetShape
11  {
12  Rectangle,
13  Circle,
14  Cross
15  }
16 
17  internal class Widget
18  {
19  public WidgetShape Shape;
20  public LocalizedString Tooltip;
21  public bool ShowTooltip = true;
22  public Rectangle DrawRect => new Rectangle((int)(DrawPos.X - (float)Size / 2), (int)(DrawPos.Y - (float)Size / 2), Size, Size);
23  public Rectangle InputRect
24  {
25  get
26  {
27  var inputRect = DrawRect;
28  inputRect.Inflate(InputAreaMargin, InputAreaMargin);
29  return inputRect;
30  }
31  }
32 
33  public Vector2 DrawPos { get; set; }
34  public int Size = 10;
35  public float Thickness = 1f;
39  public int Sides = 40;
43  public bool IsFilled;
44  public int InputAreaMargin;
45  public Color Color = GUIStyle.Red;
46  public Color? SecondaryColor;
47  public Color TextColor = Color.White;
48  public Color TextBackgroundColor = Color.Black * 0.5f;
49  public readonly string Id;
50 
51  public event Action Selected;
52  public event Action Deselected;
53  public event Action Hovered;
54  public event Action MouseUp;
55  public event Action MouseDown;
56  public event Action<float> MouseHeld;
57  public event Action<float> PreUpdate;
58  public event Action<float> PostUpdate;
59  public event Action<SpriteBatch, float> PreDraw;
60  public event Action<SpriteBatch, float> PostDraw;
61 
62  public bool RequireMouseOn = true;
63 
64  public Action Refresh;
65 
66  public object Data;
67 
68  public bool IsSelected => enabled && SelectedWidgets.Contains(this);
69  public bool IsControlled => IsSelected && PlayerInput.PrimaryMouseButtonHeld();
70  public bool IsMouseOver => GUI.MouseOn == null && InputRect.Contains(PlayerInput.MousePosition);
71  private bool enabled = true;
72  public bool Enabled
73  {
74  get { return enabled; }
75  set
76  {
77  enabled = value;
78  if (!enabled && SelectedWidgets.Contains(this))
79  {
80  SelectedWidgets.Remove(this);
81  }
82  }
83  }
84 
85  private static bool multiselect;
86  public static bool EnableMultiSelect
87  {
88  get { return multiselect; }
89  set
90  {
91  multiselect = value;
92  if (!multiselect && SelectedWidgets.Multiple())
93  {
94  SelectedWidgets = SelectedWidgets.Take(1).ToList();
95  }
96  }
97  }
98  public Vector2? TooltipOffset;
99 
100  public Widget LinkedWidget;
101 
102  public static List<Widget> SelectedWidgets = new List<Widget>();
103 
104  public Widget(string id, int size, WidgetShape shape)
105  {
106  Id = id;
107  Size = size;
108  Shape = shape;
109  }
110 
111  public virtual void Update(float deltaTime)
112  {
113  PreUpdate?.Invoke(deltaTime);
114  if (!enabled) { return; }
115  if (IsMouseOver || (!RequireMouseOn && SelectedWidgets.Contains(this) && PlayerInput.PrimaryMouseButtonHeld()))
116  {
117  Hovered?.Invoke();
118  if (RequireMouseOn || PlayerInput.PrimaryMouseButtonDown())
119  {
120  if ((multiselect && !SelectedWidgets.Contains(this)) || SelectedWidgets.None())
121  {
122  SelectedWidgets.Add(this);
123  Selected?.Invoke();
124  }
125  }
126  }
127  else if (SelectedWidgets.Contains(this))
128  {
129  System.Diagnostics.Debug.WriteLine("selectedWidgets.Contains(this) -> remove");
130  SelectedWidgets.Remove(this);
131  Deselected?.Invoke();
132  }
133  if (IsSelected)
134  {
135  if (PlayerInput.PrimaryMouseButtonDown())
136  {
137  MouseDown?.Invoke();
138  }
139  if (PlayerInput.PrimaryMouseButtonHeld())
140  {
141  MouseHeld?.Invoke(deltaTime);
142  }
143  if (PlayerInput.PrimaryMouseButtonClicked())
144  {
145  MouseUp?.Invoke();
146  }
147  }
148  PostUpdate?.Invoke(deltaTime);
149  }
150 
151  public virtual void Draw(SpriteBatch spriteBatch, float deltaTime)
152  {
153  PreDraw?.Invoke(spriteBatch, deltaTime);
154  var drawRect = DrawRect;
155  switch (Shape)
156  {
157  case WidgetShape.Rectangle:
158  if (SecondaryColor.HasValue)
159  {
160  GUI.DrawRectangle(spriteBatch, drawRect, SecondaryColor.Value, IsFilled, thickness: 2);
161  }
162  GUI.DrawRectangle(spriteBatch, drawRect, Color, IsFilled, thickness: IsSelected ? (int)(Thickness * 3) : (int)Thickness);
163  break;
164  case WidgetShape.Circle:
165  if (SecondaryColor.HasValue)
166  {
167  ShapeExtensions.DrawCircle(spriteBatch, DrawPos, Size / 2, Sides, SecondaryColor.Value, thickness: 2);
168  }
169  ShapeExtensions.DrawCircle(spriteBatch, DrawPos, Size / 2, Sides, Color, thickness: IsSelected ? 3 : 1);
170  break;
171  case WidgetShape.Cross:
172  float halfSize = Size / 2;
173  if (SecondaryColor.HasValue)
174  {
175  GUI.DrawLine(spriteBatch, DrawPos + Vector2.UnitY * halfSize, DrawPos - Vector2.UnitY * halfSize, SecondaryColor.Value, width: 2);
176  GUI.DrawLine(spriteBatch, DrawPos + Vector2.UnitX * halfSize, DrawPos - Vector2.UnitX * halfSize, SecondaryColor.Value, width: 2);
177  }
178  GUI.DrawLine(spriteBatch, DrawPos + Vector2.UnitY * halfSize, DrawPos - Vector2.UnitY * halfSize, Color, width: IsSelected ? 3 : 1);
179  GUI.DrawLine(spriteBatch, DrawPos + Vector2.UnitX * halfSize, DrawPos - Vector2.UnitX * halfSize, Color, width: IsSelected ? 3 : 1);
180  break;
181  default: throw new NotImplementedException(Shape.ToString());
182  }
183  if (IsSelected)
184  {
185  if (ShowTooltip && !Tooltip.IsNullOrEmpty())
186  {
187  var offset = TooltipOffset ?? new Vector2(Size, -Size / 2f);
188  GUIComponent.DrawToolTip(spriteBatch, Tooltip, DrawPos + offset, TextColor, TextBackgroundColor);
189  }
190  }
191  PostDraw?.Invoke(spriteBatch, deltaTime);
192  }
193  }
194 }
WidgetShape
Definition: Widget.cs:11