Buttons.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #pragma once
  2. #include "../gui/CIntObject.h"
  3. #include "../gui/SDL_Extensions.h"
  4. #include "../../lib/FunctionList.h"
  5. struct SDL_Surface;
  6. struct Rect;
  7. class CAnimImage;
  8. class CLabel;
  9. class CAnimation;
  10. namespace config
  11. {
  12. struct ButtonInfo;
  13. }
  14. /*
  15. * Buttons.h, part of VCMI engine
  16. *
  17. * Authors: listed in file AUTHORS in main folder
  18. *
  19. * License: GNU General Public License v2.0 or later
  20. * Full text of license available in license.txt file, in main folder
  21. *
  22. */
  23. class ClickableArea : public CIntObject //TODO: derive from LRCLickableArea? Or somehow use its right-click/hover data?
  24. {
  25. CFunctionList<void()> callback;
  26. CIntObject * area;
  27. protected:
  28. void onClick();
  29. public:
  30. ClickableArea(CIntObject * object, CFunctionList<void()> callback);
  31. void addCallback(std::function<void()> callback);
  32. void setArea(CIntObject * object);
  33. void clickLeft(tribool down, bool previousState) override;
  34. };
  35. /// Typical Heroes 3 button which can be inactive or active and can
  36. /// hold further information if you right-click it
  37. class CButton : public CKeyShortcut
  38. {
  39. CFunctionList<void()> callback;
  40. public:
  41. enum ButtonState
  42. {
  43. NORMAL=0,
  44. PRESSED=1,
  45. BLOCKED=2,
  46. HIGHLIGHTED=3
  47. };
  48. private:
  49. std::vector<std::string> imageNames;//store list of images that can be used by this button
  50. size_t currentImage;
  51. ButtonState state;//current state of button from enum
  52. std::array<int, 4> stateToIndex; // mapping of button state to index of frame in animation
  53. std::array<std::string, 4> hoverTexts; //texts for statusbar, if empty - first entry will be used
  54. std::string helpBox; //for right-click help
  55. CAnimImage * image; //image for this button
  56. CIntObject * overlay;//object-overlay, can be null
  57. protected:
  58. void onButtonClicked(); // calls callback
  59. void update();//to refresh button after image or text change
  60. // internal method to change state. Public change can be done only via block()
  61. void setState(ButtonState newState);
  62. ButtonState getState();
  63. public:
  64. bool actOnDown,//runs when mouse is pressed down over it, not when up
  65. hoverable,//if true, button will be highlighted when hovered (e.g. main menu)
  66. soundDisabled;
  67. // if set, button will have 1-px border around it with this color
  68. boost::optional<SDL_Color> borderColor;
  69. /// adds one more callback to on-click actions
  70. void addCallback(std::function<void()> callback);
  71. /// adds overlay on top of button image. Only one overlay can be active at once
  72. void addOverlay(CIntObject * newOverlay);
  73. void addTextOverlay(const std::string &Text, EFonts font, SDL_Color color = Colors::WHITE);
  74. void addImage(std::string filename);
  75. void addHoverText(ButtonState state, std::string text);
  76. void setImageOrder(int state1, int state2, int state3, int state4);
  77. void block(bool on);
  78. /// State modifiers
  79. bool isBlocked();
  80. bool isHighlighted();
  81. /// Constructor
  82. CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  83. CFunctionList<void()> Callback = 0, int key=0, bool playerColoredButton = false );
  84. /// Appearance modifiers
  85. void setIndex(size_t index, bool playerColoredButton=false);
  86. void setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton=false, int animFlags=0);
  87. void setPlayerColor(PlayerColor player);
  88. /// CIntObject overrides
  89. void clickRight(tribool down, bool previousState) override;
  90. void clickLeft(tribool down, bool previousState) override;
  91. void hover (bool on) override;
  92. void showAll(SDL_Surface * to) override;
  93. /// generates tooltip that can be passed into constructor
  94. static std::pair<std::string, std::string> tooltip();
  95. static std::pair<std::string, std::string> tooltip(const JsonNode & localizedTexts);
  96. static std::pair<std::string, std::string> tooltip(const std::string & hover, const std::string & help = "");
  97. };
  98. class CToggleBase
  99. {
  100. CFunctionList<void(bool)> callback;
  101. protected:
  102. bool selected;
  103. // internal method for overrides
  104. virtual void doSelect(bool on);
  105. // returns true if toggle can change its state
  106. bool canActivate();
  107. public:
  108. /// if set to false - button can not be deselected normally
  109. bool allowDeselection;
  110. CToggleBase(CFunctionList<void(bool)> callback);
  111. virtual ~CToggleBase();
  112. /// Changes selection to "on", and calls callback
  113. void setSelected(bool on);
  114. void addCallback(std::function<void(bool)> callback);
  115. };
  116. class ClickableToggle : public ClickableArea, public CToggleBase
  117. {
  118. public:
  119. ClickableToggle(CIntObject * object, CFunctionList<void()> selectFun, CFunctionList<void()> deselectFun);
  120. void clickLeft(tribool down, bool previousState) override;
  121. };
  122. /// A button which can be selected/deselected, checkbox
  123. class CToggleButton : public CButton, public CToggleBase
  124. {
  125. void doSelect(bool on) override;
  126. public:
  127. CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  128. CFunctionList<void(bool)> Callback = 0, int key=0, bool playerColoredButton = false );
  129. void clickLeft(tribool down, bool previousState) override;
  130. // bring overrides into scope
  131. //using CButton::addCallback;
  132. using CToggleBase::addCallback;
  133. };
  134. class CToggleGroup : public CIntObject
  135. {
  136. CFunctionList<void(int)> onChange; //called when changing selected button with new button's id
  137. int selectedID;
  138. void selectionChanged(int to);
  139. public:
  140. std::map<int, CToggleBase*> buttons;
  141. CToggleGroup(const CFunctionList<void(int)> & OnChange);
  142. void addCallback(std::function<void(int)> callback);
  143. /// add one toggle/button into group
  144. void addToggle(int index, CToggleBase * button);
  145. /// Changes selection to specific value. Will select toggle with this ID, if present
  146. void setSelected(int id);
  147. };
  148. /// A typical slider for volume with an animated indicator
  149. class CVolumeSlider : public CIntObject
  150. {
  151. int value;
  152. CFunctionList<void(int)> onChange;
  153. CAnimImage * animImage;
  154. const std::pair<std::string, std::string> * const helpHandlers;
  155. void setVolume(const int v);
  156. public:
  157. /// @param position coordinates of slider
  158. /// @param defName name of def animation for slider
  159. /// @param value initial value for volume
  160. /// @param help pointer to first helptext of slider
  161. CVolumeSlider(const Point &position, const std::string &defName, const int value,
  162. const std::pair<std::string, std::string> * const help);
  163. void moveTo(int id);
  164. void addCallback(std::function<void(int)> callback);
  165. void clickLeft(tribool down, bool previousState) override;
  166. void clickRight(tribool down, bool previousState) override;
  167. void wheelScrolled(bool down, bool in) override;
  168. };
  169. /// A typical slider which can be orientated horizontally/vertically.
  170. class CSlider : public CIntObject
  171. {
  172. CButton *left, *right, *slider; //if vertical then left=up
  173. int capacity;//how many elements can be active at same time (e.g. hero list = 5)
  174. int positions; //number of highest position (0 if there is only one)
  175. bool horizontal;
  176. bool wheelScrolling;
  177. bool keyScrolling;
  178. int amount; //total amount of elements (e.g. hero list = 0-8)
  179. int value; //first active element
  180. int scrollStep; // how many elements will be scrolled via one click, default = 1
  181. CFunctionList<void(int)> moved;
  182. void updateSliderPos();
  183. void sliderClicked();
  184. public:
  185. enum EStyle {
  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. };