Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ContentBrowserDetails_GalleryComponent.c
Go to the documentation of this file.
1 /*
2 Component of a gallery in addon details menu.
3 */
4 
5 class SCR_ContentBrowserDetails_GalleryComponent : ScriptedWidgetComponent
6 {
7  [Attribute("", UIWidgets.Auto, "Names of tile widgets")]
8  protected ref array<string> m_aTileWidgetNames;
9 
10  [Attribute("", UIWidgets.Auto, "See LoadImageTexture documentation")]
11  protected bool m_bFromLocalStorage;
12 
13  // Array with image preview widgets
14  protected ref array<Widget> m_aTiles = {};
15  protected ref array<SCR_BackendImageComponent> m_aTileComponents = {};
16  protected ref array<ref BackendImage> m_aImages = {};
17 
19 
20  protected int m_iCurrentItem = 0;
21 
22 
23  // -------------------- Public ----------------------
24 
25  //------------------------------------------------------------------------------------------
26  void SetImages(array<BackendImage> images)
27  {
28  m_aImages.Clear();
29 
30  int total = images.Count();
31  foreach (int i, BackendImage img : images)
32  {
33  m_aImages.Insert(img);
34  m_Widgets.m_SpinBoxComponent.AddItem(string.Empty, i == total - 1);
35  }
36 
37  UpdateAllWidgets();
38  }
39 
40 
41  // ----------- Protected -----------
42 
43  protected void Update()
44  {
45  // Update current highlighted dot
46  Widget focusedWidget = GetGame().GetWorkspace().GetFocusedWidget();
47 
48  int tileId = m_aTiles.Find(focusedWidget);
49  if (tileId != -1)
50  {
51  int spinBoxItemId = m_iCurrentItem + tileId;
52  if (m_Widgets.m_SpinBoxComponent.GetCurrentIndex() != spinBoxItemId)
53  m_Widgets.m_SpinBoxComponent.SetCurrentItem(spinBoxItemId);
54  }
55  else
56  {
57  int currentItem = m_Widgets.m_SpinBoxComponent.GetCurrentIndex();
58  int spinBoxItemId = Math.ClampInt(currentItem, m_iCurrentItem, m_iCurrentItem + m_aTiles.Count() - 1);
59  if (m_Widgets.m_SpinBoxComponent.GetCurrentIndex() != spinBoxItemId)
60  m_Widgets.m_SpinBoxComponent.SetCurrentItem(spinBoxItemId);
61  }
62 
63  GetGame().GetCallqueue().CallLater(Update, 10);
64  }
65 
66  protected void UpdateAllWidgets()
67  {
68  UpdateTiles();
69 
70  m_Widgets.m_NextButtonComponent.SetEnabled(m_iCurrentItem < (m_aImages.Count() - m_aTiles.Count()));
71  m_Widgets.m_PrevButtonComponent.SetEnabled(m_iCurrentItem > 0);
72  }
73 
74  //------------------------------------------------------------------------------------------
76  protected void UpdateTiles()
77  {
78  int nTiles = m_aTiles.Count();
79  int nImages = m_aImages.Count();
80 
81  for (int iTile = 0; iTile < nTiles; iTile++)
82  {
83  int iImage = m_iCurrentItem + iTile;
84 
85  if (iImage < nImages && iImage >= 0)
86  ShowImageOnTile(iTile, m_aImages[iImage]);
87  else
88  ShowImageOnTile(iTile, null);
89  }
90  }
91 
92 
93  //------------------------------------------------------------------------------------------
97  protected void ShowImageOnTile(int id, BackendImage backendImage)
98  {
99  SCR_BackendImageComponent comp = m_aTileComponents[id];
100 
101  if (!comp)
102  return;
103 
104 
105  Widget tile = m_aTiles[id];
106  ImageWidget wImage = ImageWidget.Cast(tile.FindAnyWidget("Image"));
107 
108  // If we want to show no image, hide image widget, disable the tile so it can't be focused
109  wImage.SetVisible(backendImage != null);
110  tile.SetEnabled(backendImage != null);
111 
112  comp.SetImage(backendImage);
113  }
114 
115  //------------------------------------------------------------------------------------------
117  protected void OffsetCurrentItem(int offset)
118  {
119  m_iCurrentItem = m_iCurrentItem + offset;
120  int maxItemId = m_aImages.Count() - m_aTiles.Count();
121  if (maxItemId < 0)
122  maxItemId = 0;
123  m_iCurrentItem = Math.ClampInt(m_iCurrentItem, 0, maxItemId);
124  UpdateAllWidgets();
125  }
126 
127 
128  //------------------------------------------------------------------------------------------
130  protected void OnTileClick(SCR_ModularButtonComponent comp)
131  {
132  // Bail if we have no images at all
133  if (m_aImages.IsEmpty())
134  return;
135 
136  // Find what image was selected in this tile
137  int tileId = m_aTiles.Find(comp.GetRootWidget());
138 
139  if (tileId == -1)
140  return;
141 
142  int imageId = m_iCurrentItem + tileId;
143 
144  // Correct the image ID if a tile without an image was selected
145  imageId = Math.Clamp(imageId, 0, m_aImages.Count() - 1);
146 
147  array<BackendImage> galleryDialogImages = {};
148  foreach (auto img : m_aImages)
149  galleryDialogImages.Insert(img);
150 
151  SCR_ContentBrowser_GalleryDialog.CreateForImages(galleryDialogImages, imageId);
152  }
153 
154 
155 
156 
157 
158 
159  // Action listeners. They are invoked by right/left keys.
160 
161  //------------------------------------------------------------------------------------------
162  protected void OnNextAction()
163  {
164  if (GetGame().GetWorkspace().GetFocusedWidget() != m_aTiles[m_aTiles.Count()-1])
165  return;
166  OffsetCurrentItem(1);
167  }
168 
169 
170  //------------------------------------------------------------------------------------------
171  protected void OnPrevAction()
172  {
173  if (GetGame().GetWorkspace().GetFocusedWidget() != m_aTiles[0])
174  return;
175  OffsetCurrentItem(-1);
176  }
177 
178 
179 
180 
181 
182 
183  // Event handlers for left/right buttons
184 
185  //------------------------------------------------------------------------------------------
186  protected void OnNextButton()
187  {
188  OffsetCurrentItem(1);
189  GetGame().GetWorkspace().SetFocusedWidget(m_aTiles[m_aTiles.Count()-1]);
190  }
191 
192  //------------------------------------------------------------------------------------------
193  protected void OnPrevButton()
194  {
195  OffsetCurrentItem(-1);
196  GetGame().GetWorkspace().SetFocusedWidget(m_aTiles[0]);
197  }
198 
199 
200 
201 
202 
203  //------------------------------------------------------------------------------------------
204  override bool OnUpdate(Widget w)
205  {
206  if (!SCR_Global.IsEditMode())
207  GetGame().GetCallqueue().CallLater(UpdateSize, 0);
208  return true;
209  }
210 
211 
212  //------------------------------------------------------------------------------------------
213  protected void UpdateSize()
214  {
215  if (m_aTiles.IsEmpty())
216  return;
217 
218  // Resize the height of all images, we must keep same aspect ratio
219  float sizex, sizey;
220  m_aTiles[0].GetScreenSize(sizex, sizey);
221  float sizexUnscaled = GetGame().GetWorkspace().DPIUnscale(sizex);
222  m_Widgets.m_ImagesHeightSize.EnableHeightOverride(true);
223  m_Widgets.m_ImagesHeightSize.SetHeightOverride(sizexUnscaled / SCR_WorkshopUiCommon.IMAGE_SIZE_RATIO);
224  }
225 
226 
227  //------------------------------------------------------------------------------------------
228  override void HandlerAttached(Widget w)
229  {
230  m_Widgets.Init(w);
231 
232 
233  // Find image gallery widgets
234  foreach (string widgetName : m_aTileWidgetNames)
235  {
236  Widget wImageTile = w.FindAnyWidget(widgetName);
237  m_aTiles.Insert(wImageTile);
238 
239  SCR_ModularButtonComponent comp = SCR_ModularButtonComponent.Cast(wImageTile.FindHandler(SCR_ModularButtonComponent));
240  comp.m_OnClicked.Insert(OnTileClick);
241 
242  if (wImageTile)
243  {
244  SCR_BackendImageComponent backendImgComp = SCR_BackendImageComponent.Cast(wImageTile.FindHandler(SCR_BackendImageComponent));
245  m_aTileComponents.Insert(backendImgComp);
246  }
247  }
248 
249  // Reset the state of dots
250  if (!SCR_Global.IsEditMode())
251  m_Widgets.m_SpinBoxComponent.ClearAll();
252 
253  // Listen to inputs
254  if (!SCR_Global.IsEditMode())
255  {
256  GetGame().GetInputManager().AddActionListener("MenuRight", EActionTrigger.DOWN, OnNextAction);
257  GetGame().GetInputManager().AddActionListener("MenuLeft", EActionTrigger.DOWN, OnPrevAction);
258  }
259 
260  m_Widgets.m_NextButtonComponent.m_OnClicked.Insert(OnNextButton);
261  m_Widgets.m_PrevButtonComponent.m_OnClicked.Insert(OnPrevButton);
262 
263  m_Widgets.m_SpinBoxComponent.SetCurrentItem(0);
264 
265  UpdateAllWidgets();
266 
267  GetGame().GetCallqueue().CallLater(Update, 50);
268  }
269 
270 
271  //------------------------------------------------------------------------------------------
272  override void HandlerDeattached(Widget w)
273  {
274  if (!SCR_Global.IsEditMode())
275  {
276  GetGame().GetInputManager().RemoveActionListener("MenuRight", EActionTrigger.DOWN, OnNextAction);
277  GetGame().GetInputManager().RemoveActionListener("MenuLeft", EActionTrigger.DOWN, OnPrevAction);
278  }
279 
280  GetGame().GetCallqueue().Remove(Update);
281  }
282 };
m_aTileComponents
protected ref array< SCR_ContentBrowserTileComponent > m_aTileComponents
Definition: SCR_ContentBrowser_AddonsSubMenu.c:52
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_Widgets
ref SCR_VoNOverlay_ElementWidgets m_Widgets
Definition: SCR_VonDisplay.c:3
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_Global
Definition: Functions.c:6
SCR_WorkshopUiCommon
Definition: SCR_WorkshopUiCommon.c:5