Buttons.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. class CDefHandler;
  11. namespace config
  12. {
  13. struct ButtonInfo;
  14. }
  15. /*
  16. * Buttons.h, part of VCMI engine
  17. *
  18. * Authors: listed in file AUTHORS in main folder
  19. *
  20. * License: GNU General Public License v2.0 or later
  21. * Full text of license available in license.txt file, in main folder
  22. *
  23. */
  24. class ClickableArea : public CIntObject //TODO: derive from LRCLickableArea? Or somehow use its right-click/hover data?
  25. {
  26. CFunctionList<void()> callback;
  27. CIntObject * area;
  28. protected:
  29. void onClick();
  30. public:
  31. ClickableArea(CIntObject * object, CFunctionList<void()> callback);
  32. void addCallback(std::function<void()> callback);
  33. void setArea(CIntObject * object);
  34. void clickLeft(tribool down, bool previousState) override;
  35. };
  36. /// Typical Heroes 3 button which can be inactive or active and can
  37. /// hold further information if you right-click it
  38. class CButton : public CKeyShortcut
  39. {
  40. CFunctionList<void()> callback;
  41. public:
  42. enum ButtonState
  43. {
  44. NORMAL=0,
  45. PRESSED=1,
  46. BLOCKED=2,
  47. HIGHLIGHTED=3
  48. };
  49. private:
  50. std::vector<std::string> imageNames;//store list of images that can be used by this button
  51. size_t currentImage;
  52. ButtonState state;//current state of button from enum
  53. std::array<int, 4> stateToIndex; // mapping of button state to index of frame in animation
  54. std::array<std::string, 4> hoverTexts; //texts for statusbar, if empty - first entry will be used
  55. std::string helpBox; //for right-click help
  56. CAnimImage * image; //image for this button
  57. CIntObject * overlay;//object-overlay, can be null
  58. protected:
  59. void onButtonClicked(); // calls callback
  60. void update();//to refresh button after image or text change
  61. // internal method to change state. Public change can be done only via block()
  62. void setState(ButtonState newState);
  63. ButtonState getState();
  64. public:
  65. bool actOnDown,//runs when mouse is pressed down over it, not when up
  66. hoverable,//if true, button will be highlighted when hovered (e.g. main menu)
  67. soundDisabled;
  68. // if set, button will have 1-px border around it with this color
  69. boost::optional<SDL_Color> borderColor;
  70. /// adds one more callback to on-click actions
  71. void addCallback(std::function<void()> callback);
  72. /// adds overlay on top of button image. Only one overlay can be active at once
  73. void addOverlay(CIntObject * newOverlay);
  74. void addTextOverlay(const std::string &Text, EFonts font, SDL_Color color = Colors::WHITE);
  75. void addImage(std::string filename);
  76. void addHoverText(ButtonState state, std::string text);
  77. void setImageOrder(int state1, int state2, int state3, int state4);
  78. void block(bool on);
  79. /// State modifiers
  80. bool isBlocked();
  81. bool isHighlighted();
  82. /// Constructor
  83. CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  84. CFunctionList<void()> Callback = 0, int key=0, bool playerColoredButton = false );
  85. /// Appearance modifiers
  86. void setIndex(size_t index, bool playerColoredButton=false);
  87. void setImage(CAnimation* anim, bool playerColoredButton=false, int animFlags=0);
  88. void setPlayerColor(PlayerColor player);
  89. /// CIntObject overrides
  90. void clickRight(tribool down, bool previousState) override;
  91. void clickLeft(tribool down, bool previousState) override;
  92. void hover (bool on) override;
  93. void showAll(SDL_Surface * to) override;
  94. /// generates tooltip that can be passed into constructor
  95. static std::pair<std::string, std::string> tooltip();
  96. static std::pair<std::string, std::string> tooltip(const JsonNode & localizedTexts);
  97. static std::pair<std::string, std::string> tooltip(const std::string & hover, const std::string & help = "");
  98. };
  99. class CToggleBase
  100. {
  101. CFunctionList<void(bool)> callback;
  102. protected:
  103. bool selected;
  104. // internal method for overrides
  105. virtual void doSelect(bool on);
  106. // returns true if toggle can change its state
  107. bool canActivate();
  108. public:
  109. /// if set to false - button can not be deselected normally
  110. bool allowDeselection;
  111. CToggleBase(CFunctionList<void(bool)> callback);
  112. virtual ~CToggleBase();
  113. /// Changes selection to "on", and calls callback
  114. void setSelected(bool on);
  115. void addCallback(std::function<void(bool)> callback);
  116. };
  117. class ClickableToggle : public ClickableArea, public CToggleBase
  118. {
  119. public:
  120. ClickableToggle(CIntObject * object, CFunctionList<void()> selectFun, CFunctionList<void()> deselectFun);
  121. void clickLeft(tribool down, bool previousState) override;
  122. };
  123. /// A button which can be selected/deselected, checkbox
  124. class CToggleButton : public CButton, public CToggleBase
  125. {
  126. void doSelect(bool on) override;
  127. public:
  128. CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  129. CFunctionList<void(bool)> Callback = 0, int key=0, bool playerColoredButton = false );
  130. void clickLeft(tribool down, bool previousState) override;
  131. // bring overrides into scope
  132. //using CButton::addCallback;
  133. using CToggleBase::addCallback;
  134. };
  135. class CToggleGroup : public CIntObject
  136. {
  137. CFunctionList<void(int)> onChange; //called when changing selected button with new button's id
  138. int selectedID;
  139. void selectionChanged(int to);
  140. public:
  141. std::map<int, CToggleBase*> buttons;
  142. CToggleGroup(const CFunctionList<void(int)> & OnChange);
  143. void addCallback(std::function<void(int)> callback);
  144. /// add one toggle/button into group
  145. void addToggle(int index, CToggleBase * button);
  146. /// Changes selection to specific value. Will select toggle with this ID, if present
  147. void setSelected(int id);
  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. 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);
  169. };
  170. /// A typical slider which can be orientated horizontally/vertically.
  171. class CSlider : public CIntObject
  172. {
  173. CButton *left, *right, *slider; //if vertical then left=up
  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. bool wheelScrolling;
  178. bool keyScrolling;
  179. int amount; //total amount of elements (e.g. hero list = 0-8)
  180. int value; //first active element
  181. int scrollStep; // how many elements will be scrolled via one click, default = 1
  182. CFunctionList<void(int)> moved;
  183. void updateSliderPos();
  184. void sliderClicked();
  185. public:
  186. enum EStyle {
  187. BROWN,
  188. BLUE
  189. };
  190. void block(bool on);
  191. /// Controls how many items wil be scrolled via one click
  192. void setScrollStep(int to);
  193. /// Value modifiers
  194. void moveLeft();
  195. void moveRight();
  196. void moveTo(int value);
  197. void moveBy(int amount);
  198. void moveToMin();
  199. void moveToMax();
  200. /// Amount modifier
  201. void setAmount(int to);
  202. /// Accessors
  203. int getAmount();
  204. int getValue();
  205. void addCallback(std::function<void(int)> callback);
  206. void keyPressed(const SDL_KeyboardEvent & key);
  207. void wheelScrolled(bool down, bool in);
  208. void clickLeft(tribool down, bool previousState);
  209. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  210. void showAll(SDL_Surface * to);
  211. /// @param position coordinates of slider
  212. /// @param length length of slider ribbon, including left/right buttons
  213. /// @param Moved function that will be called whenever slider moves
  214. /// @param Capacity maximal number of visible at once elements
  215. /// @param Amount total amount of elements, including not visible
  216. /// @param Value starting position
  217. CSlider(Point position, int length, std::function<void(int)> Moved, int Capacity, int Amount,
  218. int Value=0, bool Horizontal=true, EStyle style = BROWN);
  219. ~CSlider();
  220. };