Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_ButtonBaseComponent.c
Go to the documentation of this file.
1//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
2// Several issues: missing events, and duplicate OnClick (one already in a parent class), incomplete handling of interaction states, it's looks are too tied to old Reforger style, and setting them in component's attributes makes mantaining a consistent look across the whole project an absolute nightmare. Holding refernces to parent menu or submenu is very limiting: it's not the button's business to change based on parent menu state, it's whatever holds the button that should notify it of important changes, because there's no guarantee this button will only be in a menu or submenu.
3
5//------------------------------------------------------------------------------------------------
7{
8 [Attribute("", UIWidgets.CheckBox, "Can the button be only clicked, or also toggled?")]
9 bool m_bCanBeToggled;
10
11 [Attribute()]
12 protected bool m_bIsToggled;
13
14 [Attribute("true", UIWidgets.CheckBox, "Use in built button colorization or handle it on your own")]
16
17 [Attribute("1 1 1 0.1", UIWidgets.ColorPicker)]
19
20 [Attribute("1 1 1 0.3", UIWidgets.ColorPicker)]
22
23 [Attribute("0.760 0.392 0.08 0.3", UIWidgets.ColorPicker)]
25
26 [Attribute("0.898 0.541 0.184 1", UIWidgets.ColorPicker)]
28
29 [Attribute("0.898 0.541 0.184 1", UIWidgets.ColorPicker)]
31
32 [Attribute("true")]
34
35 [Attribute("false")]
37
38 [Attribute("false")]
40
41 [Attribute("false")]
43
44 [Attribute()]
46
49
50 // Arguments passed: SCR_ButtonBaseComponent
52 // Arguments passed: SCR_ButtonBaseComponent, bool (toggled)
54
55 // TODO: Rewrite - do not pass widget //Why? This is super useful!
58
59 // Arguments passed: SCR_ButtonBaseComponent, bool border shown
61
62 protected MenuBase m_ParentMenu; // Pointer back to menu which owns this button, initialized at FindParentMenu
64
65 //------------------------------------------------------------------------------------------------
66 override void HandlerAttached(Widget w)
67 {
68 super.HandlerAttached(w);
69 m_wBackground = w.FindAnyWidget("Background");
70 m_wBorder = w.FindAnyWidget("Border");
71
73 m_wBorder.SetOpacity(0);
74
76 m_wBackground.SetOpacity(0);
77
78 ColorizeBackground(false);
79
80 if (!m_wRoot.IsEnabled())
81 OnDisabled(false);
82
83 // Find parent menu. Due to init order, menu component is not attached to menu root widget right now,
84 // so it must be done through call queue.
85 GetGame().GetCallqueue().CallLater(FindParentMenu, 0);
86 }
87
88 //------------------------------------------------------------------------------------------------
89 override bool OnClick(Widget w, int x, int y, int button)
90 {
91 super.OnClick(w, x, y, button);
92 if (button != 0)
93 return false;
94
95 if (m_bCanBeToggled)
97 m_OnClicked.Invoke(this);
98
99 return false;
100 }
101
102 //------------------------------------------------------------------------------------------------
103 override bool OnMouseEnter(Widget w, int x, int y)
104 {
105 super.OnMouseEnter(w, x, y);
106
108 return false;
109
111 {
113 }
114
116 return false;
117
118 if (!m_wBackground)
119 return false;
120
121 if (m_bIsToggled)
123 else
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
135 return false;
136
138 {
139 if (m_bIsToggled)
141 else
143 }
144
146 {
148 }
149
150 return false;
151 }
152
153 //------------------------------------------------------------------------------------------------
154 override bool OnFocus(Widget w, int x, int y)
155 {
156 super.OnFocus(w, x, y);
157
159 {
161 }
162
164 {
166 m_wBackground.SetOpacity(1);
167 else
169 }
170
171 m_OnFocus.Invoke(m_wRoot);
172
173 return false;
174 }
175
176 //------------------------------------------------------------------------------------------------
177 override bool OnFocusLost(Widget w, int x, int y)
178 {
179 super.OnFocusLost(w, x, y);
180
181 m_OnFocusLost.Invoke(w);
182
184 {
186 m_wBackground.SetOpacity(0);
187 else
189 }
190
192 {
194 }
195
196 return false;
197 }
198
199 //------------------------------------------------------------------------------------------------
200 protected void OnMenuSelect()
201 {
202 if (!m_wRoot.IsEnabled())
203 return;
204
206
207 m_OnClicked.Invoke(this);
208 }
209
210 //------------------------------------------------------------------------------------------------
211 protected void MenuSelectBase()
212 {
213 if (m_bCanBeToggled)
214 {
216 }
218 {
220 GetGame().GetCallqueue().CallLater(ColorizeBackground, m_fAnimationTime * 500 + 1, false, true);
221 }
222 }
223
224 // Public API
225 //------------------------------------------------------------------------------------------------
226 void SetToggled(bool toggled, bool animate = true, bool invokeChange = true, bool instant = false)
227 {
228 if (!m_bCanBeToggled)
229 return;
230
231 m_bIsToggled = toggled;
232 ColorizeBackground(animate);
233 if (invokeChange)
234 m_OnToggled.Invoke(this, m_bIsToggled);
235 }
236
237 //------------------------------------------------------------------------------------------------
239 {
240 return m_bIsToggled;
241 }
242
243 //------------------------------------------------------------------------------------------------
244 void SetToggleable(bool togglable)
245 {
246 m_bCanBeToggled = togglable;
247 }
248
249 //------------------------------------------------------------------------------------------------
251 {
252 return m_bCanBeToggled;
253 }
254
255 //------------------------------------------------------------------------------------------------
256 void ShowBorder(bool show, bool animate = true)
257 {
258 if (m_wBorder)
259 {
260 if (animate)
262 else if (m_wBorder)
263 m_wBorder.SetOpacity(show);
264 }
265
266 m_OnShowBorder.Invoke(this, show);
267 }
268
269 //------------------------------------------------------------------------------------------------
271 {
272 if (!m_wBorder)
273 return false;
274 return m_wBorder.GetOpacity() != 0;
275 }
276
277 //------------------------------------------------------------------------------------------------
279 void SetBackgroundColors(Color defColor = null, Color hoverColor = null, Color selectColor = null, Color selectHoverColor = null, Color clickColor = null)
280 {
281 if (defColor)
282 m_BackgroundDefault = defColor;
283
284 if (hoverColor)
285 m_BackgroundHovered = hoverColor;
286
287 if (selectColor)
288 m_BackgroundSelected = selectColor;
289
290 if (selectHoverColor)
291 m_BackgroundSelectedHovered = selectHoverColor;
292
293 if (clickColor)
294 m_BackgroundClicked = clickColor;
295 }
296
297 //------------------------------------------------------------------------------------------------
298 void ColorizeBackground(bool animate = true)
299 {
301 return;
302
303 Color color;
304 bool isHovered = WidgetManager.GetWidgetUnderCursor() == m_wRoot;
305 if (m_bIsToggled && m_bCanBeToggled)
306 {
307 if (isHovered)
309 else
310 color = m_BackgroundSelected;
311 }
312 else
313 {
314 if (isHovered)
315 color = m_BackgroundHovered;
316 else
317 color = m_BackgroundDefault;
318 }
319
320 if (animate)
322 else
323 {
325 m_wBackground.SetColor(color);
326 }
327 }
328
329 //------------------------------------------------------------------------------------------------
330 static SCR_ButtonBaseComponent GetButtonBase(string name, Widget parent, bool searchAllChildren = true)
331 {
332 auto comp = SCR_ButtonBaseComponent.Cast(
333 SCR_WLibComponentBase.GetComponent(SCR_ButtonBaseComponent, name, parent, searchAllChildren)
334 );
335 return comp;
336 }
337
338 //------------------------------------------------------------------------------------------------
340 protected void FindParentMenu()
341 {
342 // Find parent menu
343 MenuBase parentMenu = GetGame().GetMenuManager().GetOwnerMenu(m_wRoot);
344 m_ParentMenu = parentMenu;
345
346 // Find parent submenu
347 Widget wParent = m_wRoot.GetParent();
348 while (wParent)
349 {
350 ScriptedWidgetEventHandler subMenuBase = wParent.FindHandler(SCR_SubMenuBase);
351 if (subMenuBase)
352 {
353 m_ParentSubMenu = SCR_SubMenuBase.Cast(subMenuBase);
354 break;
355 }
356 wParent = wParent.GetParent();
357 }
358
359 // Warning disabled since we have quite many places where a button is not in a menu
360 //if (!parentMenu)
361 // Print(string.Format("Failed to find parent menu of SCR_ButtonBaseComponent %1 on widget %2", this, this.GetRootWidget().GetName()), LogLevel.WARNING);
362 }
363
364 //------------------------------------------------------------------------------------------------
366 protected bool IsParentMenuFocused()
367 {
368 if (m_ParentSubMenu)
369 return m_ParentSubMenu.GetShown();
370 else if(m_ParentMenu)
371 return m_ParentMenu.IsFocused();
372 else
373 return true;
374 }
375};
376
377//---- REFACTOR NOTE END ----
ArmaReforgerScripted GetGame()
Definition game.c:1398
static bool StopAnimation(Widget w, typename typeName)
static WidgetAnimationOpacity Opacity(Widget widget, float targetValue, float speed, bool toggleVisibility=false)
static WidgetAnimationColor Color(Widget widget, Color color, float speed)
Definition Color.c:13
Base class for any button, regardless its own content.
ref ScriptInvoker< Widget > m_OnFocusLost
override bool OnMouseEnter(Widget w, int x, int y)
override bool OnFocus(Widget w, int x, int y)
override bool OnClick(Widget w, int x, int y, int button)
override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
void SetToggled(bool toggled, bool animate=true, bool invokeChange=true, bool instant=false)
void ColorizeBackground(bool animate=true)
bool IsParentMenuFocused()
Returns true if parent menu is focused, or if there is no parent menu.
override bool OnFocusLost(Widget w, int x, int y)
void SetToggleable(bool togglable)
ref ScriptInvoker< Widget > m_OnFocus
override void HandlerAttached(Widget w)
void FindParentMenu()
Finds the parent menu of this component.
void SetBackgroundColors(Color defColor=null, Color hoverColor=null, Color selectColor=null, Color selectHoverColor=null, Color clickColor=null)
Alternate way of setting button background colors through script.
static SCR_ButtonBaseComponent GetButtonBase(string name, Widget parent, bool searchAllChildren=true)
void ShowBorder(bool show, bool animate=true)
Base class for all final Reforger interactive elements.
SCR_FieldOfViewSettings Attribute
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134