Skip to content

Commit 71b8bf2

Browse files
committed
Now when importing there is only one state saved (for undo/redo)
1 parent 63331d9 commit 71b8bf2

3 files changed

Lines changed: 41 additions & 26 deletions

File tree

plugin/include/XenRoll/editor/panels/MainPanel.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class MainPanel : public juce::Component, public juce::KeyListener {
102102
*/
103103
void reattachRatiosMarks(int dcents = 0);
104104
void updateRatiosMarks();
105-
void numBarsChanged();
105+
///< For calls from PluginEditor. manualChange means not due to import.
106+
void numBarsChanged(bool manualChange = false);
106107

107108
/**
108109
* @brief Set velocity of selected notes
@@ -234,7 +235,7 @@ class MainPanel : public juce::Component, public juce::KeyListener {
234235
const int ratioMarkHalfWidth = 5;
235236
const int ratioMarkMinHeight = 10; ///< is needed for deleting small ratio marks like 1/1
236237

237-
///< This threshold will determine either user wants "drag action" or "click action"
238+
///< This threshold will determine either user wants "drag action" or "click action"
238239
const int clickMoveThrPx = 2;
239240
int clickVelPanelNoteInd = -1;
240241
int clickDelNoteInd = -1;

plugin/source/editor/PluginEditor.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ AudioPluginAudioProcessorEditor::AudioPluginAudioProcessorEditor(AudioPluginAudi
431431
processorRef.params.min_num_bars, processorRef.params.max_num_bars);
432432
numBarsInput->onValueChanged = [this](int newValue) {
433433
processorRef.params.set_num_bars(newValue);
434-
mainPanel->numBarsChanged();
434+
mainPanel->numBarsChanged(true);
435435
topPanel->numBarsChanged();
436436
};
437437
addAndMakeVisible(numBarsInput.get());
@@ -1253,7 +1253,10 @@ void AudioPluginAudioProcessorEditor::parseMidiSclFiles(const juce::File &midiFi
12531253
}
12541254
}
12551255

1256-
this->numBarsInput.get()->onValueChanged(totalBars);
1256+
//this->numBarsInput.get()->onValueChanged(totalBars);
1257+
processorRef.params.set_num_bars(totalBars);
1258+
mainPanel->numBarsChanged(false);
1259+
topPanel->numBarsChanged();
12571260
this->numBeatsInput.get()->onValueChanged(beatsPerBar);
12581261
this->numSubdivsInput.get()->onValueChanged(subdivisionsPerBeat);
12591262

@@ -1481,7 +1484,10 @@ void AudioPluginAudioProcessorEditor::parseNotesFile(const juce::File &notesFile
14811484
notes[i].bend = inputStream.readInt();
14821485
}
14831486

1484-
this->numBarsInput.get()->onValueChanged(numBars);
1487+
//this->numBarsInput.get()->onValueChanged(numBars);
1488+
processorRef.params.set_num_bars(numBars);
1489+
mainPanel->numBarsChanged(false);
1490+
topPanel->numBarsChanged();
14851491
this->numBeatsInput.get()->onValueChanged(numBeats);
14861492
this->numSubdivsInput.get()->onValueChanged(numSubdivs);
14871493

plugin/source/editor/panels/MainPanel.cpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -825,22 +825,20 @@ void MainPanel::mirrorSelNotesVertically() {
825825
repaint();
826826
}
827827

828-
void MainPanel::numBarsChanged() {
829-
bool deletedSmth = false;
828+
void MainPanel::numBarsChanged(bool manualChange) {
829+
bool deletedNotes = false;
830830
size_t notesNum = notes.size();
831831
for (int i = 0; i < notesNum; ++i) {
832832
if (notes[i].time + notes[i].duration > params->get_num_bars()) {
833833
deleteNote(i);
834834
i--;
835835
notesNum--;
836-
deletedSmth = true;
836+
deletedNotes = true;
837837
}
838838
}
839-
deletedSmth = (std::erase_if(params->ratiosMarks,
840-
[&](const auto &ratioMark) {
841-
return (ratioMark.time > params->get_num_bars());
842-
}) > 0) ||
843-
deletedSmth;
839+
bool deleteRatioMarks = (std::erase_if(params->ratiosMarks, [&](const auto &ratioMark) {
840+
return (ratioMark.time > params->get_num_bars());
841+
}) > 0);
844842
if (viewport != nullptr) {
845843
float min_bar_width_px =
846844
static_cast<float>(viewport->getViewWidth()) / params->get_num_bars();
@@ -849,12 +847,13 @@ void MainPanel::numBarsChanged() {
849847
editor->changeBarWidthPx(bar_width_px);
850848
}
851849
}
852-
remakeKeys();
853-
updateLayout();
854-
855-
if (deletedSmth) {
850+
if (deletedNotes) {
851+
remakeKeys();
852+
}
853+
if (manualChange && (deletedNotes || deleteRatioMarks)) {
856854
saveState();
857855
}
856+
updateLayout();
858857
}
859858

860859
int python_mod(int a, int b) {
@@ -1721,7 +1720,8 @@ void MainPanel::mouseDrag(const juce::MouseEvent &event) {
17211720
if ((initialTotalCentsForDrag.size() == 1) && !keys.empty()) {
17221721
int initialTotalCents = initialTotalCentsForDrag[0];
17231722
int newTotalCents = initialTotalCents + dcents;
1724-
newTotalCents = findNearestKeyTotalCents(newTotalCents, keys, params->num_octaves);
1723+
newTotalCents =
1724+
findNearestKeyTotalCents(newTotalCents, keys, params->num_octaves);
17251725
dcents = newTotalCents - initialTotalCents;
17261726

17271727
auto it = std::find_if(notes.begin(), notes.end(),
@@ -2044,11 +2044,15 @@ void MainPanel::mouseUp(const juce::MouseEvent &event) {
20442044
if (isDrawingRatioMark) {
20452045
if ((ratioMarkStartPoint != ratioMarkLastPoint) && !keys.empty()) {
20462046

2047-
int startKeyTotalCents = yToTotalCents(ratioMarkStartPoint.getY(), params->num_octaves, octave_height_px);
2048-
startKeyTotalCents = findNearestKeyTotalCents(startKeyTotalCents, keys, params->num_octaves);
2047+
int startKeyTotalCents =
2048+
yToTotalCents(ratioMarkStartPoint.getY(), params->num_octaves, octave_height_px);
2049+
startKeyTotalCents =
2050+
findNearestKeyTotalCents(startKeyTotalCents, keys, params->num_octaves);
20492051

2050-
int lastKeyTotalCents = yToTotalCents(ratioMarkLastPoint.getY(), params->num_octaves, octave_height_px);
2051-
lastKeyTotalCents = findNearestKeyTotalCents(lastKeyTotalCents, keys, params->num_octaves);
2052+
int lastKeyTotalCents =
2053+
yToTotalCents(ratioMarkLastPoint.getY(), params->num_octaves, octave_height_px);
2054+
lastKeyTotalCents =
2055+
findNearestKeyTotalCents(lastKeyTotalCents, keys, params->num_octaves);
20522056

20532057
if (startKeyTotalCents != lastKeyTotalCents) {
20542058
float startTime = ratioMarkStartPoint.getX() / bar_width_px;
@@ -2361,17 +2365,21 @@ void MainPanel::reattachRatiosMarks(int dcents) {
23612365
RatioMark &ratioMark = *it;
23622366
bool changed = false;
23632367
int higherKeyTotalCents = ratioMark.getHigherKeyTotalCents();
2364-
if (!keysFromAllNotes.contains(higherKeyTotalCents % 1200) && !keysFromAllNotes.empty()) {
2365-
int nearestKeyTotalCents = findNearestKeyTotalCents(higherKeyTotalCents, keysFromAllNotes, params->num_octaves);
2368+
if (!keysFromAllNotes.contains(higherKeyTotalCents % 1200) &&
2369+
!keysFromAllNotes.empty()) {
2370+
int nearestKeyTotalCents = findNearestKeyTotalCents(
2371+
higherKeyTotalCents, keysFromAllNotes, params->num_octaves);
23662372
int diff = std::abs(nearestKeyTotalCents - higherKeyTotalCents);
23672373
if ((diff <= maxCentsChange) && (diff > 0)) {
23682374
higherKeyTotalCents = nearestKeyTotalCents;
23692375
changed = true;
23702376
}
23712377
}
23722378
int lowerKeyTotalCents = ratioMark.getLowerKeyTotalCents();
2373-
if (!keysFromAllNotes.contains(lowerKeyTotalCents % 1200) && !keysFromAllNotes.empty()) {
2374-
int nearestKeyTotalCents = findNearestKeyTotalCents(lowerKeyTotalCents, keysFromAllNotes, params->num_octaves);
2379+
if (!keysFromAllNotes.contains(lowerKeyTotalCents % 1200) &&
2380+
!keysFromAllNotes.empty()) {
2381+
int nearestKeyTotalCents = findNearestKeyTotalCents(
2382+
lowerKeyTotalCents, keysFromAllNotes, params->num_octaves);
23752383
int diff = std::abs(nearestKeyTotalCents - lowerKeyTotalCents);
23762384
if ((diff <= maxCentsChange) && (diff > 0)) {
23772385
lowerKeyTotalCents = nearestKeyTotalCents;

0 commit comments

Comments
 (0)