Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AddonsPresetSubMenuComponent.c
Go to the documentation of this file.
1 
8 {
10 
11  protected ref array<ref SCR_WorkshopAddonPreset> m_aPresets = {};
12  protected ref array<SCR_AddonLinePresetComponent> m_aPresetLines = {};
13 
14  protected ref SCR_WorkshopAddonPreset m_InteractivePreset;
15 
16  // New temptorary preset
17  protected SCR_ListBoxElementComponent m_CreatedElement;
18  protected SCR_WorkshopAddonPreset m_CreatedPreset;
19 
20  // Navigation buttons
21  protected SCR_InputButtonComponent m_NavLoad;
22  protected SCR_InputButtonComponent m_NavOverride;
23  protected SCR_InputButtonComponent m_NavDelete;
24  protected SCR_InputButtonComponent m_NavRename;
25 
26  // Invokers
27  protected ref ScriptInvoker<bool> Event_OnRename;
28 
29  //------------------------------------------------------------------------------------------------
30  protected void InvokeEventOnRename(bool renameDone)
31  {
32  if (Event_OnRename)
33  Event_OnRename.Invoke(renameDone);
34  }
35 
36  //------------------------------------------------------------------------------------------------
37  ScriptInvoker GetEventOnRename()
38  {
39  if (!Event_OnRename)
40  Event_OnRename = new ScriptInvoker();
41 
42  return Event_OnRename;
43  }
44 
45  //---------------------------------------------------------------------------------------------------
46  override void HandlerAttached(Widget w)
47  {
48  super.HandlerAttached(w);
49 
50  m_Widgets.Init(w);
51 
52  // Setup create new button
53  m_Widgets.m_ButonNewPresetComponent.m_OnClicked.Insert(CreateNewPreset);
54  m_Widgets.m_ButonNewPresetComponent.m_OnFocus.Insert(OnNewPresetFocus);
55  m_Widgets.m_ButonNewPresetComponent.m_OnFocusLost.Insert(OnNewPresetFocusLost);
56  }
57 
58  //---------------------------------------------------------------------------------------------------
59  override void HandlerDeattached(Widget w)
60  {
61  super.HandlerDeattached(w);
62 
63  GetGame().SaveUserSettings();
64  }
65 
66  //---------------------------------------------------------------------------------------------------
67  override void OnTabShow()
68  {
69  // Create new buttons
70  if (!m_NavLoad)
71  {
72  m_NavLoad = CreateNavigationButton("MenuSelect", "#AR-PauseMenu_Load", true);
73  m_NavLoad.m_OnActivated.Insert(OnLoadButton);
74  }
75 
76  if (!m_NavRename)
77  {
78  m_NavRename = CreateNavigationButton("MenuRename", "#AR-MainMenu_Rename", true);
79  m_NavRename.m_OnActivated.Insert(OnRenameButton);
80  }
81 
82  if (!m_NavDelete)
83  {
84  m_NavDelete = CreateNavigationButton("MenuDelete", "#AR-Workshop_ButtonDelete", true);
85  m_NavDelete.m_OnActivated.Insert(OnDeleteButton);
86  }
87 
88  if (!m_NavOverride)
89  {
90  m_NavOverride = CreateNavigationButton("MenuSave", "#AR-PauseMenu_Save", true);
91  m_NavOverride.m_OnActivated.Insert(OnOverrideButton);
92  }
93 
94  super.OnTabShow();
95 
96  UpdatePresetListbox();
97 
98  if (!GetGame().InPlayMode())
99  return;
100 
101  SCR_AddonManager.GetInstance().GetPresetStorage().GetEventOnUsedPresetChanged().Insert(OnUsedPresetChanged);
102 
103  // This will update the footer buttons and make sure there's something always focused
104  GetGame().GetWorkspace().SetFocusedWidget(null);
105  GetGame().GetWorkspace().SetFocusedWidget(m_Widgets.m_ButonNewPreset);
106  }
107 
108  //---------------------------------------------------------------------------------------------------
109  // Display list
110  //---------------------------------------------------------------------------------------------------
111  //---------------------------------------------------------------------------------------------------
113  protected void UpdatePresetListbox()
114  {
115  if (!m_Widgets || !m_Widgets.m_ScrollPresetsComponent)
116  return;
117 
118  SCR_ListBoxComponent lb = m_Widgets.m_ScrollPresetsComponent;
119  if (!lb)
120  return;
121 
122  // Remove all items
123  while (lb.GetItemCount() > 0)
124  lb.RemoveItem(0);
125 
126  m_aPresetLines.Clear();
127 
128  // Read data
129  if (!GetGame().InPlayMode())
130  return;
131 
132  SCR_WorkshopAddonManagerPresetStorage presetStorage = SCR_AddonManager.GetInstance().GetPresetStorage();
133  m_aPresets = presetStorage.GetAllPresets();
134 
135  // Add items
136  for (int i = m_aPresets.Count() - 1; i >= 0; i--)
137  {
138  SCR_WorkshopAddonPreset preset = m_aPresets[i];
139 
141  int id = lb.AddItem(preset.GetName(), data);
142 
143  // Items actions
144  Widget wLine = lb.GetElementComponent(id).GetRootWidget();
145  if (!wLine)
146  continue;
147 
149  if (!line)
150  continue;
151 
152  line.SetPreset(preset);
153  line.UpdateWidgets();
154 
155  line.GetEventOnNameChanged().Insert(RenamePreset);
156  line.GetEventOnNameEditStart().Insert(OnNameEditStart);
157  line.GetEventOnLoad().Insert(LoadPreset);
158  line.GetEventOnOverride().Insert(OverridePreset);
159  line.GetEventOnDelete().Insert(DeletePreset);
160  line.GetEventOnFocus().Insert(OnLineFocus);
161  line.GetEventOnFocusLost().Insert(OnLineFocusLost);
162  line.GetEventOnButtonClick().Insert(OnLineButtonClick);
163 
164  line.ShowWarning(!HasAllPresetAddons(line));
165 
166  m_aPresetLines.Insert(line);
167  }
168 
169  GetGame().GetCallqueue().Call(OnUsedPresetChanged, SCR_AddonManager.GetInstance().GetPresetStorage().GetUsedPreset(), "");
170  }
171 
172  //---------------------------------------------------------------------------------------------------
174  protected bool HasAllPresetAddons(SCR_AddonLinePresetComponent line)
175  {
176  SCR_WorkshopAddonPreset preset = line.GetPreset();
177  if (!preset)
178  return false;
179 
180  array<ref SCR_WorkshopAddonPresetAddonMeta> addons = preset.GetAddons();
181 
182  foreach (SCR_WorkshopAddonPresetAddonMeta meta : addons)
183  {
184  string guid = meta.GetGuid();
185  SCR_WorkshopItem item = SCR_AddonManager.GetInstance().GetItemsMap().Get(guid);
186 
187  if (!item || !item.GetOffline())
188  return false;
189  }
190 
191  return true;
192  }
193 
194  //---------------------------------------------------------------------------------------------------
196  protected bool CanCreatePreset(SCR_WorkshopAddonPreset preset)
197  {
198  if (!preset)
199  new SCR_WorkshopErrorDialog("#AR-Workshop_EmptyAddonPresetMsg");
200 
201  return preset;
202  }
203 
204  //---------------------------------------------------------------------------------------------------
206  protected bool IsPresetAddonsListEmpty(SCR_WorkshopAddonPreset preset)
207  {
208  if (preset.GetAddons().IsEmpty())
209  {
210  new SCR_WorkshopErrorDialog("#AR-Workshop_EmptyAddonPresetMsg");
211  return false;
212  }
213 
214  return true;
215  }
216 
217  //---------------------------------------------------------------------------------------------------
219  protected bool IsAddonsListEmpty(array<ref SCR_WorkshopItem> items)
220  {
221  if (items.IsEmpty())
222  {
223  new SCR_WorkshopErrorDialog("#AR-Workshop_EmptyAddonPresetMsg");
224  return false;
225  }
226 
227  return true;
228  }
229 
230  //---------------------------------------------------------------------------------------------------
232  protected bool IsNameValid(string name)
233  {
234  // Empty
235  if (name.IsEmpty())
236  {
237  new SCR_WorkshopErrorDialog("#AR-Workshop_EmptyNameAddonPresetMsg");
238  return false;
239  }
240 
241  // Name is just spaces
242  bool spaceOnly = true;
243 
244  for (int i = 0, len = name.Length(); i < len; i++)
245  {
246  if (name[i] != " ")
247  {
248  spaceOnly = false;
249  break;
250  }
251  }
252 
253  if (spaceOnly)
254  {
255  new SCR_WorkshopErrorDialog("#AR-Workshop_EmptyNameAddonPresetMsg");
256  return false;
257  }
258 
259  return true;
260  }
261 
262  //---------------------------------------------------------------------------------------------------
263  protected void OnUsedPresetChanged(string name)
264  {
265  foreach (SCR_AddonLinePresetComponent preset : m_aPresetLines)
266  {
267  preset.SetSelected(preset.GetName() == name);
268  }
269  }
270 
271  //---------------------------------------------------------------------------------------------------
273  protected bool IsNameUnique(SCR_WorkshopAddonPreset preset, string name)
274  {
275  if (SCR_AddonManager.GetInstance().GetPresetStorage().PresetExists(name))
276  {
277  SCR_WorkshopPresetConfirmDialog dlg = SCR_WorkshopPresetConfirmDialog.CreateOverridePresetDialog(preset);
278  dlg.m_OnConfirm.Insert(Callback_OnConfirmOverride);
279  return false;
280  }
281 
282  return true;
283  }
284 
285  //---------------------------------------------------------------------------------------------------
286  // Preset actions
287  //---------------------------------------------------------------------------------------------------
288  //---------------------------------------------------------------------------------------------------
290  protected void CreateNewPreset()
291  {
292  int addonsCount = SCR_AddonManager.GetInstance().CountOfEnabledAddons();
293 
294  if (addonsCount == 0)
295  {
296  SCR_WorkshopAddonPreset empty = null;
297  if (!CanCreatePreset(empty))
298  return;
299  }
300 
301  SCR_ListBoxComponent lb = m_Widgets.m_ScrollPresetsComponent;
302  if (!lb)
303  return;
304 
305  int id = lb.AddItem("");
306  m_CreatedElement = lb.GetElementComponent(id);
307 
308  // Setup line
309  Widget wLine = lb.GetElementComponent(id).GetRootWidget();
310  if (!wLine)
311  return;
312 
313  wLine.SetZOrder(-1);
314  m_InteractivePreset = null;
315 
317  if (!line)
318  return;
319 
320  // Display data
321  line.ShowModCount(addonsCount);
322  line.ShowWarning(false);
323 
324  // Setup edit action
325  line.ShowDefaultName();
326  line.StartEditName();
327 
328  SetupLineCreationState(line);
329 
330  InvokeEventOnRename(false);
331  OnRename(false);
332  }
333 
334  //---------------------------------------------------------------------------------------------------
335  protected void Callback_OnNameNewPreset(SCR_AddonLinePresetComponent line, string name)
336  {
337  ClearLineCreationState(line);
338 
339  SCR_WorkshopAddonPreset preset = SCR_AddonManager.GetInstance().CreatePresetFromEnabledAddons(name);
340 
341  // Check states
342  if (!CanCreatePreset(preset))
343  return;
344 
345  if (!IsPresetAddonsListEmpty(preset))
346  return;
347 
348  if (!IsNameValid(name))
349  return;
350 
351  if (!IsNameUnique(preset, name))
352  return;
353 
354  // All fine, save this preset, add item to listbox
355  SCR_AddonManager.GetInstance().GetPresetStorage().SavePreset(preset);
356  UpdatePresetListbox();
357 
358  InvokeEventOnRename(true);
359  OnRename(true);
360 
361  m_Widgets.m_ScrollPresetsComponent.SetFocusOnFirstItem();
362  }
363 
364  //---------------------------------------------------------------------------------------------------
365  protected void Callback_OnNewPresetFocusLost(SCR_AddonLinePresetComponent line)
366  {
367  UpdatePresetListbox();
368  ClearLineCreationState(line);
369  }
370 
371  //---------------------------------------------------------------------------------------------------
372  protected void SetupLineCreationState(SCR_AddonLinePresetComponent line)
373  {
374  m_Widgets.m_ButonNewPresetComponent.SetEnabled(false);
375  SetExistingLinesEnabled(false);
376 
377  line.GetEventOnNameChanged().Insert(Callback_OnNameNewPreset);
378  line.GetEventOnFocusLost().Insert(Callback_OnNameNewPreset);
379  line.GetEventOnNameEditLeave().Insert(Callback_OnNewPresetFocusLost);
380  }
381 
382  //---------------------------------------------------------------------------------------------------
383  protected void ClearLineCreationState(SCR_AddonLinePresetComponent line)
384  {
385  m_Widgets.m_ButonNewPresetComponent.SetEnabled(true);
386  SetExistingLinesEnabled(true);
387 
388  line.GetEventOnNameChanged().Remove(Callback_OnNameNewPreset);
389  line.GetEventOnFocusLost().Remove(Callback_OnNameNewPreset);
390  line.GetEventOnNameEditLeave().Remove(Callback_OnNewPresetFocusLost);
391  }
392 
393  //---------------------------------------------------------------------------------------------------
394  protected void SetExistingLinesEnabled(bool enabled)
395  {
396  foreach (SCR_AddonLinePresetComponent line : m_aPresetLines)
397  {
398  line.SetEnabled(enabled);
399  }
400  }
401 
402  //---------------------------------------------------------------------------------------------------
404  protected void LoadPreset(SCR_AddonLinePresetComponent line)
405  {
406  SCR_WorkshopAddonPreset preset = line.GetPreset();
407  if (!preset)
408  return;
409 
410  array<ref SCR_WorkshopAddonPresetAddonMeta> addonsNotFound = {};
411  SCR_AddonManager.GetInstance().SelectPreset(preset, addonsNotFound);
412 
413  if (addonsNotFound.IsEmpty())
414  SCR_AddonManager.GetInstance().GetPresetStorage().SetUsedPreset(line.GetPreset().GetName());
415  }
416 
417  //---------------------------------------------------------------------------------------------------
419  protected void OverridePreset(SCR_AddonLinePresetComponent line)
420  {
421  SCR_WorkshopAddonPreset preset = line.GetPreset();
422  if (!preset)
423  return;
424 
425  SCR_AddonManager addonMgr = SCR_AddonManager.GetInstance();
426  array<ref SCR_WorkshopItem> enabledAddons = addonMgr.SelectItemsBasic(addonMgr.GetOfflineAddons(), EWorkshopItemQuery.ENABLED);
427 
428  if (!IsAddonsListEmpty(enabledAddons))
429  return;
430 
431  // Create confirmation dialog
432  SCR_WorkshopPresetConfirmDialog dlg = SCR_WorkshopPresetConfirmDialog.CreateOverridePresetDialog(preset);
433  dlg.m_OnConfirm.Insert(Callback_OnConfirmOverride);
434  }
435 
436  //---------------------------------------------------------------------------------------------------
437  protected void Callback_OnConfirmOverride(SCR_WorkshopPresetConfirmDialog dlg)
438  {
439  string prevName = "";
440  if (m_InteractivePreset)
441  prevName = m_InteractivePreset.GetName();
442 
443  SCR_WorkshopAddonPreset preset = SCR_AddonManager.GetInstance().CreatePresetFromEnabledAddons(dlg.m_Preset.GetName());
444  if (!preset)
445  return;
446 
447  // Change name
448  if (!m_sNameOverride.IsEmpty())
449  {
450  SCR_AddonManager.GetInstance().GetPresetStorage().DeletePreset(m_sNameOverride);
451  preset.SetName(m_sNameOverride);
452 
453  m_sNameOverride = "";
454  }
455 
456  SCR_AddonManager.GetInstance().GetPresetStorage().SavePreset(preset, prevName);
457 
458  // Update LB, the displayed preset names might have changed
459  UpdatePresetListbox();
460  }
461 
462  //---------------------------------------------------------------------------------------------------
464  protected void DeletePreset(SCR_AddonLinePresetComponent line)
465  {
466  SCR_WorkshopAddonPreset preset = line.GetPreset();
467  if (!preset)
468  return;
469 
470  SCR_WorkshopPresetConfirmDialog dlg = SCR_WorkshopPresetConfirmDialog.CreateDeletePresetDialog(preset.GetName());
471  dlg.m_OnConfirm.Insert(Callback_OnConfirmDelete);
472  }
473 
474  //---------------------------------------------------------------------------------------------------
475  protected void Callback_OnConfirmDelete(SCR_WorkshopPresetConfirmDialog dlg)
476  {
477  SCR_AddonManager.GetInstance().GetPresetStorage().DeletePreset(dlg.m_sPresetName);
478 
479  // Update LB, the displayed preset names might have changed
480  UpdatePresetListbox();
481  }
482 
483  protected string m_sNameOverride;
484 
485  //---------------------------------------------------------------------------------------------------
487  protected void RenamePreset(SCR_AddonLinePresetComponent line, string name)
488  {
489  SCR_WorkshopAddonPreset preset = line.GetPreset();
490  if (!preset)
491  return;
492 
493  if (!IsNameValid(name))
494  return;
495 
496  // Check if preset with same name is not unique
497  SCR_WorkshopAddonManagerPresetStorage storage = SCR_AddonManager.GetInstance().GetPresetStorage();
498  SCR_WorkshopAddonPreset presetCompare = storage.GetPreset(name);
499 
500  m_sNameOverride = "";
501 
502  if (presetCompare && presetCompare != preset)
503  {
504  m_sNameOverride = name;
505  SCR_WorkshopPresetConfirmDialog dlg = SCR_WorkshopPresetConfirmDialog.CreateOverridePresetDialog(preset);
506  dlg.m_OnConfirm.Insert(Callback_OnConfirmOverride);
507  return;
508  }
509 
510  string prevName = preset.GetName();
511  preset.SetName(name);
512 
513  storage.SavePreset(preset, prevName);
514  UpdatePresetListbox();
515 
516  InvokeEventOnRename(true);
517  OnRename(true);
518  }
519 
520  //---------------------------------------------------------------------------------------------------
521  protected void OnNameEditStart(SCR_AddonLinePresetComponent line)
522  {
523  if (!line)
524  return;
525 
526  m_InteractivePreset = line.GetPreset();
527 
528  InvokeEventOnRename(false);
529  OnRename(false);
530  }
531 
532  //---------------------------------------------------------------------------------------------------
533  protected void OnLineFocus(SCR_AddonLinePresetComponent line)
534  {
535  if (!line)
536  return;
537 
538  m_InteractivePreset = line.GetPreset();
539  }
540 
541  //---------------------------------------------------------------------------------------------------
542  protected void OnLineFocusLost(SCR_AddonLinePresetComponent line)
543  {
544  InvokeEventOnRename(true);
545  OnRename(true);
546  }
547 
548  //---------------------------------------------------------------------------------------------------
550  protected void OnLineButtonClick(SCR_AddonLinePresetComponent line)
551  {
552  m_InteractivePreset = line.GetPreset();
553  }
554 
555  //---------------------------------------------------------------------------------------------------
557  protected SCR_AddonLinePresetComponent FocusedPresetLine()
558  {
559  Widget focused = GetGame().GetWorkspace().GetFocusedWidget();
560  if (!focused)
561  return null;
562 
563  return SCR_AddonLinePresetComponent.Cast(focused.FindHandler(SCR_AddonLinePresetComponent));
564  }
565 
566  //---------------------------------------------------------------------------------------------------
567  // Create new preset button callbacks
568  //---------------------------------------------------------------------------------------------------
569 
570  //---------------------------------------------------------------------------------------------------
571  protected void OnNewPresetFocus()
572  {
573  if (m_NavLoad)
574  m_NavLoad.SetLabel("#AR-Workshop_CreateNewAddonPreset");
575 
576  if (m_NavOverride)
577  m_NavOverride.SetEnabled(false);
578 
579  if (m_NavDelete)
580  m_NavDelete.SetEnabled(false);
581 
582  if (m_NavRename)
583  m_NavRename.SetEnabled(false);
584  }
585 
586  //---------------------------------------------------------------------------------------------------
587  protected void OnNewPresetFocusLost()
588  {
589  if (m_NavLoad)
590  m_NavLoad.SetLabel("#AR-PauseMenu_Load");
591 
592  if (m_NavOverride)
593  m_NavOverride.SetEnabled(true);
594 
595  if (m_NavDelete)
596  m_NavDelete.SetEnabled(true);
597 
598  if (m_NavRename)
599  m_NavRename.SetEnabled(true);
600  }
601 
602  //----------------------------------------------------------------------------------------------
603  protected void OnRename(bool done)
604  {
605  if (done)
606  {
607  // Show focused widget controls
608  if (GetGame().GetWorkspace().GetFocusedWidget() == m_Widgets.m_ButonNewPreset)
609  OnNewPresetFocus();
610  else
611  OnNewPresetFocusLost();
612  }
613  else
614  {
615  if (m_NavLoad)
616  m_NavLoad.SetLabel("#AR-MainMenu_Rename");
617 
618  if (m_NavOverride)
619  m_NavOverride.SetEnabled(false);
620 
621  if (m_NavDelete)
622  m_NavDelete.SetEnabled(false);
623 
624  if (m_NavRename)
625  m_NavRename.SetEnabled(false);
626  }
627  }
628 
629  //---------------------------------------------------------------------------------------------------
630  // Nav buttons callbacks
631  //---------------------------------------------------------------------------------------------------
632 
633  //---------------------------------------------------------------------------------------------------
634  protected void OnLoadButton()
635  {
636  SCR_AddonLinePresetComponent focusedLine = FocusedPresetLine();
637 
638  if (!focusedLine)
639  return;
640 
641  LoadPreset(focusedLine);
642  }
643 
644  //---------------------------------------------------------------------------------------------------
645  protected void OnOverrideButton()
646  {
647  SCR_AddonLinePresetComponent focusedLine = FocusedPresetLine();
648 
649  if (focusedLine)
650  OverridePreset(focusedLine);
651  }
652 
653  //---------------------------------------------------------------------------------------------------
654  protected void OnRenameButton()
655  {
656  SCR_AddonLinePresetComponent focusedLine = FocusedPresetLine();
657 
658  if (focusedLine)
659  focusedLine.StartEditName();
660  }
661 
662  //---------------------------------------------------------------------------------------------------
663  protected void OnDeleteButton()
664  {
665  SCR_AddonLinePresetComponent focusedLine = FocusedPresetLine();
666 
667  if (focusedLine)
668  DeletePreset(focusedLine);
669  }
670 }
SCR_WorkshopPresetConfirmDialog
void SCR_WorkshopPresetConfirmDialog(SCR_WorkshopAddonPreset preset, string presetName, string dlgPreset)
Definition: AddonManagerDialogs.c:46
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_ListBoxElementComponent
Element of a listbox.
Definition: SCR_ListBoxElementComponent.c:2
SCR_WorkshopAddonPresetAddonMeta
Meta data of one addon.
Definition: SCR_WorkshopAddonManagerStorage.c:3
SCR_SubMenuBase
Definition: SCR_SubMenuBase.c:6
m_Widgets
ref SCR_VoNOverlay_ElementWidgets m_Widgets
Definition: SCR_VonDisplay.c:3
SCR_WorkshopItem
Definition: SCR_WorkshopItem.c:21
SCR_WorkshopErrorDialog
Definition: AddonManagerDialogs.c:25
SCR_ListBoxComponent
Definition: SCR_ListBoxComponent.c:4
SCR_AddonsPresetSubMenuWidgets
Definition: SCR_AddonsPresetSubMenuWidgets.c:4
SCR_AddonsPresetsSubMenuComponent
Definition: SCR_AddonsPresetSubMenuComponent.c:7
SCR_AddonLinePresetComponent
Definition: SCR_AddonLinePresetComponent.c:1
m_aPresets
ref array< ref SCR_ScriptedWidgetTooltipPreset > m_aPresets
Definition: SCR_ScriptedWidgetTooltip.c:407
SCR_WorkshopAddonPresetListItemData
Definition: SCR_WorkshopAddonPresetListItemData.c:2
SCR_AddonManager
Definition: SCR_AddonManager.c:72
data
Get all prefabs that have the spawner data
Definition: SCR_EntityCatalogManagerComponent.c:305
EWorkshopItemQuery
EWorkshopItemQuery
Definition: SCR_AddonManager.c:34
SCR_InputButtonComponent
Definition: SCR_InputButtonComponent.c:1
SCR_WorkshopAddonManagerPresetStorage
Definition: SCR_WorkshopAddonManagerStorage.c:110