Buttons.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /// State modifiers
  79. bool isBlocked();
  80. bool isHighlighted();
  81. /// Constructor
  82. CButton(Point position, const AnimationPath & defName, const std::pair<std::string, std::string> & help,
  83. CFunctionList<void()> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
  84. /// CIntObject overrides
  85. void showPopupWindow(const Point & cursorPosition) override;
  86. void clickPressed(const Point & cursorPosition) override;
  87. void clickReleased(const Point & cursorPosition) override;
  88. void clickCancel(const Point & cursorPosition) override;
  89. void hover (bool on) override;
  90. void showAll(Canvas & to) override;
  91. /// generates tooltip that can be passed into constructor
  92. static std::pair<std::string, std::string> tooltip();
  93. static std::pair<std::string, std::string> tooltipLocalized(const std::string & key);
  94. static std::pair<std::string, std::string> tooltip(const std::string & hover, const std::string & help = "");
  95. };
  96. class CToggleBase
  97. {
  98. CFunctionList<void(bool)> callback;
  99. bool selected;
  100. /// if set to false - button can not be deselected normally
  101. bool allowDeselection;
  102. protected:
  103. // internal method for overrides
  104. virtual void doSelect(bool on);
  105. // returns true if toggle can change its state
  106. bool canActivate() const;
  107. public:
  108. CToggleBase(CFunctionList<void(bool)> callback);
  109. virtual ~CToggleBase();
  110. /// Changes selection to "on", and calls callback
  111. void setSelected(bool on);
  112. /// Changes selection to "on" without calling callback
  113. void setSelectedSilent(bool on);
  114. bool isSelected() const;
  115. void setAllowDeselection(bool on);
  116. void addCallback(const std::function<void(bool)> & callback);
  117. /// Set whether the toggle is currently enabled for user to use, this is only inplemented in ToggleButton, not for other toggles yet.
  118. virtual void setEnabled(bool enabled);
  119. };
  120. /// A button which can be selected/deselected, checkbox
  121. class CToggleButton : public CButton, public CToggleBase
  122. {
  123. void doSelect(bool on) override;
  124. void setEnabled(bool enabled) override;
  125. public:
  126. CToggleButton(Point position, const AnimationPath &defName, const std::pair<std::string, std::string> &help,
  127. CFunctionList<void(bool)> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
  128. void clickPressed(const Point & cursorPosition) override;
  129. void clickReleased(const Point & cursorPosition) override;
  130. void clickCancel(const Point & cursorPosition) 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. void selectionChanged(int to);
  140. public:
  141. std::map<int, std::shared_ptr<CToggleBase>> buttons;
  142. CToggleGroup(const CFunctionList<void(int)> & OnChange);
  143. void addCallback(const std::function<void(int)> & callback);
  144. void resetCallback();
  145. /// add one toggle/button into group
  146. void addToggle(int index, const std::shared_ptr<CToggleBase> & button);
  147. /// Changes selection to specific value. Will select toggle with this ID, if present
  148. void setSelected(int id);
  149. /// in some cases, e.g. LoadGame difficulty selection, after refreshing the UI, the ToggleGroup should
  150. /// reset all of it's child buttons to BLOCK state, then make selection again
  151. void setSelectedOnly(int id);
  152. int getSelected() const;
  153. };