Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_DynamicFooterComponent.c
Go to the documentation of this file.
1 /*
2 Component for menu footers that handles navigation buttons, allowing quick and easy setup.
3 TODO: ideally, this should support different types of buttons, once we make them derive from a common class
4 */
5 
6 //------------------------------------------------------------------------------------------------
8 {
9  [Attribute("20", desc: "Padding between buttons")]
10  protected float m_fPadding;
11 
12  [Attribute("Footer", desc: "Left footer name")]
13  protected string m_sLeftFooter;
14 
15  [Attribute("FooterRight", desc: "Right footer name")]
16  protected string m_sRightFooter;
17 
18  protected ref map<string, SCR_InputButtonComponent> m_Buttons = new map<string, SCR_InputButtonComponent>();
19 
20  protected Widget m_wLeftFooter;
21  protected Widget m_wRightFooter;
22 
23  protected ref ScriptInvokerString m_OnButtonActivated;
24 
25  // ---- OVERRIDES ----
26  //------------------------------------------------------------------------------------------------
27  override void HandlerAttached(Widget w)
28  {
29  super.HandlerAttached(w);
30 
32  Widget root = w.GetParent();
33  if (!root)
34  root = w;
35 
36  m_wLeftFooter = root.FindAnyWidget(m_sLeftFooter);
37  m_wRightFooter = root.FindAnyWidget(m_sRightFooter);
38 
39  array<SCR_InputButtonComponent> buttons = SCR_DynamicFooterComponent.FindChildrenButtons(w, false);
40 
41  foreach (SCR_InputButtonComponent button : buttons)
42  {
43  RegisterButton(button);
44  }
45 
46  UpdateAllButtonsPadding(m_wLeftFooter);
47  UpdateAllButtonsPadding(m_wRightFooter, SCR_EDynamicFooterButtonAlignment.RIGHT);
48  }
49 
50 
51  // ---- PRIVATE ----
52  //------------------------------------------------------------------------------------------------
54  private void OnButtonActivated(SCR_InputButtonComponent button)
55  {
56  if(m_OnButtonActivated)
57  m_OnButtonActivated.Invoke(button.GetRootWidget().GetName());
58  }
59 
60  //------------------------------------------------------------------------------------------------
61  private void UpdateAllButtonsPadding(Widget wrapper, SCR_EDynamicFooterButtonAlignment alignment = 0)
62  {
63  if (!wrapper)
64  return;
65 
66  array<SCR_InputButtonComponent> buttons = SCR_DynamicFooterComponent.FindChildrenButtons(wrapper);
67 
68  foreach (SCR_InputButtonComponent button : buttons)
69  {
70  UpdateButtonPadding(button, alignment);
71  }
72  }
73 
74  //------------------------------------------------------------------------------------------------
75  private void UpdateButtonPadding(SCR_InputButtonComponent button, SCR_EDynamicFooterButtonAlignment alignment = 0)
76  {
77  if (!button)
78  return;
79 
80  float paddingRight, paddingLeft;
81  float left, top, right, bottom;
82 
83  switch (alignment)
84  {
85  case SCR_EDynamicFooterButtonAlignment.LEFT:
86  paddingRight = m_fPadding;
87  break;
88 
89  case SCR_EDynamicFooterButtonAlignment.RIGHT:
90  paddingLeft = m_fPadding;
91  break;
92  }
93 
94  Widget widget = button.GetRootWidget();
95  AlignableSlot.GetPadding(widget, left, top, right, bottom);
96 
97  AlignableSlot.SetPadding(widget, paddingLeft, top, paddingRight, bottom);
98  }
99 
100  // ---- PUBLIC ----
101  //------------------------------------------------------------------------------------------------
102  SCR_InputButtonComponent CreateButton(ResourceName layout, string tag, string label, string action, SCR_EDynamicFooterButtonAlignment alignment = 0, bool visible = true)
103  {
105  Widget wrapper;
106  switch (alignment)
107  {
108  case SCR_EDynamicFooterButtonAlignment.LEFT:
109  wrapper = m_wLeftFooter;
110  break;
111 
112  case SCR_EDynamicFooterButtonAlignment.RIGHT:
113  wrapper = m_wRightFooter;
114  break;
115  }
116 
118  Widget buttonWidget = GetGame().GetWorkspace().CreateWidgets(layout, wrapper);
119  if (!buttonWidget)
120  return null;
121 
122  SCR_InputButtonComponent button = SCR_InputButtonComponent.FindComponent(buttonWidget);
123  if (!button)
124  return null;
125 
127  RegisterButton(button);
128  UpdateButtonPadding(button, alignment);
129 
130  buttonWidget.SetName(tag);
131 
132  button.SetVisible(visible, false);
133  button.SetLabel(label);
134  button.SetAction(action);
135 
137  if (m_wRightFooter && wrapper == m_wLeftFooter)
138  m_wRightFooter.SetZOrder(buttonWidget.GetZOrder() + 1);
139 
140  return button;
141  }
142 
143  //------------------------------------------------------------------------------------------------
144  SCR_InputButtonComponent CreateButtonAtPosition(ResourceName layout, string tag, string label, string action, int position, SCR_EDynamicFooterButtonAlignment alignment = 0, bool visible = true)
145  {
147  Widget wrapper;
148  switch (alignment)
149  {
150  case SCR_EDynamicFooterButtonAlignment.LEFT:
151  wrapper = m_wLeftFooter;
152  break;
153 
154  case SCR_EDynamicFooterButtonAlignment.RIGHT:
155  wrapper = m_wRightFooter;
156  break;
157  }
158 
160  array<ref Widget> existingButtons = {};
161  SCR_WidgetHelper.GetAllChildren(wrapper, existingButtons);
162  int count;
163 
164  foreach (Widget existingButton : existingButtons)
165  {
166  if (count == position)
167  count++; //Skip what will be the position of the new button
168 
169  existingButton.SetZOrder(count);
170  count++;
171  }
172 
174  SCR_InputButtonComponent button = CreateButton(layout, tag, label, action, alignment, visible);
175  if (button)
176  button.GetRootWidget().SetZOrder(position);
177 
179  if (wrapper == m_wLeftFooter)
180  m_wRightFooter.SetZOrder(button.GetRootWidget().GetZOrder() + 1);
181 
182  UpdateButtonPadding(button, alignment);
183 
184  return button;
185  }
186 
187  //------------------------------------------------------------------------------------------------
189  void RegisterButton(SCR_InputButtonComponent button)
190  {
191  Widget w = button.GetRootWidget();
192  m_Buttons.Insert(w.GetName(), button);
193 
194  button.m_OnActivated.Insert(OnButtonActivated);
195  }
196 
197  //------------------------------------------------------------------------------------------------
199  SCR_InputButtonComponent FindButton(string tag)
200  {
201  return m_Buttons.Get(tag);
202  }
203 
204  //----------------------------------------------------------------------------------------
206  string GetButtonTag(SCR_InputButtonComponent button)
207  {
208  if(!button)
209  return string.Empty;
210 
211  return m_Buttons.GetKeyByValue(button);
212  }
213 
214  //------------------------------------------------------------------------------------------------
216  bool RemoveButton(string tag)
217  {
218  SCR_InputButtonComponent button = FindButton(tag);
219 
220  if (!button)
221  return false;
222 
223  m_Buttons.Remove(tag);
224  m_wRoot.RemoveChild(button.GetRootWidget());
225 
226  return true;
227  }
228 
229  //------------------------------------------------------------------------------------------------
231  void ClearButtons()
232  {
233  foreach (SCR_InputButtonComponent comp : m_Buttons)
234  {
235  m_wRoot.RemoveChild(comp.GetRootWidget());
236  }
237  m_Buttons.Clear();
238  }
239 
240  //------------------------------------------------------------------------------------------------
242  array<SCR_InputButtonComponent> GetButtonsInFooter(SCR_EDynamicFooterButtonAlignment alignment)
243  {
244  Widget wrapper;
245  switch (alignment)
246  {
247  case SCR_EDynamicFooterButtonAlignment.LEFT:
248  wrapper = m_wLeftFooter;
249  break;
250 
251  case SCR_EDynamicFooterButtonAlignment.RIGHT:
252  wrapper = m_wRightFooter;
253  break;
254  }
255 
256  if (!wrapper)
257  return null;
258 
259  return SCR_DynamicFooterComponent.FindChildrenButtons(wrapper);
260  }
261 
262  //------------------------------------------------------------------------------------------------
263  Widget GetLeftFooter()
264  {
265  return m_wLeftFooter;
266  }
267 
268  //------------------------------------------------------------------------------------------------
269  Widget GetRightFooter()
270  {
271  return m_wRightFooter;
272  }
273 
274  //------------------------------------------------------------------------------------------------
276  ScriptInvokerString GetOnButtonActivated()
277  {
278  if (!m_OnButtonActivated)
279  m_OnButtonActivated = new ScriptInvokerString();
280 
281  return m_OnButtonActivated;
282  }
283 
284 
285  // ---- STATIC ----
286  //------------------------------------------------------------------------------------------------
287  static array<SCR_InputButtonComponent> FindChildrenButtons(notnull Widget w, bool searchImmediateChildrenOnly = true)
288  {
289  array<SCR_InputButtonComponent> buttons = {};
290  array<ref Widget> children = {};
291 
292  SCR_WidgetHelper.GetAllChildren(w, children, !searchImmediateChildrenOnly);
293 
294  foreach (Widget child : children)
295  {
296  SCR_InputButtonComponent comp = SCR_InputButtonComponent.FindComponent(child);
297  if (comp)
298  buttons.Insert(comp);
299  }
300 
301  return buttons;
302  }
303 
304  //------------------------------------------------------------------------------------------------
305  static SCR_DynamicFooterComponent FindComponentInHierarchy(notnull Widget root)
306  {
308 
309  ScriptedWidgetEventHandler handler = SCR_WidgetTools.FindHandlerInChildren(root, SCR_DynamicFooterComponent);
310  if (handler)
311  comp = SCR_DynamicFooterComponent.Cast(handler);
312 
313  return comp;
314  }
315 
316  //------------------------------------------------------------------------------------------------
317  static SCR_DynamicFooterComponent FindComponent(notnull Widget w)
318  {
319  return SCR_DynamicFooterComponent.Cast(w.FindHandler(SCR_DynamicFooterComponent));
320  }
321 }
322 
323 enum SCR_EDynamicFooterButtonAlignment
324 {
325  LEFT = 0,
326  RIGHT
327 }
SCR_WidgetTools
Definition: SCR_WidgetTools.c:1
RIGHT
SCR_AISmartActionSentinelComponentClass RIGHT
Definition: SCR_ScriptedWidgetTooltip.c:779
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
desc
UI Textures DeployMenu Briefing conflict_HintBanner_1_UI desc
Definition: SCR_RespawnBriefingComponent.c:17
SCR_WidgetHelper
Definition: SCR_WidgetHelper.c:1
Attribute
typedef Attribute
Post-process effect of scripted camera.
ScriptInvokerString
ScriptInvokerBase< ScriptInvokerStringMethod > ScriptInvokerString
Definition: SCR_ScriptInvokerHelper.c:75
layout
UI layouts HUD CampaignMP CampaignMainHUD layout
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:20
SCR_ScriptedWidgetComponent
Definition: SCR_ScriptedWidgetComponent.c:7
position
vector position
Definition: SCR_DestructibleTreeV2.c:30
SCR_InputButtonComponent
Definition: SCR_InputButtonComponent.c:1