added animates
This commit is contained in:
53
haxework/animate/Animate.hx
Normal file
53
haxework/animate/Animate.hx
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package haxework.animate;
|
||||||
|
|
||||||
|
import openfl.events.Event;
|
||||||
|
import flash.display.Stage;
|
||||||
|
|
||||||
|
class Animate implements IAnimate {
|
||||||
|
|
||||||
|
private static var running:Array<IAnimate> = new Array<IAnimate>();
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
9
haxework/animate/IAnimate.hx
Normal file
9
haxework/animate/IAnimate.hx
Normal file
@@ -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;
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package haxework.frame;
|
package haxework.frame;
|
||||||
|
|
||||||
|
import haxework.animate.IAnimate;
|
||||||
import flash.display.Sprite;
|
import flash.display.Sprite;
|
||||||
import haxework.gui.IView;
|
import haxework.gui.IView;
|
||||||
import haxework.gui.GroupView;
|
import haxework.gui.GroupView;
|
||||||
@@ -9,27 +10,53 @@ class FrameSwitcher extends GroupView implements IFrameSwitcher<Sprite> {
|
|||||||
public var current(default, null):Null<IView<Dynamic>>;
|
public var current(default, null):Null<IView<Dynamic>>;
|
||||||
private var frames:Map<String, IView<Dynamic>>;
|
private var frames:Map<String, IView<Dynamic>>;
|
||||||
|
|
||||||
|
public var animateFactory(default, default):Class<IAnimate>;
|
||||||
|
private var animate:IAnimate;
|
||||||
|
|
||||||
public function new() {
|
public function new() {
|
||||||
super();
|
super();
|
||||||
frames = new Map<String, IView<Dynamic>>();
|
frames = new Map<String, IView<Dynamic>>();
|
||||||
current = null;
|
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> {
|
public function change(id:String):IView<Dynamic> {
|
||||||
|
var prev = null;
|
||||||
if (current != null) {
|
if (current != null) {
|
||||||
if (current.id == id) return current;
|
if (current.id == id) return current;
|
||||||
var onHideethod:Dynamic = Reflect.field(current, "onHide");
|
prev = current;
|
||||||
if (onHideethod != null) Reflect.callMethod(current, onHideethod, []);
|
|
||||||
removeView(current);
|
|
||||||
}
|
}
|
||||||
current = frames.get(id);
|
current = frames.get(id);
|
||||||
addView(current);
|
addView(current);
|
||||||
if (content.stage != null) content.stage.focus = current.content;
|
if (content.stage != null) content.stage.focus = current.content;
|
||||||
var onShowMethod:Dynamic = Reflect.field(current, "onShow");
|
var onShowMethod:Dynamic = Reflect.field(current, "onShow");
|
||||||
if (onShowMethod != null) Reflect.callMethod(current, onShowMethod, []);
|
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;
|
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>> {
|
override public function set_views(value:Array<IView<Dynamic>>):Array<IView<Dynamic>> {
|
||||||
views = [];
|
views = [];
|
||||||
if (value.length > 0) {
|
if (value.length > 0) {
|
||||||
|
|||||||
@@ -9,11 +9,13 @@ class SpriteView extends View<Sprite> {
|
|||||||
super(new Sprite());
|
super(new Sprite());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*override public function update():Void {
|
#if dev_layout
|
||||||
|
override public function update():Void {
|
||||||
super.update();
|
super.update();
|
||||||
var g:Graphics = content.graphics;
|
var g:Graphics = content.graphics;
|
||||||
g.lineStyle(1, 0x00ff00);
|
g.lineStyle(1, 0x00ff00);
|
||||||
g.drawRect(0, 0, width, height);
|
g.drawRect(0, 0, width, height);
|
||||||
g.lineStyle();
|
g.lineStyle();
|
||||||
}*/
|
}
|
||||||
|
#end
|
||||||
}
|
}
|
||||||
@@ -37,8 +37,10 @@ class TextView extends SpriteView implements ITextView<Sprite, TextField> {
|
|||||||
textField.height = 1;
|
textField.height = 1;
|
||||||
textField.multiline = true;
|
textField.multiline = true;
|
||||||
textField.wordWrap = true;
|
textField.wordWrap = true;
|
||||||
//textField.borderColor = 0x00ff00;
|
#if dev_layout
|
||||||
//textField.border = true;
|
textField.borderColor = 0xff0000;
|
||||||
|
textField.border = true;
|
||||||
|
#end
|
||||||
textFormat = textField.defaultTextFormat;
|
textFormat = textField.defaultTextFormat;
|
||||||
textFormat.font = "Arial";
|
textFormat.font = "Arial";
|
||||||
textFormat.size = 16;
|
textFormat.size = 16;
|
||||||
|
|||||||
Reference in New Issue
Block a user