Client LuaCsForBarotrauma
GUISelectionCarousel.cs
1 #nullable enable
2 
3 using System;
4 using Microsoft.Xna.Framework;
5 using System.Collections.Generic;
6 using System.Linq;
7 
8 namespace Barotrauma
9 {
15  {
16  object? GetSelectedElement();
17  void SelectElement(object? value);
18  }
19 
24  {
25  public record class Element(T value, LocalizedString text, LocalizedString toolTip);
26 
27  public delegate void OnValueChangedHandler(GUISelectionCarousel<T> carousel);
29 
33  public Func<T, bool>? ElementSelectionCondition { get; set; }
34 
35  public GUITextBlock TextBlock { get; private set; }
36 
37  public GUIButton RightButton { get; private set; }
38  public GUIButton LeftButton { get; private set; }
39 
40  private readonly List<Element> elements = new List<Element>();
41 
42  private readonly GUILayoutGroup layoutGroup;
43 
44  public Element? SelectedElement { get; private set; }
45  public T? SelectedValue => SelectedElement == null ? default : SelectedElement.value;
46  public LocalizedString SelectedText => SelectedElement?.text ?? string.Empty;
47 
48  public override bool Enabled
49  {
50  get => base.Enabled;
51  set
52  {
53  base.Enabled = RightButton.Enabled = LeftButton.Enabled = TextBlock.Enabled = value;
54  }
55  }
56 
57 
58  public override Color Color
59  {
60  get { return color; }
61  set
62  {
63  color = value;
65  }
66  }
67 
68  public Color TextColor
69  {
70  get { return TextBlock.TextColor; }
71  set { TextBlock.TextColor = value; }
72  }
73 
74  public override Color HoverColor
75  {
76  get => base.HoverColor;
77  set
78  {
79  base.HoverColor = value;
80  TextBlock.HoverColor = value;
81  }
82  }
83 
84  public GUISelectionCarousel(RectTransform rectT, string style = "", params (T value, LocalizedString text)[] newElements) : base(style, rectT)
85  {
86  layoutGroup = new GUILayoutGroup(new RectTransform(Vector2.One, rectT), isHorizontal: true, childAnchor: Anchor.CenterLeft)
87  {
88  RelativeSpacing = 0.05f,
89  Stretch = true
90  };
91 
92  LeftButton = new GUIButton(new RectTransform(new Vector2(0.15f, 1.0f), layoutGroup.RectTransform), style: "GUIButtonToggleLeft");
93  GUIStyle.Apply(LeftButton, "LeftButton", this);
94  TextBlock = new GUITextBlock(new RectTransform(new Vector2(0.7f, 1.0f), layoutGroup.RectTransform), "", textAlignment: Alignment.Center, style: "GUITextBox");
95  GUIStyle.Apply(TextBlock, "TextBlock", this);
96  RightButton = new GUIButton(new RectTransform(new Vector2(0.15f, 1.0f), layoutGroup.RectTransform), style: "GUIButtonToggleRight");
97  GUIStyle.Apply(RightButton, "RightButton", this);
98 
99  RightButton.OnClicked += (_, _) => SelectNextValidElement();
100  LeftButton.OnClicked += (_, _) => SelectNextValidElement(directionLeft: true);
101 
102  if (newElements != null && newElements.Any())
103  {
104  SetElements(newElements);
105  }
106  }
107 
108  public object? GetSelectedElement()
109  {
110  return SelectedValue;
111  }
112 
116  public void SelectElement(object? value)
117  {
118  if (value == null)
119  {
120  SelectElement(null);
121  return;
122  }
123  var matchingElement = elements.Where(e => value.Equals(e.value)) // selection is in the set of possible values
124  .FirstOrDefault(e => ElementSelectionCondition == null || ElementSelectionCondition(e.value)); // selection matches extra conditions, if any
125  if (matchingElement != null)
126  {
127  SelectElement(matchingElement);
128  }
129  }
130 
131  public void SelectElement(Element? element)
132  {
133  SelectedElement = element;
134  TextBlock.Text = element?.text ?? string.Empty;
135  TextBlock.ToolTip = element?.toolTip ?? string.Empty;
136  OnValueChanged?.Invoke(this);
137  }
138 
142  public void SetElements(params (T value, LocalizedString text)[] elements)
143  {
144  this.elements.Clear();
145  foreach ((T value, LocalizedString text) in elements)
146  {
147  AddElement(value, text);
148  }
149  }
150 
154  public void SetElements(params (T value, LocalizedString text, LocalizedString toolTip)[] elements)
155  {
156  this.elements.Clear();
157  foreach ((T value, LocalizedString text, LocalizedString toolTip) in elements)
158  {
159  AddElement(value, text, toolTip);
160  }
161  }
162 
163  public void AddElement(T value, LocalizedString text, LocalizedString? tooltip = null)
164  {
165  var newElement = new Element(value, text, tooltip ?? string.Empty);
166  elements.Add(newElement);
167  if (SelectedElement == null)
168  {
169  SelectElement(newElement);
170  }
171  }
175  public void Refresh()
176  {
177  if (SelectedElement != null)
178  {
180  {
181  return;
182  }
183  }
184 
185  SelectElement(elements.FirstOrDefault(e => ElementSelectionCondition == null || ElementSelectionCondition(e.value)));
186  }
187 
188  private bool SelectNextValidElement(bool directionLeft = false)
189  {
190  if (elements.Count < 2) { return false; }
191 
192  // Try to find a valid next/previous element
193  int currentIndex = SelectedElement == null ? -1 : elements.IndexOf(SelectedElement);
194  int newIndex = currentIndex;
195  for (int i = 0; i < elements.Count; i++)
196  {
197  newIndex = directionLeft ? MathUtils.PositiveModulo((newIndex - 1), elements.Count) : (newIndex + 1) % elements.Count;
198  if (ElementSelectionCondition == null || ElementSelectionCondition(elements[newIndex].value))
199  {
200  SelectElement(elements[newIndex]);
201  return true;
202  }
203  }
204 
205  // No valid elements found
206  SelectElement(null);
207  return true;
208  }
209  }
210 }
OnClickedHandler OnClicked
Definition: GUIButton.cs:16
override bool Enabled
Definition: GUIButton.cs:27
virtual RichString ToolTip
virtual Color HoverColor
RectTransform RectTransform