-
-
Save markknol/a7c17a1cc3d60e83461b to your computer and use it in GitHub Desktop.
Flambe Tweener class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package temple.tween; | |
import flambe.animation.AnimatedFloat; | |
import flambe.animation.Ease; | |
import flambe.display.Sprite; | |
import flambe.Entity; | |
import flambe.script.Action; | |
import flambe.script.AnimateTo; | |
import flambe.script.CallFunction; | |
import flambe.script.Delay; | |
import flambe.script.Parallel; | |
import flambe.script.Script; | |
import flambe.script.Sequence; | |
import flambe.System; | |
import flambe.util.Disposable; | |
/** | |
* Wrapper class around Flambe's Script/Sequence/Actions classes to simplify delayed tweens with callbacks | |
* @author Mark Knol [mediamonks] | |
*/ | |
class Tweener | |
{ | |
/** | |
* | |
* @param object | |
* @param seconds | |
* @param params object with references to AnimatedFloat's | |
* @param delay in seconds | |
* @param ?onComplete | |
* @param ?easing | |
*/ | |
public static function to(object:Dynamic, seconds:Float, tweenProps:Dynamic, delay:Float = 0, onComplete:Void->Void = null, ?easing:EaseFunction) :Disposable | |
{ | |
Ease; | |
var sequence:Sequence = new Sequence(); | |
if (delay < 0) delay = 0; | |
if (seconds == 0 && delay == 0) | |
{ | |
set(object, tweenProps, onComplete); | |
return null; | |
} | |
else if (seconds == 0 && delay > 0) | |
{ | |
var entity:Entity = new Entity(); | |
if (delay != 0) sequence.add(new Delay(delay)); | |
sequence.add(new CallFunction(function() { | |
set(object, tweenProps, onComplete); | |
entity.dispose(); | |
})); | |
var script:Script = new Script(); | |
System.root.addChild(entity.add(script)); | |
return script.run(sequence); | |
} | |
else | |
{ | |
if (delay > 0) sequence.add(new Delay(delay)); | |
var parallelActions = new Array<Action>(); | |
var fields = Reflect.fields(tweenProps); | |
for( param in fields) | |
{ | |
var property = Reflect.getProperty(object, param); | |
parallelActions.push(new AnimateTo(cast property, Reflect.field(tweenProps, param), seconds, easing)); | |
} | |
var parallel:Parallel = new Parallel(parallelActions); | |
sequence.add(parallel); | |
var entity:Entity = new Entity(); | |
sequence.add(new CallFunction(function() { | |
if (onComplete != null) onComplete(); | |
entity.dispose(); | |
} )); | |
var script:Script = new Script(); | |
System.root.addChild(entity.add(script)); | |
return script.run(sequence); | |
} | |
} | |
public static function delay(onComplete:Void->Void = null, delay:Float = 0) :Disposable | |
{ | |
if (delay < 0) delay = 0; | |
var entity = new Entity(); | |
var sequence:Sequence = new Sequence(); | |
sequence.add(new Delay(delay)); | |
sequence.add(new CallFunction(onComplete)); | |
sequence.add(new CallFunction(entity.dispose)); | |
var script:Script = new Script(); | |
System.root.addChild(entity.add(script)); | |
return script.run(sequence); | |
} | |
public static function fromTo(object:Dynamic, seconds:Float, fromProps:Dynamic, toProps:Dynamic, delay:Float = 0, onComplete:Void->Void = null, ?easing:EaseFunction) | |
{ | |
set(object, fromProps, null); | |
return to(object, seconds, toProps, delay, onComplete, easing); | |
} | |
public static function set(object:Dynamic, params:Dynamic, ?onComplete:Void->Void) :Void | |
{ | |
for(param in Reflect.fields(params)) | |
{ | |
var property = Reflect.getProperty(object, param); | |
cast(property, AnimatedFloat)._ = Reflect.field(params, param); | |
} | |
if (onComplete != null) onComplete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment