WidgetSlider.h 5.7 KB

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