Skip to content
Open
80 changes: 80 additions & 0 deletions haxe/ui/animation/transition/Transition.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package haxe.ui.animation.transition;

import haxe.ui.core.Component;
class Transition {
public var inAnimations:Array<Animation> = [];
public var outAnimations:Array<Animation> = [];

public var id:String;

public var componentMap:Map<String, Component> = new Map<String, Component>();

public function new() {

}

public function addInAnimation(animation:Animation):Void {
inAnimations.push(animation);
}

public function addOutAnimation(animation:Animation):Void {
outAnimations.push(animation);
}

public function setInComponent(id:String, component:Component) {
componentMap.set(id, component);
}

public function getComponent(id:String):Component {
return componentMap.get(id);
}

public function start(onComplete:Void->Void = null):Void {
var animationCallback:Void->Void = null;

if (onComplete != null) {
var total = inAnimations.length + outAnimations.length;
var current = 0;
animationCallback = function() {
if (++current >= total) {
onComplete();
}
};
}

for (a in inAnimations) {
a.start(animationCallback);
}

for (a in outAnimations) {
a.start(animationCallback);
}
}

public function stop():Void {
for (a in inAnimations) {
a.stop();
}

for (a in outAnimations) {
a.stop();
}
}

public function clone():Transition {
var c:Transition = new Transition();
c.id = this.id;

for (a in inAnimations) {
var ca = a.clone();
c.inAnimations.push(ca);
}

for (a in outAnimations) {
var ca = a.clone();
c.outAnimations.push(ca);
}

return c;
}
}
87 changes: 87 additions & 0 deletions haxe/ui/animation/transition/TransitionManager.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package haxe.ui.animation.transition;

import haxe.ui.core.Component;

class TransitionManager {
private static var _instance:TransitionManager;
public static var instance(get, never):TransitionManager;
private static function get_instance():TransitionManager {
if (_instance == null) {
_instance = new TransitionManager();
}
return _instance;
}

//***********************************************************************************************************
// Instance
//***********************************************************************************************************
private var _transitions:Map<String, Transition> = new Map<String, Transition>();
public function new() {

}

public function registerTransition(id:String, transition:Transition) {
_transitions.set(id, transition);
}

public function run(id:String, inComponents:Map<String, Component> = null, inVars:Map<String, Float> = null,
outComponents:Map<String, Component> = null, outVars:Map<String, Float> = null, complete:Void->Void = null):Transition {
var t:Transition = initTransition(id, inComponents, inVars, outComponents, outVars);
if (t != null) {
t.start(function() {
if (complete != null) {
complete();
}
});
}
return t;
}

private function initTransition(id:String, inComponents:Map<String, Component> = null, inVars:Map<String, Float> = null,
outComponents:Map<String, Component> = null, outVars:Map<String, Float> = null):Transition {
var t:Transition = get(id);
if (t != null) {
if (inComponents != null) {
for (k in inComponents.keys() ) {
for (a in t.inAnimations) {
a.setComponent(k, inComponents.get(k));
}
}
}

if (outComponents != null) {
for (k in outComponents.keys() ) {
for (a in t.outAnimations) {
a.setComponent(k, outComponents.get(k));
}
}
}

if (inVars != null) {
for (k in inVars.keys()) {
for (a in t.inAnimations) {
a.setVar(k, inVars.get(k));
}
}
}

if (outVars != null) {
for (k in outVars.keys()) {
for (a in t.outAnimations) {
a.setVar(k, outVars.get(k));
}
}
}
}
return t;
}

public function get(id:String):Transition {
var t:Transition = _transitions.get(id);
if (t == null) {
return null;
}
return t.clone();
}

}
Loading