Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_SpinBoxComponent.c
Go to the documentation of this file.
2 {
3  protected static const ref Color COLOR_HINT_SELECTED = UIColors.CONTRAST_COLOR;
4  protected static const ref Color COLOR_HINT_DESELECTED = UIColors.WHITE_HOVERED;
5 
6  protected SCR_PagingButtonComponent m_ButtonLeft;
7  protected SCR_PagingButtonComponent m_ButtonRight;
8  protected TextWidget m_wText;
9  protected Widget m_wContent;
10  protected Widget m_wCountBar;
11  protected ref array<Widget> m_aHintElements = new array<Widget>();
12 
13  [Attribute("false", UIWidgets.CheckBox, "use light grey arrows instead of big yellow ones")]
14  protected bool m_bUseLightArrows;
15 
16  [Attribute("false", UIWidgets.CheckBox, "On last item and pressing right arrow, it will go to the start of the list")]
17  protected bool m_bCycleMode;
18 
19  [Attribute("true", UIWidgets.CheckBox, "Show bar of available elements and which one is selected.")]
20  protected bool m_bShowHints;
21 
22  [Attribute("24", UIWidgets.EditBox, "Width of a hint element")]
23  protected float m_fHintElementWidth;
24 
25  [Attribute("4", UIWidgets.EditBox, "Height of a hint element")]
26  protected float m_fHintElementHeight;
27 
28  [Attribute("1", UIWidgets.EditBox, "How much wider should be the selected hint element")]
29  protected float m_fHintSelectedWidthMultiplier;
30 
31  [Attribute("true", UIWidgets.CheckBox, "Should hints fill the available space")]
32  protected bool m_fHintFillMode;
33 
34  [Attribute("4", UIWidgets.EditBox, "How large gaps between hints should there be")]
35  protected float m_fHintSpacing;
36 
37  [Attribute("", UIWidgets.EditBox, "")]
38  protected ResourceName m_sHintElementTexture;
39 
40  [Attribute("", UIWidgets.EditBox, "")]
41  protected ResourceName m_sHintElementImage;
42 
43  [Attribute("HintBarElement", UIWidgets.EditBox, "Name for generated Hint bar widgets")]
44  protected string m_sHintBarElementName;
45 
46  protected ref ScriptInvoker m_OnLeftArrowClick;
47  protected ref ScriptInvoker m_OnRightArrowClick;
48 
49  protected bool m_bHasActionListeners;
50  protected bool m_bAllowSwitchingWithoutFocus;
51 
52  //------------------------------------------------------------------------------------------------
53  override void HandlerAttached(Widget w)
54  {
55  super.HandlerAttached(w);
56  m_wText = TextWidget.Cast(w.FindAnyWidget("SelectionText"));
57  m_wContent = w.FindAnyWidget("Overlay");
58  m_wCountBar = w.FindAnyWidget("HintBar");
59 
60  // Buttons
61  Widget left = w.FindAnyWidget("ButtonLeft");
62  Widget right = w.FindAnyWidget("ButtonRight");
63 
64  if (left)
65  {
66  m_ButtonLeft = SCR_PagingButtonComponent.Cast(left.FindHandler(SCR_PagingButtonComponent));
67  if (m_ButtonLeft)
68  m_ButtonLeft.m_OnActivated.Insert(OnLeftArrowClick);
69  }
70 
71  if (right)
72  {
73  m_ButtonRight = SCR_PagingButtonComponent.Cast(right.FindHandler(SCR_PagingButtonComponent));
74  if (m_ButtonRight)
75  m_ButtonRight.m_OnActivated.Insert(OnRightArrowClick);
76  }
77 
78  if (m_wText)
79  {
80  if (m_aElementNames && m_iSelectedItem > -1 && m_iSelectedItem < m_aElementNames.Count())
81  m_wText.SetText(m_aElementNames[m_iSelectedItem]);
82  else
83  m_wText.SetText(string.Empty);
84  }
85 
86  SetInitialState(false);
87  }
88 
89  //------------------------------------------------------------------------------------------------
90  override bool OnFocus(Widget w, int x, int y)
91  {
92  super.OnFocus(w, x, y);
93 
94  AddActionListeners();
95 
96  UpdateHintBar(m_iSelectedItem, -1);
97  return false;
98  }
99 
100  //------------------------------------------------------------------------------------------------
101  override bool OnFocusLost(Widget w, int x, int y)
102  {
103  super.OnFocusLost(w, x, y);
104 
105  if (!m_bAllowSwitchingWithoutFocus)
106  RemoveActionListeners();
107 
108  UpdateHintBar(m_iSelectedItem, -1);
109  return false;
110  }
111 
112  //TODO: revise this logic: there should be no reason to reset current item every time a new one is added or removed
113  //------------------------------------------------------------------------------------------------
114  override int AddItem(string item, bool last = false, Managed data = null)
115  {
116  int i = super.AddItem(item, last, data);
117 
118  SetInitialState(last);
119  return i;
120  }
121 
122  //------------------------------------------------------------------------------------------------
123  override void RemoveItem(int item, bool last = false)
124  {
125  super.RemoveItem(item, last);
126  SetInitialState(last);
127  }
128 
129  //------------------------------------------------------------------------------------------------
130  override void ClearAll()
131  {
132  super.ClearAll();
133  SetInitialState(true);
134  }
135 
136  //------------------------------------------------------------------------------------------------
137  override bool SetCurrentItem(int i, bool playSound = false, bool animate = false)
138  {
139  return SetCurrentItem_Internal(i, playSound, animate, true);
140  }
141 
142  // --- Protected ---
143  //------------------------------------------------------------------------------------------------
144  protected bool SetCurrentItem_Internal(int i, bool playSound, bool animate, bool invokeOnChanged)
145  {
146  int lastIndex = m_iSelectedItem;
147  if (!super.SetCurrentItem(i, playSound, animate))
148  return false;
149 
150  if (m_wText)
151  m_wText.SetText(m_aElementNames[i]);
152 
153  if (m_bShowHints)
154  UpdateHintBar(i, lastIndex);
155 
156  EnableArrows(i, animate);
157 
158  if (invokeOnChanged)
159  m_OnChanged.Invoke(this, m_iSelectedItem);
160 
161  return true;
162  }
163 
164  //------------------------------------------------------------------------------------------------
165  protected void CreateHintBar()
166  {
167  // Delete any old elements
168  foreach (Widget w : m_aHintElements)
169  {
170  w.RemoveFromHierarchy();
171  }
172  m_aHintElements.Clear();
173 
174  m_wCountBar.SetVisible(true);
175 
176  for (int i = 0, len = m_aElementNames.Count(); i < len; i++)
177  {
178  Widget w = GetGame().GetWorkspace().CreateWidget(WidgetType.ImageWidgetTypeID, WidgetFlags.VISIBLE | WidgetFlags.STRETCH | WidgetFlags.BLEND | WidgetFlags.INHERIT_CLIPPING, Color.FromInt(Color.WHITE), 0, m_wCountBar);
179  ImageWidget img = ImageWidget.Cast(w);
180  if (!img)
181  break;
182 
183  SetTexture(img, m_sHintElementTexture, m_sHintElementImage);
184  // Force size only when sizes are more than 0 - automatic sizing
185  if (m_fHintElementWidth >= 0 && m_fHintElementHeight >= 0)
186  img.SetSize(m_fHintElementWidth, m_fHintElementHeight);
187 
188  img.SetColor(COLOR_HINT_DESELECTED);
189  HorizontalLayoutSlot.SetPadding(img, m_fHintSpacing * 0.5, 0, m_fHintSpacing * 0.5, 0);
190  HorizontalLayoutSlot.SetHorizontalAlign(img, LayoutHorizontalAlign.Stretch);
191 
192  // Use fill instead
193  if (m_fHintFillMode)
194  HorizontalLayoutSlot.SetSizeMode(img, LayoutSizeMode.Fill);
195 
196  m_aHintElements.Insert(img);
197  img.SetName(m_sHintBarElementName + i);
198  }
199 
200  UpdateHintBar(m_iSelectedItem, -1);
201  }
202 
203  //------------------------------------------------------------------------------------------------
204  protected void UpdateHintBar(int currentIndex, int oldIndex)
205  {
206  if (!m_bShowHints)
207  return;
208 
209  bool focused = GetGame().GetWorkspace().GetFocusedWidget() == m_wRoot;
210 
211  int count = m_aHintElements.Count();
212  if (oldIndex > -1 && oldIndex < count)
213  {
214  AnimateWidget.Color(m_aHintElements[oldIndex], COLOR_HINT_DESELECTED, m_fAnimationRate);
215  if (m_fHintSelectedWidthMultiplier != 1)
216  AnimateWidget.LayoutFill(m_aHintElements[oldIndex], 1, m_fAnimationRate);
217  }
218 
219  if (currentIndex > -1 && currentIndex < count)
220  {
221  AnimateWidget.Color(m_aHintElements[currentIndex], COLOR_HINT_SELECTED, m_fAnimationRate);
222  if (m_fHintSelectedWidthMultiplier != 1)
223  AnimateWidget.LayoutFill(m_aHintElements[currentIndex], m_fHintSelectedWidthMultiplier, m_fAnimationRate);
224  }
225  }
226 
227  //------------------------------------------------------------------------------------------------
228  protected void SetInitialState(bool invokeOnChanged = true)
229  {
230  int realIndex = m_iSelectedItem;
231  if (realIndex < 0 || realIndex >= m_aElementNames.Count())
232  realIndex = 0;
233 
234  m_iSelectedItem = int.MIN;
235  SetCurrentItem_Internal(realIndex, false, false, invokeOnChanged);
236 
237  if (m_bShowHints && m_aElementNames && m_wCountBar)
238  CreateHintBar();
239  }
240 
241  //------------------------------------------------------------------------------------------------
243  protected void EnableArrows(int selected, bool animate)
244  {
245  if (!m_ButtonLeft || !m_ButtonRight)
246  return;
247 
248  if (m_bCycleMode)
249  {
250  bool enabled = m_aElementNames.Count() > 1;
251  m_ButtonLeft.SetEnabled(enabled, animate);
252  m_ButtonRight.SetEnabled(enabled, animate);
253  }
254  else
255  {
256  m_ButtonLeft.SetEnabled(selected != 0, animate);
257  m_ButtonRight.SetEnabled(selected != (m_aElementNames.Count() - 1), animate);
258  }
259  }
260 
261  //------------------------------------------------------------------------------------------------
262  protected void OnLeftArrowClick()
263  {
264  GetGame().GetWorkspace().SetFocusedWidget(m_wRoot);
265 
266  if (m_iSelectedItem <= 0)
267  {
268  if (m_bCycleMode)
269  SetCurrentItem(m_aElementNames.Count() - 1, true, true);
270  }
271  else
272  {
273  SetCurrentItem(m_iSelectedItem - 1, true, true);
274  }
275 
276  if (m_OnLeftArrowClick)
277  m_OnLeftArrowClick.Invoke();
278  }
279 
280  //------------------------------------------------------------------------------------------------
281  protected void OnRightArrowClick()
282  {
283  GetGame().GetWorkspace().SetFocusedWidget(m_wRoot);
284 
285  if (m_iSelectedItem >= (m_aElementNames.Count() - 1))
286  {
287  if (m_bCycleMode)
288  SetCurrentItem(0, true, true);
289  }
290  else
291  {
292  SetCurrentItem(m_iSelectedItem + 1, true, true);
293  }
294 
295  if (m_OnRightArrowClick)
296  m_OnRightArrowClick.Invoke();
297  }
298 
299  //------------------------------------------------------------------------------------------------
300  protected void OnMenuLeft()
301  {
302  if (GetGame().GetWorkspace().GetFocusedWidget() != m_wRoot && !m_bAllowSwitchingWithoutFocus)
303  return;
304 
305  if (m_ButtonLeft && m_ButtonLeft.IsEnabled())
306  m_ButtonLeft.OnClick(m_ButtonLeft.m_wRoot, 0, 0, 0); // TODO: Replace with other function, which accepts more params (turn of anims and sounds separately)
307  }
308 
309  //------------------------------------------------------------------------------------------------
310  protected void OnMenuRight()
311  {
312  if (GetGame().GetWorkspace().GetFocusedWidget() != m_wRoot && !m_bAllowSwitchingWithoutFocus)
313  return;
314 
315  if (m_ButtonRight && m_ButtonRight.IsEnabled())
316  m_ButtonRight.OnClick(m_ButtonRight.m_wRoot, 0, 0, 0); // TODO: Replace with other function, which accepts more params (turn of anims and sounds separately)
317  }
318 
319  // --- Public ---
320  //------------------------------------------------------------------------------------------------
321  void AddActionListeners()
322  {
324  return;
325 
326  GetGame().GetInputManager().AddActionListener("MenuLeft", EActionTrigger.DOWN, OnMenuLeft);
327  GetGame().GetInputManager().AddActionListener("MenuRight", EActionTrigger.DOWN, OnMenuRight);
328 
329  m_bHasActionListeners = true;
330  }
331 
332  //------------------------------------------------------------------------------------------------
333  void RemoveActionListeners()
334  {
336  return;
337 
338  GetGame().GetInputManager().RemoveActionListener("MenuLeft", EActionTrigger.DOWN, OnMenuLeft);
339  GetGame().GetInputManager().RemoveActionListener("MenuRight", EActionTrigger.DOWN, OnMenuRight);
340 
341  m_bHasActionListeners = false;
342  }
343 
344  //------------------------------------------------------------------------------------------------
345  // Determines if left/right page switch action listeners should be removed on focus lost
346  void SetKeepActionListeners(bool keep)
347  {
348  m_bAllowSwitchingWithoutFocus = keep;
349  }
350 
351  //------------------------------------------------------------------------------------------------
352  void SetCycleMode(bool cycle)
353  {
354  m_bCycleMode = cycle;
355  EnableArrows(m_iSelectedItem, false);
356  }
357 
358  //------------------------------------------------------------------------------------------------
359  ScriptInvoker GetOnLeftArrowClick()
360  {
361  if (!m_OnLeftArrowClick)
362  m_OnLeftArrowClick = new ScriptInvoker();
363 
364  return m_OnLeftArrowClick;
365  }
366 
367  //------------------------------------------------------------------------------------------------
368  ScriptInvoker GetOnRightArrowClick()
369  {
370  if (!m_OnRightArrowClick)
371  m_OnRightArrowClick = new ScriptInvoker();
372 
373  return m_OnRightArrowClick;
374  }
375 
376  //------------------------------------------------------------------------------------------------
379  static SCR_SpinBoxComponent GetSpinBoxComponent(string name, Widget parent, bool searchAllChildren = true)
380  {
381  return SCR_SpinBoxComponent.Cast(SCR_ScriptedWidgetComponent.GetComponent(SCR_SpinBoxComponent, name, parent, searchAllChildren));
382  }
383 }
m_bHasActionListeners
protected bool m_bHasActionListeners
Definition: SCR_MenuActionsComponent.c:13
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_OnChanged
protected ref ScriptInvokerTabViewIndex m_OnChanged
Definition: SCR_TabViewComponent.c:64
SCR_PagingButtonComponent
Definition: SCR_PagingButtonComponent.c:2
Attribute
typedef Attribute
Post-process effect of scripted camera.
m_wContent
protected Widget m_wContent
Definition: SCR_InfoDisplay.c:64
UIColors
Definition: Constants.c:16
SCR_SpinBoxComponent
Definition: SCR_SpinBoxComponent.c:1
m_bCycleMode
bool m_bCycleMode
Definition: SCR_TabViewComponent.c:31
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305
SCR_SelectionWidgetComponent
Definition: SCR_SelectionWidgetComponent.c:1
SCR_ScriptedWidgetComponent
Definition: SCR_ScriptedWidgetComponent.c:7