Buttons.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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::string helpBox; //for right-click help
  43. CAnimImage * image; //image for this button
  44. CIntObject * overlay;//object-overlay, can be null
  45. bool animateLonelyFrame = false;
  46. protected:
  47. void onButtonClicked(); // calls callback
  48. void update();//to refresh button after image or text change
  49. // internal method to change state. Public change can be done only via block()
  50. void setState(ButtonState newState);
  51. ButtonState getState();
  52. public:
  53. bool actOnDown,//runs when mouse is pressed down over it, not when up
  54. hoverable,//if true, button will be highlighted when hovered (e.g. main menu)
  55. soundDisabled;
  56. // if set, button will have 1-px border around it with this color
  57. boost::optional<SDL_Color> borderColor;
  58. /// adds one more callback to on-click actions
  59. void addCallback(std::function<void()> callback);
  60. /// adds overlay on top of button image. Only one overlay can be active at once
  61. void addOverlay(CIntObject * newOverlay);
  62. void addTextOverlay(const std::string &Text, EFonts font, SDL_Color color = Colors::WHITE);
  63. void addImage(std::string filename);
  64. void addHoverText(ButtonState state, std::string text);
  65. void setImageOrder(int state1, int state2, int state3, int state4);
  66. void setAnimateLonelyFrame(bool agreement);
  67. void block(bool on);
  68. /// State modifiers
  69. bool isBlocked();
  70. bool isHighlighted();
  71. /// Constructor
  72. CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  73. CFunctionList<void()> Callback = 0, int key=0, bool playerColoredButton = false );
  74. /// Appearance modifiers
  75. void setIndex(size_t index, bool playerColoredButton=false);
  76. void setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton=false, int animFlags=0);
  77. void setPlayerColor(PlayerColor player);
  78. /// CIntObject overrides
  79. void clickRight(tribool down, bool previousState) override;
  80. void clickLeft(tribool down, bool previousState) override;
  81. void hover (bool on) override;
  82. void showAll(SDL_Surface * to) override;
  83. /// generates tooltip that can be passed into constructor
  84. static std::pair<std::string, std::string> tooltip();
  85. static std::pair<std::string, std::string> tooltip(const JsonNode & localizedTexts);
  86. static std::pair<std::string, std::string> tooltip(const std::string & hover, const std::string & help = "");
  87. };
  88. class CToggleBase
  89. {
  90. CFunctionList<void(bool)> callback;
  91. protected:
  92. bool selected;
  93. // internal method for overrides
  94. virtual void doSelect(bool on);
  95. // returns true if toggle can change its state
  96. bool canActivate();
  97. public:
  98. /// if set to false - button can not be deselected normally
  99. bool allowDeselection;
  100. CToggleBase(CFunctionList<void(bool)> callback);
  101. virtual ~CToggleBase();
  102. /// Changes selection to "on", and calls callback
  103. void setSelected(bool on);
  104. void addCallback(std::function<void(bool)> callback);
  105. };
  106. /// A button which can be selected/deselected, checkbox
  107. class CToggleButton : public CButton, public CToggleBase
  108. {
  109. void doSelect(bool on) override;
  110. public:
  111. CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  112. CFunctionList<void(bool)> Callback = 0, int key=0, bool playerColoredButton = false );
  113. void clickLeft(tribool down, bool previousState) override;
  114. // bring overrides into scope
  115. //using CButton::addCallback;
  116. using CToggleBase::addCallback;
  117. };
  118. class CToggleGroup : public CIntObject
  119. {
  120. CFunctionList<void(int)> onChange; //called when changing selected button with new button's id
  121. int selectedID;
  122. void selectionChanged(int to);
  123. public:
  124. std::map<int, CToggleBase*> buttons;
  125. CToggleGroup(const CFunctionList<void(int)> & OnChange);
  126. void addCallback(std::function<void(int)> callback);
  127. /// add one toggle/button into group
  128. void addToggle(int index, CToggleBase * button);
  129. /// Changes selection to specific value. Will select toggle with this ID, if present
  130. void setSelected(int id);
  131. };
  132. /// A typical slider for volume with an animated indicator
  133. class CVolumeSlider : public CIntObject
  134. {
  135. int value;
  136. CFunctionList<void(int)> onChange;
  137. CAnimImage * animImage;
  138. const std::pair<std::string, std::string> * const helpHandlers;
  139. void setVolume(const int v);
  140. public:
  141. /// @param position coordinates of slider
  142. /// @param defName name of def animation for slider
  143. /// @param value initial value for volume
  144. /// @param help pointer to first helptext of slider
  145. CVolumeSlider(const Point &position, const std::string &defName, const int value,
  146. const std::pair<std::string, std::string> * const help);
  147. void moveTo(int id);
  148. void addCallback(std::function<void(int)> callback);
  149. void clickLeft(tribool down, bool previousState) override;
  150. void clickRight(tribool down, bool previousState) override;
  151. void wheelScrolled(bool down, bool in) override;
  152. };
  153. /// A typical slider which can be orientated horizontally/vertically.
  154. class CSlider : public CIntObject
  155. {
  156. CButton *left, *right, *slider; //if vertical then left=up
  157. int capacity;//how many elements can be active at same time (e.g. hero list = 5)
  158. int positions; //number of highest position (0 if there is only one)
  159. bool horizontal;
  160. int amount; //total amount of elements (e.g. hero list = 0-8)
  161. int value; //first active element
  162. int scrollStep; // how many elements will be scrolled via one click, default = 1
  163. CFunctionList<void(int)> moved;
  164. void updateSliderPos();
  165. void sliderClicked();
  166. public:
  167. enum EStyle {
  168. BROWN,
  169. BLUE
  170. };
  171. void block(bool on);
  172. /// Controls how many items wil be scrolled via one click
  173. void setScrollStep(int to);
  174. /// Value modifiers
  175. void moveLeft();
  176. void moveRight();
  177. void moveTo(int value);
  178. void moveBy(int amount);
  179. void moveToMin();
  180. void moveToMax();
  181. /// Amount modifier
  182. void setAmount(int to);
  183. /// Accessors
  184. int getAmount();
  185. int getValue();
  186. void addCallback(std::function<void(int)> callback);
  187. void keyPressed(const SDL_KeyboardEvent & key) override;
  188. void wheelScrolled(bool down, bool in) override;
  189. void clickLeft(tribool down, bool previousState) override;
  190. void mouseMoved (const SDL_MouseMotionEvent & sEvent) override;
  191. void showAll(SDL_Surface * to) override;
  192. /// @param position coordinates of slider
  193. /// @param length length of slider ribbon, including left/right buttons
  194. /// @param Moved function that will be called whenever slider moves
  195. /// @param Capacity maximal number of visible at once elements
  196. /// @param Amount total amount of elements, including not visible
  197. /// @param Value starting position
  198. CSlider(Point position, int length, std::function<void(int)> Moved, int Capacity, int Amount,
  199. int Value=0, bool Horizontal=true, EStyle style = BROWN);
  200. ~CSlider();
  201. };