WidgetSlider.h 5.8 KB

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