optionselectorslider.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. class OptionSelectorSlider extends OptionSelectorSliderSetup
  2. {
  3. protected bool m_Changed;
  4. protected EditBoxWidget m_ValueText;
  5. protected bool m_ShowEditbox;
  6. protected float m_LastValue;
  7. void OptionSelectorSlider(Widget parent, float value, ScriptedWidgetEventHandler parent_menu, bool disabled, float min, float max, bool showEditbox = false)
  8. {
  9. if (!showEditbox)
  10. {
  11. m_Root = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/option_slider.layout", parent);
  12. }
  13. else
  14. {
  15. m_Root = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/option_slider_editbox.layout", parent);
  16. m_ValueText = EditBoxWidget.Cast(m_Root.FindAnyWidget("option_value_text"));
  17. m_ValueText.Enable(false);
  18. #ifdef PLATFORM_CONSOLE
  19. m_ValueText.SetFlags(WidgetFlags.IGNOREPOINTER);
  20. #endif
  21. }
  22. #ifdef PLATFORM_CONSOLE
  23. m_Parent = parent.GetParent().GetParent();
  24. #else
  25. #ifdef PLATFORM_WINDOWS
  26. m_Parent = parent.GetParent();
  27. #endif
  28. #endif
  29. m_SelectorType = 1;
  30. m_ParentClass = parent_menu;
  31. m_Slider = SliderWidget.Cast(m_Root.FindAnyWidget("option_value"));
  32. m_Slider.SetCurrent(value);
  33. m_MinValue = min;
  34. m_MaxValue = max;
  35. m_LastValue = value;
  36. m_ShowEditbox = showEditbox;
  37. SetValue(value);
  38. Enable();
  39. if (showEditbox)
  40. {
  41. SetValueText();
  42. }
  43. m_Parent.SetHandler(this);
  44. }
  45. void SetValueText()
  46. {
  47. float percentage = GetRangePercantageByValue();
  48. m_ValueText.SetText(percentage.ToString());
  49. }
  50. void Refresh(float defaultValue = -1)
  51. {
  52. if (m_ShowEditbox)
  53. {
  54. if (m_ValueText.GetText() == "" && defaultValue > -1)
  55. {
  56. SetValue(defaultValue);
  57. SetValueText();
  58. }
  59. }
  60. m_Changed = false;
  61. }
  62. float GetRangePercantageByValue()
  63. {
  64. float percentage = ((GetValue() - m_MinValue) * 100) / (m_MaxValue - m_MinValue);
  65. return percentage;
  66. }
  67. override bool OnChange(Widget w, int x, int y, bool finished)
  68. {
  69. float value;
  70. if (w == m_Slider)
  71. {
  72. if (m_ShowEditbox)
  73. {
  74. SetValueText();
  75. }
  76. value = GetValue();
  77. m_OptionChanged.Invoke(value);
  78. m_LastValue = value;
  79. return true;
  80. }
  81. else if (m_ShowEditbox && w == m_ValueText)
  82. {
  83. if (IsValidEditboxValue(m_ValueText.GetText()))
  84. {
  85. value = (m_ValueText.GetText().ToFloat() * (m_MaxValue - m_MinValue) / 100) + m_MinValue;
  86. m_Slider.SetCurrent(NormalizeInput(value));
  87. m_OptionChanged.Invoke(GetValue());
  88. return true;
  89. }
  90. else
  91. {
  92. m_ValueText.SetText("");
  93. Refresh();
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. bool Changed()
  100. {
  101. return m_Changed;
  102. }
  103. bool IsValidEditboxValue(string text)
  104. {
  105. TStringArray allowedCharacters = {"0","1","2","3","4","5","6","7","8","9"};
  106. for (int i = 0; i < text.Length(); i++)
  107. {
  108. int foundIndex = allowedCharacters.Find(text.Get(i));
  109. if (foundIndex == -1)
  110. return false;
  111. // make sure user input value can not contain zeros only
  112. if (i > 0 && text.ToInt() == 0)
  113. {
  114. return false;
  115. }
  116. }
  117. // make sure user input value is in valid percantage range (0 - 100)
  118. if (text.ToInt() > 100 || text.ToInt() < 0)
  119. {
  120. return false;
  121. }
  122. return true;
  123. }
  124. override bool OnDoubleClick(Widget w, int x, int y, int button)
  125. {
  126. #ifndef PLATFORM_CONSOLE
  127. if (m_ShowEditbox && w == m_ValueText)
  128. {
  129. m_ValueText.Enable(true);
  130. SetFocus(m_ValueText);
  131. m_LastValue = GetValue();
  132. m_ValueText.SetText("");
  133. m_ValueText.SetTextColor(ARGB(255, 255, 0, 0));
  134. SetActiveOption();
  135. return true;
  136. }
  137. #endif
  138. return false;
  139. }
  140. override bool OnFocus(Widget w, int x, int y)
  141. {
  142. if (m_ShowEditbox)
  143. {
  144. m_LastValue = GetValue();
  145. UpdateActiveOption();
  146. SetActiveOption();
  147. }
  148. #ifdef PLATFORM_CONSOLE
  149. return super.OnFocus(m_Parent, x, y);
  150. #else
  151. return false;
  152. #endif
  153. }
  154. override bool OnFocusLost(Widget w, int x, int y)
  155. {
  156. if (m_ShowEditbox && w == m_ValueText)
  157. {
  158. m_ValueText.Enable(false);
  159. UpdateActiveOption();
  160. }
  161. return super.OnFocusLost(w, x, y);
  162. }
  163. override bool IsFocusable(Widget w)
  164. {
  165. if (m_ShowEditbox && w)
  166. {
  167. return (w == m_Parent || w == m_Slider || w == m_ValueText);
  168. }
  169. return super.IsFocusable(w);
  170. }
  171. override bool OnMouseLeave(Widget w, Widget enterW, int x, int y)
  172. {
  173. #ifdef PLATFORM_CONSOLE
  174. if (ButtonWidget.Cast(w))
  175. {
  176. ColorNormalConsole(w);
  177. }
  178. #else
  179. if (m_ShowEditbox && m_ValueText.GetText() == "")
  180. {
  181. Refresh(m_LastValue);
  182. }
  183. #endif
  184. return super.OnMouseLeave(w, enterW, x, y);
  185. }
  186. void SetActiveOption()
  187. {
  188. OptionsMenuControls menuControls = OptionsMenuControls.Cast(m_ParentClass);
  189. if (menuControls)
  190. {
  191. menuControls.SetActiveOption(this);
  192. }
  193. }
  194. void UpdateActiveOption()
  195. {
  196. OptionsMenuControls menuControls = OptionsMenuControls.Cast(m_ParentClass);
  197. if (menuControls && menuControls.GetActiveOption())
  198. {
  199. OptionSelectorSlider optionSlider = OptionSelectorSlider.Cast(menuControls.GetActiveOption());
  200. if (!optionSlider || !optionSlider.IsValueTextVisible())
  201. return;
  202. optionSlider.Update();
  203. }
  204. }
  205. void Update()
  206. {
  207. m_ValueText.SetTextColor(ARGB(255, 255, 255, 255));
  208. if (m_ValueText.GetText() == "")
  209. Refresh(m_LastValue);
  210. }
  211. bool IsValueTextVisible()
  212. {
  213. return m_ShowEditbox;
  214. }
  215. }