-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDPAT.h
More file actions
418 lines (294 loc) · 8.67 KB
/
CDPAT.h
File metadata and controls
418 lines (294 loc) · 8.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
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
#pragma once
#include <vector>
#include <map>
#include <variant>
#include <array>
#include <fstream>
#include <string>
#include <iostream>
#include <deque>
#include <memory>
namespace cdpat {
class Pattern;
static inline const int CDPAT_VERSION = 150;
static inline size_t MAX_ACTION_HISTORY = 250;
const std::array<std::string, 4> BEAT_EVENT_TYPES { "note", "hit", "dialog", "target"};
typedef std::variant<std::string, int, float> BeatEventArg;
struct BeatEvent {
std::string type;
std::vector<BeatEventArg> args;
};
typedef std::map<float, std::vector<BeatEvent>> EventsData;
class Action {
public:
virtual ~Action() = default;
virtual void apply(Pattern& pattern) = 0;
virtual void undo(Pattern& pattern) = 0;
virtual std::string getDescription() const = 0;
protected:
static EventsData& getEventsData(Pattern& pattern);
};
class LongAction : public Action {
public:
virtual ~LongAction() = default;
virtual void update(Pattern& pattern) = 0;
};
struct NoteRef {
float beat;
int lane;
};
class Pattern {
EventsData events;
std::deque<std::unique_ptr<Action>> actions_stack;
long action_index = -1;
std::string name = "";
mutable bool unsaved_changes = false;
std::string getPatternPath() const {
return res_path + "/patterns/" + name + ".cdpat";
}
std::vector<BeatEvent>::iterator findNote(float beat, int lane) {
for (size_t i = 0; i < events[beat].size(); i++) {
if (events[beat][i].type == "note" && std::get<int>(events[beat][i].args[0]) == lane) {
return events[beat].begin() + i;
}
}
return events[beat].end();
}
void reset() {
events.clear();
actions_stack.clear();
action_index = -1;
name = "";
unsaved_changes = false;
}
bool loadFromFile(const std::string& filepath) {
reset();
std::cout << "Loading from " << filepath << "...\nres:// folder: " << res_path << "\n";
std::ifstream ifs(filepath);
if (!ifs)
return false;
std::string first_header;
ifs >> first_header; // #cdpat
if (first_header != "#cdpat") {
std::cerr << "ERROR: Invalid CDPAT header.";
return false;
}
ifs >> first_header; //Version
if (std::stoi(first_header) > CDPAT_VERSION) {
std::cerr << "ERROR: CDPAT version unsupported.";
return false;
}
// Garbage
std::getline(ifs, first_header);
while (ifs) {
std::string text;
std::getline(ifs, text);
if (text.empty())
break;
// Split by space
std::vector<std::string> items;
size_t pos;
while ((pos = text.find(' ')) != std::string::npos) {
items.push_back(text.substr(0, pos));
text.erase(0, pos + 1);
}
items.push_back(text);
// Content reading
if (items[0][0] == '#') {
// Header
const auto& header = items[0];
const auto& arg = items[1];
if (header == "#bpm")
bpm = std::stof(arg);
else if (header == "#song")
song_name = arg;
else if (header == "#sig")
sig = std::stoi(arg);
}
else {
//Event reading
float beat = std::stof(items[0]);
BeatEvent event;
// Find event type in list of known types
if (std::find(BEAT_EVENT_TYPES.begin(), BEAT_EVENT_TYPES.end(), items[1]) != BEAT_EVENT_TYPES.end()) {
event.type = items[1];
if (items.size() > 2)
for (unsigned i = 2; i < items.size(); i++) {
if (items[i].empty()) continue;
// Identify argument type
bool isNumber = true;
bool isFloat = false;
for (const auto& c : items[i]) {
if (c != '.' && (c < '0' || c > '9'))
isNumber = false;
if (c == '.' && !isNumber) {
if (isFloat) { //More than one period, can't be a number
isFloat = false;
isNumber = false;
}
else
isFloat = true;
}
}
// Add argument to vector
if (isNumber) {
if (isFloat)
event.args.emplace_back(std::stof(items[i]));
else
event.args.emplace_back(std::stoi(items[i]));
}
else {
event.args.emplace_back(items[i]);
}
}
}
else {
// Not a known event type
std::cerr << "ERROR: Unrecognized event type '" << items[1] << "'\n";
return false;
}
events[beat].push_back(event);
}
}
return true;
}
public:
bool hasUnsavedChanges() const {
return unsaved_changes;
}
static inline std::string res_path = "";
const std::string& getPatternName() const {
return name;
}
void setPatternName(const std::string& name) {
this->name = name;
}
const EventsData& getEvents() const {
return events;
}
const std::deque<std::unique_ptr<Action>>& getActionsStack() const {
return actions_stack;
}
long getActionIndex() const {
return action_index;
}
bool canUndo() const {
return action_index > -1;
}
bool canRedo() const {
return action_index + 1 < actions_stack.size();
}
bool setActionIndex(long i) {
if (i > action_index) {
while (i > action_index) {
if (canRedo())
redo();
else
return false;
}
} else if (i < action_index) {
while (i < action_index) {
if (canUndo())
undo();
else
return false;
}
}
return true;
}
std::string song_name = "";
float bpm = 120.f;
int sig = 4;
template<typename ActionType, typename... Args>
void applyAction(Args&&... args) {
std::unique_ptr<Action> action = std::make_unique<ActionType>(std::forward<Args>(args)...);
unsaved_changes = true;
if (action_index + 1 < actions_stack.size())
actions_stack.resize(action_index + 1);
while (actions_stack.size() > MAX_ACTION_HISTORY) {
actions_stack.pop_front();
action_index -= 1;
}
action->apply(*this);
std::cout << "Apply action: " << action->getDescription() << "\n";
actions_stack.emplace_back(std::move(action));
action_index++;
}
/*template<typename T>
T* initLongAction(T&& action) {
unsaved_changes = true;
long_action_process = std::make_unique<T>(action);
return dynamic_cast<T*>(long_action_process.get());
}
void processLongAction() {
if (long_action_process)
long_action_process->update(*this);
}
void finalizeLongAction(std::unique_ptr<LongAction>&& action) {
if (action != long_action_process) throw std::runtime_error("Finalized action not currently being processed.");
applyAction(std::move(action));
long_action_process = nullptr;
}*/
bool undo() {
if (actions_stack.empty()) return false;
unsaved_changes = true;
actions_stack[action_index]->undo(*this);
std::cout << "Undo action: " << actions_stack[action_index]->getDescription() << "\n";
action_index--;
return true;
}
bool redo() {
unsaved_changes = true;
if (action_index + 1 == actions_stack.size())
return false;
actions_stack[++action_index]->apply(*this);
std::cout << "Redo action: " << actions_stack[action_index]->getDescription() << "\n";
return true;
}
bool load(const std::string& pattern_name) {
if (res_path.empty())
return false;
if (loadFromFile(res_path + "patterns/" + pattern_name + ".cdpat")) {
name = pattern_name;
return true;
}
return false;
}
bool saveToFile() const {
std::ofstream ofs(getPatternPath());
if (!ofs)
return false;
ofs << "#cdpat " << CDPAT_VERSION << "\n";
ofs << "#song " << song_name << "\n";
//Deprecated, now stored in RhythmStream resource
//ofs << "#bpm " << bpm << "\n";
//ofs << "#sig " << sig << "\n";
// Save events
for (const auto& [beat, events] : events) {
for (const auto& event: events) {
ofs << beat << " " << event.type << " ";
for (size_t i = 0; i < event.args.size(); i++) {
const auto& arg = event.args[i];
switch (arg.index()) {
case 0: //string
ofs << std::get<std::string>(arg);
break;
case 1: //int
ofs << std::get<int>(arg);
break;
case 2: //float
ofs << std::get<float>(arg);
break;
}
if (i < event.args.size() - 1)
ofs << " ";
}
ofs << "\n";
}
}
unsaved_changes = false;
return true;
}
friend class Action;
};
}