Files
haxework/haxework/frame/FrameSwitcher.hx
2015-05-20 12:27:06 +03:00

71 lines
1.9 KiB
Haxe
Executable File

package haxework.frame;
import haxework.animate.IAnimate;
import flash.display.Sprite;
import haxework.gui.IView;
import haxework.gui.GroupView;
class FrameSwitcher extends GroupView implements IFrameSwitcher<Sprite> {
public var current(default, null):Null<IView<Dynamic>>;
private var frames:Map<String, IView<Dynamic>>;
public var animateFactory(default, default):Class<IAnimate>;
private var animate:IAnimate;
public function new() {
super();
frames = new Map<String, IView<Dynamic>>();
current = null;
}
private function buildAnimate(view:IView<Dynamic>):Null<IAnimate> {
if (animateFactory != null) {
return Type.createInstance(animateFactory, [view]);
}
return null;
}
public function change(id:String):IView<Dynamic> {
var prev = null;
if (current != null) {
if (current.id == id) return current;
prev = current;
}
current = frames.get(id);
addView(current);
if (content.stage != null) content.stage.focus = current.content;
var onShowMethod:Dynamic = Reflect.field(current, "onShow");
if (onShowMethod != null) Reflect.callMethod(current, onShowMethod, []);
if (animate != null) animate.cancel();
animate = buildAnimate(current);
if (animate != null && prev != null) {
animate.start(function(_) {
removePrev(prev);
});
} else {
removePrev(prev);
}
return current;
}
private function removePrev(prev:Null<IView<Dynamic>>):Void {
if (prev != null) {
var onHideMethod:Dynamic = Reflect.field(prev, "onHide");
if (onHideMethod != null) Reflect.callMethod(prev, onHideMethod, []);
removeView(prev);
}
}
override public function set_views(value:Array<IView<Dynamic>>):Array<IView<Dynamic>> {
views = [];
if (value.length > 0) {
for (view in value) {
view.pWidth = 100;
view.pHeight = 100;
frames.set(view.id, view);
}
}
return value;
}
}