8 ref ScriptInvoker m_OnChanged =
new ScriptInvoker();
12 [
Attribute(
"{2A736E823F7F72BB}UI/layouts/WidgetLibrary/ListBox/WLib_ListBoxIconElement.layout", UIWidgets.ResourceNamePicker,
"List box element",
"layout")]
13 protected ResourceName m_sElementLayout;
15 [
Attribute(
"{6F2238B8D9FDB169}UI/layouts/WidgetLibrary/ListBox/WLib_ListBoxSeparator.layout", UIWidgets.ResourceNamePicker,
"List box separator",
"layout")]
16 protected ResourceName m_sSeparatorLayout;
18 [
Attribute(
"false", UIWidgets.CheckBox,
"Multiple Selection")]
19 protected bool m_bMultiSelection;
21 protected VerticalLayoutWidget m_wList;
24 protected ref array<SCR_ListBoxElementComponent> m_aElementComponents = {};
27 protected int m_iCurrentItem = -1;
30 protected int m_iWidgetNameNextId;
40 int AddItem(
string item, Managed
data =
null, ResourceName itemLayout =
string.Empty)
44 int id = _AddItem(item,
data, comp, itemLayout);
58 int AddItemAndIcon(
string item, ResourceName imageOrImageset,
string iconName, Managed
data =
null, ResourceName itemLayout =
string.Empty)
62 int id = _AddItem(item,
data, comp, itemLayout);
64 comp.SetImage(imageOrImageset, iconName);
73 void RemoveItem(
int item)
75 if (item < 0 || item > m_aElementComponents.Count())
78 Widget elementWidget = m_aElementComponents[item].GetRootWidget();
80 m_aElementComponents.Remove(item);
81 m_wList.RemoveChild(elementWidget);
87 Managed GetItemData(
int item)
89 if (item < 0 || item > m_aElementComponents.Count())
92 return m_aElementComponents[item].GetData();
99 return m_aElementComponents.Count();
104 int FindItemWithData(Managed
data)
110 int count = m_aElementComponents.Count();
111 for(
int i = 0; i < count; i++)
125 array<int> GetSelectedItems(
bool selected =
true)
129 int c = m_aElementComponents.Count();
131 for (
int i = 0; i < c; i++)
133 if (m_aElementComponents[i].
GetToggled() == selected)
142 int GetSelectedItem()
144 if (m_bMultiSelection)
147 return m_iCurrentItem;
155 if (item < 0 || item > m_aElementComponents.Count())
158 return m_aElementComponents[item];
163 bool IsItemSelected(
int item)
165 if (item < 0 || item > m_aElementComponents.Count())
168 return m_aElementComponents[item].GetToggled();
174 void AddSeparator(
string text)
177 Widget w =
GetGame().GetWorkspace().CreateWidgets(m_sSeparatorLayout, m_wList);
179 TextWidget tw = TextWidget.Cast(w.FindAnyWidget(
"Text"));
185 void SetFocusOnFirstItem()
187 if (m_aElementComponents.IsEmpty())
197 Widget focused =
GetGame().GetWorkspace().GetFocusedWidget();
204 if (comp.GetRootWidget() == focused)
217 m_wList.GetChildren().RemoveFromHierarchy();
220 m_aElementComponents.Clear();
228 void SetItemSelected(
int item,
bool selected,
bool invokeOnChanged =
true)
230 if (item < 0 || item > m_aElementComponents.Count())
234 if (!m_bMultiSelection && selected && m_aElementComponents.IsIndexValid(m_iCurrentItem))
235 VisualizeSelection(m_iCurrentItem,
false);
237 _SetItemSelected(item, selected, invokeOnChanged);
243 void SetAllItemsSelected(
bool selected,
bool invokeOnChanged =
true)
245 int c = m_aElementComponents.Count();
247 for (
int i = 0; i < c; i++)
249 _SetItemSelected(i, selected, invokeOnChanged);
256 void SetItemText(
int item,
string text)
258 if (item < 0 || item > m_aElementComponents.Count())
261 m_aElementComponents[item].SetText(text);
269 protected void VisualizeSelection(
int item,
bool selected)
271 m_aElementComponents[item].SetToggled(selected);
275 override void HandlerAttached(Widget w)
277 super.HandlerAttached(w);
279 m_wList = VerticalLayoutWidget.Cast(w.FindAnyWidget(
"List"));
287 ResourceName selectedLayout = m_sElementLayout;
288 if (!itemLayout.IsEmpty())
289 selectedLayout = itemLayout;
290 Widget newWidget =
GetGame().GetWorkspace().CreateWidgets(selectedLayout, m_wList);
295 comp.SetToggleable(
true);
299 int id = m_aElementComponents.Insert(comp);
302 comp.m_OnClicked.Insert(OnItemClick);
307 string widgetName = this.GetUniqueWidgetName();
308 newWidget.SetName(widgetName);
309 if (m_aElementComponents.Count() > 1)
311 Widget prevWidget = m_aElementComponents[m_aElementComponents.Count() - 2].GetRootWidget();
312 prevWidget.SetNavigation(WidgetNavigationDirection.DOWN, WidgetNavigationRuleType.EXPLICIT, newWidget.GetName());
313 newWidget.SetNavigation(WidgetNavigationDirection.UP, WidgetNavigationRuleType.EXPLICIT, prevWidget.GetName());
322 protected void _SetItemSelected(
int item,
bool selected,
bool invokeOnChanged)
325 if (!m_bMultiSelection)
329 m_iCurrentItem = item;
334 if (item == m_iCurrentItem)
339 bool oldSelected = m_aElementComponents[item].GetToggled();
340 this.VisualizeSelection(item, selected);
342 if (invokeOnChanged && oldSelected != selected)
347 protected string GetUniqueWidgetName()
349 string ret =
string.Format(
"%1_Element_%2",
this, m_iWidgetNameNextId);
350 m_iWidgetNameNextId++;
357 int id = m_aElementComponents.Find(comp);
362 if (m_bMultiSelection)
365 bool selected = m_aElementComponents[id].GetToggled();
366 _SetItemSelected(
id, !selected,
true);
371 if (
id != m_iCurrentItem && m_iCurrentItem >= 0 && m_iCurrentItem < m_aElementComponents.Count())
372 this.VisualizeSelection(m_iCurrentItem,
false);
375 _SetItemSelected(
id,
true,
true);