Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ModEntryComponent.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
3 {
4  const string WIDGET_MOD_RATING = "hRating";
5  const string WIDGET_MOD_SIZE = "hDataSize";
6 
7  // Widgets
8  protected TextWidget m_wTxtRating;
9  protected TextWidget m_wTxtDataSize;
10 
11  // Mod handling
12  protected ref SCR_WorkshopItem m_Item; // Strong ref!
13  ref ScriptInvoker m_OnModDonwloaded = new ScriptInvoker();
14  ref Revision m_Version;
15 
16  // Handling button
17  protected SCR_MultipleStatesButtonComponent m_BtnDownload;
18 
19  // Other
20  protected bool m_bHover = false;
21  protected bool m_bFocus = false;
22 
23  // Constants
24  const int STATE_DOWNLOAD = 0;
25  const int STATE_DOWNLOADING = 1;
26  const int STATE_CANCEL = 2;
27  const int STATE_DOWNLOADED = 3;
28 
29  //------------------------------------------------------------------------------------------------
30  override void HandlerAttached(Widget w)
31  {
32  super.HandlerAttached(w);
33 
34  // Mini texts widgets
35  Widget wRating = w.FindAnyWidget(WIDGET_MOD_RATING);
36  if (wRating)
37  m_wTxtRating = TextWidget.Cast(wRating.FindAnyWidget("Text"));
38 
39  Widget wSize = w.FindAnyWidget(WIDGET_MOD_SIZE);
40  if (wSize)
41  m_wTxtDataSize = TextWidget.Cast(wSize.FindAnyWidget("Text"));
42 
43  // Handling setup
44  Widget btnDownloadWidget = w.FindAnyWidget(WIDGET_BTN_DOWNLOAD);
45  if (btnDownloadWidget)
46  {
47  m_BtnDownload = SCR_MultipleStatesButtonComponent.Cast(btnDownloadWidget.FindHandler(SCR_MultipleStatesButtonComponent));
48  if (m_BtnDownload)
49  {
50  m_BtnDownload.m_OnClicked.Insert(OnDownloadClick);
51  m_BtnDownload.m_OnHover.Insert(OnDownloadButtonHover);
52  m_BtnDownload.m_OnHoverLeave.Insert(OnDownloadButtonHoverLeave);
53  }
54  }
55 
56  this.UpdateAllWidgets();
57  //GetGame().GetCallqueue().CallLater(UpdateEachFrame, 1);
58  }
59 
60  //------------------------------------------------------------------------------------------------
61  override void HandlerDeattached(Widget w)
62  {
63  //GetGame().GetCallqueue().Remove(UpdateEachFrame);
64  }
65 
66  //------------------------------------------------------------------------------------------------
67  override bool OnFocus(Widget w, int x, int y)
68  {
69  //super.OnFocus(w, x, y);
70  //m_OnFocus.Invoke(m_wRoot, m_Dependency);
71 
72  //m_bFocus = true;
73 
74  return false;
75  }
76 
77  //------------------------------------------------------------------------------------------------
78  override bool OnFocusLost(Widget w, int x, int y)
79  {
80  super.OnFocusLost(w, x, y);
81 
82  m_bFocus = false;
83 
84  return true;
85  }
86 
87  //------------------------------------------------------------------------------------------------
88  void UpdateAllWidgets()
89  {
90  if (!m_Item)
91  return;
92 
93  // Set data from the workshop item, if it's fetched
94 
95  if (m_Item.GetRequestFailed())
96  {
97  SetLabelText("[Failed to load data]");
98  return;
99  }
100 
101  // Texts and image
102  /*
103  SetDescriptionText(m_Item.GetSummary());
104  bool imageLoaded, backendHasImage;
105  ImageScale image;
106  m_Item.GetPreviewImage(imageLoaded, backendHasImage, image);
107  if (imageLoaded)
108  {
109  SetThumbnail(image.Path());
110  }
111  */
112 
113  // Rating
114  int rating = m_Item.GetAverageRating() * 100;
115  SetModRating(rating.ToString() + "%");
116 
117  // Set name
118  SetLabelText(m_Item.GetName());
119 
120  // Size
121  string sizeStr = SCR_ByteFormat.GetReadableSize(m_Item.GetSizeBytes());
122  this.SetModDataSize(sizeStr);
123 
124 
125  this.UpdateDownloadButtonState();
126  }
127 
128  //------------------------------------------------------------------------------------------------
129  /*
130  void UpdateEachFrame()
131  {
132  this.Update();
133  GetGame().GetCallqueue().CallLater(UpdateEachFrame, 1);
134  }
135  */
136 
137  //------------------------------------------------------------------------------------------------
138  void UpdateDownloadButtonState()
139  {
140  if (!m_Item)
141  {
142  return;
143  }
144 
145  // Update the state of the main button
146 
147  SCR_WorkshopItem item = m_Item;
148  bool downloading = false;
149  bool paused = false;
150  bool downloaded = false;
151  Revision targetRevision;
152  float progress = -1.0;
153  int newState = -1;
154 
155  item.GetDownloadState(downloading, paused, progress, targetRevision);
156  downloaded = item.GetOffline();
157 
158  // Set state of the button
159  /*
160  if (m_bHover)
161  {
162  // Hovering the cursor
163  if (downloading)
164  newState = STATE_CANCEL;
165  else
166  {
167  if (!downloaded)
168  newState = STATE_DOWNLOAD;
169  else
170  newState = STATE_DOWNLOADED;
171  }
172  }
173  else
174  {
175 
176  if (downloaded)
177  {
178  newState = STATE_DOWNLOADED;
179  }
180  else
181  {
182  // NOT hovering the cursor
183  if (downloading)
184  newState = STATE_DOWNLOADING;
185  else
186  newState = STATE_DOWNLOAD;
187  }
188  }
189  */
190 
191 
192 
193  // On consoles we can't hover, so hovering is irrelevant
194  if (downloaded)
195  {
196  newState = STATE_DOWNLOADED;
197  }
198  else
199  {
200  // NOT hovering the cursor
201  if (downloading)
202  newState = STATE_CANCEL;
203  else
204  newState = STATE_DOWNLOAD;
205  }
206 
207  if (m_BtnDownload.GetSelectedItem() != newState)
208  {
209  m_BtnDownload.ChangeState(newState);
210  }
211 
212  // Set progress
213  if (progress != -1.0 && downloading)
214  {
215  m_BtnDownload.StartProgress();
216  m_BtnDownload.SetProgress(progress * 100.0);
217  }
218  else
219  {
220  m_BtnDownload.FinishProgress();
221  }
222 
223  // Show or hide the hint
224  m_BtnDownload.SetHintVisible(m_BtnDownload.GetSelectedItem() != STATE_DOWNLOADED && m_bFocus);
225  }
226 
227  //------------------------------------------------------------------------------------------------
229  void OnDownloadClick(SCR_ButtonBaseComponent button)
230  {
231  int state = m_BtnDownload.GetSelectedItem();
232 
233  switch (state)
234  {
235  // Ready to download
236  case STATE_DOWNLOAD:
237  DownloadContent();
238  break;
239 
240  // Is downloading
241  case STATE_DOWNLOADING:
242  m_Item.CancelDownload();
243  break;
244 
245  // Is downloading and hovered
246  case STATE_CANCEL:
247  m_Item.CancelDownload();
248  break;
249 
250  // Is up to date
251  case STATE_DOWNLOADED:
252  break;
253  }
254 
255  this.UpdateDownloadButtonState();
256  }
257 
258 
259  //------------------------------------------------------------------------------------------------
260  void SetModContent(SCR_WorkshopItem item, string version)
261  {
262  m_Item = item;
263 
264  if (item)
265  m_Item.m_OnChanged.Insert(Callback_OnChanged);
266 
267  this.UpdateAllWidgets();
268  }
269 
270  //------------------------------------------------------------------------------------------------
271  void SetModRating(string str) { SetTextSafe(m_wTxtRating, str); }
272 
273  //------------------------------------------------------------------------------------------------
274  void SetModDataSize(string str) { SetTextSafe(m_wTxtDataSize, str); }
275 
276  //------------------------------------------------------------------------------------------------
277  void DownloadContent()
278  {
279  if (!m_Item)
280  return;
281 
282  // Dowload
284  if (m_Version == null)
285  {
286  // No speficic version, just get the latest one
287  action = m_Item.DownloadLatestVersion();
288  }
289  else
290  {
291  // Download a specific version
292  action = m_Item.Download(m_Version);
293  }
294  action.Activate();
295  }
296 
297  //------------------------------------------------------------------------------------------------
298  protected void OnDownloadButtonHover()
299  {
300  m_bHover = true;
301  }
302 
303  //------------------------------------------------------------------------------------------------
304  protected void OnDownloadButtonHoverLeave()
305  {
306  m_bHover = false;
307  }
308 
309  //------------------------------------------------------------------------------------------------
311  protected void OnItemDownload()
312  {
313  Print("on item download");
314  this.UpdateDownloadButtonState();
315  }
316 
317  //------------------------------------------------------------------------------------------------
318  void MarkAsUpdated()
319  {
320  m_BtnDownload.SetProgress(0);
321  UpdateDownloadButtonState();
322  }
323 
324  //------------------------------------------------------------------------------------------------
325  SCR_WorkshopItem GetWorkshopItem() {return m_Item; }
326 
327  //------------------------------------------------------------------------------------------------
328  bool IsUpdated(WorkshopItem item = null)
329  {
330  return m_Item.GetOffline() && !m_Item.GetUpdateAvailable();
331  }
332 
333  //------------------------------------------------------------------------------------------------
334  bool IsDownloading(WorkshopItem item = null)
335  {
336  /*
337  if (!item)
338  item = m_Dependency.GetCachedItem();
339  int flags = item.GetStateFlags();
340 
341  if (flags & EWorkshopItemState.EWSTATE_DOWNLOADING)
342  {
343  return true;
344  }
345  */
346 
347  return false;
348  }
349 
350  // TODO remove this, server browser must be switched use SCR_WorkshopItem
351  Dependency GetDependency() { return null; }
352 
353  protected void Callback_OnChanged()
354  {
355  UpdateAllWidgets();
356  }
357 };
SCR_MultipleStatesButtonComponent
Definition: SCR_MultipleStatesButtonComponent.c:6
m_Item
NewsFeedItem m_Item
Definition: SCR_NewsSubMenu.c:2
STATE_DOWNLOADING
enum SCR_EDownloadManagerTabs STATE_DOWNLOADING
SCR_WorkshopItemActionDownload
Definition: SCR_WorkshopItemActionDownload.c:9
SCR_ModEntryComponent
Definition: SCR_ModEntryComponent.c:2
SCR_WorkshopItem
Definition: SCR_WorkshopItem.c:21
SCR_ByteFormat
Definition: SCR_ByteFormat.c:5
m_bFocus
protected bool m_bFocus
Definition: SCR_ModularButtonComponent.c:74
SCR_ButtonBaseComponent
Base class for any button, regardless its own content.
Definition: SCR_ButtonBaseComponent.c:3
SCR_ContentEntryComponent
Base component for widgets displaying content data.
Definition: SCR_ContentEntryComponent.c:5