WidgetSliderInput.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/Controls/ElementFormControl.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 value)
  45. {
  46. float num_steps = (value - min_value) / step;
  47. float new_value = min_value + Rml::Core::Math::RoundFloat(num_steps) * step;
  48. SetBarPosition(SetValueInternal(new_value));
  49. }
  50. float WidgetSliderInput::GetValue() const
  51. {
  52. return value;
  53. }
  54. // Sets the minimum value of the slider.
  55. void WidgetSliderInput::SetMinValue(float _min_value)
  56. {
  57. min_value = _min_value;
  58. }
  59. // Sets the maximum value of the slider.
  60. void WidgetSliderInput::SetMaxValue(float _max_value)
  61. {
  62. max_value = _max_value;
  63. }
  64. // Sets the slider's value increment.
  65. void WidgetSliderInput::SetStep(float _step)
  66. {
  67. // Can't have a zero step!
  68. if (_step == 0)
  69. return;
  70. step = _step;
  71. }
  72. // Formats the slider's elements.
  73. void WidgetSliderInput::FormatElements()
  74. {
  75. RMLUI_ZoneScopedNC("RangeOnResize", 0x228044);
  76. Rml::Core::Vector2f box = GetParent()->GetBox().GetSize();
  77. WidgetSlider::FormatElements(box, GetOrientation() == VERTICAL ? box.y : box.x);
  78. }
  79. // Called when the slider's bar position is set or dragged.
  80. float WidgetSliderInput::OnBarChange(float bar_position)
  81. {
  82. float new_value = min_value + bar_position * (max_value - min_value);
  83. int num_steps = Rml::Core::Math::RoundToInteger((new_value - value) / step);
  84. return SetValueInternal(value + num_steps * step);
  85. }
  86. // Called when the slider is incremented by one 'line'.
  87. float WidgetSliderInput::OnLineIncrement()
  88. {
  89. return SetValueInternal(value + step);
  90. }
  91. // Called when the slider is decremented by one 'line'.
  92. float WidgetSliderInput::OnLineDecrement()
  93. {
  94. return SetValueInternal(value - step);
  95. }
  96. // Called when the slider is incremented by one 'page'.
  97. float WidgetSliderInput::OnPageIncrement(float click_position)
  98. {
  99. return OnBarChange(click_position);
  100. }
  101. // Called when the slider is incremented by one 'page'.
  102. float WidgetSliderInput::OnPageDecrement(float click_position)
  103. {
  104. return OnBarChange(click_position);
  105. }
  106. // Clamps the new value, sets it on the slider.
  107. float WidgetSliderInput::SetValueInternal(float new_value)
  108. {
  109. if (min_value < max_value)
  110. {
  111. value = Rml::Core::Math::Clamp(new_value, min_value, max_value);
  112. }
  113. else if (min_value > max_value)
  114. {
  115. value = Rml::Core::Math::Clamp(new_value, max_value, min_value);
  116. }
  117. else
  118. {
  119. value = min_value;
  120. return 0;
  121. }
  122. Rml::Core::Dictionary parameters;
  123. parameters["value"] = value;
  124. parent->DispatchEvent(Core::EventId::Change, parameters);
  125. return (value - min_value) / (max_value - min_value);
  126. }
  127. }
  128. }