Buttons.h 8.8 KB

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