diff --git a/haxework/animate/Animate.hx b/haxework/animate/Animate.hx new file mode 100644 index 0000000..2e78742 --- /dev/null +++ b/haxework/animate/Animate.hx @@ -0,0 +1,53 @@ +package haxework.animate; + +import openfl.events.Event; +import flash.display.Stage; + +class Animate implements IAnimate { + + private static var running:Array = new Array(); + + public static function bind(stage:Stage):Void { + stage.addEventListener(Event.ENTER_FRAME, function(_) { + Animate.updateAll(); + }); + } + + public static function updateAll():Void { + if (running.length > 0) { + var time = Date.now().getTime(); + for (animate in running) animate.update(time); + } + } + + private var callback:Animate -> Void; + private var duration:Int; + private var startTime:Float; + private var progress:Float; + + public function new(duration:Int) { + this.duration = duration; + } + + public function start(callback:IAnimate -> Void, ?custom:Bool = false):Void { + startTime = Date.now().getTime(); + this.callback = callback; + if (!custom) running.push(this); + update(startTime); + } + + private function update(time:Float):Void { + progress = (time - startTime) / duration; + if (progress >= 1) { + running.remove(this); + if (callback != null) { + callback(this); + callback = null; + } + } + } + + public function cancel():Void { + if (!Math.isNaN(startTime)) update(startTime + duration); + } +} diff --git a/haxework/animate/IAnimate.hx b/haxework/animate/IAnimate.hx new file mode 100644 index 0000000..ae4bb26 --- /dev/null +++ b/haxework/animate/IAnimate.hx @@ -0,0 +1,9 @@ +package haxework.animate; + +interface IAnimate { + + public function start(callback:IAnimate->Void, ?custom:Bool = false):Void; + public function cancel():Void; + + private function update(time:Float):Void; +} \ No newline at end of file diff --git a/haxework/frame/FrameSwitcher.hx b/haxework/frame/FrameSwitcher.hx index 062fbf7..7206bc8 100755 --- a/haxework/frame/FrameSwitcher.hx +++ b/haxework/frame/FrameSwitcher.hx @@ -1,5 +1,6 @@ package haxework.frame; +import haxework.animate.IAnimate; import flash.display.Sprite; import haxework.gui.IView; import haxework.gui.GroupView; @@ -9,27 +10,53 @@ class FrameSwitcher extends GroupView implements IFrameSwitcher { public var current(default, null):Null>; private var frames:Map>; + public var animateFactory(default, default):Class; + private var animate:IAnimate; + public function new() { super(); frames = new Map>(); current = null; } + private function buildAnimate(view:IView):Null { + if (animateFactory != null) { + return Type.createInstance(animateFactory, [view]); + } + return null; + } + public function change(id:String):IView { + var prev = null; if (current != null) { if (current.id == id) return current; - var onHideethod:Dynamic = Reflect.field(current, "onHide"); - if (onHideethod != null) Reflect.callMethod(current, onHideethod, []); - removeView(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>):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>):Array> { views = []; if (value.length > 0) { diff --git a/haxework/gui/SpriteView.hx b/haxework/gui/SpriteView.hx index 817aa07..60bb9d8 100755 --- a/haxework/gui/SpriteView.hx +++ b/haxework/gui/SpriteView.hx @@ -9,11 +9,13 @@ class SpriteView extends View { super(new Sprite()); } - /*override public function update():Void { + #if dev_layout + override public function update():Void { super.update(); var g:Graphics = content.graphics; g.lineStyle(1, 0x00ff00); g.drawRect(0, 0, width, height); g.lineStyle(); - }*/ + } + #end } \ No newline at end of file diff --git a/haxework/gui/TextView.hx b/haxework/gui/TextView.hx index df8ecbc..b0fb901 100755 --- a/haxework/gui/TextView.hx +++ b/haxework/gui/TextView.hx @@ -37,8 +37,10 @@ class TextView extends SpriteView implements ITextView { textField.height = 1; textField.multiline = true; textField.wordWrap = true; - //textField.borderColor = 0x00ff00; - //textField.border = true; + #if dev_layout + textField.borderColor = 0xff0000; + textField.border = true; + #end textFormat = textField.defaultTextFormat; textFormat.font = "Arial"; textFormat.size = 16;