update
This commit is contained in:
@@ -7,53 +7,37 @@ import haxe.macro.Context;
|
||||
@:remove @:autoBuild(haxework.gui.ViewBuilderImpl.build())
|
||||
extern interface ViewBuilder {}
|
||||
|
||||
typedef BuildData = {
|
||||
var fields:Array<Field>;
|
||||
var exprs:Array<Expr>;
|
||||
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<Dynamic> {
|
||||
var template = null;
|
||||
var style = null;
|
||||
private static function getMeta(key:String):Array<String> {
|
||||
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<Expr>, name:String, style:Dynamic, key:String, a:Array<String>):Dynamic {
|
||||
private static function specialValue(data:BuildData, name:String, key:String, a:Array<String>):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<Expr>, 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<Expr>, 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
|
||||
})
|
||||
|
||||
@@ -9,13 +9,13 @@ class PopupManager {
|
||||
public var showAnimateFactory(default, default):Class<IAnimate>;
|
||||
public var closeAnimateFactory(default, default):Class<IAnimate>;
|
||||
|
||||
private var popups:Array<PopupView>;
|
||||
private var popups:Array<PopupView<Dynamic>>;
|
||||
|
||||
public function new() {
|
||||
popups = new Array<PopupView>();
|
||||
popups = new Array<PopupView<Dynamic>>();
|
||||
}
|
||||
|
||||
public function show(popup:PopupView):Void {
|
||||
public function show(popup:PopupView<Dynamic>):Void {
|
||||
cast(Root.instance.view, IGroupView<Dynamic>).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<Dynamic>):Void {
|
||||
popups.remove(popup);
|
||||
if (closeAnimateFactory != null) {
|
||||
Type.createInstance(closeAnimateFactory, [popup]).start(function(_) {
|
||||
|
||||
@@ -11,13 +11,13 @@ import haxework.gui.ButtonView;
|
||||
import haxework.gui.skin.ColorSkin;
|
||||
import haxework.gui.GroupView;
|
||||
|
||||
class PopupView extends GroupView {
|
||||
class PopupView<V:IView<Dynamic>> extends GroupView {
|
||||
|
||||
private var buttonId:String;
|
||||
private var contentView:IGroupView<Dynamic>;
|
||||
private var contentView:V;
|
||||
private var callback:ICallback<String>;
|
||||
|
||||
public function new(contentViewFactory:Class<IGroupView<Dynamic>>) {
|
||||
public function new(contentViewFactory:Class<V>) {
|
||||
super();
|
||||
|
||||
pWidth = 100;
|
||||
|
||||
Reference in New Issue
Block a user