Skip to content

Instantly share code, notes, and snippets.

@kinoko87
Created December 26, 2022 01:57
Show Gist options
  • Save kinoko87/a7b9ed8b7579221ea4ed69aabb000a76 to your computer and use it in GitHub Desktop.
Save kinoko87/a7b9ed8b7579221ea4ed69aabb000a76 to your computer and use it in GitHub Desktop.
The shit needed for undo/redo implementation in FNF's charting state
enum abstract ChartActionType(Int) from Int to Int {
var AddNote:Int = 0;
var RemoveNote:Int = 1;
var AddEvent:Int = 2;
var RemoveEvent:Int = 3;
}
typedef ChartAction = {
var action:ChartActionType;
var data:Dynamic;
}
var chartActions:Array<ChartAction> = [];
var undoneStuff:Array<ChartAction> = [];
function undo() {
if (chartActions.length == 0) return;
trace(chartActions);
var cA = chartActions[chartActions.length - 1];
switch (cA.action) {
case AddNote:
_song.notes[curSec].sectionNotes.remove(cA.data);
if (curSelectedNote == cA.data)
curSelectedNote = null;
case RemoveNote:
_song.notes[curSec].sectionNotes.push(cA.data);
curSelectedNote = cA.data;
case AddEvent:
_song.events.remove(cA.data);
if (curSelectedNote == cA.data)
curSelectedNote = null;
case RemoveEvent:
_song.events.push(cA.data);
curSelectedNote = cA.data;
}
undoneStuff.push(cA);
chartActions.pop();
updateGrid();
}
function redo() {
if (undoneStuff.length == 0) return;
trace(undoneStuff);
var cA = undoneStuff[undoneStuff.length - 1];
switch (cA.action) {
case AddNote:
_song.notes[curSec].sectionNotes.push(cA.data);
curSelectedNote = cA.data;
case RemoveNote:
_song.notes[curSec].sectionNotes.remove(cA.data);
if (curSelectedNote == cA.data)
curSelectedNote = null;
case AddEvent:
_song.events.push(cA.data);
curSelectedNote = cA.data;
case RemoveEvent:
_song.events.remove(cA.data);
if (curSelectedNote == cA.data)
curSelectedNote = null;
}
undoneStuff.pop();
chartActions.push(cA);
updateGrid();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment