WidgetSliderInput.cpp 3.8 KB

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