[macro] update template parser
This commit is contained in:
@@ -21,6 +21,14 @@ class FormView extends VGroupView {
|
|||||||
trace("init");
|
trace("init");
|
||||||
list.data = [for (i in 0...100) StringTools.hex(Math.floor(Math.random() * 1e9))];
|
list.data = [for (i in 0...100) StringTools.hex(Math.floor(Math.random() * 1e9))];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function onPress(view:ButtonView):Void {
|
||||||
|
trace('onPress: ${view.id}');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function onItemSelect(item:IListItemView<String>):Void {
|
||||||
|
trace('onItemSelect: ${item.data}');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ViewExample {
|
class ViewExample {
|
||||||
@@ -36,17 +44,5 @@ class ViewExample {
|
|||||||
trace(form.button1);
|
trace(form.button1);
|
||||||
trace(form.button2);
|
trace(form.button2);
|
||||||
trace(form.button3);
|
trace(form.button3);
|
||||||
form.button1.onPress.connect(onPress);
|
|
||||||
form.button2.onPress.connect(onPress);
|
|
||||||
form.button3.onPress.connect(onPress);
|
|
||||||
form.list.onItemSelect.connect(onItemSelect);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function onPress(view:ButtonView):Void {
|
|
||||||
trace('onPress: ${view.id}');
|
|
||||||
}
|
|
||||||
|
|
||||||
private function onItemSelect(item:IListItemView<String>):Void {
|
|
||||||
trace('onItemSelect: ${item.data}');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,30 +2,27 @@
|
|||||||
$type: haxework.gui.VGroupView
|
$type: haxework.gui.VGroupView
|
||||||
paddings: 20
|
paddings: 20
|
||||||
layoutMargin: 10
|
layoutMargin: 10
|
||||||
# skin:
|
skin: [ $type: [haxework.gui.skin.Skin.color, 0xff0000] ]
|
||||||
# $type: haxework.gui.skin.ColorSkin
|
|
||||||
# color: 0xff0000
|
|
||||||
views:
|
views:
|
||||||
- id: list
|
- id: list
|
||||||
$type: haxework.gui.list.VListView<String>
|
$type: haxework.gui.list.VListView<String>
|
||||||
factory: $class:haxework.gui.list.LabelListItem
|
+onItemSelect: $this:onItemSelect
|
||||||
|
factory: { $class: haxework.gui.list.LabelListItem }
|
||||||
pWidth: 100
|
pWidth: 100
|
||||||
pHeight: 100
|
pHeight: 100
|
||||||
leftMargin: 5
|
|
||||||
rightMargin: 10
|
|
||||||
scroll:
|
scroll:
|
||||||
$type: haxework.gui.list.VScrollView
|
$type: haxework.gui.list.VScrollView
|
||||||
width: 10
|
width: 10
|
||||||
pHeight: 100
|
pHeight: 100
|
||||||
skin: [ "$class:haxework.gui.list.ScrollSkin.vertical(0xcccccc,0x555555)" ]
|
skin: [ $type: [haxework.gui.list.ScrollSkin.vertical, 0xcffcc, 0x55cc55] ]
|
||||||
skin: [$class:haxework.gui.skin.Skin.color(0xffffff)]
|
skin: [ $type: [haxework.gui.skin.Skin.color, 0xffffff] ]
|
||||||
- $type: haxework.gui.SpriteView
|
- $type: haxework.gui.SpriteView
|
||||||
vAlign: BOTTOM
|
vAlign: BOTTOM
|
||||||
width: 50
|
width: 50
|
||||||
height: 50
|
height: 50
|
||||||
leftMargin: 5
|
leftMargin: 5
|
||||||
rightMargin: 10
|
rightMargin: 10
|
||||||
skin: [$class:haxework.gui.skin.Skin.color(0x0000ff)]
|
skin: [ $type: [haxework.gui.skin.Skin.color, 0x0000ff] ]
|
||||||
- id: panel
|
- id: panel
|
||||||
$type: haxework.gui.HGroupView
|
$type: haxework.gui.HGroupView
|
||||||
layoutHAlign: RIGHT
|
layoutHAlign: RIGHT
|
||||||
@@ -33,32 +30,28 @@ views:
|
|||||||
height: 30
|
height: 30
|
||||||
paddings: 3
|
paddings: 3
|
||||||
layoutMargin: 3
|
layoutMargin: 3
|
||||||
skin: [$class:haxework.gui.skin.Skin.color(0xffff00)]
|
skin: [ $type: [haxework.gui.skin.Skin.color, 0xffff00] ]
|
||||||
views:
|
views:
|
||||||
- id: button1
|
- id: button1
|
||||||
$type: haxework.gui.ButtonView
|
$type: haxework.gui.ButtonView
|
||||||
|
+onPress: $this:onPress
|
||||||
width: 100
|
width: 100
|
||||||
pHeight: 100
|
pHeight: 100
|
||||||
skin: [$class:haxework.gui.skin.ButtonSkin.color(0xcc0000)]
|
skin: [ $type: [haxework.gui.skin.ButtonSkin.color, 0xcc0000] ]
|
||||||
# skin:
|
|
||||||
# $type: haxework.gui.skin.ButtonColorSkin
|
|
||||||
# color: 0xcc0000
|
|
||||||
text: Text1
|
text: Text1
|
||||||
- id: button2
|
- id: button2
|
||||||
$type: haxework.gui.ButtonView
|
$type: haxework.gui.ButtonView
|
||||||
|
+onPress: $this:onPress
|
||||||
contentSize: true
|
contentSize: true
|
||||||
# skin:
|
skin: [ $type: [haxework.gui.skin.ButtonSkin.color, 0x00cc00] ]
|
||||||
# $type: haxework.gui.skin.ButtonColorSkin
|
|
||||||
# color: 0x00cc00
|
|
||||||
text: Text2
|
text: Text2
|
||||||
fontFamily: Georgia
|
fontFamily: Georgia
|
||||||
fontColor: 0xffffff
|
fontColor: 0xffffff
|
||||||
- id: button3
|
- id: button3
|
||||||
$type: haxework.gui.ButtonView
|
$type: haxework.gui.ButtonView
|
||||||
|
+onPress: $this:onPress
|
||||||
contentSize: true
|
contentSize: true
|
||||||
# skin:
|
skin: [ $type: [haxework.gui.skin.ButtonSkin.color, 0x00cccc] ]
|
||||||
# $type: haxework.gui.skin.ButtonColorSkin
|
|
||||||
# color: 0x00cccc
|
|
||||||
text: Text 3333333333 ddd
|
text: Text 3333333333 ddd
|
||||||
fontFamily: Tahoma
|
fontFamily: Tahoma
|
||||||
fontColor: 0xff0000
|
fontColor: 0xff0000
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
echo "`pwd`/target" > ~/.macromedia/Flash_Player/#Security/FlashPlayerTrust/haxework_test.cfg
|
||||||
. /opt/sdk/haxe/3.4.7/activate
|
. /opt/sdk/haxe/3.4.7/activate
|
||||||
haxe build.hxml && flashplayerdebugger target/LoaderExample.swf
|
haxe build.hxml && flashplayerdebugger target/LoaderExample.swf
|
||||||
|
|||||||
@@ -7,10 +7,7 @@ import haxe.macro.Expr;
|
|||||||
class ProvideMacro {
|
class ProvideMacro {
|
||||||
|
|
||||||
public static function has(field:Field):Bool {
|
public static function has(field:Field):Bool {
|
||||||
for (md in field.meta) if (md.name == ":provide") {
|
return Util.hasFieldMeta(":provide", field);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,7 @@ class StyleMacro {
|
|||||||
private static inline var metaName:String = ':style';
|
private static inline var metaName:String = ':style';
|
||||||
|
|
||||||
public static function has(classType:ClassType):Bool {
|
public static function has(classType:ClassType):Bool {
|
||||||
for (md in classType.meta.get()) if (md.name == metaName) {
|
return Util.hasClassMeta(metaName, classType);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static var style:Dynamic;
|
public static var style:Dynamic;
|
||||||
|
|||||||
@@ -11,10 +11,7 @@ class TemplateMacro {
|
|||||||
private static inline var metaName:String = ':template';
|
private static inline var metaName:String = ':template';
|
||||||
|
|
||||||
public static function has(classType:ClassType):Bool {
|
public static function has(classType:ClassType):Bool {
|
||||||
for (md in classType.meta.get()) if (md.name == metaName) {
|
return Util.hasClassMeta(metaName, classType);
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -77,32 +74,30 @@ class TemplateMacro {
|
|||||||
case "asset":
|
case "asset":
|
||||||
switch (a[1]) {
|
switch (a[1]) {
|
||||||
case "image":
|
case "image":
|
||||||
"openfl.Assets.getBitmapData(\"" + a[2] + "\")";
|
'openfl.Assets.getBitmapData("${a[2]}")';
|
||||||
case _:
|
case _:
|
||||||
a[2];
|
a[2];
|
||||||
}
|
}
|
||||||
case "res":
|
case "res":
|
||||||
var res = "haxework.provider.Provider.get(haxework.resources.IResources)." + a[1];
|
var bindExpr = 'haxework.provider.Provider.get(haxework.resources.IResources).${a[1]}.bind("${[2]}", "${name}", "${key}")';
|
||||||
var bindExpr = res + ".bind(\"" + a[2] + "\", " + name + ", \"" + key + "\")";
|
|
||||||
exprs.push(Context.parse(bindExpr, getPosition(position)));
|
exprs.push(Context.parse(bindExpr, getPosition(position)));
|
||||||
//res + ".get(\"" + a[2] + "\")";
|
|
||||||
null;
|
null;
|
||||||
case "locale":
|
case "locale":
|
||||||
"new haxework.locale.LString(\"" + a[1] + "\")";
|
'new haxework.locale.LString("${[1]}")';
|
||||||
case "class":
|
case "raw":
|
||||||
a[1];
|
a[1];
|
||||||
|
case "this":
|
||||||
|
'this.${a[1]}';
|
||||||
case "layout":
|
case "layout":
|
||||||
var template = FileUtil.loadJsonFile(a[1]);
|
var template = FileUtil.loadJsonFile(a[1]);
|
||||||
return createValue(name, key, template, position, exprs);
|
return createValue(name, key, template, position, exprs);
|
||||||
case "link":
|
|
||||||
"(links == null) ? untyped this : Reflect.field(links, \"" + a[1] + "\")";
|
|
||||||
case _:
|
case _:
|
||||||
Context.error("Unsupported prefix \"" + a[0] + "\"", getPosition(position));
|
Context.error('Unsupported prefix "${a[0]}"', getPosition(position));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static function getType(value:Dynamic, position:Position):String {
|
private static function getType(value:Dynamic, position:Position):Dynamic {
|
||||||
var type:String = getSpecField(value, "type");
|
var type:Dynamic = getSpecField(value, "type");
|
||||||
if (type == null) {
|
if (type == null) {
|
||||||
Context.error("Need @type field", position);
|
Context.error("Need @type field", position);
|
||||||
}
|
}
|
||||||
@@ -125,19 +120,26 @@ class TemplateMacro {
|
|||||||
} else if (Std.is(value, Float) || (Std.is(value, Bool))) {
|
} else if (Std.is(value, Float) || (Std.is(value, Bool))) {
|
||||||
value;
|
value;
|
||||||
} else if (value != null) {
|
} else if (value != null) {
|
||||||
var type = getType(value, getPosition(position));
|
var className:String = getSpecField(value, "class");
|
||||||
if (type != null) {
|
if (className != null) {
|
||||||
var n = 'a${i++}';
|
className;
|
||||||
if (type == "Dynamic") {
|
|
||||||
//ToDo:
|
|
||||||
exprs.push(Context.parse("var " + n + " = cast {}", getPosition(position)));
|
|
||||||
} else {
|
|
||||||
exprs.push(Context.parse("var " + n + " = new " + type + "()", getPosition(position)));
|
|
||||||
}
|
|
||||||
createElement(n, value, exprs);
|
|
||||||
n;
|
|
||||||
} else {
|
} else {
|
||||||
null;
|
var type:Dynamic = getType(value, getPosition(position));
|
||||||
|
if (type != null) {
|
||||||
|
var varName = 'a${i++}';
|
||||||
|
if (Std.is(type, Array)) {
|
||||||
|
var args = cast(type,Array<Dynamic>).slice(1).join(",");
|
||||||
|
exprs.push(Context.parse('var ${varName} = ${type[0]}(${args})', getPosition(position)));
|
||||||
|
} else if (type == "Dynamic") {
|
||||||
|
exprs.push(Context.parse('var ${varName} = cast {}', getPosition(position)));
|
||||||
|
} else {
|
||||||
|
exprs.push(Context.parse('var ${varName} = new ${type}()', getPosition(position)));
|
||||||
|
}
|
||||||
|
createElement(varName, value, exprs);
|
||||||
|
varName;
|
||||||
|
} else {
|
||||||
|
null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
value;
|
value;
|
||||||
@@ -169,7 +171,11 @@ class TemplateMacro {
|
|||||||
var position = Reflect.field(data, "$" + key);
|
var position = Reflect.field(data, "$" + key);
|
||||||
var value = createValue(name, key, Reflect.field(data, key), position, exprs);
|
var value = createValue(name, key, Reflect.field(data, key), position, exprs);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
exprs.push(Context.parse(name + "." + key + " = " + value, getPosition(position)));
|
if (key.charAt(0) == "+") {
|
||||||
|
exprs.push(Context.parse('${name}.${key.substr(1)}.connect(${value})', getPosition(position)));
|
||||||
|
} else {
|
||||||
|
exprs.push(Context.parse('${name}.${key} = ${value}', getPosition(position)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package haxework.macro;
|
package haxework.macro;
|
||||||
|
|
||||||
|
import haxe.macro.Type.ClassType;
|
||||||
import haxe.macro.Expr;
|
import haxe.macro.Expr;
|
||||||
|
|
||||||
|
|
||||||
@@ -15,4 +16,18 @@ class Util {
|
|||||||
public inline static function DynamicType():ComplexType {
|
public inline static function DynamicType():ComplexType {
|
||||||
return ComplexType.TPath({name:'Dynamic', pack:[], params:[]});
|
return ComplexType.TPath({name:'Dynamic', pack:[], params:[]});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public static function hasClassMeta(metaName:String, classType:ClassType):Bool {
|
||||||
|
for (md in classType.meta.get()) if (md.name == metaName) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function hasFieldMeta(metaName:String, field:Field):Bool {
|
||||||
|
for (md in field.meta) if (md.name == metaName) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user