2
0

Buttons.h 8.8 KB

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