Buttons.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. VCMI_LIB_NAMESPACE_END
  20. struct SDL_Surface;
  21. struct Rect;
  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> tooltip(const JsonNode & localizedTexts);
  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. int value;
  153. CFunctionList<void(int)> onChange;
  154. std::shared_ptr<CAnimImage> animImage;
  155. const std::pair<std::string, std::string> * const helpHandlers;
  156. void setVolume(const int v);
  157. public:
  158. /// @param position coordinates of slider
  159. /// @param defName name of def animation for slider
  160. /// @param value initial value for volume
  161. /// @param help pointer to first helptext of slider
  162. CVolumeSlider(const Point & position, const std::string & defName, const int value,
  163. const std::pair<std::string, std::string> * const help);
  164. void moveTo(int id);
  165. void addCallback(std::function<void(int)> callback);
  166. void clickLeft(tribool down, bool previousState) override;
  167. void clickRight(tribool down, bool previousState) override;
  168. void wheelScrolled(bool down, bool in) override;
  169. };
  170. /// A typical slider which can be orientated horizontally/vertically.
  171. class CSlider : public CIntObject
  172. {
  173. //if vertical then left=up
  174. std::shared_ptr<CButton> left;
  175. std::shared_ptr<CButton> right;
  176. std::shared_ptr<CButton> slider;
  177. int capacity;//how many elements can be active at same time (e.g. hero list = 5)
  178. int positions; //number of highest position (0 if there is only one)
  179. bool horizontal;
  180. int amount; //total amount of elements (e.g. hero list = 0-8)
  181. int value; //first active element
  182. int scrollStep; // how many elements will be scrolled via one click, default = 1
  183. CFunctionList<void(int)> moved;
  184. void updateSliderPos();
  185. void sliderClicked();
  186. public:
  187. enum EStyle
  188. {
  189. BROWN,
  190. BLUE
  191. };
  192. void block(bool on);
  193. /// Controls how many items wil be scrolled via one click
  194. void setScrollStep(int to);
  195. /// Value modifiers
  196. void moveLeft();
  197. void moveRight();
  198. void moveTo(int value);
  199. void moveBy(int amount);
  200. void moveToMin();
  201. void moveToMax();
  202. /// Amount modifier
  203. void setAmount(int to);
  204. /// Accessors
  205. int getAmount() const;
  206. int getValue() const;
  207. int getCapacity() const;
  208. void addCallback(std::function<void(int)> callback);
  209. void keyPressed(const SDL_KeyboardEvent & key) override;
  210. void wheelScrolled(bool down, bool in) override;
  211. void clickLeft(tribool down, bool previousState) override;
  212. void mouseMoved (const SDL_MouseMotionEvent & sEvent) override;
  213. void showAll(SDL_Surface * to) override;
  214. /// @param position coordinates of slider
  215. /// @param length length of slider ribbon, including left/right buttons
  216. /// @param Moved function that will be called whenever slider moves
  217. /// @param Capacity maximal number of visible at once elements
  218. /// @param Amount total amount of elements, including not visible
  219. /// @param Value starting position
  220. CSlider(Point position, int length, std::function<void(int)> Moved, int Capacity, int Amount,
  221. int Value=0, bool Horizontal=true, EStyle style = BROWN);
  222. ~CSlider();
  223. };