-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradio.cpp
More file actions
118 lines (107 loc) · 2.02 KB
/
radio.cpp
File metadata and controls
118 lines (107 loc) · 2.02 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
#include "radio.h"
RadioButton::RadioButton(const std::string& caption, int w)
{
m_x = 0; m_y = 0; m_w = w; m_h = 0;
m_caption = caption;
m_curitem = 0;
m_curhoveritem = -1;
m_font = new Font("images/mono.ttf",18);
m_enabled = true;
m_audio = NULL;
}
void RadioButton::addAudio(Audio *audio)
{
m_audio = audio;
}
void RadioButton::render(int x, int y)
{
m_x = x; m_y = y + 25;
glColor3f(0,1,0);
m_font->render(m_caption, m_x, m_y - 25 + 17);
glBegin(GL_LINE_LOOP);
glVertex2f(m_x, m_y);
glVertex2f(m_x + m_w, m_y);
glVertex2f(m_x + m_w, m_y + m_h);
glVertex2f(m_x, m_y + m_h);
glEnd();
for(int i=0; i < m_items.size(); ++i)
{
int x = m_x + 4;
int y = m_y + i*25 + 17;
if(i == m_curitem)
{
glColor3f(1, 0, 0);
m_font->render(" " + m_items[i], x, y);
}
else if(i == m_curhoveritem)
{
glColor3f(1, 1, 0);
m_font->render(" " + m_items[i], x, y);
}
else
{
glColor3f(0, 1, 0);
m_font->render(" " + m_items[i], x, y);
}
}
}
void RadioButton::addItem(const std::string& item)
{
m_items.push_back(item);
m_h += 25;
}
void RadioButton::setChecked(const std::string& item)
{
int i=0;
for(i=0; i<m_items.size(); ++i)
{
if(m_items[i] == item) break;
}
if(i < m_items.size())
{
m_curitem = i;
}
}
std::string RadioButton::getChecked()
{
return m_items[m_curitem];
}
void RadioButton::handleEvents(SDL_Event event)
{
if(m_enabled == false) return;
if(event.type == SDL_MOUSEBUTTONDOWN)
{
int x = event.button.x;
int y = event.button.y;
if((x >= m_x && x <= m_x+m_w) && (y >= m_y && y <= m_y+m_h))
{
if(m_audio) m_audio->playSound("click");
m_curitem = (y - m_y)/25;
}
}
if(event.type == SDL_MOUSEMOTION)
{
int x = event.motion.x;
int y = event.motion.y;
if((x >= m_x && x <= m_x+m_w) && (y >= m_y && y <= m_y+m_h))
{
m_curhoveritem = (y - m_y)/25;
}
else
{
m_curhoveritem = -1;
}
}
}
bool RadioButton::isEnabled()
{
return m_enabled;
}
void RadioButton::setEnabled(bool truth)
{
m_enabled = truth;
}
RadioButton::~RadioButton()
{
delete m_font;
}