Buttons.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. };
  113. /// A button which can be selected/deselected, checkbox
  114. class CToggleButton : public CButton, public CToggleBase
  115. {
  116. void doSelect(bool on) override;
  117. public:
  118. CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  119. CFunctionList<void(bool)> Callback = 0, int key=0, bool playerColoredButton = false );
  120. void clickLeft(tribool down, bool previousState) override;
  121. // bring overrides into scope
  122. //using CButton::addCallback;
  123. using CToggleBase::addCallback;
  124. };
  125. class CToggleGroup : public CIntObject
  126. {
  127. CFunctionList<void(int)> onChange; //called when changing selected button with new button's id
  128. int selectedID;
  129. void selectionChanged(int to);
  130. public:
  131. std::map<int, std::shared_ptr<CToggleBase>> buttons;
  132. CToggleGroup(const CFunctionList<void(int)> & OnChange);
  133. void addCallback(std::function<void(int)> callback);
  134. void resetCallback();
  135. /// add one toggle/button into group
  136. void addToggle(int index, std::shared_ptr<CToggleBase> button);
  137. /// Changes selection to specific value. Will select toggle with this ID, if present
  138. void setSelected(int id);
  139. };
  140. /// A typical slider for volume with an animated indicator
  141. class CVolumeSlider : public CIntObject
  142. {
  143. int value;
  144. CFunctionList<void(int)> onChange;
  145. std::shared_ptr<CAnimImage> animImage;
  146. const std::pair<std::string, std::string> * const helpHandlers;
  147. void setVolume(const int v);
  148. public:
  149. /// @param position coordinates of slider
  150. /// @param defName name of def animation for slider
  151. /// @param value initial value for volume
  152. /// @param help pointer to first helptext of slider
  153. CVolumeSlider(const Point & position, const std::string & defName, const int value,
  154. const std::pair<std::string, std::string> * const help);
  155. void moveTo(int id);
  156. void addCallback(std::function<void(int)> callback);
  157. void clickLeft(tribool down, bool previousState) override;
  158. void clickRight(tribool down, bool previousState) override;
  159. void wheelScrolled(bool down, bool in) override;
  160. };
  161. /// A typical slider which can be orientated horizontally/vertically.
  162. class CSlider : public CIntObject
  163. {
  164. //if vertical then left=up
  165. std::shared_ptr<CButton> left;
  166. std::shared_ptr<CButton> right;
  167. std::shared_ptr<CButton> slider;
  168. int capacity;//how many elements can be active at same time (e.g. hero list = 5)
  169. int positions; //number of highest position (0 if there is only one)
  170. bool horizontal;
  171. int amount; //total amount of elements (e.g. hero list = 0-8)
  172. int value; //first active element
  173. int scrollStep; // how many elements will be scrolled via one click, default = 1
  174. CFunctionList<void(int)> moved;
  175. void updateSliderPos();
  176. void sliderClicked();
  177. public:
  178. enum EStyle
  179. {
  180. BROWN,
  181. BLUE
  182. };
  183. void block(bool on);
  184. /// Controls how many items wil be scrolled via one click
  185. void setScrollStep(int to);
  186. /// Value modifiers
  187. void moveLeft();
  188. void moveRight();
  189. void moveTo(int value);
  190. void moveBy(int amount);
  191. void moveToMin();
  192. void moveToMax();
  193. /// Amount modifier
  194. void setAmount(int to);
  195. /// Accessors
  196. int getAmount();
  197. int getValue();
  198. void addCallback(std::function<void(int)> callback);
  199. void keyPressed(const SDL_KeyboardEvent & key) override;
  200. void wheelScrolled(bool down, bool in) override;
  201. void clickLeft(tribool down, bool previousState) override;
  202. void mouseMoved (const SDL_MouseMotionEvent & sEvent) override;
  203. void showAll(SDL_Surface * to) override;
  204. /// @param position coordinates of slider
  205. /// @param length length of slider ribbon, including left/right buttons
  206. /// @param Moved function that will be called whenever slider moves
  207. /// @param Capacity maximal number of visible at once elements
  208. /// @param Amount total amount of elements, including not visible
  209. /// @param Value starting position
  210. CSlider(Point position, int length, std::function<void(int)> Moved, int Capacity, int Amount,
  211. int Value=0, bool Horizontal=true, EStyle style = BROWN);
  212. ~CSlider();
  213. };