Funkin/source/funkin/play/song/SongDataUtils.hx

101 lines
3 KiB
Haxe
Raw Normal View History

2022-10-10 20:04:09 -04:00
package funkin.play.song;
2022-10-11 03:14:57 -04:00
import funkin.play.song.SongData.SongEventData;
import funkin.play.song.SongData.SongNoteData;
2022-10-12 21:42:40 -04:00
import funkin.util.ClipboardUtil;
import funkin.util.SerializerUtil;
2022-10-10 20:04:09 -04:00
2022-10-11 03:14:57 -04:00
using Lambda;
2022-10-10 20:04:09 -04:00
2022-10-11 03:14:57 -04:00
class SongDataUtils
{
/**
* Given an array of SongNoteData objects, return a new array of SongNoteData objects
* whose timestamps are shifted by the given amount.
*
* @param notes The notes to modify.
* @param offset The time difference to apply in milliseconds.
*/
public static function offsetSongNoteData(notes:Array<SongNoteData>, offset:Int):Array<SongNoteData>
{
return notes.map(function(note:SongNoteData):SongNoteData
{
return new SongNoteData(note.time + offset, note.data, note.length, note.kind);
});
}
2022-10-10 20:04:09 -04:00
2022-10-11 03:14:57 -04:00
/**
* Remove a certain subset of notes from an array of SongNoteData objects.
*
* @param notes The array of notes to be subtracted from.
* @param subtrahend The notes to remove from the `notes` array. Yes, subtrahend is a real word.
*/
public static function subtractNotes(notes:Array<SongNoteData>, subtrahend:Array<SongNoteData>)
{
if (notes.length == 0 || subtrahend.length == 0)
return notes;
2022-10-10 20:04:09 -04:00
2022-10-11 03:14:57 -04:00
return notes.filter(function(note:SongNoteData):Bool
{
// SongNoteData's == operation has been overridden so that this will work.
return !subtrahend.has(note);
});
}
2022-10-10 20:04:09 -04:00
2022-10-11 03:14:57 -04:00
/**
* Remove a certain subset of events from an array of SongEventData objects.
*
* @param events The array of events to be subtracted from.
* @param subtrahend The events to remove from the `events` array. Yes, subtrahend is a real word.
*/
public static function subtractEvents(events:Array<SongEventData>, subtrahend:Array<SongEventData>)
{
if (events.length == 0 || subtrahend.length == 0)
return events;
2022-10-10 20:04:09 -04:00
2022-10-11 03:14:57 -04:00
return events.filter(function(event:SongEventData):Bool
{
// SongEventData's == operation has been overridden so that this will work.
return !subtrahend.has(event);
});
}
2022-10-10 20:04:09 -04:00
2022-10-11 03:14:57 -04:00
/**
* Prepare an array of notes to be used as the clipboard data.
*
* Offset the provided array of notes such that the first note is at 0 milliseconds.
*/
public static function buildClipboard(notes:Array<SongNoteData>):Array<SongNoteData>
{
2022-10-12 21:42:40 -04:00
return offsetSongNoteData(notes, -Std.int(notes[0].time));
2022-10-11 03:14:57 -04:00
}
2022-10-12 21:42:40 -04:00
public static function writeNotesToClipboard(notes:Array<SongNoteData>):Void
{
var notesString = SerializerUtil.toJSON(notes);
ClipboardUtil.setClipboard(notesString);
trace('Wrote ' + notes.length + ' notes to clipboard.');
trace(notesString);
}
public static function readNotesFromClipboard():Array<SongNoteData>
{
var notesString = ClipboardUtil.getClipboard();
trace('Read ' + notesString.length + ' characters from clipboard.');
var notes:Array<SongNoteData> = SerializerUtil.fromJSON(notesString);
if (notes == null) {
trace('Failed to parse notes from clipboard.');
return [];
} else {
trace('Parsed ' + notes.length + ' notes from clipboard.');
return notes;
}
}
2022-10-11 03:14:57 -04:00
}