Slider.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "BorderImage.h"
  25. /// %Slider bar %UI element.
  26. class Slider : public BorderImage
  27. {
  28. OBJECT(Slider);
  29. using UIElement::SetStyle;
  30. public:
  31. /// Construct.
  32. Slider(Context* context);
  33. /// Destruct.
  34. virtual ~Slider();
  35. /// Register object factory.
  36. static void RegisterObject(Context* context);
  37. /// %Set UI element style from XML data.
  38. virtual void SetStyle(const XMLElement& element);
  39. /// Perform UI element update.
  40. virtual void Update(float timeStep);
  41. /// React to mouse hover.
  42. virtual void OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
  43. /// React to mouse drag start.
  44. virtual void OnDragStart(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
  45. /// React to mouse drag motion.
  46. virtual void OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor);
  47. /// React to mouse drag end.
  48. virtual void OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor);
  49. /// React to resize.
  50. virtual void OnResize();
  51. /// %Set orientation type.
  52. void SetOrientation(Orientation orientation);
  53. /// %Set slider range maximum value (minimum value is always 0.)
  54. void SetRange(float range);
  55. /// %Set slider current value.
  56. void SetValue(float value);
  57. /// Change value by a delta.
  58. void ChangeValue(float delta);
  59. /// Return orientation type.
  60. Orientation GetOrientation() const { return orientation_; }
  61. /// Return slider range.
  62. float GetRange() const { return range_; }
  63. /// Return slider current value.
  64. float GetValue() const { return value_; }
  65. /// Return knob element.
  66. BorderImage* GetKnob() const { return knob_; }
  67. protected:
  68. /// Update slider knob position & size.
  69. void UpdateSlider();
  70. /// Slider knob.
  71. SharedPtr<BorderImage> knob_;
  72. /// Orientation.
  73. Orientation orientation_;
  74. /// Slider range.
  75. float range_;
  76. /// Slider current value.
  77. float value_;
  78. /// Internal flag of whether the slider is being dragged.
  79. bool dragSlider_;
  80. /// Original mouse cursor position at drag start.
  81. IntVector2 dragStartCursor_;
  82. /// Original slider position at drag start.
  83. IntVector2 dragStartPosition_;
  84. };