Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions source/funkin/data/song/SongDataUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import funkin.data.song.SongData.SongEventData;
import funkin.data.song.SongData.SongNoteData;
import funkin.data.song.SongData.SongTimeChange;
import funkin.util.ClipboardUtil;
import funkin.util.SortUtil;

using Lambda;

Expand Down Expand Up @@ -176,7 +177,7 @@ class SongDataUtils
{
// TODO: Modifies the array in place. Is this okay?
notes.sort(function(a:SongNoteData, b:SongNoteData):Int {
return FlxSort.byValues(desc ? FlxSort.DESCENDING : FlxSort.ASCENDING, a.time, b.time);
return SortUtil.noteDataByTime(desc ? FlxSort.DESCENDING : FlxSort.ASCENDING, a, b);
});
return notes;
}
Expand All @@ -188,19 +189,19 @@ class SongDataUtils
{
// TODO: Modifies the array in place. Is this okay?
events.sort(function(a:SongEventData, b:SongEventData):Int {
return FlxSort.byValues(desc ? FlxSort.DESCENDING : FlxSort.ASCENDING, a.time, b.time);
return SortUtil.eventDataByTime(desc ? FlxSort.DESCENDING : FlxSort.ASCENDING, a, b);
});
return events;
}

/**
* Sort an array of notes by strum time.
* Sort an array of time changes by strum time.
*/
public static function sortTimeChanges(timeChanges:Array<SongTimeChange>, desc:Bool = false):Array<SongTimeChange>
{
// TODO: Modifies the array in place. Is this okay?
timeChanges.sort(function(a:SongTimeChange, b:SongTimeChange):Int {
return FlxSort.byValues(desc ? FlxSort.DESCENDING : FlxSort.ASCENDING, a.timeStamp, b.timeStamp);
return SortUtil.timeChangeByTime(desc ? FlxSort.DESCENDING : FlxSort.ASCENDING, a, b);
});
return timeChanges;
}
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/play/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ class PlayState extends MusicBeatSubState
if (generatedMusic)
{
// TODO: Sort more efficiently, or less often, to improve performance.
// activeNotes.sort(SortUtil.byStrumtime, FlxSort.DESCENDING);
// activeNotes.sort(SortUtil.notebyStrumtime, FlxSort.DESCENDING);
}

if (FlxG.sound.music != null)
Expand Down
44 changes: 4 additions & 40 deletions source/funkin/play/notes/Strumline.hx
Original file line number Diff line number Diff line change
Expand Up @@ -805,9 +805,9 @@ class Strumline extends FlxSpriteGroup
public function onBeatHit():Void
{
// why are we doing this every beat? >:(
if (notes.members.length > 1) notes.members.insertionSort(compareNoteSprites.bind(FlxSort.ASCENDING));
if (notes.members.length > 1) notes.members.insertionSort(SortUtil.notebyStrumtime.bind(FlxSort.ASCENDING));

if (holdNotes.members.length > 1) holdNotes.members.insertionSort(compareHoldNoteSprites.bind(FlxSort.ASCENDING));
if (holdNotes.members.length > 1) holdNotes.members.insertionSort(SortUtil.sustainTrailbyStrumtime.bind(FlxSort.ASCENDING));
}

/**
Expand Down Expand Up @@ -895,7 +895,7 @@ class Strumline extends FlxSpriteGroup
this.nextNoteIndex = 0;

// Sort the notes by strumtime.
this.noteData.insertionSort(compareNoteData.bind(FlxSort.ASCENDING));
this.noteData.insertionSort(SortUtil.noteDataByTime.bind(FlxSort.ASCENDING));
}

/**
Expand All @@ -909,7 +909,7 @@ class Strumline extends FlxSpriteGroup
if (note == null) return;

this.noteData.push(note);
if (sort) this.noteData.sort(compareNoteData.bind(FlxSort.ASCENDING));
if (sort) this.noteData.sort(SortUtil.noteDataByTime.bind(FlxSort.ASCENDING));
}

/**
Expand Down Expand Up @@ -1366,42 +1366,6 @@ class Strumline extends FlxSpriteGroup
}
}

/**
* Compare two note data objects by their strumtime.
* @param order The order to sort the notes in.
* @param a The first note data object.
* @param b The second note data object.
* @return The comparison result, based on the time of the notes.
*/
function compareNoteData(order:Int, a:SongNoteData, b:SongNoteData):Int
{
return FlxSort.byValues(order, a.time, b.time);
}

/**
* Compare two note sprites by their strumtime.
* @param order The order to sort the notes in.
* @param a The first note sprite.
* @param b The second note sprite.
* @return The comparison result, based on the time of the notes.
*/
function compareNoteSprites(order:Int, a:NoteSprite, b:NoteSprite):Int
{
return FlxSort.byValues(order, a?.strumTime, b?.strumTime);
}

/**
* Compare two hold note sprites by their strumtime.
* @param order The order to sort the notes in.
* @param a The first hold note sprite.
* @param b The second hold note sprite.
* @return The comparison result, based on the time of the notes.
*/
function compareHoldNoteSprites(order:Int, a:SustainTrail, b:SustainTrail):Int
{
return FlxSort.byValues(order, a?.strumTime, b?.strumTime);
}

/**
* Find the minimum Y position of the strumline.
* Ignores the background to ensure the strumline is positioned correctly.
Expand Down
4 changes: 2 additions & 2 deletions source/funkin/ui/debug/charting/ChartEditorState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6356,12 +6356,12 @@ class ChartEditorState extends UIState // UIState derives from MusicBeatState
{
// TODO: .insertionSort()
currentSongChartNoteData.sort(function(a:SongNoteData, b:SongNoteData):Int {
return FlxSort.byValues(FlxSort.ASCENDING, a.time, b.time);
return SortUtil.noteDataByTime(FlxSort.ASCENDING, a, b);
});

// TODO: .insertionSort()
currentSongChartEventData.sort(function(a:SongEventData, b:SongEventData):Int {
return FlxSort.byValues(FlxSort.ASCENDING, a.time, b.time);
return SortUtil.eventDataByTime(FlxSort.ASCENDING, a, b);
});
}

Expand Down
40 changes: 38 additions & 2 deletions source/funkin/util/SortUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import flixel.FlxBasic;
import flixel.util.FlxSort;
#end
import funkin.play.notes.NoteSprite;
import funkin.play.notes.SustainTrail;
import funkin.data.song.SongData.SongEventData;
import funkin.data.song.SongData.SongNoteData;
import funkin.data.song.SongData.SongTimeChange;

/**
* Utility functions related to sorting.
Expand Down Expand Up @@ -48,11 +50,26 @@ class SortUtil
* @param b The second Note to compare.
* @return 1 if `a` has an earlier strumtime, -1 if `b` has an earlier strumtime.
*/
public static inline function byStrumtime(order:Int, a:NoteSprite, b:NoteSprite):Int
public static inline function notebyStrumtime(order:Int, a:NoteSprite, b:NoteSprite):Int
{
if (a == null || b == null) return 0;
return noteDataByTime(order, a.noteData, b.noteData);
}

/**
* Given two hold note sprites, returns 1 or -1 based on whether `a` or `b` has an earlier strumtime.
*
* @param order Either `FlxSort.ASCENDING` or `FlxSort.DESCENDING`
* @param a The first hold note sprite to compare.
* @param b The second hold note sprite to compare.
* @return 1 if `a` has an earlier strumtime, -1 if `b` has an earlier strumtime.
*/
public static inline function sustainTrailbyStrumtime(order:Int, a:SustainTrail, b:SustainTrail):Int
{
if (a == null || b == null) return 0;
return FlxSort.byValues(order, a.strumTime, b.strumTime);
}

/**
* Given two Note Data objects, returns 1 or -1 based on whether `a` or `b` has an earlier time.
*
Expand All @@ -63,7 +80,11 @@ class SortUtil
*/
public static inline function noteDataByTime(order:Int, a:SongNoteData, b:SongNoteData):Int
{
return FlxSort.byValues(order, a.time, b.time);
if (a == null || b == null) return 0;

var result = FlxSort.byValues(order, a.time, b.time);
if (result == 0) result = FlxSort.byValues(order, a.length, b.length);
return result;
}

/**
Expand All @@ -76,9 +97,24 @@ class SortUtil
*/
public static inline function eventDataByTime(order:Int, a:SongEventData, b:SongEventData):Int
{
if (a == null || b == null) return 0;
return FlxSort.byValues(order, a.time, b.time);
}

/**
* Given two Time Change objects, returns 1 or -1 based on whether `a` or `b` has an earlier time.
*
* @param order Either `FlxSort.ASCENDING` or `FlxSort.DESCENDING`
* @param a The first Event to compare.
* @param b The second Event to compare.
* @return 1 if `a` has an earlier time, -1 if `b` has an earlier time.
*/
public static inline function timeChangeByTime(order:Int, a:SongTimeChange, b:SongTimeChange):Int
{
if (a == null || b == null) return 0;
return FlxSort.byValues(order, a.timeStamp, b.timeStamp);
}

/**
* Given two FlxFrames, sort their names alphabetically.
*
Expand Down
Loading