forked from Flyburg/MyMUG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewModel.hpp
More file actions
241 lines (207 loc) · 6.36 KB
/
Copy pathViewModel.hpp
File metadata and controls
241 lines (207 loc) · 6.36 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
#ifndef VIEWMODEL_HPP
#define VIEWMODEL_HPP
#include <QObject>
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include <utility>
#include <vector>
#include <memory>
#include <string>
#include <windows.h>
#include "Song.hpp"
#include "Note.hpp"
#include "../Common/Key.hpp"
#include "../Common/NoteInfo.h"
#include "PlayThread.h"
#include "MusicThread.h"
#include <filesystem>
// 全局常量
const int LENGTH = 1440;
const int HEIGHT = 900;
const int LINE = 750;
const int LEFTEST_TRACK = 480;
const int WIDTH = 120;
const int NOTE_HEIGHT = 60;
const int TIME_INTERVAL = 16;
const int SPEED = 4;
const int PERFECT_TIME = 80;
const int GOOD_TIME = 120;
class ViewModel : public QObject{
Q_OBJECT
public:
// explicit ViewModel(QObject *parent = nullptr);
ViewModel()
: point(0),
comb(0),
gameStartTime(std::chrono::steady_clock::now()),
activeNotesPtr(nullptr),
pointPtr(nullptr),
titlePtr(nullptr),
pngPathPtr(nullptr),
song(Song("", "", 0, std::vector<Note>(), 0)),
maxCombo(0),
perfect(0),
good(0),
miss(0),
accuracy(0.0),
grade("")
{}
~ViewModel() override {
if (musicThread && musicThread->isRunning()) {
musicThread->quit();
musicThread->wait();
}
if (playThread && playThread->isRunning()) {
playThread->quit();
playThread->wait();
}
}
std::vector<QString> select_songs() {
std::vector<QString> filenames;
for (const auto& entry : std::filesystem::directory_iterator("../resources/charts")) {
if (entry.is_regular_file()) { // 不含后缀名
filenames.push_back(QString::fromStdString(entry.path().stem().string()));
}
}
return filenames;
}
void initialize(std::string songTitle) {
title = std::move(songTitle);
std::string songFile;
songFile = "../resources/charts/" + title + ".json";
pngPath = QString::fromStdString("../resources/covers/" + title + ".png");
musicPath = QString::fromStdString("../resources/music/" + title + ".wav");
emit createBackground(pngPath);
emit createTracks();
emit createJudgementLine();
emit createTitleBlock(QString::fromStdString(title));
std::ifstream file(songFile);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file");
}
song = Song(file);
notes = song.getNotes();
if (notes.empty()) {
throw std::runtime_error("No notes in the song");
}
gameStartTime = std::chrono::steady_clock::now();
keys[0] = Key('d', false, gameStartTime, 0, 0);
keys[1] = Key('f', false, gameStartTime, 0, 0);
keys[2] = Key('j', false, gameStartTime, 0, 0);
keys[3] = Key('k', false, gameStartTime, 0, 0);
activeNotesPtr = &activeNotes;
pointPtr = &point;
titlePtr = &title;
}
void play() {
std::string songPath = "../resources/music/" + title + ".wav";
musicThread = new MusicThread;
musicThread->setPath(songPath);
playThread = std::make_unique<PlayThread>(this);
musicThread->start();
playThread->start();
}
std::vector<NoteInfo>* getActiveNotes() const { return activeNotesPtr; }
int* getPoint() const { return pointPtr; }
std::string* getTitle() const { return titlePtr; }
std::string* getPngPath() const { return pngPathPtr; }
signals:
void updateNoteView();
void updateScore(int newScore);
void updateCombo(int newCombo);
void createBackground(QString pngPath);
void createTitleBlock(QString title);
void createTracks();
void createJudgementLine();
void playtapsound();
void showSettlement(QString title,int point,QString grade,double accuracy,int maxComb,int perfect,int good,int miss);
public slots:
void startplay(std::string song) {
initialize(song);
play();
}
void dIsPressed() {
// std::cout << "dIsPressed" << std::endl;
keyFromView[0] = true;
}
void fIsPressed() {
keyFromView[1] = true;
}
void jIsPressed() {
keyFromView[2] = true;
}
void kIsPressed() {
keyFromView[3] = true;
}
void dIsReleased() {
keyFromView[0] = false;
}
void fIsReleased() {
keyFromView[1] = false;
}
void jIsReleased() {
keyFromView[2] = false;
}
void kIsReleased() {
keyFromView[3] = false;
}
public:
void updateNotes(std::vector<Note>::iterator& it) {
auto currentTime = std::chrono::steady_clock::now();
auto timePassed = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - gameStartTime).count();
activeNotes.clear();
for (auto i = it; i != notes.end(); ++i) {
int x = (i->getTrack()-1) * WIDTH + LEFTEST_TRACK;
int y = SPEED * timePassed / 5 + LINE - i->getTimestamp() * SPEED / 5;
if (y > HEIGHT) {
it = i + 1;
continue;
}
if (y < 0) {
break;
}
NoteInfo noteInfo = {x, y, i->visible};
activeNotes.push_back(noteInfo);
}
}
int updateKeyStates() {
int result = 4;
bool result1 = false;
for (int j = 0; j < 4; ++j) {
result1 = keys[j].updateState(keyFromView[j]);
// std::cout << "result1: " << result1 << std::endl;
if (result1 && result == 4) {
result = j;
break;
}
}
return result;
}
int point;
int comb;
Song song;
bool keyFromView[4] = {false, false, false, false};
std::vector<Note> notes;
std::chrono::time_point<std::chrono::steady_clock> gameStartTime;
Key keys[4];
std::vector<NoteInfo> activeNotes;
std::vector<NoteInfo>* activeNotesPtr;
int* pointPtr;
std::string title;
std::string* titlePtr;
QString pngPath;
QString musicPath;
std::string* pngPathPtr;
//varialbes for Settlement Interface
int maxCombo;
int perfect;
int good;
int miss;
double accuracy;
QString grade;
MusicThread* musicThread;
std::unique_ptr<PlayThread> playThread;
};
#endif // VIEWMODEL_HPP