2023-02-02 23:08:30 +00:00
|
|
|
package funkin.graphics.adobeanimate;
|
|
|
|
|
2023-02-22 01:58:15 +00:00
|
|
|
import flixel.util.FlxSignal.FlxTypedSignal;
|
2023-02-02 23:08:30 +00:00
|
|
|
import flxanimate.FlxAnimate;
|
2023-02-22 01:58:15 +00:00
|
|
|
import flxanimate.FlxAnimate.Settings;
|
2024-03-20 18:37:24 +00:00
|
|
|
import flxanimate.frames.FlxAnimateFrames;
|
|
|
|
import openfl.display.BitmapData;
|
|
|
|
import openfl.utils.Assets;
|
2023-02-02 23:08:30 +00:00
|
|
|
|
|
|
|
/**
|
2023-02-22 01:58:15 +00:00
|
|
|
* A sprite which provides convenience functions for rendering a texture atlas with animations.
|
2023-02-02 23:08:30 +00:00
|
|
|
*/
|
|
|
|
class FlxAtlasSprite extends FlxAnimate
|
|
|
|
{
|
2023-02-22 01:58:15 +00:00
|
|
|
static final SETTINGS:Settings =
|
|
|
|
{
|
|
|
|
// ?ButtonSettings:Map<String, flxanimate.animate.FlxAnim.ButtonSettings>,
|
|
|
|
FrameRate: 24.0,
|
|
|
|
Reversed: false,
|
|
|
|
// ?OnComplete:Void -> Void,
|
2023-05-31 07:03:10 +00:00
|
|
|
ShowPivot: #if debug false #else false #end,
|
2023-02-22 01:58:15 +00:00
|
|
|
Antialiasing: true,
|
2024-02-14 08:27:34 +00:00
|
|
|
ScrollFactor: null,
|
2023-02-22 01:58:15 +00:00
|
|
|
// Offset: new FlxPoint(0, 0), // This is just FlxSprite.offset
|
|
|
|
};
|
|
|
|
|
2023-02-02 23:08:30 +00:00
|
|
|
/**
|
2023-02-22 01:58:15 +00:00
|
|
|
* Signal dispatched when an animation finishes playing.
|
2023-02-02 23:08:30 +00:00
|
|
|
*/
|
2023-02-22 01:58:15 +00:00
|
|
|
public var onAnimationFinish:FlxTypedSignal<String->Void> = new FlxTypedSignal<String->Void>();
|
|
|
|
|
|
|
|
var currentAnimation:String;
|
2023-02-02 23:08:30 +00:00
|
|
|
|
2023-02-22 01:58:15 +00:00
|
|
|
var canPlayOtherAnims:Bool = true;
|
|
|
|
|
2024-03-20 18:37:24 +00:00
|
|
|
public function new(x:Float, y:Float, ?path:String, ?settings:Settings)
|
2023-02-02 23:08:30 +00:00
|
|
|
{
|
2023-04-16 19:33:20 +00:00
|
|
|
if (settings == null) settings = SETTINGS;
|
|
|
|
|
|
|
|
super(x, y, path, settings);
|
2023-02-22 01:58:15 +00:00
|
|
|
|
|
|
|
if (this.anim.curInstance == null)
|
|
|
|
{
|
|
|
|
throw 'FlxAtlasSprite not initialized properly. Are you sure the path (${path}) exists?';
|
|
|
|
}
|
|
|
|
|
|
|
|
onAnimationFinish.add(cleanupAnimation);
|
|
|
|
|
|
|
|
// This defaults the sprite to play the first animation in the atlas,
|
|
|
|
// then pauses it. This ensures symbols are intialized properly.
|
|
|
|
this.anim.play('');
|
|
|
|
this.anim.pause();
|
2023-02-02 23:08:30 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 01:58:15 +00:00
|
|
|
/**
|
|
|
|
* @return A list of all the animations this sprite has available.
|
|
|
|
*/
|
|
|
|
public function listAnimations():Array<String>
|
|
|
|
{
|
2024-02-16 18:08:34 +00:00
|
|
|
if (this.anim == null) return [];
|
2024-02-14 08:27:34 +00:00
|
|
|
return this.anim.getFrameLabels();
|
|
|
|
// return [""];
|
2023-02-22 01:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param id A string ID of the animation.
|
|
|
|
* @return Whether the animation was found on this sprite.
|
|
|
|
*/
|
|
|
|
public function hasAnimation(id:String):Bool
|
|
|
|
{
|
|
|
|
return getLabelIndex(id) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The current animation being played.
|
|
|
|
*/
|
|
|
|
public function getCurrentAnimation():String
|
|
|
|
{
|
|
|
|
return this.currentAnimation;
|
|
|
|
}
|
|
|
|
|
2024-03-24 08:53:05 +00:00
|
|
|
/**
|
|
|
|
* `anim.finished` always returns false on looping animations,
|
|
|
|
* but this function will return true if we are on the last frame of the looping animation.
|
|
|
|
*/
|
|
|
|
public function isLoopFinished():Bool
|
|
|
|
{
|
|
|
|
if (this.anim == null) return false;
|
|
|
|
if (!this.anim.isPlaying) return false;
|
|
|
|
|
|
|
|
// Reverse animation finished.
|
|
|
|
if (this.anim.reversed && this.anim.curFrame == 0) return true;
|
|
|
|
// Forward animation finished.
|
|
|
|
if (!this.anim.reversed && this.anim.curFrame >= (this.anim.length - 1)) return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-02-22 01:58:15 +00:00
|
|
|
/**
|
|
|
|
* Plays an animation.
|
|
|
|
* @param id A string ID of the animation to play.
|
|
|
|
* @param restart Whether to restart the animation if it is already playing.
|
|
|
|
* @param ignoreOther Whether to ignore all other animation inputs, until this one is done playing
|
2024-03-01 13:13:06 +00:00
|
|
|
* @param loop Whether to loop the animation
|
|
|
|
* NOTE: `loop` and `ignoreOther` are not compatible with each other!
|
2023-02-22 01:58:15 +00:00
|
|
|
*/
|
2024-02-14 08:27:34 +00:00
|
|
|
public function playAnimation(id:String, restart:Bool = false, ignoreOther:Bool = false, ?loop:Bool = false):Void
|
2023-02-22 01:58:15 +00:00
|
|
|
{
|
2024-02-14 08:27:34 +00:00
|
|
|
if (loop == null) loop = false;
|
|
|
|
|
2023-02-22 01:58:15 +00:00
|
|
|
// Skip if not allowed to play animations.
|
|
|
|
if ((!canPlayOtherAnims && !ignoreOther)) return;
|
|
|
|
|
|
|
|
if (id == null || id == '') id = this.currentAnimation;
|
|
|
|
|
|
|
|
if (this.currentAnimation == id && !restart)
|
|
|
|
{
|
|
|
|
if (anim.isPlaying)
|
|
|
|
{
|
|
|
|
// Skip if animation is already playing.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Resume animation if it's paused.
|
|
|
|
anim.play('', false, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if the animation doesn't exist
|
|
|
|
if (!hasAnimation(id))
|
|
|
|
{
|
|
|
|
trace('Animation ' + id + ' not found');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-14 08:27:34 +00:00
|
|
|
anim.callback = function(_, frame:Int) {
|
2024-03-03 03:46:13 +00:00
|
|
|
var offset = loop ? 0 : -1;
|
|
|
|
|
|
|
|
if (frame == (anim.getFrameLabel(id).duration + offset) + anim.getFrameLabel(id).index)
|
2024-02-14 08:27:34 +00:00
|
|
|
{
|
2024-03-01 13:13:06 +00:00
|
|
|
if (loop)
|
|
|
|
{
|
|
|
|
playAnimation(id, true, false, true);
|
|
|
|
}
|
2024-02-14 08:27:34 +00:00
|
|
|
else
|
2024-03-01 13:13:06 +00:00
|
|
|
{
|
2024-02-14 08:27:34 +00:00
|
|
|
onAnimationFinish.dispatch(id);
|
2024-03-01 13:13:06 +00:00
|
|
|
}
|
2024-02-14 08:27:34 +00:00
|
|
|
}
|
|
|
|
};
|
2023-02-22 01:58:15 +00:00
|
|
|
|
|
|
|
// Prevent other animations from playing if `ignoreOther` is true.
|
|
|
|
if (ignoreOther) canPlayOtherAnims = false;
|
|
|
|
|
|
|
|
// Move to the first frame of the animation.
|
|
|
|
goToFrameLabel(id);
|
|
|
|
this.currentAnimation = id;
|
|
|
|
}
|
|
|
|
|
2024-02-14 08:27:34 +00:00
|
|
|
override public function update(elapsed:Float)
|
|
|
|
{
|
|
|
|
super.update(elapsed);
|
|
|
|
}
|
|
|
|
|
2023-02-22 01:58:15 +00:00
|
|
|
/**
|
|
|
|
* Stops the current animation.
|
|
|
|
*/
|
|
|
|
public function stopAnimation():Void
|
|
|
|
{
|
|
|
|
if (this.currentAnimation == null) return;
|
|
|
|
|
|
|
|
this.anim.removeAllCallbacksFrom(getNextFrameLabel(this.currentAnimation));
|
|
|
|
|
|
|
|
goToFrameIndex(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addFrameCallback(label:String, callback:Void->Void):Void
|
|
|
|
{
|
|
|
|
var frameLabel = this.anim.getFrameLabel(label);
|
|
|
|
frameLabel.add(callback);
|
|
|
|
}
|
|
|
|
|
2024-02-16 18:08:34 +00:00
|
|
|
function goToFrameLabel(label:String):Void
|
2023-02-22 01:58:15 +00:00
|
|
|
{
|
|
|
|
this.anim.goToFrameLabel(label);
|
|
|
|
}
|
|
|
|
|
2024-02-16 18:08:34 +00:00
|
|
|
function getNextFrameLabel(label:String):String
|
2023-02-22 01:58:15 +00:00
|
|
|
{
|
|
|
|
return listAnimations()[(getLabelIndex(label) + 1) % listAnimations().length];
|
|
|
|
}
|
|
|
|
|
2024-02-16 18:08:34 +00:00
|
|
|
function getLabelIndex(label:String):Int
|
2023-02-22 01:58:15 +00:00
|
|
|
{
|
|
|
|
return listAnimations().indexOf(label);
|
|
|
|
}
|
|
|
|
|
2024-02-16 18:08:34 +00:00
|
|
|
function goToFrameIndex(index:Int):Void
|
2023-02-22 01:58:15 +00:00
|
|
|
{
|
|
|
|
this.anim.curFrame = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cleanupAnimation(_:String):Void
|
|
|
|
{
|
|
|
|
canPlayOtherAnims = true;
|
2024-03-03 03:46:13 +00:00
|
|
|
// this.currentAnimation = null;
|
2024-03-01 13:13:06 +00:00
|
|
|
this.anim.pause();
|
2023-02-22 01:58:15 +00:00
|
|
|
}
|
2023-02-02 23:08:30 +00:00
|
|
|
}
|