Client LuaCsForBarotrauma
GUITickBox.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 
4 namespace Barotrauma
5 {
6  public class GUITickBox : GUIComponent
7  {
8  private readonly GUILayoutGroup layoutGroup;
9  private readonly GUIFrame box;
10  private readonly GUITextBlock text;
11 
12  public delegate bool OnSelectedHandler(GUITickBox obj);
14 
15  private GUIRadioButtonGroup radioButtonGroup;
16 
17  public override bool Selected
18  {
19  get { return isSelected; }
20  set
21  {
22  SetSelected(value, callOnSelected: true);
23  }
24  }
25 
26  public override ComponentState State
27  {
28  get
29  {
30  return base.State;
31  }
32  set
33  {
34  base.State = value;
35  box.State = TextBlock.State = value;
36  }
37  }
38 
39  public override bool Enabled
40  {
41  get
42  {
43  return enabled;
44  }
45 
46  set
47  {
48  if (value == enabled) { return; }
49  enabled = box.Enabled = TextBlock.Enabled = value;
50  }
51  }
52 
53  public Color TextColor
54  {
55  get { return text.TextColor; }
56  set { text.TextColor = value; }
57  }
58 
59  /*public override Rectangle MouseRect
60  {
61  get
62  {
63  if (!CanBeFocused) return Rectangle.Empty;
64  Rectangle union = Rectangle.Union(box.Rect, TextBlock.Rect);
65  Vector2 textPos = TextBlock.Rect.Location.ToVector2() + TextBlock.TextPos + TextBlock.TextOffset;
66  Vector2 textSize = TextBlock.Font.MeasureString(TextBlock.Text);
67  union = Rectangle.Union(union, new Rectangle(textPos.ToPoint(), textSize.ToPoint()));
68  union = Rectangle.Union(union, Rect);
69  return ClampMouseRectToParent ? ClampRect(union) : union;
70  }
71  }*/
72 
73  public override GUIFont Font
74  {
75  get
76  {
77  return base.Font;
78  }
79 
80  set
81  {
82  base.Font = value;
83  if (text != null) text.Font = value;
84  }
85  }
86 
87  public GUIFrame Box
88  {
89  get { return box; }
90  }
91 
93  {
94  get { return text; }
95  }
96 
97  public override RichString ToolTip
98  {
99  get { return base.ToolTip; }
100  set
101  {
102  base.ToolTip = value;
103  box.ToolTip = value;
104  text.ToolTip = value;
105  }
106  }
107 
109  {
110  get { return text.Text; }
111  set { text.Text = value; }
112  }
113 
114  public float ContentWidth { get; private set; }
115 
116  public GUISoundType SoundType { private get; set; } = GUISoundType.TickBox;
117 
118  public override bool PlaySoundOnSelect { get; set; } = true;
119 
120  public GUITickBox(RectTransform rectT, LocalizedString label, GUIFont font = null, string style = "") : base(null, rectT)
121  {
122  CanBeFocused = true;
123  HoverCursor = CursorState.Hand;
124 
125  layoutGroup = new GUILayoutGroup(new RectTransform(Vector2.One, rectT), true);
126 
127  box = new GUIFrame(new RectTransform(Vector2.One, layoutGroup.RectTransform, Anchor.CenterLeft, scaleBasis: ScaleBasis.BothHeight)
128  {
129  IsFixedSize = true
130  }, string.Empty, Color.DarkGray)
131  {
132  HoverColor = Color.Gray,
133  SelectedColor = Color.DarkGray,
134  CanBeFocused = false
135  };
136  GUIStyle.Apply(box, style == "" ? "GUITickBox" : style);
137  if (box.RectTransform.MinSize.Y > 0)
138  {
139  RectTransform.MinSize = box.RectTransform.MinSize;
140  RectTransform.MaxSize = box.RectTransform.MaxSize;
142  box.RectTransform.MinSize = new Point(box.RectTransform.MinSize.Y);
143  box.RectTransform.Resize(box.RectTransform.MinSize);
144  }
145  Vector2 textBlockScale = new Vector2((float)(Rect.Width - Rect.Height) / (float)Math.Max(Rect.Width, 1.0), 1.0f);
146  text = new GUITextBlock(new RectTransform(textBlockScale, layoutGroup.RectTransform, Anchor.CenterLeft), label, font: font, textAlignment: Alignment.CenterLeft)
147  {
148  CanBeFocused = false
149  };
150  GUIStyle.Apply(text, "GUITextBlock", this);
151  Enabled = true;
152 
153  ResizeBox();
154 
155  rectT.ScaleChanged += ResizeBox;
156  rectT.SizeChanged += ResizeBox;
157  }
158 
160  {
161  radioButtonGroup = rbg;
162  }
163 
164  public void ResizeBox()
165  {
166  Vector2 textBlockScale = new Vector2(Math.Max(Rect.Width - box.Rect.Width, 0.0f) / Math.Max(Rect.Width, 1.0f), 1.0f);
167  text.RectTransform.RelativeSize = textBlockScale;
168  box.RectTransform.MinSize = new Point(Rect.Height);
169  box.RectTransform.Resize(box.RectTransform.MinSize);
170  text.SetTextPos();
171  ContentWidth = box.Rect.Width + text.Padding.X + text.TextSize.X + text.Padding.Z;
172  }
173 
174  public void SetSelected(bool selected, bool callOnSelected = true)
175  {
176  if (selected == isSelected) { return; }
177  if (radioButtonGroup != null && radioButtonGroup.SelectedRadioButton == this)
178  {
179  isSelected = true;
180  return;
181  }
182 
183  isSelected = selected;
184  State = isSelected ? ComponentState.Selected : ComponentState.None;
185  if (selected && radioButtonGroup != null)
186  {
187  radioButtonGroup.SelectRadioButton(this);
188  }
189  if (callOnSelected)
190  {
191  OnSelected?.Invoke(this);
192  }
193  }
194 
195  protected override void Update(float deltaTime)
196  {
197  if (!Visible) { return; }
198 
199  base.Update(deltaTime);
200 
201  if (GUI.MouseOn == this && Enabled &&
202  //allow clicking on the text area, but not further to the right (the dimensions of the component itself can extend further than the text)
204  {
205  State = Selected ?
206  ComponentState.HoverSelected :
207  ComponentState.Hover;
208 
210  {
211  State = ComponentState.Selected;
212  }
213 
215  {
216  if (radioButtonGroup == null)
217  {
218  Selected = !Selected;
219  }
220  else if (!isSelected)
221  {
222  Selected = true;
223  }
224  if (PlaySoundOnSelect)
225  {
226  SoundPlayer.PlayUISound(SoundType);
227  }
228  }
229  }
230  else if (isSelected)
231  {
232  State = ComponentState.Selected;
233  }
234  else
235  {
236  State = ComponentState.None;
237  }
238  }
239  }
240 }
virtual ComponentState State
virtual Color SelectedColor
virtual Rectangle Rect
virtual Color HoverColor
RectTransform RectTransform
void SelectRadioButton(GUITickBox radioButton)
LocalizedString Text
Definition: GUITickBox.cs:109
GUITextBlock TextBlock
Definition: GUITickBox.cs:93
delegate bool OnSelectedHandler(GUITickBox obj)
override bool Enabled
Definition: GUITickBox.cs:40
OnSelectedHandler OnSelected
Definition: GUITickBox.cs:13
override bool PlaySoundOnSelect
Definition: GUITickBox.cs:118
override GUIFont Font
Definition: GUITickBox.cs:74
override bool Selected
Definition: GUITickBox.cs:18
void SetSelected(bool selected, bool callOnSelected=true)
Definition: GUITickBox.cs:174
GUITickBox(RectTransform rectT, LocalizedString label, GUIFont font=null, string style="")
Definition: GUITickBox.cs:120
override ComponentState State
Definition: GUITickBox.cs:27
override void Update(float deltaTime)
Definition: GUITickBox.cs:195
override RichString ToolTip
Definition: GUITickBox.cs:98
void SetRadioButtonGroup(GUIRadioButtonGroup rbg)
Definition: GUITickBox.cs:159
void Resize(Point absoluteSize, bool resizeChildren=true)
Point?? MinSize
Min size in pixels. Does not affect scaling.
Point NonScaledSize
Size before scale multiplications.
Point?? MaxSize
Max size in pixels. Does not affect scaling.
GUISoundType
Definition: GUI.cs:21
CursorState
Definition: GUI.cs:40