WidgetSliderInput.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "WidgetSliderInput.h"
  29. #include "../../Include/RmlUi/Core/Math.h"
  30. #include "../../Include/RmlUi/Core/Element.h"
  31. namespace Rml {
  32. namespace Controls {
  33. WidgetSliderInput::WidgetSliderInput(ElementFormControl* parent) : WidgetSlider(parent)
  34. {
  35. value = 0;
  36. min_value = 0;
  37. max_value = 100;
  38. step = 1;
  39. }
  40. WidgetSliderInput::~WidgetSliderInput()
  41. {
  42. }
  43. void WidgetSliderInput::SetValue(float value)
  44. {
  45. float num_steps = (value - min_value) / step;
  46. float new_value = min_value + Rml::Core::Math::RoundFloat(num_steps) * step;
  47. SetBarPosition(SetValueInternal(new_value));
  48. }
  49. float WidgetSliderInput::GetValue() const
  50. {
  51. return value;
  52. }
  53. // Sets the minimum value of the slider.
  54. void WidgetSliderInput::SetMinValue(float _min_value)
  55. {
  56. min_value = _min_value;
  57. }
  58. // Sets the maximum value of the slider.
  59. void WidgetSliderInput::SetMaxValue(float _max_value)
  60. {
  61. max_value = _max_value;
  62. }
  63. // Sets the slider's value increment.
  64. void WidgetSliderInput::SetStep(float _step)
  65. {
  66. // Can't have a zero step!
  67. if (_step == 0)
  68. return;
  69. step = _step;
  70. }
  71. // Formats the slider's elements.
  72. void WidgetSliderInput::FormatElements()
  73. {
  74. RMLUI_ZoneScopedNC("RangeOnResize", 0x228044);
  75. Rml::Core::Vector2f box = GetParent()->GetBox().GetSize();
  76. WidgetSlider::FormatElements(box, GetOrientation() == VERTICAL ? box.y : box.x);
  77. }
  78. // Called when the slider's bar position is set or dragged.
  79. float WidgetSliderInput::OnBarChange(float bar_position)
  80. {
  81. float new_value = min_value + bar_position * (max_value - min_value);
  82. int num_steps = Rml::Core::Math::RoundToInteger((new_value - value) / step);
  83. return SetValueInternal(value + num_steps * step);
  84. }
  85. // Called when the slider is incremented by one 'line'.
  86. float WidgetSliderInput::OnLineIncrement()
  87. {
  88. return SetValueInternal(value + step);
  89. }
  90. // Called when the slider is decremented by one 'line'.
  91. float WidgetSliderInput::OnLineDecrement()
  92. {
  93. return SetValueInternal(value - step);
  94. }
  95. // Clamps the new value, sets it on the slider.
  96. float WidgetSliderInput::SetValueInternal(float new_value)
  97. {
  98. if (min_value < max_value)
  99. {
  100. value = Rml::Core::Math::Clamp(new_value, min_value, max_value);
  101. }
  102. else if (min_value > max_value)
  103. {
  104. value = Rml::Core::Math::Clamp(new_value, max_value, min_value);
  105. }
  106. else
  107. {
  108. value = min_value;
  109. return 0;
  110. }
  111. Rml::Core::Dictionary parameters;
  112. parameters["value"] = value;
  113. GetParent()->DispatchEvent(Core::EventId::Change, parameters);
  114. return (value - min_value) / (max_value - min_value);
  115. }
  116. }
  117. }