Skip to content

Commit

Permalink
Add ArrayUtil
Browse files Browse the repository at this point in the history
Adds a utility primarily for haxe versions that did not support array resizing
  • Loading branch information
dimensionscape committed Jul 10, 2024
1 parent c9ca80f commit c03d806
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/starling/animation/Juggler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import openfl.errors.ArgumentError;

import starling.events.Event;
import starling.events.EventDispatcher;
import starling.utils.ArrayUtil;

/** The Juggler takes objects that implement IAnimatable (like Tweens) and executes them.
*
Expand Down Expand Up @@ -403,8 +404,11 @@ class Juggler implements IAnimatable

while (i < numObjects)
__objects[currentIndex++] = __objects[i++];

#if (haxe_ver >= 4.0)
__objects.resize(currentIndex);
#else
ArrayUtil.resize(__objects, currentIndex);
#end
}
}

Expand Down
44 changes: 44 additions & 0 deletions src/starling/utils/ArrayUtil.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package starling.utils;

/**
* Array Utility.
*/
class ArrayUtil
{
/**
* Truncates an array to the specified length.
* @param arr The array to truncate.
* @param newSize The new size of the array.
*/
public static function truncate<T>(arr:Array<T>, newSize:Int):Void {
if (newSize < arr.length) {
arr.splice(newSize, arr.length - newSize);
}
}

/**
* Extends an array to the specified length, filling new elements with the default value.
* @param arr The array to extend.
* @param newSize The new size of the array.
* @param defaultValue The value to fill new elements with.
*/
public static function extend<T>(arr:Array<T>, newSize:Int, defaultValue:T):Void {
while (arr.length < newSize) {
arr.push(defaultValue);
}
}

/**
* Resizes an array to the specified length, truncating or extending as needed.
* @param arr The array to resize.
* @param newSize The new size of the array.
* @param defaultValue The value to fill new elements with when extending.
*/
public static function resize<T>(arr:Array<T>, newSize:Int, defaultValue:T):Void {
if (newSize < arr.length) {
truncate(arr, newSize);
} else {
extend(arr, newSize, defaultValue);
}
}
}

0 comments on commit c03d806

Please sign in to comment.