Client LuaCsForBarotrauma
GUIButton.cs
1 using System;
2 using System.Linq;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Graphics;
5 
6 namespace Barotrauma
7 {
8  public class GUIButton : GUIComponent
9  {
11  public GUITextBlock TextBlock { get { return textBlock; } }
12  protected GUIFrame frame;
13  public GUIFrame Frame { get { return frame; } }
14 
15  public delegate bool OnClickedHandler(GUIButton button, object obj);
17 
18  public delegate bool OnPressedHandler();
20 
21  public delegate bool OnButtonDownHandler();
23 
24  public bool CanBeSelected = true;
25 
26  public override bool Enabled
27  {
28  get
29  {
30  return enabled;
31  }
32 
33  set
34  {
35  if (value == enabled) { return; }
37  }
38  }
39 
40  public override Color Color
41  {
42  get { return base.Color; }
43  set
44  {
45  base.Color = value;
46  frame.Color = value;
47  }
48  }
49 
50  public override Color HoverColor
51  {
52  get { return base.HoverColor; }
53  set
54  {
55  base.HoverColor = value;
56  frame.HoverColor = value;
57  }
58  }
59 
60  public override Color SelectedColor
61  {
62  get
63  {
64  return base.SelectedColor;
65  }
66  set
67  {
68  base.SelectedColor = value;
69  frame.SelectedColor = value;
70  }
71  }
72 
73  public override Color PressedColor
74  {
75  get
76  {
77  return base.PressedColor;
78  }
79  set
80  {
81  base.PressedColor = value;
82  frame.PressedColor = value;
83  }
84  }
85 
86  public override Color OutlineColor
87  {
88  get { return base.OutlineColor; }
89  set
90  {
91  base.OutlineColor = value;
92  if (frame != null) frame.OutlineColor = value;
93  }
94  }
95 
96  public Color TextColor
97  {
98  get { return textBlock.TextColor; }
99  set { textBlock.TextColor = value; }
100  }
101 
102  public Color HoverTextColor
103  {
104  get { return textBlock.HoverTextColor; }
105  set { textBlock.HoverTextColor = value; }
106  }
107 
108  public Color SelectedTextColor
109  {
110  get { return textBlock.SelectedTextColor; }
111  set { textBlock.SelectedTextColor = value; }
112  }
113 
114  public Color DisabledTextColor
115  {
116  get { return textBlock.DisabledTextColor; }
117  }
118 
119  public override float FlashTimer
120  {
121  get { return Frame.FlashTimer; }
122  }
123 
124  public override GUIFont Font
125  {
126  get
127  {
128  return (textBlock == null) ? GUIStyle.Font : textBlock.Font;
129  }
130  set
131  {
132  base.Font = value;
133  if (textBlock != null) { textBlock.Font = value; }
134  }
135  }
136 
138  {
139  get { return textBlock.Text; }
140  set { textBlock.Text = value; }
141  }
142 
144  {
145  get { return textBlock.ForceUpperCase; }
146  set { textBlock.ForceUpperCase = value; }
147  }
148 
149  public override RichString ToolTip
150  {
151  get
152  {
153  return base.ToolTip;
154  }
155  set
156  {
157  base.ToolTip = value;
158  textBlock.ToolTip = value;
159  }
160  }
161 
162  private GUIComponent holdOverlay;
163 
164  private bool requireHold;
165  public bool RequireHold
166  {
167  get => requireHold;
168  set
169  {
170  requireHold = value;
171  if (value)
172  {
173  holdOverlay ??= new GUIFrame(new RectTransform(new Vector2(0.5f, 1f), Frame.RectTransform, Anchor.CenterLeft), style: null)
174  {
175  Color = GUIStyle.Yellow * 0.33f,
176  CanBeFocused = false,
177  IgnoreLayoutGroups = true,
178  Visible = true
179  };
180  }
181  else if (holdOverlay != null)
182  {
183  holdOverlay.Visible = false;
184  }
185  }
186  }
187  public float HoldDurationSeconds { get; set; } = 5f;
188  private float holdTimer;
189 
190  public bool Pulse { get; set; }
191  private float pulseTimer;
192  private float pulseExpand;
193  private bool flashed;
194 
195  public GUISoundType ClickSound { get; set; } = GUISoundType.Select;
196 
197  public override bool PlaySoundOnSelect { get; set; } = true;
198 
199  public GUIButton(RectTransform rectT, Alignment textAlignment = Alignment.Center, string style = "", Color? color = null) : this(rectT, LocalizedString.EmptyString, textAlignment, style, color) { }
200 
201  public GUIButton(RectTransform rectT, LocalizedString text, Alignment textAlignment = Alignment.Center, string style = "", Color? color = null) : base(style, rectT)
202  {
203  CanBeFocused = true;
204  HoverCursor = CursorState.Hand;
205 
206  frame = new GUIFrame(new RectTransform(Vector2.One, rectT), style) { CanBeFocused = false };
207  if (style != null) { GUIStyle.Apply(frame, style == "" ? "GUIButton" : style); }
208  if (color.HasValue)
209  {
210  this.color = frame.Color = color.Value;
211  }
212 
213  var selfStyle = Style;
214  textBlock = new GUITextBlock(new RectTransform(Vector2.One, rectT, Anchor.Center), text, textAlignment: textAlignment, style: null)
215  {
216  TextColor = selfStyle?.TextColor ?? Color.Black,
217  HoverTextColor = selfStyle?.HoverTextColor ?? Color.Black,
218  SelectedTextColor = selfStyle?.SelectedTextColor ?? Color.Black,
219  CanBeFocused = false
220  };
221  if (rectT.Rect.Height == 0 && !text.IsNullOrEmpty())
222  {
224  RectTransform.MinSize = textBlock.RectTransform.MinSize = new Point(0, System.Math.Max(rectT.MinSize.Y, Rect.Height));
226  }
227  GUIStyle.Apply(textBlock, "", this);
228 
229  Enabled = true;
230  }
231 
232  public override void ApplyStyle(GUIComponentStyle style)
233  {
234  base.ApplyStyle(style);
235 
236  frame?.ApplyStyle(style);
237  }
238 
239  public override void Flash(Color? color = null, float flashDuration = 1.5f, bool useRectangleFlash = false, bool useCircularFlash = false, Vector2? flashRectInflate = null)
240  {
241  Frame.Flash(color, flashDuration, useRectangleFlash, useCircularFlash, flashRectInflate);
242  }
243 
244  protected override void Draw(SpriteBatch spriteBatch)
245  {
246  if (Pulse && pulseTimer > 1.0f)
247  {
248  Rectangle expandRect = Rect;
249  float expand = (pulseExpand * 20.0f) * GUI.Scale;
250  expandRect.Inflate(expand, expand);
251 
252  GUIStyle.EndRoundButtonPulse.Draw(spriteBatch, expandRect, ToolBox.GradientLerp(pulseExpand, Color.White, Color.White, Color.Transparent));
253  }
254  }
255 
256  protected override void Update(float deltaTime)
257  {
258  if (!Visible) return;
259  base.Update(deltaTime);
260  if (Rect.Contains(PlayerInput.MousePosition) && CanBeSelected && CanBeFocused && Enabled && GUI.IsMouseOn(this))
261  {
262  State = Selected ?
263  ComponentState.HoverSelected :
264  ComponentState.Hover;
266  {
267  OnButtonDown?.Invoke();
268  }
270  {
271  if (RequireHold)
272  {
273  holdTimer += deltaTime;
274  }
275 
276  if (OnPressed != null)
277  {
278  if (OnPressed())
279  {
280  State = ComponentState.Pressed;
281  }
282  }
283  else
284  {
285  State = ComponentState.Pressed;
286  }
287  }
289  {
290  if (!RequireHold || holdTimer > HoldDurationSeconds)
291  {
292  if (PlaySoundOnSelect)
293  {
294  SoundPlayer.PlayUISound(ClickSound);
295  }
296 
297  if (OnClicked != null)
298  {
299  if (OnClicked(this, UserData))
300  {
301  State = ComponentState.Selected;
302  }
303  }
304  else
305  {
306  Selected = !Selected;
307  }
308  }
309  }
310  else
311  {
312  holdTimer = 0.0f;
313  }
314  }
315  else
316  {
317  holdTimer = 0.0f;
318  if (!ExternalHighlight)
319  {
320  State = Selected ? ComponentState.Selected : ComponentState.None;
321  }
322  else
323  {
324  State = ComponentState.Hover;
325  }
326  }
327 
328  if (RequireHold)
329  {
330  float width = MathHelper.Clamp(holdTimer / HoldDurationSeconds, 0f, 1f);
331  if (!MathUtils.NearlyEqual(width, holdOverlay.RectTransform.RelativeSize.X))
332  {
333  holdOverlay.RectTransform.RelativeSize = new Vector2(width, 1f);
334  }
335 
336  holdOverlay.Color =
337  holdTimer >= HoldDurationSeconds
338  ? Color.Green * 0.33f
339  : Color.Red * 0.33f;
340  }
341 
342  foreach (GUIComponent child in Children)
343  {
344  child.State = State;
345  }
346 
347  if (Pulse)
348  {
349  pulseTimer += deltaTime;
350  if (pulseTimer > 1.0f)
351  {
352  if (!flashed)
353  {
354  flashed = true;
355  Frame.Flash(Color.White * 0.2f, 0.8f, true);
356  }
357 
358  pulseExpand += deltaTime;
359  if (pulseExpand > 1.0f)
360  {
361  pulseTimer = 0.0f;
362  pulseExpand = 0.0f;
363  flashed = false;
364  }
365  }
366  }
367  }
368  }
369 }
OnClickedHandler OnClicked
Definition: GUIButton.cs:16
override Color OutlineColor
Definition: GUIButton.cs:87
override void ApplyStyle(GUIComponentStyle style)
Definition: GUIButton.cs:232
override bool Enabled
Definition: GUIButton.cs:27
override RichString ToolTip
Definition: GUIButton.cs:150
override void Flash(Color? color=null, float flashDuration=1.5f, bool useRectangleFlash=false, bool useCircularFlash=false, Vector2? flashRectInflate=null)
Definition: GUIButton.cs:239
override bool PlaySoundOnSelect
Definition: GUIButton.cs:197
override void Draw(SpriteBatch spriteBatch)
Definition: GUIButton.cs:244
override Color HoverColor
Definition: GUIButton.cs:51
GUIButton(RectTransform rectT, Alignment textAlignment=Alignment.Center, string style="", Color? color=null)
Definition: GUIButton.cs:199
override GUIFont? Font
Definition: GUIButton.cs:125
OnButtonDownHandler OnButtonDown
Definition: GUIButton.cs:22
GUITextBlock TextBlock
Definition: GUIButton.cs:11
OnPressedHandler OnPressed
Definition: GUIButton.cs:19
LocalizedString Text
Definition: GUIButton.cs:138
GUITextBlock textBlock
Definition: GUIButton.cs:10
override Color PressedColor
Definition: GUIButton.cs:74
GUIButton(RectTransform rectT, LocalizedString text, Alignment textAlignment=Alignment.Center, string style="", Color? color=null)
Definition: GUIButton.cs:201
delegate bool OnPressedHandler()
delegate bool OnButtonDownHandler()
delegate bool OnClickedHandler(GUIButton button, object obj)
GUISoundType ClickSound
Definition: GUIButton.cs:195
override void Update(float deltaTime)
Definition: GUIButton.cs:256
override float FlashTimer
Definition: GUIButton.cs:120
override Color SelectedColor
Definition: GUIButton.cs:61
virtual ComponentState State
virtual void Flash(Color? color=null, float flashDuration=1.5f, bool useRectangleFlash=false, bool useCircularFlash=false, Vector2? flashRectInflate=null)
virtual Color OutlineColor
virtual float FlashTimer
virtual void ApplyStyle(GUIComponentStyle style)
virtual RichString ToolTip
virtual Color SelectedColor
virtual Rectangle Rect
virtual Color HoverColor
GUIComponentStyle Style
RectTransform RectTransform
IEnumerable< GUIComponent > Children
Definition: GUIComponent.cs:29
virtual Color PressedColor
Vector2 MeasureString(LocalizedString str, bool removeExtraSpacing=false)
Definition: GUIPrefab.cs:284
override GUIFont Font
Definition: GUITextBlock.cs:66
ForceUpperCase ForceUpperCase
Vector2 RelativeSize
Relative to the parent rect.
void Resize(Point absoluteSize, bool resizeChildren=true)
Point?? MinSize
Min size in pixels. Does not affect scaling.
GUISoundType
Definition: GUI.cs:21
CursorState
Definition: GUI.cs:40