diff --git a/examples/ViewExample.hx b/examples/ViewExample.hx index e344055..bf4eadb 100755 --- a/examples/ViewExample.hx +++ b/examples/ViewExample.hx @@ -1,9 +1,10 @@ package examples; +import haxework.gui.IGroupView; +import haxework.gui.ButtonView; import haxework.gui.GuiBuilder; import haxework.asset.JsonAsset; import haxework.gui.Root; -import haxework.gui.IView; import flash.display.Sprite; @:file("examples/form.json") @@ -13,7 +14,12 @@ class ViewExample { public static function main() { var form:Dynamic = new Form().value; - var v:IView = GuiBuilder.build(form); + var listener = { + onPress:function(view:ButtonView):Void { + trace("onPress: " + view.id); + } + }; + var v:IGroupView = GuiBuilder.build(form, {listener:listener}); new Root(v); } } \ No newline at end of file diff --git a/examples/form.json b/examples/form.json index 02984ea..d072bb5 100755 --- a/examples/form.json +++ b/examples/form.json @@ -24,6 +24,7 @@ "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x0000ff"} }, { + "id":"panel", "type":"haxework.gui.HGroupView", "layoutHAlign":"~haxework.gui.core.HAlign:LEFT", "layoutVAlign":"~haxework.gui.core.VAlign:MIDDLE", @@ -34,27 +35,33 @@ "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xffff00"}, "views":[ { - "type":"haxework.gui.LabelView", + "id":"button1", + "type":"haxework.gui.ButtonView", "width":100, "pHeight":100, "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ffff"}, - "text":"Text1" + "text":"Text1", + "onPress":"#listener" }, { - "type":"haxework.gui.LabelView", + "id":"button2", + "type":"haxework.gui.ButtonView", "contentSize":true, "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ffff"}, "text":"Text2", "fontFamily":"Georgia", - "fontColor":"0xffffff" + "fontColor":"0xffffff", + "onPress":"#listener" }, { - "type":"haxework.gui.LabelView", + "id":"button3", + "type":"haxework.gui.ButtonView", "contentSize":true, "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ffff"}, "text":"Text 3333333333 ddd", "fontFamily":"Tahoma", - "fontColor":"0xff0000" + "fontColor":"0xff0000", + "onPress":"#listener" } ] } diff --git a/haxework/dispath/Dispatcher.hx b/haxework/dispath/Dispatcher.hx new file mode 100755 index 0000000..67530ac --- /dev/null +++ b/haxework/dispath/Dispatcher.hx @@ -0,0 +1,30 @@ +package haxework.dispath; + +import haxe.ds.ObjectMap; + +class Dispatcher implements IDispatcher { + + private var listeners(null, null):ObjectMap; + + public function new() { + listeners = new ObjectMap(); + } + + public function addListener(listener:L):Void { + listeners.set(listener, true); + } + + public function removeListener(listener:L):Bool { + return listeners.remove(listener); + } + + public function removeAllListeners():Void { + var i:Iterator = listeners.keys(); + while (i.hasNext()) listeners.remove(i.next()); + } + + public function dispatch(caller:L->Void):Void { + var i:Iterator = listeners.keys(); + while (i.hasNext()) caller(i.next()); + } +} \ No newline at end of file diff --git a/haxework/dispath/IDispatcher.hx b/haxework/dispath/IDispatcher.hx new file mode 100755 index 0000000..a9ef139 --- /dev/null +++ b/haxework/dispath/IDispatcher.hx @@ -0,0 +1,13 @@ +package haxework.dispath; + +import haxe.ds.ObjectMap; + +interface IDispatcher { + + private var listeners(null, null):ObjectMap; + + public function addListener(listener:L):Void; + public function removeListener(listener:L):Bool; + public function removeAllListeners():Void; + public function dispatch(caller:L->Void):Void; +} \ No newline at end of file diff --git a/haxework/gui/ButtonView.hx b/haxework/gui/ButtonView.hx new file mode 100755 index 0000000..5ee0e8e --- /dev/null +++ b/haxework/gui/ButtonView.hx @@ -0,0 +1,41 @@ +package haxework.gui; + +import flash.events.MouseEvent; +import haxework.dispath.Dispatcher; +import haxework.dispath.IDispatcher; + +enum ButtonState { + UP; + OVER; + DOWN; +} + +class ButtonView extends LabelView { + + public var state(default, null):ButtonState; + public var dispatcher(default, null):IDispatcher; + public var onPress(null, set):ButtonViewListener; + + public function new() { + super(); + state = ButtonState.UP; + dispatcher = new Dispatcher(); + content.addEventListener(MouseEvent.CLICK, onMouseClick); + } + + private function onMouseClick(event:MouseEvent):Void { + var self:ButtonView = this; + dispatcher.dispatch(function(listener:ButtonViewListener):Void { + listener.onPress(self); + }); + } + + private function set_onPress(value:ButtonViewListener):ButtonViewListener { + dispatcher.addListener(value); + return value; + } +} + +typedef ButtonViewListener = { + public function onPress(view:ButtonView):Void; +} \ No newline at end of file diff --git a/haxework/gui/GroupView.hx b/haxework/gui/GroupView.hx index d20db42..bd4902b 100755 --- a/haxework/gui/GroupView.hx +++ b/haxework/gui/GroupView.hx @@ -1,6 +1,5 @@ package haxework.gui; -import flash.errors.Error; import haxework.gui.core.VAlign; import haxework.gui.core.HAlign; import haxework.gui.layout.DefaultLayout; @@ -71,6 +70,25 @@ class GroupView extends View implements IGroupView { } } + public function findViewById>(id:String):Null { + var idd:Array = id.split(":"); + if (idd.length > 1) { + var id0 = idd.shift(); + if (viewsById.exists(id0)) { + var g:GroupView = findViewById(id0); + return g.findViewById(idd.join(":")); + } else { + return null; + } + } else { + if (viewsById.exists(id)) { + return cast viewsById.get(id); + } else { + return null; + } + } + } + private function set_layoutVAlign(value:VAlign):VAlign { if (layoutVAlign != value) { layoutVAlign = value; diff --git a/haxework/gui/GuiBuilder.hx b/haxework/gui/GuiBuilder.hx index e049ddf..cf01075 100755 --- a/haxework/gui/GuiBuilder.hx +++ b/haxework/gui/GuiBuilder.hx @@ -8,31 +8,34 @@ import haxework.gui.HGroupView; import haxework.gui.VGroupView; import haxework.gui.TextView; import haxework.gui.LabelView; +import haxework.gui.ButtonView; import haxework.gui.skin.ColorSkin; class GuiBuilder { private function new() {} - public static function build(data:Dynamic):Dynamic { + public static function build(data:Dynamic, links:Dynamic):Dynamic { var type:String = data.type; Reflect.deleteField(data, "type"); var object:Dynamic = instance(type); - fill(object, data); + fill(object, data, links); return object; } - private static function fill(object:Dynamic, data:Dynamic):Void { + private static function fill(object:Dynamic, data:Dynamic, links:Dynamic):Void { var fields:Array = Reflect.fields(data); for (field in fields) { var value:Dynamic = Reflect.field(data, field); if (Std.is(value, Array)) { var a:Array = []; - for (o in cast(value, Array)) a.push(build(o)); + for (o in cast(value, Array)) a.push(build(o, links)); value = a; } else if (Std.is(value, String)) { var s:String = cast(value, String); - if (s.charAt(0) == "~") { + if (s.charAt(0) == "#") { + value = Reflect.field(links, s.substr(1)); + } else if (s.charAt(0) == "~") { var a:Array = s.substr(1).split(":"); var e:Enum = Type.resolveEnum(a[0]); value = Type.createEnum(e, a[1]); @@ -44,8 +47,8 @@ class GuiBuilder { } else if (Std.is(value, Bool)) { } else { - var o:Dynamic = build(value); - fill(o, value); + var o:Dynamic = build(value, links); + fill(o, value, links); value = o; } Reflect.setProperty(object, field, value); diff --git a/haxework/gui/IGroupView.hx b/haxework/gui/IGroupView.hx index d6a384e..00f60a0 100755 --- a/haxework/gui/IGroupView.hx +++ b/haxework/gui/IGroupView.hx @@ -22,4 +22,5 @@ interface IGroupView extends IView { public function addView(view:IView):IView; public function removeView(view:IView):IView; public function removeViewById(id:String):IView; + public function findViewById>(id:String):Null; } \ No newline at end of file