Slider.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. //if vertical then left=up
  18. std::shared_ptr<CButton> left;
  19. std::shared_ptr<CButton> right;
  20. std::shared_ptr<CButton> slider;
  21. std::optional<Rect> scrollBounds;
  22. /// how many elements are visible simultaneously
  23. int capacity;
  24. /// number of highest position, or 0 if there is only one
  25. int positions;
  26. /// total amount of elements in the list
  27. int amount;
  28. /// topmost vislble (first active) element
  29. int value;
  30. CFunctionList<void(int)> moved;
  31. void updateSliderPos();
  32. public:
  33. enum EStyle
  34. {
  35. BROWN,
  36. BLUE
  37. };
  38. void block(bool on);
  39. /// If set, mouse scroll will only scroll slider when inside of this area
  40. void setScrollBounds(const Rect & bounds );
  41. void clearScrollBounds();
  42. /// Value modifiers
  43. void scrollTo(int value);
  44. void scrollBy(int amount) override;
  45. void scrollToMin();
  46. void scrollToMax();
  47. /// Amount modifier
  48. void setAmount(int to);
  49. virtual void setValue(int to);
  50. /// Accessors
  51. int getAmount() const;
  52. virtual int getValue() const;
  53. int getCapacity() const;
  54. void addCallback(std::function<void(int)> callback);
  55. bool receiveEvent(const Point & position, int eventType) const override;
  56. void keyPressed(EShortcut key) override;
  57. void clickPressed(const Point & cursorPosition) override;
  58. void mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance) override;
  59. void gesturePanning(const Point & initialPosition, const Point & currentPosition, const Point & lastUpdateDistance) override;
  60. void showAll(Canvas & to) override;
  61. /// @param position coordinates of slider
  62. /// @param length length of slider ribbon, including left/right buttons
  63. /// @param Moved function that will be called whenever slider moves
  64. /// @param Capacity maximal number of visible at once elements
  65. /// @param Amount total amount of elements, including not visible
  66. /// @param Value starting position
  67. CSlider(Point position, int length, const std::function<void(int)> & Moved, int Capacity, int Amount,
  68. int Value, Orientation orientation, EStyle style = BROWN);
  69. ~CSlider();
  70. };
  71. class SliderNonlinear : public CSlider
  72. {
  73. /// 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
  74. std::vector<int> scaledValues;
  75. using CSlider::setAmount; // make private
  76. public:
  77. void setValue(int to) override;
  78. int getValue() const override;
  79. SliderNonlinear(Point position, int length, const std::function<void(int)> & Moved, const std::vector<int> & values, int Value, Orientation orientation, EStyle style);
  80. };