Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ToolboxComponent.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
3 {
4  protected Widget m_wButtonRow;
5 
6  [Attribute()]
7  bool m_bAllowMultiselection;
8 
9  [Attribute("{D27D044A8145DC7C}UI/layouts/WidgetLibrary/Buttons/WLib_ButtonTextUnderlined.layout", UIWidgets.ResourceNamePicker, "Layout element used", "layout")]
10  ResourceName m_ElementLayout;
11 
12  [Attribute("4")]
13  float m_fElementSpacing;
14 
15  [Attribute("false", UIWidgets.CheckBox, "On last item and pressing right arrow, it will go to the start of the list")]
16  bool m_bCycleMode;
17 
18  ref array<SCR_ButtonBaseComponent> m_aSelectionElements = new array<SCR_ButtonBaseComponent>;
19  SCR_ButtonBaseComponent m_FocusedElement;
20 
21  //------------------------------------------------------------------------------------------------
22  override void HandlerAttached(Widget w)
23  {
24  super.HandlerAttached(w);
25  m_wButtonRow = w.FindAnyWidget("ButtonRow");
26 
27  if (!m_wButtonRow || !m_ElementLayout)
28  return;
29 
30  CreateWidgets();
31  SetInitialState();
32  }
33 
34  //------------------------------------------------------------------------------------------------
35  protected void CreateWidgets()
36  {
37  float padding = m_fElementSpacing * 0.5;
38  LayoutSlot.SetPadding(m_wButtonRow, -padding, -padding, -padding, -padding);
39  foreach (string name : m_aElementNames)
40  {
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);
46 
48  if (!comp)
49  continue;
50 
51  m_aSelectionElements.Insert(comp);
53  if (textComp)
54  textComp.SetText(name);
55  comp.m_OnClicked.Insert(OnElementClicked);
56  comp.m_OnToggled.Insert(OnElementChanged);
57  }
58 
59  SetInitialState();
60  }
61 
62  //------------------------------------------------------------------------------------------------
63  protected void ClearWidgets()
64  {
65  foreach (SCR_ButtonBaseComponent element : m_aSelectionElements)
66  {
67  if (!element)
68  continue;
69 
70  Widget w = element.GetRootWidget();
71  if (w)
72  w.RemoveFromHierarchy();
73  }
74 
75  m_aSelectionElements.Clear();
76  }
77 
78  //------------------------------------------------------------------------------------------------
79  override bool OnFocus(Widget w, int x, int y)
80  {
81  super.OnFocus(w, x, y);
82 
83  if (m_FocusedElement)
84  {
85  m_FocusedElement.ShowBorder(true);
86  }
87  else if (m_aSelectionElements.Count() > 0)
88  {
89  m_aSelectionElements[0].ShowBorder(true);
90  m_FocusedElement = m_aSelectionElements[0];
91  }
92 
93  GetGame().GetInputManager().AddActionListener("MenuLeft", EActionTrigger.DOWN, OnMenuLeft);
94  GetGame().GetInputManager().AddActionListener("MenuRight", EActionTrigger.DOWN, OnMenuRight);
95 
96  if (m_bAllowMultiselection)
97  GetGame().GetInputManager().AddActionListener("MenuSelect", EActionTrigger.DOWN, OnMenuSelect);
98 
99  return false;
100  }
101 
102  //------------------------------------------------------------------------------------------------
103  override bool OnFocusLost(Widget w, int x, int y)
104  {
105  super.OnFocusLost(w, x, y);
106 
107  if (m_FocusedElement)
108  m_FocusedElement.ShowBorder(false);
109 
110  GetGame().GetInputManager().RemoveActionListener("MenuLeft", EActionTrigger.DOWN, OnMenuLeft);
111  GetGame().GetInputManager().RemoveActionListener("MenuRight", EActionTrigger.DOWN, OnMenuRight);
112 
113  if (m_bAllowMultiselection)
114  GetGame().GetInputManager().RemoveActionListener("MenuSelect", EActionTrigger.DOWN, OnMenuSelect);
115 
116  return false;
117  }
118 
119  //------------------------------------------------------------------------------------------------
120  protected void SetInitialState()
121  {
122  m_FocusedElement = GetFirstSelectedIndex();
123  int realIndex = m_iSelectedItem;
124  m_iSelectedItem = -int.MAX;
125  SetCurrentItem(realIndex, false, false);
126  }
127 
128  //------------------------------------------------------------------------------------------------
129  protected void OnMenuSelect()
130  {
131  if (m_FocusedElement)
132  m_FocusedElement.SetToggled(!m_FocusedElement.IsToggled());
133  }
134 
135  //------------------------------------------------------------------------------------------------
136  protected SCR_ButtonBaseComponent GetFirstSelectedIndex()
137  {
138  foreach (int i, SCR_ButtonBaseComponent element : m_aSelectionElements)
139  {
140  if (element.IsToggled())
141  return element;
142  }
143 
144  return null;
145  }
146 
147  //------------------------------------------------------------------------------------------------
148  void OnElementClicked(SCR_ButtonBaseComponent comp)
149  {
150  GetGame().GetWorkspace().SetFocusedWidget(m_wRoot);
151  int i = m_aSelectionElements.Find(comp);
152 
153  if (m_bAllowMultiselection)
154  SetFocusedItem(comp);
155  else
156  {
157  if (i == m_iSelectedItem)
158  {
159  comp.SetToggled(!comp.IsToggled(), false, false);
160  m_iSelectedItem = i;
161  }
162  else
163  {
164  SetCurrentItem(i, true, false);
165  }
166  }
167  }
168 
169  //------------------------------------------------------------------------------------------------
170  void OnElementChanged(SCR_ButtonBaseComponent comp, bool state)
171  {
172  // Do not invoke on false state when not having a multiselection
173  if (!m_bAllowMultiselection && !state)
174  return;
175 
176  int i = m_aSelectionElements.Find(comp);
177  if (m_bAllowMultiselection)
178  m_OnChanged.Invoke(this, i, state);
179  else
180  m_OnChanged.Invoke(this, i);
181  }
182 
183  //------------------------------------------------------------------------------------------------
184  override bool SetCurrentItem(int i, bool playSound = false, bool animate = false)
185  {
186  int oldIndex = m_iSelectedItem;
187  int itemsCount = m_aSelectionElements.Count();
188  if (!super.SetCurrentItem(i, true, true))
189  return false;
190 
191  if (!m_bAllowMultiselection && oldIndex >= 0 && oldIndex < itemsCount)
192  {
193  m_aSelectionElements[oldIndex].SetToggled(false, playSound, animate);
194  }
195 
196  if (i >= 0 && i < itemsCount)
197  {
198  m_aSelectionElements[i].SetToggled(true, playSound, animate);
199  //m_OnChanged.Invoke(this, m_aSelectionElements[i].GetRootWidget(), i);
200  SetFocusedItem(m_aSelectionElements[i]);
201  }
202 
203  return true;
204  }
205 
206  //------------------------------------------------------------------------------------------------
207  override bool OnMouseEnter(Widget w, int x, int y)
208  {
209  super.OnMouseEnter(w, x, y);
210  if (IsChildWidget(w, WidgetManager.GetWidgetUnderCursor()))
211  super.OnMouseEnter(w, x, y);
212 
213  return false;
214  }
215 
216  //------------------------------------------------------------------------------------------------
217  protected void OnMenuLeft()
218  {
219  int i = GetNextValidItem(true);
220  if (i < 0)
221  return;
222 
223  if (m_bAllowMultiselection)
224  SetFocusedItem(i);
225  else
226  SetCurrentItem(i, true, true);
227  }
228 
229  //------------------------------------------------------------------------------------------------
230  protected void OnMenuRight()
231  {
232  int i = GetNextValidItem(false);
233  if (i < 0)
234  return;
235 
236  if (m_bAllowMultiselection)
237  SetFocusedItem(i);
238  else
239  SetCurrentItem(i, true, true);
240  }
241 
242  //------------------------------------------------------------------------------------------------
243  protected int GetNextValidItem(bool toLeft)
244  {
245  if (m_aSelectionElements.IsEmpty())
246  return -1;
247 
248  SCR_ButtonBaseComponent selectedElement;
249  int lastIndex = m_aSelectionElements.Count() - 1;
250 
251  int i = m_aSelectionElements.Find(m_FocusedElement);
252  int foundItem = -1;
253  int nextItem = 1;
254  if (toLeft)
255  nextItem = -1;
256 
257  while (foundItem < 0)
258  {
259  i += nextItem;
260  if (i < 0)
261  {
262  if (m_bCycleMode)
263  i = lastIndex;
264  else
265  return -1;
266 
267  }
268  else if (i > lastIndex)
269  {
270  if (m_bCycleMode)
271  i = 0;
272  else
273  return -1;
274  }
275 
276  if (m_FocusedElement == m_aSelectionElements[i])
277  return -1; // Went through all elements, found nothing
278 
279  if (m_aSelectionElements[i].IsEnabled())
280  foundItem = i;
281  }
282  return foundItem;
283  }
284 
285  //------------------------------------------------------------------------------------------------
286  int GetItems(notnull array<SCR_ButtonBaseComponent> elements)
287  {
288  elements.Clear();
289 
290  foreach (SCR_ButtonBaseComponent element: m_aSelectionElements)
291  elements.Insert(element);
292 
293  return elements.Count();
294  }
295 
296  SCR_ButtonBaseComponent GetItem(int index)
297  {
298  if (index < 0 || index >= m_aSelectionElements.Count())
299  return null;
300 
301  return m_aSelectionElements[index];
302  }
303 
304  //------------------------------------------------------------------------------------------------
305  override int AddItem(string item, bool last = false, Managed data = null)
306  {
307  int i = super.AddItem(item, last, data);
308  ClearWidgets();
309  CreateWidgets();
310  return i;
311  }
312 
313  //------------------------------------------------------------------------------------------------
314  override void ClearAll()
315  {
316  super.ClearAll();
317  ClearWidgets();
318  }
319 
320  //------------------------------------------------------------------------------------------------
321  override void RemoveItem(int item, bool last = false)
322  {
323  super.RemoveItem(item, last);
324  ClearWidgets();
325  CreateWidgets();
326  }
327 
328  //------------------------------------------------------------------------------------------------
329  bool IsItemSelected(int index)
330  {
331  if (index < 0 || index >= m_aSelectionElements.Count())
332  return false;
333 
334  return m_aSelectionElements[index].IsToggled();
335  }
336 
337  //------------------------------------------------------------------------------------------------
338  void SetItemSelected(int index, bool select, bool animate = true)
339  {
340  if (index < 0 || index >= m_aSelectionElements.Count())
341  return;
342 
343  m_iSelectedItem = index;
344  m_aSelectionElements[index].SetToggled(select, animate, animate);
345  }
346 
347  //------------------------------------------------------------------------------------------------
348  void SetFocusedItem(int index, bool animate = true)
349  {
350  if (index < 0 || index >= m_aSelectionElements.Count())
351  return;
352 
353  SCR_ButtonBaseComponent element = m_aSelectionElements[index];
354  if (element == m_FocusedElement)
355  return;
356 
357  bool focused = GetGame().GetWorkspace().GetFocusedWidget() == m_wRoot;
358 
359  if (m_FocusedElement && focused)
360  m_FocusedElement.ShowBorder(false, animate);
361 
362  m_FocusedElement = element;
363 
364  if (focused)
365  m_FocusedElement.ShowBorder(true, animate);
366  }
367 
368  //------------------------------------------------------------------------------------------------
369  void SetFocusedItem(SCR_ButtonBaseComponent element, bool animate = true)
370  {
371  if (!element || element == m_FocusedElement)
372  return;
373 
374  bool focused = GetGame().GetWorkspace().GetFocusedWidget() == m_wRoot;
375 
376  if (m_FocusedElement && focused)
377  m_FocusedElement.ShowBorder(false, animate);
378 
379  m_FocusedElement = element;
380 
381  if (focused)
382  m_FocusedElement.ShowBorder(true, animate);
383  }
384 
385  //------------------------------------------------------------------------------------------------
386  int GetFocusedItem()
387  {
388  return m_aSelectionElements.Find(m_FocusedElement);
389  }
390 
391  //------------------------------------------------------------------------------------------------
394  static SCR_ToolboxComponent GetToolboxComponent(string name, Widget parent, bool searchAllChildren = true)
395  {
396  auto comp = SCR_ToolboxComponent.Cast(
397  SCR_WLibComponentBase.GetComponent(SCR_ToolboxComponent, name, parent, searchAllChildren)
398  );
399  return comp;
400  }
401 };
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
SCR_WLibComponentBase
Base class for all final Reforger interactive elements.
Definition: SCR_WLibComponentBase.c:4
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_OnChanged
protected ref ScriptInvokerTabViewIndex m_OnChanged
Definition: SCR_TabViewComponent.c:64
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_ButtonTextComponent
Definition: SCR_ButtonTextComponent.c:2
IsEnabled
int IsEnabled()
Definition: SCR_BaseManualCameraComponent.c:99
SCR_ButtonBaseComponent
Base class for any button, regardless its own content.
Definition: SCR_ButtonBaseComponent.c:3
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
m_bCycleMode
bool m_bCycleMode
Definition: SCR_TabViewComponent.c:31
SCR_ToolboxComponent
Definition: SCR_ToolboxComponent.c:2
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305
SCR_SelectionWidgetComponent
Definition: SCR_SelectionWidgetComponent.c:1