Buttons.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "../../lib/filesystem/ResourcePath.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class Rect;
  17. VCMI_LIB_NAMESPACE_END
  18. class CAnimImage;
  19. class InterfaceObjectConfigurable;
  20. enum class EButtonState
  21. {
  22. NORMAL=0,
  23. PRESSED=1,
  24. BLOCKED=2,
  25. HIGHLIGHTED=3 // used for: highlighted state for selectable buttons, hovered state for hoverable buttons (e.g. main menu)
  26. };
  27. class ButtonBase : public CKeyShortcut
  28. {
  29. std::shared_ptr<CAnimImage> image; //image for this button
  30. std::shared_ptr<InterfaceObjectConfigurable> configurable; //image for this button
  31. std::shared_ptr<CIntObject> overlay;//object-overlay, can be null
  32. std::unique_ptr<JsonNode> config;
  33. std::array<int, 4> stateToIndex; // mapping of button state to index of frame in animation
  34. EButtonState state;//current state of button from enum
  35. void update();//to refresh button after image or text change
  36. const JsonNode & getCurrentConfig() const;
  37. protected:
  38. ButtonBase(Point position, const AnimationPath & defName, EShortcut key, bool playerColoredButton);
  39. ~ButtonBase();
  40. std::shared_ptr<CIntObject> getOverlay();
  41. void setStateImpl(EButtonState state);
  42. EButtonState getState() const;
  43. public:
  44. /// Appearance modifiers
  45. void setPlayerColor(PlayerColor player);
  46. void setImage(const AnimationPath & defName, bool playerColoredButton = false);
  47. void setConfigurable(const JsonPath & jsonName, bool playerColoredButton = false);
  48. void setImageOrder(int state1, int state2, int state3, int state4);
  49. /// adds overlay on top of button image. Only one overlay can be active at once
  50. void setOverlay(const std::shared_ptr<CIntObject>& newOverlay);
  51. void setTextOverlay(const std::string & Text, EFonts font, ColorRGBA color);
  52. };
  53. /// Typical Heroes 3 button which can be inactive or active and can
  54. /// hold further information if you right-click it
  55. class CButton : public ButtonBase
  56. {
  57. CFunctionList<void()> callback;
  58. CFunctionList<void()> callbackPopup;
  59. std::array<std::string, 4> hoverTexts; //texts for statusbar, if empty - first entry will be used
  60. std::optional<ColorRGBA> borderColor; // mapping of button state to border color
  61. std::optional<ColorRGBA> highlightedBorderColor; // mapping of button state to border color
  62. std::string helpBox; //for right-click help
  63. bool actOnDown; //runs when mouse is pressed down over it, not when up
  64. bool hoverable; //if true, button will be highlighted when hovered (e.g. main menu)
  65. bool soundDisabled;
  66. protected:
  67. void onButtonClicked(); // calls callback
  68. // internal method to change state. Public change can be done only via block()
  69. void setState(EButtonState newState);
  70. public:
  71. // sets the same border color for all button states.
  72. void setBorderColor(std::optional<ColorRGBA> borderColor);
  73. void setHighlightedBorderColor(std::optional<ColorRGBA> borderColor);
  74. /// adds one more callback to on-click actions
  75. void addCallback(const std::function<void()> & callback);
  76. void addPopupCallback(const std::function<void()> & callback);
  77. void addHoverText(EButtonState state, const std::string & text);
  78. void block(bool on);
  79. void setHoverable(bool on);
  80. void setSoundDisabled(bool on);
  81. void setActOnDown(bool on);
  82. void setHelp(const std::pair<std::string, std::string> & help);
  83. /// State modifiers
  84. bool isBlocked();
  85. bool isHighlighted();
  86. bool isPressed();
  87. /// Constructor
  88. CButton(Point position, const AnimationPath & defName, const std::pair<std::string, std::string> & help,
  89. CFunctionList<void()> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
  90. /// CIntObject overrides
  91. void showPopupWindow(const Point & cursorPosition) override;
  92. void clickPressed(const Point & cursorPosition) override;
  93. void clickReleased(const Point & cursorPosition) override;
  94. void clickCancel(const Point & cursorPosition) override;
  95. void hover (bool on) override;
  96. void showAll(Canvas & to) override;
  97. /// generates tooltip that can be passed into constructor
  98. static std::pair<std::string, std::string> tooltip();
  99. static std::pair<std::string, std::string> tooltipLocalized(const std::string & key);
  100. static std::pair<std::string, std::string> tooltip(const std::string & hover, const std::string & help = "");
  101. };
  102. class CToggleBase
  103. {
  104. CFunctionList<void(bool)> callback;
  105. bool selected;
  106. /// if set to false - button can not be deselected normally
  107. bool allowDeselection;
  108. protected:
  109. // internal method for overrides
  110. virtual void doSelect(bool on);
  111. // returns true if toggle can change its state
  112. bool canActivate() const;
  113. public:
  114. CToggleBase(CFunctionList<void(bool)> callback);
  115. virtual ~CToggleBase();
  116. /// Changes selection to "on", and calls callback
  117. void setSelected(bool on);
  118. /// Changes selection to "on" without calling callback
  119. void setSelectedSilent(bool on);
  120. bool isSelected() const;
  121. void setAllowDeselection(bool on);
  122. void addCallback(const std::function<void(bool)> & callback);
  123. /// Set whether the toggle is currently enabled for user to use, this is only implemented in ToggleButton, not for other toggles yet.
  124. virtual void setEnabled(bool enabled);
  125. };
  126. /// A button which can be selected/deselected, checkbox
  127. class CToggleButton : public CButton, public CToggleBase
  128. {
  129. void doSelect(bool on) override;
  130. void setEnabled(bool enabled) override;
  131. CFunctionList<void()> callbackSelected;
  132. public:
  133. CToggleButton(Point position, const AnimationPath &defName, const std::pair<std::string, std::string> &help,
  134. CFunctionList<void(bool)> Callback = nullptr, EShortcut key = {}, bool playerColoredButton = false,
  135. CFunctionList<void()> CallbackSelected = nullptr );
  136. void clickPressed(const Point & cursorPosition) override;
  137. void clickReleased(const Point & cursorPosition) override;
  138. void clickCancel(const Point & cursorPosition) override;
  139. void clickDouble(const Point & cursorPosition) override;
  140. // bring overrides into scope
  141. //using CButton::addCallback;
  142. using CToggleBase::addCallback;
  143. };
  144. class CToggleGroup : public CIntObject
  145. {
  146. CFunctionList<void(int)> onChange; //called when changing selected button with new button's id
  147. int selectedID;
  148. void selectionChanged(int to);
  149. public:
  150. std::map<int, std::shared_ptr<CToggleBase>> buttons;
  151. CToggleGroup(const CFunctionList<void(int)> & OnChange);
  152. void addCallback(const std::function<void(int)> & callback);
  153. void resetCallback();
  154. /// add one toggle/button into group
  155. void addToggle(int index, const std::shared_ptr<CToggleBase> & button);
  156. /// Changes selection to specific value. Will select toggle with this ID, if present
  157. void setSelected(int id);
  158. /// in some cases, e.g. LoadGame difficulty selection, after refreshing the UI, the ToggleGroup should
  159. /// reset all of it's child buttons to BLOCK state, then make selection again
  160. void setSelectedOnly(int id);
  161. int getSelected() const;
  162. };