Slider.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. void sliderClicked();
  33. public:
  34. enum EStyle
  35. {
  36. BROWN,
  37. BLUE
  38. };
  39. void block(bool on);
  40. /// If set, mouse scroll will only scroll slider when inside of this area
  41. void setScrollBounds(const Rect & bounds );
  42. void clearScrollBounds();
  43. /// Value modifiers
  44. void scrollTo(int value);
  45. void scrollBy(int amount) override;
  46. void scrollToMin();
  47. void scrollToMax();
  48. /// Amount modifier
  49. void setAmount(int to);
  50. /// Accessors
  51. int getAmount() const;
  52. 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 clickLeft(tribool down, bool previousState) override;
  58. void mouseMoved (const Point & cursorPosition) override;
  59. void showAll(Canvas & to) override;
  60. /// @param position coordinates of slider
  61. /// @param length length of slider ribbon, including left/right buttons
  62. /// @param Moved function that will be called whenever slider moves
  63. /// @param Capacity maximal number of visible at once elements
  64. /// @param Amount total amount of elements, including not visible
  65. /// @param Value starting position
  66. CSlider(Point position, int length, std::function<void(int)> Moved, int Capacity, int Amount,
  67. int Value=0, bool Horizontal=true, EStyle style = BROWN);
  68. ~CSlider();
  69. };