Scrollable.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Scrollable.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Scrollable.h"
  12. Scrollable::Scrollable(int used, Point position, Orientation orientation)
  13. : CIntObject(used | WHEEL | GESTURE, position)
  14. , scrollStep(1)
  15. , panningDistanceSingle(32)
  16. , panningDistanceAccumulated(0)
  17. , orientation(orientation)
  18. {
  19. }
  20. void Scrollable::gesture(bool on, const Point & initialPosition, const Point & finalPosition)
  21. {
  22. panningDistanceAccumulated = 0;
  23. }
  24. void Scrollable::wheelScrolled(int distance)
  25. {
  26. scrollBy(-distance * scrollStep);
  27. }
  28. void Scrollable::gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance)
  29. {
  30. if (orientation == Orientation::HORIZONTAL)
  31. panningDistanceAccumulated += -lastUpdateDistance.x;
  32. else
  33. panningDistanceAccumulated += lastUpdateDistance.y;
  34. if (-panningDistanceAccumulated > panningDistanceSingle )
  35. {
  36. int scrollAmount = (-panningDistanceAccumulated) / panningDistanceSingle;
  37. scrollBy(-scrollAmount);
  38. panningDistanceAccumulated += scrollAmount * panningDistanceSingle;
  39. }
  40. if (panningDistanceAccumulated > panningDistanceSingle )
  41. {
  42. int scrollAmount = panningDistanceAccumulated / panningDistanceSingle;
  43. scrollBy(scrollAmount);
  44. panningDistanceAccumulated += -scrollAmount * panningDistanceSingle;
  45. }
  46. }
  47. int Scrollable::getScrollStep() const
  48. {
  49. return scrollStep;
  50. }
  51. Orientation Scrollable::getOrientation() const
  52. {
  53. return orientation;
  54. }
  55. void Scrollable::scrollNext()
  56. {
  57. scrollBy(+1);
  58. }
  59. void Scrollable::scrollPrev()
  60. {
  61. scrollBy(-1);
  62. }
  63. void Scrollable::setScrollStep(int to)
  64. {
  65. scrollStep = to;
  66. }
  67. void Scrollable::setPanningStep(int to)
  68. {
  69. panningDistanceSingle = to;
  70. }
  71. void Scrollable::setScrollingEnabled(bool on)
  72. {
  73. if (on)
  74. addUsedEvents(WHEEL | GESTURE);
  75. else
  76. removeUsedEvents(WHEEL | GESTURE);
  77. }