Buttons.h 7.5 KB

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