2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* Buttons.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2014-07-15 10:14:49 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../gui/CIntObject.h"
|
2023-07-31 18:50:55 +02:00
|
|
|
#include "../render/EFont.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
#include "../../lib/FunctionList.h"
|
2023-08-23 14:07:50 +02:00
|
|
|
#include "../../lib/filesystem/ResourcePath.h"
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
2023-01-18 17:32:57 +02:00
|
|
|
class Rect;
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
class CAnimImage;
|
|
|
|
class CLabel;
|
|
|
|
class CAnimation;
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
/// Typical Heroes 3 button which can be inactive or active and can
|
|
|
|
/// hold further information if you right-click it
|
|
|
|
class CButton : public CKeyShortcut
|
|
|
|
{
|
|
|
|
CFunctionList<void()> callback;
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
public:
|
|
|
|
enum ButtonState
|
|
|
|
{
|
|
|
|
NORMAL=0,
|
|
|
|
PRESSED=1,
|
|
|
|
BLOCKED=2,
|
|
|
|
HIGHLIGHTED=3
|
|
|
|
};
|
2023-08-25 12:07:38 +02:00
|
|
|
protected:
|
2023-08-23 14:07:50 +02:00
|
|
|
std::vector<AnimationPath> imageNames;//store list of images that can be used by this button
|
2014-08-03 14:16:19 +03:00
|
|
|
size_t currentImage;
|
2014-08-03 17:31:56 +03:00
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
ButtonState state;//current state of button from enum
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
std::array<int, 4> stateToIndex; // mapping of button state to index of frame in animation
|
2014-08-03 17:31:56 +03:00
|
|
|
std::array<std::string, 4> hoverTexts; //texts for statusbar, if empty - first entry will be used
|
2023-07-31 18:50:55 +02:00
|
|
|
std::optional<ColorRGBA> borderColor; // mapping of button state to border color
|
2014-08-03 14:16:19 +03:00
|
|
|
std::string helpBox; //for right-click help
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2018-04-07 13:34:11 +02:00
|
|
|
std::shared_ptr<CAnimImage> image; //image for this button
|
|
|
|
std::shared_ptr<CIntObject> overlay;//object-overlay, can be null
|
2017-12-26 12:03:19 +02:00
|
|
|
bool animateLonelyFrame = false;
|
2014-08-03 14:16:19 +03:00
|
|
|
protected:
|
|
|
|
void onButtonClicked(); // calls callback
|
2014-07-15 10:14:49 +03:00
|
|
|
void update();//to refresh button after image or text change
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
// internal method to change state. Public change can be done only via block()
|
2014-07-15 10:14:49 +03:00
|
|
|
void setState(ButtonState newState);
|
|
|
|
ButtonState getState();
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
public:
|
|
|
|
bool actOnDown,//runs when mouse is pressed down over it, not when up
|
2014-08-03 17:31:56 +03:00
|
|
|
hoverable,//if true, button will be highlighted when hovered (e.g. main menu)
|
2014-08-03 14:16:19 +03:00
|
|
|
soundDisabled;
|
|
|
|
|
2018-03-17 06:23:22 +02:00
|
|
|
// sets the same border color for all button states.
|
2023-07-31 18:50:55 +02:00
|
|
|
void setBorderColor(std::optional<ColorRGBA> borderColor);
|
2014-08-03 14:16:19 +03:00
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
/// adds one more callback to on-click actions
|
2023-10-29 00:55:42 +02:00
|
|
|
void addCallback(const std::function<void()> & callback);
|
2014-08-03 17:31:56 +03:00
|
|
|
|
|
|
|
/// adds overlay on top of button image. Only one overlay can be active at once
|
2018-04-07 13:34:11 +02:00
|
|
|
void addOverlay(std::shared_ptr<CIntObject> newOverlay);
|
2023-07-31 18:50:55 +02:00
|
|
|
void addTextOverlay(const std::string & Text, EFonts font, ColorRGBA color);
|
2014-08-03 14:16:19 +03:00
|
|
|
|
2023-08-23 14:07:50 +02:00
|
|
|
void addImage(const AnimationPath & filename);
|
2014-08-03 14:16:19 +03:00
|
|
|
void addHoverText(ButtonState state, std::string text);
|
|
|
|
|
|
|
|
void setImageOrder(int state1, int state2, int state3, int state4);
|
2017-12-26 12:03:19 +02:00
|
|
|
void setAnimateLonelyFrame(bool agreement);
|
2014-07-15 10:14:49 +03:00
|
|
|
void block(bool on);
|
2014-08-03 14:16:19 +03:00
|
|
|
|
|
|
|
/// State modifiers
|
2014-07-15 10:14:49 +03:00
|
|
|
bool isBlocked();
|
|
|
|
bool isHighlighted();
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
/// Constructor
|
2023-08-23 14:07:50 +02:00
|
|
|
CButton(Point position, const AnimationPath & defName, const std::pair<std::string, std::string> & help,
|
2023-04-27 19:21:06 +02:00
|
|
|
CFunctionList<void()> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
/// Appearance modifiers
|
2023-05-03 18:05:07 +02:00
|
|
|
void setIndex(size_t index);
|
|
|
|
void setImage(std::shared_ptr<CAnimation> anim, int animFlags=0);
|
2014-08-03 14:16:19 +03:00
|
|
|
void setPlayerColor(PlayerColor player);
|
|
|
|
|
|
|
|
/// CIntObject overrides
|
2023-07-08 13:33:04 +02:00
|
|
|
void showPopupWindow(const Point & cursorPosition) override;
|
|
|
|
void clickPressed(const Point & cursorPosition) override;
|
|
|
|
void clickReleased(const Point & cursorPosition) override;
|
|
|
|
void clickCancel(const Point & cursorPosition) override;
|
2014-08-03 14:16:19 +03:00
|
|
|
void hover (bool on) override;
|
2023-06-02 15:42:18 +02:00
|
|
|
void showAll(Canvas & to) override;
|
2014-08-03 14:16:19 +03:00
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
/// generates tooltip that can be passed into constructor
|
2014-08-03 14:16:19 +03:00
|
|
|
static std::pair<std::string, std::string> tooltip();
|
2022-12-27 22:19:05 +02:00
|
|
|
static std::pair<std::string, std::string> tooltipLocalized(const std::string & key);
|
2014-08-03 14:16:19 +03:00
|
|
|
static std::pair<std::string, std::string> tooltip(const std::string & hover, const std::string & help = "");
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
class CToggleBase
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
CFunctionList<void(bool)> callback;
|
|
|
|
protected:
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
bool selected;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
// internal method for overrides
|
2014-08-03 14:16:19 +03:00
|
|
|
virtual void doSelect(bool on);
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
// returns true if toggle can change its state
|
|
|
|
bool canActivate();
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
public:
|
2014-08-03 17:31:56 +03:00
|
|
|
/// if set to false - button can not be deselected normally
|
2014-08-03 14:16:19 +03:00
|
|
|
bool allowDeselection;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
CToggleBase(CFunctionList<void(bool)> callback);
|
|
|
|
virtual ~CToggleBase();
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
/// Changes selection to "on", and calls callback
|
2014-08-03 14:16:19 +03:00
|
|
|
void setSelected(bool on);
|
|
|
|
|
2023-07-05 17:14:37 +02:00
|
|
|
/// Changes selection to "on" without calling callback
|
|
|
|
void setSelectedSilent(bool on);
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
void addCallback(std::function<void(bool)> callback);
|
2020-01-21 11:09:08 +02:00
|
|
|
|
|
|
|
/// Set whether the toggle is currently enabled for user to use, this is only inplemented in ToggleButton, not for other toggles yet.
|
|
|
|
virtual void setEnabled(bool enabled);
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
/// A button which can be selected/deselected, checkbox
|
|
|
|
class CToggleButton : public CButton, public CToggleBase
|
2014-07-15 10:14:49 +03:00
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
void doSelect(bool on) override;
|
2020-01-21 11:09:08 +02:00
|
|
|
void setEnabled(bool enabled) override;
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
public:
|
2023-08-23 14:07:50 +02:00
|
|
|
CToggleButton(Point position, const AnimationPath &defName, const std::pair<std::string, std::string> &help,
|
2023-04-27 19:21:06 +02:00
|
|
|
CFunctionList<void(bool)> Callback = 0, EShortcut key = {}, bool playerColoredButton = false );
|
2023-07-08 13:33:04 +02:00
|
|
|
|
|
|
|
void clickPressed(const Point & cursorPosition) override;
|
|
|
|
void clickReleased(const Point & cursorPosition) override;
|
|
|
|
void clickCancel(const Point & cursorPosition) override;
|
2014-08-03 14:16:19 +03:00
|
|
|
|
|
|
|
// bring overrides into scope
|
2014-08-11 17:58:34 +03:00
|
|
|
//using CButton::addCallback;
|
2014-08-03 14:16:19 +03:00
|
|
|
using CToggleBase::addCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CToggleGroup : public CIntObject
|
|
|
|
{
|
2014-07-15 10:14:49 +03:00
|
|
|
CFunctionList<void(int)> onChange; //called when changing selected button with new button's id
|
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
int selectedID;
|
2014-07-15 10:14:49 +03:00
|
|
|
void selectionChanged(int to);
|
2014-08-03 14:16:19 +03:00
|
|
|
public:
|
2018-04-07 13:34:11 +02:00
|
|
|
std::map<int, std::shared_ptr<CToggleBase>> buttons;
|
2014-08-03 14:16:19 +03:00
|
|
|
|
2015-08-22 05:07:51 +02:00
|
|
|
CToggleGroup(const CFunctionList<void(int)> & OnChange);
|
2014-08-03 14:16:19 +03:00
|
|
|
|
|
|
|
void addCallback(std::function<void(int)> callback);
|
2018-04-07 13:34:11 +02:00
|
|
|
void resetCallback();
|
2014-08-03 17:31:56 +03:00
|
|
|
|
|
|
|
/// add one toggle/button into group
|
2018-04-07 13:34:11 +02:00
|
|
|
void addToggle(int index, std::shared_ptr<CToggleBase> button);
|
2014-08-03 17:31:56 +03:00
|
|
|
/// Changes selection to specific value. Will select toggle with this ID, if present
|
2014-08-03 14:16:19 +03:00
|
|
|
void setSelected(int id);
|
2020-01-21 11:09:08 +02:00
|
|
|
/// in some cases, e.g. LoadGame difficulty selection, after refreshing the UI, the ToggleGroup should
|
|
|
|
/// reset all of it's child buttons to BLOCK state, then make selection again
|
|
|
|
void setSelectedOnly(int id);
|
2022-12-17 06:19:16 +02:00
|
|
|
int getSelected() const;
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|