2 using System.Collections.Generic;
4 using Microsoft.Xna.Framework;
5 using Microsoft.Xna.Framework.Graphics;
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
27 var inputRect = DrawRect;
28 inputRect.Inflate(InputAreaMargin, InputAreaMargin);
33 public Vector2 DrawPos {
get;
set; }
35 public float Thickness = 1f;
39 public int Sides = 40;
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;
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;
62 public bool RequireMouseOn =
true;
64 public Action Refresh;
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;
74 get {
return enabled; }
78 if (!enabled && SelectedWidgets.Contains(
this))
80 SelectedWidgets.Remove(
this);
85 private static bool multiselect;
86 public static bool EnableMultiSelect
88 get {
return multiselect; }
92 if (!multiselect && SelectedWidgets.Multiple())
94 SelectedWidgets = SelectedWidgets.Take(1).ToList();
98 public Vector2? TooltipOffset;
100 public Widget LinkedWidget;
102 public static List<Widget> SelectedWidgets =
new List<Widget>();
104 public Widget(
string id,
int size,
WidgetShape shape)
111 public virtual void Update(
float deltaTime)
113 PreUpdate?.Invoke(deltaTime);
114 if (!enabled) {
return; }
115 if (IsMouseOver || (!RequireMouseOn && SelectedWidgets.Contains(
this) && PlayerInput.PrimaryMouseButtonHeld()))
118 if (RequireMouseOn || PlayerInput.PrimaryMouseButtonDown())
120 if ((multiselect && !SelectedWidgets.Contains(
this)) || SelectedWidgets.None())
122 SelectedWidgets.Add(
this);
127 else if (SelectedWidgets.Contains(
this))
129 System.Diagnostics.Debug.WriteLine(
"selectedWidgets.Contains(this) -> remove");
130 SelectedWidgets.Remove(
this);
131 Deselected?.Invoke();
135 if (PlayerInput.PrimaryMouseButtonDown())
139 if (PlayerInput.PrimaryMouseButtonHeld())
141 MouseHeld?.Invoke(deltaTime);
143 if (PlayerInput.PrimaryMouseButtonClicked())
148 PostUpdate?.Invoke(deltaTime);
151 public virtual void Draw(SpriteBatch spriteBatch,
float deltaTime)
153 PreDraw?.Invoke(spriteBatch, deltaTime);
154 var drawRect = DrawRect;
158 if (SecondaryColor.HasValue)
160 GUI.DrawRectangle(spriteBatch, drawRect, SecondaryColor.Value, IsFilled, thickness: 2);
162 GUI.DrawRectangle(spriteBatch, drawRect, Color, IsFilled, thickness: IsSelected ? (
int)(Thickness * 3) : (
int)Thickness);
165 if (SecondaryColor.HasValue)
167 ShapeExtensions.DrawCircle(spriteBatch, DrawPos, Size / 2, Sides, SecondaryColor.Value, thickness: 2);
169 ShapeExtensions.DrawCircle(spriteBatch, DrawPos, Size / 2, Sides, Color, thickness: IsSelected ? 3 : 1);
172 float halfSize = Size / 2;
173 if (SecondaryColor.HasValue)
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);
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);
181 default:
throw new NotImplementedException(Shape.ToString());
185 if (ShowTooltip && !Tooltip.IsNullOrEmpty())
187 var offset = TooltipOffset ??
new Vector2(Size, -Size / 2f);
188 GUIComponent.DrawToolTip(spriteBatch, Tooltip, DrawPos + offset, TextColor, TextBackgroundColor);
191 PostDraw?.Invoke(spriteBatch, deltaTime);