From 1df0857db33fe0bf94979768c24031cddb687317 Mon Sep 17 00:00:00 2001 From: shmyga Date: Thu, 2 Jul 2015 15:09:21 +0300 Subject: [PATCH] update --- haxework/gui/ViewBuilder.hx | 122 ++++++++++++++++------------- haxework/gui/popup/PopupManager.hx | 8 +- haxework/gui/popup/PopupView.hx | 6 +- 3 files changed, 74 insertions(+), 62 deletions(-) diff --git a/haxework/gui/ViewBuilder.hx b/haxework/gui/ViewBuilder.hx index 1798619..40230d8 100644 --- a/haxework/gui/ViewBuilder.hx +++ b/haxework/gui/ViewBuilder.hx @@ -7,53 +7,37 @@ import haxe.macro.Context; @:remove @:autoBuild(haxework.gui.ViewBuilderImpl.build()) extern interface ViewBuilder {} +typedef BuildData = { + var fields:Array; + var exprs:Array; + var style:Dynamic; +} + class ViewBuilderImpl { #if macro - static function loadFileAsString(path:String, json:Bool = true) { - try { - var p = Context.resolvePath(path); - Context.registerModuleDependency(Context.getLocalModule(), p); - var content = sys.io.File.getContent(p); - return json ? Json.parse(content) : content; - } - catch(e:Dynamic) { - return haxe.macro.Context.error("Failed to load file $path: $e", Context.currentPos()); - } + static function loadFile(path:String, json:Bool = true) { + var p = Context.resolvePath(path); + Context.registerModuleDependency(Context.getLocalModule(), p); + var content = sys.io.File.getContent(p); + return json ? Json.parse(content) : content; } - private static function getTemplate():Array { - var template = null; - var style = null; + private static function getMeta(key:String):Array { var c = Context.getLocalClass().get(); - for (m in c.meta.get()) { - if (m.name == ":template") { - template = switch(m.params[0].expr) { + for (meta in c.meta.get()) { + if (meta.name == key) { + return meta.params.map(function(param) return switch(param.expr) { case ExprDef.EConst(Constant.CString(value)): value; case _: null; - } - if (template != null) { - var t = template.split("@"); - template = loadFileAsString(t[0]); - if (t[1] != null) template = Reflect.field(template, t[1]); - } - style = switch(m.params[1].expr) { - case ExprDef.EConst(Constant.CString(value)): value; - case _: null; - } - if (style != null) { - style = loadFileAsString(style); - } + }); } } - return [ - template, - style - ]; + return []; } private static var i = 0; - private static function specialValue(data:Array, name:String, style:Dynamic, key:String, a:Array):Dynamic { + private static function specialValue(data:BuildData, name:String, key:String, a:Array):Dynamic { return switch (a[0]) { case "asset": switch (a[1]) { @@ -65,7 +49,7 @@ class ViewBuilderImpl { case "res": var res = "haxework.provider.Provider.get(haxework.resources.IResources)." + a[1]; var bindExpr = res + ".bind(\"" + a[2] + "\", " + name + ", \"" + key + "\")"; - data.push(Context.parse(bindExpr, Context.currentPos())); + data.exprs.push(Context.parse(bindExpr, Context.currentPos())); //res + ".get(\"" + a[2] + "\")"; null; case "locale": @@ -73,8 +57,8 @@ class ViewBuilderImpl { case "class": a[1]; case "layout": - var template = Json.parse(loadFileAsString(a[1])); - return getValue(data, name, style, key, template); + var template = loadFile(a[1]); + return getValue(data, name, key, template); case "link": "(links == null) ? untyped this : Reflect.field(links, \"" + a[1] + "\")"; case _: @@ -82,12 +66,12 @@ class ViewBuilderImpl { } } - private static function getValue(data:Array, name:String, style:Dynamic, key:String, value:Dynamic):Dynamic { + private static function getValue(data:BuildData, name:String, key:String, value:Dynamic):Dynamic { return if (Std.is(value, Array)) { - value.map(function(v) { return getValue(data, null, style, null, v); }); + value.map(function(v) { return getValue(data, null, null, v); }); } else if (Std.is(value, String)) { if (value.charAt(0) == "@") { - specialValue(data, name, style, key, value.substring(1, value.length).split(":")); + specialValue(data, name, key, value.substring(1, value.length).split(":")); } else if (~/(0x|#)[A-Fa-f\d]{6}/.match(value)) { Std.parseInt(StringTools.replace(Std.string(value), "#", "0x")); } else { @@ -99,8 +83,8 @@ class ViewBuilderImpl { if (Reflect.hasField(value, "type")) { var n = "a" + i++; var type = Reflect.field(value, "type"); - data.push(Context.parse("var " + n + " = new " + type + "()", Context.currentPos())); - createElement(data, value, n, style); + data.exprs.push(Context.parse("var " + n + " = new " + type + "()", Context.currentPos())); + createElement(data, value, n); n; } else { value; @@ -110,30 +94,58 @@ class ViewBuilderImpl { } } - private static function createElement(data:Array, template:Dynamic, name:String, style:Dynamic):String { + private static function createElement(data:BuildData, template:Dynamic, name:String):String { if (Reflect.hasField(template, "style")) { - var s = Reflect.field(style, Reflect.field(template, "style")); + var s = Reflect.field(data.style, Reflect.field(template, "style")); for (key in Reflect.fields(s)) if (!Reflect.hasField(template, key)) { Reflect.setField(template, key, Reflect.field(s, key)); } } + + if (Reflect.hasField(template, "id")) { + var id = Reflect.field(template, "id"); + var type = Reflect.field(template, "type"); + var expr = Context.parse("var a:" + type, Context.currentPos()); + var type = switch (expr.expr) { + case EVars(vars): vars[0].type; + case _: null; + } + data.fields.push({ + name: id, + access: [APublic], + pos: Context.currentPos(), + kind: FProp("default", "null", type) + }); + data.exprs.push(Context.parse("this." + id + " = " + name, Context.currentPos())); + } + for (key in Reflect.fields(template)) { if (["type", "style"].indexOf(key) > -1) continue; - var value = getValue(data, name, style, key, Reflect.field(template, key)); + var value = getValue(data, name, key, Reflect.field(template, key)); if (value != null) { - data.push(Context.parse(name + "." + key + " = cast " + value, Context.currentPos())); + data.exprs.push(Context.parse(name + "." + key + " = cast " + value, Context.currentPos())); } } return name; } public static function build() { - var template = getTemplate(); + //trace("Build: " + Context.getLocalClass().get().module); + var templateMeta = getMeta(":template"); + var templatePath = templateMeta[0].split("@"); + var template = loadFile(templatePath[0]); + if (templatePath[1] != null) template = Reflect.field(template, templatePath[1]); + var style = templateMeta[1] == null ? {} : loadFile(templateMeta[1]); + var fields = Context.getBuildFields(); - var data = []; + var data = { + fields: fields, + exprs: [], + style: style + }; i = 0; - createElement(data, template[0], "this", template[1]); + createElement(data, template, "this"); var init = false; for (f in fields) if (f.name == "init") { @@ -147,16 +159,16 @@ class ViewBuilderImpl { pos: Context.currentPos(), kind: FFun({ args: [{name:"links", type:TPath({name:"Dynamic", pack:[], params:[]}), opt:true, value:null}], - expr: macro $b{data}, + expr: macro $b{data.exprs}, params: [], ret: null }) }); - data = []; - data.push(macro super()); - data.push(macro build(links)); - if (init) data.push(macro init()); + var contrExprs = []; + contrExprs.push(macro super()); + contrExprs.push(macro build(links)); + if (init) contrExprs.push(macro init()); fields.push({ name: "new", @@ -164,7 +176,7 @@ class ViewBuilderImpl { pos: Context.currentPos(), kind: FFun({ args: [{name:"links", type:TPath({name:"Dynamic", pack:[], params:[]}), opt:true, value:null}], - expr: macro $b{data}, + expr: macro $b{contrExprs}, params: [], ret: null }) diff --git a/haxework/gui/popup/PopupManager.hx b/haxework/gui/popup/PopupManager.hx index ac34f6a..2a4e795 100644 --- a/haxework/gui/popup/PopupManager.hx +++ b/haxework/gui/popup/PopupManager.hx @@ -9,13 +9,13 @@ class PopupManager { 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(); + popups = new Array>(); } - public function show(popup:PopupView):Void { + public function show(popup:PopupView):Void { cast(Root.instance.view, IGroupView).addView(popup); if (showAnimateFactory != null) { Type.createInstance(showAnimateFactory, [popup]).start(null); @@ -24,7 +24,7 @@ class PopupManager { popup.onShow(); } - public function close(popup:PopupView):Void { + public function close(popup:PopupView):Void { popups.remove(popup); if (closeAnimateFactory != null) { Type.createInstance(closeAnimateFactory, [popup]).start(function(_) { diff --git a/haxework/gui/popup/PopupView.hx b/haxework/gui/popup/PopupView.hx index 690a6d2..6ed42bd 100755 --- a/haxework/gui/popup/PopupView.hx +++ b/haxework/gui/popup/PopupView.hx @@ -11,13 +11,13 @@ import haxework.gui.ButtonView; import haxework.gui.skin.ColorSkin; import haxework.gui.GroupView; -class PopupView extends GroupView { +class PopupView> extends GroupView { private var buttonId:String; - private var contentView:IGroupView; + private var contentView:V; private var callback:ICallback; - public function new(contentViewFactory:Class>) { + public function new(contentViewFactory:Class) { super(); pWidth = 100;