-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbutton.cpp
More file actions
320 lines (281 loc) · 7.67 KB
/
Copy pathbutton.cpp
File metadata and controls
320 lines (281 loc) · 7.67 KB
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
//=============================================================================
// Author: Arvind
// Purpose: Contains the button functions
//=============================================================================
#include "master.h"
#include "button.h"
#include "audio.h"
using namespace pyrodactyl;
//------------------------------------------------------------------------
// Purpose: Constructor
//------------------------------------------------------------------------
Button::Button()
{
visible = false;
canmove = false;
hover_prev = false;
type = TYPE_SIMPLE;
Reset();
}
//------------------------------------------------------------------------
// Purpose: Load a new Button from a file
//------------------------------------------------------------------------
void Button::Load(rapidxml::xml_node<char> * node, pyroRect *parent, const bool &echo)
{
img.Load(node, echo);
if (NodeValid("slider", node, false))
{
type = TYPE_SLIDER;
canmove = true;
slider.Load(node->first_node("slider"), parent);
Element::Load(node, &slider, echo);
//Since this is a slider, the button position (x,y) doesn't matter at all
//It's set based on the value of the slider
slider.Value(0.5f);
x = slider.x + slider.Offset(w);
y = slider.y;
//In case of a slider, the knob moves around - so the caption and tooltip
//positions are relative to the background image
tooltip.Load(node->first_node("tooltip"), &slider);
caption.Load(node->first_node("caption"), &slider);
}
else
{
Element::Load(node, parent, echo);
if (NodeValid("radio", node, false))
{
type = TYPE_RADIO;
radio.Load(node->first_node("radio"), this);
}
else if (NodeValid("select", node, false))
{
type = TYPE_STATE;
rapidxml::xml_node<char> *selnode = node->first_node("select");
state_data.img.select.Load(selnode, echo);
LoadNum(state_data.col_select.col, "color", selnode);
LoadNum(state_data.col_select.col_s, "color_s", selnode);
LoadNum(state_data.col_select.col_h, "color_h", selnode);
state_data.img.normal = img;
state_data.col_normal.col = caption.col;
state_data.col_normal.col_s = caption.col_s;
state_data.col_normal.col_h = caption.col_h;
}
else
type = TYPE_SIMPLE;
tooltip.Load(node->first_node("tooltip"), this);
caption.Load(node->first_node("caption"), this);
desc.Load(node->first_node("desc"), this);
}
if (NodeValid("hotkey", node, false))
hotkey.Load(node->first_node("hotkey"), this);
visible = true;
canmove = false;
Reset();
}
//------------------------------------------------------------------------
// Purpose: Load a new Button
//------------------------------------------------------------------------
void Button::Init(const Button &ref, pyroRect *parent, const float &XOffset, const float &YOffset)
{
img = ref.img;
type = ref.type;
Element::Init(ref, parent, XOffset, YOffset);
caption.Init(ref.caption, this);
tooltip.Init(ref.tooltip, this);
desc.Init(ref.desc, this);
state_data = ref.state_data;
visible = true;
canmove = ref.canmove;
Reset();
}
//------------------------------------------------------------------------
// Purpose: Reset the button
//------------------------------------------------------------------------
void Button::Reset()
{
mousepressed = false;
hover_mouse = false;
hover_key = false;
}
//------------------------------------------------------------------------
// Purpose: Draw
//------------------------------------------------------------------------
void Button::Draw()
{
if (visible)
{
if (type == TYPE_SLIDER)
slider.Draw();
if (mousepressed)
{
gImageManager.Draw(x, y, w, h, img.select);
tooltip.Draw();
caption.Draw(CAP_SELECT);
desc.Draw(CAP_SELECT);
}
else if (hover_mouse || hover_key)
{
gImageManager.Draw(x, y, w, h, img.hover);
tooltip.Draw();
caption.Draw(CAP_HOVER);
desc.Draw(CAP_HOVER);
}
else
{
gImageManager.Draw(x, y, w, h, img.normal);
caption.Draw(CAP_NORMAL);
desc.Draw(CAP_NORMAL);
}
if (type == TYPE_RADIO)
radio.Draw(x, y);
hotkey.Draw();
}
}
//------------------------------------------------------------------------
// Purpose: Handle input and stuff
//------------------------------------------------------------------------
ButtonAction Button::HandleEvents()
{
pyroRect dim = *this;
if (visible)
{
GLcoord2 pos = InputMousePosition();
if (InputKeyState(INPUT_MOUSE_MOVED))
{
if (dim.Contains(pos.x, pos.y))
{
hover_mouse = true;
if (!hover_prev)
{
hover_prev = true;
AudioPlay("mouseover");
}
}
else
{
hover_prev = false;
hover_mouse = false;
}
if (canmove && mousepressed)
{
GLcoord2 motion = InputMouseMovement();
if (type == TYPE_SLIDER)
{
x += motion.x;
if (x < slider.x)
x = slider.x;
else if (x + w > slider.x + slider.w)
x = slider.x + slider.w - w;
y = slider.y;
slider.Value(x, w);
}
else
{
x += motion.x;
y += motion.y;
}
return BUAC_GRABBED;
}
}
else if (InputKeyState(INPUT_LMB))
{
//Mouse button pressed, then released, comprises of a click action
if (dim.Contains(pos.x, pos.y))
mousepressed = true;
else if (type == TYPE_SLIDER) //If a person clicks on the slider bar, the knob needs to travel there
{
if (slider.Contains(pos.x, pos.y)) //incorporate if we need it
{
x = pos.x;
y = slider.y;
slider.Value(x, w);
mousepressed = true;
return BUAC_GRABBED;
}
}
}
else if (!InputKeyState(INPUT_LMB) && mousepressed)
{
Reset();
if (dim.Contains(pos.x, pos.y))
{
mousepressed = false;
AudioPlay("click");
ToggleRadioState();
return BUAC_LCLICK;
}
}
else if (hotkey.HandleEvents())
{
AudioPlay("click");
ToggleRadioState();
return BUAC_LCLICK;
}
if (type == TYPE_SLIDER && (hover_key || hover_mouse))
{
if (gInput.Pressed(CONTROL_RIGHT)) { IncSlider(slider.Inc()); }
else if (gInput.Pressed(CONTROL_LEFT)) { IncSlider(-1 * slider.Inc()); }
}
}
return BUAC_IGNORE;
}
//------------------------------------------------------------------------
// Purpose: State button functions
//------------------------------------------------------------------------
void Button::State(const bool val)
{
if (val)
{
img = state_data.img.select;
caption.col = state_data.col_select.col;
caption.col_s = state_data.col_select.col_s;
caption.col_h = state_data.col_select.col_h;
}
else
{
img = state_data.img.normal;
caption.col = state_data.col_normal.col;
caption.col_s = state_data.col_normal.col_s;
caption.col_h = state_data.col_normal.col_h;
}
//Images might be different in size
//w = gImageManager.GetTexture(img.normal)->Size().x;
//h = gImageManager.GetTexture(img.normal)->Size().y;
}
void Button::Img(const StateButtonImage &sbi)
{
//Find which is the current image and set it
if (img == state_data.img.normal)
img = sbi.normal;
else
img = sbi.select;
state_data.img = sbi;
}
//------------------------------------------------------------------------
// Purpose: Set UI position
//------------------------------------------------------------------------
void Button::SetUI(pyroRect *parent)
{
Element::SetUI(parent);
if (type == TYPE_SLIDER)
{
slider.SetUI();
//The position (x,y) of the knob doesn't matter
//It depends on the value and position of the background image
x = slider.x + slider.KnobPos(w);
y = slider.y;
//In case of a slider, the knob moves around - so the caption and tooltip
//positions are relative to the background image
tooltip.SetUI(&slider);
caption.SetUI(&slider);
}
else
{
if (type == TYPE_RADIO)
radio.SetUI(this);
tooltip.SetUI(this);
caption.SetUI(this);
desc.SetUI(this);
hotkey.SetUI(this);
}
}