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