Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_GalleryComponent.c
Go to the documentation of this file.
1 class SCR_GalleryComponent : ScriptedWidgetComponent
2 {
3  [Attribute("0")]
4  protected int m_iInitialItemCount;
5 
6  protected int m_iSelectedItem;
7 
8  [Attribute("3")]
9  protected int m_iCountShownItems;
10 
11  [Attribute("true", desc: "Should movement switch between whole pages, or just individual items?")]
12  protected bool m_bMovePerPage;
13 
14  [Attribute("10")]
15  protected float m_fSpacing;
16 
17  [Attribute("{02155A85F2DC521F}UI/layouts/Menus/PlayMenu/PlayMenuTile.layout", UIWidgets.ResourceNamePicker, "", "layout")]
18  protected ResourceName m_sItemLayout;
19 
20  [Attribute("MouseWheelUp")]
21  protected string m_sActionLeft;
22 
23  [Attribute("MouseWheelDown")]
24  protected string m_sActionRight;
25 
26  [Attribute("true")]
27  protected bool m_bShowPagingHints;
28 
29  [Attribute("false")]
30  protected bool m_bCycleMode;
31 
32  [Attribute("0.2")]
33  protected float m_fAnimationTime;
34 
35  [Attribute("true")]
36  protected bool m_bShowNavigationArrows;
37 
38  [Attribute("Hint")]
39  protected string m_sHintName;
40 
41  [Attribute("Content")]
42  protected string m_sContentName;
43 
44  protected ref array<Widget> m_aWidgets = {};
45  protected SCR_SelectionHintComponent m_Hint;
46  protected SCR_PagingButtonComponent m_PagingLeft;
47  protected SCR_PagingButtonComponent m_PagingRight;
48  protected Widget m_wContentRoot;
49  protected Widget m_wRoot;
50  protected float m_fAnimationRate = SCR_WLibComponentBase.START_ANIMATION_RATE;
51 
52  //------------------------------------------------------------------------------------------------
53  override void HandlerAttached(Widget w)
54  {
55  m_wContentRoot = w.FindAnyWidget(m_sContentName);
56  m_wRoot = w;
57 
58  // Convert time to animation speed
59  GetGame().GetCallqueue().CallLater(SetAnimationRate, SCR_WLibComponentBase.START_ANIMATION_PERIOD);
60 
61  if (m_iInitialItemCount > 0)
62  {
63  CreateWidgets(m_iInitialItemCount);
64  SetupHints(m_iInitialItemCount, false);
65  SetFocusedItem(m_iSelectedItem);
66  }
67 
68  // Setup controls
69  m_PagingLeft = SCR_PagingButtonComponent.GetPagingButtonComponent("PagingLeft", w);
70  if (m_PagingLeft)
71  {
72  m_PagingLeft.m_OnClicked.Insert(OnNavigationLeft);
73  m_PagingLeft.GetRootWidget().SetVisible(m_bShowNavigationArrows);
74  }
75 
76  m_PagingRight = SCR_PagingButtonComponent.GetPagingButtonComponent("PagingRight", w);
77  if (m_PagingRight)
78  {
79  m_PagingRight.m_OnClicked.Insert(OnNavigationRight);
80  m_PagingRight.GetRootWidget().SetVisible(m_bShowNavigationArrows);
81  }
82 
83  UpdatePagingButtons();
84 
85  GetGame().GetInputManager().AddActionListener("MenuLeft", EActionTrigger.DOWN, OnPreviousItem);
86  GetGame().GetInputManager().AddActionListener("MenuRight", EActionTrigger.DOWN, OnNextItem);
87  GetGame().GetInputManager().AddActionListener(m_sActionLeft, EActionTrigger.DOWN, OnCustomLeft);
88  GetGame().GetInputManager().AddActionListener(m_sActionRight, EActionTrigger.DOWN, OnCustomRight);
89 
90  GetGame().OnInputDeviceIsGamepadInvoker().Insert(OnInputDeviceIsGamepad);
91  }
92 
93  //------------------------------------------------------------------------------------------------
94  protected void OnInputDeviceIsGamepad(bool isGamepad)
95  {
96  ShowPagingButtons();
97  }
98 
99 
100  // TODO: This method should be used everywhere, but is bugged!!!
101  //------------------------------------------------------------------------------------------------
102  void ShowPagingButtons(bool animate = true)
103  {
104  bool show = GetGame().GetInputManager().IsUsingMouseAndKeyboard() && m_aWidgets.Count() > m_iCountShownItems;
105 
106  if (m_PagingLeft)
107  m_PagingLeft.SetVisible(show, animate);
108 
109  if (m_PagingRight)
110  m_PagingRight.SetVisible(show, animate);
111  }
112 
113  //------------------------------------------------------------------------------------------------
114  protected void UpdatePagingButtons()
115  {
116  if (!m_PagingLeft || !m_PagingRight)
117  return;
118 
119  ShowPagingButtons();
120 
121  // In cycle mode, if paging buttons are shown, they are always enabled
122  if (m_bCycleMode)
123  {
124  m_PagingLeft.SetEnabled(true);
125  m_PagingRight.SetEnabled(true);
126  return;
127  }
128 
129  int count = m_aWidgets.Count();
130  int currentItem = m_iSelectedItem;
131  if (m_bMovePerPage && m_iCountShownItems > 0)
132  {
133  count = Math.Ceil(count / m_iCountShownItems);
134  currentItem = Math.Floor(m_iSelectedItem / m_iCountShownItems);
135  }
136 
137  m_PagingLeft.SetEnabled(currentItem > 0);
138  m_PagingRight.SetEnabled(currentItem < count - 1);
139  }
140 
141  //------------------------------------------------------------------------------------------------
142  protected void SetupHints(int count, bool animate = true)
143  {
144  if (!m_Hint)
145  {
146  Widget hint = m_wRoot.FindAnyWidget(m_sHintName);
147  if (hint)
148  m_Hint = SCR_SelectionHintComponent.Cast(hint.FindHandler(SCR_SelectionHintComponent));
149  }
150 
151  if (!m_Hint)
152  return;
153 
154  int hintsCount = count;
155  int currentItem = m_iSelectedItem;
156  if (m_bMovePerPage && m_iCountShownItems > 0)
157  {
158  hintsCount = Math.Ceil(count / m_iCountShownItems);
159  currentItem = Math.Ceil(m_iSelectedItem / m_iCountShownItems);
160  }
161 
162  m_Hint.SetItemCount(hintsCount, animate);
163  m_Hint.SetCurrentItem(currentItem);
164  }
165 
166  //------------------------------------------------------------------------------------------------
167  protected void CreateWidgets(int count)
168  {
169  if (!m_wContentRoot)
170  return;
171 
172  // Delete old widgets
173  foreach (Widget w: m_aWidgets)
174  {
175  if (w)
176  w.RemoveFromHierarchy();
177  }
178  m_aWidgets.Clear();
179 
180  for (int i = 0; i < count; i++)
181  {
182  CreateWidget();
183  }
184 
185  m_iSelectedItem = 0;
186  ShowTiles(0);
187  }
188 
189  //------------------------------------------------------------------------------------------------
190  protected Widget CreateWidget()
191  {
192  if (!m_wContentRoot)
193  return null;
194 
195  Widget w = GetGame().GetWorkspace().CreateWidgets(m_sItemLayout, m_wContentRoot);
196  if (!w)
197  return null;
198 
199  SetupWidget(w);
200  return w;
201  }
202 
203  //------------------------------------------------------------------------------------------------
204  private void SetupWidget(Widget w)
205  {
206  // Listen on focus event
208  if (!comp)
209  {
210  comp = new SCR_EventHandlerComponent();
211  w.AddHandler(comp);
212  }
213  comp.GetOnFocus().Insert(OnItemFocused);
214 
215  // Check if root is actually a button
216  ButtonWidget button = ButtonWidget.Cast(w);
217  if (!button)
218  {
219  Print("[SCR_GalleryComponent.SetupWidget] the gallery element root has to be a button,"
220  + "otherwise interactivity will not work", LogLevel.WARNING);
221  }
222 
223  // Apply spacing
224  float l, t, r, b;
225  AlignableSlot.GetPadding(w, l, t, r, b);
226  AlignableSlot.SetPadding(w, m_fSpacing * 0.5, t, m_fSpacing * 0.5, b);
227  UpdatePagingButtons();
228 
229  m_aWidgets.Insert(w);
230  }
231 
232  //------------------------------------------------------------------------------------------------
233  protected void SetAnimationRate()
234  {
235  if (m_fAnimationTime <= 0)
236  m_fAnimationRate = 1000;
237  else
238  m_fAnimationRate = 1 / m_fAnimationTime;
239  }
240 
241  //------------------------------------------------------------------------------------------------
242  void OnItemFocused(Widget w)
243  {
244  m_iSelectedItem = m_aWidgets.Find(w);
245 
246 
247  int index = m_iSelectedItem;
248  if (m_bMovePerPage && m_iCountShownItems > 0)
249  index = Math.Floor(m_iSelectedItem / m_iCountShownItems);
250 
251  m_Hint.SetCurrentItem(index);
252  UpdatePagingButtons();
253  }
254 
255  //------------------------------------------------------------------------------------------------
256  void OnCustomLeft()
257  {
258  if (!m_wRoot.IsEnabledInHierarchy() || !m_wRoot.IsVisibleInHierarchy())
259  return;
260 
261  int selectedItem;
262  if (m_bMovePerPage)
263  selectedItem = Math.Max(m_iSelectedItem - m_iCountShownItems, 0);
264  else
265  selectedItem = m_iSelectedItem - 1;
266 
267  SetFocusedItem(selectedItem);
268  }
269 
270  //------------------------------------------------------------------------------------------------
271  void OnCustomRight()
272  {
273  if (!m_wRoot.IsEnabledInHierarchy() || !m_wRoot.IsVisibleInHierarchy())
274  return;
275 
276  int selectedItem;
277  if (m_bMovePerPage)
278  selectedItem = Math.Min(m_iSelectedItem + m_iCountShownItems, m_aWidgets.Count() - 1);
279  else
280  selectedItem = m_iSelectedItem + 1;
281 
282  SetFocusedItem(selectedItem);
283  }
284 
285  //------------------------------------------------------------------------------------------------
286  void OnNavigationLeft()
287  {
288  if (!m_wRoot.IsEnabledInHierarchy() || !m_wRoot.IsVisibleInHierarchy())
289  return;
290 
291  SetFocusedItem(m_iSelectedItem - 1);
292  }
293 
294  //------------------------------------------------------------------------------------------------
295  void OnNavigationRight()
296  {
297  if (!m_wRoot.IsEnabledInHierarchy() || !m_wRoot.IsVisibleInHierarchy())
298  return;
299 
300  SetFocusedItem(m_iSelectedItem + 1);
301  }
302 
303  //------------------------------------------------------------------------------------------------
304  protected void OnNextItem()
305  {
306  if (!m_wRoot.IsEnabledInHierarchy() || !m_wRoot.IsVisibleInHierarchy())
307  return;
308 
309  int nextItem = m_iSelectedItem + 1;
310  if (nextItem >= m_aWidgets.Count())
311  return;
312 
313  if (!m_aWidgets[nextItem].IsEnabled())
314  SetFocusedItem(nextItem);
315  }
316 
317  //------------------------------------------------------------------------------------------------
318  protected void OnPreviousItem()
319  {
320  if (!m_wRoot.IsEnabledInHierarchy() || !m_wRoot.IsVisibleInHierarchy())
321  return;
322 
323  int previousItem = m_iSelectedItem - 1;
324  if (previousItem < 0)
325  return;
326 
327  if (!m_aWidgets[previousItem].IsEnabled())
328  SetFocusedItem(previousItem);
329  }
330 
331  //------------------------------------------------------------------------------------------------
332  void SetFocusedItem(int index, bool force = false)
333  {
334  index = Math.ClampInt(index, 0, m_aWidgets.Count() - 1);
335  if ((!force && m_iSelectedItem == index) || index < 0)
336  return;
337 
338  bool moveRight;
339  if (m_iSelectedItem < index)
340  moveRight = true;
341 
342  m_iSelectedItem = index;
343  //if (m_Hint)
344  //m_Hint.SetCurrentItem(m_iSelectedItem);
345 
346  if (!m_aWidgets[m_iSelectedItem].IsEnabled())
347  {
348  int firstShownTile = m_iSelectedItem;
349  if (m_bMovePerPage && m_iCountShownItems > 0)
350  {
351  firstShownTile = Math.Floor(firstShownTile / m_iCountShownItems) * m_iCountShownItems;
352  }
353  else
354  {
355  if (moveRight)
356  firstShownTile = m_iSelectedItem - m_iCountShownItems + 1;
357  }
358 
359  ShowTiles(firstShownTile);
360  }
361 
362  GetGame().GetWorkspace().SetFocusedWidget(m_aWidgets[m_iSelectedItem]);
363  }
364 
365  //------------------------------------------------------------------------------------------------
366  protected void ShowTiles(int startingIndex, bool animate = true)
367  {
368  foreach (int i, Widget w : m_aWidgets)
369  {
370  bool setVisible = i >= startingIndex && i < (startingIndex + m_iCountShownItems);
371  w.SetEnabled(setVisible);
372  w.SetVisible(setVisible);
373  }
374 
375  // TODO: Implement any animation for the widget gallery
376  }
377 
378  /*
379  //------------------------------------------------------------------------------------------------
380  protected void ExpandWidget(Widget widget, bool expand, bool animate = true)
381  {
382  if (!widget)
383  return;
384 
385  if (animate && m_fAnimationRate < 1000)
386  {
387  LayoutSlot.SetFillWeight(widget, !expand);
388  AnimateWidget.LayoutFill(widget, expand, m_fAnimationRate);
389  }
390  else
391  {
392  LayoutSlot.SetFillWeight(widget, !expand);
393  }
394  }
395  */
396 
397  // Public API
398  //------------------------------------------------------------------------------------------------
399  int GetWidgets(out array<Widget> widgets)
400  {
401  if (widgets)
402  widgets = m_aWidgets;
403 
404  return m_aWidgets.Count();
405  }
406 
407  //------------------------------------------------------------------------------------------------
408  Widget AddItem()
409  {
410  Widget w = CreateWidget();
411  ShowTiles(m_iSelectedItem);
412  SetupHints(m_aWidgets.Count());
413  return w;
414  }
415 
416  //------------------------------------------------------------------------------------------------
417  int AddItem(Widget widget)
418  {
419  if (!widget)
420  return -1;
421 
422  m_wContentRoot.AddChild(widget);
423  SetupWidget(widget);
424  ShowTiles(m_iSelectedItem);
425 
426  int count = m_aWidgets.Count();
427  SetupHints(count);
428  return count;
429  }
430 
431  //------------------------------------------------------------------------------------------------
432  void ClearAll()
433  {
434  CreateWidgets(0);
435  SetupHints(0);
436  }
437 
438  //------------------------------------------------------------------------------------------------
439  void RemoveItem(int item)
440  {
441  if (item < 0 || item >= m_aWidgets.Count())
442  return;
443 
444  Widget w = m_aWidgets[item];
445  w.RemoveFromHierarchy();
446  m_aWidgets.Remove(item);
447 
448  SetupHints(m_aWidgets.Count());
449  }
450 
451  //------------------------------------------------------------------------------------------------
452  void SetCurrentItem(int i, bool animate = false)
453  {
454  SetFocusedItem(i);
455  }
456 
457  //------------------------------------------------------------------------------------------------
460  static SCR_GalleryComponent GetGalleryComponent(string name, Widget parent, bool searchAllChildren = true)
461  {
462  if (!parent || name == string.Empty)
463  return null;
464 
465  Widget w;
466  if (searchAllChildren)
467  w = parent.FindAnyWidget(name);
468  else
469  w = parent.FindWidget(name);
470 
471  if (!w)
472  return null;
473 
475  return comp;
476  }
477 };
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
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
SCR_PagingButtonComponent
Definition: SCR_PagingButtonComponent.c:2
Attribute
typedef Attribute
Post-process effect of scripted camera.
m_PagingRight
protected SCR_PagingButtonComponent m_PagingRight
Definition: SCR_TabViewComponent.c:61
SCR_EventHandlerComponent
Definition: SCR_EventHandlerComponent.c:5
IsEnabled
int IsEnabled()
Definition: SCR_BaseManualCameraComponent.c:99
m_sActionLeft
string m_sActionLeft
Definition: SCR_TabViewComponent.c:40
m_aWidgets
ref array< Widget > m_aWidgets
Definition: SCR_UITaskManagerComponent.c:25
m_PagingLeft
protected SCR_PagingButtonComponent m_PagingLeft
Definition: SCR_TabViewComponent.c:60
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
SCR_SelectionHintComponent
Definition: SCR_SelectionHintComponent.c:2
m_bCycleMode
bool m_bCycleMode
Definition: SCR_TabViewComponent.c:31
m_sActionRight
string m_sActionRight
Definition: SCR_TabViewComponent.c:43