added GuiBuilder
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
-main examples.ViewExample
|
||||
-swf target/ViewExample.swf
|
||||
-debug
|
||||
-debug
|
||||
-dce no
|
||||
@@ -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<Sprite> = new GroupView();
|
||||
group.layoutVAlign = VAlign.MIDDLE;
|
||||
group.layoutHAlign = HAlign.CENTER;
|
||||
group.skin = new ColorSkin(0xffff00);
|
||||
var view:IView<Sprite> = 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<Sprite> = GuiBuilder.build(form);
|
||||
new Root(v);
|
||||
}
|
||||
}
|
||||
20
examples/form.json
Executable file
20
examples/form.json
Executable file
@@ -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"}
|
||||
}
|
||||
]
|
||||
}
|
||||
14
haxework/asset/JsonAsset.hx
Executable file
14
haxework/asset/JsonAsset.hx
Executable file
@@ -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));
|
||||
}
|
||||
}
|
||||
13
haxework/asset/StringAsset.hx
Executable file
13
haxework/asset/StringAsset.hx
Executable file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<Sprite> {
|
||||
|
||||
public var views(default, null):Array<IView<Sprite>>;
|
||||
public var views(default, set):Array<IView<Sprite>>;
|
||||
public var layout(default, default):ILayout;
|
||||
|
||||
public var layoutVAlign(default, set):VAlign;
|
||||
@@ -40,6 +41,12 @@ class GroupView extends View implements IGroupView<Sprite> {
|
||||
super.update();
|
||||
}
|
||||
|
||||
public function set_views(value:Array<IView<Sprite>>):Array<IView<Sprite>> {
|
||||
if (views == null) views = [];
|
||||
for (view in value) addView(view);
|
||||
return views;
|
||||
}
|
||||
|
||||
public function addView(view:IView<Sprite>):IView<Sprite> {
|
||||
views.push(view);
|
||||
viewsById.set(view.id, view);
|
||||
|
||||
47
haxework/gui/GuiBuilder.hx
Executable file
47
haxework/gui/GuiBuilder.hx
Executable file
@@ -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<String> = Reflect.fields(data);
|
||||
for (field in fields) {
|
||||
var value:Dynamic = Reflect.field(data, field);
|
||||
if (Std.is(value, Array)) {
|
||||
var a:Array<Dynamic> = [];
|
||||
for (o in cast(value, Array<Dynamic>)) 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<String> = s.substr(1).split(":");
|
||||
var e:Enum<Dynamic> = 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<Dynamic> = Type.resolveClass(type);
|
||||
var instance:Dynamic = Type.createInstance(clazz, []);
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@@ -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<Sprite>;
|
||||
|
||||
public function new(view:IView<Sprite>) {
|
||||
this.view = view;
|
||||
Lib.current.addChild(view.content);
|
||||
var content:DisplayObject = view.content;
|
||||
if (content.stage == null) {
|
||||
content.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
|
||||
|
||||
Reference in New Issue
Block a user