4 protected Widget m_wButtonRow;
7 bool m_bAllowMultiselection;
9 [
Attribute(
"{D27D044A8145DC7C}UI/layouts/WidgetLibrary/Buttons/WLib_ButtonTextUnderlined.layout", UIWidgets.ResourceNamePicker,
"Layout element used",
"layout")]
10 ResourceName m_ElementLayout;
13 float m_fElementSpacing;
15 [
Attribute(
"false", UIWidgets.CheckBox,
"On last item and pressing right arrow, it will go to the start of the list")]
18 ref array<SCR_ButtonBaseComponent> m_aSelectionElements =
new array<SCR_ButtonBaseComponent>;
22 override void HandlerAttached(Widget w)
24 super.HandlerAttached(w);
25 m_wButtonRow = w.FindAnyWidget(
"ButtonRow");
27 if (!m_wButtonRow || !m_ElementLayout)
35 protected void CreateWidgets()
37 float padding = m_fElementSpacing * 0.5;
38 LayoutSlot.SetPadding(m_wButtonRow, -padding, -padding, -padding, -padding);
39 foreach (
string name : m_aElementNames)
41 Widget button =
GetGame().GetWorkspace().CreateWidgets(m_ElementLayout, m_wButtonRow);
42 LayoutSlot.SetPadding(button, padding, padding, padding, padding);
43 LayoutSlot.SetSizeMode(button, LayoutSizeMode.Fill);
44 LayoutSlot.SetVerticalAlign(button, LayoutHorizontalAlign.Stretch);
45 LayoutSlot.SetHorizontalAlign(button, LayoutHorizontalAlign.Stretch);
51 m_aSelectionElements.Insert(comp);
54 textComp.SetText(name);
55 comp.m_OnClicked.Insert(OnElementClicked);
56 comp.m_OnToggled.Insert(OnElementChanged);
63 protected void ClearWidgets()
70 Widget w = element.GetRootWidget();
72 w.RemoveFromHierarchy();
75 m_aSelectionElements.Clear();
79 override bool OnFocus(Widget w,
int x,
int y)
81 super.OnFocus(w, x, y);
85 m_FocusedElement.ShowBorder(
true);
87 else if (m_aSelectionElements.Count() > 0)
89 m_aSelectionElements[0].ShowBorder(
true);
90 m_FocusedElement = m_aSelectionElements[0];
93 GetGame().GetInputManager().AddActionListener(
"MenuLeft", EActionTrigger.DOWN, OnMenuLeft);
94 GetGame().GetInputManager().AddActionListener(
"MenuRight", EActionTrigger.DOWN, OnMenuRight);
96 if (m_bAllowMultiselection)
97 GetGame().GetInputManager().AddActionListener(
"MenuSelect", EActionTrigger.DOWN, OnMenuSelect);
103 override bool OnFocusLost(Widget w,
int x,
int y)
105 super.OnFocusLost(w, x, y);
107 if (m_FocusedElement)
108 m_FocusedElement.ShowBorder(
false);
110 GetGame().GetInputManager().RemoveActionListener(
"MenuLeft", EActionTrigger.DOWN, OnMenuLeft);
111 GetGame().GetInputManager().RemoveActionListener(
"MenuRight", EActionTrigger.DOWN, OnMenuRight);
113 if (m_bAllowMultiselection)
114 GetGame().GetInputManager().RemoveActionListener(
"MenuSelect", EActionTrigger.DOWN, OnMenuSelect);
120 protected void SetInitialState()
122 m_FocusedElement = GetFirstSelectedIndex();
123 int realIndex = m_iSelectedItem;
124 m_iSelectedItem = -
int.MAX;
125 SetCurrentItem(realIndex,
false,
false);
129 protected void OnMenuSelect()
131 if (m_FocusedElement)
132 m_FocusedElement.SetToggled(!m_FocusedElement.IsToggled());
140 if (element.IsToggled())
151 int i = m_aSelectionElements.Find(comp);
153 if (m_bAllowMultiselection)
154 SetFocusedItem(comp);
157 if (i == m_iSelectedItem)
159 comp.SetToggled(!comp.IsToggled(),
false,
false);
164 SetCurrentItem(i,
true,
false);
173 if (!m_bAllowMultiselection && !state)
176 int i = m_aSelectionElements.Find(comp);
177 if (m_bAllowMultiselection)
184 override bool SetCurrentItem(
int i,
bool playSound =
false,
bool animate =
false)
186 int oldIndex = m_iSelectedItem;
187 int itemsCount = m_aSelectionElements.Count();
188 if (!super.SetCurrentItem(i,
true,
true))
191 if (!m_bAllowMultiselection && oldIndex >= 0 && oldIndex < itemsCount)
193 m_aSelectionElements[oldIndex].SetToggled(
false, playSound, animate);
196 if (i >= 0 && i < itemsCount)
198 m_aSelectionElements[i].SetToggled(
true, playSound, animate);
200 SetFocusedItem(m_aSelectionElements[i]);
207 override bool OnMouseEnter(Widget w,
int x,
int y)
209 super.OnMouseEnter(w, x, y);
210 if (IsChildWidget(w, WidgetManager.GetWidgetUnderCursor()))
211 super.OnMouseEnter(w, x, y);
217 protected void OnMenuLeft()
219 int i = GetNextValidItem(
true);
223 if (m_bAllowMultiselection)
226 SetCurrentItem(i,
true,
true);
230 protected void OnMenuRight()
232 int i = GetNextValidItem(
false);
236 if (m_bAllowMultiselection)
239 SetCurrentItem(i,
true,
true);
243 protected int GetNextValidItem(
bool toLeft)
245 if (m_aSelectionElements.IsEmpty())
249 int lastIndex = m_aSelectionElements.Count() - 1;
251 int i = m_aSelectionElements.Find(m_FocusedElement);
257 while (foundItem < 0)
268 else if (i > lastIndex)
276 if (m_FocusedElement == m_aSelectionElements[i])
286 int GetItems(notnull array<SCR_ButtonBaseComponent> elements)
291 elements.Insert(element);
293 return elements.Count();
298 if (index < 0 || index >= m_aSelectionElements.Count())
301 return m_aSelectionElements[
index];
305 override int AddItem(
string item,
bool last =
false, Managed
data =
null)
307 int i = super.AddItem(item, last,
data);
314 override void ClearAll()
321 override void RemoveItem(
int item,
bool last =
false)
323 super.RemoveItem(item, last);
329 bool IsItemSelected(
int index)
331 if (index < 0 || index >= m_aSelectionElements.Count())
334 return m_aSelectionElements[
index].IsToggled();
338 void SetItemSelected(
int index,
bool select,
bool animate =
true)
340 if (index < 0 || index >= m_aSelectionElements.Count())
343 m_iSelectedItem =
index;
344 m_aSelectionElements[
index].SetToggled(select, animate, animate);
348 void SetFocusedItem(
int index,
bool animate =
true)
350 if (index < 0 || index >= m_aSelectionElements.Count())
354 if (element == m_FocusedElement)
357 bool focused =
GetGame().GetWorkspace().GetFocusedWidget() ==
m_wRoot;
359 if (m_FocusedElement && focused)
360 m_FocusedElement.ShowBorder(
false, animate);
362 m_FocusedElement = element;
365 m_FocusedElement.ShowBorder(
true, animate);
371 if (!element || element == m_FocusedElement)
374 bool focused =
GetGame().GetWorkspace().GetFocusedWidget() ==
m_wRoot;
376 if (m_FocusedElement && focused)
377 m_FocusedElement.ShowBorder(
false, animate);
379 m_FocusedElement = element;
382 m_FocusedElement.ShowBorder(
true, animate);
388 return m_aSelectionElements.Find(m_FocusedElement);
394 static SCR_ToolboxComponent GetToolboxComponent(
string name, Widget parent,
bool searchAllChildren =
true)