[animate] update

This commit is contained in:
2020-03-24 23:37:08 +03:00
parent 279baa1113
commit fe60e78b74
10 changed files with 73 additions and 54 deletions

View File

@@ -1,35 +1,22 @@
package hw.animate; package hw.animate;
import flash.display.DisplayObject; import flash.display.DisplayObject;
import flash.display.Stage;
import flash.events.Event;
import hw.view.IView; import hw.view.IView;
import promhx.Deferred;
import promhx.Promise;
class Animate implements IAnimate { class Animate implements IAnimate {
public static var defaultDuraion = 300; public static var defaultDuraion = 300;
private static var running:Array<IAnimate> = new Array<IAnimate>(); @: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<Dynamic>; private var view:IView<Dynamic>;
private var duration:Int; private var duration:Int;
private var startTime:Float; private var startTime:Float;
private var progress:Float; private var progress:Float;
private var object(get, null):DisplayObject; private var object(get, null):DisplayObject;
private var deferred:Deferred<IAnimate>;
public function new(view:IView<Dynamic>, duration:Int = -1) { public function new(view:IView<Dynamic>, duration:Int = -1) {
this.view = view; this.view = view;
@@ -40,25 +27,22 @@ class Animate implements IAnimate {
return cast view.content; return cast view.content;
} }
public function start(callback:IAnimate -> Void, custom:Bool = false):Void { public function start():Promise<IAnimate> {
startTime = Date.now().getTime(); startTime = runner.run(this);
this.callback = callback; deferred = new Deferred();
if (!custom) running.push(this); return deferred.promise();
update(startTime);
} }
private function update(time:Float):Void { public function update(time:Float):Bool {
progress = (time - startTime) / duration; progress = (time - startTime) / duration;
if (progress >= 1) { if (progress >= 1) {
running.remove(this); deferred.resolve(this);
if (callback != null) { return false;
callback(this);
callback = null;
}
} }
return true;
} }
public function cancel():Void { public function cancel():Void {
if (!Math.isNaN(startTime)) update(startTime + duration); progress = startTime + duration;
} }
} }

View File

@@ -0,0 +1,28 @@
package hw.animate;
import flash.events.Event;
import flash.Lib;
@:provide class AnimateRunner {
private var animates:Array<IAnimate>;
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;
}
}

View File

@@ -5,6 +5,7 @@ import flash.display.Sprite;
import hw.animate.Animate; import hw.animate.Animate;
import hw.animate.IAnimate; import hw.animate.IAnimate;
import hw.view.IView; import hw.view.IView;
import promhx.Promise;
class CircleMaskAnimate extends Animate { class CircleMaskAnimate extends Animate {
@@ -19,7 +20,7 @@ class CircleMaskAnimate extends Animate {
this.cyrcle = new Sprite(); this.cyrcle = new Sprite();
} }
override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { override public function start():Promise<IAnimate> {
var width = view.parent.width; var width = view.parent.width;
var height = view.parent.height; var height = view.parent.height;
size = Math.sqrt(width * width + height * height); size = Math.sqrt(width * width + height * height);
@@ -33,7 +34,7 @@ class CircleMaskAnimate extends Animate {
view.content.mask = mask; view.content.mask = mask;
view.parent.container.addChild(cyrcle); view.parent.container.addChild(cyrcle);
super.start(callback, custom); return super.start();
} }
private function redraw(size:Float, r:Float):Void { private function redraw(size:Float, r:Float):Void {
@@ -48,15 +49,14 @@ class CircleMaskAnimate extends Animate {
cyrcle.graphics.lineStyle(); cyrcle.graphics.lineStyle();
} }
override private function update(time:Float):Void { override public function update(time:Float):Bool {
super.update(time); var result = super.update(time);
redraw(size, size * progress); redraw(size, size * progress);
if (progress >= 1 && view.content.parent != null) { if (progress >= 1 && view.content.parent != null) {
if (view.content.parent.contains(mask)) view.content.parent.removeChild(mask); if (view.content.parent.contains(mask)) view.content.parent.removeChild(mask);
view.content.mask = null; view.content.mask = null;
if (view.content.parent.contains(cyrcle)) view.parent.container.removeChild(cyrcle); if (view.content.parent.contains(cyrcle)) view.parent.container.removeChild(cyrcle);
} }
return result;
} }
} }

View File

@@ -2,19 +2,21 @@ package hw.animate;
import hw.animate.Animate; import hw.animate.Animate;
import hw.animate.IAnimate; import hw.animate.IAnimate;
import promhx.Promise;
class FadeAnimate extends Animate { class FadeAnimate extends Animate {
override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { override public function start():Promise<IAnimate> {
object.alpha = 1.0; object.alpha = 1.0;
super.start(callback, custom); return super.start();
} }
override private function update(time:Float):Void { override public function update(time:Float):Bool {
super.update(time); var result = super.update(time);
object.alpha = 1 - (progress * 1.0); object.alpha = 1 - (progress * 1.0);
if (progress >= 1) { if (progress >= 1) {
object.alpha = 0.0; object.alpha = 0.0;
} }
return result;
} }
} }

View File

@@ -1,10 +1,12 @@
package hw.animate; package hw.animate;
import promhx.Promise;
interface IAnimate { interface IAnimate {
public function start(callback:IAnimate -> Void, custom:Bool = false):Void; public function start():Promise<IAnimate>;
public function cancel():Void; public function cancel():Void;
private function update(time:Float):Void; public function update(time:Float):Bool;
} }

View File

@@ -1,14 +1,17 @@
package hw.animate; package hw.animate;
import promhx.Promise;
class SlideAnimate extends Animate { class SlideAnimate extends Animate {
override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { override public function start():Promise<IAnimate> {
object.x = view.x - this.view.width + this.view.width / progress; 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 { override public function update(time:Float):Bool {
super.update(time); var result = super.update(time);
object.x = view.x - this.view.width + this.view.width / Math.min(1, progress); object.x = view.x - this.view.width + this.view.width / Math.min(1, progress);
return result;
} }
} }

View File

@@ -1,19 +1,21 @@
package hw.animate; package hw.animate;
import hw.animate.Animate; import hw.animate.Animate;
import promhx.Promise;
class UnFadeAnimate extends Animate { class UnFadeAnimate extends Animate {
override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { override public function start():Promise<IAnimate> {
object.alpha = 0.0; object.alpha = 0.0;
super.start(callback, custom); return super.start();
} }
override private function update(time:Float):Void { override public function update(time:Float):Bool {
super.update(time); var result = super.update(time);
object.alpha = progress * 1.0; object.alpha = progress * 1.0;
if (progress >= 1) { if (progress >= 1) {
object.alpha = 1.0; object.alpha = 1.0;
} }
return result;
} }
} }

View File

@@ -4,7 +4,6 @@ import flash.display.BitmapData;
import flash.display.StageDisplayState; import flash.display.StageDisplayState;
import flash.events.FullScreenEvent; import flash.events.FullScreenEvent;
import flash.Lib; import flash.Lib;
import hw.animate.Animate;
import hw.animate.FadeAnimate; import hw.animate.FadeAnimate;
import hw.animate.IAnimate; import hw.animate.IAnimate;
import hw.animate.UnFadeAnimate; import hw.animate.UnFadeAnimate;
@@ -65,7 +64,6 @@ class App {
public function new() { public function new() {
Lib.current.stage.stageFocusRect = false; Lib.current.stage.stageFocusRect = false;
Lib.current.stage.addEventListener(FullScreenEvent.FULL_SCREEN, event -> fullScreenSignal.emit(event.fullScreen)); Lib.current.stage.addEventListener(FullScreenEvent.FULL_SCREEN, event -> fullScreenSignal.emit(event.fullScreen));
Animate.bind(Lib.current.stage);
popupManager.showAnimateFactory = createShowAnimate; popupManager.showAnimateFactory = createShowAnimate;
popupManager.closeAnimateFactory = createCloseAnimate; popupManager.closeAnimateFactory = createCloseAnimate;

View File

@@ -62,7 +62,7 @@ class FrameSwitcher extends GroupView {
} }
animate = buildAnimate(current); animate = buildAnimate(current);
if (animate != null && prev != null) { if (animate != null && prev != null) {
animate.start(function(_) removePrev(prev)); animate.start().then(_ -> removePrev(prev));
} else { } else {
removePrev(prev); removePrev(prev);
} }

View File

@@ -21,7 +21,7 @@ typedef P = PopupView<Dynamic>;
public function show(popup:P):Void { public function show(popup:P):Void {
root.addView(popup); root.addView(popup);
if (showAnimateFactory != null) { if (showAnimateFactory != null) {
showAnimateFactory(popup).start(null); showAnimateFactory(popup).start();
} }
popups.push(popup); popups.push(popup);
} }
@@ -29,7 +29,7 @@ typedef P = PopupView<Dynamic>;
public function close(popup:P):Void { public function close(popup:P):Void {
popups.remove(popup); popups.remove(popup);
if (closeAnimateFactory != null) { if (closeAnimateFactory != null) {
closeAnimateFactory(popup).start(function(_) remove(popup)); closeAnimateFactory(popup).start().then(_ -> remove(popup));
} else { } else {
remove(popup); remove(popup);
} }