From 7f172ab7a0194d7d6bb93692ab9ca6df11ae91cf Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 13 Jul 2015 14:59:40 +0300 Subject: [PATCH] view builder update --- haxework/gui/build/Builder.hx | 31 +++++++++++++++--------- haxework/gui/build/BuilderUtil.hx | 2 +- haxework/gui/build/PositionJsonParser.hx | 12 ++++++--- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/haxework/gui/build/Builder.hx b/haxework/gui/build/Builder.hx index cd7beac..52c34bc 100755 --- a/haxework/gui/build/Builder.hx +++ b/haxework/gui/build/Builder.hx @@ -41,10 +41,11 @@ class Builder { private function getPosition(?position:JsonKeyPosition):Position { var min = position == null ? 1 : position.min + 32; // :-( var max = position == null ? 1 : position.max + 32; - return Context.makePosition({min:min, max:max, file:templateFile}); + var file = position == null || position.file == null ? templateFile : position.file; + return Context.makePosition({min:min, max:max, file:file}); } - private function specialValue(name:String, key:String, a:Array):Dynamic { + private function specialValue(name:String, key:String, a:Array, position:JsonKeyPosition):Dynamic { return switch (a[0]) { case "asset": switch (a[1]) { @@ -56,7 +57,7 @@ class Builder { case "res": var res = "haxework.provider.Provider.get(haxework.resources.IResources)." + a[1]; var bindExpr = res + ".bind(\"" + a[2] + "\", " + name + ", \"" + key + "\")"; - exprs.push(Context.parse(bindExpr, getPosition())); + exprs.push(Context.parse(bindExpr, getPosition(position))); //res + ".get(\"" + a[2] + "\")"; null; case "locale": @@ -65,7 +66,7 @@ class Builder { a[1]; case "layout": var template = BuilderUtil.loadFile(a[1]); - return getValue(name, key, template); + return getValue(name, key, template, position); case "link": "(links == null) ? untyped this : Reflect.field(links, \"" + a[1] + "\")"; case _: @@ -73,12 +74,14 @@ class Builder { } } - private function getValue(name:String, key:String, value:Dynamic):Dynamic { + private function getValue(name:String, key:String, value:Dynamic, position:JsonKeyPosition):Dynamic { return if (Std.is(value, Array)) { - value.map(function(v) { return getValue(null, null, v); }); + value.map(function(v) { + return getValue(null, null, v, position); + }); } else if (Std.is(value, String)) { if (value.charAt(0) == "@") { - specialValue(name, key, value.substring(1, value.length).split(":")); + specialValue(name, key, value.substring(1, value.length).split(":"), position); } else if (~/(0x|#)[A-Fa-f\d]{6}/.match(value)) { Std.parseInt(StringTools.replace(Std.string(value), "#", "0x")); } else { @@ -90,11 +93,12 @@ class Builder { if (Reflect.hasField(value, "type")) { var n = "a" + i++; var type = Reflect.field(value, "type"); - exprs.push(Context.parse("var " + n + " = new " + type + "()", getPosition())); + exprs.push(Context.parse("var " + n + " = new " + type + "()", getPosition(position))); createElement(value, n); n; } else { - value; + Context.error("Need type field", getPosition(position)); + null; } } else { value; @@ -104,8 +108,11 @@ class Builder { private function createElement(template:Dynamic, name:String):String { if (Reflect.hasField(template, "style")) { var s = Reflect.field(style, Reflect.field(template, "style")); - for (key in Reflect.fields(s)) if (!Reflect.hasField(template, key)) { - Reflect.setField(template, key, Reflect.field(s, key)); + for (key in Reflect.fields(s)) { + if (key.charAt(0) != "$" && !Reflect.hasField(template, key)) { + Reflect.setField(template, key, Reflect.field(s, key)); + Reflect.setField(template, "$" + key, Reflect.field(s, "$" + key)); + } } } @@ -129,7 +136,7 @@ class Builder { for (key in Reflect.fields(template)) { if (key.charAt(0) == "$" || ["type", "style"].indexOf(key) > -1) continue; var position = Reflect.field(template, "$" + key); - var value = getValue(name, key, Reflect.field(template, key)); + var value = getValue(name, key, Reflect.field(template, key), position); if (value != null) { exprs.push(Context.parse(name + "." + key + " = " + value, getPosition(position))); } diff --git a/haxework/gui/build/BuilderUtil.hx b/haxework/gui/build/BuilderUtil.hx index b13c729..7c6c125 100755 --- a/haxework/gui/build/BuilderUtil.hx +++ b/haxework/gui/build/BuilderUtil.hx @@ -11,7 +11,7 @@ class BuilderUtil { Context.registerModuleDependency(Context.getLocalModule(), path); var content = sys.io.File.getContent(path); Context.parse(content, Context.makePosition({min:0, max:0, file:path})); - return json ? PositionJsonParser.parse(content) : content; + return json ? PositionJsonParser.parse(content, path) : content; } public static function getMeta(key:String):Array { diff --git a/haxework/gui/build/PositionJsonParser.hx b/haxework/gui/build/PositionJsonParser.hx index bb6d45f..bac1733 100755 --- a/haxework/gui/build/PositionJsonParser.hx +++ b/haxework/gui/build/PositionJsonParser.hx @@ -3,26 +3,30 @@ package haxework.gui.build; typedef JsonKeyPosition = { var min:Int; var max:Int; + var file:String; } class PositionJsonParser { - static public inline function parse(str : String) : Dynamic { - return new PositionJsonParser(str).parseRec(); + static public inline function parse(str : String, file : String) : Dynamic { + return new PositionJsonParser(str, file).parseRec(); } var str : String; var pos : Int; + var file : String; - function new( str : String ) { + function new( str : String, file:String ) { this.str = str; this.pos = 0; + this.file = file; } function getKeyPosition():JsonKeyPosition { return { min: pos, - max: pos + max: pos, + file: file } }