Funkin/source/funkin/ui/debug/charting/commands/PasteItemsCommand.hx
2023-11-28 20:36:59 -05:00

85 lines
2.9 KiB
Haxe

package funkin.ui.debug.charting.commands;
import funkin.data.song.SongData.SongEventData;
import funkin.data.song.SongData.SongNoteData;
import funkin.data.song.SongDataUtils;
import funkin.data.song.SongDataUtils.SongClipboardItems;
/**
* A command which inserts the contents of the clipboard into the chart editor.
*/
@:nullSafety
@:access(funkin.ui.debug.charting.ChartEditorState)
class PasteItemsCommand implements ChartEditorCommand
{
var targetTimestamp:Float;
// Notes we added with this command, for undo.
var addedNotes:Array<SongNoteData> = [];
var addedEvents:Array<SongEventData> = [];
public function new(targetTimestamp:Float)
{
this.targetTimestamp = targetTimestamp;
}
public function execute(state:ChartEditorState):Void
{
var currentClipboard:SongClipboardItems = SongDataUtils.readItemsFromClipboard();
if (currentClipboard.valid != true)
{
state.error('Failed to Paste', 'Could not parse clipboard contents.');
return;
}
var stepEndOfSong:Float = Conductor.getTimeInSteps(state.songLengthInMs);
var stepCutoff:Float = stepEndOfSong - 1.0;
var msCutoff:Float = Conductor.getStepTimeInMs(stepCutoff);
addedNotes = SongDataUtils.offsetSongNoteData(currentClipboard.notes, Std.int(targetTimestamp));
addedNotes = SongDataUtils.clampSongNoteData(addedNotes, 0.0, msCutoff);
addedEvents = SongDataUtils.offsetSongEventData(currentClipboard.events, Std.int(targetTimestamp));
addedEvents = SongDataUtils.clampSongEventData(addedEvents, 0.0, msCutoff);
state.currentSongChartNoteData = state.currentSongChartNoteData.concat(addedNotes);
state.currentSongChartEventData = state.currentSongChartEventData.concat(addedEvents);
state.currentNoteSelection = addedNotes.copy();
state.currentEventSelection = addedEvents.copy();
state.saveDataDirty = true;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
state.sortChartData();
state.success('Paste Successful', 'Successfully pasted clipboard contents.');
}
public function undo(state:ChartEditorState):Void
{
state.playSound(Paths.sound('chartingSounds/undo'));
state.currentSongChartNoteData = SongDataUtils.subtractNotes(state.currentSongChartNoteData, addedNotes);
state.currentSongChartEventData = SongDataUtils.subtractEvents(state.currentSongChartEventData, addedEvents);
state.currentNoteSelection = [];
state.currentEventSelection = [];
state.saveDataDirty = true;
state.noteDisplayDirty = true;
state.notePreviewDirty = true;
state.sortChartData();
}
public function toString():String
{
var currentClipboard:SongClipboardItems = SongDataUtils.readItemsFromClipboard();
var len:Int = currentClipboard.notes.length + currentClipboard.events.length;
if (currentClipboard.notes.length == 0) return 'Paste $len Events';
else if (currentClipboard.events.length == 0) return 'Paste $len Notes';
else
return 'Paste $len Items';
}
}