4 protected int m_iInitialItemCount;
6 protected int m_iSelectedItem;
9 protected int m_iCountShownItems;
11 [
Attribute(
"true",
desc:
"Should movement switch between whole pages, or just individual items?")]
12 protected bool m_bMovePerPage;
15 protected float m_fSpacing;
17 [
Attribute(
"{02155A85F2DC521F}UI/layouts/Menus/PlayMenu/PlayMenuTile.layout", UIWidgets.ResourceNamePicker,
"",
"layout")]
18 protected ResourceName m_sItemLayout;
21 protected string m_sActionLeft;
24 protected string m_sActionRight;
27 protected bool m_bShowPagingHints;
30 protected bool m_bCycleMode;
33 protected float m_fAnimationTime;
36 protected bool m_bShowNavigationArrows;
39 protected string m_sHintName;
42 protected string m_sContentName;
44 protected ref array<Widget> m_aWidgets = {};
48 protected Widget m_wContentRoot;
49 protected Widget m_wRoot;
53 override void HandlerAttached(Widget w)
55 m_wContentRoot = w.FindAnyWidget(m_sContentName);
61 if (m_iInitialItemCount > 0)
63 CreateWidgets(m_iInitialItemCount);
64 SetupHints(m_iInitialItemCount,
false);
65 SetFocusedItem(m_iSelectedItem);
73 m_PagingLeft.GetRootWidget().SetVisible(m_bShowNavigationArrows);
80 m_PagingRight.GetRootWidget().SetVisible(m_bShowNavigationArrows);
83 UpdatePagingButtons();
85 GetGame().GetInputManager().AddActionListener(
"MenuLeft", EActionTrigger.DOWN, OnPreviousItem);
86 GetGame().GetInputManager().AddActionListener(
"MenuRight", EActionTrigger.DOWN, OnNextItem);
90 GetGame().OnInputDeviceIsGamepadInvoker().Insert(OnInputDeviceIsGamepad);
94 protected void OnInputDeviceIsGamepad(
bool isGamepad)
102 void ShowPagingButtons(
bool animate =
true)
104 bool show =
GetGame().GetInputManager().IsUsingMouseAndKeyboard() &&
m_aWidgets.Count() > m_iCountShownItems;
114 protected void UpdatePagingButtons()
130 int currentItem = m_iSelectedItem;
131 if (m_bMovePerPage && m_iCountShownItems > 0)
133 count = Math.Ceil(count / m_iCountShownItems);
134 currentItem = Math.Floor(m_iSelectedItem / m_iCountShownItems);
142 protected void SetupHints(
int count,
bool animate =
true)
146 Widget hint =
m_wRoot.FindAnyWidget(m_sHintName);
154 int hintsCount = count;
155 int currentItem = m_iSelectedItem;
156 if (m_bMovePerPage && m_iCountShownItems > 0)
158 hintsCount = Math.Ceil(count / m_iCountShownItems);
159 currentItem = Math.Ceil(m_iSelectedItem / m_iCountShownItems);
162 m_Hint.SetItemCount(hintsCount, animate);
163 m_Hint.SetCurrentItem(currentItem);
167 protected void CreateWidgets(
int count)
176 w.RemoveFromHierarchy();
180 for (
int i = 0; i < count; i++)
190 protected Widget CreateWidget()
195 Widget w =
GetGame().GetWorkspace().CreateWidgets(m_sItemLayout, m_wContentRoot);
204 private void SetupWidget(Widget w)
213 comp.GetOnFocus().Insert(OnItemFocused);
216 ButtonWidget button = ButtonWidget.Cast(w);
219 Print(
"[SCR_GalleryComponent.SetupWidget] the gallery element root has to be a button,"
220 +
"otherwise interactivity will not work", LogLevel.WARNING);
225 AlignableSlot.GetPadding(w, l, t, r, b);
226 AlignableSlot.SetPadding(w, m_fSpacing * 0.5, t, m_fSpacing * 0.5, b);
227 UpdatePagingButtons();
233 protected void SetAnimationRate()
235 if (m_fAnimationTime <= 0)
236 m_fAnimationRate = 1000;
238 m_fAnimationRate = 1 / m_fAnimationTime;
242 void OnItemFocused(Widget w)
247 int index = m_iSelectedItem;
248 if (m_bMovePerPage && m_iCountShownItems > 0)
249 index = Math.Floor(m_iSelectedItem / m_iCountShownItems);
251 m_Hint.SetCurrentItem(
index);
252 UpdatePagingButtons();
258 if (!
m_wRoot.IsEnabledInHierarchy() || !
m_wRoot.IsVisibleInHierarchy())
263 selectedItem = Math.Max(m_iSelectedItem - m_iCountShownItems, 0);
265 selectedItem = m_iSelectedItem - 1;
267 SetFocusedItem(selectedItem);
273 if (!
m_wRoot.IsEnabledInHierarchy() || !
m_wRoot.IsVisibleInHierarchy())
278 selectedItem = Math.Min(m_iSelectedItem + m_iCountShownItems,
m_aWidgets.Count() - 1);
280 selectedItem = m_iSelectedItem + 1;
282 SetFocusedItem(selectedItem);
286 void OnNavigationLeft()
288 if (!
m_wRoot.IsEnabledInHierarchy() || !
m_wRoot.IsVisibleInHierarchy())
291 SetFocusedItem(m_iSelectedItem - 1);
295 void OnNavigationRight()
297 if (!
m_wRoot.IsEnabledInHierarchy() || !
m_wRoot.IsVisibleInHierarchy())
300 SetFocusedItem(m_iSelectedItem + 1);
304 protected void OnNextItem()
306 if (!
m_wRoot.IsEnabledInHierarchy() || !
m_wRoot.IsVisibleInHierarchy())
309 int nextItem = m_iSelectedItem + 1;
314 SetFocusedItem(nextItem);
318 protected void OnPreviousItem()
320 if (!
m_wRoot.IsEnabledInHierarchy() || !
m_wRoot.IsVisibleInHierarchy())
323 int previousItem = m_iSelectedItem - 1;
324 if (previousItem < 0)
328 SetFocusedItem(previousItem);
332 void SetFocusedItem(
int index,
bool force =
false)
335 if ((!force && m_iSelectedItem ==
index) ||
index < 0)
339 if (m_iSelectedItem <
index)
342 m_iSelectedItem =
index;
348 int firstShownTile = m_iSelectedItem;
349 if (m_bMovePerPage && m_iCountShownItems > 0)
351 firstShownTile = Math.Floor(firstShownTile / m_iCountShownItems) * m_iCountShownItems;
356 firstShownTile = m_iSelectedItem - m_iCountShownItems + 1;
359 ShowTiles(firstShownTile);
366 protected void ShowTiles(
int startingIndex,
bool animate =
true)
370 bool setVisible = i >= startingIndex && i < (startingIndex + m_iCountShownItems);
371 w.SetEnabled(setVisible);
372 w.SetVisible(setVisible);
399 int GetWidgets(out array<Widget> widgets)
410 Widget w = CreateWidget();
411 ShowTiles(m_iSelectedItem);
417 int AddItem(Widget widget)
422 m_wContentRoot.AddChild(widget);
424 ShowTiles(m_iSelectedItem);
439 void RemoveItem(
int item)
445 w.RemoveFromHierarchy();
452 void SetCurrentItem(
int i,
bool animate =
false)
460 static SCR_GalleryComponent GetGalleryComponent(
string name, Widget parent,
bool searchAllChildren =
true)
462 if (!parent || name ==
string.Empty)
466 if (searchAllChildren)
467 w = parent.FindAnyWidget(name);
469 w = parent.FindWidget(name);