Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 5.62 KB | None | 0 0
  1. package;
  2.  
  3. import haxe.macro.ComplexTypeTools;
  4. import haxe.macro.Context;
  5. import haxe.macro.TypeTools;
  6. import haxe.ui.macros.helpers.ClassBuilder;
  7. import haxe.macro.Expr;
  8. import haxe.ui.macros.helpers.FieldBuilder;
  9.  
  10. class IonMacros {
  11.     public static function buildElement():Array<Field> {
  12.         var builder = new ClassBuilder(Context.getBuildFields(), Context.getLocalType(), Context.currentPos());
  13.        
  14.         if (builder.constructor == null) {
  15.             builder.addFunction("new", macro {
  16.                 super();
  17.             });
  18.         }
  19.  
  20.         for (f in builder.getFieldsWithMeta("editable")) {
  21.             var name = f.name;
  22.             var type = getType(f);
  23.             var componentType = inferComponentType(f);
  24.             var values:Array<Dynamic> = extractEnumValues(f);
  25.             builder.constructor.add(macro {
  26.                 editableProps.set($v{name}, {
  27.                     name: $v{name},
  28.                     type: $v{type},
  29.                     componentType: $v{componentType},
  30.                     values: $v{values}
  31.                 });
  32.             });
  33.         }
  34.        
  35.         return builder.fields;
  36.     }
  37.    
  38.     private static function inferComponentType(f:FieldBuilder):String {
  39.         var c = "haxe.ui.components.Box";
  40.        
  41.         var param = f.getMetaValueString("editable");
  42.         if (param == null) { // no component specified lets work it out
  43.             c = typeToComponent(f);
  44.         } else {
  45.             if (param.indexOf(".") == -1) {
  46.                 param = "haxe.ui.components." + param;
  47.             }
  48.             c = param;
  49.         }
  50.        
  51.         return c;
  52.     }
  53.    
  54.     private static function typeToComponent(f:FieldBuilder):String {
  55.         var typeString = null;
  56.         switch(f.field.kind) {
  57.             case FVar(t, e):
  58.                 typeString = ComplexTypeTools.toString(t);
  59.             case _:
  60.                 trace(f.field.kind);
  61.         }
  62.            
  63.         var c = "haxe.ui.components.Box";
  64.         switch (typeString) {
  65.             case "Bool":
  66.                 c = "haxe.ui.components.CheckBox";
  67.             case "Int" | "Float":
  68.                 c = "haxe.ui.components.NumberStepper";
  69.             case "String":
  70.                 c = "haxe.ui.components.TextField";
  71.             case _:
  72.                 var enumValues = extractEnumValues(f);
  73.                 if (enumValues != null && enumValues.length > 0) {
  74.                     c = "haxe.ui.components.DropDown";
  75.                 }
  76.         }
  77.         return c;
  78.     }
  79.    
  80.     private static function getType(f:FieldBuilder):String {
  81.         var r = "Dynamic";
  82.        
  83.         switch(f.field.kind) {
  84.             case FVar(t, e):
  85.                 var typeString = ComplexTypeTools.toString(t);
  86.                 r = typeString;
  87.             case _:
  88.                 trace(f.field.kind);
  89.         }
  90.        
  91.         return r;
  92.     }
  93.    
  94.     // all very ugly... pure haxe macros, no swish builder stuff :(
  95.     private static function extractEnumValues(f:FieldBuilder):Array<Dynamic> {
  96.         var r:Array<Dynamic> = null;
  97.  
  98.         switch(f.field.kind) {
  99.             case FVar(t, e):
  100.                 var typeString = ComplexTypeTools.toString(t);
  101.                 var tt = Context.getType(typeString);
  102.                 switch (tt) {
  103.                     case TAbstract(_.get() => ab, _) if (ab.meta.has(":enum")):
  104.                         for (field in ab.impl.get().statics.get()) {
  105.                             if (field.meta.has(":enum") && field.meta.has(":impl")) {
  106.                                 var fieldName = field.name;
  107.                                 switch (field.expr().expr) {
  108.                                     case TCast(t, e):
  109.                                         switch(t.expr) {
  110.                                             case TConst(TInt(n)):
  111.                                                 if (r == null) {
  112.                                                     r = [];
  113.                                                 }
  114.                                                 r.push({
  115.                                                     name: fieldName,
  116.                                                     value: n
  117.                                                 });
  118.                                             case TConst(TFloat(n)):
  119.                                                 if (r == null) {
  120.                                                     r = [];
  121.                                                 }
  122.                                                 r.push({
  123.                                                     name: fieldName,
  124.                                                     value: n
  125.                                                 });
  126.                                             case TConst(TString(n)):
  127.                                                 if (r == null) {
  128.                                                     r = [];
  129.                                                 }
  130.                                                 r.push({
  131.                                                     name: fieldName,
  132.                                                     value: n
  133.                                                 });
  134.                                             case _:
  135.                                                 trace(t.expr);
  136.                                         }
  137.                                     case _:    
  138.                                 }
  139.                             }
  140.                         }
  141.                     case _:    
  142.                 }
  143.             case _:
  144.                 trace(f.field.kind);
  145.         }
  146.        
  147.         return r;
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement