Buttons.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. std::array<std::string, 4> hoverTexts; //texts for statusbar, if empty - first entry will be used
  59. std::optional<ColorRGBA> borderColor; // mapping of button state to border color
  60. std::string helpBox; //for right-click help
  61. bool actOnDown; //runs when mouse is pressed down over it, not when up
  62. bool hoverable; //if true, button will be highlighted when hovered (e.g. main menu)
  63. bool soundDisabled;
  64. protected:
  65. void onButtonClicked(); // calls callback
  66. // internal method to change state. Public change can be done only via block()
  67. void setState(EButtonState newState);
  68. public:
  69. // sets the same border color for all button states.
  70. void setBorderColor(std::optional<ColorRGBA> borderColor);
  71. /// adds one more callback to on-click actions
  72. void addCallback(const std::function<void()> & callback);
  73. void addHoverText(EButtonState state, const std::string & text);
  74. void block(bool on);
  75. void setHoverable(bool on);
  76. void setSoundDisabled(bool on);
  77. void setActOnDown(bool on);
  78. void setHelp(const std::pair<std::string, std::string> & help);
  79. /// State modifiers
  80. bool isBlocked();
  81. bool isHighlighted();
  82. /// Constructor
  83. CButton(Point position, const AnimationPath & defName, const std::pair<std::string, std::string> & help,
  84. CFunctionList<void()> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
  85. /// CIntObject overrides
  86. void showPopupWindow(const Point & cursorPosition) override;
  87. void clickPressed(const Point & cursorPosition) override;
  88. void clickReleased(const Point & cursorPosition) override;
  89. void clickCancel(const Point & cursorPosition) override;
  90. void hover (bool on) override;
  91. void showAll(Canvas & to) override;
  92. /// generates tooltip that can be passed into constructor
  93. static std::pair<std::string, std::string> tooltip();
  94. static std::pair<std::string, std::string> tooltipLocalized(const std::string & key);
  95. static std::pair<std::string, std::string> tooltip(const std::string & hover, const std::string & help = "");
  96. };
  97. class CToggleBase
  98. {
  99. CFunctionList<void(bool)> callback;
  100. bool selected;
  101. /// if set to false - button can not be deselected normally
  102. bool allowDeselection;
  103. protected:
  104. // internal method for overrides
  105. virtual void doSelect(bool on);
  106. // returns true if toggle can change its state
  107. bool canActivate() const;
  108. public:
  109. CToggleBase(CFunctionList<void(bool)> callback);
  110. virtual ~CToggleBase();
  111. /// Changes selection to "on", and calls callback
  112. void setSelected(bool on);
  113. /// Changes selection to "on" without calling callback
  114. void setSelectedSilent(bool on);
  115. bool isSelected() const;
  116. void setAllowDeselection(bool on);
  117. void addCallback(const std::function<void(bool)> & callback);
  118. /// Set whether the toggle is currently enabled for user to use, this is only inplemented in ToggleButton, not for other toggles yet.
  119. virtual void setEnabled(bool enabled);
  120. };
  121. /// A button which can be selected/deselected, checkbox
  122. class CToggleButton : public CButton, public CToggleBase
  123. {
  124. void doSelect(bool on) override;
  125. void setEnabled(bool enabled) override;
  126. public:
  127. CToggleButton(Point position, const AnimationPath &defName, const std::pair<std::string, std::string> &help,
  128. CFunctionList<void(bool)> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
  129. void clickPressed(const Point & cursorPosition) override;
  130. void clickReleased(const Point & cursorPosition) override;
  131. void clickCancel(const Point & cursorPosition) override;
  132. // bring overrides into scope
  133. //using CButton::addCallback;
  134. using CToggleBase::addCallback;
  135. };
  136. class CToggleGroup : public CIntObject
  137. {
  138. CFunctionList<void(int)> onChange; //called when changing selected button with new button's id
  139. int selectedID;
  140. void selectionChanged(int to);
  141. public:
  142. std::map<int, std::shared_ptr<CToggleBase>> buttons;
  143. CToggleGroup(const CFunctionList<void(int)> & OnChange);
  144. void addCallback(const std::function<void(int)> & callback);
  145. void resetCallback();
  146. /// add one toggle/button into group
  147. void addToggle(int index, const std::shared_ptr<CToggleBase> & button);
  148. /// Changes selection to specific value. Will select toggle with this ID, if present
  149. void setSelected(int id);
  150. /// in some cases, e.g. LoadGame difficulty selection, after refreshing the UI, the ToggleGroup should
  151. /// reset all of it's child buttons to BLOCK state, then make selection again
  152. void setSelectedOnly(int id);
  153. int getSelected() const;
  154. };