Buttons.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. bool musicLike; //determines the behaviour of this group
  140. void selectionChanged(int to);
  141. public:
  142. std::map<int, CToggleBase*> buttons;
  143. CToggleGroup(const CFunctionList<void(int)> & OnChange, bool musicLikeButtons = false);
  144. void addCallback(std::function<void(int)> callback);
  145. /// add one toggle/button into group
  146. void addToggle(int index, CToggleBase * button);
  147. /// Changes selection to specific value. Will select toggle with this ID, if present
  148. void setSelected(int id);
  149. void show(SDL_Surface * to);
  150. void showAll(SDL_Surface * to);
  151. };
  152. /// A typical slider which can be orientated horizontally/vertically.
  153. class CSlider : public CIntObject
  154. {
  155. CButton *left, *right, *slider; //if vertical then left=up
  156. int capacity;//how many elements can be active at same time (e.g. hero list = 5)
  157. int positions; //number of highest position (0 if there is only one)
  158. bool horizontal;
  159. bool wheelScrolling;
  160. bool keyScrolling;
  161. int amount; //total amount of elements (e.g. hero list = 0-8)
  162. int value; //first active element
  163. int scrollStep; // how many elements will be scrolled via one click, default = 1
  164. CFunctionList<void(int)> moved;
  165. void updateSliderPos();
  166. void sliderClicked();
  167. public:
  168. enum EStyle {
  169. BROWN,
  170. BLUE
  171. };
  172. void block(bool on);
  173. /// Controls how many items wil be scrolled via one click
  174. void setScrollStep(int to);
  175. /// Value modifiers
  176. void moveLeft();
  177. void moveRight();
  178. void moveTo(int value);
  179. void moveBy(int amount);
  180. void moveToMin();
  181. void moveToMax();
  182. /// Amount modifier
  183. void setAmount(int to);
  184. /// Accessors
  185. int getAmount();
  186. int getValue();
  187. void addCallback(std::function<void(int)> callback);
  188. void keyPressed(const SDL_KeyboardEvent & key);
  189. void wheelScrolled(bool down, bool in);
  190. void clickLeft(tribool down, bool previousState);
  191. void mouseMoved (const SDL_MouseMotionEvent & sEvent);
  192. void showAll(SDL_Surface * to);
  193. /**
  194. * @param position, coordinates of slider
  195. * @param length, length of slider ribbon, including left/right buttons
  196. * @param Moved, function that will be called whenever slider moves
  197. * @param Capacity, maximal number of visible at once elements
  198. * @param Amount, total amount of elements, including not visible
  199. * @param Value, starting position
  200. */
  201. CSlider(Point position, int length, std::function<void(int)> Moved, int Capacity, int Amount,
  202. int Value=0, bool Horizontal=true, EStyle style = BROWN);
  203. ~CSlider();
  204. };