added batchloader
This commit is contained in:
@@ -12,9 +12,11 @@ import haxework.gui.TextView;
|
||||
import haxework.gui.LabelView;
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.gui.ToggleButtonView;
|
||||
import haxework.gui.ProgressView;
|
||||
import haxework.gui.skin.ColorSkin;
|
||||
import haxework.gui.skin.ButtonColorSkin;
|
||||
import haxework.gui.skin.ButtonBitmapSkin;
|
||||
import haxework.gui.skin.ProgressSkin;
|
||||
|
||||
import haxework.frame.FrameSwitcher;
|
||||
|
||||
@@ -49,7 +51,9 @@ class GuiBuilder {
|
||||
value = Type.createEnum(e, a[1]);
|
||||
} else if (c == "@") {
|
||||
var a:Array<String> = s.substr(1).split(":");
|
||||
value = Reflect.field(Provider.get(IResources), a[0]).get(a[1]);
|
||||
//value = Reflect.field(Provider.get(IResources), a[0]).get(a[1]);
|
||||
Reflect.field(Provider.get(IResources), a[0]).bind(a[1], object, field);
|
||||
continue;
|
||||
} else if (~/0x[A-Fa-f\d]{6}/.match(value)) {
|
||||
value = Std.parseInt(value);
|
||||
}
|
||||
|
||||
29
haxework/gui/ProgressView.hx
Executable file
29
haxework/gui/ProgressView.hx
Executable file
@@ -0,0 +1,29 @@
|
||||
package haxework.gui;
|
||||
|
||||
class ProgressView extends View {
|
||||
|
||||
public var value(default, set):Int;
|
||||
public var max(default, set):Int;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
value = 0;
|
||||
max = 1;
|
||||
}
|
||||
|
||||
private function set_value(value:Int):Int {
|
||||
if (this.value != value) {
|
||||
this.value = value;
|
||||
invalidate();
|
||||
}
|
||||
return this.value;
|
||||
}
|
||||
|
||||
private function set_max(value:Int):Int {
|
||||
if (max != value) {
|
||||
max = value;
|
||||
invalidate();
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
@@ -261,9 +261,13 @@ class Updater {
|
||||
}
|
||||
|
||||
public function update(?_):Void {
|
||||
try {
|
||||
while (invalidated.length > 0) {
|
||||
invalidated.shift().update();
|
||||
}
|
||||
} catch (error:Dynamic) {
|
||||
L.e("UPDATE", "", error);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
21
haxework/gui/skin/ProgressSkin.hx
Executable file
21
haxework/gui/skin/ProgressSkin.hx
Executable file
@@ -0,0 +1,21 @@
|
||||
package haxework.gui.skin;
|
||||
|
||||
import flash.display.Graphics;
|
||||
import flash.display.Sprite;
|
||||
import haxework.gui.skin.ISkin;
|
||||
|
||||
class ProgressSkin implements ISkin<Sprite, ProgressView> {
|
||||
|
||||
public var foreColor:Int;
|
||||
public var backColor:Int;
|
||||
|
||||
public function draw(view:ProgressView):Void {
|
||||
var graphics:Graphics = view.content.graphics;
|
||||
graphics.clear();
|
||||
graphics.beginFill(backColor);
|
||||
graphics.drawRect(0, 0, view.width, view.height);
|
||||
graphics.beginFill(foreColor);
|
||||
graphics.drawRect(0, 0, view.width * (view.value / view.max), view.height);
|
||||
graphics.endFill();
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ class BaseLoader<T> implements ILoader<T> {
|
||||
this.data = data;
|
||||
callback = new Callback<T>();
|
||||
var url:String = this.url;
|
||||
trace(prepareUrl(url));
|
||||
//trace(prepareUrl(url));
|
||||
internalRequest(prepareUrl(url));
|
||||
return callback;
|
||||
}
|
||||
|
||||
52
haxework/net/BatchLoader.hx
Executable file
52
haxework/net/BatchLoader.hx
Executable file
@@ -0,0 +1,52 @@
|
||||
package haxework.net;
|
||||
|
||||
import haxework.net.callback.Callback;
|
||||
import haxework.net.callback.ICallback;
|
||||
import haxework.net.ILoader.Method;
|
||||
import flash.events.Event;
|
||||
import flash.net.URLLoader;
|
||||
|
||||
class BatchLoader<T> {
|
||||
|
||||
public var factory:Class<ILoader<T>>;
|
||||
|
||||
public function new(factory:Class<ILoader<T>>) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public function GET(urls:Array<String>):ICallback<Array<T>> {
|
||||
var callbacks:Array<ICallback<T>> = urls.map(function(url:String):ICallback<T> {
|
||||
var loader:ILoader<T> = Type.createInstance(factory, []);
|
||||
return loader.GET(url);
|
||||
});
|
||||
return new BatchCallback(callbacks);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class BatchCallback<T> extends Callback<Array<T>> {
|
||||
|
||||
private var data:Array<T>;
|
||||
private var counter:Int;
|
||||
|
||||
public function new(callbacks:Array<ICallback<T>>) {
|
||||
super();
|
||||
data = new Array<T>();
|
||||
counter = callbacks.length;
|
||||
for (i in 0...callbacks.length) {
|
||||
register(callbacks[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
private function register(callback:ICallback<T>, index:Int):Void {
|
||||
callback
|
||||
.success(function(d:T):Void {
|
||||
data[index] = d;
|
||||
if (--counter == 0) callSuccess(data);
|
||||
})
|
||||
.fail(function(error:Dynamic):Void {
|
||||
L.e("BatchLoader", "", error);
|
||||
if (--counter == 0) callSuccess(data);
|
||||
});
|
||||
}
|
||||
}
|
||||
11
haxework/net/XmlLoader.hx
Executable file
11
haxework/net/XmlLoader.hx
Executable file
@@ -0,0 +1,11 @@
|
||||
package haxework.net;
|
||||
|
||||
import flash.events.Event;
|
||||
import flash.net.URLLoader;
|
||||
|
||||
class XmlLoader extends BaseURLLoader<Xml> {
|
||||
|
||||
override private function extrudeResult(e:Event):Xml {
|
||||
return Xml.parse(Std.string(cast(e.currentTarget, URLLoader).data));
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ class Callback<T> implements ICallback<T> {
|
||||
try {
|
||||
if (_fail != null) _fail(error);
|
||||
} catch (error:Dynamic) {
|
||||
trace(error);
|
||||
L.d("Callback", "", error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,49 @@
|
||||
package haxework.resources;
|
||||
|
||||
import haxework.core.Tuple;
|
||||
import haxe.ds.StringMap;
|
||||
import flash.display.BitmapData;
|
||||
|
||||
typedef F = Tuple2<Dynamic, String>
|
||||
|
||||
class ResMap<T> extends StringMap<T> {
|
||||
|
||||
private var listeners:Map<String, Array<F>>;
|
||||
|
||||
public function new() {
|
||||
super();
|
||||
listeners = new Map<String, Array<F>>();
|
||||
}
|
||||
|
||||
override public function set(key:String, value:T):Void {
|
||||
super.set(key, value);
|
||||
if (listeners.exists(key)) {
|
||||
for (f in listeners.get(key)) call(f, value);
|
||||
}
|
||||
}
|
||||
|
||||
public function bind(key:String, object:Dynamic, field:String):Void {
|
||||
var f:F = Tuple.two(object, field);
|
||||
if (listeners.exists(key)) {
|
||||
listeners.get(key).push(f);
|
||||
} else {
|
||||
listeners.set(key, [f]);
|
||||
}
|
||||
if (exists(key)) call(f, get(key));
|
||||
}
|
||||
|
||||
private function call(field:F, value:T):Void {
|
||||
Reflect.setProperty(field.first, field.second, value);
|
||||
}
|
||||
}
|
||||
|
||||
class Resources implements IResources {
|
||||
|
||||
public var image(default, null):Map<String, BitmapData>;
|
||||
public var color(default, null):Map<String, Int>;
|
||||
|
||||
public function new() {
|
||||
image = new Map<String, BitmapData>();
|
||||
color = new Map<String, Int>();
|
||||
image = new ResMap<BitmapData>();
|
||||
color = new ResMap<Int>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user