WidgetSlider.h 5.5 KB

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