WidgetSlider.h 5.8 KB

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