-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdgui.cpp
254 lines (234 loc) · 5.94 KB
/
cdgui.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
#include "stdafx.h"
#include "cdg.h"
#include "cdgmain.h"
#include "cdgui.h"
#include "vera.h"
const char* CHANNEL_TEXT[] = {
"0", "1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15"
};
CDGUI::CDGUI() {
TTF_Init();
//if this fails we'll crash, but what do I care?
//try http://www.fltk.org/newsgroups.php?gfltk.general+v:22083
//now we're loading in from executable image
_fontMem = SDL_RWFromMem(resource_Vera_TTF(), resource_Vera_TTF_len());
font = TTF_OpenFontRW(_fontMem, 12, 12);
if (!font)
{
cerr << "TTF_OpenFontRW: " << TTF_GetError() << endl;
throw "Could not load font (see std err)";
}
resetButtonStates();
}
CDGUI::~CDGUI() {
TTF_CloseFont(font);
SDL_FreeRW(_fontMem);
TTF_Quit();
resetButtonStates();
font = NULL;
}
void CDGUI::resetButtonStates() {
btnPlayDown = btnLoadCDGDown = btnLoadAudioDown = btnSaveImageDown = btnPauseDown = btnStopDown = btnChannelDownDown = btnChannelUpDown = btnSeekbarDown = false;
}
Button CDGUI::mouseLocationToButton(const SDL_Event *event, const MainConfig *config) {
int hoff = config->h - UI_HEIGHT;
int x = event->button.x;
if (event->button.y > hoff
&& event->button.y < config->h - 1) {
if (x < UI_WIDTH)
return B_PLAY;
else if (x < UI_WIDTH * 2)
return B_PAUSE;
else if (x < UI_WIDTH * 3)
return B_STOP;
else if (x < UI_WIDTH * 4)
return B_LOADCDG;
else if (x < UI_WIDTH * 5)
return B_LOADAUDIO;
else if (x < UI_WIDTH * 6)
return B_SAVEIMAGE;
else if (x < UI_WIDTH * 7)
return B_CHANNEL_DOWN;
else if (x < UI_WIDTH * 8)
return B_NONE; // channel indicator
else if (x < UI_WIDTH * 9)
return B_CHANNEL_UP;
else
return B_SEEKBAR;
}
return B_NONE;
}
void CDGUI::processMouseDown(MainRunState *run, const MainConfig *config) {
Button btn = mouseLocationToButton(&(run->event), config);
switch (btn) {
case B_PLAY:
btnPlayDown = true;
break;
case B_PAUSE:
btnPauseDown = true;
break;
case B_STOP:
btnStopDown = true;
break;
case B_LOADCDG:
btnLoadCDGDown = true;
break;
case B_LOADAUDIO:
btnLoadAudioDown = true;
break;
case B_SAVEIMAGE:
btnSaveImageDown = true;
break;
case B_CHANNEL_DOWN:
btnChannelDownDown = true;
break;
case B_CHANNEL_UP:
btnChannelUpDown = true;
break;
case B_SEEKBAR:
btnSeekbarDown = true;
break;
case B_NONE:
// Do nothing
break;
}
}
void CDGUI::processMouseUp(MainRunState *run, const MainConfig *config) {
Button btn = mouseLocationToButton(&(run->event), config);
switch (btn) {
case B_PLAY:
if (btnPlayDown)
run->nextTransport = T_PLAY;
break;
case B_PAUSE:
if (btnPauseDown)
run->nextTransport = T_PAUSE;
break;
case B_STOP:
if (btnStopDown)
run->nextTransport = T_STOP;
break;
case B_LOADCDG:
if (btnLoadCDGDown)
if (open_filechooser(run))
run->nextTransport = T_STOP;
break;
case B_LOADAUDIO:
if (btnLoadAudioDown)
if (config->audioEnabled)
if (open_filechooser(run, true))
run->nextTransport = T_STOP;
break;
case B_SAVEIMAGE:
if (btnSaveImageDown)
run->dumpImage = true;
break;
case B_CHANNEL_DOWN:
if (btnChannelDownDown) {
if (run->channel == 1) {
run->nextChannel = 15;
} else {
run->nextChannel = run->channel - 1;
}
}
break;
case B_CHANNEL_UP:
if (btnChannelUpDown) {
if (run->channel == 15) {
run->nextChannel = 1;
} else {
run->nextChannel = run->channel + 1;
}
}
break;
case B_SEEKBAR:
if (btnSeekbarDown) {
double percent = double((UI_WIDTH * UI_NUM_BUTTONS) - run->event.button.x) / double((UI_WIDTH * UI_NUM_BUTTONS) - config->w);
if (percent < 0.00000001) // random number
percent = 0; //prevent -0% issues
//cout << (percent * 100) << "%" << endl;
run->nextLoc = percent;
}
break;
case B_NONE:
// Do nothing
break;
}
resetButtonStates();
}
void CDGUI::render(SDL_Renderer *renderer, const MainRunState *run, const MainConfig *config) {
int hoff = config->h - UI_HEIGHT;
char postext[80];
SDL_Color text = {255, 255, 255};
bool btnDown[] = {
btnPlayDown,
btnPauseDown,
btnStopDown,
btnLoadCDGDown,
btnLoadAudioDown,
btnSaveImageDown,
btnChannelDownDown,
false,
btnChannelUpDown
};
const char* btnText[] = {
"Play",
"Pause",
"Stop",
"Open",
"Audio",
"SavImg",
"Ch-",
CHANNEL_TEXT[run->channel],
"Ch+"
};
for (int i = 0; i < UI_NUM_BUTTONS; i++) {
SDL_Rect border;
border.x = (UI_WIDTH * i);
border.y = hoff;
border.w = UI_WIDTH;
border.h = UI_HEIGHT - 1;
if (btnDown[i]) {
SDL_SetRenderDrawColor(renderer, 83, 83, 83, 255);
} else {
SDL_SetRenderDrawColor(renderer, 122, 122, 122, 255);
}
SDL_RenderFillRect(renderer, &border);
SDL_SetRenderDrawColor(renderer, 163, 163, 163, 255);
SDL_RenderDrawRect(renderer, &border);
render_text(renderer, (UI_WIDTH * i) + 4, hoff + 4, font, text, btnText[i]);
}
if (btnSeekbarDown) {
SDL_SetRenderDrawColor(renderer, 83, 83, 83, 255);
} else {
SDL_SetRenderDrawColor(renderer, 163, 163, 163, 255);
}
SDL_Rect seekbarRect;
seekbarRect.x = (UI_WIDTH * UI_NUM_BUTTONS) + 1;
seekbarRect.y = hoff + 1;
seekbarRect.w = config->w - (UI_WIDTH * UI_NUM_BUTTONS) - 1;
seekbarRect.h = UI_HEIGHT - 1;
SDL_RenderFillRect(renderer, &seekbarRect);
if (run->cdgLoaded) {
if (btnSeekbarDown) {
SDL_SetRenderDrawColor(renderer, 44, 44, 44, 255);
} else {
SDL_SetRenderDrawColor(renderer, 83, 83, 83, 255);
}
SDL_Rect posRect;
posRect.x = (UI_WIDTH * UI_NUM_BUTTONS) + 1;
posRect.y = hoff + 1;
posRect.w = int((config->w - (UI_WIDTH * UI_NUM_BUTTONS) - 1) * (run->cdgLoc / double(run->cdgStat.st_size)));
posRect.h = UI_HEIGHT - 1;
SDL_RenderFillRect(renderer, &posRect);
int limsecs = CDG::sizeToSeconds(run->cdgStat.st_size);
int cursecs = CDG::sizeToSeconds(run->cdgLoc);
#ifdef WIN32
sprintf_s(postext, 78, "%i / %i (seconds)", cursecs, limsecs);
#else
sprintf(postext, "%i / %i (seconds)", cursecs, limsecs);
#endif
render_text(renderer, (UI_WIDTH * UI_NUM_BUTTONS) + 4, hoff + 4, font, text, postext);
}
}