Buttons.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Buttons.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 "../gui/CIntObject.h"
  12. #include "../render/Colors.h"
  13. #include "../../lib/FunctionList.h"
  14. #include <SDL_pixels.h>
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class Rect;
  17. VCMI_LIB_NAMESPACE_END
  18. class CAnimImage;
  19. class CLabel;
  20. class CAnimation;
  21. /// Typical Heroes 3 button which can be inactive or active and can
  22. /// hold further information if you right-click it
  23. class CButton : public CKeyShortcut
  24. {
  25. CFunctionList<void()> callback;
  26. public:
  27. enum ButtonState
  28. {
  29. NORMAL=0,
  30. PRESSED=1,
  31. BLOCKED=2,
  32. HIGHLIGHTED=3
  33. };
  34. private:
  35. std::vector<std::string> imageNames;//store list of images that can be used by this button
  36. size_t currentImage;
  37. ButtonState state;//current state of button from enum
  38. std::array<int, 4> stateToIndex; // mapping of button state to index of frame in animation
  39. std::array<std::string, 4> hoverTexts; //texts for statusbar, if empty - first entry will be used
  40. std::array<std::optional<SDL_Color>, 4> stateToBorderColor; // mapping of button state to border color
  41. std::string helpBox; //for right-click help
  42. std::shared_ptr<CAnimImage> image; //image for this button
  43. std::shared_ptr<CIntObject> overlay;//object-overlay, can be null
  44. bool animateLonelyFrame = false;
  45. protected:
  46. void onButtonClicked(); // calls callback
  47. void update();//to refresh button after image or text change
  48. // internal method to change state. Public change can be done only via block()
  49. void setState(ButtonState newState);
  50. ButtonState getState();
  51. public:
  52. bool actOnDown,//runs when mouse is pressed down over it, not when up
  53. hoverable,//if true, button will be highlighted when hovered (e.g. main menu)
  54. soundDisabled;
  55. // sets border color for each button state;
  56. // if it's set, the button will have 1-px border around it with this color
  57. void setBorderColor(std::optional<SDL_Color> normalBorderColor,
  58. std::optional<SDL_Color> pressedBorderColor,
  59. std::optional<SDL_Color> blockedBorderColor,
  60. std::optional<SDL_Color> highlightedBorderColor);
  61. // sets the same border color for all button states.
  62. void setBorderColor(std::optional<SDL_Color> borderColor);
  63. /// adds one more callback to on-click actions
  64. void addCallback(std::function<void()> callback);
  65. /// adds overlay on top of button image. Only one overlay can be active at once
  66. void addOverlay(std::shared_ptr<CIntObject> newOverlay);
  67. void addTextOverlay(const std::string & Text, EFonts font, SDL_Color color = Colors::WHITE);
  68. void addImage(std::string filename);
  69. void addHoverText(ButtonState state, std::string text);
  70. void setImageOrder(int state1, int state2, int state3, int state4);
  71. void setAnimateLonelyFrame(bool agreement);
  72. void block(bool on);
  73. /// State modifiers
  74. bool isBlocked();
  75. bool isHighlighted();
  76. /// Constructor
  77. CButton(Point position, const std::string & defName, const std::pair<std::string, std::string> & help,
  78. CFunctionList<void()> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
  79. /// Appearance modifiers
  80. void setIndex(size_t index);
  81. void setImage(std::shared_ptr<CAnimation> anim, int animFlags=0);
  82. void setPlayerColor(PlayerColor player);
  83. /// CIntObject overrides
  84. void clickRight(tribool down, bool previousState) override;
  85. void clickLeft(tribool down, bool previousState) override;
  86. void hover (bool on) override;
  87. void showAll(Canvas & to) override;
  88. /// generates tooltip that can be passed into constructor
  89. static std::pair<std::string, std::string> tooltip();
  90. static std::pair<std::string, std::string> tooltipLocalized(const std::string & key);
  91. static std::pair<std::string, std::string> tooltip(const std::string & hover, const std::string & help = "");
  92. };
  93. class CToggleBase
  94. {
  95. CFunctionList<void(bool)> callback;
  96. protected:
  97. bool selected;
  98. // internal method for overrides
  99. virtual void doSelect(bool on);
  100. // returns true if toggle can change its state
  101. bool canActivate();
  102. public:
  103. /// if set to false - button can not be deselected normally
  104. bool allowDeselection;
  105. CToggleBase(CFunctionList<void(bool)> callback);
  106. virtual ~CToggleBase();
  107. /// Changes selection to "on", and calls callback
  108. void setSelected(bool on);
  109. void addCallback(std::function<void(bool)> callback);
  110. /// Set whether the toggle is currently enabled for user to use, this is only inplemented in ToggleButton, not for other toggles yet.
  111. virtual void setEnabled(bool enabled);
  112. };
  113. /// A button which can be selected/deselected, checkbox
  114. class CToggleButton : public CButton, public CToggleBase
  115. {
  116. void doSelect(bool on) override;
  117. void setEnabled(bool enabled) override;
  118. public:
  119. CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  120. CFunctionList<void(bool)> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
  121. void clickLeft(tribool down, bool previousState) override;
  122. // bring overrides into scope
  123. //using CButton::addCallback;
  124. using CToggleBase::addCallback;
  125. };
  126. class CToggleGroup : public CIntObject
  127. {
  128. CFunctionList<void(int)> onChange; //called when changing selected button with new button's id
  129. int selectedID;
  130. void selectionChanged(int to);
  131. public:
  132. std::map<int, std::shared_ptr<CToggleBase>> buttons;
  133. CToggleGroup(const CFunctionList<void(int)> & OnChange);
  134. void addCallback(std::function<void(int)> callback);
  135. void resetCallback();
  136. /// add one toggle/button into group
  137. void addToggle(int index, std::shared_ptr<CToggleBase> button);
  138. /// Changes selection to specific value. Will select toggle with this ID, if present
  139. void setSelected(int id);
  140. /// in some cases, e.g. LoadGame difficulty selection, after refreshing the UI, the ToggleGroup should
  141. /// reset all of it's child buttons to BLOCK state, then make selection again
  142. void setSelectedOnly(int id);
  143. int getSelected() const;
  144. };
  145. /// A typical slider which can be orientated horizontally/vertically.
  146. class CSlider : public CIntObject
  147. {
  148. //if vertical then left=up
  149. std::shared_ptr<CButton> left;
  150. std::shared_ptr<CButton> right;
  151. std::shared_ptr<CButton> slider;
  152. std::optional<Rect> scrollBounds;
  153. int capacity;//how many elements can be active at same time (e.g. hero list = 5)
  154. int positions; //number of highest position (0 if there is only one)
  155. bool horizontal;
  156. int amount; //total amount of elements (e.g. hero list = 0-8)
  157. int value; //first active element
  158. int scrollStep; // how many elements will be scrolled via one click, default = 1
  159. CFunctionList<void(int)> moved;
  160. void updateSliderPos();
  161. void sliderClicked();
  162. public:
  163. enum EStyle
  164. {
  165. BROWN,
  166. BLUE
  167. };
  168. void block(bool on);
  169. /// Controls how many items wil be scrolled via one click
  170. void setScrollStep(int to);
  171. /// If set, mouse scroll will only scroll slider when inside of this area
  172. void setScrollBounds(const Rect & bounds );
  173. void clearScrollBounds();
  174. /// Value modifiers
  175. void moveLeft();
  176. void moveRight();
  177. void moveTo(int value);
  178. void moveBy(int amount);
  179. void moveToMin();
  180. void moveToMax();
  181. /// Amount modifier
  182. void setAmount(int to);
  183. /// Accessors
  184. int getAmount() const;
  185. int getValue() const;
  186. int getCapacity() const;
  187. void addCallback(std::function<void(int)> callback);
  188. void keyPressed(EShortcut key) override;
  189. void wheelScrolled(bool down, bool in) override;
  190. void clickLeft(tribool down, bool previousState) override;
  191. void mouseMoved (const Point & cursorPosition) override;
  192. void showAll(Canvas & to) override;
  193. /// @param position coordinates of slider
  194. /// @param length length of slider ribbon, including left/right buttons
  195. /// @param Moved function that will be called whenever slider moves
  196. /// @param Capacity maximal number of visible at once elements
  197. /// @param Amount total amount of elements, including not visible
  198. /// @param Value starting position
  199. CSlider(Point position, int length, std::function<void(int)> Moved, int Capacity, int Amount,
  200. int Value=0, bool Horizontal=true, EStyle style = BROWN);
  201. ~CSlider();
  202. };