1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-12-27 07:27:12 +00:00
Funkin/source/funkin/util/SortUtil.hx

148 lines
5.6 KiB
Haxe
Raw Normal View History

package funkin.util;
2022-01-26 19:19:57 +00:00
import flixel.graphics.frames.FlxFrame;
2022-02-23 21:49:54 +00:00
#if !macro
import flixel.FlxBasic;
2022-01-26 19:19:57 +00:00
import flixel.util.FlxSort;
2022-02-23 21:49:54 +00:00
#end
2023-06-22 05:41:01 +00:00
import funkin.play.notes.NoteSprite;
import funkin.data.song.SongData.SongEventData;
import funkin.data.song.SongData.SongNoteData;
2022-01-26 19:19:57 +00:00
/**
* Utility functions related to sorting.
*
* NOTE: `Array.sort()` takes a function `(x, y) -> Int`.
* If the objects are in the correct order (x before y), return a negative value.
* If the objects need to be swapped (y before x), return a negative value.
* If the objects are equal, return 0.
*
* NOTE: `Array.sort()` does NOT guarantee that the order of equal elements. `haxe.ds.ArraySort.sort()` does guarantee this.
* NOTE: `Array.sort()` may not be the most efficient sorting algorithm for all use cases (especially if the array is known to be mostly sorted).
* You may consider using one of the functions in `funkin.util.tools.ArraySortTools` instead.
* NOTE: Both sort functions modify the array in-place. You may consider using `Reflect.copy()` to make a copy of the array before sorting.
*/
2022-01-26 19:19:57 +00:00
class SortUtil
{
/**
* You can use this function in FlxTypedGroup.sort() to sort FlxObjects by their z-index values.
* The value defaults to 0, but by assigning it you can easily rearrange objects as desired.
2024-03-17 02:20:22 +00:00
*
* @param order Either `FlxSort.ASCENDING` or `FlxSort.DESCENDING`
* @param a The first FlxObject to compare.
* @param b The second FlxObject to compare.
* @return 1 if `a` has a higher z-index, -1 if `b` has a higher z-index.
*/
2024-03-17 02:20:22 +00:00
public static inline function byZIndex(order:Int, a:FlxBasic, b:FlxBasic):Int
{
2024-03-17 02:20:22 +00:00
if (a == null || b == null) return 0;
return FlxSort.byValues(order, a.zIndex, b.zIndex);
}
/**
* Given two Notes, returns 1 or -1 based on whether `a` or `b` has an earlier strumtime.
2023-06-08 20:30:45 +00:00
*
* @param order Either `FlxSort.ASCENDING` or `FlxSort.DESCENDING`
2024-03-17 02:20:22 +00:00
* @param a The first Note to compare.
* @param b The second Note to compare.
* @return 1 if `a` has an earlier strumtime, -1 if `b` has an earlier strumtime.
*/
2024-03-17 02:20:22 +00:00
public static inline function byStrumtime(order:Int, a:NoteSprite, b:NoteSprite):Int
{
return noteDataByTime(order, a.noteData, b.noteData);
}
2024-03-17 02:20:22 +00:00
/**
* Given two Note Data 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 noteDataByTime(order:Int, a:SongNoteData, b:SongNoteData):Int
{
return FlxSort.byValues(order, a.time, b.time);
}
2024-03-17 02:20:22 +00:00
/**
* Given two Event Data 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 eventDataByTime(order:Int, a:SongEventData, b:SongEventData):Int
{
return FlxSort.byValues(order, a.time, b.time);
}
2023-07-19 05:29:43 +00:00
/**
* Given two FlxFrames, sort their names alphabetically.
*
* @param order Either `FlxSort.ASCENDING` or `FlxSort.DESCENDING`
2024-03-17 02:20:22 +00:00
* @param a The first Frame to compare.
* @param b The second Frame to compare.
* @return 1 if `a` has an earlier time, -1 if `b` has an earlier time.
*/
2024-03-17 02:20:22 +00:00
public static inline function byFrameName(a:FlxFrame, b:FlxFrame):Int
{
return alphabetically(a.name, b.name);
}
2023-07-19 05:29:43 +00:00
/**
* Sort predicate for sorting strings alphabetically.
* @param a The first string to compare.
* @param b The second string to compare.
2024-03-17 02:20:22 +00:00
* @return 1 if `a` comes before `b`, -1 if `b` comes before `a`, 0 if they are equal
2023-07-19 05:29:43 +00:00
*/
2024-06-20 20:17:53 +00:00
public static function alphabetically(?a:String, ?b:String):Int
2023-07-19 05:29:43 +00:00
{
a = a.toUpperCase();
b = b.toUpperCase();
2023-07-19 05:29:43 +00:00
// Sort alphabetically. Yes that's how this works.
return a == b ? 0 : a > b ? 1 : -1;
2023-07-19 05:29:43 +00:00
}
/**
* Sort predicate which sorts two strings alphabetically, but prioritizes a specific string first.
* Example usage: `array.sort(defaultThenAlphabetical.bind('test'))` will sort the array so that the string 'test' is first.
2024-03-17 02:20:22 +00:00
*
* @param defaultValue The value to prioritize.
* @param a The first string to compare.
* @param b The second string to compare.
2024-03-17 02:20:22 +00:00
* @return 1 if `a` comes before `b`, -1 if `b` comes before `a`, 0 if they are equal
*/
public static function defaultThenAlphabetically(defaultValue:String, a:String, b:String):Int
{
if (a == b) return 0;
if (a == defaultValue) return -1;
if (b == defaultValue) return 1;
return alphabetically(a, b);
}
/**
* Sort predicate which sorts two strings alphabetically, but prioritizes a specific string first.
* Example usage: `array.sort(defaultsThenAlphabetical.bind(['test']))` will sort the array so that the string 'test' is first.
2024-03-17 02:20:22 +00:00
*
* @param defaultValues The values to prioritize.
* @param a The first string to compare.
* @param b The second string to compare.
2024-03-17 02:20:22 +00:00
* @return 1 if `a` comes before `b`, -1 if `b` comes before `a`, 0 if they are equal
*/
public static function defaultsThenAlphabetically(defaultValues:Array<String>, a:String, b:String):Int
{
if (a == b) return 0;
if (defaultValues.contains(a) && defaultValues.contains(b))
{
// Sort by index in defaultValues
return defaultValues.indexOf(a) - defaultValues.indexOf(b);
};
if (defaultValues.contains(a)) return -1;
if (defaultValues.contains(b)) return 1;
return alphabetically(a, b);
}
2022-01-26 19:19:57 +00:00
}