Skip to content

Commit 8053ed1

Browse files
Add editor "add note" mouse event
1 parent d8e611d commit 8053ed1

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

src/ui/component/editor/MIDIContentViewer.cpp

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,11 +361,48 @@ void MIDIContentViewer::paintOverChildren(juce::Graphics& g) {
361361
}
362362

363363
void MIDIContentViewer::mouseDown(const juce::MouseEvent& event) {
364-
if (event.mods.isLeftButtonDown() && event.mods.isAltDown()) {
364+
if (event.mods.isLeftButtonDown()) {
365365
/** Move View Area */
366-
this->viewMoving = true;
367-
this->setMouseCursor(juce::MouseCursor::DraggingHandCursor);
368-
this->dragStartFunc();
366+
if (event.mods.isAltDown()) {
367+
this->viewMoving = true;
368+
this->setMouseCursor(juce::MouseCursor::DraggingHandCursor);
369+
this->dragStartFunc();
370+
}
371+
372+
/** Edit Note */
373+
else if (Tools::getInstance()->getType() == Tools::Type::Pencil) {
374+
auto& pos = event.position;
375+
376+
auto [type, index] = this->getNoteController(pos);
377+
378+
/** Add Note */
379+
if (type == NoteControllerType::None) {
380+
/** Get Time */
381+
double time = this->secStart + (pos.x / this->getWidth()) * (this->secEnd - this->secStart);
382+
time = quickAPI::limitTimeSec(time, Tools::getInstance()->getAdsorb());
383+
384+
/** Get Pitch */
385+
uint8_t pitch = (uint8_t)std::floor(this->keyTop - (pos.y / this->getHeight()) * (this->keyTop - this->keyBottom));
386+
387+
/** Get Length */
388+
double length = Tools::getInstance()->getLastNoteLength();
389+
if (length <= 0) {
390+
/** Init Length */
391+
int tempoIndex = quickAPI::getTempoTempIndexBySec(time);
392+
auto tempo = quickAPI::getTempoData(tempoIndex);
393+
length = std::get<3>(tempo);
394+
395+
Tools::getInstance()->setLastNoteLength(length);
396+
}
397+
398+
/** Set Temp */
399+
this->noteInsertTime = time;
400+
this->noteInsertLength = length;
401+
this->noteInsertPitch = pitch;
402+
this->noteInsertChannel = Tools::getInstance()->getMIDIChannel();
403+
this->repaint();
404+
}
405+
}
369406
}
370407
}
371408

@@ -377,6 +414,21 @@ void MIDIContentViewer::mouseUp(const juce::MouseEvent& event) {
377414
this->setMouseCursor(juce::MouseCursor::NormalCursor);
378415
this->dragEndFunc();
379416
}
417+
418+
/** Add Note */
419+
if (this->noteInsertTime >= 0) {
420+
/** Insert Note */
421+
this->insertNote(
422+
this->noteInsertTime, this->noteInsertLength,
423+
this->noteInsertPitch, this->noteInsertChannel);
424+
425+
/** Reset Temp */
426+
this->noteInsertTime = -1;
427+
this->noteInsertLength = -1;
428+
this->noteInsertPitch = 0;
429+
this->noteInsertChannel = 0;
430+
this->repaint();
431+
}
380432
}
381433
}
382434

@@ -415,6 +467,22 @@ void MIDIContentViewer::mouseDrag(const juce::MouseEvent& event) {
415467
int distanceY = event.getDistanceFromDragStartY();
416468
this->dragProcessFunc(distanceX, distanceY, true, true);
417469
}
470+
471+
/** Add Note */
472+
auto& pos = event.position;
473+
if (this->noteInsertTime >= 0) {
474+
/** Get Time */
475+
double time = this->secStart + (pos.x / this->getWidth()) * (this->secEnd - this->secStart);
476+
time = quickAPI::limitTimeSec(time, Tools::getInstance()->getAdsorb());
477+
478+
/** Get Pitch */
479+
uint8_t pitch = (uint8_t)std::floor(this->keyTop - (pos.y / this->getHeight()) * (this->keyTop - this->keyBottom));
480+
481+
/** Set Temp */
482+
this->noteInsertTime = time;
483+
this->noteInsertPitch = pitch;
484+
this->repaint();
485+
}
418486
}
419487
}
420488

@@ -428,6 +496,16 @@ void MIDIContentViewer::mouseExit(const juce::MouseEvent& event) {
428496
this->setMouseCursor(juce::MouseCursor::NormalCursor);
429497
this->dragEndFunc();
430498
}
499+
500+
/** Add Note */
501+
if (this->noteInsertTime >= 0) {
502+
/** Reset Temp */
503+
this->noteInsertTime = -1;
504+
this->noteInsertLength = -1;
505+
this->noteInsertPitch = 0;
506+
this->noteInsertChannel = 0;
507+
this->repaint();
508+
}
431509
}
432510

433511
void MIDIContentViewer::mouseWheelMove(
@@ -450,6 +528,12 @@ void MIDIContentViewer::midiChannelChanged() {
450528
this->repaint();
451529
}
452530

531+
void MIDIContentViewer::insertNote(
532+
double startTime, double length,
533+
uint8_t pitch, uint8_t channel) {
534+
/** TODO */
535+
}
536+
453537
void MIDIContentViewer::updateKeyImageTemp() {
454538
/** Clear Temp */
455539
juce::Graphics g(*(this->keyTemp.get()));

src/ui/component/editor/MIDIContentViewer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ class MIDIContentViewer final
121121

122122
std::unique_ptr<juce::ChangeListener> midiChannelListener = nullptr;
123123

124+
void insertNote(double startTime, double length, uint8_t pitch, uint8_t channel);
125+
124126
void updateKeyImageTemp();
125127
void updateRulerImageTemp();
126128
void updateBlockImageTemp();

0 commit comments

Comments
 (0)