Buttons.h 8.7 KB

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