diff --git a/build.hxml b/build.hxml index 86f6fab..df3fca8 100755 --- a/build.hxml +++ b/build.hxml @@ -1,3 +1,4 @@ -main examples.ViewExample -swf target/ViewExample.swf --debug \ No newline at end of file +-debug +-dce no \ No newline at end of file diff --git a/examples/ViewExample.hx b/examples/ViewExample.hx index 9bb6837..e344055 100755 --- a/examples/ViewExample.hx +++ b/examples/ViewExample.hx @@ -1,40 +1,19 @@ package examples; +import haxework.gui.GuiBuilder; +import haxework.asset.JsonAsset; import haxework.gui.Root; -import haxework.gui.core.HAlign; -import haxework.gui.core.VAlign; -import flash.Lib; -import haxework.gui.skin.ColorSkin; -import haxework.gui.View; import haxework.gui.IView; import flash.display.Sprite; -import haxework.gui.GroupView; -import haxework.gui.IGroupView; + +@:file("examples/form.json") +class Form extends JsonAsset {} class ViewExample { public static function main() { - var group:IGroupView = new GroupView(); - group.layoutVAlign = VAlign.MIDDLE; - group.layoutHAlign = HAlign.CENTER; - group.skin = new ColorSkin(0xffff00); - var view:IView = new View(); - view.pWidth = 80; - view.pHeight = 80; - view.skin = new ColorSkin(0xff0000); - group.addView(view); - view = new View(); - view.width = 100; - view.height = 100; - view.skin = new ColorSkin(0x00ff00); - group.addView(view); - view = new View(); - view.width = 50; - view.height = 50; - view.skin = new ColorSkin(0x0000ff); - group.addView(view); - Lib.current.addChild(group.content); - - new Root(group); + var form:Dynamic = new Form().value; + var v:IView = GuiBuilder.build(form); + new Root(v); } } \ No newline at end of file diff --git a/examples/form.json b/examples/form.json new file mode 100755 index 0000000..ce08866 --- /dev/null +++ b/examples/form.json @@ -0,0 +1,20 @@ +{ + "type":"haxework.gui.GroupView", + "layoutHAlign":"~haxework.gui.core.HAlign:CENTER", + "layoutVAlign":"~haxework.gui.core.VAlign:MIDDLE", + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0xff0000"}, + "views":[ + { + "type":"haxework.gui.View", + "pWidth":70, + "pHeight":70, + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x00ff00"} + }, + { + "type":"haxework.gui.View", + "width":50, + "height":50, + "skin":{"type":"haxework.gui.skin.ColorSkin", "color":"0x0000ff"} + } + ] +} \ No newline at end of file diff --git a/haxework/asset/JsonAsset.hx b/haxework/asset/JsonAsset.hx new file mode 100755 index 0000000..87d4898 --- /dev/null +++ b/haxework/asset/JsonAsset.hx @@ -0,0 +1,14 @@ +package haxework.asset; + +import haxe.Json; +import flash.utils.ByteArray; + +class JsonAsset extends ByteArray { + + public var value(default, null):Dynamic; + + public function new() { + super(); + value = Json.parse(readUTFBytes(bytesAvailable)); + } +} diff --git a/haxework/asset/StringAsset.hx b/haxework/asset/StringAsset.hx new file mode 100755 index 0000000..17d03c6 --- /dev/null +++ b/haxework/asset/StringAsset.hx @@ -0,0 +1,13 @@ +package haxework.asset; + +import flash.utils.ByteArray; + +class StringAsset extends ByteArray { + + public var value(default, null):String; + + public function new() { + super(); + value = readUTFBytes(bytesAvailable); + } +} diff --git a/haxework/gui/GroupView.hx b/haxework/gui/GroupView.hx index b2f7f48..d20db42 100755 --- a/haxework/gui/GroupView.hx +++ b/haxework/gui/GroupView.hx @@ -1,5 +1,6 @@ package haxework.gui; +import flash.errors.Error; import haxework.gui.core.VAlign; import haxework.gui.core.HAlign; import haxework.gui.layout.DefaultLayout; @@ -8,7 +9,7 @@ import flash.display.Sprite; class GroupView extends View implements IGroupView { - public var views(default, null):Array>; + public var views(default, set):Array>; public var layout(default, default):ILayout; public var layoutVAlign(default, set):VAlign; @@ -40,6 +41,12 @@ class GroupView extends View implements IGroupView { super.update(); } + public function set_views(value:Array>):Array> { + if (views == null) views = []; + for (view in value) addView(view); + return views; + } + public function addView(view:IView):IView { views.push(view); viewsById.set(view.id, view); diff --git a/haxework/gui/GuiBuilder.hx b/haxework/gui/GuiBuilder.hx new file mode 100755 index 0000000..8e8511c --- /dev/null +++ b/haxework/gui/GuiBuilder.hx @@ -0,0 +1,47 @@ +package haxework.gui; + + +class GuiBuilder { + + private function new() {} + + public static function build(data:Dynamic):Dynamic { + var type:String = data.type; + Reflect.deleteField(data, "type"); + var object:Dynamic = instance(type); + fill(object, data); + return object; + } + + private static function fill(object:Dynamic, data: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)); + value = a; + } else if (Std.is(value, String)) { + var s:String = cast(value, String); + if (s.charAt(0) == "~") { + var a:Array = s.substr(1).split(":"); + var e:Enum = Type.resolveEnum(a[0]); + value = Type.createEnum(e, a[1]); + } + } else if (Std.is(value, Float)) { + + } else { + var o:Dynamic = build(value); + fill(o, value); + value = o; + } + Reflect.setProperty(object, field, value); + } + } + + private static function instance(type:String):Dynamic { + var clazz:Class = Type.resolveClass(type); + var instance:Dynamic = Type.createInstance(clazz, []); + return instance; + } +} diff --git a/haxework/gui/Root.hx b/haxework/gui/Root.hx index 5966de5..6a4a8aa 100755 --- a/haxework/gui/Root.hx +++ b/haxework/gui/Root.hx @@ -1,17 +1,23 @@ package haxework.gui; +import flash.Lib; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.display.DisplayObject; import flash.events.Event; import flash.display.Sprite; +//ToDo: +import haxework.gui.GroupView; +import haxework.gui.skin.ColorSkin; + class Root { private var view:IView; public function new(view:IView) { this.view = view; + Lib.current.addChild(view.content); var content:DisplayObject = view.content; if (content.stage == null) { content.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);