Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ListBoxComponent.c
Go to the documentation of this file.
1 class SCR_ListBoxComponent : ScriptedWidgetComponent
5 {
6  // ---- Public member variables ----
7 
8  ref ScriptInvoker m_OnChanged = new ScriptInvoker(); // (SCR_ListBoxComponent comp, int item, bool newSelected)
9 
10  // ---- Protected member variables ----
11 
12  [Attribute("{2A736E823F7F72BB}UI/layouts/WidgetLibrary/ListBox/WLib_ListBoxIconElement.layout", UIWidgets.ResourceNamePicker, "List box element", "layout")]
13  protected ResourceName m_sElementLayout;
14 
15  [Attribute("{6F2238B8D9FDB169}UI/layouts/WidgetLibrary/ListBox/WLib_ListBoxSeparator.layout", UIWidgets.ResourceNamePicker, "List box separator", "layout")]
16  protected ResourceName m_sSeparatorLayout;
17 
18  [Attribute("false", UIWidgets.CheckBox, "Multiple Selection")]
19  protected bool m_bMultiSelection;
20 
21  protected VerticalLayoutWidget m_wList;
22 
23  // Parallel arrays to manage element components, selected state, custom data
24  protected ref array<SCR_ListBoxElementComponent> m_aElementComponents = {};
25 
26  // Currently selected item - if multi selection is disabled
27  protected int m_iCurrentItem = -1;
28 
29  // Used for generation of unique names for widgets
30  protected int m_iWidgetNameNextId;
31 
32  // ---- Public ----
33 
34  //------------------------------------------------------------------------------------------------
40  int AddItem(string item, Managed data = null, ResourceName itemLayout = string.Empty)
41  {
43 
44  int id = _AddItem(item, data, comp, itemLayout);
45 
46  return id;
47  }
48 
49  //------------------------------------------------------------------------------------------------
57  // -----------------------------------------------------------------------------------------
58  int AddItemAndIcon(string item, ResourceName imageOrImageset, string iconName, Managed data = null, ResourceName itemLayout = string.Empty)
59  {
61 
62  int id = _AddItem(item, data, comp, itemLayout);
63 
64  comp.SetImage(imageOrImageset, iconName);
65 
66  return id;
67  }
68 
69  //------------------------------------------------------------------------------------------------
72  // -----------------------------------------------------------------------------------------
73  void RemoveItem(int item)
74  {
75  if (item < 0 || item > m_aElementComponents.Count())
76  return;
77 
78  Widget elementWidget = m_aElementComponents[item].GetRootWidget();
79 
80  m_aElementComponents.Remove(item);
81  m_wList.RemoveChild(elementWidget);
82  }
83 
84  //------------------------------------------------------------------------------------------------
87  Managed GetItemData(int item)
88  {
89  if (item < 0 || item > m_aElementComponents.Count())
90  return null;
91 
92  return m_aElementComponents[item].GetData();
93  }
94 
95  //------------------------------------------------------------------------------------------------
97  int GetItemCount()
98  {
99  return m_aElementComponents.Count();
100  }
101 
102  //------------------------------------------------------------------------------------------------
104  int FindItemWithData(Managed data)
105  {
106  if (!data)
107  return -1;
108 
109  int ret = -1;
110  int count = m_aElementComponents.Count();
111  for(int i = 0; i < count; i++)
112  {
113  if (m_aElementComponents[i].GetData() == data)
114  {
115  ret = i;
116  break;
117  }
118  }
119 
120  return ret;
121  }
122 
123  //------------------------------------------------------------------------------------------------
125  array<int> GetSelectedItems(bool selected = true)
126  {
127  array<int> a = {};
128 
129  int c = m_aElementComponents.Count();
130 
131  for (int i = 0; i < c; i++)
132  {
133  if (m_aElementComponents[i].GetToggled() == selected)
134  a.Insert(i);
135  }
136 
137  return a;
138  }
139 
140  //------------------------------------------------------------------------------------------------
142  int GetSelectedItem()
143  {
144  if (m_bMultiSelection)
145  return -1;
146 
147  return m_iCurrentItem;
148  }
149 
150  //------------------------------------------------------------------------------------------------
153  SCR_ListBoxElementComponent GetElementComponent(int item)
154  {
155  if (item < 0 || item > m_aElementComponents.Count())
156  return null;
157 
158  return m_aElementComponents[item];
159  }
160 
161  //------------------------------------------------------------------------------------------------
163  bool IsItemSelected(int item)
164  {
165  if (item < 0 || item > m_aElementComponents.Count())
166  return false;
167 
168  return m_aElementComponents[item].GetToggled();
169  }
170 
171  //------------------------------------------------------------------------------------------------
174  void AddSeparator(string text)
175  {
176  // Create widget for this item
177  Widget w = GetGame().GetWorkspace().CreateWidgets(m_sSeparatorLayout, m_wList);
178 
179  TextWidget tw = TextWidget.Cast(w.FindAnyWidget("Text"));
180  if (tw)
181  tw.SetText(text);
182  }
183 
184  //------------------------------------------------------------------------------------------------
185  void SetFocusOnFirstItem()
186  {
187  if (m_aElementComponents.IsEmpty())
188  return;
189 
190  GetGame().GetWorkspace().SetFocusedWidget(m_aElementComponents[0].GetRootWidget());
191  }
192 
193  //------------------------------------------------------------------------------------------------
195  bool GetFocused()
196  {
197  Widget focused = GetGame().GetWorkspace().GetFocusedWidget();
198 
199  if (!focused)
200  return false;
201 
202  foreach (SCR_ListBoxElementComponent comp : m_aElementComponents)
203  {
204  if (comp.GetRootWidget() == focused)
205  return true;
206  }
207 
208  return false;
209  }
210 
211  //------------------------------------------------------------------------------------------------
213  void Clear()
214  {
215  while (m_wList.GetChildren())
216  {
217  m_wList.GetChildren().RemoveFromHierarchy();
218  }
219 
220  m_aElementComponents.Clear();
221  m_iCurrentItem = -1;
222  }
223 
224  //------------------------------------------------------------------------------------------------
228  void SetItemSelected(int item, bool selected, bool invokeOnChanged = true)
229  {
230  if (item < 0 || item > m_aElementComponents.Count())
231  return;
232 
233  // If multiselection is disabled, unselect current item
234  if (!m_bMultiSelection && selected && m_aElementComponents.IsIndexValid(m_iCurrentItem))
235  VisualizeSelection(m_iCurrentItem, false);
236 
237  _SetItemSelected(item, selected, invokeOnChanged);
238  }
239 
240  //------------------------------------------------------------------------------------------------
243  void SetAllItemsSelected(bool selected, bool invokeOnChanged = true)
244  {
245  int c = m_aElementComponents.Count();
246 
247  for (int i = 0; i < c; i++)
248  {
249  _SetItemSelected(i, selected, invokeOnChanged);
250  }
251  }
252 
253  //------------------------------------------------------------------------------------------------
256  void SetItemText(int item, string text)
257  {
258  if (item < 0 || item > m_aElementComponents.Count())
259  return;
260 
261  m_aElementComponents[item].SetText(text);
262  }
263 
264  //------------------------------------------------------------------------------------------------
265  // ---- Protected ----
266  //------------------------------------------------------------------------------------------------
267 
268  //------------------------------------------------------------------------------------------------
269  protected void VisualizeSelection(int item, bool selected)
270  {
271  m_aElementComponents[item].SetToggled(selected);
272  }
273 
274  //------------------------------------------------------------------------------------------------
275  override void HandlerAttached(Widget w)
276  {
277  super.HandlerAttached(w);
278 
279  m_wList = VerticalLayoutWidget.Cast(w.FindAnyWidget("List"));
280  }
281 
282  //------------------------------------------------------------------------------------------------
283  protected int _AddItem(string item, Managed data, out SCR_ListBoxElementComponent compOut, ResourceName itemLayout = string.Empty)
284  {
285  // Create widget for this item
286  // The layout can be provided either as argument or through attribute
287  ResourceName selectedLayout = m_sElementLayout;
288  if (!itemLayout.IsEmpty())
289  selectedLayout = itemLayout;
290  Widget newWidget = GetGame().GetWorkspace().CreateWidgets(selectedLayout, m_wList);
291 
293 
294  comp.SetText(item);
295  comp.SetToggleable(true);
296  comp.SetData(data);
297 
298  // Pushback to internal arrays
299  int id = m_aElementComponents.Insert(comp);
300 
301  // Setup event handlers
302  comp.m_OnClicked.Insert(OnItemClick);
303 
304  // Set up explicit navigation rules for elements. Otherwise we can't navigate
305  // Through separators when we are at the edge of scrolling if there is an element
306  // directly above/below the list box which intercepts focus
307  string widgetName = this.GetUniqueWidgetName();
308  newWidget.SetName(widgetName);
309  if (m_aElementComponents.Count() > 1)
310  {
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());
314  }
315 
316  compOut = comp;
317 
318  return id;
319  }
320 
321  //------------------------------------------------------------------------------------------------
322  protected void _SetItemSelected(int item, bool selected, bool invokeOnChanged)
323  {
324  // Set m_iCurrentItem, if multi selection is not used
325  if (!m_bMultiSelection)
326  {
327  if (selected)
328  {
329  m_iCurrentItem = item;
330  }
331  else
332  {
333  // Nothing will be selected
334  if (item == m_iCurrentItem)
335  m_iCurrentItem = -1;
336  }
337  }
338 
339  bool oldSelected = m_aElementComponents[item].GetToggled();
340  this.VisualizeSelection(item, selected);
341 
342  if (invokeOnChanged && oldSelected != selected) // Only invoke if value actually changed
343  m_OnChanged.Invoke(this, item, selected);
344  }
345 
346  //------------------------------------------------------------------------------------------------
347  protected string GetUniqueWidgetName()
348  {
349  string ret = string.Format("%1_Element_%2", this, m_iWidgetNameNextId);
350  m_iWidgetNameNextId++;
351  return ret;
352  }
353 
354  //------------------------------------------------------------------------------------------------
355  protected void OnItemClick(SCR_ListBoxElementComponent comp)
356  {
357  int id = m_aElementComponents.Find(comp);
358  if (id == -1)
359  return;
360 
361  // Behaviour depends on multi selection
362  if (m_bMultiSelection)
363  {
364  // If multi selection is enabled, inverse the selection state for this item
365  bool selected = m_aElementComponents[id].GetToggled();
366  _SetItemSelected(id, !selected, true);
367  }
368  else
369  {
370  // Unselect previous item
371  if (id != m_iCurrentItem && m_iCurrentItem >= 0 && m_iCurrentItem < m_aElementComponents.Count())
372  this.VisualizeSelection(m_iCurrentItem, false);
373 
374  // Select new item
375  _SetItemSelected(id, true, true);
376  }
377  }
378 }
GetToggled
bool GetToggled()
Definition: SCR_ModularButtonComponent.c:129
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_ListBoxElementComponent
Element of a listbox.
Definition: SCR_ListBoxElementComponent.c:2
m_wList
protected Widget m_wList
Definition: SCR_DownloadManagerListComponent.c:12
GetRootWidget
Widget GetRootWidget()
Definition: SCR_UITaskManagerComponent.c:160
m_OnChanged
protected ref ScriptInvokerTabViewIndex m_OnChanged
Definition: SCR_TabViewComponent.c:64
SCR_ListBoxComponent
Definition: SCR_ListBoxComponent.c:4
Attribute
typedef Attribute
Post-process effect of scripted camera.
GetData
Managed GetData()
Definition: SCR_ModularButtonComponent.c:171
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305