WidgetSlider.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #ifndef RMLUI_CORE_ELEMENTS_WIDGETSLIDER_H
  29. #define RMLUI_CORE_ELEMENTS_WIDGETSLIDER_H
  30. #include "../../../Include/RmlUi/Core/EventListener.h"
  31. namespace Rml {
  32. class ElementFormControl;
  33. /**
  34. A generic widget for incorporating sliding functionality into an element.
  35. @author Peter Curry
  36. */
  37. class WidgetSlider final : public EventListener
  38. {
  39. public:
  40. enum Orientation
  41. {
  42. VERTICAL,
  43. HORIZONTAL
  44. };
  45. WidgetSlider(ElementFormControl* parent);
  46. virtual ~WidgetSlider();
  47. /// Initialises the slider's hidden elements.
  48. bool Initialise();
  49. /// Updates the key repeats for the increment / decrement arrows.
  50. void Update();
  51. /// Sets the position of the bar.
  52. /// @param[in] bar_position The new position of the bar (0 representing the start of the track, 1 representing the end).
  53. void SetBarPosition(float bar_position);
  54. /// Returns the current position of the bar.
  55. /// @return The current position of the bar (0 representing the start of the track, 1 representing the end).
  56. float GetBarPosition();
  57. /// Sets the orientation of the slider.
  58. void SetOrientation(Orientation orientation);
  59. /// Returns the slider's orientation.
  60. Orientation GetOrientation() const;
  61. /// Sets the dimensions to the size of the slider.
  62. void GetDimensions(Vector2f& dimensions) const;
  63. /// Sets a new value on the slider, clamped to the min and max values, and rounded to the nearest increment.
  64. void SetValue(float value);
  65. /// Returns the current value of the slider.
  66. float GetValue() const;
  67. /// Sets the minimum value of the slider.
  68. void SetMinValue(float min_value);
  69. /// Sets the maximum value of the slider.
  70. void SetMaxValue(float max_value);
  71. /// Sets the slider's value increment.
  72. void SetStep(float step);
  73. /// Formats the slider's elements.
  74. void FormatElements();
  75. private:
  76. /// Lays out and resizes the slider's internal elements.
  77. /// @param[in] containing_block The padded box containing the slider. This is used to resolve relative properties.
  78. /// @param[in] slider_length The total length, in pixels, of the slider widget.
  79. void FormatElements(Vector2f containing_block, float slider_length);
  80. /// Lays out and positions the bar element.
  81. void FormatBar();
  82. /// Returns the widget's parent element.
  83. Element* GetParent() const;
  84. /// Handles events coming through from the slider's components.
  85. void ProcessEvent(Event& event) override;
  86. /// Called when the slider's bar position is set or dragged.
  87. /// @param[in] bar_position The new position of the bar (0 representing the start of the track, 1 representing the end).
  88. /// @return The new position of the bar.
  89. float OnBarChange(float bar_position);
  90. /// Called when the slider is incremented by one 'line', either by the down / right key or a mouse-click on the
  91. /// increment arrow.
  92. /// @return The new position of the bar.
  93. float OnLineIncrement();
  94. /// Called when the slider is decremented by one 'line', either by the up / left key or a mouse-click on the
  95. /// decrement arrow.
  96. /// @return The new position of the bar.
  97. float OnLineDecrement();
  98. /// Determine the normalized bar position given an absolute position coordinate.
  99. /// @param[in] absolute_position Absolute position along the axis determined by 'orientation'.
  100. /// @return The normalized bar position [0, 1]
  101. float AbsolutePositionToBarPosition(float absolute_position) const;
  102. void PositionBar();
  103. /// Clamps the new value, sets it on the slider and returns it as a number from 0 to 1, 0 being the minimum
  104. /// value and 1 the maximum.
  105. /// @param[in] new_value The new value to set on the slider.
  106. /// @return The new parametric value of the slider.
  107. float SetValueInternal(float new_value);
  108. ElementFormControl* parent;
  109. Orientation orientation;
  110. // The background track element, across which the bar slides.
  111. Element* track;
  112. // The bar element. This is the element that is dragged across the trough.
  113. Element* bar;
  114. // The two (optional) buttons for incrementing and decrementing the slider.
  115. Element* arrows[2];
  116. // A number from 0 to 1, indicating how far along the track the bar is.
  117. float bar_position;
  118. // If the bar is being dragged, this is the pixel offset from the start of the bar to where it was picked up.
  119. float bar_drag_anchor;
  120. // Set to the auto-repeat timer if either of the arrow buttons have been pressed, -1 if they haven't.
  121. float arrow_timers[2];
  122. double last_update_time;
  123. float value;
  124. float min_value;
  125. float max_value;
  126. float step;
  127. };
  128. } // namespace Rml
  129. #endif