Arma Reforger Explorer  1.1.0.42
Arma Reforger Code Explorer by Zeroy - Thanks to MisterOutofTime
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 saves the value. It can be overwritten by SetValueString() function")]
17  protected string m_sFormatText;
18 
19  [Attribute("1", UIWidgets.EditBox, "Multiplies the internal value in the text. With 100 and percentage format, value 1 is visualized as 100%")]
20  protected float m_fShownValueMultiplier;
21 
22  [Attribute("0", UIWidgets.CheckBox, "Should the value text be rounded?")]
23  private bool m_bRoundValue;
24 
25  [Attribute("0")]
26  protected int m_iDecimalPrecision;
27 
28  [Attribute(SCR_SoundEvent.SOUND_FE_ITEM_CHANGE)]
29  protected string m_sChangeSound;
30 
31  protected TextWidget m_wText;
32  protected SliderWidget m_wSlider;
33  protected float m_fOldValue;
34 
35  protected ref SCR_EventHandlerComponent m_Handler;
36  protected ref ScriptInvoker m_OnChangedFinal;
37 
38  //------------------------------------------------------------------------------------------------
39  override void HandlerAttached(Widget w)
40  {
41  super.HandlerAttached(w);
42 
43  m_wSlider = SliderWidget.Cast(w.FindAnyWidget("Slider"));
44  m_wText = TextWidget.Cast(w.FindAnyWidget("SliderText"));
45 
46  if (!m_wSlider)
47  return;
48 
49  // Get slider handling
50  m_Handler = SCR_EventHandlerComponent.Cast(m_wSlider.FindHandler(SCR_EventHandlerComponent));
51  if (!m_Handler)
52  return;
53 
54  SetSliderSettings(m_fMinValue, m_fMaxValue, m_fStep, m_sFormatText);
55 
56  m_Handler.GetOnFocus().Insert(OnSliderFocus);
57  m_Handler.GetOnFocusLost().Insert(OnSliderFocusLost);
58 
59  m_Handler.GetOnChange().Insert(OnValueChanged);
60  m_Handler.GetOnChangeFinal().Insert(OnValueFinal);
61 
62  if (m_wText)
63  m_wText.SetTextFormat(m_sFormatText, RoundValue(m_wSlider.GetCurrent() * m_fShownValueMultiplier, m_iDecimalPrecision));
64  }
65 
66  //------------------------------------------------------------------------------------------------
67  override void HandlerDeattached(Widget w)
68  {
69  super.HandlerDeattached(w);
70 
71  m_Handler.GetOnChange().Remove(OnValueChanged);
72  m_Handler.GetOnChangeFinal().Remove(OnValueFinal);
73  }
74 
75  //------------------------------------------------------------------------------------------------
76  override bool OnFocus(Widget w, int x, int y)
77  {
78  // Do not focus parent, call the super function on slider focus
79 
80  // Set focus to handler
81  GetGame().GetWorkspace().SetFocusedWidget(m_wSlider);
82  return false;
83  }
84 
85  //------------------------------------------------------------------------------------------------
86  protected void OnValueChanged(Widget w)
87  {
88  float value;
89  if (m_wSlider)
90  value = m_wSlider.GetCurrent();
91 
92  if (m_wText && m_wText.IsVisible())
93  m_wText.SetTextFormat(m_sFormatText, RoundValue(value * m_fShownValueMultiplier, m_iDecimalPrecision));
94 
95  if (m_sChangeSound != string.Empty && m_fOldValue != value)
96  PlaySound(m_sChangeSound);
97 
98  m_fOldValue = value;
99 
100  m_OnChanged.Invoke(this, value);
101  }
102 
103  //------------------------------------------------------------------------------------------------
104  protected float RoundValue(float value, int precision)
105  {
106  if (!m_bRoundValue)
107  return value;
108 
109  float coef = Math.Pow(10, m_iDecimalPrecision);
110  value = Math.Round(value * coef) / coef;
111 
112  return value;
113  }
114 
115  //------------------------------------------------------------------------------------------------
116  protected void OnValueFinal(Widget w)
117  {
118  float value;
119  if (m_wSlider)
120  value = m_wSlider.GetCurrent();
121 
122  if (m_OnChangedFinal)
123  m_OnChangedFinal.Invoke(this, value);
124  }
125 
126  //------------------------------------------------------------------------------------------------
127  protected void OnSliderFocus()
128  {
129  m_wRoot.SetFlags(WidgetFlags.NOFOCUS);
130 
131  super.OnFocus(m_wRoot, 0, 0); // Emulate focus on a parent class
132  }
133 
134  //------------------------------------------------------------------------------------------------
135  protected void OnSliderFocusLost()
136  {
137  m_wRoot.ClearFlags(WidgetFlags.NOFOCUS);
138 
139  super.OnFocusLost(m_wRoot, 0, 0); // Emulate focus on a parent class
140  }
141 
142  // User API
143  //------------------------------------------------------------------------------------------------
144  void SetValue(float value)
145  {
146  if (!m_wSlider)
147  return;
148 
149  m_fValue = value;
150  m_wSlider.SetCurrent(value);
151  OnValueChanged(m_wSlider);
152  }
153 
154  //------------------------------------------------------------------------------------------------
155  ScriptInvoker GetOnChangedFinal()
156  {
157  if (!m_OnChangedFinal)
158  m_OnChangedFinal = new ScriptInvoker();
159  return m_OnChangedFinal;
160  }
161 
162  //------------------------------------------------------------------------------------------------
163  float GetValue()
164  {
165  return m_wSlider.GetCurrent();
166  }
167 
168  //------------------------------------------------------------------------------------------------
169  void SetFormatText(string text)
170  {
171  m_sFormatText = text;
172  }
173 
174  //------------------------------------------------------------------------------------------------
175  string GetFormatText()
176  {
177  return m_sFormatText;
178  }
179 
180  //------------------------------------------------------------------------------------------------
181  void ShowCustomValue(string value)
182  {
183  if (!m_wText || !m_wText.IsVisible())
184  return;
185 
186  m_wText.SetTextFormat(m_sFormatText, value);
187  }
188 
189  //------------------------------------------------------------------------------------------------
190  void SetSliderSettings(float min, float max, float step, string formatText = string.Empty)
191  {
192  m_wSlider.SetMin(min);
193  m_wSlider.SetMax(max);
194  m_wSlider.SetStep(step);
195  if (formatText != string.Empty)
196  m_sFormatText = formatText;
197  }
198 
199  //------------------------------------------------------------------------------------------------
200  void SetMin(float min)
201  {
202  m_wSlider.SetMin(min);
203  }
204 
205  //------------------------------------------------------------------------------------------------
206  void SetMax(float max)
207  {
208  m_wSlider.SetMax(max);
209  }
210 
211  //------------------------------------------------------------------------------------------------
212  void SetStep(float step)
213  {
214  m_wSlider.SetStep(step);
215  }
216 
217  //------------------------------------------------------------------------------------------------
218  float GetMin()
219  {
220  if (!m_wSlider)
221  return 0;
222 
223  return m_wSlider.GetMin();
224  }
225 
226  //------------------------------------------------------------------------------------------------
227  float GetMax()
228  {
229  if (!m_wSlider)
230  return 0;
231 
232  return m_wSlider.GetMax();
233  }
234 
235  //------------------------------------------------------------------------------------------------
236  float GetStep()
237  {
238  if (!m_wSlider)
239  return 0;
240 
241  return m_wSlider.GetStep();
242  }
243 
244  //------------------------------------------------------------------------------------------------
245  void SetShownValueMultiplier(float multiplier)
246  {
247  m_fShownValueMultiplier = multiplier;
248  }
249 
250  //------------------------------------------------------------------------------------------------
251  float GetShownValueMultiplier()
252  {
253  return m_fShownValueMultiplier;
254  }
255 
256  //------------------------------------------------------------------------------------------------
259  static SCR_SliderComponent GetSliderComponent(string name, Widget parent, bool searchAllChildren = true)
260  {
261  return SCR_SliderComponent.Cast(SCR_ScriptedWidgetComponent.GetComponent(SCR_SliderComponent, name, parent, searchAllChildren));
262  }
263 };
SCR_ChangeableComponentBase
Base class for all widgets that can change their internal state as editbox or spinbox.
Definition: SCR_ChangeableComponentBase.c:4
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
GetGame
ArmaReforgerScripted GetGame()
Definition: game.c:1424
SCR_SoundEvent
Definition: SCR_SoundEvent.c:1
SCR_SliderComponent
Definition: SCR_SliderComponent.c:2
m_OnChanged
protected ref ScriptInvokerTabViewIndex m_OnChanged
Definition: SCR_TabViewComponent.c:64
Attribute
typedef Attribute
Post-process effect of scripted camera.
SCR_EventHandlerComponent
Definition: SCR_EventHandlerComponent.c:5
m_fStep
protected float m_fStep
Definition: SCR_BaseEditorAttribute.c:521
SCR_ScriptedWidgetComponent
Definition: SCR_ScriptedWidgetComponent.c:7