Arma Reforger Explorer 1.7.0.54
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
Loading...
Searching...
No Matches
SCR_SliderComponent.c
Go to the documentation of this file.
1//------------------------------------------------------------------------------------------------
3{
4 [Attribute("0.5")]
5 protected float m_fValue;
6
7 [Attribute("0")]
8 protected float m_fMinValue;
9
10 [Attribute("1")]
11 protected float m_fMaxValue;
12
13 [Attribute("0.05")]
14 protected float m_fStep;
15
16 [Attribute("%1", UIWidgets.LocaleEditBox, "Localization friendly string: %1 shows primary value, %2 shows secondary value. It can be overwritten by SetValueString() function")]
17 protected string m_sFormatText;
18
19 [Attribute("1", UIWidgets.EditBox, "Multiplies the value in displayed text.\nWith 100 and percentage format, value 1 is visualized as 100%")]
20 protected float m_fShownValueMultiplier;
21
22 [Attribute("1", UIWidgets.EditBox, "Multiplies the value in displayed text.\nWith 100 and percentage format, value 1 is visualized as 100%")]
24
25 [Attribute("1", UIWidgets.EditBox, "Multiplies the internal value for primary value display before offset is added.")]
26 protected float m_fValueMultiplier;
27
28 [Attribute("1", UIWidgets.EditBox, "Multiplies the internal value for secondary value display before offset is added.")]
29 protected float m_fSecondaryMultiplier;
30
31 [Attribute("0", UIWidgets.EditBox, "Offsets the internal value for primary value display.\nWith 1, value 1 is read as 2")]
32 protected float m_fValueOffset;
33
34 [Attribute("0", UIWidgets.EditBox, "Offsets the internal value for secondary value display.\nWith 1, value 1 is read as 2")]
35 protected float m_fSecondaryOffset;
36
37 [Attribute("0", UIWidgets.CheckBox, "Should the value text be clamped to Min/Max value after offset is added?")]
38 protected bool m_bClampValue;
39
40 [Attribute("0", UIWidgets.CheckBox, "Should the value text be rounded?")]
41 protected bool m_bRoundValue;
42
43 [Attribute("0")]
44 protected int m_iDecimalPrecision;
45
46 [Attribute(SCR_SoundEvent.SOUND_FE_ITEM_CHANGE)]
47 protected string m_sChangeSound;
48
51 protected float m_fOldValue;
52
55
56 //------------------------------------------------------------------------------------------------
57 override void HandlerAttached(Widget w)
58 {
59 super.HandlerAttached(w);
60
61 m_wSlider = SliderWidget.Cast(w.FindAnyWidget("Slider"));
62 m_wText = TextWidget.Cast(w.FindAnyWidget("SliderText"));
63
64 if (!m_wSlider)
65 return;
66
67 // Get slider handling
69 if (!m_Handler)
70 return;
71
73
74 m_Handler.GetOnFocus().Insert(OnSliderFocus);
75 m_Handler.GetOnFocusLost().Insert(OnSliderFocusLost);
76
77 m_Handler.GetOnChange().Insert(OnValueChanged);
78 m_Handler.GetOnChangeFinal().Insert(OnValueFinal);
79
81 }
82
83 //------------------------------------------------------------------------------------------------
84 override void HandlerDeattached(Widget w)
85 {
86 super.HandlerDeattached(w);
87
88 m_Handler.GetOnChange().Remove(OnValueChanged);
89 m_Handler.GetOnChangeFinal().Remove(OnValueFinal);
90 }
91
92 //------------------------------------------------------------------------------------------------
93 override bool OnFocus(Widget w, int x, int y)
94 {
95 // Do not focus parent, call the super function on slider focus
96
97 // Set focus to handler
98 GetGame().GetWorkspace().SetFocusedWidget(m_wSlider);
99 return false;
100 }
101
102 //------------------------------------------------------------------------------------------------
103 protected void OnValueChanged(Widget w)
104 {
105 float value = UpdateValue();
106
107//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
108// Can lead to severe sound spamming based on speed of user interaction
109
110 if (m_sChangeSound != string.Empty && !float.AlmostEqual(m_fOldValue, value))
112
113//---- REFACTOR NOTE END ----
114
115 m_fOldValue = value;
116
117 m_OnChanged.Invoke(this, value);
118 }
119
120 //------------------------------------------------------------------------------------------------
121 protected float UpdateValue()
122 {
123 float value;
124 if (m_wSlider)
125 value = m_wSlider.GetCurrent();
126
127 // Update displayed text, optional secondary value display
128 if (m_wText && m_wText.IsVisible())
129 {
130 float value1 = value * m_fValueMultiplier + m_fValueOffset;
131 float value2 = value * m_fSecondaryMultiplier + m_fSecondaryOffset;
132
133 if (m_bClampValue)
134 {
135 value1 = Math.Clamp(value1, m_fMinValue, m_fMaxValue);
136 value2 = Math.Clamp(value2, m_fMinValue, m_fMaxValue);
137 }
138
139 if (m_bRoundValue)
140 {
143 }
144
145 m_wText.SetTextFormat(m_sFormatText, value1, value2);
146 }
147
148 return value;
149 }
150
151 //------------------------------------------------------------------------------------------------
152 protected float RoundValue(float value, int precision)
153 {
154 if (!m_bRoundValue)
155 return value;
156
157 float coef = Math.Pow(10, precision);
158 value = Math.Round(value * coef) / coef;
159
160 return value;
161 }
162
163 //------------------------------------------------------------------------------------------------
164 protected void OnValueFinal(Widget w)
165 {
166 float value;
167 if (m_wSlider)
168 value = m_wSlider.GetCurrent();
169
171 m_OnChangedFinal.Invoke(this, value);
172 }
173
174//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
175// Solutions such as these feel weird given the current widget event system. There's probably a better setup
176
177 //------------------------------------------------------------------------------------------------
178 protected void OnSliderFocus()
179 {
180 m_wRoot.SetFlags(WidgetFlags.NOFOCUS);
181
182 super.OnFocus(m_wRoot, 0, 0); // Emulate focus on a parent class
183 }
184
185 //------------------------------------------------------------------------------------------------
186 protected void OnSliderFocusLost()
187 {
188 m_wRoot.ClearFlags(WidgetFlags.NOFOCUS);
189
190 super.OnFocusLost(m_wRoot, 0, 0); // Emulate focus on a parent class
191 }
192
193
194//---- REFACTOR NOTE END ----
195
196 // User API
197 //------------------------------------------------------------------------------------------------
198 void SetValue(float value)
199 {
200 if (!m_wSlider)
201 return;
202
203 m_fValue = value;
204 m_wSlider.SetCurrent(value);
206 }
207
208 //------------------------------------------------------------------------------------------------
215
216 //------------------------------------------------------------------------------------------------
217 float GetValue()
218 {
219 return m_wSlider.GetCurrent();
220 }
221
222 //------------------------------------------------------------------------------------------------
223 void SetFormatText(string text)
224 {
225 m_sFormatText = text;
226 }
227
228 //------------------------------------------------------------------------------------------------
230 {
231 return m_sFormatText;
232 }
233
234 //------------------------------------------------------------------------------------------------
235 void ShowCustomValue(string value)
236 {
237 if (!m_wText || !m_wText.IsVisible())
238 return;
239
240 m_wText.SetTextFormat(m_sFormatText, value);
241 }
242
243 //------------------------------------------------------------------------------------------------
244 void SetSliderSettings(float min, float max, float step, string formatText = string.Empty)
245 {
246 m_wSlider.SetMin(min);
247 m_wSlider.SetMax(max);
248 m_wSlider.SetStep(step);
249 if (formatText != string.Empty)
250 m_sFormatText = formatText;
251 }
252
253//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
254// m_wSlider could be null
255
256 //------------------------------------------------------------------------------------------------
257 void SetMin(float min)
258 {
259 m_wSlider.SetMin(min);
260 }
261
262 //------------------------------------------------------------------------------------------------
263 void SetMax(float max)
264 {
265 m_wSlider.SetMax(max);
266 }
267
268 //------------------------------------------------------------------------------------------------
269 void SetStep(float step)
270 {
271 m_wSlider.SetStep(step);
272 }
273
274//---- REFACTOR NOTE END ----
275
276//---- REFACTOR NOTE START: This code will need to be refactored as current implementation is not conforming to the standards ----
277// the error case is a valid value: why not return -1 instead?
278
279 //------------------------------------------------------------------------------------------------
280 float GetMin()
281 {
282 if (!m_wSlider)
283 return 0;
284
285 return m_wSlider.GetMin();
286 }
287
288 //------------------------------------------------------------------------------------------------
289 float GetMax()
290 {
291 if (!m_wSlider)
292 return 0;
293
294 return m_wSlider.GetMax();
295 }
296
297 //------------------------------------------------------------------------------------------------
298 float GetStep()
299 {
300 if (!m_wSlider)
301 return 0;
302
303 return m_wSlider.GetStep();
304 }
305
306//---- REFACTOR NOTE END ----
307
308 //------------------------------------------------------------------------------------------------
309 void SetShownValueMultiplier(float multiplier)
310 {
311 m_fShownValueMultiplier = multiplier;
312 }
313
314 //------------------------------------------------------------------------------------------------
316 {
318 }
319
320 //------------------------------------------------------------------------------------------------
323 static SCR_SliderComponent GetSliderComponent(string name, Widget parent, bool searchAllChildren = true)
324 {
325 return SCR_SliderComponent.Cast(SCR_ScriptedWidgetComponent.GetComponent(SCR_SliderComponent, name, parent, searchAllChildren));
326 }
327};
ArmaReforgerScripted GetGame()
Definition game.c:1398
params precision
Definition Math.c:13
Base class for all widgets that can change their internal state as editbox or spinbox.
static SCR_ScriptedWidgetComponent GetComponent(typename componentType, string name, Widget parent, bool searchAllChildren=true)
Base method for component lookup through the widget library.
void SetFormatText(string text)
static SCR_SliderComponent GetSliderComponent(string name, Widget parent, bool searchAllChildren=true)
override void HandlerDeattached(Widget w)
ref SCR_EventHandlerComponent m_Handler
ScriptInvoker GetOnChangedFinal()
void SetShownValueMultiplier(float multiplier)
void ShowCustomValue(string value)
override void HandlerAttached(Widget w)
void SetSliderSettings(float min, float max, float step, string formatText=string.Empty)
float RoundValue(float value, int precision)
ref ScriptInvoker m_OnChangedFinal
void SetValue(float value)
void OnValueChanged(Widget w)
override bool OnFocus(Widget w, int x, int y)
SCR_FieldOfViewSettings Attribute
WidgetFlags
Widget flags. See enf::Widget::SetFlags().
Definition WidgetFlags.c:14
ScriptInvokerBase< func > ScriptInvoker
Definition tools.c:134