Scrollable.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_PANNING, position)
  14. , scrollStep(1)
  15. , panningDistanceSingle(32)
  16. , panningDistanceAccumulated(0)
  17. , orientation(orientation)
  18. {
  19. }
  20. void Scrollable::panning(bool on)
  21. {
  22. panningDistanceAccumulated = 0;
  23. }
  24. void Scrollable::wheelScrolled(int distance)
  25. {
  26. if (orientation == Orientation::HORIZONTAL)
  27. scrollBy(distance * 3);
  28. else
  29. scrollBy(-distance * 3);
  30. }
  31. void Scrollable::gesturePanning(const Point & distanceDelta)
  32. {
  33. if (orientation == Orientation::HORIZONTAL)
  34. panningDistanceAccumulated += -distanceDelta.x;
  35. else
  36. panningDistanceAccumulated += distanceDelta.y;
  37. if (-panningDistanceAccumulated > panningDistanceSingle )
  38. {
  39. int scrollAmount = (-panningDistanceAccumulated) / panningDistanceSingle;
  40. scrollBy(-scrollAmount);
  41. panningDistanceAccumulated += scrollAmount * panningDistanceSingle;
  42. }
  43. if (panningDistanceAccumulated > panningDistanceSingle )
  44. {
  45. int scrollAmount = panningDistanceAccumulated / panningDistanceSingle;
  46. scrollBy(scrollAmount);
  47. panningDistanceAccumulated += -scrollAmount * panningDistanceSingle;
  48. }
  49. }
  50. int Scrollable::getScrollStep() const
  51. {
  52. return scrollStep;
  53. }
  54. Orientation Scrollable::getOrientation() const
  55. {
  56. return orientation;
  57. }
  58. void Scrollable::scrollNext()
  59. {
  60. scrollBy(+1);
  61. }
  62. void Scrollable::scrollPrev()
  63. {
  64. scrollBy(-1);
  65. }
  66. void Scrollable::setScrollStep(int to)
  67. {
  68. scrollStep = to;
  69. }
  70. void Scrollable::setPanningStep(int to)
  71. {
  72. panningDistanceSingle = to;
  73. }
  74. void Scrollable::setScrollingEnabled(bool on)
  75. {
  76. if (on)
  77. addUsedEvents(WHEEL | GESTURE_PANNING);
  78. else
  79. removeUsedEvents(WHEEL | GESTURE_PANNING);
  80. }