├── README.md └── io ├── CsvImporter.hx ├── MyConstants.hx └── VariablesRetriever.hx /README.md: -------------------------------------------------------------------------------- 1 | # spreadSheet-haxe 2 | OpenFl 3 | 4 | import variables from spreadSheet, dynamically update with the option to inline them at relase. 5 | 6 | ##How to use 7 | create a cvs file in your machine, you can a text editor 8 | example: 9 | 10 | floorHeight,200 11 | 12 | heroSpeed,1000 13 | 14 | heroJumpSpeed,-200 15 | 16 | Change MyConstants.hx to use that file as input. 17 | 18 | Upload that file to a google spreadSheet, or crate a new one using the same values. 19 | 20 | Click share and make it so "Anyone with the link can view" 21 | 22 | 23 | copy your spreadSheet id 24 | https://docs.google.com/spreadsheets/d/"spreadSheetId"/edit?usp=sharing 25 | 26 | To update your values from the web call 27 | 28 | CsvImporter.load(spreadSheetID,0); 29 | 30 | 31 | to get the values just type MyConstants.heroSpeed, the auto-complete should work. Use them directly so when you download the data from the web the values are updated. The initial value is taken from your machine file. 32 | 33 | To inline the values use = regex.split(loader.data); 38 | trace("enter"); 39 | trace(loader.data); 40 | for (couple in table) 41 | { 42 | var parts:Array = couple.split(","); 43 | if (parts.length > 1) 44 | { 45 | MyConstants.setValue(parts[0], Std.parseFloat(parts[1])); 46 | } 47 | } 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /io/MyConstants.hx: -------------------------------------------------------------------------------- 1 | package io; 2 | 3 | 4 | @:build(io.VariablesRetriever.buildFileReferences("put your cvs file here"))//Change 5 | class MyConstants 6 | { 7 | 8 | } -------------------------------------------------------------------------------- /io/VariablesRetriever.hx: -------------------------------------------------------------------------------- 1 | package io; 2 | import haxe.macro.Context; 3 | import haxe.macro.Expr; 4 | import haxe.macro.Expr.Access; 5 | import haxe.macro.Expr.Field; 6 | import haxe.macro.Expr.FieldType; 7 | import openfl.Assets; 8 | import sys.FileSystem; 9 | import sys.io.File; 10 | 11 | /** 12 | * ... 13 | * @author Joaquin 14 | */ 15 | class VariablesRetriever 16 | { 17 | 18 | public function new() 19 | { 20 | 21 | } 22 | macro public static function buildFileReferences(file:String):Array 23 | { 24 | var code:String; 25 | var exp:Array = new Array(); 26 | 27 | var file=File.read(file).readAll(); 28 | var fileAsString:String = file.getString(0, file.length); 29 | var regex:EReg = new EReg("[ \t]*((\r\n)|\r|\n)[ \t]*", "g"); 30 | var table:Array = regex.split(fileAsString); 31 | var counter = table.length - 1; 32 | var fields:Array = Context.getBuildFields(); 33 | while (counter>=0) 34 | { 35 | var parts:Array = table[counter].split(","); 36 | if (parts.length > 1) 37 | { 38 | // create new field based on file references! 39 | fields.push({ 40 | name: parts[0], 41 | doc: parts[0], 42 | #if INLINE_VARIABLES 43 | access: [Access.APublic, Access.AStatic, Access.AInline], 44 | #else 45 | access: [Access.APublic,Access.AStatic], 46 | #end 47 | kind: FieldType.FVar(macro:Float, macro $v{ Std.parseFloat(parts[1])}), 48 | pos: Context.currentPos() 49 | }); 50 | #if !INLINE_VARIABLES 51 | exp.push(Context.parseInlineString(createIf(parts[0]), Context.currentPos())); 52 | #end 53 | } 54 | --counter; 55 | } 56 | 57 | 58 | //setValue function 59 | var c = macro : { 60 | public static function setValue(aName:String,aValue:Dynamic):Void{ 61 | $b { exp } 62 | } 63 | } 64 | 65 | switch (c) { 66 | case TAnonymous(setFunction): 67 | return fields.concat(setFunction); 68 | default: 69 | throw 'unreachable'; 70 | } 71 | 72 | return fields; 73 | } 74 | private static function createIf(aVariableName:String):String 75 | { 76 | 77 | return "if(aName==\"" + aVariableName+"\") " + aVariableName+"= cast aValue"; 78 | } 79 | } --------------------------------------------------------------------------------