Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_AddonLinePresetComponent.c
Go to the documentation of this file.
2 {
3  [Attribute(UIColors.GetColorAttribute(UIColors.CONTRAST_COLOR))]
4  protected ref Color m_cSelectedColor;
5 
6  [Attribute(UIColors.GetColorAttribute(UIColors.NEUTRAL_INFORMATION))]
7  protected ref Color m_cDefaultColor;
8 
9  protected const string STR_DEFAULT_NAME = "Preset ";
10 
11  protected ref SCR_AddonLinePresetWidgets m_Widgets = new SCR_AddonLinePresetWidgets();
12  protected SCR_WorkshopAddonPreset m_Preset;
13 
14  protected bool m_bSelected = false;
15 
16  // Buttons
17  protected SCR_ModularButtonComponent m_ButtonComponent;
18 
19  //------------------------------------------------------------------------------------------------
20  // Invokers
21  //------------------------------------------------------------------------------------------------
22 
23  protected ref ScriptInvoker<SCR_AddonLinePresetComponent, string> Event_OnNameChanged;
24  protected ref ScriptInvoker<SCR_AddonLinePresetComponent> Event_OnNameEditStart;
25  protected ref ScriptInvoker<SCR_AddonLinePresetComponent> Event_OnNameEditLeave;
26  protected ref ScriptInvoker<SCR_AddonLinePresetComponent> Event_OnFocus;
27  protected ref ScriptInvoker<SCR_AddonLinePresetComponent> Event_OnFocusLost;
28  protected ref ScriptInvoker<SCR_AddonLinePresetComponent> Event_OnLoad;
29  protected ref ScriptInvoker<SCR_AddonLinePresetComponent> Event_OnOverride;
30  protected ref ScriptInvoker<SCR_AddonLinePresetComponent> Event_OnDelete;
31  protected ref ScriptInvoker<SCR_AddonLinePresetComponent> Event_OnButtonClick;
32 
33  //------------------------------------------------------------------------------------------------
34  protected void InvokeEventOnButtonClick()
35  {
36  if (Event_OnButtonClick)
37  Event_OnButtonClick.Invoke(this);
38  }
39 
40  //------------------------------------------------------------------------------------------------
41  ScriptInvoker GetEventOnButtonClick()
42  {
43  if (!Event_OnButtonClick)
44  Event_OnButtonClick = new ScriptInvoker();
45 
46  return Event_OnButtonClick;
47  }
48 
49  //------------------------------------------------------------------------------------------------
50  protected void InvokeEventOnNameChanged(string arg0)
51  {
52  if (Event_OnNameChanged)
53  Event_OnNameChanged.Invoke(this, arg0);
54  }
55 
56  //------------------------------------------------------------------------------------------------
57  ScriptInvoker GetEventOnNameChanged()
58  {
59  if (!Event_OnNameChanged)
60  Event_OnNameChanged = new ScriptInvoker();
61 
62  return Event_OnNameChanged;
63  }
64 
65  //------------------------------------------------------------------------------------------------
66  protected void InvokeEventOnNameEditStart()
67  {
68  if (Event_OnNameEditStart)
69  Event_OnNameEditStart.Invoke(this);
70 
71  InvokeEventOnButtonClick();
72  }
73 
74  //------------------------------------------------------------------------------------------------
75  ScriptInvoker GetEventOnNameEditStart()
76  {
77  if (!Event_OnNameEditStart)
78  Event_OnNameEditStart = new ScriptInvoker();
79 
80  return Event_OnNameEditStart;
81  }
82 
83  //------------------------------------------------------------------------------------------------
84  protected void InvokeEventOnNameEditLeave()
85  {
86  if (Event_OnNameEditLeave)
87  Event_OnNameEditLeave.Invoke(this);
88  }
89 
90  //------------------------------------------------------------------------------------------------
91  ScriptInvoker GetEventOnNameEditLeave()
92  {
93  if (!Event_OnNameEditLeave)
94  Event_OnNameEditLeave = new ScriptInvoker();
95 
96  return Event_OnNameEditLeave;
97  }
98 
99  //------------------------------------------------------------------------------------------------
100  protected void InvokeEventOnFocus()
101  {
102  if (Event_OnFocus)
103  Event_OnFocus.Invoke(this);
104  }
105 
106  //------------------------------------------------------------------------------------------------
107  ScriptInvoker GetEventOnFocus()
108  {
109  if (!Event_OnFocus)
110  Event_OnFocus = new ScriptInvoker();
111 
112  return Event_OnFocus;
113  }
114 
115  //------------------------------------------------------------------------------------------------
116  protected void InvokeEventOnFocusLost()
117  {
118  if (Event_OnFocusLost)
119  Event_OnFocusLost.Invoke(this);
120  }
121 
122  //------------------------------------------------------------------------------------------------
123  ScriptInvoker GetEventOnFocusLost()
124  {
125  if (!Event_OnFocusLost)
126  Event_OnFocusLost = new ScriptInvoker();
127 
128  return Event_OnFocusLost;
129  }
130 
131  //------------------------------------------------------------------------------------------------
132  protected void InvokeEventOnLoad()
133  {
134  if (Event_OnLoad)
135  Event_OnLoad.Invoke(this);
136 
137  InvokeEventOnButtonClick();
138  }
139 
140  //------------------------------------------------------------------------------------------------
141  ScriptInvoker GetEventOnLoad()
142  {
143  if (!Event_OnLoad)
144  Event_OnLoad = new ScriptInvoker();
145 
146  return Event_OnLoad;
147  }
148 
149  //------------------------------------------------------------------------------------------------
150  protected void InvokeEventOnOverride()
151  {
152  if (Event_OnOverride)
153  Event_OnOverride.Invoke(this);
154 
155  InvokeEventOnButtonClick();
156  }
157 
158  //------------------------------------------------------------------------------------------------
159  ScriptInvoker GetEventOnOverride()
160  {
161  if (!Event_OnOverride)
162  Event_OnOverride = new ScriptInvoker();
163 
164  return Event_OnOverride;
165  }
166 
167  //------------------------------------------------------------------------------------------------
168  protected void InvokeEventOnDelete()
169  {
170  if (Event_OnDelete)
171  Event_OnDelete.Invoke(this);
172 
173  InvokeEventOnButtonClick();
174  }
175 
176  //------------------------------------------------------------------------------------------------
177  ScriptInvoker GetEventOnDelete()
178  {
179  if (!Event_OnDelete)
180  Event_OnDelete = new ScriptInvoker();
181 
182  return Event_OnDelete;
183  }
184 
185  //------------------------------------------------------------------------------------------------
186  // Override widget functions
187  //------------------------------------------------------------------------------------------------
188 
189  //------------------------------------------------------------------------------------------------
190  override void HandlerAttached(Widget w)
191  {
192  super.HandlerAttached(w);
193 
194  m_Widgets.Init(w);
195 
196  ShowEditWidget(false);
197  ShowButtons(false);
198 
199  m_ButtonComponent = SCR_ModularButtonComponent.FindComponent(w);
200 
201  // Buttons and actions
202  m_Widgets.m_EditNameButtonComponent.m_OnClicked.Insert(StartEditName);
203  m_Widgets.m_LoadButtonComponent.m_OnClicked.Insert(InvokeEventOnLoad);
204  m_Widgets.m_OverrideButtonComponent.m_OnClicked.Insert(InvokeEventOnOverride);
205  m_Widgets.m_DeleteButtonComponent.m_OnClicked.Insert(InvokeEventOnDelete);
206 
207  SCR_MenuHelper.GetOnDialogClose().Insert(OnDialogClose);
208  }
209 
210  //------------------------------------------------------------------------------------------------
211  override void HandlerDeattached(Widget w)
212  {
213  super.HandlerDeattached(w);
214 
215  SCR_MenuHelper.GetOnDialogClose().Remove(OnDialogClose);
216  }
217 
218  //------------------------------------------------------------------------------------------------
219  override bool OnDoubleClick(Widget w, int x, int y, int button)
220  {
221  super.OnDoubleClick(w, x, y, button);
222 
223  if (button == 0)
224  InvokeEventOnLoad();
225 
226  return true;
227  }
228 
229  //------------------------------------------------------------------------------------------------
230  override bool OnFocus(Widget w, int x, int y)
231  {
232  super.OnFocus(w, x, y);
233  ShowButtons(true);
234  InvokeEventOnFocus();
235  return true;
236  }
237 
238  //------------------------------------------------------------------------------------------------
239  override bool OnFocusLost(Widget w, int x, int y)
240  {
241  super.OnFocusLost(w, x, y);
242  InvokeEventOnFocusLost();
243  ShowButtons(false);
244  return true;
245  }
246 
247  //------------------------------------------------------------------------------------------------
248  // Hide the inline buttons when another dialog (ie save or delete confirmation) is closed
249  protected void OnDialogClose(DialogUI dialog)
250  {
251  ShowButtons(false);
252  }
253 
254  //------------------------------------------------------------------------------------------------
255  // Functions
256  //------------------------------------------------------------------------------------------------
257 
258  //------------------------------------------------------------------------------------------------
260  void StartEditName()
261  {
262  if (!m_Widgets.m_EditName)
263  {
264  #ifdef DEBUG_WORKSHOP
265  Print("Missing m_NameEdit!");
266  #endif
267 
268  return;
269  }
270 
271  // Show edit
272  // This will prevent the edit box from immediately closing due to the button refocusing
273  if (m_ButtonComponent)
274  m_ButtonComponent.SetIsFocusOnMouseEnter(false);
275 
276  ShowEditWidget(true);
277  GetGame().GetWorkspace().SetFocusedWidget(m_Widgets.m_EditNameComponent.GetEditBoxWidget());
278 
279  m_Widgets.m_EditNameComponent.SetValue(m_Widgets.m_NameText.GetText());
280  m_Widgets.m_EditNameComponent.ActivateWriteMode();
281 
282  // Actions
283  // Edit action
284  GetGame().GetInputManager().AddActionListener(UIConstants.MENU_ACTION_BACK, EActionTrigger.PRESSED, OnEditNameCancel);
285 
286  m_Widgets.m_EditNameComponent.m_OnConfirm.Insert(OnEditNameConfirm); //TODO: this does not get called on gamepad
287  m_Widgets.m_EditNameComponent.m_OnFocusChangedEditBox.Insert(OnEditNameCancel);
288 
289  InvokeEventOnNameEditStart();
290  }
291 
292  //------------------------------------------------------------------------------------------------
293  protected void ShowEditWidget(bool edit)
294  {
295  if (m_Widgets.m_NameText)
296  m_Widgets.m_NameText.SetVisible(!edit);
297 
298  if (m_Widgets.m_EditName)
299  m_Widgets.m_EditName.SetVisible(edit);
300 
301  ShowButtons(!edit);
302  }
303 
304  //------------------------------------------------------------------------------------------------
305  protected void RemoveEditActions()
306  {
307  m_Widgets.m_EditNameComponent.m_OnConfirm.Remove(OnEditNameConfirm);
308  m_Widgets.m_EditNameComponent.m_OnFocusChangedEditBox.Remove(OnEditNameCancel);
309 
310  m_Widgets.m_EditNameComponent.ClearInteractionState();
311  }
312 
313  //------------------------------------------------------------------------------------------------
314  protected void OnEditNameConfirm()
315  {
316  EditNameFinishedStateReset();
317 
318  InvokeEventOnNameChanged(m_Widgets.m_EditNameComponent.GetValue());
319  RemoveEditActions();
320 
321  if (m_bSelected)
322  InvokeEventOnLoad();
323  }
324 
325  //------------------------------------------------------------------------------------------------
326  protected void OnEditNameCancel()
327  {
328  // If the focus change was a result of clicking the same line we're editing, then consider it a confirmation
329  // This is also necessary to register confirmation with gamepad
330  if (GetGame().GetWorkspace().GetFocusedWidget() == GetRootWidget())
331  {
332  OnEditNameConfirm();
333  return;
334  }
335 
336  EditNameFinishedStateReset();
337 
338  ShowEditWidget(false);
339  RemoveEditActions();
340 
341  InvokeEventOnNameEditLeave();
342  }
343 
344  //------------------------------------------------------------------------------------------------
345  protected void EditNameFinishedStateReset()
346  {
347  GetGame().GetInputManager().RemoveActionListener(UIConstants.MENU_ACTION_BACK, EActionTrigger.PRESSED, OnEditNameCancel);
348 
349  // Reactivate focus on mouse enter
350  if (m_ButtonComponent)
351  m_ButtonComponent.SetIsFocusOnMouseEnter(true);
352  }
353 
354  //------------------------------------------------------------------------------------------------
355  protected void ShowButtons(bool show)
356  {
357  if (m_Widgets.m_OverrideButton)
358  m_Widgets.m_OverrideButton.SetVisible(show);
359 
360  if (m_Widgets.m_EditNameButton)
361  m_Widgets.m_EditNameButton.SetVisible(show);
362 
363  if (m_Widgets.m_DeleteButton)
364  m_Widgets.m_DeleteButton.SetVisible(show);
365  }
366 
367  //------------------------------------------------------------------------------------------------
369  protected string DefaultName()
370  {
371  int count = SCR_AddonManager.GetInstance().GetPresetStorage().GetPresets().Count();
372  string name = STR_DEFAULT_NAME + count;
373 
374  // Is name unique
375  int fallback = 100;
376 
377  while (name || fallback > 0)
378  {
379  if (!SCR_AddonManager.GetInstance().GetPresetStorage().PresetExists(name))
380  {
381  return name;
382  }
383  else
384  {
385  count++;
386  name = STR_DEFAULT_NAME + count;
387 
388  fallback++;
389  }
390  }
391 
392  return "";
393  }
394 
395  //------------------------------------------------------------------------------------------------
396  // Api
397  //------------------------------------------------------------------------------------------------
398 
399  //------------------------------------------------------------------------------------------------
400  SCR_WorkshopAddonPreset GetPreset()
401  {
402  return m_Preset;
403  }
404 
405  //------------------------------------------------------------------------------------------------
406  void SetPreset(SCR_WorkshopAddonPreset preset)
407  {
408  m_Preset = preset;
409  }
410 
411  //------------------------------------------------------------------------------------------------
412  void UpdateWidgets()
413  {
414  ShowModCount(m_Preset.GetAddonCount());
415  }
416 
417  //------------------------------------------------------------------------------------------------
418  void ShowWarning(bool show)
419  {
420  m_Widgets.m_SizeWarning.SetVisible(show);
421  }
422 
423  //------------------------------------------------------------------------------------------------
424  void ShowModCount(int count)
425  {
426  m_Widgets.m_TxtAddonsCount.SetText(count.ToString());
427  }
428 
429  //---------------------------------------------------------------------------------------------------
430  void ShowDefaultName()
431  {
432  m_Widgets.m_NameText.SetText(DefaultName());
433  }
434 
435  //------------------------------------------------------------------------------------------------
436  string GetName()
437  {
438  return m_Widgets.m_NameText.GetText();
439  }
440 
441  //------------------------------------------------------------------------------------------------
442  void SetSelected(bool selected)
443  {
444  if (m_ButtonComponent)
445  m_ButtonComponent.SetToggled(selected);
446 
447  if (m_bSelected == selected)
448  return;
449 
450  m_bSelected = selected;
451 
452  if (m_bSelected)
453  m_Widgets.m_NameText.SetColor(m_cSelectedColor);
454  else
455  m_Widgets.m_NameText.SetColor(m_cDefaultColor);
456  }
457 
458  //------------------------------------------------------------------------------------------------
459  bool GetSelected()
460  {
461  return m_bSelected;
462  }
463 }
SCR_WLibComponentBase
Base class for all final Reforger interactive elements.
Definition: SCR_WLibComponentBase.c:4
UIConstants
Definition: Constants.c:130
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_Widgets
ref SCR_VoNOverlay_ElementWidgets m_Widgets
Definition: SCR_VonDisplay.c:3
GetRootWidget
Widget GetRootWidget()
Definition: SCR_UITaskManagerComponent.c:160
Attribute
typedef Attribute
Post-process effect of scripted camera.
UIColors
Definition: Constants.c:16
SCR_AddonLinePresetWidgets
Definition: SCR_AddonLinePresetWidgets.c:4
SCR_AddonLinePresetComponent
Definition: SCR_AddonLinePresetComponent.c:1
m_Preset
ref SCR_ScriptedWidgetTooltipPreset m_Preset
Definition: SCR_BrowserHoverTooltipComponent.c:3
DialogUI
Definition: DialogUI.c:1
SCR_MenuHelper
Definition: SCR_MenuHelper.c:15
m_bSelected
protected bool m_bSelected
Definition: SCR_InventoryHitZonePointUI.c:396
SCR_AddonManager
Definition: SCR_AddonManager.c:72