Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_DownloadManager_Dialog.c
Go to the documentation of this file.
1/*
2Main dialog of the download manager, which is typically opened from the download manager panel.
3
4It lists all current and previous downloads.
5
6!!! For the menu to work, it needs a SCR_DownloadManager_Entity to be placed in the world.
7This is required because the menu can be opened after any of the downloads have been started,
8but by that time we must store the download references somewhere.
9*/
15
16// TODO: move tab specific stuff to a child of SCR_SuperMenuComponent
17class SCR_DownloadManager_Dialog : SCR_TabDialog
18{
19 protected const string STATE_DOWNLOADING = "#AR-Workshop_TabName_Downloaded";
20 protected const string STATE_ALL_PAUSE = "#AR-DownloadManager_State_AllDownloadsPaused";
21 protected const string STATE_NO_ACTIVE_DOWNLOADS = "#AR-DownloadManager_State_NoActiveDownloads";
22
23 protected const ResourceName DOWNLOAD_LINE_LAYOUT = "{FB196DBC0ABA6AE4}UI/layouts/Menus/ContentBrowser/DownloadManager/DownloadManagerEntry.layout";
24 protected const string DOWNLOAD_SUMMARY_FORMAT = "%1 / %2";
25 protected const string DOWNLOAD_SUMMARY_COLORED_FORMAT = "<color rgba=%1>%2</color>";
26
28
29 // Tabs
32
33 //-----------------------------------------------------------------------------------------------
34 static SCR_DownloadManager_Dialog Create()
35 {
36 return SCR_CommonDialogs.CreateDownloadManagerDialog();
37 }
38
39 //------------------------------------------------------------------------------------------
40 // Override
41 //------------------------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------------------------
44 {
45 super.OnMenuOpen(preset);
46
47 // Init widgets
49 GetGame().GetInputManager().AddActionListener("MenuDownloadManager", EActionTrigger.DOWN, OnBackButton);
50
51 m_Widgets.m_Overlay.SetVisible(false);
52 m_Widgets.m_DownloadSummaryText.SetVisible(false);
53 m_Widgets.m_ProgressText.SetVisible(false);
54
55 // Setup submenus
56 m_ActiveDownloads = SCR_DownloadManagerListComponent.Cast(m_SuperMenuComponent.GetSubMenu(SCR_EDownloadManagerTabs.ACTIVE));
57 m_HistoryDownloads = SCR_DownloadManagerListComponent.Cast(m_SuperMenuComponent.GetSubMenu(SCR_EDownloadManagerTabs.HISTORY));
58
59 m_HistoryDownloads.ShowPauseResumeAllButton(false);
60
61 InitList();
62
63 // Setup callback
65
66 downloadManager.m_OnNewDownload.Insert(OnNewDownload);
67 downloadManager.m_OnDownloadComplete.Insert(OnDownloadComplete);
68 downloadManager.m_OnDownloadFailed.Insert(OnDownloadFailed);
69 downloadManager.m_OnDownloadCanceled.Insert(OnDownloadCanceled);
70 }
71
72 //-----------------------------------------------------------------------------------------------
73 override void OnMenuClose()
74 {
75 super.OnMenuClose();
76
77 GetGame().GetInputManager().RemoveActionListener("MenuDownloadManager", EActionTrigger.DOWN, OnBackButton);
78
79 // Clear all recent failed downloads
81 }
82
83 //-----------------------------------------------------------------------------------------------
84 override void OnMenuUpdate(float tDelta)
85 {
87 if (!mgr)
88 return;
89 int nCompleted, nTotal;
90 mgr.GetDownloadQueueState(nCompleted, nTotal);
91
93 bool pauseCancelEnabled; // True when pause or cancel buttons should be enabled
94 if (dlAction)
95 {
96 if (!dlAction.IsFailed() && !dlAction.IsCompleted() && !dlAction.IsCanceled())
97 pauseCancelEnabled = true;
98 }
99
100 // Pause All / Resume All button label
101 bool paused = mgr.GetDownloadsPaused();
102 string pauseAllButtonLabel = "#AR-DownloadManager_ButtonPauseAll";
103 if (paused)
104 pauseAllButtonLabel = "#AR-DownloadManager_ButtonResumeAll";
105
106 // Pause button
107 string pauseButtonLabel = "#AR-DownloadManager_ButtonPause";
108 if (dlAction)
109 {
110 if (dlAction.IsPaused() || dlAction.IsInactive())
111 pauseButtonLabel = "#AR-DownloadManager_ButtonResume";
112 }
113
114 // Show downlaoding speed
116 UpdateDownloadingSpeedWidget(m_ActiveDownloads.HasContent(), nCompleted == nTotal);
117
118 if (m_ActiveDownloads.GetShown())
119 m_ActiveDownloads.ShowPauseResumeAllButton(nCompleted != nTotal);
120 else
121 {
122 m_ActiveDownloads.ShowPauseResumeAllButton(false);
123 m_HistoryDownloads.ShowPauseResumeAllButton(false);
124 }
125 }
126
127 //------------------------------------------------------------------------------------------
128 protected void UpdateProgressWidgets()
129 {
131 string downloadStateText;
132 int nCompleted, nTotal;
133 mgr.GetDownloadQueueState(nCompleted, nTotal);
134
135 if (nTotal > 0 && !mgr.GetDownloadsPaused())
136 downloadStateText = string.Format("%1 %2 / %3", WidgetManager.Translate(STATE_DOWNLOADING), nCompleted, nTotal);
137 else if (nTotal > 0 && mgr.GetDownloadsPaused())
138 downloadStateText = string.Format("%1 %2 / %3", WidgetManager.Translate(STATE_ALL_PAUSE), nCompleted, nTotal);
139 else if (mgr.GetDownloadsPaused())
140 downloadStateText = STATE_ALL_PAUSE;
141 else
142 downloadStateText = STATE_NO_ACTIVE_DOWNLOADS;
143
144 m_Widgets.m_StateText.SetText(downloadStateText);
145
146 m_Widgets.m_Overlay.SetVisible(m_ActiveDownloads.HasContent());
147 m_Widgets.m_DownloadSummaryText.SetVisible(mgr.HasRunningDownloads() || nCompleted > 0);
148 m_Widgets.m_ProgressText.SetVisible(nTotal > 0);
149
150 // Progress bar, progress text
151 if (nTotal > 0)
152 {
153 // Get total progress of all downloads
154 array<ref SCR_WorkshopItemActionDownload> downloadQueue = mgr.GetDownloadQueue();
155 float progress = SCR_DownloadManager.GetDownloadActionsProgress(downloadQueue);
156
157 // Progress bar
158 m_Widgets.m_ProgressBarComponent.SetValue(progress);
159
160 // Progress percent text
161 m_Widgets.m_ProgressText.SetText(UIConstants.FormatUnitPercentage(SCR_WorkshopUiCommon.GetDownloadProgressPercentage(progress)));
162
163 // Display total size
164 float downloadTotalSize = SCR_DownloadManager.GetInstance().GetDownloadQueueSize();
165 m_Widgets.m_DownloadSummaryText.SetVisible(downloadTotalSize > 0);
166
167 if (downloadTotalSize > 0)
168 {
169 float downloadDoneSize = SCR_DownloadManager.GetInstance().GetDownloadedSize();
170
171 string downloadDoneSizeStr = SCR_ByteFormat.ContentDownloadFormat(downloadDoneSize);
172 string downloadTotalSizeStr = SCR_ByteFormat.ContentDownloadFormat(downloadTotalSize);
173
174 string coloredDone = WidgetManager.Translate(DOWNLOAD_SUMMARY_COLORED_FORMAT, UIColors.FormatColor(UIColors.CONTRAST_COLOR), downloadDoneSizeStr);
175 string coloredTotal = WidgetManager.Translate(DOWNLOAD_SUMMARY_COLORED_FORMAT, UIColors.FormatColor(UIColors.CONTRAST_COLOR), downloadTotalSizeStr);
176
177 m_Widgets.m_DownloadSummaryText.SetText(string.Format(DOWNLOAD_SUMMARY_FORMAT, coloredDone, coloredTotal));
178 }
179 }
180 else
181 {
182 m_Widgets.m_ProgressBarComponent.SetValue(0);
183 }
184 }
185
186 //------------------------------------------------------------------------------------------
187 protected void UpdateDownloadingSpeedWidget(bool display, bool showFallback)
188 {
189 m_Widgets.m_DownloadSpeed.SetVisible(display);
190 if (!display)
191 return;
192
193 if (showFallback)
194 {
195 m_Widgets.m_DownloadSpeed.SetText("0 KB/s");
196 return;
197 }
198
199 string unit = " KB/s";
200 float speed = 0;
201
203 speed = GetGame().GetRestApi().GetDownloadTrafficKBPerS();
204
205 // Faster speed
206 if (speed > SCR_ByteFormat.BYTE_UNIT_SIZE)
207 {
208 speed /= SCR_ByteFormat.BYTE_UNIT_SIZE;
209 unit = " MB/s";
210 }
211
212 // Show text
213 m_Widgets.m_DownloadSpeed.SetText(Math.Round(speed).ToString() + unit);
214 }
215
216 //-----------------------------------------------------------------------------------------------
217 // Custom
218 //-----------------------------------------------------------------------------------------------
219 //-----------------------------------------------------------------------------------------------
220 // Init list of downloads
221 protected void InitList()
222 {
223 array<ref SCR_DownloadManager_Entry> downloads = {};
225
226 array<ref SCR_WorkshopItemActionDownload> failedDownloads = SCR_DownloadManager.GetInstance().GetFailedDownloads();
227
228 for (int i = 0, count = downloads.Count(); i < count; i++)
229 {
230 SCR_WorkshopItemActionDownload action = downloads[i].m_Action;
231
233 {
234 m_ActiveDownloads.AddEntry(DOWNLOAD_LINE_LAYOUT, action);
235 }
236 else
237 {
238 // Show recent fails in active
239 if (failedDownloads.Find(action) != -1)
240 m_ActiveDownloads.AddEntry(DOWNLOAD_LINE_LAYOUT, action);
241 else
243 }
244 }
245 }
246
247 //-----------------------------------------------------------------------------------------------
249 {
250 Widget w = GetGame().GetWorkspace().GetFocusedWidget();
251
252 if (!w)
253 return null;
254
256 if (!entry)
257 return null;
258
259 return entry.GetDownloadAction();
260 }
261
262 //------------------------------------------------------------------------------------------
263 // Callbacks
264 //------------------------------------------------------------------------------------------
265 //------------------------------------------------------------------------------------------
267 {
268 // Remove same item in history
269 SCR_DownloadManagerEntry sameEntry = m_HistoryDownloads.EntryWithItem(item);
270 if (sameEntry)
271 m_HistoryDownloads.RemoveEntry(sameEntry);
272
273 // Check same item in active
274 sameEntry = m_ActiveDownloads.EntryWithItem(item);
275
276 if (sameEntry)
277 {
278 m_ActiveDownloads.ChangeEntryCategory(sameEntry.GetRootWidget(), EDownloadManagerActionState.RUNNING);
279 sameEntry.InitForDownloadAction(item, item.GetDownloadAction());
280 }
281 else
282 {
283 // Add action on activate in order to have target version
284 action.m_OnActivated.Insert(OnNewActionActivate);
285 }
286 }
287
288 //------------------------------------------------------------------------------------------
290 {
291 m_ActiveDownloads.AddEntry(DOWNLOAD_LINE_LAYOUT, action);
292 action.m_OnActivated.Remove(OnNewActionActivate);
293 }
294
295 //------------------------------------------------------------------------------------------
297 {
298 // Move to downloaded
299 SCR_DownloadManagerEntry entry = m_ActiveDownloads.DownloadActionLine(action);
300
301 if (entry)
302 m_ActiveDownloads.ChangeEntryCategory(entry.GetRootWidget(), EDownloadManagerActionState.DOWNLOADED);
303 }
304
305 //------------------------------------------------------------------------------------------
309 {
310 array<ref SCR_DownloadManager_Entry> downloads = {};
312
313 SCR_DownloadManagerEntry entry = m_ActiveDownloads.DownloadActionLine(action);
314
315 if (entry)
316 m_ActiveDownloads.ChangeEntryCategory(entry.GetRootWidget(), EDownloadManagerActionState.FAILED);
317
318 // Open active and scroll up
319 m_SuperMenuComponent.GetTabView().ShowTab(SCR_EDownloadManagerTabs.ACTIVE, false);
320 }
321
322 //------------------------------------------------------------------------------------------
324 {
325 SCR_DownloadManagerEntry entry = m_ActiveDownloads.DownloadActionLine(action);
326
327 if (entry)
328 m_ActiveDownloads.ChangeEntryCategory(entry.GetRootWidget(), EDownloadManagerActionState.FAILED);
329 }
330
331 //-----------------------------------------------------------
332 protected void OnBackButton()
333 {
334 m_OnCancel.Invoke();
335 Close();
336 }
337
338 //-----------------------------------------------------------
339 protected void OnPauseAllButton()
340 {
342
343 if (!mgr)
344 return;
345
346 bool paused = mgr.GetDownloadsPaused();
347 mgr.SetDownloadsPaused(!paused);
348 }
349
350 //-----------------------------------------------------------------------------------------------
351 protected void OnPauseButton()
352 {
354
355 if (!action)
356 return;
357
358 if (action.IsPaused())
359 action.Resume();
360 else if (action.IsInactive())
361 action.Activate();
362 else
363 action.Pause();
364 }
365
366 //-----------------------------------------------------------------------------------------------
367 protected void OnCancelButton()
368 {
370
371 if (!action)
372 return;
373
374 action.Cancel();
375 }
376
377 //-----------------------------------------------------------------------------------------------
378 protected void OnAutoenableButton()
379 {
381 SCR_WorkshopSettings.Save(settings);
382 }
383}
ArmaReforgerScripted GetGame()
Definition game.c:1398
EDownloadManagerActionState
Enum describing current state of downloading action.
const string DOWNLOAD_SUMMARY_FORMAT
void OnBackButton()
const string STATE_ALL_PAUSE
const ResourceName DOWNLOAD_LINE_LAYOUT
ref SCR_DownloadManagerListComponent m_HistoryDownloads
void UpdateDownloadingSpeedWidget(bool display, bool showFallback)
enum SCR_EDownloadManagerTabs STATE_DOWNLOADING
void UpdateProgressWidgets()
void OnDownloadComplete(SCR_WorkshopItemActionDownload action)
void OnCancelButton()
void OnDownloadCanceled(SCR_WorkshopItemActionDownload action)
void OnDownloadFailed(SCR_WorkshopItemActionDownload action, int reason)
void OnNewActionActivate(SCR_WorkshopItemActionDownload action)
void OnPauseAllButton()
void OnPauseButton()
const string STATE_NO_ACTIVE_DOWNLOADS
ref SCR_DownloadManagerListComponent m_ActiveDownloads
const string DOWNLOAD_SUMMARY_COLORED_FORMAT
void OnNewDownload(SCR_WorkshopItem item, SCR_WorkshopItemActionDownload action)
void OnAutoenableButton()
ref SCR_HeaderNavigationWidgets m_Widgets
override void OnMenuOpen()
Definition Math.c:13
void InitForDownloadAction(SCR_WorkshopItem item, SCR_WorkshopItemActionDownload action)
static EDownloadManagerActionState DownloadActionState(SCR_WorkshopItemActionDownload action)
SCR_WorkshopItemActionDownload GetDownloadAction()
void GetDownloadQueueState(out int nCompleted, out int nTotal)
Might get delayed by a frame! Just use it for UI.
array< ref SCR_WorkshopItemActionDownload > GetFailedDownloads()
ref ScriptInvokerBase< ScriptInvoker_DownloadManagerActionError > m_OnDownloadFailed
void SetDownloadsPaused(bool pause, int count=-1)
static float GetDownloadActionsProgress(array< ref SCR_WorkshopItemActionDownload > actions)
Returns overall progress of all download actions, from 0 to 1.
static SCR_DownloadManager GetInstance()
array< ref SCR_WorkshopItemActionDownload > GetDownloadQueue()
ref ScriptInvoker m_OnNewDownload
ref ScriptInvokerBase< ScriptInvoker_DownloadManagerAction > m_OnDownloadComplete
ref ScriptInvokerBase< ScriptInvoker_DownloadManagerAction > m_OnDownloadCanceled
void GetAllDownloads(array< ref SCR_DownloadManager_Entry > downloads)
Returns an array of all downloads regardless of their state.
override void OnMenuClose()
override void OnMenuUpdate(float tDelta)
SCR_WorkshopItemActionDownload GetDownloadAction()
Returns current download action.
static void Save(SCR_WorkshopSettings settings)
static SCR_WorkshopSettings Get()
static int GetDownloadProgressPercentage(float progress)
@ ACTIVE
Tells that this entity has to be actively updated by engine.
Definition EntityFlags.c:30
EActionTrigger
proto external BaseUserAction GetSelectedAction()