-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeatMapEditor.cpp
545 lines (476 loc) · 17.6 KB
/
BeatMapEditor.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#include "BeatMapEditor.h"
#include "Menu.h"
#include "Styles.h"
#include <memory>
BeatMapEditor::BeatMapEditor(StateManager &stateManager, std::string folderPath) : StateScreen(stateManager)
{
using namespace sf;
using namespace std;
loadStaticAssets();
text.setFont(assetManager->getFont("game font"));
text.setFillColor(Color::White);
beatButton.setBaseColor(sf::Color::Black);
beatButton.setFillColor(sf::Color(255, 255, 255, 200));
beatButton.setHighlightColor(TRStyles::btnHLColor);
beatButton.setSize(sf::Vector2f(400, 400));
beatButton.setPosition(sf::Vector2f(1024 + 1024 / 2 - 200, 1152 / 2 - 200));
speedButton025 = new Button(sf::Text("x0.25", assetManager->getFont("game font"), 35));
speedButton025->setHighlightColor(TRStyles::btnHLColor);
speedButton025->setFillColor(TRStyles::btnFillColor);
speedButton025->setSize(sf::Vector2f(120, 60));
speedButton025->setPosition(sf::Vector2f(2048 - 3 * 170, 1152 - sliderHeight - 70));
speedButton025->setShortcut(Keyboard::Key::Num2);
speedButton050 = new Button(sf::Text("x0.50", assetManager->getFont("game font"), 35));
speedButton050->setHighlightColor(TRStyles::btnHLColor);
speedButton050->setFillColor(TRStyles::btnFillColor);
speedButton050->setSize(sf::Vector2f(120, 60));
speedButton050->setPosition(sf::Vector2f(2048 - 2 * 170, 1152 - sliderHeight - 70));
speedButton050->setShortcut(Keyboard::Key::Num5);
speedButton100 = new Button(sf::Text("x1", assetManager->getFont("game font"), 35));
speedButton100->setHighlightColor(TRStyles::btnHLColor);
speedButton100->setFillColor(TRStyles::btnFillColor);
speedButton100->setSize(sf::Vector2f(120, 60));
speedButton100->setPosition(sf::Vector2f(2048 - 170, 1152 - sliderHeight - 70));
speedButton100->setShortcut(Keyboard::Key::Num1);
dividerButton1 = new Button(sf::Text("1", assetManager->getFont("game font"), 35));
dividerButton1->setHighlightColor(TRStyles::btnHLColor);
dividerButton1->setFillColor(TRStyles::btnFillColor);
dividerButton1->setSize(sf::Vector2f(120, 60));
dividerButton1->setPosition(sf::Vector2f(2048 - 170, sliderHeight + 10));
dividerButton12 = new Button(sf::Text("1/2", assetManager->getFont("game font"), 35));
dividerButton12->setHighlightColor(TRStyles::btnHLColor);
dividerButton12->setFillColor(TRStyles::btnFillColor);
dividerButton12->setSize(sf::Vector2f(120, 60));
dividerButton12->setPosition(sf::Vector2f(2048 - 2 * 170, sliderHeight + 10));
dividerButton13 = new Button(sf::Text("1/3", assetManager->getFont("game font"), 35));
dividerButton13->setHighlightColor(TRStyles::btnHLColor);
dividerButton13->setFillColor(TRStyles::btnFillColor);
dividerButton13->setSize(sf::Vector2f(120, 60));
dividerButton13->setPosition(sf::Vector2f(2048 - 3 * 170, sliderHeight + 10));
dividerButton14 = new Button(sf::Text("1/4", assetManager->getFont("game font"), 35));
dividerButton14->setHighlightColor(TRStyles::btnHLColor);
dividerButton14->setFillColor(TRStyles::btnFillColor);
dividerButton14->setSize(sf::Vector2f(120, 60));
dividerButton14->setPosition(sf::Vector2f(2048 - 4 * 170, sliderHeight + 10));
fs::path audioPath = folderPath;
audioPath.append(audioPath.filename().string());
audioPath = AssetManager::getAudioFilePathExtension(audioPath);
std::cout << audioPath.string() << std::endl;
buffer.loadFromFile(audioPath.string()); // TODO: add error catching
sound.setBuffer(buffer);
fs::path txtPath = folderPath;
txtPath.append(txtPath.filename().string() + ".txt");
if (!fs::exists(txtPath))
{
cerr << "Text file doesn't exist." << endl;
ofstream newBeatMapTextFile(txtPath);
newBeatMapTextFile.close();
}
this->textFilePath = fs::absolute(txtPath).string(); // change to text file file with audio file path
musicDurationMS = buffer.getDuration().asMilliseconds();
std::ifstream inFile;
inFile.open(textFilePath);
if (!inFile)
{
cerr << "Unable to open file " + textFilePath << endl;;
}
else
{
char beat[10];
if (inFile.getline(beat, 10, '\r')) // read bpm
{
char temp[6] = { 0 };
std::copy(beat + 10 - 6, beat + 10, temp);
bpm = atoi(temp);
mspb = std::round(60 * 1000 / bpm);
std::cout << bpm << std::endl;
}
else
{
breakfastquay::MiniBPM bpmDetector(buffer.getSampleRate());
int sampleCount = buffer.getSampleCount();
auto samples = buffer.getSamples();
float* fSamples = new float[buffer.getSampleCount()];
for (int i = 0; i < sampleCount; i++)
{
fSamples[i] = static_cast<float>(samples[i]);
}
bpm = std::round(bpmDetector.estimateTempoOfSamples(fSamples, buffer.getSampleCount()));
mspb = 60 * 1000 / bpm;
std::cout << bpm << std::endl;
}
while (inFile.getline(beat, 10, '\r'))
{
beatsTime.push_back(atoi(beat));
}
}
beatIt = beatsTime.begin();
// must play before setPlayingOffset
sound.play();
sound.pause();
sound.setPlayingOffset(milliseconds(0));
cursorRelToMusicMS = sound.getPlayingOffset().asMilliseconds();
}
void BeatMapEditor::loadStaticAssets()
{
using namespace sf;
CircleShape *playButton = new CircleShape(20, 3);
playButton->setFillColor(Color::White);
playButton->setPosition(80, 1152 - sliderHeight - 40);
playButton->rotate(90);
RectangleShape *leftPause = new RectangleShape();
leftPause->setSize(Vector2f(10, 40));
leftPause->setFillColor(Color::White);
leftPause->setPosition(50, 1152 - sliderHeight - 40);
RectangleShape *rightPause = new RectangleShape();
rightPause->setSize(Vector2f(10, 40));
rightPause->setFillColor(Color::White);
rightPause->setPosition(70, 1152 - sliderHeight - 40);
RectangleShape *wholeAudioSlider = new RectangleShape(); // a slider of the whole audio file
wholeAudioSlider->setSize(Vector2f(2048, sliderHeight * 2/3));
wholeAudioSlider->setFillColor(Color(0, 0, 0, 150));
wholeAudioSlider->setOutlineColor(Color::Transparent);
wholeAudioSlider->setOutlineThickness(5);
wholeAudioSlider->setPosition(0, 1152 - sliderHeight);
RectangleShape *partAudioSlider = new RectangleShape(); // a slider show only 5 sec part of the audio at the cursor
partAudioSlider->setSize(Vector2f(2048, sliderHeight));
partAudioSlider->setOutlineColor(Color::Transparent);
partAudioSlider->setOutlineThickness(5);
partAudioSlider->setFillColor(Color(0, 0, 0, 150));
partAudioSlider->setPosition(0, 0);
RectangleShape *partAudioCursor = new RectangleShape(); // cursor for part audio slider
partAudioCursor->setSize(Vector2f(10, sliderHeight));
partAudioCursor->setFillColor(Color(245, 222, 72));
partAudioCursor->setPosition(24 - 5 + sliderLength / 2, 0); // middle of slider
CircleShape *beatButton = new CircleShape();
beatButton->setRadius(250);
beatButton->setPosition(1024 - 250, 576 - 250);
beatButton->setFillColor(Color::White);
CircleShape *beatHLButton = new CircleShape();
beatHLButton->setRadius(250);
beatHLButton->setPosition(1024 - 250, 576 - 250);
beatHLButton->setFillColor(Color(0, 186, 211));
assetManager->loadDrawable("play button", std::unique_ptr<sf::Drawable>(playButton));
assetManager->loadDrawable("left pause", std::unique_ptr<sf::Drawable>(leftPause));
assetManager->loadDrawable("right pause", std::unique_ptr<sf::Drawable>(rightPause));
assetManager->loadDrawable("whole audio slider", std::unique_ptr<sf::Drawable>(wholeAudioSlider));
assetManager->loadDrawable("part audio slider", std::unique_ptr<sf::Drawable>(partAudioSlider));
assetManager->loadDrawable("part audio cursor", std::unique_ptr<sf::Drawable>(partAudioCursor));
assetManager->loadDrawable("beat button", std::unique_ptr<sf::Drawable>(beatButton));
assetManager->loadDrawable("beat highlight button", std::unique_ptr<sf::Drawable>(beatHLButton));
}
BeatMapEditor::~BeatMapEditor()
{
delete speedButton025;
delete speedButton050;
delete speedButton100;
}
void BeatMapEditor::save()
{
using namespace std;
std::ofstream outFile;
outFile.open(textFilePath, ios::out);
outFile << "bpm " << bpm << '\r';
list<int>::iterator it = beatsTime.begin();
while (it != beatsTime.end())
{
outFile << *it << '\r';
it++;
}
outFile.close();
}
void BeatMapEditor::tick(const float & dt, sf::RenderWindow& window)
{
using namespace sf;
if (sound.getStatus() == Music::Status::Stopped)
{
sound.play();
sound.pause();
sound.setPlayingOffset(milliseconds(musicDurationMS));
}
cursorRelToMusicMS = sound.getPlayingOffset().asMilliseconds();
}
void BeatMapEditor::render(sf::RenderWindow& window)
{
using namespace sf;
using namespace std;
simulatorBoard.render(window);
beatButton.setHighlight(beatButton.mouseInButton(window));
window.draw(beatButton);
speedButton025->setHighlight(speedButton025->mouseInButton(window) || sound.getPitch() == 0.25);
speedButton050->setHighlight(speedButton050->mouseInButton(window) || sound.getPitch() == 0.5);
speedButton100->setHighlight(speedButton100->mouseInButton(window) || sound.getPitch() == 1);
window.draw(*speedButton025);
window.draw(*speedButton050);
window.draw(*speedButton100);
dividerButton1->setHighlight(dividerButton1->mouseInButton(window) || divider == 1);
dividerButton12->setHighlight(dividerButton12->mouseInButton(window) || divider == 2);
dividerButton13->setHighlight(dividerButton13->mouseInButton(window) || divider == 3);
dividerButton14->setHighlight(dividerButton14->mouseInButton(window) || divider == 4);
window.draw(*dividerButton1);
window.draw(*dividerButton12);
window.draw(*dividerButton13);
window.draw(*dividerButton14);
text.setFillColor(Color(255, 255, 255, 100));
text.setCharacterSize(30);
text.setString("Right click over the beat to delete it");
text.setPosition(100, sliderHeight);
window.draw(text);
if (sound.getStatus() != Music::Status::Playing) // draw playing status
{
window.draw(assetManager->getDrawable("play button"));
}
else
{
window.draw(assetManager->getDrawable("left pause"));
window.draw(assetManager->getDrawable("right pause"));
}
window.draw(assetManager->getDrawable("whole audio slider"));
window.draw(assetManager->getDrawable("part audio slider"));
window.draw(assetManager->getDrawable("part audio cursor"));
RectangleShape wholeAudioCursor; // cursor for whole audio slider
wholeAudioCursor.setSize(Vector2f(10, sliderHeight * 2 / 3));
wholeAudioCursor.setFillColor(Color::Yellow);
wholeAudioCursor.setOutlineColor(Color::Yellow);
wholeAudioCursor.setPosition(24 - 5 + sliderLength * cursorRelToMusicMS / musicDurationMS, 1152 - sliderHeight * 2 /3);
window.draw(wholeAudioCursor);
std::list<int>::iterator it = beatsTime.begin(); // draw beats
int count = 0;
int currentBeatIndex = 0;
while (it != beatsTime.end())
{
count++;
// draw the bottom beats (the entire audio)
RectangleShape beat;
beat.setSize(Vector2f(4, sliderHeight / 2));
beat.setFillColor(Color::Red);
beat.setPosition(24 - 2 + sliderLength * (*it) / musicDurationMS, 1152 - sliderHeight / 2);
window.draw(beat);
// draw the top beats (the closeup audio)
if (cursorRelToMusicMS >= *it - 2500 && cursorRelToMusicMS <= *it + 2500)
{
RectangleShape beatInPartSlider;
beatInPartSlider.setSize(Vector2f(10, sliderHeight * 2 / 3));
beatInPartSlider.setFillColor(Color::Red);
beatInPartSlider.setPosition(24 - 10 / 2 + sliderLength / 2 - sliderLength / 2 * (cursorRelToMusicMS - *it) / 2500, sliderHeight / 3);
window.draw(beatInPartSlider);
}
if (cursorRelToMusicMS >= *it)
{
currentBeatIndex = count;
}
it++;
}
text.setFillColor(Color(255, 255, 255, 50));
text.setCharacterSize(100);
text.setString("Preview");
text.setPosition(100 + (boardSquareSize*boardWidth)/2 - text.getLocalBounds().width/2, 1152 / 2 - (boardSquareSize * boardHeight) / 2 + 2* boardSquareSize);
window.draw(text);
simulatorBoard.setBoard(infBoardSimulation.at(currentBeatIndex % infBoardSimulation.size()));
Board simulatorBoard = Board(100, 1152 / 2 - (boardSquareSize * boardHeight)/2);
// render major/minor notes
// major notes occur every 4 minor notes
for (int i = std::ceil((float)(cursorRelToMusicMS - 2500) / ((float)mspb / divider)); i <= std::floor((float)(cursorRelToMusicMS + 2500) / ((float)mspb / divider)); i++)
{
RectangleShape beatInPartSlider;
beatInPartSlider.setSize(Vector2f(4, sliderHeight / 4));
beatInPartSlider.setFillColor(i % divider == 0 ? Color::White : Color(0, 159, 180));
int t = i * (float)mspb / divider;
beatInPartSlider.setPosition(24 - 2 + sliderLength / 2 - sliderLength / 2 * (cursorRelToMusicMS - t) / 2500, sliderHeight * 3 / 8);
window.draw(beatInPartSlider);
}
//window.draw(assetManager->getDrawable("beat button"));
// draw audio timestamp
text.setFillColor(Color::White);
Int32 tleft = sound.getPlayingOffset().asMilliseconds();
Int32 tTotal = sound.getBuffer()->getDuration().asMilliseconds();
String tLeftString = getPaddingString(std::to_string(tleft / 1000 / 60), 2, '0', false) + ":"
+ getPaddingString(to_string(tleft / 1000 % 60), 2, ' 0', false) + ":"
+ getPaddingString(to_string(tleft % 1000), 3, '0', false);
String tTotalString = getPaddingString(to_string(tTotal / 1000 / 60), 2, '0', false) + ":"
+ getPaddingString(to_string(tTotal / 1000 % 60), 2, ' 0', false) + ":"
+ getPaddingString(to_string(tTotal % 1000), 3, '0', false);
text.setCharacterSize(35);
text.setString(tLeftString);
text.setPosition(100, 1152 - sliderHeight - 40);
window.draw(text);
text.setString("/");
text.setPosition(290, 1152 - sliderHeight - 40);
window.draw(text);
text.setPosition(330, 1152 - sliderHeight - 40);
text.setString(tTotalString);
window.draw(text);
if (mouseInBox(window, 20, 20, 40, 40)) // back button
{
window.draw(assetManager->getDrawable("back button hl"));
}
else
{
window.draw(assetManager->getDrawable("back button"));
}
}
void BeatMapEditor::keyEvent(const float & dt, sf::Event event)
{
using namespace sf;
if (event.type != Event::KeyPressed) return;
Keyboard::Key key = event.key.code;
switch (key)
{
case Keyboard::Key::Escape:
save();
sound.stop();
stateManager.addState(std::unique_ptr<StateScreen>(new Menu(stateManager)));
break;
case Keyboard::Key::Num2:
sound.setPitch(0.25);
break;
case Keyboard::Key::Num5:
sound.setPitch(0.5);
break;
case Keyboard::Key::Num1:
sound.setPitch(1);
break;
case Keyboard::Key::Space:
if (sound.getPlayingOffset().asMilliseconds() == musicDurationMS)
{
//basically restart if at the end
sound.setPlayingOffset(milliseconds(0));
sound.play();
}
else if (sound.getStatus() != Music::Status::Playing)
{
sound.play();
}
else
{
sound.pause();
}
break;
case Keyboard::Key::B:
addCursorToBeatList();
break;
}
}
void BeatMapEditor::addCursorToBeatList()
{
int nearestBeat = std::round((float)(cursorRelToMusicMS) / ((float)mspb / divider));
nearestBeat = nearestBeat * ((float)mspb / divider);
std::list<int>::iterator temp = beatsTime.begin();
int prev = 0;
while (temp != beatsTime.end() && *temp < nearestBeat)
{
prev = *temp;
temp++;
}
if (temp == beatsTime.end())
{
beatsTime.push_back(nearestBeat);
return;
}
// reject beat that already exists
if (nearestBeat != *temp)
{
beatsTime.insert(temp, nearestBeat);
}
}
void BeatMapEditor::mouseEvent(const float & dt, sf::RenderWindow& window, sf::Event event)
{
using namespace sf;
using namespace std;
if (event.type == Event::MouseButtonReleased && event.mouseButton.button == Mouse::Left)
{
cursorSelected = false;
}
else if (event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left)
{
if (beatButton.mouseInButton(window))
{
addCursorToBeatList();
}
else if (speedButton025->mouseInButton(window))
{
sound.setPitch(0.25);
}
else if (speedButton050->mouseInButton(window))
{
sound.setPitch(0.5);
}
else if (speedButton100->mouseInButton(window))
{
sound.setPitch(1);
}
else if (dividerButton1->mouseInButton(window))
{
divider = 1;
}
else if (dividerButton12->mouseInButton(window))
{
divider = 2;
}
else if (dividerButton13->mouseInButton(window))
{
divider = 3;
}
else if (dividerButton14->mouseInButton(window))
{
divider = 4;
}
else if (mouseInBox(window, 50, 1152 - sliderHeight - 40, 30, 40))
{
if (sound.getPlayingOffset().asMilliseconds() == musicDurationMS)
{
//basically restart if at the end
sound.setPlayingOffset(milliseconds(0));
sound.play();
}
else if (sound.getStatus() != Music::Status::Playing)
{
sound.play();
}
else
{
sound.pause();
}
}
else if (mouseInBox(window, 20, 20, 40, 40)) // back button
{
save();
sound.stop();
stateManager.addState(std::unique_ptr<StateScreen>(new Menu(stateManager)));
return;
}
}
else if (Mouse::isButtonPressed(Mouse::Right))
{
list<int>::iterator it = beatsTime.begin();
while (it != beatsTime.end())
{
// for the part slider only
if (cursorRelToMusicMS >= *it - 2500 && cursorRelToMusicMS <= *it + 2500)
{
// erase beat that mouse if over when right mouse is pressed
if (mouseInBox(window, 24 - 2 + sliderLength / 2 - sliderLength / 2 * (cursorRelToMusicMS - *it) / 2500, sliderHeight / 2, 10, sliderHeight / 2))
{
beatsTime.erase(it);
break;
}
}
it++;
}
}
Vector2i pixelPos = Mouse::getPosition(window);
Vector2f mouseViewPos = window.mapPixelToCoords(pixelPos);
if (event.type == Event::MouseButtonPressed && event.mouseButton.button == Mouse::Left && mouseInBox(window, 0, 1152 - sliderHeight, sliderLength, sliderHeight))
{
cursorSelected = true;
cursorRelToMusicMS = (mouseViewPos.x - 24 + 5) * musicDurationMS / sliderLength;
cursorRelToMusicMS = clamp(cursorRelToMusicMS, 0, musicDurationMS);
sound.setPlayingOffset(milliseconds(cursorRelToMusicMS));
}
if (Mouse::isButtonPressed(Mouse::Left) && cursorSelected)
{
cursorRelToMusicMS = (mouseViewPos.x - 24 + 5) * musicDurationMS / sliderLength;
cursorRelToMusicMS = clamp(cursorRelToMusicMS, 0, musicDurationMS);
sound.setPlayingOffset(milliseconds(cursorRelToMusicMS));
}
}