From b08e3b7ef2fc233ea4213712b046d48c99d2073e Mon Sep 17 00:00:00 2001 From: shmyga Date: Fri, 9 Aug 2013 10:00:21 +0200 Subject: [PATCH] added media loader --- .gitignore | 5 - com/abit/haxework/Test.hx | 21 ---- com/abit/haxework/core/Tuple.hx | 133 ++++++++++++++++++++ com/abit/haxework/net/BaseLoader.hx | 31 ++--- com/abit/haxework/net/BaseMediaLoader.hx | 37 ++++++ com/abit/haxework/net/BaseURLLoader.hx | 54 ++++++++ com/abit/haxework/net/ILoader.hx | 4 +- com/abit/haxework/net/ImageLoader.hx | 16 +++ com/abit/haxework/net/JsonLoader.hx | 10 +- com/abit/haxework/net/TextLoader.hx | 11 +- com/abit/haxework/net/callback/Callback.hx | 2 +- com/abit/haxework/net/callback/ICallback.hx | 2 +- 12 files changed, 271 insertions(+), 55 deletions(-) delete mode 100755 .gitignore delete mode 100755 com/abit/haxework/Test.hx create mode 100755 com/abit/haxework/core/Tuple.hx create mode 100755 com/abit/haxework/net/BaseMediaLoader.hx create mode 100755 com/abit/haxework/net/BaseURLLoader.hx create mode 100755 com/abit/haxework/net/ImageLoader.hx diff --git a/.gitignore b/.gitignore deleted file mode 100755 index d8082da..0000000 --- a/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -*.iml -*.ipr -*.iws -.idea/ -*target* diff --git a/com/abit/haxework/Test.hx b/com/abit/haxework/Test.hx deleted file mode 100755 index 59ade76..0000000 --- a/com/abit/haxework/Test.hx +++ /dev/null @@ -1,21 +0,0 @@ -package - -import com.abit.haxework.net.JsonLoader; -import flash.display.Sprite; - -class Test extends Sprite { - - public function new() { - super(); - trace("Ok"); - - new JsonLoader() - .GET("http://umix.tv/channel/data/maxim.json") - .success(function(data:String):Void { - trace("OK: " + data); - }) - .fail(function(error:Dynamic):Void { - trace("FAIL: " + error); - }); - } -} diff --git a/com/abit/haxework/core/Tuple.hx b/com/abit/haxework/core/Tuple.hx new file mode 100755 index 0000000..f02c2d4 --- /dev/null +++ b/com/abit/haxework/core/Tuple.hx @@ -0,0 +1,133 @@ +package com.abit.haxework.core; +typedef Tuple2#if!H#end = { + var first(default, null):T1; + var second(default, null):T2; +} +typedef Tuple3#if!H#end = {> Tuple2, + var third(default, null):T3; +} +typedef Tuple4#if!H#end = {> Tuple3, + var fourth(default, null):T4; +} +typedef Tuple5#if!H#end = {> Tuple4, + var fifth(default, null):T5; +} + +class Tuple { + public static function five(first:T1, second:T2, third:T3, fourth:T4, fifth:T5):Tuple5 { + return new InternalTuple5(first, second, third, fourth, fifth); + } + + public static function four(first:T1, second:T2, third:T3, fourth:T4):Tuple4 { + return new InternalTuple4(first, second, third, fourth); + } + + public static function three(first:T1, second:T2, third:T3):Tuple3 { + return new InternalTuple3(first, second, third); + } + + public static function two(first:T1, second:T2):Tuple2 { + return new InternalTuple2(first, second); + } + + public static inline function asTuple2(tuple:Tuple3):Tuple2 + return tuple; + + public static inline function asTuple3(tuple:Tuple4):Tuple3 + return tuple; + + public static inline function asTuple4(tuple:Tuple5):Tuple4 + return tuple; +} + +private class InternalTuple2 { + public var first(default, null):T1; + public var second(default, null):T2; + +/** + * Creates a new tuple. + * @param first The first value. + * @param second The second value. + */ + + public function new(first:T1, second:T2) { + this.first = first; + this.second = second; + } + + public function toString():String { + return "(" + first + ", " + second + ")"; + } +} +private class InternalTuple3 extends InternalTuple2 { + public var third(default, null):T3; + +/** + * Creates a new tuple. + * @param first The first value. + * @param second The second value. + * @param third The third value. + */ + + public function new(first:T1, second:T2, third:T3) { + super(first, second); + this.third = third; + } + + public override function toString():String { + return "(" + + first + ", " + + second + ", " + + third + ")"; + } +} +private class InternalTuple4 extends InternalTuple3 { + public var fourth(default, null):T4; + +/** + * Creates a new tuple. + * @param first The first value. + * @param second The second value. + * @param third The third value. + * @param fourth The fourth value. + */ + + public function new(first:T1, second:T2, third:T3, fourth:T4) { + super(first, second, third); + this.fourth = fourth; + } + + public override function toString():String { + return "(" + + first + ", " + + second + ", " + + third + ", " + + fourth + ")"; + } +} +private class InternalTuple5 extends InternalTuple4 { + public var fifth(default, null):T5; + +/** + * Creates a new tuple. + * @param first The first value. + * @param second The second value. + * @param third The third value. + * @param fourth The fourth value. + * @param fifth The fifth value. + */ + + public function new(first:T1, second:T2, third:T3, fourth:T4, fifth:T5) { + super(first, second, third, fourth); + this.fifth = fifth; + } + + public override function toString():String { + return "(" + + first + ", " + + second + ", " + + third + ", " + + fourth + ", " + + fifth + ")"; + } +} diff --git a/com/abit/haxework/net/BaseLoader.hx b/com/abit/haxework/net/BaseLoader.hx index 7da72f6..493f174 100755 --- a/com/abit/haxework/net/BaseLoader.hx +++ b/com/abit/haxework/net/BaseLoader.hx @@ -1,5 +1,6 @@ -package +package com.abit.haxework.net; +import flash.net.URLLoaderDataFormat; import flash.events.SecurityErrorEvent; import com.abit.haxework.net.callback.Callback; import com.abit.haxework.net.callback.ICallback; @@ -13,7 +14,6 @@ class BaseLoader implements ILoader { private var busy:Bool; private var url:String; - private var loader:URLLoader; private var callback:ICallback; public function new() { @@ -25,8 +25,7 @@ class BaseLoader implements ILoader { busy = true; this.url = url; callback = new Callback(); - loader = buildLoader(); - loader.load(new URLRequest(url)); + internalRequest(); return callback; } @@ -34,16 +33,12 @@ class BaseLoader implements ILoader { return request(url, Method.GET); } - private function buildLoader():URLLoader { - var loader:URLLoader = new URLLoader(); - loader.addEventListener(Event.COMPLETE, onComplete); - loader.addEventListener(IOErrorEvent.IO_ERROR, onError); - loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); - return loader; + private function internalRequest():Void { + throw "Abstract"; } private function onComplete(e:Event):Void { - var data:T = extrudeResult(loader.data); + var data:T = extrudeResult(e); var c:ICallback = callback; dispose(); c.callSuccess(data); @@ -55,19 +50,19 @@ class BaseLoader implements ILoader { c.callFail(e); } + private function extrudeResult(e:Event):T { + throw "Abstract"; + return null; + } + private function dispose():Void { - loader.removeEventListener(Event.COMPLETE, onComplete); - loader.removeEventListener(IOErrorEvent.IO_ERROR, onError); - loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); - loader = null; url = null; callback = null; busy = false; } - private function extrudeResult(data:Dynamic):T { - throw "Abstract"; - return null; + public function cancel():Void { + dispose(); } } diff --git a/com/abit/haxework/net/BaseMediaLoader.hx b/com/abit/haxework/net/BaseMediaLoader.hx new file mode 100755 index 0000000..17b520a --- /dev/null +++ b/com/abit/haxework/net/BaseMediaLoader.hx @@ -0,0 +1,37 @@ +package com.abit.haxework.net; + +import com.abit.haxework.net.BaseLoader; +import flash.events.SecurityErrorEvent; +import flash.events.IOErrorEvent; +import flash.events.Event; +import flash.net.URLRequest; +import flash.display.Loader; + +class BaseMediaLoader extends BaseLoader { + + private var loader:Loader; + + override private function internalRequest():Void { + loader = buildLoader(); + loader.load(new URLRequest(url)); + } + + private function buildLoader():Loader { + var loader:Loader = new Loader(); + loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); + loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError); + loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); + return loader; + } + + override private function dispose():Void { + super.dispose(); + if (loader != null) { + loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete); + loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError); + loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); + loader = null; + } + } +} + diff --git a/com/abit/haxework/net/BaseURLLoader.hx b/com/abit/haxework/net/BaseURLLoader.hx new file mode 100755 index 0000000..f0c33a9 --- /dev/null +++ b/com/abit/haxework/net/BaseURLLoader.hx @@ -0,0 +1,54 @@ +package com.abit.haxework.net; + +import flash.net.URLLoaderDataFormat; +import flash.events.SecurityErrorEvent; +import com.abit.haxework.net.callback.Callback; +import com.abit.haxework.net.callback.ICallback; +import flash.events.IOErrorEvent; +import flash.net.URLRequest; +import flash.events.Event; +import flash.net.URLLoader; +import com.abit.haxework.net.ILoader.Method; + +class BaseURLLoader extends BaseLoader { + + private var dataFormat:URLLoaderDataFormat; + private var loader:URLLoader; + + public function new() { + super(); + dataFormat = URLLoaderDataFormat.TEXT; + } + + override private function internalRequest():Void { + loader = buildLoader(); + loader.load(new URLRequest(url)); + } + + private function buildLoader():URLLoader { + var loader:URLLoader = new URLLoader(); + loader.dataFormat = dataFormat; + loader.addEventListener(Event.COMPLETE, onComplete); + loader.addEventListener(IOErrorEvent.IO_ERROR, onError); + loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); + return loader; + } + + override private function dispose():Void { + super.dispose(); + if (loader != null) { + loader.removeEventListener(Event.COMPLETE, onComplete); + loader.removeEventListener(IOErrorEvent.IO_ERROR, onError); + loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onError); + loader = null; + } + } + + override public function cancel():Void { + if (loader != null) { + try {loader.close();} catch (error:Dynamic) {} + } + super.cancel(); + } +} + diff --git a/com/abit/haxework/net/ILoader.hx b/com/abit/haxework/net/ILoader.hx index 04f9b63..20a91d1 100755 --- a/com/abit/haxework/net/ILoader.hx +++ b/com/abit/haxework/net/ILoader.hx @@ -1,4 +1,4 @@ -package +package com.abit.haxework.net; import com.abit.haxework.net.callback.ICallback; @@ -10,4 +10,6 @@ enum Method { interface ILoader { public function request(url:String, method:Method):ICallback; public function GET(url:String):ICallback; + + public function cancel():Void; } diff --git a/com/abit/haxework/net/ImageLoader.hx b/com/abit/haxework/net/ImageLoader.hx new file mode 100755 index 0000000..11b2f06 --- /dev/null +++ b/com/abit/haxework/net/ImageLoader.hx @@ -0,0 +1,16 @@ +package com.abit.haxework.net; + +import flash.display.LoaderInfo; +import flash.display.Loader; +import flash.display.Bitmap; +import flash.events.Event; +import flash.display.BitmapData; + +class ImageLoader extends BaseMediaLoader { + + override private function extrudeResult(e:Event):BitmapData { + var content:Bitmap = cast(cast(e.currentTarget, LoaderInfo).loader.content,Bitmap); + return content.bitmapData; + } +} + diff --git a/com/abit/haxework/net/JsonLoader.hx b/com/abit/haxework/net/JsonLoader.hx index dbc61f7..17073a2 100755 --- a/com/abit/haxework/net/JsonLoader.hx +++ b/com/abit/haxework/net/JsonLoader.hx @@ -1,10 +1,12 @@ -package +package com.abit.haxework.net; +import flash.events.Event; +import flash.net.URLLoader; import haxe.Json; -class JsonLoader extends BaseLoader { +class JsonLoader extends BaseURLLoader { - override private function extrudeResult(data:Dynamic):Dynamic { - return Json.parse(Std.string(data)); + override private function extrudeResult(e:Event):Dynamic { + return Json.parse(Std.string(cast(e.currentTarget, URLLoader).data)); } } diff --git a/com/abit/haxework/net/TextLoader.hx b/com/abit/haxework/net/TextLoader.hx index c14a3e4..6bf5367 100755 --- a/com/abit/haxework/net/TextLoader.hx +++ b/com/abit/haxework/net/TextLoader.hx @@ -1,8 +1,11 @@ -package +package com.abit.haxework.net; -class TextLoader extends BaseLoader { +import flash.net.URLLoader; +import flash.events.Event; - override private function extrudeResult(data:Dynamic):String { - return Std.string(data); +class TextLoader extends BaseURLLoader { + + override private function extrudeResult(e:Event):String { + return Std.string(cast(e.currentTarget, URLLoader).data); } } diff --git a/com/abit/haxework/net/callback/Callback.hx b/com/abit/haxework/net/callback/Callback.hx index 6502ca8..0a5d0aa 100755 --- a/com/abit/haxework/net/callback/Callback.hx +++ b/com/abit/haxework/net/callback/Callback.hx @@ -1,4 +1,4 @@ -package +package com.abit.haxework.net.callback; class Callback implements ICallback { diff --git a/com/abit/haxework/net/callback/ICallback.hx b/com/abit/haxework/net/callback/ICallback.hx index 38ec355..5a653ed 100755 --- a/com/abit/haxework/net/callback/ICallback.hx +++ b/com/abit/haxework/net/callback/ICallback.hx @@ -1,4 +1,4 @@ -package +package com.abit.haxework.net.callback; interface ICallback { public function success(f:T -> Void):ICallback;