WidgetSliderInput.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include "../../Include/RmlUi/Core/Profiling.h"
  32. namespace Rml {
  33. namespace Controls {
  34. WidgetSliderInput::WidgetSliderInput(ElementFormControl* parent) : WidgetSlider(parent)
  35. {
  36. value = 0;
  37. min_value = 0;
  38. max_value = 100;
  39. step = 1;
  40. }
  41. WidgetSliderInput::~WidgetSliderInput()
  42. {
  43. }
  44. void WidgetSliderInput::SetValue(float target_value)
  45. {
  46. float num_steps = (target_value - min_value) / step;
  47. float new_value = min_value + Rml::Core::Math::RoundFloat(num_steps) * step;
  48. if(new_value != value)
  49. SetBarPosition(SetValueInternal(new_value));
  50. }
  51. float WidgetSliderInput::GetValue() const
  52. {
  53. return value;
  54. }
  55. // Sets the minimum value of the slider.
  56. void WidgetSliderInput::SetMinValue(float _min_value)
  57. {
  58. min_value = _min_value;
  59. }
  60. // Sets the maximum value of the slider.
  61. void WidgetSliderInput::SetMaxValue(float _max_value)
  62. {
  63. max_value = _max_value;
  64. }
  65. // Sets the slider's value increment.
  66. void WidgetSliderInput::SetStep(float _step)
  67. {
  68. // Can't have a zero step!
  69. if (_step == 0)
  70. return;
  71. step = _step;
  72. }
  73. // Formats the slider's elements.
  74. void WidgetSliderInput::FormatElements()
  75. {
  76. RMLUI_ZoneScopedNC("RangeOnResize", 0x228044);
  77. Rml::Core::Vector2f box = GetParent()->GetBox().GetSize();
  78. WidgetSlider::FormatElements(box, GetOrientation() == VERTICAL ? box.y : box.x);
  79. }
  80. // Called when the slider's bar position is set or dragged.
  81. float WidgetSliderInput::OnBarChange(float bar_position)
  82. {
  83. float new_value = min_value + bar_position * (max_value - min_value);
  84. int num_steps = Rml::Core::Math::RoundToInteger((new_value - value) / step);
  85. return SetValueInternal(value + num_steps * step);
  86. }
  87. // Called when the slider is incremented by one 'line'.
  88. float WidgetSliderInput::OnLineIncrement()
  89. {
  90. return SetValueInternal(value + step);
  91. }
  92. // Called when the slider is decremented by one 'line'.
  93. float WidgetSliderInput::OnLineDecrement()
  94. {
  95. return SetValueInternal(value - step);
  96. }
  97. // Clamps the new value, sets it on the slider.
  98. float WidgetSliderInput::SetValueInternal(float new_value)
  99. {
  100. if (min_value < max_value)
  101. {
  102. value = Rml::Core::Math::Clamp(new_value, min_value, max_value);
  103. }
  104. else if (min_value > max_value)
  105. {
  106. value = Rml::Core::Math::Clamp(new_value, max_value, min_value);
  107. }
  108. else
  109. {
  110. value = min_value;
  111. return 0;
  112. }
  113. Rml::Core::Dictionary parameters;
  114. parameters["value"] = value;
  115. GetParent()->DispatchEvent(Core::EventId::Change, parameters);
  116. // TODO: This might not be the safest approach as this will call SetValue(),
  117. // thus, a slight mismatch will result in infinite recursion.
  118. GetParent()->SetAttribute("value", value);
  119. return (value - min_value) / (max_value - min_value);
  120. }
  121. }
  122. }