Buttons.h 6.6 KB

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