1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-10 08:44:47 +00:00
Funkin/source/funkin/ui/debug/charting/ChartEditorCommand.hx

605 lines
14 KiB
Haxe
Raw Normal View History

package funkin.ui.debug.charting;
2022-10-11 00:04:09 +00:00
import funkin.play.song.SongData.SongEventData;
2022-10-11 07:14:57 +00:00
import funkin.play.song.SongData.SongNoteData;
import funkin.play.song.SongDataUtils;
2022-10-11 00:04:09 +00:00
using Lambda;
/**
* Actions in the chart editor are backed by the Command pattern
* (see Bob Nystrom's book "Game Programming Patterns" for more info)
*
* To make a function compatible with the undo/redo history, create a new class
* that implements ChartEditorCommand, then call `ChartEditorState.performCommand(new Command())`
*/
interface ChartEditorCommand
{
/**
* Calling this function should perform the action that this command represents.
* @param state The ChartEditorState to perform the action on.
*/
public function execute(state:ChartEditorState):Void;
/**
* Calling this function should perform the inverse of the action that this command represents,
* effectively undoing the action.
* @param state The ChartEditorState to undo the action on.
*/
public function undo(state:ChartEditorState):Void;
/**
* Get a short description of the action (for the UI).
* For example, return `Add Left Note` to display `Undo Add Left Note` in the menu.
*/
public function toString():String;
}
2022-10-11 00:04:09 +00:00
class AddNotesCommand implements ChartEditorCommand
{
2022-10-11 07:14:57 +00:00
private var notes:Array<SongNoteData>;
private var appendToSelection:Bool;
public function new(notes:Array<SongNoteData>, appendToSelection:Bool = false)
{
2022-10-11 00:04:09 +00:00
this.notes = notes;
this.appendToSelection = appendToSelection;
}
public function execute(state:ChartEditorState):Void
{
2022-10-11 07:14:57 +00:00
for (note in notes)
{
2022-10-11 00:04:09 +00:00
state.currentSongChartNoteData.push(note);
}
if (appendToSelection)
{
state.currentSelection = state.currentSelection.concat(notes);
}
else
{
state.currentSelection = notes;
}
state.playSound(Paths.sound('funnyNoise/funnyNoise-08'));
2022-10-11 07:14:57 +00:00
state.noteDisplayDirty = true;
2022-10-11 00:04:09 +00:00
state.notePreviewDirty = true;
2022-10-11 07:14:57 +00:00
state.sortChartData();
}
public function undo(state:ChartEditorState):Void
{
2022-10-11 00:04:09 +00:00
state.currentSongChartNoteData = SongDataUtils.subtractNotes(state.currentSongChartNoteData, notes);
state.currentSelection = [];
state.playSound(Paths.sound('funnyNoise/funnyNoise-01'));
2022-10-11 00:04:09 +00:00
state.noteDisplayDirty = true;
2022-10-11 00:04:09 +00:00
state.notePreviewDirty = true;
2022-10-11 07:14:57 +00:00
state.sortChartData();
}
public function toString():String
{
2022-10-11 07:14:57 +00:00
if (notes.length == 1)
{
2022-10-11 00:04:09 +00:00
var dir:String = notes[0].getDirectionName();
return 'Add $dir Note';
}
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
return 'Add ${notes.length} Notes';
}
}
2022-10-11 07:14:57 +00:00
class RemoveNotesCommand implements ChartEditorCommand
{
2022-10-11 07:14:57 +00:00
private var notes:Array<SongNoteData>;
2022-10-11 07:14:57 +00:00
public function new(notes:Array<SongNoteData>)
{
2022-10-11 07:14:57 +00:00
this.notes = notes;
}
public function execute(state:ChartEditorState):Void
{
2022-10-11 07:14:57 +00:00
state.currentSongChartNoteData = SongDataUtils.subtractNotes(state.currentSongChartNoteData, notes);
2022-10-11 00:04:09 +00:00
state.currentSelection = [];
state.playSound(Paths.sound('funnyNoise/funnyNoise-01'));
2022-10-11 07:14:57 +00:00
state.noteDisplayDirty = true;
2022-10-11 00:04:09 +00:00
state.notePreviewDirty = true;
2022-10-11 07:14:57 +00:00
state.sortChartData();
}
public function undo(state:ChartEditorState):Void
{
2022-10-11 07:14:57 +00:00
for (note in notes)
{
state.currentSongChartNoteData.push(note);
}
state.currentSelection = notes;
state.playSound(Paths.sound('funnyNoise/funnyNoise-08'));
2022-10-11 07:14:57 +00:00
state.noteDisplayDirty = true;
2022-10-11 00:04:09 +00:00
state.notePreviewDirty = true;
state.sortChartData();
}
public function toString():String
{
2022-10-14 01:32:19 +00:00
if (notes.length == 1 && notes[0] != null)
2022-10-11 07:14:57 +00:00
{
2022-10-11 00:04:09 +00:00
var dir:String = notes[0].getDirectionName();
return 'Remove $dir Note';
}
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
return 'Remove ${notes.length} Notes';
}
}
class SwitchDifficultyCommand implements ChartEditorCommand
{
private var prevDifficulty:String;
private var newDifficulty:String;
private var prevVariation:String;
private var newVariation:String;
public function new(prevDifficulty:String, newDifficulty:String, prevVariation:String, newVariation:String)
{
this.prevDifficulty = prevDifficulty;
this.newDifficulty = newDifficulty;
this.prevVariation = prevVariation;
this.newVariation = newVariation;
}
public function execute(state:ChartEditorState):Void
{
state.selectedVariation = newVariation != null ? newVariation : prevVariation;
state.selectedDifficulty = newDifficulty != null ? newDifficulty : prevDifficulty;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function undo(state:ChartEditorState):Void
{
state.selectedVariation = prevVariation != null ? prevVariation : newVariation;
state.selectedDifficulty = prevDifficulty != null ? prevDifficulty : newDifficulty;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function toString():String
{
return 'Switch Difficulty';
}
}
2022-10-11 00:04:09 +00:00
/**
* Adds one or more notes to the selection.
*/
2022-10-11 00:04:09 +00:00
class SelectNotesCommand implements ChartEditorCommand
{
private var notes:Array<SongNoteData>;
public function new(notes:Array<SongNoteData>)
{
this.notes = notes;
}
public function execute(state:ChartEditorState):Void
{
2022-10-11 07:14:57 +00:00
for (note in this.notes)
{
2022-10-11 00:04:09 +00:00
state.currentSelection.push(note);
}
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function undo(state:ChartEditorState):Void
{
state.currentSelection = SongDataUtils.subtractNotes(state.currentSelection, this.notes);
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function toString():String
{
2022-10-11 07:14:57 +00:00
if (notes.length == 1)
{
2022-10-11 00:04:09 +00:00
var dir:String = notes[0].getDirectionName();
return 'Select $dir Note';
}
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
return 'Select ${notes.length} Notes';
}
}
2022-10-11 07:14:57 +00:00
class DeselectNotesCommand implements ChartEditorCommand
2022-10-11 00:04:09 +00:00
{
private var notes:Array<SongNoteData>;
public function new(notes:Array<SongNoteData>)
{
this.notes = notes;
}
public function execute(state:ChartEditorState):Void
{
state.currentSelection = SongDataUtils.subtractNotes(state.currentSelection, this.notes);
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function undo(state:ChartEditorState):Void
{
2022-10-11 07:14:57 +00:00
for (note in this.notes)
{
2022-10-11 00:04:09 +00:00
state.currentSelection.push(note);
}
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function toString():String
{
2022-10-11 07:14:57 +00:00
if (notes.length == 1)
{
2022-10-11 00:04:09 +00:00
var dir:String = notes[0].getDirectionName();
return 'Deselect $dir Note';
}
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
return 'Deselect ${notes.length} Notes';
}
}
/**
* Sets the selection rather than appends it.
* Deselects any notes that are not in the new selection.
*/
class SetNoteSelectionCommand implements ChartEditorCommand
{
private var notes:Array<SongNoteData>;
private var previousSelection:Array<SongNoteData>;
public function new(notes:Array<SongNoteData>, ?previousSelection:Array<SongNoteData>)
{
this.notes = notes;
this.previousSelection = previousSelection == null ? [] : previousSelection;
}
public function execute(state:ChartEditorState):Void
{
state.currentSelection = notes;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function undo(state:ChartEditorState):Void
{
state.currentSelection = previousSelection;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function toString():String
{
return 'Select ${notes.length} Notes';
}
}
2022-10-11 00:04:09 +00:00
class SelectAllNotesCommand implements ChartEditorCommand
{
private var previousSelection:Array<SongNoteData>;
public function new(?previousSelection:Array<SongNoteData>)
{
this.previousSelection = previousSelection == null ? [] : previousSelection;
}
public function execute(state:ChartEditorState):Void
{
2022-10-11 07:14:57 +00:00
state.currentSelection = state.currentSongChartNoteData;
2022-10-11 00:04:09 +00:00
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function undo(state:ChartEditorState):Void
{
state.currentSelection = previousSelection;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function toString():String
{
return 'Select All Notes';
}
}
class InvertSelectedNotesCommand implements ChartEditorCommand
{
private var previousSelection:Array<SongNoteData>;
public function new(?previousSelection:Array<SongNoteData>)
{
this.previousSelection = previousSelection == null ? [] : previousSelection;
}
public function execute(state:ChartEditorState):Void
{
state.currentSelection = SongDataUtils.subtractNotes(state.currentSongChartNoteData, previousSelection);
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function undo(state:ChartEditorState):Void
{
state.currentSelection = previousSelection;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function toString():String
{
return 'Invert Selected Notes';
}
}
2022-10-11 00:04:09 +00:00
class DeselectAllNotesCommand implements ChartEditorCommand
{
private var previousSelection:Array<SongNoteData>;
public function new(?previousSelection:Array<SongNoteData>)
{
this.previousSelection = previousSelection == null ? [] : previousSelection;
}
public function execute(state:ChartEditorState):Void
{
state.currentSelection = [];
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function undo(state:ChartEditorState):Void
{
state.currentSelection = previousSelection;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
}
public function toString():String
{
return 'Deselect All Notes';
}
}
class CutNotesCommand implements ChartEditorCommand
{
private var notes:Array<SongNoteData>;
2022-10-14 01:32:19 +00:00
public function new(notes:Array<SongNoteData>)
2022-10-11 00:04:09 +00:00
{
this.notes = notes;
}
public function execute(state:ChartEditorState):Void
{
// Copy the notes.
2022-10-13 01:42:40 +00:00
SongDataUtils.writeNotesToClipboard(SongDataUtils.buildClipboard(notes));
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
// Delete the notes.
state.currentSongChartNoteData = SongDataUtils.subtractNotes(state.currentSongChartNoteData, notes);
state.currentSelection = [];
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
state.sortChartData();
}
public function undo(state:ChartEditorState):Void
{
state.currentSongChartNoteData = state.currentSongChartNoteData.concat(notes);
state.currentSelection = notes;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
state.sortChartData();
}
public function toString():String
{
var len:Int = notes.length;
return 'Cut $len Notes to Clipboard';
}
}
class FlipNotesCommand implements ChartEditorCommand
{
private var notes:Array<SongNoteData>;
private var flippedNotes:Array<SongNoteData>;
public function new(notes:Array<SongNoteData>)
{
this.notes = notes;
}
public function execute(state:ChartEditorState):Void
{
// Delete the notes.
state.currentSongChartNoteData = SongDataUtils.subtractNotes(state.currentSongChartNoteData, notes);
// Add the flipped notes.
flippedNotes = SongDataUtils.flipNotes(notes);
state.currentSongChartNoteData = state.currentSongChartNoteData.concat(flippedNotes);
state.currentSelection = flippedNotes;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
state.sortChartData();
}
public function undo(state:ChartEditorState):Void
{
state.currentSongChartNoteData = SongDataUtils.subtractNotes(state.currentSongChartNoteData, flippedNotes);
state.currentSongChartNoteData = state.currentSongChartNoteData.concat(notes);
state.currentSelection = notes;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
state.sortChartData();
}
public function toString():String
{
var len:Int = notes.length;
return 'Flip $len Notes';
}
}
2022-10-11 00:04:09 +00:00
class PasteNotesCommand implements ChartEditorCommand
{
2022-10-13 01:42:40 +00:00
private var targetTimestamp:Float;
2022-10-14 01:32:19 +00:00
// Notes we added with this command, for undo.
private var addedNotes:Array<SongNoteData>;
2022-10-11 00:04:09 +00:00
2022-10-13 01:42:40 +00:00
public function new(targetTimestamp:Float)
2022-10-11 00:04:09 +00:00
{
this.targetTimestamp = targetTimestamp;
}
public function execute(state:ChartEditorState):Void
{
2022-10-13 01:42:40 +00:00
var currentClipboard:Array<SongNoteData> = SongDataUtils.readNotesFromClipboard();
2022-10-14 01:32:19 +00:00
addedNotes = SongDataUtils.offsetSongNoteData(currentClipboard, Std.int(targetTimestamp));
2022-10-11 00:04:09 +00:00
2022-10-14 01:32:19 +00:00
state.currentSongChartNoteData = state.currentSongChartNoteData.concat(addedNotes);
state.currentSelection = addedNotes.copy();
2022-10-11 00:04:09 +00:00
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
state.sortChartData();
}
public function undo(state:ChartEditorState):Void
{
2022-10-14 01:32:19 +00:00
state.currentSongChartNoteData = SongDataUtils.subtractNotes(state.currentSongChartNoteData, addedNotes);
2022-10-11 00:04:09 +00:00
state.currentSelection = [];
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
state.sortChartData();
}
public function toString():String
{
2022-10-13 01:42:40 +00:00
var currentClipboard:Array<SongNoteData> = SongDataUtils.readNotesFromClipboard();
return 'Paste ${currentClipboard.length} Notes from Clipboard';
2022-10-11 00:04:09 +00:00
}
}
class AddEventsCommand implements ChartEditorCommand
{
private var events:Array<SongEventData>;
private var appendToSelection:Bool;
2022-10-11 07:14:57 +00:00
public function new(events:Array<SongEventData>, ?appendToSelection:Bool = false)
2022-10-11 00:04:09 +00:00
{
this.events = events;
this.appendToSelection = appendToSelection;
2022-10-11 00:04:09 +00:00
}
public function execute(state:ChartEditorState):Void
{
state.currentSongChartEventData = state.currentSongChartEventData.concat(events);
// TODO: Allow selecting events.
// state.currentSelection = events;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
state.sortChartData();
}
public function undo(state:ChartEditorState):Void
{
state.currentSongChartEventData = SongDataUtils.subtractEvents(state.currentSongChartEventData, events);
state.currentSelection = [];
2022-10-11 00:04:09 +00:00
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
2022-10-11 07:14:57 +00:00
2022-10-11 00:04:09 +00:00
state.sortChartData();
}
public function toString():String
{
var len:Int = events.length;
return 'Add $len Events';
}
2022-10-11 07:14:57 +00:00
}
class ExtendNoteLengthCommand implements ChartEditorCommand
{
private var note:SongNoteData;
private var oldLength:Float;
private var newLength:Float;
public function new(note:SongNoteData, newLength:Float)
{
this.note = note;
this.oldLength = note.length;
this.newLength = newLength;
}
public function execute(state:ChartEditorState):Void
{
note.length = newLength;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
state.sortChartData();
}
public function undo(state:ChartEditorState):Void
{
note.length = oldLength;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
state.sortChartData();
}
public function toString():String
{
return 'Extend Note Length';
}
}