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  public bool Pulse { get; set; }
163  private float pulseTimer;
164  private float pulseExpand;
165  private bool flashed;
166 
167  public GUISoundType ClickSound { get; set; } = GUISoundType.Select;
168 
169  public override bool PlaySoundOnSelect { get; set; } = true;
170 
171  public GUIButton(RectTransform rectT, Alignment textAlignment = Alignment.Center, string style = "", Color? color = null) : this(rectT, LocalizedString.EmptyString, textAlignment, style, color) { }
172 
173  public GUIButton(RectTransform rectT, LocalizedString text, Alignment textAlignment = Alignment.Center, string style = "", Color? color = null) : base(style, rectT)
174  {
175  CanBeFocused = true;
176  HoverCursor = CursorState.Hand;
177 
178  frame = new GUIFrame(new RectTransform(Vector2.One, rectT), style) { CanBeFocused = false };
179  if (style != null) { GUIStyle.Apply(frame, style == "" ? "GUIButton" : style); }
180  if (color.HasValue)
181  {
182  this.color = frame.Color = color.Value;
183  }
184 
185  var selfStyle = Style;
186  textBlock = new GUITextBlock(new RectTransform(Vector2.One, rectT, Anchor.Center), text, textAlignment: textAlignment, style: null)
187  {
188  TextColor = selfStyle?.TextColor ?? Color.Black,
189  HoverTextColor = selfStyle?.HoverTextColor ?? Color.Black,
190  SelectedTextColor = selfStyle?.SelectedTextColor ?? Color.Black,
191  CanBeFocused = false
192  };
193  if (rectT.Rect.Height == 0 && !text.IsNullOrEmpty())
194  {
196  RectTransform.MinSize = textBlock.RectTransform.MinSize = new Point(0, System.Math.Max(rectT.MinSize.Y, Rect.Height));
198  }
199  GUIStyle.Apply(textBlock, "", this);
200 
201  Enabled = true;
202  }
203 
204  public override void ApplyStyle(GUIComponentStyle style)
205  {
206  base.ApplyStyle(style);
207 
208  frame?.ApplyStyle(style);
209  }
210 
211  public override void Flash(Color? color = null, float flashDuration = 1.5f, bool useRectangleFlash = false, bool useCircularFlash = false, Vector2? flashRectInflate = null)
212  {
213  Frame.Flash(color, flashDuration, useRectangleFlash, useCircularFlash, flashRectInflate);
214  }
215 
216  protected override void Draw(SpriteBatch spriteBatch)
217  {
218  if (Pulse && pulseTimer > 1.0f)
219  {
220  Rectangle expandRect = Rect;
221  float expand = (pulseExpand * 20.0f) * GUI.Scale;
222  expandRect.Inflate(expand, expand);
223 
224  GUIStyle.EndRoundButtonPulse.Draw(spriteBatch, expandRect, ToolBox.GradientLerp(pulseExpand, Color.White, Color.White, Color.Transparent));
225  }
226  }
227 
228  protected override void Update(float deltaTime)
229  {
230  if (!Visible) return;
231  base.Update(deltaTime);
232  if (Rect.Contains(PlayerInput.MousePosition) && CanBeSelected && CanBeFocused && Enabled && GUI.IsMouseOn(this))
233  {
234  State = Selected ?
235  ComponentState.HoverSelected :
236  ComponentState.Hover;
238  {
239  OnButtonDown?.Invoke();
240  }
242  {
243  if (OnPressed != null)
244  {
245  if (OnPressed())
246  {
247  State = ComponentState.Pressed;
248  }
249  }
250  else
251  {
252  State = ComponentState.Pressed;
253  }
254  }
256  {
257  if (PlaySoundOnSelect)
258  {
259  SoundPlayer.PlayUISound(ClickSound);
260  }
261  if (OnClicked != null)
262  {
263  if (OnClicked(this, UserData))
264  {
265  State = ComponentState.Selected;
266  }
267  }
268  else
269  {
270  Selected = !Selected;
271  }
272  }
273  }
274  else
275  {
276  if (!ExternalHighlight)
277  {
278  State = Selected ? ComponentState.Selected : ComponentState.None;
279  }
280  else
281  {
282  State = ComponentState.Hover;
283  }
284  }
285 
286  foreach (GUIComponent child in Children)
287  {
288  child.State = State;
289  }
290 
291  if (Pulse)
292  {
293  pulseTimer += deltaTime;
294  if (pulseTimer > 1.0f)
295  {
296  if (!flashed)
297  {
298  flashed = true;
299  Frame.Flash(Color.White * 0.2f, 0.8f, true);
300  }
301 
302  pulseExpand += deltaTime;
303  if (pulseExpand > 1.0f)
304  {
305  pulseTimer = 0.0f;
306  pulseExpand = 0.0f;
307  flashed = false;
308  }
309  }
310  }
311  }
312  }
313 }
OnClickedHandler OnClicked
Definition: GUIButton.cs:16
override Color OutlineColor
Definition: GUIButton.cs:87
override void ApplyStyle(GUIComponentStyle style)
Definition: GUIButton.cs:204
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:211
override bool PlaySoundOnSelect
Definition: GUIButton.cs:169
override void Draw(SpriteBatch spriteBatch)
Definition: GUIButton.cs:216
override Color HoverColor
Definition: GUIButton.cs:51
GUIButton(RectTransform rectT, Alignment textAlignment=Alignment.Center, string style="", Color? color=null)
Definition: GUIButton.cs:171
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:173
delegate bool OnPressedHandler()
delegate bool OnButtonDownHandler()
delegate bool OnClickedHandler(GUIButton button, object obj)
GUISoundType ClickSound
Definition: GUIButton.cs:167
override void Update(float deltaTime)
Definition: GUIButton.cs:228
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
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