File: Buttons.h

package info (click to toggle)
vcmi 0.99%2Bdfsg%2Bgit20190113.f06c8a87-1
  • links: PTS, VCS
  • area: contrib
  • in suites: buster
  • size: 11,096 kB
  • sloc: cpp: 142,605; sh: 315; objc: 248; makefile: 32; ansic: 28; python: 13
file content (267 lines) | stat: -rw-r--r-- 8,487 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * 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
 *
 */
#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;
}

/// 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;

public:
	enum ButtonState
	{
		NORMAL=0,
		PRESSED=1,
		BLOCKED=2,
		HIGHLIGHTED=3
	};
private:
	std::vector<std::string> imageNames;//store list of images that can be used by this button
	size_t currentImage;

	ButtonState state;//current state of button from enum

	std::array<int, 4> stateToIndex; // mapping of button state to index of frame in animation
	std::array<std::string, 4> hoverTexts; //texts for statusbar, if empty - first entry will be used
	std::array<boost::optional<SDL_Color>, 4> stateToBorderColor; // mapping of button state to border color
	std::string helpBox; //for right-click help

	std::shared_ptr<CAnimImage> image; //image for this button
	std::shared_ptr<CIntObject> overlay;//object-overlay, can be null
	bool animateLonelyFrame = false;
protected:
	void onButtonClicked(); // calls callback
	void update();//to refresh button after image or text change

	// internal method to change state. Public change can be done only via block()
	void setState(ButtonState newState);
	ButtonState getState();

public:
	bool actOnDown,//runs when mouse is pressed down over it, not when up
		hoverable,//if true, button will be highlighted when hovered (e.g. main menu)
		soundDisabled;

	// sets border color for each button state;
	// if it's set, the button will have 1-px border around it with this color
	void setBorderColor(boost::optional<SDL_Color> normalBorderColor,
	                    boost::optional<SDL_Color> pressedBorderColor,
	                    boost::optional<SDL_Color> blockedBorderColor,
	                    boost::optional<SDL_Color> highlightedBorderColor);

	// sets the same border color for all button states.
	void setBorderColor(boost::optional<SDL_Color> borderColor);

	/// adds one more callback to on-click actions
	void addCallback(std::function<void()> callback);

	/// adds overlay on top of button image. Only one overlay can be active at once
	void addOverlay(std::shared_ptr<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);
	void setAnimateLonelyFrame(bool agreement);
	void block(bool on);

	/// State modifiers
	bool isBlocked();
	bool isHighlighted();

	/// 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 );

	/// Appearance modifiers
	void setIndex(size_t index, bool playerColoredButton=false);
	void setImage(std::shared_ptr<CAnimation> anim, bool playerColoredButton=false, int animFlags=0);
	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;

	/// generates tooltip that can be passed into constructor
	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 = "");
};

class CToggleBase
{
	CFunctionList<void(bool)> callback;
protected:

	bool selected;

	// internal method for overrides
	virtual void doSelect(bool on);

	// returns true if toggle can change its state
	bool canActivate();

public:
	/// if set to false - button can not be deselected normally
	bool allowDeselection;

	CToggleBase(CFunctionList<void(bool)> callback);
	virtual ~CToggleBase();

	/// Changes selection to "on", and calls callback
	void setSelected(bool on);

	void addCallback(std::function<void(bool)> callback);
};

/// A button which can be selected/deselected, checkbox
class CToggleButton : public CButton, public CToggleBase
{
	void doSelect(bool on) override;
public:
	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
	//using CButton::addCallback;
	using CToggleBase::addCallback;
};

class CToggleGroup : public CIntObject
{
	CFunctionList<void(int)> onChange; //called when changing selected button with new button's id

	int selectedID;
	void selectionChanged(int to);
public:
	std::map<int, std::shared_ptr<CToggleBase>> buttons;

	CToggleGroup(const CFunctionList<void(int)> & OnChange);

	void addCallback(std::function<void(int)> callback);
	void resetCallback();

	/// add one toggle/button into group
	void addToggle(int index, std::shared_ptr<CToggleBase> button);
	/// Changes selection to specific value. Will select toggle with this ID, if present
	void setSelected(int id);
};

/// A typical slider for volume with an animated indicator
class CVolumeSlider : public CIntObject
{
	int value;
	CFunctionList<void(int)> onChange;
	std::shared_ptr<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;
	void wheelScrolled(bool down, bool in) override;
};

/// A typical slider which can be orientated horizontally/vertically.
class CSlider : public CIntObject
{
	//if vertical then left=up
	std::shared_ptr<CButton> left;
	std::shared_ptr<CButton> right;
	std::shared_ptr<CButton> slider;

	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;
	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;

	void updateSliderPos();
	void sliderClicked();

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
	void moveLeft();
	void moveRight();
	void moveTo(int value);
	void moveBy(int amount);
	void moveToMin();
	void moveToMax();

	/// Amount modifier
	void setAmount(int to);

	/// Accessors
	int getAmount();
	int getValue();

	void addCallback(std::function<void(int)> callback);

	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;
	void showAll(SDL_Surface * to) override;

	 /// @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
	CSlider(Point position, int length, std::function<void(int)> Moved, int Capacity, int Amount,
		int Value=0, bool Horizontal=true, EStyle style = BROWN);
	~CSlider();
};