5 protected float m_fValue;
8 protected float m_fMinValue;
11 protected float m_fMaxValue;
14 protected float m_fStep;
16 [
Attribute(
"%1", UIWidgets.LocaleEditBox,
"Localization friendly string: %1 saves the value. It can be overwritten by SetValueString() function")]
17 protected string m_sFormatText;
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;
22 [
Attribute(
"0", UIWidgets.CheckBox,
"Should the value text be rounded?")]
23 private bool m_bRoundValue;
26 protected int m_iDecimalPrecision;
29 protected string m_sChangeSound;
31 protected TextWidget m_wText;
32 protected SliderWidget m_wSlider;
33 protected float m_fOldValue;
36 protected ref ScriptInvoker m_OnChangedFinal;
39 override void HandlerAttached(Widget w)
41 super.HandlerAttached(w);
43 m_wSlider = SliderWidget.Cast(w.FindAnyWidget(
"Slider"));
44 m_wText = TextWidget.Cast(w.FindAnyWidget(
"SliderText"));
54 SetSliderSettings(m_fMinValue, m_fMaxValue,
m_fStep, m_sFormatText);
56 m_Handler.GetOnFocus().Insert(OnSliderFocus);
57 m_Handler.GetOnFocusLost().Insert(OnSliderFocusLost);
59 m_Handler.GetOnChange().Insert(OnValueChanged);
60 m_Handler.GetOnChangeFinal().Insert(OnValueFinal);
63 m_wText.SetTextFormat(m_sFormatText, RoundValue(m_wSlider.GetCurrent() * m_fShownValueMultiplier, m_iDecimalPrecision));
67 override void HandlerDeattached(Widget w)
69 super.HandlerDeattached(w);
71 m_Handler.GetOnChange().Remove(OnValueChanged);
72 m_Handler.GetOnChangeFinal().Remove(OnValueFinal);
76 override bool OnFocus(Widget w,
int x,
int y)
81 GetGame().GetWorkspace().SetFocusedWidget(m_wSlider);
86 protected void OnValueChanged(Widget w)
90 value = m_wSlider.GetCurrent();
92 if (m_wText && m_wText.IsVisible())
93 m_wText.SetTextFormat(m_sFormatText, RoundValue(value * m_fShownValueMultiplier, m_iDecimalPrecision));
95 if (m_sChangeSound !=
string.Empty && m_fOldValue != value)
104 protected float RoundValue(
float value,
int precision)
109 float coef = Math.Pow(10, m_iDecimalPrecision);
110 value = Math.Round(value * coef) / coef;
116 protected void OnValueFinal(Widget w)
120 value = m_wSlider.GetCurrent();
122 if (m_OnChangedFinal)
123 m_OnChangedFinal.Invoke(
this, value);
127 protected void OnSliderFocus()
129 m_wRoot.SetFlags(WidgetFlags.NOFOCUS);
135 protected void OnSliderFocusLost()
137 m_wRoot.ClearFlags(WidgetFlags.NOFOCUS);
139 super.OnFocusLost(
m_wRoot, 0, 0);
144 void SetValue(
float value)
150 m_wSlider.SetCurrent(value);
151 OnValueChanged(m_wSlider);
155 ScriptInvoker GetOnChangedFinal()
157 if (!m_OnChangedFinal)
158 m_OnChangedFinal =
new ScriptInvoker();
159 return m_OnChangedFinal;
165 return m_wSlider.GetCurrent();
169 void SetFormatText(
string text)
171 m_sFormatText = text;
175 string GetFormatText()
177 return m_sFormatText;
181 void ShowCustomValue(
string value)
183 if (!m_wText || !m_wText.IsVisible())
186 m_wText.SetTextFormat(m_sFormatText, value);
190 void SetSliderSettings(
float min,
float max,
float step,
string formatText =
string.Empty)
192 m_wSlider.SetMin(min);
193 m_wSlider.SetMax(max);
194 m_wSlider.SetStep(step);
195 if (formatText !=
string.Empty)
196 m_sFormatText = formatText;
200 void SetMin(
float min)
202 m_wSlider.SetMin(min);
206 void SetMax(
float max)
208 m_wSlider.SetMax(max);
212 void SetStep(
float step)
214 m_wSlider.SetStep(step);
223 return m_wSlider.GetMin();
232 return m_wSlider.GetMax();
241 return m_wSlider.GetStep();
245 void SetShownValueMultiplier(
float multiplier)
247 m_fShownValueMultiplier = multiplier;
251 float GetShownValueMultiplier()
253 return m_fShownValueMultiplier;
259 static SCR_SliderComponent GetSliderComponent(
string name, Widget parent,
bool searchAllChildren =
true)