Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_CustomDropdownEditorUIComponent.c
Go to the documentation of this file.
1 class SCR_CustomDropdownEditorUIComponent : ScriptedWidgetComponent
2 {
3  //TODO: Check how to close the drop down if anything else is clicked (How does the combobox work?)
4  [Attribute("ItemHolder")]
5  protected string m_sItemHolderName;
6 
7  [Attribute("ModeSelection_List0")]
8  protected string m_sListWidgetName;
9 
10  [Attribute("Stripe")]
11  protected string m_sListWidgetStripeName;
12 
13  [Attribute("ModeSelection_Mode0")]
14  protected string m_sDropdownButtonName;
15 
16  [Attribute("Content")]
17  protected string m_DropdownTextName;
18 
19  [Attribute("PcArrow")]
20  protected string m_sArrowWidgetName;
21 
22  [Attribute("GamepadHint")]
23  protected string m_sGamepadHintWidgetName;
24 
25  [Attribute()]
26  protected ResourceName m_sItemPrefab;
27 
28  [Attribute("0.025", "Fade delay in seconds. For every new button that is added the delay increases slightly")]
29  protected float m_fFadeDelayNextButton;
30 
31  [Attribute("4", "How fast each button appears")]
32  protected float m_fButtonFadeSpeed;
33 
34  [Attribute("-1", desc: "How much will the list of controls hints be offset vertically.\nKeep -1 to leave it unaffected.")]
35  protected int m_iAvailableActionsOffsetY;
36 
37  //Refs
38  protected Widget m_Root;
39  protected Widget m_ItemHolder;
40  protected Widget m_ListWidget;
41  protected SCR_FadeUIComponent m_ListWidgetStripeFadeComponent;
42  protected TextWidget m_DropdownText;
43  protected ImageWidget m_ArrowWidget;
44  protected SCR_ButtonImageComponent m_DropdownButton;
45  protected ref array<SCR_ButtonImageComponent> m_aItemButtons = {};
46  protected ref array<SCR_EditorModeUIInfo> m_aModeUIInfo = {};
47 
48  //States
49  protected bool m_bIsOpened;
50  protected int m_iSelectedItem;
51  protected bool m_bHovered;
52 
53  protected ref ScriptInvoker Event_OnDropdownOpened;
54  protected ref ScriptInvoker Event_OnDropdownClosed;
55  protected ref ScriptInvoker Event_OnChanged;
56 
57  protected int m_iCloseOnIndex;
58  protected bool m_bIsEnabled = true;
59 
60  //------------------------------------------------------------------------------------------------
63  void SetDropdownEnable(bool enable)
64  {
65  m_bIsEnabled = enable;
66 
67  if (m_bIsOpened)
68  CloseDropdown();
69 
70  m_Root.SetEnabled(enable);
71 
72  if (enable)
73  {
74  OnInputDeviceIsGamepad(!GetGame().GetInputManager().IsUsingMouseAndKeyboard());
75  }
76  else
77  {
78  m_ArrowWidget.SetVisible(false);
79 
80  Widget gamePadhintWidget = m_Root.FindAnyWidget(m_sGamepadHintWidgetName);
81  if (gamePadhintWidget)
82  gamePadhintWidget.SetVisible(false);
83  }
84 
85  }
86 
87  //------------------------------------------------------------------------------------------------
88  protected void OnDropDownClicked(SCR_ButtonImageComponent button)
89  {
90  if (m_bIsOpened)
91  CloseDropdown();
92  else
93  OpenDropdown();
94  }
95 
96  //------------------------------------------------------------------------------------------------
97  protected void OnItemClicked(SCR_ButtonImageComponent button)
98  {
99  int index = m_aItemButtons.Find(button);
100 
101  if (index < 0)
102  return;
103 
104  SetCurrentItem(index, true);
105  }
106 
107  //------------------------------------------------------------------------------------------------
108  protected void OnDropDownFocus()
109  {
110  if (!m_bIsOpened)
111  OpenDropdown();
112 
113  GetGame().GetWorkspace().SetFocusedWidget(m_aItemButtons[0].GetRootWidget());
114  }
115 
116  //------------------------------------------------------------------------------------------------
117  protected void OnMenuActionLeft()
118  {
119  CloseDropdown();
120  }
121 
122  //------------------------------------------------------------------------------------------------
126  void AddItem(SCR_EditorModeUIInfo uiInfo, Color color)
127  {
128  if (!m_ItemHolder)
129  return;
130 
131  Widget itemWidget = GetGame().GetWorkspace().CreateWidgets(m_sItemPrefab, m_ItemHolder);
132  if (!itemWidget)
133  return;
134 
135  SCR_ButtonImageComponent newItem = SCR_ButtonImageComponent.Cast(itemWidget.FindHandler(SCR_ButtonImageComponent));
136 
137  if (newItem)
138  {
139  newItem.m_OnClicked.Insert(OnItemClicked);
140  m_aItemButtons.Insert(newItem);
141  m_aModeUIInfo.Insert(uiInfo);
142 
143  uiInfo.SetIconTo(newItem.GetImageWidget());
144  newItem.GetImageWidget().SetVisible(true);
145 
146  TextWidget textWidget = TextWidget.Cast(newItem.GetRootWidget().FindAnyWidget("Text"));
147  if (textWidget)
148  uiInfo.SetNameTo(textWidget);
149 
150  Widget sideBar = newItem.GetRootWidget().FindAnyWidget("SideBar");
151 
152  if (sideBar)
153  sideBar.SetColor(uiInfo.GetModeColor());
154 
156  if (tooltip)
157  tooltip.SetInfo(uiInfo);
158  }
159  else
160  {
161  itemWidget.RemoveFromHierarchy();
162  }
163  }
164 
165  //------------------------------------------------------------------------------------------------
169  bool IsOpened()
170  {
171  return m_bIsOpened;
172  }
173 
174  //------------------------------------------------------------------------------------------------
177  void OpenDropdown(int focusIndex =-1)
178  {
179  float fadeDelay = 0;
180  SCR_FadeUIComponent fadeComponent
181  foreach (SCR_ButtonImageComponent button: m_aItemButtons)
182  {
183  if (!button.GetRootWidget().IsVisible())
184  continue;
185 
186  fadeComponent = SCR_FadeUIComponent.Cast(button.GetRootWidget().FindHandler(SCR_FadeUIComponent));
187  if (fadeComponent)
188  {
189  fadeComponent.SetFadeInSpeed(m_fButtonFadeSpeed);
190  fadeComponent.DelayedFadeIn(fadeDelay * 1000);
191  fadeDelay += m_fFadeDelayNextButton;
192  }
193  }
194 
195  if (m_ListWidgetStripeFadeComponent)
196  m_ListWidgetStripeFadeComponent.DelayedFadeIn(fadeDelay * 1000);
197 
198  m_ListWidget.SetVisible(true);
199 
200  // Set arrow image angle
201  if (m_ArrowWidget)
202  m_ArrowWidget.SetRotation(90);
203 
204  if (m_iAvailableActionsOffsetY != -1)
205  {
207  if (availableActionsDisplay)
208  availableActionsDisplay.SetAdditionalOffsetY(m_iAvailableActionsOffsetY);
209  }
210 
211  //Rotate arrow
212 
213  m_bIsOpened = true;
214  if (Event_OnDropdownOpened)
215  Event_OnDropdownOpened.Invoke(this);
216 
217  if (focusIndex > -1)
218  {
219  WorkspaceWidget workspace = GetGame().GetWorkspace();
220  if (!workspace)
221  return;
222 
223  workspace.SetFocusedWidget(m_aItemButtons[focusIndex].GetRootWidget());
224  }
225 
226  // Setup action listeners
227  GetGame().GetInputManager().AddActionListener(UIConstants.MENU_ACTION_LEFT, EActionTrigger.DOWN, OnMenuActionLeft);
228  }
229 
230  //------------------------------------------------------------------------------------------------
232  void CloseDropdown()
233  {
234  m_ListWidget.SetVisible(false);
235 
236  // Set arrow image angle
237  if (m_ArrowWidget)
238  m_ArrowWidget.SetRotation(270);
239 
240  if (m_iAvailableActionsOffsetY != -1)
241  {
243  if (availableActionsDisplay)
244  availableActionsDisplay.SetAdditionalOffsetY(0);
245  }
246 
247  m_bIsOpened = false;
248  if (Event_OnDropdownClosed)
249  Event_OnDropdownClosed.Invoke(this);
250 
251  SCR_FadeUIComponent fadeComponent;
252  foreach (SCR_ButtonImageComponent button: m_aItemButtons)
253  {
254  fadeComponent = SCR_FadeUIComponent.Cast(button.GetRootWidget().FindHandler(SCR_FadeUIComponent));
255  if (fadeComponent)
256  fadeComponent.CancelFade(false);
257  }
258 
259  if (m_ListWidgetStripeFadeComponent)
260  m_ListWidgetStripeFadeComponent.CancelFade(false);
261 
262  // Remove action listeners
263  GetGame().GetInputManager().RemoveActionListener(UIConstants.MENU_ACTION_LEFT, EActionTrigger.DOWN, OnMenuActionLeft);
264  }
265 
266  protected void OnLMB()
267  {
268  if (m_bIsOpened && !m_bHovered)
269  CloseDropdown();
270  }
271 
272  //------------------------------------------------------------------------------------------------
276  void SetCurrentItem(int index, bool callOnChanged)
277  {
278  m_iSelectedItem = index;
279 
280  if (callOnChanged && Event_OnChanged)
281  Event_OnChanged.Invoke(this, index);
282 
283  if (!m_aModeUIInfo.IsIndexValid(index))
284  return;
285 
286  SCR_EditorModeUIInfo uiInfo = m_aModeUIInfo[index];
287 
288  if (m_DropdownText)
289  uiInfo.SetNameTo(m_DropdownText);
290 
291  if (m_DropdownButton)
292  uiInfo.SetIconTo(m_DropdownButton.GetImageWidget());
293  }
294 
295  //------------------------------------------------------------------------------------------------
298  int GetSelectedIndex()
299  {
300  return m_iSelectedItem;
301  }
302 
303 // //------------------------------------------------------------------------------------------------
304 // //! Clear all elements
305 // void ClearAll()
306 // {
307 // }
308 
309  //------------------------------------------------------------------------------------------------
311  Widget GetRootWidget()
312  {
313  return m_Root;
314  }
315 
316  //------------------------------------------------------------------------------------------------
320  void SetItemEnabled(int index, bool enabled)
321  {
322  if (index < 0 || index >= m_aItemButtons.Count())
323  return;
324 
325  m_aItemButtons[index].GetRootWidget().SetEnabled(enabled);
326  }
327 
328  //------------------------------------------------------------------------------------------------
333  void SetItemVisible(int index, bool visible)
334  {
335  if (index < 0 || index >= m_aItemButtons.Count())
336  return;
337 
338  m_aItemButtons[index].GetRootWidget().SetVisible(visible);
339  }
340 
342 
343  //------------------------------------------------------------------------------------------------
345  ScriptInvoker GetOnChanged()
346  {
347  if (!Event_OnChanged)
348  Event_OnChanged = new ScriptInvoker();
349 
350  return Event_OnChanged;
351  }
352 
353  //------------------------------------------------------------------------------------------------
355  ScriptInvoker GetOnOpened()
356  {
357  if (!Event_OnDropdownOpened)
358  Event_OnDropdownOpened = new ScriptInvoker();
359 
360  return Event_OnDropdownOpened;
361  }
362 
363  //------------------------------------------------------------------------------------------------
365  ScriptInvoker GetOnClosed()
366  {
367  if (!Event_OnDropdownClosed)
368  Event_OnDropdownClosed = new ScriptInvoker();
369 
370  return Event_OnDropdownClosed;
371  }
372 
373  //------------------------------------------------------------------------------------------------
374  protected void OnInputDeviceIsGamepad(bool isGamepad)
375  {
376  if (!m_bIsEnabled)
377  return;
378 
379  m_ArrowWidget.SetVisible(!isGamepad);
380 
381  Widget gamePadhintWidget = m_Root.FindAnyWidget(m_sGamepadHintWidgetName);
382  if (gamePadhintWidget)
383  gamePadhintWidget.SetVisible(isGamepad);
384  }
385 
386  //------------------------------------------------------------------------------------------------
387  override bool OnMouseEnter(Widget w, int x, int y)
388  {
389  m_bHovered = true;
390  return false;
391  }
392 
393  //------------------------------------------------------------------------------------------------
394  override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
395  {
396  m_bHovered = false;
397  return false;
398  }
399 
400  //------------------------------------------------------------------------------------------------
401  override void HandlerAttached(Widget w)
402  {
403  if (SCR_Global.IsEditMode())
404  return;
405 
406  GetGame().GetInputManager().AddActionListener("MouseLeft", EActionTrigger.DOWN, OnLMB);
407  m_Root = w;
408  m_ItemHolder = w.FindAnyWidget(m_sItemHolderName);
409  m_ListWidget = w.FindAnyWidget(m_sListWidgetName);
410  m_ArrowWidget = ImageWidget.Cast(w.FindAnyWidget(m_sArrowWidgetName));
411 
412  if (m_ListWidget)
413  {
414  Widget listWidgetStripe = m_ListWidget.FindAnyWidget(m_sListWidgetStripeName);
415 
416  if (listWidgetStripe)
417  {
418  m_ListWidgetStripeFadeComponent = SCR_FadeUIComponent.Cast(listWidgetStripe.FindHandler(SCR_FadeUIComponent));
419 
420  if (m_ListWidgetStripeFadeComponent)
421  m_ListWidgetStripeFadeComponent.SetFadeInSpeed(m_fButtonFadeSpeed);
422  }
423  }
424 
425 
426  Widget dropDownWidget = w.FindAnyWidget(m_sDropdownButtonName);
427  if (!dropDownWidget)
428  return;
429 
430  m_DropdownText = TextWidget.Cast(w.FindAnyWidget(m_DropdownTextName));
431  if (!m_DropdownText)
432  return;
433 
434  m_DropdownButton = SCR_ButtonImageComponent.Cast(dropDownWidget.FindHandler(SCR_ButtonImageComponent));
435  m_DropdownButton.m_OnClicked.Insert(OnDropDownClicked);
436  m_DropdownButton.m_OnFocus.Insert(OnDropDownFocus);
437 
438  ScriptInvoker invoker = GetGame().OnInputDeviceIsGamepadInvoker();
439  if (invoker)
440  invoker.Insert(OnInputDeviceIsGamepad);
441 
442  OnInputDeviceIsGamepad(!GetGame().GetInputManager().IsUsingMouseAndKeyboard());
443  }
444 
445  //------------------------------------------------------------------------------------------------
446  override void HandlerDeattached(Widget w)
447  {
448  ScriptInvoker invoker = GetGame().OnInputDeviceIsGamepadInvoker();
449  if (invoker)
450  invoker.Remove(OnInputDeviceIsGamepad);
451 
452  GetGame().GetInputManager().RemoveActionListener("MouseLeft", EActionTrigger.DOWN, OnLMB);
453  }
454 }
Event_OnChanged
ref ScriptInvoker Event_OnChanged
Definition: SCR_AccessKeysEditorComponent.c:18
m_Root
SCR_ServerHostingModSubMenu m_Root
SCR_FadeUIComponent
Definition: SCR_FadeUIComponent.c:1
UIConstants
Definition: Constants.c:130
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_CustomDropdownEditorUIComponent
Definition: SCR_CustomDropdownEditorUIComponent.c:1
m_bIsEnabled
protected bool m_bIsEnabled
Definition: SCR_BaseSupportStationComponent.c:97
index
SCR_DestructionSynchronizationComponentClass ScriptComponentClass int index
Definition: SCR_DestructionSynchronizationComponent.c:17
SCR_Global
Definition: Functions.c:6
GetInputManager
protected InputManager GetInputManager()
Definition: SCR_BaseManualCameraComponent.c:65
GetHUDManager
SCR_HUDManagerComponent GetHUDManager()
Definition: game.c:1196
SCR_ButtonImageComponent
Definition: SCR_ButtonImageComponent.c:2
SCR_AvailableActionsDisplay
Definition: SCR_AvailableActionsDisplay.c:237
SCR_EditorModeUIInfo
Definition: SCR_EditorModeUIInfo.c:2