Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import haxe.macro.ComplexTypeTools;
- import haxe.macro.Context;
- import haxe.macro.TypeTools;
- import haxe.ui.macros.helpers.ClassBuilder;
- import haxe.macro.Expr;
- import haxe.ui.macros.helpers.FieldBuilder;
- class IonMacros {
- public static function buildElement():Array<Field> {
- var builder = new ClassBuilder(Context.getBuildFields(), Context.getLocalType(), Context.currentPos());
- if (builder.constructor == null) {
- builder.addFunction("new", macro {
- super();
- });
- }
- for (f in builder.getFieldsWithMeta("editable")) {
- var name = f.name;
- var type = getType(f);
- var componentType = inferComponentType(f);
- var values:Array<Dynamic> = extractEnumValues(f);
- builder.constructor.add(macro {
- editableProps.set($v{name}, {
- name: $v{name},
- type: $v{type},
- componentType: $v{componentType},
- values: $v{values}
- });
- });
- }
- return builder.fields;
- }
- private static function inferComponentType(f:FieldBuilder):String {
- var c = "haxe.ui.components.Box";
- var param = f.getMetaValueString("editable");
- if (param == null) { // no component specified lets work it out
- c = typeToComponent(f);
- } else {
- if (param.indexOf(".") == -1) {
- param = "haxe.ui.components." + param;
- }
- c = param;
- }
- return c;
- }
- private static function typeToComponent(f:FieldBuilder):String {
- var typeString = null;
- switch(f.field.kind) {
- case FVar(t, e):
- typeString = ComplexTypeTools.toString(t);
- case _:
- trace(f.field.kind);
- }
- var c = "haxe.ui.components.Box";
- switch (typeString) {
- case "Bool":
- c = "haxe.ui.components.CheckBox";
- case "Int" | "Float":
- c = "haxe.ui.components.NumberStepper";
- case "String":
- c = "haxe.ui.components.TextField";
- case _:
- var enumValues = extractEnumValues(f);
- if (enumValues != null && enumValues.length > 0) {
- c = "haxe.ui.components.DropDown";
- }
- }
- return c;
- }
- private static function getType(f:FieldBuilder):String {
- var r = "Dynamic";
- switch(f.field.kind) {
- case FVar(t, e):
- var typeString = ComplexTypeTools.toString(t);
- r = typeString;
- case _:
- trace(f.field.kind);
- }
- return r;
- }
- // all very ugly... pure haxe macros, no swish builder stuff :(
- private static function extractEnumValues(f:FieldBuilder):Array<Dynamic> {
- var r:Array<Dynamic> = null;
- switch(f.field.kind) {
- case FVar(t, e):
- var typeString = ComplexTypeTools.toString(t);
- var tt = Context.getType(typeString);
- switch (tt) {
- case TAbstract(_.get() => ab, _) if (ab.meta.has(":enum")):
- for (field in ab.impl.get().statics.get()) {
- if (field.meta.has(":enum") && field.meta.has(":impl")) {
- var fieldName = field.name;
- switch (field.expr().expr) {
- case TCast(t, e):
- switch(t.expr) {
- case TConst(TInt(n)):
- if (r == null) {
- r = [];
- }
- r.push({
- name: fieldName,
- value: n
- });
- case TConst(TFloat(n)):
- if (r == null) {
- r = [];
- }
- r.push({
- name: fieldName,
- value: n
- });
- case TConst(TString(n)):
- if (r == null) {
- r = [];
- }
- r.push({
- name: fieldName,
- value: n
- });
- case _:
- trace(t.expr);
- }
- case _:
- }
- }
- }
- case _:
- }
- case _:
- trace(f.field.kind);
- }
- return r;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement