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"
|
|
|
|
#include "../gui/SDL_Extensions.h"
|
|
|
|
|
|
|
|
#include "../../lib/FunctionList.h"
|
|
|
|
|
|
|
|
struct SDL_Surface;
|
|
|
|
struct Rect;
|
|
|
|
class CAnimImage;
|
|
|
|
class CLabel;
|
|
|
|
class CAnimation;
|
|
|
|
|
|
|
|
namespace config
|
|
|
|
{
|
|
|
|
struct ButtonInfo;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
private:
|
2014-08-03 14:16:19 +03:00
|
|
|
std::vector<std::string> imageNames;//store list of images that can be used by this button
|
|
|
|
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
|
2014-08-03 14:16:19 +03:00
|
|
|
std::string helpBox; //for right-click help
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
CAnimImage * image; //image for this button
|
2014-08-03 17:31:56 +03:00
|
|
|
CIntObject * overlay;//object-overlay, can be null
|
|
|
|
|
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;
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
// if set, button will have 1-px border around it with this color
|
2014-08-03 14:16:19 +03:00
|
|
|
boost::optional<SDL_Color> borderColor;
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
/// adds one more callback to on-click actions
|
2014-08-03 14:16:19 +03:00
|
|
|
void addCallback(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
|
2014-08-03 14:16:19 +03:00
|
|
|
void addOverlay(CIntObject * newOverlay);
|
|
|
|
void addTextOverlay(const std::string &Text, EFonts font, SDL_Color color = Colors::WHITE);
|
|
|
|
|
|
|
|
void addImage(std::string filename);
|
|
|
|
void addHoverText(ButtonState state, std::string text);
|
|
|
|
|
|
|
|
void setImageOrder(int state1, int state2, int state3, int state4);
|
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
|
|
|
|
CButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
|
|
|
|
CFunctionList<void()> Callback = 0, int key=0, bool playerColoredButton = false );
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 14:16:19 +03:00
|
|
|
/// Appearance modifiers
|
|
|
|
void setIndex(size_t index, bool playerColoredButton=false);
|
2016-10-16 11:58:18 +02:00
|
|
|
void setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton=false, int animFlags=0);
|
2014-08-03 14:16:19 +03:00
|
|
|
void setPlayerColor(PlayerColor player);
|
|
|
|
|
|
|
|
/// CIntObject overrides
|
|
|
|
void clickRight(tribool down, bool previousState) override;
|
|
|
|
void clickLeft(tribool down, bool previousState) override;
|
|
|
|
void hover (bool on) override;
|
|
|
|
void showAll(SDL_Surface * to) override;
|
|
|
|
|
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();
|
|
|
|
static std::pair<std::string, std::string> tooltip(const JsonNode & localizedTexts);
|
|
|
|
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);
|
|
|
|
|
|
|
|
void addCallback(std::function<void(bool)> callback);
|
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;
|
2014-07-15 10:14:49 +03:00
|
|
|
public:
|
2014-08-03 14:16:19 +03:00
|
|
|
CToggleButton(Point position, const std::string &defName, const std::pair<std::string, std::string> &help,
|
|
|
|
CFunctionList<void(bool)> Callback = 0, int key=0, bool playerColoredButton = false );
|
|
|
|
void clickLeft(tribool down, bool previousState) override;
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
std::map<int, CToggleBase*> buttons;
|
|
|
|
|
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);
|
2014-08-03 17:31:56 +03:00
|
|
|
|
|
|
|
/// add one toggle/button into group
|
2014-08-03 14:16:19 +03:00
|
|
|
void addToggle(int index, 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);
|
2014-07-15 10:14:49 +03:00
|
|
|
};
|
|
|
|
|
2015-08-22 04:45:31 +02:00
|
|
|
/// A typical slider for volume with an animated indicator
|
|
|
|
class CVolumeSlider : public CIntObject
|
|
|
|
{
|
|
|
|
int value;
|
|
|
|
CFunctionList<void(int)> onChange;
|
|
|
|
CAnimImage * animImage;
|
|
|
|
const std::pair<std::string, std::string> * const helpHandlers;
|
|
|
|
void setVolume(const int v);
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// @param position coordinates of slider
|
|
|
|
/// @param defName name of def animation for slider
|
|
|
|
/// @param value initial value for volume
|
|
|
|
/// @param help pointer to first helptext of slider
|
|
|
|
CVolumeSlider(const Point &position, const std::string &defName, const int value,
|
|
|
|
const std::pair<std::string, std::string> * const help);
|
|
|
|
|
|
|
|
void moveTo(int id);
|
|
|
|
void addCallback(std::function<void(int)> callback);
|
|
|
|
|
|
|
|
|
|
|
|
void clickLeft(tribool down, bool previousState) override;
|
|
|
|
void clickRight(tribool down, bool previousState) override;
|
2015-10-12 15:47:10 +02:00
|
|
|
void wheelScrolled(bool down, bool in) override;
|
2015-08-22 04:45:31 +02:00
|
|
|
};
|
|
|
|
|
2014-07-15 10:14:49 +03:00
|
|
|
/// A typical slider which can be orientated horizontally/vertically.
|
|
|
|
class CSlider : public CIntObject
|
|
|
|
{
|
2014-08-03 14:16:19 +03:00
|
|
|
CButton *left, *right, *slider; //if vertical then left=up
|
2014-07-15 10:14:49 +03:00
|
|
|
int capacity;//how many elements can be active at same time (e.g. hero list = 5)
|
|
|
|
int positions; //number of highest position (0 if there is only one)
|
|
|
|
bool horizontal;
|
2014-08-03 17:31:56 +03:00
|
|
|
int amount; //total amount of elements (e.g. hero list = 0-8)
|
|
|
|
int value; //first active element
|
|
|
|
int scrollStep; // how many elements will be scrolled via one click, default = 1
|
|
|
|
CFunctionList<void(int)> moved;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
void updateSliderPos();
|
2014-07-15 10:14:49 +03:00
|
|
|
void sliderClicked();
|
2014-08-03 17:31:56 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
enum EStyle {
|
|
|
|
BROWN,
|
|
|
|
BLUE
|
|
|
|
};
|
|
|
|
|
|
|
|
void block(bool on);
|
|
|
|
|
|
|
|
/// Controls how many items wil be scrolled via one click
|
|
|
|
void setScrollStep(int to);
|
|
|
|
|
|
|
|
/// Value modifiers
|
2014-07-15 10:14:49 +03:00
|
|
|
void moveLeft();
|
|
|
|
void moveRight();
|
2014-08-03 17:31:56 +03:00
|
|
|
void moveTo(int value);
|
|
|
|
void moveBy(int amount);
|
2014-11-24 14:24:03 +02:00
|
|
|
void moveToMin();
|
2014-08-03 17:31:56 +03:00
|
|
|
void moveToMax();
|
|
|
|
|
|
|
|
/// Amount modifier
|
2014-07-15 10:14:49 +03:00
|
|
|
void setAmount(int to);
|
|
|
|
|
2014-08-03 17:31:56 +03:00
|
|
|
/// Accessors
|
|
|
|
int getAmount();
|
|
|
|
int getValue();
|
|
|
|
|
|
|
|
void addCallback(std::function<void(int)> callback);
|
|
|
|
|
2015-10-12 15:47:10 +02:00
|
|
|
void keyPressed(const SDL_KeyboardEvent & key) override;
|
|
|
|
void wheelScrolled(bool down, bool in) override;
|
|
|
|
void clickLeft(tribool down, bool previousState) override;
|
|
|
|
void mouseMoved (const SDL_MouseMotionEvent & sEvent) override;
|
2016-10-16 02:52:11 +02:00
|
|
|
void showAll(SDL_Surface * to) override;
|
2014-07-15 10:14:49 +03:00
|
|
|
|
2015-08-22 18:38:34 +02:00
|
|
|
/// @param position coordinates of slider
|
|
|
|
/// @param length length of slider ribbon, including left/right buttons
|
|
|
|
/// @param Moved function that will be called whenever slider moves
|
|
|
|
/// @param Capacity maximal number of visible at once elements
|
|
|
|
/// @param Amount total amount of elements, including not visible
|
|
|
|
/// @param Value starting position
|
2014-08-03 17:31:56 +03:00
|
|
|
CSlider(Point position, int length, std::function<void(int)> Moved, int Capacity, int Amount,
|
|
|
|
int Value=0, bool Horizontal=true, EStyle style = BROWN);
|
2014-07-15 10:14:49 +03:00
|
|
|
~CSlider();
|
|
|
|
};
|