Skip to content

Commit da60222

Browse files
committed
Add method to update state mid chain.
This solves the 2 separate history entries for beat insert then note editing. This method is inheritably unsafe to use due to possible desync in history / chart state if misused, but allows for much more possibilities for compressing history entries.
1 parent dd362ac commit da60222

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

src/Editor/Editing.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -990,14 +990,12 @@ struct EditingImpl : public Editing {
990990
lastBoundStart = boundStart;
991991
rit++;
992992
}
993-
gHistory->finishChain("[Recolorize] Beat Inserts");
993+
gHistory->updateChain();
994994

995995
// Step 2: Move Notes, and Tempo changes.
996996
// The selected notes will now have moved from the beat inserts, so the
997997
// relative location will be accurate. Now just adjust the BPM / Scroll
998998
// to offset the changes needed.
999-
gHistory->startChain();
1000-
1001999
NoteEdit notesEdit;
10021000
SegmentEdit tempoEdit;
10031001

@@ -1067,7 +1065,7 @@ struct EditingImpl : public Editing {
10671065

10681066
gNotes->modify(notesEdit, false);
10691067
gTempo->modify(tempoEdit, false);
1070-
gHistory->finishChain("[Recolorize] Note Movement");
1068+
gHistory->finishChain("Recolorized Selected Notes");
10711069
}
10721070

10731071
/*

src/Editor/History.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ struct HistoryImpl : public History {
156156
EntryList myChain;
157157
int myOpenChains = 0;
158158

159+
int myChainEntries = 0;
160+
int myAppliedChainEntries = 0;
161+
159162
Vector<Callback> myCallbacks;
160163

161164
// ================================================================================================
@@ -217,6 +220,7 @@ struct HistoryImpl : public History {
217220
Entry* entry = CreateEntry(id, data, size, targetChart, targetTempo);
218221
if (myOpenChains > 0) {
219222
myChain.add(entry);
223+
myChainEntries++;
220224
} else {
221225
pushEntry(entry);
222226
}
@@ -346,17 +350,46 @@ struct HistoryImpl : public History {
346350
void finishChain(std::string msg) override {
347351
myOpenChains = max(0, myOpenChains - 1);
348352
if (myChain.head && myOpenChains == 0) {
353+
bool partialApply = myAppliedChainEntries > 0;
354+
349355
clearUnappliedEntries();
356+
if (partialApply) updateChain();
350357

351358
WriteStream stream;
352359
stream.write(myChain);
353360
stream.writeStr(msg);
354361

355362
myChain.head = nullptr;
363+
myChainEntries = 0;
364+
myAppliedChainEntries = 0;
356365

357366
auto entry =
358367
CreateEntry(0, stream.data(), stream.size(), nullptr, nullptr);
359-
pushEntry(entry);
368+
369+
if (partialApply) {
370+
myEntries.add(entry);
371+
++myAppliedEntries;
372+
++myTotalEntries;
373+
} else {
374+
pushEntry(entry);
375+
}
376+
}
377+
}
378+
379+
void updateChain() override {
380+
if (myChain.head && myAppliedChainEntries < myChainEntries) {
381+
clearUnappliedEntries();
382+
383+
Bindings bound = {mySimfile, nullptr, nullptr};
384+
auto it = myEntries.head;
385+
while (it) it = Advance(it, bound);
386+
387+
it = myChain.head;
388+
for (int i = 0; i < myAppliedChainEntries && it; ++i) it = it->next;
389+
for (; it; it = it->next) {
390+
ApplyEntry(it, bound, false, false);
391+
myAppliedChainEntries++;
392+
}
360393
}
361394
}
362395

src/Editor/History.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ struct History : public InputHandler {
3333
virtual void startChain() = 0;
3434
virtual void finishChain(std::string message) = 0;
3535

36+
/// Applies queued changes directly without a history entry.
37+
/// This can cause a desync between changes and history if "finishChain()"
38+
/// isn't called, but allows for chart state to be updated during a chain
39+
/// instead of at the end of it.
40+
virtual void updateChain() = 0;
41+
3642
virtual void onFileOpen(Simfile* simfile) = 0;
3743
virtual void onFileClosed() = 0;
3844
virtual void onFileSaved() = 0;

0 commit comments

Comments
 (0)