diff --git a/demo/src/demo/Demo.hx b/demo/src/demo/Demo.hx index 1d8c1cf..448b06e 100644 --- a/demo/src/demo/Demo.hx +++ b/demo/src/demo/Demo.hx @@ -1,38 +1,50 @@ package demo; -import haxework.gui.frame.IFrameSwitcher; +import haxework.net.JsonLoader; +import demo.popup.ColorPopup; +import haxework.App; +import haxework.gui.frame.FrameSwitcher; import haxework.gui.IGroupView; import haxework.gui.IView; -import haxework.gui.Root; import haxework.gui.ToggleButtonView; import haxework.gui.VGroupView; import haxework.log.TraceLogger; -import haxework.net.manage.ILoaderManager; -import haxework.net.manage.LoaderManager; -import haxework.resources.IResources; -import haxework.resources.Resources; -@:template class Demo extends VGroupView { - - @:provide static var resources:IResources; - @:provide static var manager:ILoaderManager; - - public static function main() { - L.push(new TraceLogger()); - resources = new Resources(); - manager = new LoaderManager(); - resources.image.put("logo", HaxeLogo.resolve()); - Theme.setColor(0x33aa33); - var demo = new Demo(); - demo.switcher.change("list_form"); - Root.bind(demo); - Root.instance.onResize.connect(function(rect) trace("resize", rect)); - } - - @:view var switcher:IFrameSwitcher; +@:template class DemoView extends VGroupView { + @:view var switcher:FrameSwitcher; @:view var tabs:IGroupView; + private function init():Void { + switcher.change("list_form"); + } + private function onFrameSwitch(frame:IView):Void { for (view in tabs.views) cast(view, ToggleButtonView).on = view.id == frame.id; } + + private function choiceColor():Void { + new ColorPopup() + .show() + .then(function(color) Theme.setColor(color)) + .catchError(function(e) {}); + } +} + +class Demo extends App { + + public static function main() { + L.push(new TraceLogger()); + + var app = new App(); + app.resources.image.put("logo", HaxeLogo.resolve()); + Theme.setColor(0x33aa33); + app.start(new DemoView()); + + new JsonLoader().GET("http://umix.tv/channel/data2/renova.json") + .then(function(data:Array) { + app.resources.any.put("data", data); + app.resources.any.put("data50", Util.marray(data, 50)); + }) + .catchError(function(error) trace(error)); + } } diff --git a/demo/src/demo/Demo.yaml b/demo/src/demo/DemoView.yaml similarity index 77% rename from demo/src/demo/Demo.yaml rename to demo/src/demo/DemoView.yaml index 7e4a8ba..ca93021 100644 --- a/demo/src/demo/Demo.yaml +++ b/demo/src/demo/DemoView.yaml @@ -33,19 +33,23 @@ views: +onPress: "$code:switcher.change('data_form')" - id: switcher $type: haxework.gui.frame.FrameSwitcher - skin: $r:skin:border + skin: $r:skin:panel + animateFactory: { $class: haxework.animate.SlideAnimate } +onSwitch: $this:onFrameSwitch geometry.size.stretch: true geometry.padding: 5 views: - id: list_form $type: demo.form.ListForm + skin: $r:skin:background geometry.size.stretch: true - id: tail_form $type: demo.form.TailForm + skin: $r:skin:background geometry.size.stretch: true - id: data_form $type: demo.form.DataForm + skin: $r:skin:background geometry.size.stretch: true - $type: haxework.gui.HGroupView geometry.size.percent.width: 100 @@ -55,23 +59,8 @@ views: - $type: haxework.gui.ButtonView geometry.padding: [25, 8] skin: $r:skin:button - text: green - +onPress: "$code:Theme.setColor(0x33AA33)" - - $type: haxework.gui.ButtonView - geometry.padding: [25, 8] - skin: $r:skin:button - text: red - +onPress: "$code:Theme.setColor(0xAA3333)" - - $type: haxework.gui.ButtonView - geometry.padding: [25, 8] - skin: $r:skin:button - text: yellow - +onPress: "$code:Theme.setColor(0xFFCC55, 0x555555)" - - $type: haxework.gui.ButtonView - geometry.padding: [25, 8] - skin: $r:skin:button - text: pink - +onPress: "$code:Theme.setColor(0xCC33AA)" + text: Color + +onPress: "$code:choiceColor()" # separator - $type: haxework.gui.SpriteView geometry.size.stretch: true diff --git a/demo/src/demo/Model.hx b/demo/src/demo/Model.hx new file mode 100644 index 0000000..e0f5aab --- /dev/null +++ b/demo/src/demo/Model.hx @@ -0,0 +1,10 @@ +package demo; + +typedef Model = { + var id:String; + var created_at:Int; + var maker:String; + var title:String; + var message:String; + var image:{url:String, width:Int, height:Int}; +} diff --git a/demo/src/demo/Theme.hx b/demo/src/demo/Theme.hx index d297c02..7afe0ae 100644 --- a/demo/src/demo/Theme.hx +++ b/demo/src/demo/Theme.hx @@ -13,9 +13,7 @@ class Theme { public static function setColor(color:Int, textColor:Int = 0xffffff):Void { var text:ISkin = new TextSkin(textColor, 16, "Courirer"); - var background:SkinSet = [ - Skin.color(0x00000), - ]; + var background = Skin.color(0x00000); var button:SkinSet = [ Skin.buttonColor(color), text, @@ -32,18 +30,22 @@ class Theme { var scroll:SkinSet = [ ScrollBarSkin.vertical(color, ColorUtils.diff(color, 128)), ]; - var border:SkinSet = [ - Skin.border(ColorUtils.multiply(color, 1.5), 1, 2), - ]; - resources.skin.put("text", [ + var border = Skin.border(ColorUtils.multiply(color, 1.5), 1, 2); + resources.skin.put("text0", [ Skin.color(ColorUtils.diff(color, 128)), new TextSkin(ColorUtils.diff(color, -128), 16, "Courirer"), ]); - resources.skin.put("background", background); + resources.skin.put("text1", [ + Skin.color(ColorUtils.diff(color, 64)), + new TextSkin(ColorUtils.diff(color, -128), 16, "Courirer"), + ]); + resources.skin.put("text", resources.skin.get("text0")); + resources.skin.put("background", [background]); resources.skin.put("button", button); resources.skin.put("tab", tab); resources.skin.put("view", view); resources.skin.put("scroll", scroll); - resources.skin.put("border", border); + resources.skin.put("border", [border]); + resources.skin.put("panel", [background, border]); } } diff --git a/demo/src/demo/form/DataForm.hx b/demo/src/demo/form/DataForm.hx index 7e72daa..2093d75 100644 --- a/demo/src/demo/form/DataForm.hx +++ b/demo/src/demo/form/DataForm.hx @@ -1,31 +1,17 @@ package demo.form; -import haxework.gui.DataView; import haxework.gui.TextView; import haxework.gui.VGroupView; -import haxework.net.JsonLoader; -import haxework.resources.IResources; @:template class DataForm extends VGroupView { - @:view var data:DataView; - @:provide var resources:IResources; - private function init() { - new JsonLoader().GET("http://umix.tv/channel/data2/renova.json") - .then(function(data:Array) { - var values = data.map(function(item) return '${item.id}: ${item.message}'); - this.data.data = Util.marray(values, 1); - }) - .catchError(function(error) trace(error)); - } - - private function factory(index:Int, value:String):TextView { + private function factory(index:Int, value:Model):TextView { var label = new TextView(); label.geometry.size.percent.width = 100; label.geometry.margin = 1; label.geometry.padding = 2; - resources.skin.bind("text", label, "skin"); - label.text = value; + label.skinId = "text"; + label.text = (value.title != null ? '${value.title}\n-\n' : '') + value.message; return label; } } diff --git a/demo/src/demo/form/DataForm.yaml b/demo/src/demo/form/DataForm.yaml index 38df8db..11fa7ed 100644 --- a/demo/src/demo/form/DataForm.yaml +++ b/demo/src/demo/form/DataForm.yaml @@ -5,11 +5,12 @@ views: geometry.size.stretch: true view: id: data - $type: haxework.gui.DataView + $type: haxework.gui.DataView layout: $type: haxework.gui.layout.VerticalLayout factory: $this:factory geometry.size.width: 100% + data: $r:any:data scroll: $type: haxework.gui.list.VScrollBarView skin: $r:skin:scroll diff --git a/demo/src/demo/form/ListForm.hx b/demo/src/demo/form/ListForm.hx index aa26616..bfdf68c 100644 --- a/demo/src/demo/form/ListForm.hx +++ b/demo/src/demo/form/ListForm.hx @@ -1,23 +1,18 @@ package demo.form; +import haxework.gui.list.LabelListItem; import haxework.gui.list.ListView.IListItemView; import haxework.gui.list.VListView; import haxework.gui.VGroupView; -import haxework.net.JsonLoader; @:template class ListForm extends VGroupView { - @:view public var list(default, null):VListView; + @:view public var list(default, null):VListView; - private function init() { - new JsonLoader().GET("http://umix.tv/channel/data2/renova.json") - .then(function(data:Array) { - var values = data.map(function(item) return '${item.id}: ${item.message}'); - list.data = Util.marray(values, 50); - }) - .catchError(function(error) trace(error)); + private function factory() { + return new LabelListItem(function(index:Int, value:Model) return '${index}. ${value.id}: ${value.title}'); } - private function onItemSelect(item:IListItemView):Void { - trace('onItemSelect: ${item.data}'); + private function onItemSelect(item:IListItemView):Void { + trace('onItemSelect: ${item.data.id}'); } } diff --git a/demo/src/demo/form/ListForm.yaml b/demo/src/demo/form/ListForm.yaml index a101be1..50c2640 100644 --- a/demo/src/demo/form/ListForm.yaml +++ b/demo/src/demo/form/ListForm.yaml @@ -1,12 +1,13 @@ --- views: - id: list - $type: haxework.gui.list.VListView + $type: haxework.gui.list.VListView +onItemSelect: $this:onItemSelect - factory: { $class: haxework.gui.list.LabelListItem } + factory: $this:factory geometry.size.stretch: true scroll: $type: haxework.gui.list.VScrollBarView skin: $r:skin:scroll geometry.size.height: 100% geometry.size.width: 10 + data: $r:any:data50 diff --git a/demo/src/demo/form/TailForm.hx b/demo/src/demo/form/TailForm.hx index e31e11d..8723779 100644 --- a/demo/src/demo/form/TailForm.hx +++ b/demo/src/demo/form/TailForm.hx @@ -1,24 +1,29 @@ package demo.form; -import haxework.gui.DataView; import haxework.gui.HGroupView; +import haxework.gui.ImageView; +import haxework.gui.IView; import haxework.gui.TextView; -import haxework.resources.IResources; +import haxework.gui.utils.DrawUtil.FillType; @:template class TailForm extends HGroupView { - @:view public var data:DataView; - @:provide var resources:IResources; - private function init() { - data.data = [for (i in 0...100) '${i}']; - } - - private function factory(index:Int, value:String):TextView { - var view = new TextView(); - view.geometry.size.fixed.width = 100 + 100 * Math.random(); - view.geometry.size.fixed.height = 100 + 100 * Math.random(); - resources.skin.bind("view", view, "skin"); - view.text = 'View #${index}'; + private function factory(index:Int, value:Model):IView { + var view:IView; + if (value.image != null) { + var imageView = new ImageView(); + imageView.skinId = "border"; + imageView.fillType = FillType.CONTAIN; + imageView.imageUrl = value.image.url; + view = imageView; + } else { + var textView = new TextView(); + textView.skinId = "view"; + textView.text = '${value.id}\n${value.maker}'; + view = textView; + } + view.geometry.size.fixed.width = 350; + view.geometry.size.fixed.height = 200; return view; } } diff --git a/demo/src/demo/form/TailForm.yaml b/demo/src/demo/form/TailForm.yaml index 59278b9..4e4a2fb 100644 --- a/demo/src/demo/form/TailForm.yaml +++ b/demo/src/demo/form/TailForm.yaml @@ -4,12 +4,13 @@ views: geometry.size.stretch: true view: id: data - $type: haxework.gui.DataView + $type: haxework.gui.DataView layout: $type: haxework.gui.layout.TailLayout margin: 2 factory: $this:factory geometry.size.width: 100% + data: $r:any:data scroll: $type: haxework.gui.list.VScrollBarView skin: $r:skin:scroll diff --git a/demo/src/demo/popup/ColorPopup.hx b/demo/src/demo/popup/ColorPopup.hx new file mode 100644 index 0000000..f8fef69 --- /dev/null +++ b/demo/src/demo/popup/ColorPopup.hx @@ -0,0 +1,15 @@ +package demo.popup; + +import haxework.gui.ButtonView; +import haxework.gui.popup.PopupView; +import haxework.gui.skin.Skin; + +@:template class ColorPopup extends PopupView { + + private function colorViewFactory(index:Int, color:Int) { + var view = new ButtonView(); + view.geometry.size.fixed = [48, 48]; + view.skin = [Skin.buttonColor(color)]; + return view; + } +} diff --git a/demo/src/demo/popup/ColorPopup.yaml b/demo/src/demo/popup/ColorPopup.yaml new file mode 100644 index 0000000..97a416e --- /dev/null +++ b/demo/src/demo/popup/ColorPopup.yaml @@ -0,0 +1,35 @@ +--- +view: + $type: haxework.gui.VGroupView + geometry.size.width: 400 + geometry.size.height: 200 + geometry.padding: 10 + geometry.hAlign: center + geometry.vAlign: middle + skinId: panel + views: + - id: colors + $type: haxework.gui.DataView + geometry.size.stretch: true + layout: + $type: haxework.gui.layout.TailLayout + vAlign: middle + margin: 5 + factory: $this:colorViewFactory + data: + - 0x33AA33 + - 0xAA3333 + - 0xFFCC55 # 0x555555 + - 0xCC33AA + - 0x3333AA + +onDataSelect: $this:close + - $type: haxework.gui.HGroupView + geometry.size.width: 100% + layout.hAlign: right + layout.margin: 10 + views: + - $type: haxework.gui.ButtonView + geometry.padding: [25, 8] + skinId: button + text: Cancel + +onPress: $code:reject('cancel') diff --git a/src/main/haxework/App.hx b/src/main/haxework/App.hx new file mode 100644 index 0000000..af5460c --- /dev/null +++ b/src/main/haxework/App.hx @@ -0,0 +1,35 @@ +package haxework; + +import haxework.gui.Root; +import haxework.gui.IView; +import flash.Lib; +import haxework.animate.Animate; +import haxework.animate.FadeAnimate; +import haxework.animate.UnFadeAnimate; +import haxework.gui.popup.PopupManager; +import haxework.net.manage.ILoaderManager; +import haxework.net.manage.LoaderManager; +import haxework.resources.IResources; +import haxework.resources.Resources; + +class App { + + @:provide var app:App; + @:provide var resources:IResources; + @:provide var loaderManager:ILoaderManager; + @:provide var popupManager:PopupManager; + + public function new() { + resources = new Resources(); + loaderManager = new LoaderManager(); + popupManager = new PopupManager(); + popupManager.showAnimateFactory = UnFadeAnimate; + popupManager.closeAnimateFactory = FadeAnimate; + app = this; + } + + public function start(view:IView) { + Animate.bind(Lib.current.stage); + Root.bind(view); + } +} diff --git a/src/main/haxework/animate/Animate.hx b/src/main/haxework/animate/Animate.hx index 32c03ea..09e887c 100644 --- a/src/main/haxework/animate/Animate.hx +++ b/src/main/haxework/animate/Animate.hx @@ -4,50 +4,51 @@ import flash.events.Event; import flash.display.Stage; class Animate implements IAnimate { + public static var defaultDuraion = 300; - private static var running:Array = new Array(); + private static var running:Array = new Array(); - 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); + public static function bind(stage:Stage):Void { + stage.addEventListener(Event.ENTER_FRAME, function(_) { + Animate.updateAll(); + }); } - } - 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 static function updateAll():Void { + if (running.length > 0) { + var time = Date.now().getTime(); + for (animate in running) animate.update(time); + } } - } - public function cancel():Void { - if (!Math.isNaN(startTime)) update(startTime + duration); - } + 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 > -1 ? duration : defaultDuraion; + } + + 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); + } } diff --git a/src/main/haxework/animate/CircleMaskAnimate.hx b/src/main/haxework/animate/CircleMaskAnimate.hx index 4ff5733..35116d5 100755 --- a/src/main/haxework/animate/CircleMaskAnimate.hx +++ b/src/main/haxework/animate/CircleMaskAnimate.hx @@ -1,63 +1,62 @@ package haxework.animate; -import flash.display.DisplayObjectContainer; -import haxework.animate.IAnimate; import flash.display.Sprite; -import haxework.gui.IView; import haxework.animate.Animate; +import haxework.animate.IAnimate; +import haxework.gui.IView; class CircleMaskAnimate extends Animate { - private var view:IView; - private var mask:Sprite; - private var cyrcle:Sprite; - private var size:Float; + private var view:IView; + private var mask:Sprite; + private var cyrcle:Sprite; + private var size:Float; - public function new(view:IView, ?duration:Int = 1000) { - super(duration); - this.view = view; - this.mask = new Sprite(); - this.cyrcle = new Sprite(); - } - - override public function start(callback:IAnimate -> Void, ?custom:Bool = false):Void { - var width = view.parent.width; - var height = view.parent.height; - size = Math.sqrt(width * width + height * height); - //size = Math.max(width, height); - cyrcle.x = mask.x = -(size - width) / 2 - size; - cyrcle.y = mask.y = -(size - height) / 2 - size; - - redraw(size, size); - - view.parent.container.addChild(mask); - view.content.mask = mask; - view.parent.container.addChild(cyrcle); - - super.start(callback, custom); - } - - private function redraw(size:Float, r:Float):Void { - mask.graphics.clear(); - mask.graphics.beginFill(0xffffff); - mask.graphics.drawCircle(size + size / 2, size + size / 2, r / 2); - mask.graphics.endFill(); - - cyrcle.graphics.clear(); - cyrcle.graphics.lineStyle(8, 0xffffff); - cyrcle.graphics.drawCircle(size + size / 2, size + size / 2, r / 2); - cyrcle.graphics.lineStyle(); - } - - override private function update(time:Float):Void { - 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); + public function new(view:IView, duration:Int = -1) { + super(duration); + this.view = view; + this.mask = new Sprite(); + this.cyrcle = new Sprite(); + } + + override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { + var width = view.parent.width; + var height = view.parent.height; + size = Math.sqrt(width * width + height * height); + //size = Math.max(width, height); + cyrcle.x = mask.x = -(size - width) / 2 - size; + cyrcle.y = mask.y = -(size - height) / 2 - size; + + redraw(size, size); + + view.parent.container.addChild(mask); + view.content.mask = mask; + view.parent.container.addChild(cyrcle); + + super.start(callback, custom); + } + + private function redraw(size:Float, r:Float):Void { + mask.graphics.clear(); + mask.graphics.beginFill(0xffffff); + mask.graphics.drawCircle(size + size / 2, size + size / 2, r / 2); + mask.graphics.endFill(); + + cyrcle.graphics.clear(); + cyrcle.graphics.lineStyle(4, 0xffffff); + cyrcle.graphics.drawCircle(size + size / 2, size + size / 2, r / 2); + cyrcle.graphics.lineStyle(); + } + + override private function update(time:Float):Void { + 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); + } } - } } diff --git a/src/main/haxework/animate/FadeAnimate.hx b/src/main/haxework/animate/FadeAnimate.hx index b737cfb..31fdb7b 100755 --- a/src/main/haxework/animate/FadeAnimate.hx +++ b/src/main/haxework/animate/FadeAnimate.hx @@ -1,29 +1,29 @@ package haxework.animate; +import flash.display.DisplayObject; import haxework.animate.IAnimate; -import flash.display.Sprite; import haxework.gui.IView; import haxework.animate.Animate; class FadeAnimate extends Animate { - private var view:IView; + private var view:IView; - public function new(view:IView, ?duration = 500) { - super(duration); - this.view = view; - } - - override public function start(callback:IAnimate -> Void, ?custom:Bool = false):Void { - view.content.alpha = 1.0; - super.start(callback, custom); - } - - override private function update(time:Float):Void { - super.update(time); - view.content.alpha = 1 - (progress * 1.0); - if (progress >= 1) { - view.content.alpha = 0.0; + public function new(view:IView, duration:Int = -1) { + super(duration); + this.view = view; + } + + override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { + view.content.alpha = 1.0; + super.start(callback, custom); + } + + override private function update(time:Float):Void { + super.update(time); + view.content.alpha = 1 - (progress * 1.0); + if (progress >= 1) { + view.content.alpha = 0.0; + } } - } } diff --git a/src/main/haxework/animate/IAnimate.hx b/src/main/haxework/animate/IAnimate.hx index ae4bb26..95728e0 100644 --- a/src/main/haxework/animate/IAnimate.hx +++ b/src/main/haxework/animate/IAnimate.hx @@ -2,8 +2,9 @@ package haxework.animate; interface IAnimate { - public function start(callback:IAnimate->Void, ?custom:Bool = false):Void; - public function cancel():Void; + public function start(callback:IAnimate -> Void, custom:Bool = false):Void; - private function update(time:Float):Void; -} \ No newline at end of file + public function cancel():Void; + + private function update(time:Float):Void; +} diff --git a/src/main/haxework/animate/SlideAnimate.hx b/src/main/haxework/animate/SlideAnimate.hx new file mode 100644 index 0000000..86d64e3 --- /dev/null +++ b/src/main/haxework/animate/SlideAnimate.hx @@ -0,0 +1,24 @@ +package haxework.animate; + +import flash.display.DisplayObject; +import haxework.gui.IView; + +class SlideAnimate extends Animate { + + private var view:IView; + + public function new(view:IView, duration:Int = -1) { + super(duration); + this.view = view; + } + + override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { + view.content.x = view.x - this.view.width + this.view.width / progress; + super.start(callback, custom); + } + + override private function update(time:Float):Void { + super.update(time); + view.content.x = view.x - this.view.width + this.view.width / Math.min(1, progress); + } +} diff --git a/src/main/haxework/animate/UnFadeAnimate.hx b/src/main/haxework/animate/UnFadeAnimate.hx index e334177..23e3f02 100755 --- a/src/main/haxework/animate/UnFadeAnimate.hx +++ b/src/main/haxework/animate/UnFadeAnimate.hx @@ -1,28 +1,28 @@ package haxework.animate; -import flash.display.Sprite; +import flash.display.DisplayObject; import haxework.gui.IView; import haxework.animate.Animate; class UnFadeAnimate extends Animate { - private var view:IView; + private var view:IView; - public function new(view:IView, ?duration = 500) { - super(duration); - this.view = view; - } - - override public function start(callback:IAnimate -> Void, ?custom:Bool = false):Void { - view.content.alpha = 0.0; - super.start(callback, custom); - } - - override private function update(time:Float):Void { - super.update(time); - view.content.alpha = progress * 1.0; - if (progress >= 1) { - view.content.alpha = 1.0; + public function new(view:IView, duration:Int = -1) { + super(duration); + this.view = view; + } + + override public function start(callback:IAnimate -> Void, custom:Bool = false):Void { + view.content.alpha = 0.0; + super.start(callback, custom); + } + + override private function update(time:Float):Void { + super.update(time); + view.content.alpha = progress * 1.0; + if (progress >= 1) { + view.content.alpha = 1.0; + } } - } } diff --git a/src/main/haxework/core/IDisposable.hx b/src/main/haxework/core/IDisposable.hx deleted file mode 100755 index a227526..0000000 --- a/src/main/haxework/core/IDisposable.hx +++ /dev/null @@ -1,5 +0,0 @@ -package haxework.core; - -interface IDisposable { - public function dispose():Void; -} \ No newline at end of file diff --git a/src/main/haxework/core/Tuple.hx b/src/main/haxework/core/Tuple.hx deleted file mode 100755 index a59beb7..0000000 --- a/src/main/haxework/core/Tuple.hx +++ /dev/null @@ -1,133 +0,0 @@ -package 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/src/main/haxework/gui/AnimateView.hx b/src/main/haxework/gui/AnimateView.hx deleted file mode 100755 index 36bdcce..0000000 --- a/src/main/haxework/gui/AnimateView.hx +++ /dev/null @@ -1,57 +0,0 @@ -package haxework.gui; - -import haxe.Timer; -import flash.display.Bitmap; -import flash.display.BitmapData; -import haxework.gui.SpriteView; - -class AnimateView extends SpriteView { - - private var bitmap:Bitmap; - public var frames(default, set):Array; - public var interval(default, set):Int; - private var frame:Int; - - public function new() { - super(); - bitmap = new Bitmap(); - frames = []; - frame = 0; - interval = 200; - content.addChild(bitmap); - changeFrame(); - } - - private function set_frames(value:Array):Array { - if (frames != value) { - frames = value; - frame = 0; - changeFrame(true); - } - return frames; - } - - private function set_interval(value:Int):Int { - if (interval != value) { - interval = value; - } - return interval; - } - - private function changeFrame(?forse:Bool = false):Void { - frame = ++frame % frames.length; - bitmap.bitmapData = frames[frame]; - update(); - if (!forse) Timer.delay(function() changeFrame(false), interval); - } - - override public function update():Void { - if (contentSize && bitmap.bitmapData != null) { - width = bitmap.bitmapData.width; - height = bitmap.bitmapData.height; - } - super.update(); - bitmap.x = (width - bitmap.width) / 2; - bitmap.y = (height - bitmap.height) / 2; - } -} diff --git a/src/main/haxework/gui/DataView.hx b/src/main/haxework/gui/DataView.hx index 7ec3a8f..37c758e 100644 --- a/src/main/haxework/gui/DataView.hx +++ b/src/main/haxework/gui/DataView.hx @@ -4,10 +4,12 @@ import flash.display.DisplayObject; import flash.events.MouseEvent; import haxework.signal.Signal; +typedef Factory = Int -> D -> IView + class DataView extends GroupView { public var data(default, set):Array; - public var factory(default, default):Int -> D -> IView; + public var factory(default, set):Factory; public var onItemSelect(default, null):Signal3> = new Signal3(); public var onDataSelect(default, null):Signal = new Signal(); @@ -15,10 +17,16 @@ class DataView extends GroupView { private function set_data(value:Array):Array { data = value; - rebuild(); + if (factory != null) rebuild(); return data; } + private function set_factory(value:Factory):Factory { + factory = value; + if (data != null) rebuild(); + return factory; + } + private function rebuild():Void { for (view in views) { view.content.removeEventListener(MouseEvent.CLICK, onItemClick); diff --git a/src/main/haxework/gui/IView.hx b/src/main/haxework/gui/IView.hx index 712f9b8..c00bf7a 100755 --- a/src/main/haxework/gui/IView.hx +++ b/src/main/haxework/gui/IView.hx @@ -17,6 +17,7 @@ interface IView { public var content(default, null):C; public var skin(default, set):SkinSet; + public var skinId(null, set):String; public var parent(default, null):Null; diff --git a/src/main/haxework/gui/ImageView.hx b/src/main/haxework/gui/ImageView.hx index c2aef53..5d91a67 100755 --- a/src/main/haxework/gui/ImageView.hx +++ b/src/main/haxework/gui/ImageView.hx @@ -2,6 +2,7 @@ package haxework.gui; import flash.display.BitmapData; import haxework.gui.skin.BitmapSkin; +import haxework.gui.skin.ISkin; import haxework.gui.utils.DrawUtil.FillType; import haxework.net.ImageLoader; @@ -9,22 +10,29 @@ class ImageView extends SpriteView { public var image(default, set):BitmapData; public var imageUrl(default, set):String; + public var fillType(default, set):FillType; - public function new(?image:BitmapData) { + private var bitmapSkin:BitmapSkin = new BitmapSkin(); + + public function new(image:BitmapData = null) { super(); + fillType = FillType.DEFAULT; if (image != null) { this.image = image; } } + override private function set_skin(value:SkinSet):SkinSet { + value = value.slice(0); + value.unshift(bitmapSkin); + return super.set_skin(value); + } + private function set_image(value:BitmapData):BitmapData { if (image != value) { image = value; - skin = [new BitmapSkin(image, FillType.DEFAULT)]; - //geometry.size.content.width = value.width; - //geometry.size.content.height = value.height; - //toUpdate(); - //toRedraw(); + bitmapSkin.image = value; + toRedraw(); } return image; } @@ -34,8 +42,15 @@ class ImageView extends SpriteView { imageUrl = value; new ImageLoader().GET(imageUrl).then(function(data) { image = data; - }); + }).catchError(function(e) L.w("ImageView", "load", e)); } return imageUrl; } + + private function set_fillType(value:FillType):FillType { + if (fillType != value) { + bitmapSkin.fillType = fillType = value; + } + return fillType; + } } diff --git a/src/main/haxework/gui/Root.hx b/src/main/haxework/gui/Root.hx index b96d4ad..bdeb73a 100755 --- a/src/main/haxework/gui/Root.hx +++ b/src/main/haxework/gui/Root.hx @@ -7,10 +7,9 @@ import flash.errors.Error; import flash.events.Event; import flash.geom.Rectangle; import flash.Lib; -import haxework.core.IDisposable; import haxework.signal.Signal; -class Root implements IDisposable { +class Root { public static function bind(view:IView, autoSize:Bool = true) { new Root(view, autoSize); diff --git a/src/main/haxework/gui/TextView.hx b/src/main/haxework/gui/TextView.hx index a8cfa6c..de77477 100755 --- a/src/main/haxework/gui/TextView.hx +++ b/src/main/haxework/gui/TextView.hx @@ -144,7 +144,7 @@ class TextView extends SpriteView implements ITextView { private function updateTextSize():Void { var size = TextUtil.getSize(textField); - setContentSize(size.x, size.y); + setContentSize(size.x, size.y, "text"); } override public function update():Void { @@ -168,7 +168,7 @@ class TextView extends SpriteView implements ITextView { private function placeTextField(textField:TextField):Void { textField.width = width; - textField.height = geometry.size.content.height; + textField.height = geometry.size.content.exists("text") ? geometry.size.content.get("text").height : height; textField.x = switch (layout.hAlign) { case LEFT | NONE: geometry.padding.left; @@ -178,8 +178,8 @@ class TextView extends SpriteView implements ITextView { } textField.y = switch (layout.vAlign) { case TOP | NONE: geometry.padding.top; - case MIDDLE: (height - geometry.size.content.height) / 2 + geometry.padding.top - geometry.padding.bottom; - case BOTTOM: height - geometry.size.content.height - geometry.padding.bottom; + case MIDDLE: (height - textField.height) / 2 + geometry.padding.top - geometry.padding.bottom; + case BOTTOM: height - textField.height - geometry.padding.bottom; default: 0; } } diff --git a/src/main/haxework/gui/View.hx b/src/main/haxework/gui/View.hx index 375537c..d435614 100755 --- a/src/main/haxework/gui/View.hx +++ b/src/main/haxework/gui/View.hx @@ -1,12 +1,14 @@ package haxework.gui; -import haxework.gui.skin.ISkin.ISizeSkin; import flash.display.DisplayObject; import flash.display.InteractiveObject; import haxework.gui.core.Geometry; +import haxework.gui.skin.ISkin.ISizeSkin; import haxework.gui.skin.ISkin.SkinSet; +import haxework.resources.IResources; class View implements IView { + @:provide private var r:IResources; private static var counter:Int = 0; public static var updater(default, null):ViewUpdater = new ViewUpdater(); @@ -23,6 +25,7 @@ class View implements IView { public var content(default, null):C; public var skin(default, set):SkinSet; + public var skinId(null, set):String; public var parent(default, null):Null; @@ -63,22 +66,16 @@ class View implements IView { for (skin in this.skin) { if (Std.is(skin, ISizeSkin)) { var sizeSkin:ISizeSkin = cast skin; - setSize(sizeSkin.width, sizeSkin.height); + setContentSize(sizeSkin.width, sizeSkin.height, "skin"); } skin.draw(this); } } - private function setSize(width:Float, height:Float):Void { - if (width != geometry.size.fixed.width || height != geometry.size.fixed.height) { - geometry.size.fixed = [width, height]; - toUpdateParent(); - } - } - - private function setContentSize(width:Float, height:Float):Void { - if (width != geometry.size.content.width || height != geometry.size.content.height) { - geometry.size.content = [width, height]; + private function setContentSize(width:Float, height:Float, type:String="default"):Void { + var contentSize = geometry.size.content.get(type); + if (contentSize == null || width != contentSize.width || height != contentSize.height) { + geometry.size.content.set(type, [width, height]); toUpdateParent(); } } @@ -123,6 +120,11 @@ class View implements IView { return this.skin; } + private function set_skinId(value:String):String { + r.skin.bind(value, this, "skin"); + return value; + } + private function set_visible(value:Bool):Bool { if (visible != value) { visible = value; diff --git a/src/main/haxework/gui/core/Geometry.hx b/src/main/haxework/gui/core/Geometry.hx index f1244d1..1fd5bbd 100644 --- a/src/main/haxework/gui/core/Geometry.hx +++ b/src/main/haxework/gui/core/Geometry.hx @@ -29,7 +29,7 @@ abstract ASizeValue(SizeValue) { } class SizeSet { - public var content(default, default):Size; + public var content(default, default):Map; public var fixed(default, default):Size; public var percent(default, default):Size; public var stretch(null, set):Bool; @@ -38,7 +38,7 @@ class SizeSet { public var height(null, set):ASizeValue; public function new() { - this.content = []; + this.content = new Map(); this.fixed = []; this.percent = []; } @@ -96,7 +96,9 @@ class Geometry { } var result = size.fixed.width; if (result < 0) { - result = size.content.width; + for (s in size.content.iterator()) { + result = Math.max(result, s.width); + } } result += padding.horizontal; return FIXED(result); @@ -108,7 +110,9 @@ class Geometry { } var result = size.fixed.height; if (result < 0) { - result = size.content.height; + for (s in size.content.iterator()) { + result = Math.max(result, s.height); + } } result += padding.vertical; return FIXED(result); diff --git a/src/main/haxework/gui/frame/FrameSwitcher.hx b/src/main/haxework/gui/frame/FrameSwitcher.hx index e4af141..88d347e 100755 --- a/src/main/haxework/gui/frame/FrameSwitcher.hx +++ b/src/main/haxework/gui/frame/FrameSwitcher.hx @@ -5,7 +5,7 @@ import haxework.animate.IAnimate; import haxework.gui.IView; import haxework.gui.GroupView; -class FrameSwitcher extends GroupView implements IFrameSwitcher { +class FrameSwitcher extends GroupView { public var current(default, null):Null>; public var onSwitch:Signal> = new Signal(); @@ -39,6 +39,7 @@ class FrameSwitcher extends GroupView implements IFrameSwitcher { throw 'frame "$id" not found'; } addView(current); + update(); //ToDo: if (content.stage != null) content.stage.focus = cast(current, SpriteView).content; var onShowMethod:Dynamic = Reflect.field(current, "onShow"); diff --git a/src/main/haxework/gui/frame/IFrameSwitcher.hx b/src/main/haxework/gui/frame/IFrameSwitcher.hx deleted file mode 100755 index 9bde08f..0000000 --- a/src/main/haxework/gui/frame/IFrameSwitcher.hx +++ /dev/null @@ -1,8 +0,0 @@ -package haxework.gui.frame; - -import haxework.gui.IView; - -interface IFrameSwitcher extends IView { - public var current(default, null):Null>; - public function change(id:String):IView; -} diff --git a/src/main/haxework/gui/layout/HorizontalLayout.hx b/src/main/haxework/gui/layout/HorizontalLayout.hx index 4175657..109f851 100755 --- a/src/main/haxework/gui/layout/HorizontalLayout.hx +++ b/src/main/haxework/gui/layout/HorizontalLayout.hx @@ -29,8 +29,7 @@ class HorizontalLayout extends DefaultLayout { maxSize = Math.max(maxSize, view.height); } - group.geometry.size.content.height = maxSize; - group.geometry.size.content.width = fixedSize; + group.geometry.size.content.set("group", [fixedSize, maxSize]); leftSize -= fixedSize; for (view in views) { diff --git a/src/main/haxework/gui/layout/TailLayout.hx b/src/main/haxework/gui/layout/TailLayout.hx index a9fe8cc..e0e0e67 100644 --- a/src/main/haxework/gui/layout/TailLayout.hx +++ b/src/main/haxework/gui/layout/TailLayout.hx @@ -1,5 +1,6 @@ package haxework.gui.layout; +import haxework.gui.core.VAlign; typedef Row = { var width:Float; var height:Float; @@ -50,10 +51,18 @@ class TailLayout extends DefaultLayout { var y:Float = Math.max(group.geometry.padding.top, (group.height - h) / 2); y = group.geometry.padding.top; + if (h < group.height) { + y = switch vAlign { + case TOP | NONE: group.geometry.padding.top; + case MIDDLE: (group.height - h) / 2; + case BOTTOM: group.height - h - group.geometry.padding.bottom; + } + } for (row in rows) { placeRow(group, y, row); y += row.height + margin; } - group.geometry.size.content.height = h; + + group.geometry.size.content.set("group", [-1, h]); } } diff --git a/src/main/haxework/gui/layout/VerticalLayout.hx b/src/main/haxework/gui/layout/VerticalLayout.hx index 01fad4b..8271d80 100755 --- a/src/main/haxework/gui/layout/VerticalLayout.hx +++ b/src/main/haxework/gui/layout/VerticalLayout.hx @@ -25,8 +25,7 @@ class VerticalLayout extends DefaultLayout { maxSize = Math.max(maxSize, view.width); } - group.geometry.size.content.width = maxSize; - group.geometry.size.content.height = fixedSize; + group.geometry.size.content.set("group", [maxSize, fixedSize]); leftSize -= fixedSize; for (view in views) { diff --git a/src/main/haxework/gui/list/LabelListItem.hx b/src/main/haxework/gui/list/LabelListItem.hx index f2b4a7b..5755079 100644 --- a/src/main/haxework/gui/list/LabelListItem.hx +++ b/src/main/haxework/gui/list/LabelListItem.hx @@ -2,24 +2,32 @@ package haxework.gui.list; import haxework.gui.core.HAlign; import haxework.gui.list.ListView.IListItemView; -import haxework.gui.skin.ColorSkin; + +private typedef Formatter = Int -> T -> String; class LabelListItem extends LabelView implements IListItemView { public var item_index(default, default):Int; public var data(default, set):T; + public var formatter(default, default):Formatter; - public function new() { + private static function defaultFormatter(index:Int, value:T):String { + return Std.string(value); + } + + public function new(formatter:Formatter = null) { super(); + this.formatter = formatter == null ? defaultFormatter : formatter; geometry.size.percent.width = 100; geometry.size.fixed.height = 20; geometry.padding = 8; - layout.hAlign = HAlign.LEFT; + layout.hAlign = LEFT; } + private function set_data(value:T):T { data = value; - text = Std.string(value); - skin = item_index % 2 == 1 ? [new ColorSkin(0xdddddd)] : [new ColorSkin(0xcccccc)]; + text = formatter(item_index, value); + skinId = 'text${item_index % 2}'; return value; } } diff --git a/src/main/haxework/gui/list/ListView.hx b/src/main/haxework/gui/list/ListView.hx index 983990a..8067918 100755 --- a/src/main/haxework/gui/list/ListView.hx +++ b/src/main/haxework/gui/list/ListView.hx @@ -12,7 +12,7 @@ import haxework.utils.NumberUtil; class ListView extends GroupView { public var data(default, set):Array; - public var factory(null, default):Class>; + public var factory(null, default):Void->IListItemView; public var offset(default, set):Int; private var offsetDiff(default, set):Float; @@ -183,7 +183,7 @@ class ListView extends GroupView { override public function update():Void { super.update(); - recalcSize(Type.createInstance(factory, [])); + recalcSize(factory()); render(); } @@ -195,7 +195,7 @@ class ListView extends GroupView { var diff:Int = size - items.length; if (diff > 0) { for (i in 0...diff) { - var item:IListItemView = Type.createInstance(factory, []); + var item:IListItemView = factory(); items.push(item); setClickListener(item); box.addView(item); diff --git a/src/main/haxework/gui/popup/PopupManager.hx b/src/main/haxework/gui/popup/PopupManager.hx index bff027d..31c1ce5 100755 --- a/src/main/haxework/gui/popup/PopupManager.hx +++ b/src/main/haxework/gui/popup/PopupManager.hx @@ -4,44 +4,43 @@ import haxework.animate.IAnimate; import haxework.gui.Root; import haxework.gui.IGroupView; +typedef P = PopupView; + class PopupManager { - public var showAnimateFactory(default, default):Class; - public var closeAnimateFactory(default, default):Class; + public var showAnimateFactory(default, default):Class; + public var closeAnimateFactory(default, default):Class; - private var popups:Array>; + private var popups:Array

; - public function new() { - popups = new Array>(); - } - - public function show(popup:PopupView):Void { - cast(Root.instance.view, IGroupView).addView(popup); - if (showAnimateFactory != null) { - Type.createInstance(showAnimateFactory, [popup]).start(null); + public function new() { + popups = new Array

(); } - popups.push(popup); - popup.onShow(); - } - public function close(popup:PopupView):Void { - popups.remove(popup); - if (closeAnimateFactory != null) { - Type.createInstance(closeAnimateFactory, [popup]).start(function(_) { - cast(Root.instance.view, IGroupView).removeView(popup); - popup.onClose(); - }); - } else { - cast(Root.instance.view, IGroupView).removeView(popup); - popup.onClose(); + public function show(popup:P):Void { + cast(Root.instance.view, IGroupView).addView(popup); + if (showAnimateFactory != null) { + Type.createInstance(showAnimateFactory, [popup]).start(null); + } + popups.push(popup); } - } - public function closeTop():Bool { - if (popups.length > 0) { - close(popups[popups.length - 1]); - return true; + public function close(popup:P):Void { + popups.remove(popup); + if (closeAnimateFactory != null) { + Type.createInstance(closeAnimateFactory, [popup]).start(function(_) { + cast(Root.instance.view, IGroupView).removeView(popup); + }); + } else { + cast(Root.instance.view, IGroupView).removeView(popup); + } + } + + public function closeTop():Bool { + if (popups.length > 0) { + close(popups[popups.length - 1]); + return true; + } + return false; } - return false; - } } diff --git a/src/main/haxework/gui/popup/PopupView.hx b/src/main/haxework/gui/popup/PopupView.hx index 117abb1..14412a7 100755 --- a/src/main/haxework/gui/popup/PopupView.hx +++ b/src/main/haxework/gui/popup/PopupView.hx @@ -1,56 +1,49 @@ package haxework.gui.popup; -import promhx.Deferred; -import haxe.Timer; -import haxework.provider.Provider; -import haxework.dispath.Dispatcher; -import haxework.dispath.IDispatcher; -import haxework.gui.IGroupView; -import haxework.gui.ButtonView; -import haxework.gui.skin.ColorSkin; +import haxework.gui.core.Geometry.Position; import haxework.gui.GroupView; +import haxework.gui.skin.Skin; +import promhx.Deferred; +import promhx.Promise; -class PopupView extends GroupView { +class PopupView extends GroupView { + @:provide var manager:PopupManager; - private var buttonId:String; - private var contentView:V; - private var deferred:Deferred; + public var view(default, set):IView; + private var deferred:Deferred; - public function new(contentViewFactory:Class) { - super(); - - pWidth = 100; - pHeight = 100; - inLayout = false; - skin = new ColorSkin(0x000000, 0.6); - - this.contentView = Type.createInstance(contentViewFactory, [{listener:this}]); - addView(contentView); - } - - public function onPress(button:ButtonView) { - this.buttonId = button.id; - close(); - } - - public function show():Deferred { - Provider.get(PopupManager).show(this); - deferred = new Deferred(); - return deferred; - } - - public function close():Void { - Provider.get(PopupManager).close(this); - } - - public function onShow():Void { - buttonId = "close"; - } - - public function onClose():Void { - if (deferred != null) { - deferred.resolve(buttonId); - deferred = null; + public function new() { + super(); + geometry.size.stretch = true; + geometry.position = Position.ABSOLUTE; + skin = [Skin.color(0x000000, 0.6)]; + } + + private function set_view(value:IView):IView { + this.view = value; + this.views = [value]; + return this.view; + } + + public function show():Promise { + manager.show(this); + deferred = new Deferred(); + return deferred.promise(); + } + + public function close(result:R):Void { + manager.close(this); + if (deferred != null) { + deferred.resolve(result); + deferred = null; + } + } + + public function reject(reason:Dynamic):Void { + manager.close(this); + if (deferred != null) { + deferred.throwError(reason); + deferred = null; + } } - } } diff --git a/src/main/haxework/gui/skin/BitmapSkin.hx b/src/main/haxework/gui/skin/BitmapSkin.hx index f3b6115..e6a7049 100755 --- a/src/main/haxework/gui/skin/BitmapSkin.hx +++ b/src/main/haxework/gui/skin/BitmapSkin.hx @@ -32,6 +32,6 @@ class BitmapSkin implements ISkin implements ISizeSkin { public function draw(view:SpriteView):Void { if (image == null) return; - DrawUtil.draw(view.content.graphics, image, new Rectangle(0, 0, view.width, view.height), fillType, color); + DrawUtil.draw(view.content.graphics, image, new Rectangle(0, 0, view.width, view.height), fillType, color, false); } } diff --git a/src/main/haxework/gui/skin/BorderSkin.hx b/src/main/haxework/gui/skin/BorderSkin.hx index 375c903..092ba79 100644 --- a/src/main/haxework/gui/skin/BorderSkin.hx +++ b/src/main/haxework/gui/skin/BorderSkin.hx @@ -14,7 +14,7 @@ class BorderSkin implements ISkin { public function draw(view:SpriteView):Void { view.content.graphics.lineStyle(tickness, color, alpha, true); - view.content.graphics.drawRect(tickness, tickness, view.width - tickness * 2, view.height - tickness * 2); + view.content.graphics.drawRect(tickness / 2, tickness / 2, view.width - tickness, view.height - tickness); view.content.graphics.lineStyle(); } } diff --git a/src/main/haxework/gui/utils/BitmapUtil.hx b/src/main/haxework/gui/utils/BitmapUtil.hx index da5357c..c3d16cb 100755 --- a/src/main/haxework/gui/utils/BitmapUtil.hx +++ b/src/main/haxework/gui/utils/BitmapUtil.hx @@ -7,40 +7,40 @@ import flash.display.BitmapData; class BitmapUtil { - private static var cache:Map> = new Map>(); + private static var cache:Map> = new Map>(); - private static function fromCache(image:BitmapData, key:String):Null { - return cache.exists(image) && cache.get(image).exists(key) ? cache.get(image).get(key) : null; - } + private static function fromCache(image:BitmapData, key:String):Null { + return cache.exists(image) && cache.get(image).exists(key) ? cache.get(image).get(key) : null; + } - private static function toCache(image:BitmapData, key:String, value:BitmapData):Void { - if (!cache.exists(image)) cache.set(image, new Map()); - cache.get(image).set(key, value); - } + private static function toCache(image:BitmapData, key:String, value:BitmapData):Void { + if (!cache.exists(image)) cache.set(image, new Map()); + cache.get(image).set(key, value); + } - public static function multiply(image:BitmapData, m:Float):BitmapData { - var result = fromCache(image, "multiply:" + m); - if (result != null) return result; - var ct:ColorTransform = new ColorTransform(m, m, m, 1.0, 0, 0, 0); - var out:BitmapData = image.clone(); - out.colorTransform(out.rect, ct); - toCache(image, "multiply:" + m, out); - return out; - } + public static function multiply(image:BitmapData, m:Float):BitmapData { + var result = fromCache(image, "multiply:" + m); + if (result != null) return result; + var ct:ColorTransform = new ColorTransform(m, m, m, 1.0, 0, 0, 0); + var out:BitmapData = image.clone(); + out.colorTransform(out.rect, ct); + toCache(image, "multiply:" + m, out); + return out; + } - public static function grayscale(image:BitmapData, m:Float):BitmapData { - var result = fromCache(image, "grayscale:" + m); - if (result != null) return result; - var matrix:Array = []; - matrix = matrix.concat([m, m, m, 0, 0]); - matrix = matrix.concat([m, m, m, 0, 0]); - matrix = matrix.concat([m, m, m, 0, 0]); - matrix = matrix.concat([0, 0, 0, 1, 0]); - var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix); - var out:BitmapData = image.clone(); - out.applyFilter(out, out.rect, new Point(0,0), cmf); - toCache(image, "grayscale:" + m, out); - return out; - } -} \ No newline at end of file + public static function grayscale(image:BitmapData, m:Float):BitmapData { + var result = fromCache(image, "grayscale:" + m); + if (result != null) return result; + var matrix:Array = []; + matrix = matrix.concat([m, m, m, 0, 0]); + matrix = matrix.concat([m, m, m, 0, 0]); + matrix = matrix.concat([m, m, m, 0, 0]); + matrix = matrix.concat([0, 0, 0, 1, 0]); + var cmf:ColorMatrixFilter = new ColorMatrixFilter(matrix); + var out:BitmapData = image.clone(); + out.applyFilter(out, out.rect, new Point(0, 0), cmf); + toCache(image, "grayscale:" + m, out); + return out; + } +} diff --git a/src/main/haxework/gui/utils/ColorUtils.hx b/src/main/haxework/gui/utils/ColorUtils.hx index fd7044e..e01a9a6 100755 --- a/src/main/haxework/gui/utils/ColorUtils.hx +++ b/src/main/haxework/gui/utils/ColorUtils.hx @@ -2,30 +2,30 @@ package haxework.gui.utils; class ColorUtils { - public static function multiply(color:Int, m:Float):Int { - var rgb:Array = color2rgb(color); - var red:Int = cast Math.min(255, Math.round(rgb[0] * m)); - var green:Int = cast Math.min(255, Math.round(rgb[1] * m)); - var blue:Int = cast Math.min(255, Math.round(rgb[2] * m)); - return rgb2color(red, green, blue); - } + public static function multiply(color:Int, m:Float):Int { + var rgb:Array = color2rgb(color); + var red:Int = cast Math.min(255, Math.round(rgb[0] * m)); + var green:Int = cast Math.min(255, Math.round(rgb[1] * m)); + var blue:Int = cast Math.min(255, Math.round(rgb[2] * m)); + return rgb2color(red, green, blue); + } - public static function diff(color:Int, d:Int):Int { - var rgb:Array = color2rgb(color); - var red:Int = cast Math.max(0, Math.min(255, rgb[0] + d)); - var green:Int = cast Math.max(0, Math.min(255, rgb[1] + d)); - var blue:Int = cast Math.max(0, Math.min(255, rgb[2] + d)); - return rgb2color(red, green, blue); - } + public static function diff(color:Int, d:Int):Int { + var rgb:Array = color2rgb(color); + var red:Int = cast Math.max(0, Math.min(255, rgb[0] + d)); + var green:Int = cast Math.max(0, Math.min(255, rgb[1] + d)); + var blue:Int = cast Math.max(0, Math.min(255, rgb[2] + d)); + return rgb2color(red, green, blue); + } - public static function rgb2color(red:Int, green:Int, blue:Int):Int { - return (red << 16) + (green << 8) + blue; - } + public static function rgb2color(red:Int, green:Int, blue:Int):Int { + return (red << 16) + (green << 8) + blue; + } - public static function color2rgb(color:Int):Array { - var red:Int = ((color & 0xFF0000) >>> 16); - var green:Int = ((color & 0x00FF00) >> 8); - var blue:Int = (color & 0x0000FF); - return [red, green, blue]; - } -} \ No newline at end of file + public static function color2rgb(color:Int):Array { + var red:Int = ((color & 0xFF0000) >>> 16); + var green:Int = ((color & 0x00FF00) >> 8); + var blue:Int = (color & 0x0000FF); + return [red, green, blue]; + } +} diff --git a/src/main/haxework/net/order/IOrderSupplier.hx b/src/main/haxework/net/order/IOrderSupplier.hx deleted file mode 100755 index 32c714a..0000000 --- a/src/main/haxework/net/order/IOrderSupplier.hx +++ /dev/null @@ -1,18 +0,0 @@ -package haxework.net.order; - -import promhx.Promise; -import promhx.Deferred; - -typedef Order = { - var id:String; - var data:Null; - var deferred:Deferred; - var clients:Int; -} - -interface IOrderSupplier { - public var orders(default, null):Map>; - - public function request(url:String, clazz:Class):Promise; - public function release(url:String, force:Bool = false):Void; -} \ No newline at end of file diff --git a/src/main/haxework/net/order/OrderSupplier.hx b/src/main/haxework/net/order/OrderSupplier.hx deleted file mode 100755 index e9d7633..0000000 --- a/src/main/haxework/net/order/OrderSupplier.hx +++ /dev/null @@ -1,82 +0,0 @@ -package haxework.net.order; - -import promhx.Promise; -import promhx.Deferred; -import haxework.net.order.IOrderSupplier.Order; -import flash.display.BitmapData; - -class OrderSupplier implements IOrderSupplier { - - private static inline var TAG:String = "OrderSupplier"; - - public var orders(default, null):Map>; - - public function new() { - orders = new Map>(); - } - - public function request(url:String, clazz:Class):Promise { - if (orders.exists(url)) { - var order:Order = orders.get(url); - order.clients++; - //L.d(TAG, "Request(" + order.clients + "): " + url); - return order.deferred.promise(); - } else { - var deferred = new Deferred(); - var order:Order = { - id:url, - data:null, - deferred:deferred, - clients:1 - } - //L.d(TAG, "Request(" + order.clients + "): " + url); - orders.set(url, order); - var loader:ILoader = buildLoader(clazz); - loader.GET(url) - .then(function(data:T):Void { - if (orders.exists(url)) { - var order:Order = orders.get(url); - order.data = data; - order.deferred.resolve(data); - } - }) - .catchError(function(error:Dynamic):Void { - if (orders.exists(url)) orders.get(url).deferred.throwError(error); - orders.remove(url); - }); - return deferred.promise(); - } - } - - public function release(url:String, force:Bool = false):Void { - if (orders.exists(url)) { - var order:Order = orders.get(url); - if (--order.clients <= 0 || force) { - var data:Dynamic = order.data; - if (data != null && Std.is(data, BitmapData)) { - cast(data, BitmapData).dispose(); - } - orders.remove(url); - } - L.d(TAG, "Release(" + order.clients + "): " + url); - //log(); - } - } - - private function buildLoader(clazz:Class):ILoader { - var c:Class = clazz; - return if (c == BitmapData) { - var loader:ILoader = cast new ImageLoader(); - loader.timeout = 7000; //ToDo: hardcode timeout for loading images - loader; - } else { - throw "Unsupported order: " + c; - } - } - - private function log():Void { - L.d(TAG, "\n" + Lambda.map(orders, function(order:Order):String { - return "(" + order.clients + ") " + order.id; - }).join("\n")); - } -} \ No newline at end of file diff --git a/src/main/haxework/resources/IResources.hx b/src/main/haxework/resources/IResources.hx index d6057b9..7c965da 100755 --- a/src/main/haxework/resources/IResources.hx +++ b/src/main/haxework/resources/IResources.hx @@ -1,17 +1,17 @@ package haxework.resources; -import haxework.gui.skin.ISkin.SkinSet; -import flash.display.MovieClip; -import haxework.resources.Resources.ResMap; import flash.display.BitmapData; +import flash.display.MovieClip; +import haxework.gui.skin.ISkin.SkinSet; +import haxework.resources.Resources.ResMap; interface IResources { - public var image(default, null):ResMap; - public var color(default, null):ResMap; - public var movie(default, null):ResMap; - public var text(default, null):ResMap; - public var float(default, null):ResMap; - public var int(default, null):ResMap; - public var any(default, null):ResMap; - public var skin(default, null):ResMap; + public var image(default, null):ResMap; + public var color(default, null):ResMap; + public var movie(default, null):ResMap; + public var text(default, null):ResMap; + public var float(default, null):ResMap; + public var int(default, null):ResMap; + public var any(default, null):ResMap; + public var skin(default, null):ResMap; } diff --git a/src/main/haxework/resources/Resources.hx b/src/main/haxework/resources/Resources.hx index 9cac845..f1a99ba 100755 --- a/src/main/haxework/resources/Resources.hx +++ b/src/main/haxework/resources/Resources.hx @@ -1,20 +1,19 @@ package haxework.resources; -import haxework.gui.skin.ISkin.SkinSet; import flash.display.BitmapData; import flash.display.MovieClip; import haxe.ds.StringMap; -import haxework.core.Tuple; +import haxework.gui.skin.ISkin; -private typedef F = Tuple2 +private typedef Listener = {object:Dynamic, field:String}; class ResMap extends StringMap { - private var listeners:Map>; + private var listeners:StringMap>; public function new() { super(); - listeners = new Map>(); + listeners = new StringMap(); } public function put(key:String, value:T):Void { @@ -25,17 +24,18 @@ class ResMap extends StringMap { } public function bind(key:String, object:Dynamic, field:String):Void { - var f:F = Tuple.two(object, field); + var listener:Listener = {object:object, field:field}; if (listeners.exists(key)) { - listeners.get(key).push(f); + listeners.set(key, listeners.get(key).filter(function(l) return l.object != object || l.field != field)); + listeners.get(key).push(listener); } else { - listeners.set(key, [f]); + listeners.set(key, [listener]); } - if (exists(key)) call(f, get(key)); + if (exists(key)) call(listener, get(key)); } - private function call(field:F, value:T):Void { - Reflect.setProperty(field.first, field.second, value); + private function call(listener:Listener, value:T):Void { + Reflect.setProperty(listener.object, listener.field, value); } public function merge(value:Dynamic):Void {