Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
SimpleButtonComponent.c
Go to the documentation of this file.
1 // Simple button provides 4 states in which it can be:
2 // Default, focused, pressed and disabled.
3 // Hovering over the button will focus it instantly.
4 // Use it in places where you don't need onHover functionality.
5 
6 // TODO:
7 // Need events OnDisable and onEnable to recolorize widgets
8 
9 class SimpleButtonComponent : ScriptedWidgetComponent
10 {
11  [Attribute("0 0 0 0.7", UIWidgets.ColorPicker, "")]
12  protected ref Color m_ColorButtonDefault;
13 
14  [Attribute("1 1 1 0.25", UIWidgets.ColorPicker, "")]
15  protected ref Color m_ColorButtonFocused;
16 
17  [Attribute("1 1 1 0.3", UIWidgets.ColorPicker, "")]
18  protected ref Color m_ColorButtonPressed;
19 
20  [Attribute("0.8 0.8 0.8 0.1", UIWidgets.ColorPicker, "")]
21  protected ref Color m_ColorButtonDisabled;
22 
23  [Attribute("false", UIWidgets.CheckBox, "")]
24  protected bool m_bColorizeChildren;
25 
26  [Attribute("1 1 1 1", UIWidgets.ColorPicker, "")]
27  protected ref Color m_ColorChildDefault;
28 
29  [Attribute("0 0 0 1", UIWidgets.ColorPicker, "")]
30  protected ref Color m_ColorChildFocused;
31 
32  [Attribute("0 0 0 1", UIWidgets.ColorPicker, "")]
33  protected ref Color m_ColorChildPressed;
34 
35  [Attribute("1 1 1 0.6", UIWidgets.ColorPicker, "")]
36  protected ref Color m_ColorChildDisabled;
37 
38  [Attribute("0.2", UIWidgets.EditBox, "")]
39  protected float m_fAnimationLength;
40 
41  [Attribute(SCR_SoundEvent.CLICK, UIWidgets.EditBox, "")]
42  protected string m_sSoundClicked;
43 
44  [Attribute(SCR_SoundEvent.FOCUS, UIWidgets.EditBox, "")]
45  protected string m_sSoundFocused;
46 
47 
48  protected Widget m_wChild;
49  protected bool m_bIsFocused = false;
50 
51  //------------------------------------------------------------------------------------------------
52  override void HandlerAttached(Widget w)
53  {
54  m_wChild = w.GetChildren();
55  m_fAnimationLength = 1 / m_fAnimationLength;
56 
57  if (!w.IsEnabled())
58  {
59  w.SetColor(m_ColorButtonDisabled);
60  if (m_bColorizeChildren && m_wChild)
61  m_wChild.SetColor(m_ColorChildDisabled);
62  }
63  else
64  {
65  w.SetColor(m_ColorButtonDefault);
66  if (m_bColorizeChildren && m_wChild)
67  m_wChild.SetColor(m_ColorChildDefault);
68  }
69  }
70 
71  //------------------------------------------------------------------------------------------------
72  override bool OnClick(Widget w, int x, int y, int button)
73  {
74  SCR_UISoundEntity.SoundEvent(m_sSoundClicked);
75  return false;
76  }
77 
78  //------------------------------------------------------------------------------------------------
79  void SetOnClickSound(string newOnClickedSound)
80  {
81  m_sSoundClicked = newOnClickedSound;
82  }
83 
84  //------------------------------------------------------------------------------------------------
85  override bool OnMouseButtonDown(Widget w, int x, int y, int button)
86  {
87  AnimateWidget.Color(w, m_ColorButtonPressed, m_fAnimationLength);
88  if (m_bColorizeChildren && m_wChild)
89  AnimateWidget.Color(m_wChild, m_ColorChildPressed, m_fAnimationLength);
90 
91  return false;
92  }
93 
94  //------------------------------------------------------------------------------------------------
95  override bool OnMouseButtonUp(Widget w, int x, int y, int button)
96  {
97  if (w.IsEnabled())
98  AnimateWidget.Color(w, m_ColorButtonFocused, m_fAnimationLength);
99  else
100  AnimateWidget.Color(w, m_ColorButtonDisabled, m_fAnimationLength);
101 
102  if (m_bColorizeChildren && m_wChild)
103  {
104  if (w.IsEnabled())
105  AnimateWidget.Color(m_wChild, m_ColorChildFocused, m_fAnimationLength);
106  else
107  AnimateWidget.Color(m_wChild, m_ColorChildDisabled, m_fAnimationLength);
108  }
109 
110  return false;
111  }
112 
113  //------------------------------------------------------------------------------------------------
114  override bool OnFocus(Widget w, int x, int y)
115  {
116  m_bIsFocused = true;
117  SCR_UISoundEntity.SoundEvent(m_sSoundFocused);
118 
119  if (w.IsEnabled())
120  AnimateWidget.Color(w, m_ColorButtonFocused, m_fAnimationLength);
121  else
122  AnimateWidget.Color(w, m_ColorButtonDisabled, m_fAnimationLength);
123 
124  if (m_bColorizeChildren && m_wChild)
125  {
126  if (w.IsEnabled())
127  AnimateWidget.Color(m_wChild, m_ColorChildFocused, m_fAnimationLength);
128  else
129  AnimateWidget.Color(m_wChild, m_ColorChildDisabled, m_fAnimationLength);
130  }
131 
132  return false;
133  }
134 
135  //------------------------------------------------------------------------------------------------
136  override bool OnFocusLost(Widget w, int x, int y)
137  {
138  m_bIsFocused = false;
139 
140  if (w.IsEnabled())
141  AnimateWidget.Color(w, m_ColorButtonDefault, m_fAnimationLength);
142  else
143  AnimateWidget.Color(w, m_ColorButtonDisabled, m_fAnimationLength);
144 
145  if (m_bColorizeChildren && m_wChild)
146  {
147  if (w.IsEnabled())
148  AnimateWidget.Color(m_wChild, m_ColorChildDefault, m_fAnimationLength);
149  else
150  AnimateWidget.Color(m_wChild, m_ColorChildDisabled, m_fAnimationLength);
151  }
152 
153  return false;
154  }
155 
156  //------------------------------------------------------------------------------------------------
157  override bool OnMouseEnter(Widget w, int x, int y)
158  {
159  if (w.GetFlags() & WidgetFlags.NOFOCUS)
160  OnFocus(w, x, y);
161  else
162  GetGame().GetWorkspace().SetFocusedWidget(w);
163 
164  return false;
165  }
166 
167  //------------------------------------------------------------------------------------------------
168  override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
169  {
170  if (w.GetFlags() & WidgetFlags.NOFOCUS)
171  OnFocusLost(w, x, y);
172  else
173  GetGame().GetWorkspace().SetFocusedWidget(enterW);
174 
175  return false;
176  }
177 };
SCR_UISoundEntity
Definition: SCR_UISoundEntity.c:7
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_SoundEvent
Definition: SCR_SoundEvent.c:1
Attribute
typedef Attribute
Post-process effect of scripted camera.
SimpleButtonComponent
Definition: SimpleButtonComponent.c:9