Buttons.h 5.9 KB

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