-
Notifications
You must be signed in to change notification settings - Fork 5
/
button.cpp
331 lines (271 loc) · 5.22 KB
/
button.cpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
Copyright 2013-2015 Rohit Nirmal
This file is part of Clonepoint.
Clonepoint is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clonepoint is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clonepoint. If not, see <http://www.gnu.org/licenses/>.
*/
#include "button.h"
TextLabel::TextLabel(int x, int y, std::string text, float r, float g, float b)
{
_text = text;
_x = x;
_y = y;
_red = r;
_green = g;
_blue = b;
_alpha = 1.0f;
_visible = true;
}
TextLabel::~TextLabel()
{
}
std::string& TextLabel::getText()
{
return _text;
}
void TextLabel::getPosition(int* x, int* y)
{
*x = _x;
*y = _y;
}
void TextLabel::getColors(float* r, float* g, float* b, float* a)
{
*r = _red;
*g = _green;
*b = _blue;
*a = _alpha;
}
void TextLabel::setText(std::string text)
{
_text = text;
}
void TextLabel::setPosition(int x, int y)
{
_x = x;
_y = y;
}
void TextLabel::setPositionWithOffset(int x, int y, int xOff, int yOff)
{
setPosition(x, y);
_x += xOff;
_y += yOff;
}
void TextLabel::update(unsigned int dT) {}
bool TextLabel::isVisible()
{
return _visible;
}
void TextLabel::setVisible(bool b)
{
_visible = b;
}
FloatingMessage::FloatingMessage(int x, int y, std::string text, float r, float g, float b) : TextLabel(x, y, text, r, g, b)
{
_timeRemaining = 0;
_setTime = 0;
}
FloatingMessage::~FloatingMessage()
{
}
void FloatingMessage::update(unsigned int dT)
{
if (_visible)
{
_timeRemaining -= dT;
int diff = _setTime - _timeRemaining;
if (diff < 250)
{
_alpha = ((float)(diff) / 250);
}
if (_timeRemaining < 500)
{
_alpha = (float)(_timeRemaining) / 500;
}
if (_timeRemaining <= 0)
{
setVisible(false);
_timeRemaining = 0;
_setTime = 0;
}
}
}
void FloatingMessage::init(int x, int y, std::string text, unsigned int timeRemaining)
{
setPosition(x, y);
setText(text);
setTimeRemaining(timeRemaining);
_alpha = 0.0f;
}
void FloatingMessage::setTimeRemaining(unsigned int timeRemaining)
{
_setTime = timeRemaining;
_timeRemaining = timeRemaining;
setVisible(_timeRemaining > 0);
}
Button::Button(int x, int y, int w, int h)
{
_x = x;
_y = y;
_w = w;
_h = h;
_highlighted = false;
_clickable = true;
}
Button::Button(int x, int y, int w, int h, bool clickable)
{
_x = x;
_y = y;
_w = w;
_h = h;
_highlighted = false;
_clickable = clickable;
}
Button::~Button()
{
}
bool Button::isMouseIntersecting(int mx, int my)
{
return (mx >= _x && mx <= _x + _w && my >= _y && my <= _y + _h);
}
bool Button::isHighlighted()
{
return _highlighted;
}
void Button::setHighlighted(bool b)
{
_highlighted = b;
}
int Button::getX()
{
return _x;
}
int Button::getY()
{
return _y;
}
int Button::getW()
{
return _w;
}
int Button::getH()
{
return _h;
}
void Button::setW(int w)
{
_w = w;
}
void Button::setPosition(int x, int y)
{
_x = x;
_y = y;
}
void Button::setPositionWithOffset(int x, int y, int xOff, int yOff)
{
setPosition(x, y);
_x += xOff;
_y += yOff;
}
bool Button::isClickable()
{
return _clickable;
}
//Text button functions
TextButton::TextButton(int x, int y, int w, int h, std::string text) : Button(x, y, w, h)
{
_text = text;
_red = 1.0f;
_green = 1.0f;
_blue = 1.0f;
}
TextButton::TextButton(int x, int y, int w, int h, std::string text, float r, float g, float b) : Button(x, y, w, h)
{
_text = text;
_red = r;
_green = g;
_blue = b;
}
TextButton::~TextButton()
{
}
void TextButton::changeColor(float r, float g, float b)
{
_red = r;
_green = g;
_blue = b;
}
const char* TextButton::getText()
{
return _text.c_str();
}
void TextButton::setText(std::string text)
{
_text = text;
_w = (text.length() + 2) * 16;
}
void TextButton::getColors(float* r, float* g, float* b)
{
*r = _red;
*g = _green;
*b = _blue;
}
void TextButton::handleMouseIntersection(int mx, int my)
{
if (isMouseIntersecting(mx, my))
{
changeColor(0, 1, 0);
}
else
{
changeColor(1, 1, 1);
}
}
ImageButton::ImageButton(int x, int y, int w, int h, unsigned int index) : Button(x, y, w, h)
{
_defaultIndex = index;
_selectedIndex = index;
_currIndex = index;
}
ImageButton::ImageButton(int x, int y, int w, int h, bool clickable, unsigned int index) : Button(x, y, w, h, clickable)
{
_defaultIndex = index;
_selectedIndex = index;
_currIndex = index;
}
ImageButton::ImageButton(int x, int y, int w, int h, unsigned int index, unsigned int selectedIndex) : Button(x, y, w, h)
{
_defaultIndex = index;
_selectedIndex = selectedIndex;
_currIndex = index;
}
ImageButton::~ImageButton()
{
}
unsigned int ImageButton::getSpriteIndex()
{
return _currIndex;
}
void ImageButton::handleMouseIntersection(int mx, int my)
{
if (isMouseIntersecting(mx, my))
{
_currIndex = _selectedIndex;
}
else
{
_currIndex = _defaultIndex;
}
}
void ImageButton::changeSprites(unsigned int index, unsigned int selectedIndex)
{
_currIndex = _defaultIndex = index;
_selectedIndex = selectedIndex;
}