Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ListBoxComponent.c
Go to the documentation of this file.
1
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")]
14
15 [Attribute("{6F2238B8D9FDB169}UI/layouts/WidgetLibrary/ListBox/WLib_ListBoxSeparator.layout", UIWidgets.ResourceNamePicker, "List box separator", "layout")]
17
18 [Attribute("false", UIWidgets.CheckBox, "Multiple Selection")]
19 protected bool m_bMultiSelection;
20
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, out SCR_ListBoxElementComponent outComp, 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 outComp = comp;
67
68 return id;
69 }
70
71 //------------------------------------------------------------------------------------------------
74 // -----------------------------------------------------------------------------------------
75 void RemoveItem(int item)
76 {
77 if (item < 0 || item > m_aElementComponents.Count())
78 return;
79
80 Widget elementWidget = m_aElementComponents[item].GetRootWidget();
81
82 m_aElementComponents.Remove(item);
83 m_wList.RemoveChild(elementWidget);
84 }
85
86 //------------------------------------------------------------------------------------------------
89 Managed GetItemData(int item)
90 {
91 if (item < 0 || item > m_aElementComponents.Count())
92 return null;
93
94 return m_aElementComponents[item].GetData();
95 }
96
97 //------------------------------------------------------------------------------------------------
100 {
101 return m_aElementComponents.Count();
102 }
103
104 //------------------------------------------------------------------------------------------------
107 {
108 if (!data)
109 return -1;
110
111 int ret = -1;
112 int count = m_aElementComponents.Count();
113 for(int i = 0; i < count; i++)
114 {
116 {
117 ret = i;
118 break;
119 }
120 }
121
122 return ret;
123 }
124
125 //------------------------------------------------------------------------------------------------
127 array<int> GetSelectedItems(bool selected = true)
128 {
129 array<int> a = {};
130
131 int c = m_aElementComponents.Count();
132
133 for (int i = 0; i < c; i++)
134 {
135 if (m_aElementComponents[i].GetToggled() == selected)
136 a.Insert(i);
137 }
138
139 return a;
140 }
141
142 //------------------------------------------------------------------------------------------------
145 {
147 return -1;
148
149 return m_iCurrentItem;
150 }
151
152 //------------------------------------------------------------------------------------------------
156 {
157 if (item < 0 || item > m_aElementComponents.Count())
158 return null;
159
160 return m_aElementComponents[item];
161 }
162
163 //------------------------------------------------------------------------------------------------
165 bool IsItemSelected(int item)
166 {
167 if (item < 0 || item > m_aElementComponents.Count())
168 return false;
169
170 return m_aElementComponents[item].GetToggled();
171 }
172
173 //------------------------------------------------------------------------------------------------
176 Widget AddSeparator(string text)
177 {
178 // Create widget for this item
179 Widget w = GetGame().GetWorkspace().CreateWidgets(m_sSeparatorLayout, m_wList);
180
181 TextWidget tw = TextWidget.Cast(w.FindAnyWidget("Text"));
182 if (tw)
183 tw.SetText(text);
184
185 return w;
186 }
187
188 //------------------------------------------------------------------------------------------------
190 {
191 if (m_aElementComponents.IsEmpty())
192 return;
193
194 GetGame().GetWorkspace().SetFocusedWidget(m_aElementComponents[0].GetRootWidget());
195 }
196
197 //------------------------------------------------------------------------------------------------
200 {
201 Widget focused = GetGame().GetWorkspace().GetFocusedWidget();
202
203 if (!focused)
204 return false;
205
207 {
208 if (comp.GetRootWidget() == focused)
209 return true;
210 }
211
212 return false;
213 }
214
215 //------------------------------------------------------------------------------------------------
217 void Clear()
218 {
219 while (m_wList.GetChildren())
220 {
221 m_wList.GetChildren().RemoveFromHierarchy();
222 }
223
224 m_aElementComponents.Clear();
225 m_iCurrentItem = -1;
226 }
227
228 //------------------------------------------------------------------------------------------------
232 void SetItemSelected(int item, bool selected, bool invokeOnChanged = true, bool instant = false)
233 {
234 if (item < 0 || item > m_aElementComponents.Count())
235 return;
236
237 // If multiselection is disabled, unselect current item
238 if (!m_bMultiSelection && selected && m_aElementComponents.IsIndexValid(m_iCurrentItem))
239 VisualizeSelection(m_iCurrentItem, false, instant);
240
241 _SetItemSelected(item, selected, invokeOnChanged, instant);
242 }
243
244 //------------------------------------------------------------------------------------------------
247 void SetAllItemsSelected(bool selected, bool invokeOnChanged = true)
248 {
249 int c = m_aElementComponents.Count();
250
251 for (int i = 0; i < c; i++)
252 {
253 _SetItemSelected(i, selected, invokeOnChanged);
254 }
255 }
256
257 //------------------------------------------------------------------------------------------------
260 void SetItemText(int item, string text)
261 {
262 if (item < 0 || item > m_aElementComponents.Count())
263 return;
264
265 m_aElementComponents[item].SetText(text);
266 }
267
268 //------------------------------------------------------------------------------------------------
269 // ---- Protected ----
270 //------------------------------------------------------------------------------------------------
271
272 //------------------------------------------------------------------------------------------------
273 protected void VisualizeSelection(int item, bool selected, bool instant = false)
274 {
275 m_aElementComponents[item].SetToggled(selected, true, instant);
276 }
277
278 //------------------------------------------------------------------------------------------------
279 override void HandlerAttached(Widget w)
280 {
281 super.HandlerAttached(w);
282
283 m_wList = VerticalLayoutWidget.Cast(w.FindAnyWidget("List"));
284 }
285
286 //------------------------------------------------------------------------------------------------
287 protected int _AddItem(string item, Managed data, out SCR_ListBoxElementComponent compOut, ResourceName itemLayout = string.Empty)
288 {
289 // Create widget for this item
290 // The layout can be provided either as argument or through attribute
291 ResourceName selectedLayout = m_sElementLayout;
292 if (!itemLayout.IsEmpty())
293 selectedLayout = itemLayout;
294 Widget newWidget = GetGame().GetWorkspace().CreateWidgets(selectedLayout, m_wList);
295
297
298 comp.SetText(item);
299 comp.SetToggleable(true);
300 comp.SetData(data);
301
302 // Pushback to internal arrays
303 int id = m_aElementComponents.Insert(comp);
304
305 // Setup event handlers
306 comp.m_OnClicked.Insert(OnItemClick);
307
308 // Set up explicit navigation rules for elements. Otherwise we can't navigate
309 // Through separators when we are at the edge of scrolling if there is an element
310 // directly above/below the list box which intercepts focus
311 string widgetName = this.GetUniqueWidgetName();
312 newWidget.SetName(widgetName);
313 if (m_aElementComponents.Count() > 1)
314 {
315 Widget prevWidget = m_aElementComponents[m_aElementComponents.Count() - 2].GetRootWidget();
316 prevWidget.SetNavigation(WidgetNavigationDirection.DOWN, WidgetNavigationRuleType.EXPLICIT, newWidget.GetName());
317 newWidget.SetNavigation(WidgetNavigationDirection.UP, WidgetNavigationRuleType.EXPLICIT, prevWidget.GetName());
318 }
319
320 compOut = comp;
321
322 return id;
323 }
324
325 //------------------------------------------------------------------------------------------------
326 protected void _SetItemSelected(int item, bool selected, bool invokeOnChanged, bool instant = false)
327 {
328 // Set m_iCurrentItem, if multi selection is not used
330 {
331 if (selected)
332 {
333 m_iCurrentItem = item;
334 }
335 else
336 {
337 // Nothing will be selected
338 if (item == m_iCurrentItem)
339 m_iCurrentItem = -1;
340 }
341 }
342
343 bool oldSelected = m_aElementComponents[item].GetToggled();
344 VisualizeSelection(item, selected, instant);
345
346 if (invokeOnChanged && oldSelected != selected) // Only invoke if value actually changed
347 m_OnChanged.Invoke(this, item, selected);
348 }
349
350 //------------------------------------------------------------------------------------------------
351 protected string GetUniqueWidgetName()
352 {
353 string ret = string.Format("%1_Element_%2", this, m_iWidgetNameNextId);
355 return ret;
356 }
357
358 //------------------------------------------------------------------------------------------------
360 {
361 int id = m_aElementComponents.Find(comp);
362 if (id == -1)
363 return;
364
365 // Behaviour depends on multi selection
367 {
368 // If multi selection is enabled, inverse the selection state for this item
369 bool selected = m_aElementComponents[id].GetToggled();
370 _SetItemSelected(id, !selected, true);
371 }
372 else
373 {
374 // Unselect previous item
377
378 // Select new item
379 _SetItemSelected(id, true, true);
380 }
381 }
382}
AddonBuildInfoTool id
ArmaReforgerScripted GetGame()
Definition game.c:1398
override string GetData()
Get all prefabs that have the spawner data
bool GetToggled()
Widget GetRootWidget()
void Clear()
Removes all items and separators from the listbox.
array< int > GetSelectedItems(bool selected=true)
Returns IDs of selected items.
int GetSelectedItem()
Returns ID of currently selected item, if multiselection is disabled. Otherwise returns -1.
Managed GetItemData(int item)
bool IsItemSelected(int item)
Returns true if item with given ID is selected.
void SetItemText(int item, string text)
Widget AddSeparator(string text)
int AddItemAndIcon(string item, ResourceName imageOrImageset, string iconName, out SCR_ListBoxElementComponent outComp, Managed data=null, ResourceName itemLayout=string.Empty)
ref array< SCR_ListBoxElementComponent > m_aElementComponents
void VisualizeSelection(int item, bool selected, bool instant=false)
override void HandlerAttached(Widget w)
int FindItemWithData(Managed data)
Returns ID of item with same user data as propvided. Returns -1 if not found or null was passed.
bool GetFocused()
Returns true when any list entry is focused.
void OnItemClick(SCR_ListBoxElementComponent comp)
SCR_ListBoxElementComponent GetElementComponent(int item)
int _AddItem(string item, Managed data, out SCR_ListBoxElementComponent compOut, ResourceName itemLayout=string.Empty)
VerticalLayoutWidget m_wList
void SetAllItemsSelected(bool selected, bool invokeOnChanged=true)
void SetItemSelected(int item, bool selected, bool invokeOnChanged=true, bool instant=false)
void _SetItemSelected(int item, bool selected, bool invokeOnChanged, bool instant=false)
int AddItem(string item, Managed data=null, ResourceName itemLayout=string.Empty)
void SetImage(ResourceName imageOrImageset, string iconName)
SCR_FieldOfViewSettings Attribute
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134