Buttons.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "../render/EFont.h"
  13. #include "../../lib/FunctionList.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class Rect;
  16. VCMI_LIB_NAMESPACE_END
  17. class CAnimImage;
  18. class CLabel;
  19. class CAnimation;
  20. /// Typical Heroes 3 button which can be inactive or active and can
  21. /// hold further information if you right-click it
  22. class CButton : public CKeyShortcut
  23. {
  24. CFunctionList<void()> callback;
  25. public:
  26. enum ButtonState
  27. {
  28. NORMAL=0,
  29. PRESSED=1,
  30. BLOCKED=2,
  31. HIGHLIGHTED=3
  32. };
  33. private:
  34. std::vector<std::string> imageNames;//store list of images that can be used by this button
  35. size_t currentImage;
  36. ButtonState state;//current state of button from enum
  37. std::array<int, 4> stateToIndex; // mapping of button state to index of frame in animation
  38. std::array<std::string, 4> hoverTexts; //texts for statusbar, if empty - first entry will be used
  39. std::optional<ColorRGBA> borderColor; // mapping of button state to border color
  40. std::string helpBox; //for right-click help
  41. std::shared_ptr<CAnimImage> image; //image for this button
  42. std::shared_ptr<CIntObject> overlay;//object-overlay, can be null
  43. bool animateLonelyFrame = false;
  44. protected:
  45. void onButtonClicked(); // calls callback
  46. void update();//to refresh button after image or text change
  47. // internal method to change state. Public change can be done only via block()
  48. void setState(ButtonState newState);
  49. ButtonState getState();
  50. public:
  51. bool actOnDown,//runs when mouse is pressed down over it, not when up
  52. hoverable,//if true, button will be highlighted when hovered (e.g. main menu)
  53. soundDisabled;
  54. // sets the same border color for all button states.
  55. void setBorderColor(std::optional<ColorRGBA> borderColor);
  56. /// adds one more callback to on-click actions
  57. void addCallback(std::function<void()> callback);
  58. /// adds overlay on top of button image. Only one overlay can be active at once
  59. void addOverlay(std::shared_ptr<CIntObject> newOverlay);
  60. void addTextOverlay(const std::string & Text, EFonts font, ColorRGBA color);
  61. void addImage(std::string filename);
  62. void addHoverText(ButtonState state, std::string text);
  63. void setImageOrder(int state1, int state2, int state3, int state4);
  64. void setAnimateLonelyFrame(bool agreement);
  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, EShortcut key = {}, bool playerColoredButton = false );
  72. /// Appearance modifiers
  73. void setIndex(size_t index);
  74. void setImage(std::shared_ptr<CAnimation> anim, int animFlags=0);
  75. void setPlayerColor(PlayerColor player);
  76. /// CIntObject overrides
  77. void showPopupWindow(const Point & cursorPosition) override;
  78. void clickPressed(const Point & cursorPosition) override;
  79. void clickReleased(const Point & cursorPosition) override;
  80. void clickCancel(const Point & cursorPosition) override;
  81. void hover (bool on) override;
  82. void showAll(Canvas & 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> tooltipLocalized(const std::string & key);
  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. /// Changes selection to "on" without calling callback
  105. void setSelectedSilent(bool on);
  106. void addCallback(std::function<void(bool)> callback);
  107. /// Set whether the toggle is currently enabled for user to use, this is only inplemented in ToggleButton, not for other toggles yet.
  108. virtual void setEnabled(bool enabled);
  109. };
  110. /// A button which can be selected/deselected, checkbox
  111. class CToggleButton : public CButton, public CToggleBase
  112. {
  113. void doSelect(bool on) override;
  114. void setEnabled(bool enabled) override;
  115. public:
  116. CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
  117. CFunctionList<void(bool)> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
  118. void clickPressed(const Point & cursorPosition) override;
  119. void clickReleased(const Point & cursorPosition) override;
  120. void clickCancel(const Point & cursorPosition) override;
  121. // bring overrides into scope
  122. //using CButton::addCallback;
  123. using CToggleBase::addCallback;
  124. };
  125. class CToggleGroup : public CIntObject
  126. {
  127. CFunctionList<void(int)> onChange; //called when changing selected button with new button's id
  128. int selectedID;
  129. void selectionChanged(int to);
  130. public:
  131. std::map<int, std::shared_ptr<CToggleBase>> buttons;
  132. CToggleGroup(const CFunctionList<void(int)> & OnChange);
  133. void addCallback(std::function<void(int)> callback);
  134. void resetCallback();
  135. /// add one toggle/button into group
  136. void addToggle(int index, std::shared_ptr<CToggleBase> button);
  137. /// Changes selection to specific value. Will select toggle with this ID, if present
  138. void setSelected(int id);
  139. /// in some cases, e.g. LoadGame difficulty selection, after refreshing the UI, the ToggleGroup should
  140. /// reset all of it's child buttons to BLOCK state, then make selection again
  141. void setSelectedOnly(int id);
  142. int getSelected() const;
  143. };