-
Notifications
You must be signed in to change notification settings - Fork 4
/
displayarea.cpp
305 lines (262 loc) · 10.5 KB
/
displayarea.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
#include "displayarea.h"
#include <QPainter>
#include <QPainterPath>
#include <QPaintEvent>
#include <QTimer>
#include <QDebug>
#include <QMessageBox>
DisplayArea::DisplayArea(QWidget *parent) : QWidget(parent)
{
setAttribute(Qt::WA_StaticContents);
QLinearGradient gradient(QPointF(50, -20), QPointF(80, 20));
gradient.setColorAt(0.0, Qt::white);
gradient.setColorAt(1.0, QColor(0xa6, 0xce, 0x39));
backgroundColor = QColor(40, 40, 40);
background = QBrush(backgroundColor);
midiNoteColor = QColor(0x31, 0x8C, 0xE7);
keyboardNoteColor = QColor(0x79, 0x1C, 0xF8);
for (int i = 0; i < 9 ; i++ ) trackColors << QColor(0x31, 0x8C, 0xE7) ;
minNote = 21;
maxNote = 108;
whiteKeySize = 17;
blackKeySize = 11;
songTime = -2; //in sec
songTimeShowed = 5; //in sec
selectedTrack = 0;
isIntervalOn = false;
intInitTime = -1;
intFinalTime = -1;
}
//compute drawing parameters for new file then call update which will call paintEvent.
bool DisplayArea::createInitialDisplay(smf::MidiFile *midiFile)
{
this->midifile = midiFile;
// compute (once) all parameters that depends only on the midifile
//find highest and lowest notes of the piano
//Note careful : works only for single track midi file
int highestNote = 21; //lowest possible note on a keyboard : C2 = 21 in MIDI
int lowestNote = 108; //highest possible note on a keyboard : C8 = 108 in MIDI
for (int i = 0; i < (*midifile).getTrackCount();i++ )
{
for (int j = 0; j < (*midifile)[i].getEventCount();j++ )
{
if ((*midifile)[i][j].isNoteOn())
{
int currNote = (*midifile)[i][j].getP1() + transpose;
if (currNote > highestNote) highestNote = currNote;
if (currNote < lowestNote ) lowestNote = currNote;
}
}
}
//minNote and maxNote are defined as the first white keys outside the range of play
this->minNote = std::max(lowestNote - 1,21);
if (isBlackKey(this->minNote)) this->minNote -=1;
this->maxNote = std::min(highestNote +1,108);
if (isBlackKey(this->maxNote)) this->maxNote +=1;
this->setOneLoopDisplayParameters();
this->update(); //update the painting.
return true;
}
void DisplayArea::setOneLoopDisplayParameters()
{
//reinitialize things that need to be reinitialized at each loop :
midiCurrentNotes.clear(); // clear this vector that holds data from the previous midifile
//Get the notes and keys sizes.
int whiteKeyNumber = 0;
for (int i = this->minNote; i <= this->maxNote; i++)
{
if (!isBlackKey(i)) whiteKeyNumber++;
}
int w = this->width();
whiteKeySize = std::max(w/whiteKeyNumber,1);
blackKeySize = whiteKeySize * 1.5/2.3; //size ratio from my keyboard
return;
}
bool DisplayArea::isBlackKey(int keyId)
{
int id = keyId%12;
if(id == 10 || id == 1 || id == 3 || id == 6 || id == 8 ) return true;
return false;
}
void DisplayArea::paintPiano(QPainter *painter)
{
int h = this->height();
int w = this->width();
int ph = getPianoHeight();
int h0 = h - ph;
//draw black background
painter->fillRect(0,h0, w, ph, Qt::black);
//draw red line above the piano
int redLineHeight = std::max(2,ph/50);
painter->fillRect(0,h0, w, redLineHeight, Qt::red);
int h0rl = h0 + redLineHeight;
int keyBorderWidth = std::min(1,whiteKeySize/10);
// draw all the white keys
for(int i = keyBorderWidth; i < w ; i += whiteKeySize )
{
painter->fillRect(i,h0rl, whiteKeySize-keyBorderWidth, h - h0rl, Qt::white);
}
//paint MIDIWhitekeyPressed if there are some.
if ( midiCurrentNotes.size() != 0 )
{
for (int i = 0; i <midiCurrentNotes.size(); i++)
{
int currentNote = midiCurrentNotes.at(i);
if (!isBlackKey(currentNote))
{
QColor keyColor = midiNoteColor;
keyColor.setAlpha(170);
painter->fillRect(getNoteX0(currentNote), h0rl, whiteKeySize, h - h0rl, QBrush(keyColor));
}
}
}
//paint PIANO WhitekeyPressed if there are some.
std::set<int>::iterator iter;
for(iter=currPressedNotes.begin(); iter!=currPressedNotes.end();++iter){
if (!isBlackKey(*iter))
{
QColor keyColor = keyboardNoteColor;
keyColor.setAlpha(220);
painter->fillRect(getNoteX0(*iter), h0rl, whiteKeySize, h - h0rl, QBrush(keyColor));
}
}
//Draw the black keys above the white ones
for (int i = this->minNote; i <= this->maxNote; i++)
{
if (isBlackKey(i))
{
painter->fillRect(getNoteX0(i),h0rl, blackKeySize, (h - h0rl)*0.65, Qt::black);
}
}
//paint the midiBlackKeyPressed if there are some.
if ( midiCurrentNotes.size() != 0 )
{
for (int i = 0; i <midiCurrentNotes.size(); i++)
{
int currentNote = midiCurrentNotes.at(i);
if (isBlackKey(currentNote))
{
QColor keyColor = midiNoteColor;
keyColor.setAlpha(170);
painter->fillRect(getNoteX0(currentNote),h0rl, blackKeySize, (h - h0rl)*0.65, QBrush(keyColor));
}
}
}
//and finally paint the keyboard back key pressed
for(iter=currPressedNotes.begin(); iter!=currPressedNotes.end();++iter){
if (isBlackKey(*iter))
{
QColor keyColor = keyboardNoteColor;
keyColor.setAlpha(220);
painter->fillRect(getNoteX0(*iter), h0rl, blackKeySize, (h - h0rl)*0.65, QBrush(keyColor));
}
}
return;
}
void DisplayArea::paintNotes(QPainter *painter)
{
// update the curent keypressed
midiCurrentNotes.clear();
// paint the keys.
float maxTime = songTime + songTimeShowed;
for (int i = 0; i < midifile->getTrackCount(); i++ )
{
for (int j = 0; j < (*midifile)[i].getEventCount(); j++ )
{
if ((*midifile)[i][j].isNoteOn())
{
int currNote = (*midifile)[i][j].getP1() + transpose;
float startNoteTime = (*midifile)[i][j].seconds;
float endNoteTime = (*midifile)[i][j].seconds + (*midifile)[i][j].getDurationInSeconds();
if (startNoteTime > maxTime + 0.05) continue;
if (endNoteTime < songTime - 0.05 ) continue;
int noteSize = isBlackKey(currNote) ? blackKeySize : whiteKeySize;
QColor thismidiNoteColor = isBlackKey(currNote) ? trackColors.at(i).darker() : this->trackColors.at(i);
if (selectedTrack != 0 && i != selectedTrack - 1) thismidiNoteColor = Qt::gray;
if (isIntervalOn && (startNoteTime < intInitTime || startNoteTime > intFinalTime)) thismidiNoteColor = Qt::gray;
QPainterPath path;
// -.5 below improve the quality of the borders, see https://stackoverflow.com/questions/29196610/qt-drawing-a-filled-rounded-rectangle-with-border#29196812
int visualOffset = -2; // With a visual small offset, the sound and the visual seems to be better synchronized.
path.addRoundedRect(QRectF(getNoteX0(currNote)-0.5, getHeightForTime(endNoteTime)-0.5+visualOffset, noteSize-1, getHeightForTime(startNoteTime)-getHeightForTime(endNoteTime)-1+visualOffset), 4, 4);
QPen pen(Qt::black, 2);
painter->setPen(pen);
painter->fillPath(path, thismidiNoteColor);
painter->drawPath(path);
//painter->fillRect(getNoteX0(currNote),getHeightForTime(endNoteTime), noteSize-1, getHeightForTime(startNoteTime)-getHeightForTime(endNoteTime) -1 , midiNoteColor);
if (startNoteTime < songTime && endNoteTime > songTime ) midiCurrentNotes.push_back(currNote);
}
}
}
}
int DisplayArea::getHeightForTime(float t)
{
float h = getNoteAreaHeight();
float timeAtH = songTime;
float timeAt0 = songTime + songTimeShowed;
return (timeAt0 - t) * h / (timeAt0 - timeAtH);
}
int DisplayArea::getNoteX0(int note)
{
int numberOfWhiteNotesB4Note = 0; //not counting current note.
for (int i = minNote; i < note; i++)
{
if (!isBlackKey(i)) numberOfWhiteNotesB4Note ++;
}
if (!isBlackKey(note))
{
return numberOfWhiteNotesB4Note*whiteKeySize;
}
else if (note%12 == 8) //centered black key between 2 white
{
return numberOfWhiteNotesB4Note*whiteKeySize-blackKeySize/2;
}
else if (note%12 == 1 || note%12 == 6) //black keys more on the left white key
{
return numberOfWhiteNotesB4Note*whiteKeySize-blackKeySize/4*3;
}
else //black keys more on the right white key
{
return numberOfWhiteNotesB4Note*whiteKeySize-blackKeySize/4;
}
return -1;
}
int DisplayArea::getNoteAreaHeight()
{
return this->height() - getPianoHeight();
}
int DisplayArea::getPianoHeight()
{
return std::max(100,this->height()/4);
}
void DisplayArea::paintEvent(QPaintEvent *event)
{
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
//qDebug() << "why does a Qdebug here make it work ???? ";
painter.fillRect(event->rect(), backgroundColor); //create the uniform colored background
this->setOneLoopDisplayParameters(); //(mainly) update key sizes depending on window width
if (isMidiSelectedFileValid) this->paintNotes(&painter); //add all the notes if there are some
this->paintPiano(&painter); //add the keyboard above the notes (to have proper boarders, the notes goes "under" the keyboard)
painter.end();
}
void DisplayArea::getParamsFromManager(GameManager *manager){
isMidiSelectedFileValid = manager->isMidiSelectedFileValid;
songTime = manager->songTime;
selectedTrack = manager->selectedTrack;
isIntervalOn = manager->isIntervalOn;
intInitTime = manager->intInitTime;
intFinalTime = manager->intFinalTime;
currPressedNotes = manager->currPressedNotes;
transpose = manager->transpose;
backgroundColor = manager->options.backgroundColor;
if (manager->options.MIDINoteColors.size() > 0 ) midiNoteColor = manager->options.MIDINoteColors.at(0); //TODO
else midiNoteColor = QColor(0x31, 0x8C, 0xE7); //TODO
keyboardNoteColor = manager->options.keyboardNoteColor;
if (manager->options.MIDINoteColors.size() > 0 ) trackColors = manager->options.MIDINoteColors;
if (manager->isMidiSelectedFileValid) createInitialDisplay(&manager->midifile);
}
void DisplayArea::animate()
{
update(); //QT created slot that call paintEvent.
}