2023-02-02 18:08:30 -05:00
|
|
|
package funkin.graphics.adobeanimate;
|
|
|
|
|
2023-02-21 20:58:15 -05:00
|
|
|
import flixel.util.FlxSignal.FlxTypedSignal;
|
2023-02-02 18:08:30 -05:00
|
|
|
import flxanimate.FlxAnimate;
|
2023-02-21 20:58:15 -05:00
|
|
|
import flxanimate.FlxAnimate.Settings;
|
2024-03-20 14:37:24 -04:00
|
|
|
import flxanimate.frames.FlxAnimateFrames;
|
2024-07-10 17:25:52 -04:00
|
|
|
import flixel.graphics.frames.FlxFrame;
|
|
|
|
import flixel.system.FlxAssets.FlxGraphicAsset;
|
2024-03-20 14:37:24 -04:00
|
|
|
import openfl.display.BitmapData;
|
|
|
|
import openfl.utils.Assets;
|
2024-07-10 17:25:52 -04:00
|
|
|
import flixel.math.FlxPoint;
|
2024-08-27 20:48:56 -04:00
|
|
|
import flxanimate.animate.FlxKeyFrame;
|
2023-02-02 18:08:30 -05:00
|
|
|
|
|
|
|
/**
|
2023-02-21 20:58:15 -05:00
|
|
|
* A sprite which provides convenience functions for rendering a texture atlas with animations.
|
2023-02-02 18:08:30 -05:00
|
|
|
*/
|
|
|
|
class FlxAtlasSprite extends FlxAnimate
|
|
|
|
{
|
2023-02-21 20:58:15 -05:00
|
|
|
static final SETTINGS:Settings =
|
|
|
|
{
|
|
|
|
// ?ButtonSettings:Map<String, flxanimate.animate.FlxAnim.ButtonSettings>,
|
|
|
|
FrameRate: 24.0,
|
|
|
|
Reversed: false,
|
|
|
|
// ?OnComplete:Void -> Void,
|
2024-08-26 18:01:36 -04:00
|
|
|
ShowPivot: false,
|
2023-02-21 20:58:15 -05:00
|
|
|
Antialiasing: true,
|
2024-02-14 03:27:34 -05:00
|
|
|
ScrollFactor: null,
|
2023-02-21 20:58:15 -05:00
|
|
|
// Offset: new FlxPoint(0, 0), // This is just FlxSprite.offset
|
|
|
|
};
|
|
|
|
|
2023-02-02 18:08:30 -05:00
|
|
|
/**
|
2024-07-10 17:25:52 -04:00
|
|
|
* Signal dispatched when an animation advances to the next frame.
|
2023-02-02 18:08:30 -05:00
|
|
|
*/
|
2024-07-10 17:25:52 -04:00
|
|
|
public var onAnimationFrame:FlxTypedSignal<String->Int->Void> = new FlxTypedSignal();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signal dispatched when a non-looping animation finishes playing.
|
|
|
|
*/
|
|
|
|
public var onAnimationComplete:FlxTypedSignal<String->Void> = new FlxTypedSignal();
|
|
|
|
|
2023-02-21 20:58:15 -05:00
|
|
|
var currentAnimation:String;
|
2023-02-02 18:08:30 -05:00
|
|
|
|
2023-02-21 20:58:15 -05:00
|
|
|
var canPlayOtherAnims:Bool = true;
|
|
|
|
|
2024-03-20 14:37:24 -04:00
|
|
|
public function new(x:Float, y:Float, ?path:String, ?settings:Settings)
|
2023-02-02 18:08:30 -05:00
|
|
|
{
|
2023-04-16 15:33:20 -04:00
|
|
|
if (settings == null) settings = SETTINGS;
|
|
|
|
|
2024-04-20 23:04:08 -04:00
|
|
|
if (path == null)
|
|
|
|
{
|
|
|
|
throw 'Null path specified for FlxAtlasSprite!';
|
|
|
|
}
|
|
|
|
|
2024-08-26 21:25:04 -04:00
|
|
|
// Validate asset path.
|
|
|
|
if (!Assets.exists('${path}/Animation.json'))
|
|
|
|
{
|
|
|
|
throw 'FlxAtlasSprite does not have an Animation.json file at the specified path (${path})';
|
|
|
|
}
|
|
|
|
|
2023-04-16 15:33:20 -04:00
|
|
|
super(x, y, path, settings);
|
2023-02-21 20:58:15 -05:00
|
|
|
|
2024-07-10 17:25:52 -04:00
|
|
|
if (this.anim.stageInstance == null)
|
2023-02-21 20:58:15 -05:00
|
|
|
{
|
|
|
|
throw 'FlxAtlasSprite not initialized properly. Are you sure the path (${path}) exists?';
|
|
|
|
}
|
|
|
|
|
2024-07-10 17:25:52 -04:00
|
|
|
onAnimationComplete.add(cleanupAnimation);
|
2023-02-21 20:58:15 -05:00
|
|
|
|
|
|
|
// 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();
|
2024-07-10 17:25:52 -04:00
|
|
|
|
|
|
|
this.anim.onComplete.add(_onAnimationComplete);
|
|
|
|
this.anim.onFrame.add(_onAnimationFrame);
|
2023-02-02 18:08:30 -05:00
|
|
|
}
|
|
|
|
|
2023-02-21 20:58:15 -05:00
|
|
|
/**
|
|
|
|
* @return A list of all the animations this sprite has available.
|
|
|
|
*/
|
|
|
|
public function listAnimations():Array<String>
|
|
|
|
{
|
2024-07-10 17:25:52 -04:00
|
|
|
var mainSymbol = this.anim.symbolDictionary[this.anim.stageInstance.symbol.name];
|
|
|
|
if (mainSymbol == null)
|
|
|
|
{
|
|
|
|
FlxG.log.error('FlxAtlasSprite does not have its main symbol!');
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return mainSymbol.getFrameLabels().map(keyFrame -> keyFrame.name).filterNull();
|
2023-02-21 20:58:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param id A string ID of the animation.
|
|
|
|
* @return Whether the animation was found on this sprite.
|
|
|
|
*/
|
|
|
|
public function hasAnimation(id:String):Bool
|
|
|
|
{
|
2024-09-11 14:47:45 -04:00
|
|
|
return getLabelIndex(id) != -1 || anim.symbolDictionary.exists(id);
|
2023-02-21 20:58:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return The current animation being played.
|
|
|
|
*/
|
|
|
|
public function getCurrentAnimation():String
|
|
|
|
{
|
|
|
|
return this.currentAnimation;
|
|
|
|
}
|
|
|
|
|
2024-08-27 20:48:56 -04:00
|
|
|
var _completeAnim:Bool = false;
|
|
|
|
|
|
|
|
var fr:FlxKeyFrame = null;
|
|
|
|
|
|
|
|
var looping:Bool = false;
|
|
|
|
|
2024-09-11 12:29:31 -04:00
|
|
|
public var ignoreExclusionPref:Array<String> = [];
|
|
|
|
|
2023-02-21 20:58:15 -05: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 08:13:06 -05:00
|
|
|
* @param loop Whether to loop the animation
|
2024-07-10 17:25:52 -04:00
|
|
|
* @param startFrame The frame to start the animation on
|
2024-03-01 08:13:06 -05:00
|
|
|
* NOTE: `loop` and `ignoreOther` are not compatible with each other!
|
2023-02-21 20:58:15 -05:00
|
|
|
*/
|
2024-07-10 17:25:52 -04:00
|
|
|
public function playAnimation(id:String, restart:Bool = false, ignoreOther:Bool = false, loop:Bool = false, startFrame:Int = 0):Void
|
2023-02-21 20:58:15 -05:00
|
|
|
{
|
|
|
|
// Skip if not allowed to play animations.
|
2024-09-11 12:29:31 -04:00
|
|
|
if ((!canPlayOtherAnims))
|
|
|
|
{
|
|
|
|
if (this.currentAnimation == id && restart) {}
|
|
|
|
else if (ignoreExclusionPref != null && ignoreExclusionPref.length > 0)
|
|
|
|
{
|
|
|
|
var detected:Bool = false;
|
|
|
|
for (entry in ignoreExclusionPref)
|
|
|
|
{
|
|
|
|
if (StringTools.startsWith(id, entry))
|
|
|
|
{
|
|
|
|
detected = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!detected) return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
|
|
|
}
|
2023-02-21 20:58:15 -05:00
|
|
|
|
2024-09-01 03:22:34 -04:00
|
|
|
if (anim == null) return;
|
|
|
|
|
2023-02-21 20:58:15 -05:00
|
|
|
if (id == null || id == '') id = this.currentAnimation;
|
|
|
|
|
|
|
|
if (this.currentAnimation == id && !restart)
|
|
|
|
{
|
2024-09-11 12:29:31 -04:00
|
|
|
if (!anim.isPlaying)
|
2023-02-21 20:58:15 -05:00
|
|
|
{
|
|
|
|
// Resume animation if it's paused.
|
2024-09-11 14:47:45 -04:00
|
|
|
anim.resume();
|
2023-02-21 20:58:15 -05:00
|
|
|
}
|
2024-09-11 14:47:45 -04:00
|
|
|
|
2024-09-11 12:29:31 -04:00
|
|
|
return;
|
2023-02-21 20:58:15 -05:00
|
|
|
}
|
2024-09-11 14:47:45 -04:00
|
|
|
else if (!hasAnimation(id))
|
2023-02-21 20:58:15 -05:00
|
|
|
{
|
2024-07-03 22:50:39 -04:00
|
|
|
// Skip if the animation doesn't exist
|
2024-09-11 14:47:45 -04:00
|
|
|
trace('Animation ' + id + ' not found');
|
|
|
|
return;
|
2023-02-21 20:58:15 -05:00
|
|
|
}
|
|
|
|
|
2024-09-11 14:47:45 -04:00
|
|
|
this.currentAnimation = id;
|
|
|
|
anim.onComplete.removeAll();
|
|
|
|
anim.onComplete.add(function() {
|
|
|
|
_onAnimationComplete();
|
|
|
|
});
|
|
|
|
|
2024-08-27 20:48:56 -04:00
|
|
|
looping = loop;
|
|
|
|
|
2023-02-21 20:58:15 -05:00
|
|
|
// Prevent other animations from playing if `ignoreOther` is true.
|
|
|
|
if (ignoreOther) canPlayOtherAnims = false;
|
|
|
|
|
|
|
|
// Move to the first frame of the animation.
|
2024-07-10 17:25:52 -04:00
|
|
|
// goToFrameLabel(id);
|
|
|
|
trace('Playing animation $id');
|
2024-09-11 14:47:45 -04:00
|
|
|
if (this.anim.symbolDictionary.exists(id) || (this.anim.getByName(id) != null))
|
|
|
|
{
|
|
|
|
this.anim.play(id, true, false, startFrame);
|
|
|
|
fr = null;
|
|
|
|
}
|
2024-09-01 03:22:34 -04:00
|
|
|
// Only call goToFrameLabel if there is a frame label with that name. This prevents annoying warnings!
|
2024-09-11 14:47:45 -04:00
|
|
|
if (getFrameLabelNames().indexOf(id) != -1)
|
2024-09-01 03:22:34 -04:00
|
|
|
{
|
|
|
|
goToFrameLabel(id);
|
|
|
|
fr = anim.getFrameLabel(id);
|
2024-09-11 12:29:31 -04:00
|
|
|
anim.curFrame += startFrame;
|
|
|
|
}
|
2023-02-21 20:58:15 -05:00
|
|
|
}
|
|
|
|
|
2024-02-14 03:27:34 -05:00
|
|
|
override public function update(elapsed:Float)
|
|
|
|
{
|
|
|
|
super.update(elapsed);
|
|
|
|
}
|
|
|
|
|
2024-07-10 17:25:52 -04:00
|
|
|
/**
|
|
|
|
* Returns true if the animation has finished playing.
|
|
|
|
* Never true if animation is configured to loop.
|
|
|
|
*/
|
|
|
|
public function isAnimationFinished():Bool
|
|
|
|
{
|
|
|
|
return this.anim.finished;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the animation has reached the last frame.
|
|
|
|
* Can be true even if animation is configured to loop.
|
|
|
|
*/
|
|
|
|
public function isLoopComplete():Bool
|
|
|
|
{
|
2024-09-01 03:22:34 -04:00
|
|
|
if (this.anim == null) return false;
|
|
|
|
if (!this.anim.isPlaying) return false;
|
2024-09-09 02:23:46 -04:00
|
|
|
|
|
|
|
if (fr != null) return (anim.reversed && anim.curFrame < fr.index || !anim.reversed && anim.curFrame >= (fr.index + fr.duration));
|
|
|
|
|
2024-07-10 17:25:52 -04:00
|
|
|
return (anim.reversed && anim.curFrame == 0 || !(anim.reversed) && (anim.curFrame) >= (anim.length - 1));
|
|
|
|
}
|
|
|
|
|
2023-02-21 20:58:15 -05: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 13:08:34 -05:00
|
|
|
function goToFrameLabel(label:String):Void
|
2023-02-21 20:58:15 -05:00
|
|
|
{
|
|
|
|
this.anim.goToFrameLabel(label);
|
|
|
|
}
|
|
|
|
|
2024-09-01 03:22:34 -04:00
|
|
|
function getFrameLabelNames(?layer:haxe.extern.EitherType<Int, String> = null)
|
|
|
|
{
|
|
|
|
var labels = this.anim.getFrameLabels(layer);
|
|
|
|
var array = [];
|
|
|
|
for (label in labels)
|
|
|
|
{
|
|
|
|
array.push(label.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2024-02-16 13:08:34 -05:00
|
|
|
function getNextFrameLabel(label:String):String
|
2023-02-21 20:58:15 -05:00
|
|
|
{
|
|
|
|
return listAnimations()[(getLabelIndex(label) + 1) % listAnimations().length];
|
|
|
|
}
|
|
|
|
|
2024-02-16 13:08:34 -05:00
|
|
|
function getLabelIndex(label:String):Int
|
2023-02-21 20:58:15 -05:00
|
|
|
{
|
|
|
|
return listAnimations().indexOf(label);
|
|
|
|
}
|
|
|
|
|
2024-02-16 13:08:34 -05:00
|
|
|
function goToFrameIndex(index:Int):Void
|
2023-02-21 20:58:15 -05:00
|
|
|
{
|
|
|
|
this.anim.curFrame = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cleanupAnimation(_:String):Void
|
|
|
|
{
|
|
|
|
canPlayOtherAnims = true;
|
2024-03-02 22:46:13 -05:00
|
|
|
// this.currentAnimation = null;
|
2024-03-01 08:13:06 -05:00
|
|
|
this.anim.pause();
|
2023-02-21 20:58:15 -05:00
|
|
|
}
|
2024-07-10 17:25:52 -04:00
|
|
|
|
|
|
|
function _onAnimationFrame(frame:Int):Void
|
|
|
|
{
|
|
|
|
if (currentAnimation != null)
|
|
|
|
{
|
|
|
|
onAnimationFrame.dispatch(currentAnimation, frame);
|
2024-08-27 20:48:56 -04:00
|
|
|
|
2024-09-09 02:23:46 -04:00
|
|
|
if (isLoopComplete())
|
2024-08-27 20:48:56 -04:00
|
|
|
{
|
|
|
|
anim.pause();
|
|
|
|
_onAnimationComplete();
|
2024-09-11 14:47:45 -04:00
|
|
|
|
2024-08-27 20:48:56 -04:00
|
|
|
if (looping)
|
|
|
|
{
|
|
|
|
anim.curFrame = (fr != null) ? fr.index : 0;
|
|
|
|
anim.resume();
|
|
|
|
}
|
2024-09-11 14:47:45 -04:00
|
|
|
else if (fr != null)
|
2024-08-27 20:48:56 -04:00
|
|
|
{
|
|
|
|
anim.curFrame--;
|
|
|
|
}
|
|
|
|
}
|
2024-07-10 17:25:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _onAnimationComplete():Void
|
|
|
|
{
|
|
|
|
if (currentAnimation != null)
|
|
|
|
{
|
2024-09-09 19:11:41 -04:00
|
|
|
onAnimationComplete.dispatch(currentAnimation);
|
2024-07-10 17:25:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var prevFrames:Map<Int, FlxFrame> = [];
|
|
|
|
|
|
|
|
public function replaceFrameGraphic(index:Int, ?graphic:FlxGraphicAsset):Void
|
|
|
|
{
|
|
|
|
if (graphic == null || !Assets.exists(graphic))
|
|
|
|
{
|
|
|
|
var prevFrame:Null<FlxFrame> = prevFrames.get(index);
|
|
|
|
if (prevFrame == null) return;
|
|
|
|
|
|
|
|
prevFrame.copyTo(frames.getByIndex(index));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var prevFrame:FlxFrame = prevFrames.get(index) ?? frames.getByIndex(index).copyTo();
|
|
|
|
prevFrames.set(index, prevFrame);
|
|
|
|
|
|
|
|
var frame = FlxG.bitmap.add(graphic).imageFrame.frame;
|
|
|
|
frame.copyTo(frames.getByIndex(index));
|
|
|
|
|
|
|
|
// Additional sizing fix.
|
|
|
|
@:privateAccess
|
|
|
|
if (true)
|
|
|
|
{
|
|
|
|
var frame = frames.getByIndex(index);
|
|
|
|
frame.tileMatrix[0] = prevFrame.frame.width / frame.frame.width;
|
|
|
|
frame.tileMatrix[3] = prevFrame.frame.height / frame.frame.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBasePosition():Null<FlxPoint>
|
|
|
|
{
|
2024-08-22 17:36:43 -04:00
|
|
|
// var stagePos = new FlxPoint(anim.stageInstance.matrix.tx, anim.stageInstance.matrix.ty);
|
2024-07-10 17:25:52 -04:00
|
|
|
var instancePos = new FlxPoint(anim.curInstance.matrix.tx, anim.curInstance.matrix.ty);
|
|
|
|
var firstElement = anim.curSymbol.timeline?.get(0)?.get(0)?.get(0);
|
|
|
|
if (firstElement == null) return instancePos;
|
|
|
|
var firstElementPos = new FlxPoint(firstElement.matrix.tx, firstElement.matrix.ty);
|
|
|
|
|
|
|
|
return instancePos + firstElementPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPivotPosition():Null<FlxPoint>
|
|
|
|
{
|
|
|
|
return anim.curInstance.symbol.transformationPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override function destroy():Void
|
|
|
|
{
|
|
|
|
for (prevFrameId in prevFrames.keys())
|
|
|
|
{
|
|
|
|
replaceFrameGraphic(prevFrameId, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
super.destroy();
|
|
|
|
}
|
2023-02-02 18:08:30 -05:00
|
|
|
}
|