Slider.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Slider.h, 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. #pragma once
  11. #include "Scrollable.h"
  12. #include "../../lib/FunctionList.h"
  13. class CButton;
  14. /// A typical slider which can be orientated horizontally/vertically.
  15. class CSlider : public Scrollable
  16. {
  17. public:
  18. enum EStyle
  19. {
  20. BROWN,
  21. BLUE
  22. };
  23. private:
  24. //if vertical then left=up
  25. std::shared_ptr<CButton> left;
  26. std::shared_ptr<CButton> right;
  27. std::shared_ptr<CButton> slider;
  28. std::optional<Rect> scrollBounds;
  29. /// how many elements are visible simultaneously
  30. int capacity;
  31. /// number of highest position, or 0 if there is only one
  32. int positions;
  33. /// total amount of elements in the list
  34. int amount;
  35. /// topmost vislble (first active) element
  36. int value;
  37. /// length of slider
  38. int length;
  39. /// length of slider button
  40. int barLength;
  41. /// color of slider
  42. EStyle style;
  43. CFunctionList<void(int)> moved;
  44. void updateSliderPos();
  45. void updateSlider();
  46. double getClickPos(const Point & cursorPosition);
  47. public:
  48. void block(bool on);
  49. /// If set, mouse scroll will only scroll slider when inside of this area
  50. void setScrollBounds(const Rect & bounds );
  51. void clearScrollBounds();
  52. /// Value modifiers
  53. void scrollTo(int value, bool callCallbacks = true);
  54. void scrollBy(int amount) override;
  55. void scrollToMin();
  56. void scrollToMax();
  57. /// Amount modifier
  58. void setAmount(int to);
  59. virtual void setValue(int to, bool callCallbacks = true);
  60. /// Accessors
  61. int getAmount() const;
  62. virtual int getValue() const;
  63. int getCapacity() const;
  64. void addCallback(std::function<void(int)> callback);
  65. bool receiveEvent(const Point & position, int eventType) const override;
  66. void keyPressed(EShortcut key) override;
  67. void clickPressed(const Point & cursorPosition) override;
  68. void clickReleased(const Point & cursorPosition) override;
  69. void mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance) override;
  70. void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override;
  71. void showAll(Canvas & to) override;
  72. using SliderMovingFunctor = std::function<void(int)>;
  73. /// @param position coordinates of slider
  74. /// @param length length of slider ribbon, including left/right buttons
  75. /// @param Moved function that will be called whenever slider moves
  76. /// @param Capacity maximal number of visible at once elements
  77. /// @param Amount total amount of elements, including not visible
  78. /// @param Value starting position
  79. CSlider(Point position, int length, const SliderMovingFunctor & Moved, int Capacity, int Amount,
  80. int Value, Orientation orientation, EStyle style = BROWN);
  81. ~CSlider();
  82. };
  83. class SliderNonlinear : public CSlider
  84. {
  85. /// If non-empty then slider has non-linear values, e.g. if slider is at position 5 out of 10 then actual "value" is not 5, but 5th value in this vector
  86. std::vector<int> scaledValues;
  87. using CSlider::setAmount; // make private
  88. public:
  89. void setValue(int to, bool callCallbacks) override;
  90. int getValue() const override;
  91. SliderNonlinear(Point position, int length, const std::function<void(int)> & Moved, const std::vector<int> & values, int Value, Orientation orientation, EStyle style);
  92. };