Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SCR_ButtonBaseComponent.c
Go to the documentation of this file.
1 //------------------------------------------------------------------------------------------------
4 {
5  [Attribute("", UIWidgets.CheckBox, "Can the button be only clicked, or also toggled?")]
6  bool m_bCanBeToggled;
7 
8  [Attribute()]
9  protected bool m_bIsToggled;
10 
11  [Attribute("true", UIWidgets.CheckBox, "Use in built button colorization or handle it on your own")]
12  bool m_bUseColorization;
13 
14  [Attribute("1 1 1 0.1", UIWidgets.ColorPicker)]
15  ref Color m_BackgroundDefault;
16 
17  [Attribute("1 1 1 0.3", UIWidgets.ColorPicker)]
18  ref Color m_BackgroundHovered;
19 
20  [Attribute("0.760 0.392 0.08 0.3", UIWidgets.ColorPicker)]
21  ref Color m_BackgroundSelected;
22 
23  [Attribute("0.898 0.541 0.184 1", UIWidgets.ColorPicker)]
24  ref Color m_BackgroundSelectedHovered;
25 
26  [Attribute("0.898 0.541 0.184 1", UIWidgets.ColorPicker)]
27  ref Color m_BackgroundClicked;
28 
29  [Attribute("true")]
30  bool m_bShowBorderOnFocus;
31 
32  [Attribute("false")]
33  bool m_bShowBackgroundOnFocus;
34 
35  [Attribute("false")]
36  bool m_bNoBorderAnimation;
37 
38  [Attribute("false")]
39  bool m_bNoBackgroundAnimation;
40 
41  [Attribute()]
42  bool m_bShowBorderOnHover;
43 
44  Widget m_wBackground;
45  Widget m_wBorder;
46 
47  // Arguments passed: SCR_ButtonBaseComponent
48  ref ScriptInvoker m_OnClicked = new ScriptInvoker();
49  // Arguments passed: SCR_ButtonBaseComponent, bool (toggled)
50  ref ScriptInvoker m_OnToggled = new ScriptInvoker();
51 
52  // TODO: Rewrite - do not pass widget //Why? This is super useful!
53  ref ScriptInvoker<Widget> m_OnFocus = new ScriptInvoker<Widget>();
54  ref ScriptInvoker<Widget> m_OnFocusLost = new ScriptInvoker<Widget>();
55 
56  // Arguments passed: SCR_ButtonBaseComponent, bool border shown
57  ref ScriptInvoker m_OnShowBorder = new ScriptInvoker();
58 
59  protected MenuBase m_ParentMenu; // Pointer back to menu which owns this button, initialized at FindParentMenu
60  protected SCR_SubMenuBase m_ParentSubMenu;
61 
62  //------------------------------------------------------------------------------------------------
63  override void HandlerAttached(Widget w)
64  {
65  super.HandlerAttached(w);
66  m_wBackground = w.FindAnyWidget("Background");
67  m_wBorder = w.FindAnyWidget("Border");
68 
69  if (m_wBorder && m_bUseColorization)
70  m_wBorder.SetOpacity(0);
71 
72  if (m_wBackground && m_bShowBackgroundOnFocus)
73  m_wBackground.SetOpacity(0);
74 
75  ColorizeBackground(false);
76 
77  if (!m_wRoot.IsEnabled())
78  OnDisabled(false);
79 
80  // Find parent menu. Due to init order, menu component is not attached to menu root widget right now,
81  // so it must be done through call queue.
82  GetGame().GetCallqueue().CallLater(FindParentMenu, 0);
83  }
84 
85  //------------------------------------------------------------------------------------------------
86  override bool OnClick(Widget w, int x, int y, int button)
87  {
88  super.OnClick(w, x, y, button);
89  if (button != 0)
90  return false;
91 
92  if (m_bCanBeToggled)
93  SetToggled(!m_bIsToggled);
94  m_OnClicked.Invoke(this);
95 
96  return false;
97  }
98 
99  //------------------------------------------------------------------------------------------------
100  override bool OnMouseEnter(Widget w, int x, int y)
101  {
102  super.OnMouseEnter(w, x, y);
103 
104  if (m_bMouseOverToFocus)
105  return false;
106 
107  if (m_bShowBorderOnHover && m_wBorder)
108  {
109  if (m_bNoBorderAnimation)
110  m_wBorder.SetOpacity(1);
111  else
112  AnimateWidget.Opacity(m_wBorder, 1, m_fAnimationRate, true);
113  }
114 
115  if (!m_bUseColorization)
116  return false;
117 
118  if (!m_wBackground)
119  return false;
120 
121  if (m_bIsToggled)
122  AnimateWidget.Color(m_wBackground, m_BackgroundSelectedHovered, m_fAnimationRate);
123  else
124  AnimateWidget.Color(m_wBackground, m_BackgroundHovered, m_fAnimationRate);
125 
126  return false;
127  }
128 
129  //------------------------------------------------------------------------------------------------
130  override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
131  {
132  super.OnMouseLeave(w, enterW, x, y);
133 
134  if (m_bMouseOverToFocus)
135  return false;
136 
137  if (m_bUseColorization && m_wBackground)
138  {
139  if (m_bIsToggled)
140  AnimateWidget.Color(m_wBackground, m_BackgroundSelected, m_fAnimationRate);
141  else
142  AnimateWidget.Color(m_wBackground, m_BackgroundDefault, m_fAnimationRate);
143  }
144 
145  if (!m_wBorder || !m_bShowBorderOnHover || (m_bShowBorderOnFocus && GetGame().GetWorkspace().GetFocusedWidget() == m_wRoot))
146  return false;
147 
148  if (m_bNoBorderAnimation)
149  m_wBorder.SetOpacity(0);
150  else
151  AnimateWidget.Opacity(m_wBorder, 0, m_fAnimationRate, true);
152  return false;
153  }
154 
155  //------------------------------------------------------------------------------------------------
156  override bool OnFocus(Widget w, int x, int y)
157  {
158  super.OnFocus(w, x, y);
159 
160  if (m_bShowBorderOnFocus && m_wBorder)
161  {
162  if (m_bNoBorderAnimation)
163  m_wBorder.SetOpacity(1);
164  else
165  AnimateWidget.Opacity(m_wBorder, 1, m_fAnimationRate, true);
166  }
167 
168  if (m_bShowBackgroundOnFocus && m_wBackground)
169  {
170  if (m_bNoBackgroundAnimation)
171  m_wBackground.SetOpacity(1);
172  else
173  AnimateWidget.Opacity(m_wBackground, 1, m_fAnimationRate, true);
174  }
175 
176  m_OnFocus.Invoke(m_wRoot);
177 
178  return false;
179  }
180 
181  //------------------------------------------------------------------------------------------------
182  override bool OnFocusLost(Widget w, int x, int y)
183  {
184  super.OnFocusLost(w, x, y);
185 
186  m_OnFocusLost.Invoke(w);
187 
188  if (m_bShowBackgroundOnFocus && m_wBackground)
189  {
190  if (m_bNoBackgroundAnimation)
191  m_wBackground.SetOpacity(0);
192  else
193  AnimateWidget.Opacity(m_wBackground, 0, m_fAnimationRate, true);
194  }
195 
196  if (!m_bShowBorderOnFocus || !m_wBorder || (m_bShowBorderOnHover && WidgetManager.GetWidgetUnderCursor() == m_wRoot))
197  return false;
198 
199  if (m_bNoBorderAnimation)
200  m_wBorder.SetOpacity(0);
201  else
202  AnimateWidget.Opacity(m_wBorder, 0, m_fAnimationRate, true);
203 
204  return false;
205  }
206 
207  //------------------------------------------------------------------------------------------------
208  protected void OnMenuSelect()
209  {
210  if (!m_wRoot.IsEnabled())
211  return;
212 
213  MenuSelectBase();
214 
215  m_OnClicked.Invoke(this);
216  }
217 
218  //------------------------------------------------------------------------------------------------
219  protected void MenuSelectBase()
220  {
221  if (m_bCanBeToggled)
222  {
223  SetToggled(!m_bIsToggled);
224  }
225  else if (m_bUseColorization && m_wBackground)
226  {
227  AnimateWidget.Color(m_wBackground, m_BackgroundClicked, m_fAnimationRate);
228  GetGame().GetCallqueue().CallLater(ColorizeBackground, m_fAnimationTime * 500 + 1, false, true);
229  }
230  }
231 
232  // Public API
233  //------------------------------------------------------------------------------------------------
234  void SetToggled(bool toggled, bool animate = true, bool invokeChange = true)
235  {
236  if (!m_bCanBeToggled)
237  return;
238 
239  m_bIsToggled = toggled;
240  PlaySound(m_sSoundClicked);
241  ColorizeBackground(animate);
242  if (invokeChange)
243  m_OnToggled.Invoke(this, m_bIsToggled);
244  }
245 
246  //------------------------------------------------------------------------------------------------
247  bool IsToggled()
248  {
249  return m_bIsToggled;
250  }
251 
252  //------------------------------------------------------------------------------------------------
253  void SetToggleable(bool togglable)
254  {
255  m_bCanBeToggled = togglable;
256  }
257 
258  //------------------------------------------------------------------------------------------------
259  bool GetToggleable()
260  {
261  return m_bCanBeToggled;
262  }
263 
264  //------------------------------------------------------------------------------------------------
265  void ShowBorder(bool show, bool animate = true)
266  {
267  if (m_wBorder)
268  {
269  if (animate)
270  AnimateWidget.Opacity(m_wBorder, show, m_fAnimationRate, true);
271  else if (m_wBorder)
272  m_wBorder.SetOpacity(show);
273  }
274 
275  m_OnShowBorder.Invoke(this, show);
276  }
277 
278  //------------------------------------------------------------------------------------------------
279  bool IsBorderShown()
280  {
281  if (!m_wBorder)
282  return false;
283  return m_wBorder.GetOpacity() != 0;
284  }
285 
286  //------------------------------------------------------------------------------------------------
288  void SetBackgroundColors(Color defColor = null, Color hoverColor = null, Color selectColor = null, Color selectHoverColor = null, Color clickColor = null)
289  {
290  if (defColor)
291  m_BackgroundDefault = defColor;
292 
293  if (hoverColor)
294  m_BackgroundHovered = hoverColor;
295 
296  if (selectColor)
297  m_BackgroundSelected = selectColor;
298 
299  if (selectHoverColor)
300  m_BackgroundSelectedHovered = selectHoverColor;
301 
302  if (clickColor)
303  m_BackgroundClicked = clickColor;
304  }
305 
306  //------------------------------------------------------------------------------------------------
307  void ColorizeBackground(bool animate = true)
308  {
309  if (!m_bUseColorization || !m_wBackground)
310  return;
311 
312  Color color;
313  bool isHovered = WidgetManager.GetWidgetUnderCursor() == m_wRoot;
314  if (m_bIsToggled && m_bCanBeToggled)
315  {
316  if (isHovered)
317  color = m_BackgroundSelectedHovered;
318  else
319  color = m_BackgroundSelected;
320  }
321  else
322  {
323  if (isHovered)
324  color = m_BackgroundHovered;
325  else
326  color = m_BackgroundDefault;
327  }
328 
329  if (animate)
330  AnimateWidget.Color(m_wBackground, color, m_fAnimationRate);
331  else
332  {
333  AnimateWidget.StopAnimation(m_wBackground, WidgetAnimationColor);
334  m_wBackground.SetColor(color);
335  }
336  }
337 
338  //------------------------------------------------------------------------------------------------
339  static SCR_ButtonBaseComponent GetButtonBase(string name, Widget parent, bool searchAllChildren = true)
340  {
341  auto comp = SCR_ButtonBaseComponent.Cast(
342  SCR_WLibComponentBase.GetComponent(SCR_ButtonBaseComponent, name, parent, searchAllChildren)
343  );
344  return comp;
345  }
346 
347  //------------------------------------------------------------------------------------------------
349  protected void FindParentMenu()
350  {
351  // Find parent menu
352  MenuBase parentMenu = GetGame().GetMenuManager().GetOwnerMenu(m_wRoot);
353  m_ParentMenu = parentMenu;
354 
355  // Find parent submenu
356  Widget wParent = m_wRoot.GetParent();
357  while (wParent)
358  {
359  ScriptedWidgetEventHandler subMenuBase = wParent.FindHandler(SCR_SubMenuBase);
360  if (subMenuBase)
361  {
362  m_ParentSubMenu = SCR_SubMenuBase.Cast(subMenuBase);
363  break;
364  }
365  wParent = wParent.GetParent();
366  }
367 
368  // Warning disabled since we have quite many places where a button is not in a menu
369  //if (!parentMenu)
370  // Print(string.Format("Failed to find parent menu of SCR_ButtonBaseComponent %1 on widget %2", this, this.GetRootWidget().GetName()), LogLevel.WARNING);
371  }
372 
373  //------------------------------------------------------------------------------------------------
375  protected bool IsParentMenuFocused()
376  {
377  if (m_ParentSubMenu)
378  return m_ParentSubMenu.GetShown();
379  else if(m_ParentMenu)
380  return m_ParentMenu.IsFocused();
381  else
382  return true;
383  }
384 };
OnDisabled
void OnDisabled()
Definition: SCR_CampaignMilitaryBaseComponent.c:557
PlaySound
SCR_ParticleContactComponentClass ScriptComponentClass PlaySound(IEntity owner, SCR_ParticleContactComponentClass prefabData, Contact contact)
Definition: SCR_ParticleContactComponent.c:38
m_wRoot
protected Widget m_wRoot
Definition: SCR_ScenarioFrameworkLayerTaskDefend.c:59
SCR_WLibComponentBase
Base class for all final Reforger interactive elements.
Definition: SCR_WLibComponentBase.c:4
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
m_bCanBeToggled
protected bool m_bCanBeToggled
Definition: SCR_ModularButtonComponent.c:41
SCR_SubMenuBase
Definition: SCR_SubMenuBase.c:6
m_OnFocusLost
ref ScriptInvoker m_OnFocusLost
Definition: SCR_ModularButtonComponent.c:64
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_ButtonBaseComponent
Base class for any button, regardless its own content.
Definition: SCR_ButtonBaseComponent.c:3
m_wBackground
protected ImageWidget m_wBackground
Definition: SCR_InventoryHitZonePointUI.c:382
m_OnFocus
ref ScriptInvoker m_OnFocus
Definition: SCR_ModularButtonComponent.c:63
m_OnClicked
ref ScriptInvoker m_OnClicked
Definition: SCR_ModularButtonComponent.c:60
m_OnToggled
ref ScriptInvoker m_OnToggled
Definition: SCR_ModularButtonComponent.c:62