diff --git a/src/main/hw/animate/Animate.hx b/src/main/hw/animate/Animate.hx index 34a71ad..af0b4a1 100644 --- a/src/main/hw/animate/Animate.hx +++ b/src/main/hw/animate/Animate.hx @@ -1,35 +1,22 @@ package hw.animate; import flash.display.DisplayObject; -import flash.display.Stage; -import flash.events.Event; import hw.view.IView; +import promhx.Deferred; +import promhx.Promise; class Animate implements IAnimate { public static var defaultDuraion = 300; - private static var running:Array = new Array(); + @:provide private static var runner:AnimateRunner; - 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 view:IView; private var duration:Int; private var startTime:Float; private var progress:Float; private var object(get, null):DisplayObject; + private var deferred:Deferred; public function new(view:IView, duration:Int = -1) { this.view = view; @@ -40,25 +27,22 @@ class Animate implements IAnimate { return cast view.content; } - public function start(callback:IAnimate -> Void, custom:Bool = false):Void { - startTime = Date.now().getTime(); - this.callback = callback; - if (!custom) running.push(this); - update(startTime); + public function start():Promise { + startTime = runner.run(this); + deferred = new Deferred(); + return deferred.promise(); } - private function update(time:Float):Void { + public function update(time:Float):Bool { progress = (time - startTime) / duration; if (progress >= 1) { - running.remove(this); - if (callback != null) { - callback(this); - callback = null; - } + deferred.resolve(this); + return false; } + return true; } public function cancel():Void { - if (!Math.isNaN(startTime)) update(startTime + duration); + progress = startTime + duration; } } diff --git a/src/main/hw/animate/AnimateRunner.hx b/src/main/hw/animate/AnimateRunner.hx new file mode 100644 index 0000000..7bd972e --- /dev/null +++ b/src/main/hw/animate/AnimateRunner.hx @@ -0,0 +1,28 @@ +package hw.animate; + +import flash.events.Event; +import flash.Lib; + +@:provide class AnimateRunner { + + private var animates:Array; + + public function new() { + animates = []; + Lib.current.stage.addEventListener(Event.ENTER_FRAME, _ -> update()); + } + + private function update():Void { + if (animates.length > 0) { + var time = Date.now().getTime(); + animates = animates.filter(animate -> animate.update(time)); + } + } + + public function run(animate:IAnimate):Float { + animates.push(animate); + var time = Date.now().getTime(); + animate.update(time); + return time; + } +} diff --git a/src/main/hw/animate/CircleMaskAnimate.hx b/src/main/hw/animate/CircleMaskAnimate.hx index 3c80044..930dfda 100755 --- a/src/main/hw/animate/CircleMaskAnimate.hx +++ b/src/main/hw/animate/CircleMaskAnimate.hx @@ -5,6 +5,7 @@ import flash.display.Sprite; import hw.animate.Animate; import hw.animate.IAnimate; import hw.view.IView; +import promhx.Promise; class CircleMaskAnimate extends Animate { @@ -19,7 +20,7 @@ class CircleMaskAnimate extends Animate { this.cyrcle = new Sprite(); } - override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { + override public function start():Promise { var width = view.parent.width; var height = view.parent.height; size = Math.sqrt(width * width + height * height); @@ -33,7 +34,7 @@ class CircleMaskAnimate extends Animate { view.content.mask = mask; view.parent.container.addChild(cyrcle); - super.start(callback, custom); + return super.start(); } private function redraw(size:Float, r:Float):Void { @@ -48,15 +49,14 @@ class CircleMaskAnimate extends Animate { cyrcle.graphics.lineStyle(); } - override private function update(time:Float):Void { - super.update(time); - + override public function update(time:Float):Bool { + var result = super.update(time); redraw(size, size * progress); - if (progress >= 1 && view.content.parent != null) { if (view.content.parent.contains(mask)) view.content.parent.removeChild(mask); view.content.mask = null; if (view.content.parent.contains(cyrcle)) view.parent.container.removeChild(cyrcle); } + return result; } } diff --git a/src/main/hw/animate/FadeAnimate.hx b/src/main/hw/animate/FadeAnimate.hx index 3efe072..d73163f 100755 --- a/src/main/hw/animate/FadeAnimate.hx +++ b/src/main/hw/animate/FadeAnimate.hx @@ -2,19 +2,21 @@ package hw.animate; import hw.animate.Animate; import hw.animate.IAnimate; +import promhx.Promise; class FadeAnimate extends Animate { - override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { + override public function start():Promise { object.alpha = 1.0; - super.start(callback, custom); + return super.start(); } - override private function update(time:Float):Void { - super.update(time); + override public function update(time:Float):Bool { + var result = super.update(time); object.alpha = 1 - (progress * 1.0); if (progress >= 1) { object.alpha = 0.0; } + return result; } } diff --git a/src/main/hw/animate/IAnimate.hx b/src/main/hw/animate/IAnimate.hx index bd9f9cf..6326107 100644 --- a/src/main/hw/animate/IAnimate.hx +++ b/src/main/hw/animate/IAnimate.hx @@ -1,10 +1,12 @@ package hw.animate; +import promhx.Promise; + interface IAnimate { - public function start(callback:IAnimate -> Void, custom:Bool = false):Void; + public function start():Promise; public function cancel():Void; - private function update(time:Float):Void; + public function update(time:Float):Bool; } diff --git a/src/main/hw/animate/SlideAnimate.hx b/src/main/hw/animate/SlideAnimate.hx index 774b101..1f17903 100644 --- a/src/main/hw/animate/SlideAnimate.hx +++ b/src/main/hw/animate/SlideAnimate.hx @@ -1,14 +1,17 @@ package hw.animate; +import promhx.Promise; + class SlideAnimate extends Animate { - override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { + override public function start():Promise { object.x = view.x - this.view.width + this.view.width / progress; - super.start(callback, custom); + return super.start(); } - override private function update(time:Float):Void { - super.update(time); + override public function update(time:Float):Bool { + var result = super.update(time); object.x = view.x - this.view.width + this.view.width / Math.min(1, progress); + return result; } } diff --git a/src/main/hw/animate/UnFadeAnimate.hx b/src/main/hw/animate/UnFadeAnimate.hx index 0a0e906..8852690 100755 --- a/src/main/hw/animate/UnFadeAnimate.hx +++ b/src/main/hw/animate/UnFadeAnimate.hx @@ -1,19 +1,21 @@ package hw.animate; import hw.animate.Animate; +import promhx.Promise; class UnFadeAnimate extends Animate { - override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { + override public function start():Promise { object.alpha = 0.0; - super.start(callback, custom); + return super.start(); } - override private function update(time:Float):Void { - super.update(time); + override public function update(time:Float):Bool { + var result = super.update(time); object.alpha = progress * 1.0; if (progress >= 1) { object.alpha = 1.0; } + return result; } } diff --git a/src/main/hw/app/App.hx b/src/main/hw/app/App.hx index d06def1..3bc403c 100644 --- a/src/main/hw/app/App.hx +++ b/src/main/hw/app/App.hx @@ -4,7 +4,6 @@ import flash.display.BitmapData; import flash.display.StageDisplayState; import flash.events.FullScreenEvent; import flash.Lib; -import hw.animate.Animate; import hw.animate.FadeAnimate; import hw.animate.IAnimate; import hw.animate.UnFadeAnimate; @@ -65,7 +64,6 @@ class App { public function new() { Lib.current.stage.stageFocusRect = false; Lib.current.stage.addEventListener(FullScreenEvent.FULL_SCREEN, event -> fullScreenSignal.emit(event.fullScreen)); - Animate.bind(Lib.current.stage); popupManager.showAnimateFactory = createShowAnimate; popupManager.closeAnimateFactory = createCloseAnimate; diff --git a/src/main/hw/view/frame/FrameSwitcher.hx b/src/main/hw/view/frame/FrameSwitcher.hx index 8010a64..2ab5e5a 100755 --- a/src/main/hw/view/frame/FrameSwitcher.hx +++ b/src/main/hw/view/frame/FrameSwitcher.hx @@ -62,7 +62,7 @@ class FrameSwitcher extends GroupView { } animate = buildAnimate(current); if (animate != null && prev != null) { - animate.start(function(_) removePrev(prev)); + animate.start().then(_ -> removePrev(prev)); } else { removePrev(prev); } diff --git a/src/main/hw/view/popup/PopupManager.hx b/src/main/hw/view/popup/PopupManager.hx index f39609f..6dba1e1 100755 --- a/src/main/hw/view/popup/PopupManager.hx +++ b/src/main/hw/view/popup/PopupManager.hx @@ -21,7 +21,7 @@ typedef P = PopupView; public function show(popup:P):Void { root.addView(popup); if (showAnimateFactory != null) { - showAnimateFactory(popup).start(null); + showAnimateFactory(popup).start(); } popups.push(popup); } @@ -29,7 +29,7 @@ typedef P = PopupView; public function close(popup:P):Void { popups.remove(popup); if (closeAnimateFactory != null) { - closeAnimateFactory(popup).start(function(_) remove(popup)); + closeAnimateFactory(popup).start().then(_ -> remove(popup)); } else { remove(popup); }