ScrollBar.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #ifndef FREESHOP_SCROLLBAR_HPP
  2. #define FREESHOP_SCROLLBAR_HPP
  3. #include <vector>
  4. #include <cpp3ds/Graphics/Rect.hpp>
  5. #include <cpp3ds/Window/Event.hpp>
  6. #include <TweenEngine/TweenManager.h>
  7. #include <cpp3ds/System/Clock.hpp>
  8. #include "Scrollable.hpp"
  9. #include "NinePatch.hpp"
  10. #include "../TweenObjects.hpp"
  11. namespace FreeShop {
  12. class ScrollBar : public cpp3ds::Drawable, public util3ds::TweenTransformable<cpp3ds::Transformable> {
  13. public:
  14. static const int ALPHA = 11; // for tweening
  15. ScrollBar();
  16. ~ScrollBar();
  17. void attachObject(Scrollable *object);
  18. void detachObject(Scrollable *object);
  19. void setScroll(float scrollPos);
  20. void setScrollRelative(float scrollDelta);
  21. void setPosition(const cpp3ds::Vector2f& position);
  22. void setPosition(float x, float y);
  23. const cpp3ds::Vector2f& getPosition() const;
  24. void setDragRect(const cpp3ds::IntRect &rect);
  25. void setSize(const cpp3ds::Vector2u &size);
  26. void setScrollAreaSize(const cpp3ds::Vector2u &size);
  27. void setDeceleration(float rate) { m_deceleration = rate; }
  28. void setColor(const cpp3ds::Color &color);
  29. void setAutoHide(bool autoHide) { m_autoHide = autoHide; }
  30. void show();
  31. void hide();
  32. void markDirty();
  33. // Returns true when event is captured
  34. bool processEvent(const cpp3ds::Event &event);
  35. void update(float delta);
  36. protected:
  37. virtual void draw(cpp3ds::RenderTarget& target, cpp3ds::RenderStates states) const;
  38. virtual void setValues(int tweenType, float *newValues);
  39. virtual int getValues(int tweenType, float *returnValues);
  40. void ensureUpdateScrollBar() const;
  41. private:
  42. bool m_autoHide;
  43. bool m_isHidden;
  44. bool m_isTouching;
  45. bool m_isScrolling;
  46. float m_scrollPos;
  47. mutable float m_scrollPosMin;
  48. mutable float m_scrollPosMax;
  49. mutable float m_scrollSize;
  50. float m_scrollTolerance; // Value to move before scrolling starts
  51. cpp3ds::Vector2i m_lastTouchPos;
  52. cpp3ds::Clock m_clockHide;
  53. cpp3ds::Color m_color;
  54. mutable bool m_needsUpdate;
  55. mutable std::vector<Scrollable*> m_scrollObjects;
  56. cpp3ds::IntRect m_dragRect;
  57. cpp3ds::Vector2u m_scrollAreaSize;
  58. cpp3ds::Vector2u m_size;
  59. cpp3ds::Vector2f m_position;
  60. gui3ds::NinePatch m_scrollBar;
  61. TweenEngine::TweenManager m_tweenManager;
  62. // Kinetic scrolling
  63. float m_deceleration; // Rate at which the velocity slows to a stop (1.f to never end, 0.f for instant stop)
  64. float m_velocity; // Store current velocity
  65. float m_veloctyModifer; // For fine tuning velocity
  66. cpp3ds::Clock m_clockVelocity;
  67. cpp3ds::Vector2i m_startTouchPos;
  68. };
  69. } // namespace FreeShop
  70. #endif // FREESHOP_SCROLLBAR_HPP