diff --git a/components/Core/src/Fragment.ts b/components/Core/src/Fragment.ts index b18875a..79d2910 100644 --- a/components/Core/src/Fragment.ts +++ b/components/Core/src/Fragment.ts @@ -4,10 +4,7 @@ module Sanara.Core { - /** - valueGetter because properties will change over time!. - */ - export type Property = {name:string, getter:ValueGetter}; + export type Property = {name:string, value:Value}; /** Type of Fragment classes. FragmentTemplate is a class that extends Fragment */ @@ -39,10 +36,10 @@ module Sanara.Core { } if(! this.hasProperty("width")) { - this.properties.push({name:"width", getter:Fragment.ZERO_GETTER}); + this.properties.push({name:"width",value: Value.ZERO}); } if(! this.hasProperty("height")) { - this.properties.push({name:"height", getter:Fragment.ZERO_GETTER}) + this.properties.push({name:"height", value: Value.ZERO}) } } @@ -53,31 +50,41 @@ module Sanara.Core { } paintImplementation(context : SanaraContext) {} - hasProperty(name:string) : boolean{ + private getPropertyByName(name:string) : Property { for(var i = 0 ; i < this.properties.length ; i++) { if(this.properties[i].name === name) { - return true; + return this.properties[i]; } } - return false; + return null; } - /** - fragment must have the property Or unexpected results - */ - getPropertyValue(name:string) : Value { - for(var i = 0 ; i < this.properties.length ; i++) { - if(this.properties[i].name === name) { - return this.properties[i].getter(); + hasProperty(name:string) : boolean { + return ( this.getPropertyByName(name) === null ? false : true); + } + getPropertyValue(name:string, onError?: (string?)=>Value) : Value { + var p = this.getPropertyByName(name); + if(p === null){ + if(onError) { + return onError(name); + } else { + return Value.ZERO; } } - return Value.ZERO; + return p.value; + } + setPropertyValue(name:string, value: Value) : void { + //does not add if not exist + var p = this.getPropertyByName(name); + if(p !== null) { + p.value = value; + } } width() : number { - return this.getPropertyValue("width").toNumber(); + return this.getPropertyValue("width", ()=>Value.ZERO).toNumber(); } height() : number { - return this.getPropertyValue("height").toNumber(); + return this.getPropertyValue("height", ()=>Value.ZERO).toNumber(); } } } diff --git a/components/Core/src/Value.ts b/components/Core/src/Value.ts index ebf140a..4c3f182 100644 --- a/components/Core/src/Value.ts +++ b/components/Core/src/Value.ts @@ -1,7 +1,6 @@ /** */ module Sanara.Core { - export type ValueGetter = () => Value; export type ValueInput = number|boolean|string; export class Value { //numbers diff --git a/components/Core/tsconfig.json b/components/Core/tsconfig.json index 5028853..3a28a3c 100644 --- a/components/Core/tsconfig.json +++ b/components/Core/tsconfig.json @@ -14,7 +14,6 @@ "./src/Dictionary.ts", "./src/Fragment.ts", "./src/Paper.ts", - "./src/StandardLibrary.ts", "./src/Value.ts" ] } diff --git a/components/Std/src/justAnExample.ts b/components/Std/src/justAnExample.ts index 8c178f2..138a096 100644 --- a/components/Std/src/justAnExample.ts +++ b/components/Std/src/justAnExample.ts @@ -40,24 +40,24 @@ module Sanara.Std { var properties = []; - var widthGetter = () => { - return new Sanara.Core.Value(Math.max(this.lefty.width()+this.righty.width(),this.content.width())) - } - properties.push({name:"width",getter:widthGetter}); - - var heightGetter = () => { - return new Sanara.Core.Value(this.content.height() + Math.max(this.lefty.height(), this.righty.height())); - } - properties.push({name:"height",getter:heightGetter}); - var nodePivotGetter = () => { - return new Sanara.Core.Value(this.lefty.width() + this.content.width()/2); - } - properties.push({name:"node-pivot",getter:nodePivotGetter}); + properties.push({name:"node-pivot",value:this.calcNodePivot()}); + properties.push({name:"width",value:this.calcWidth()}); + properties.push({name:"height",value:this.calcHeight()}); super(null,null,properties); } + private calcWidth() : Sanara.Core.Value { + return new Sanara.Core.Value(Math.max(this.lefty.width()+this.righty.width(),this.content.width())) + } + private calcHeight() : Sanara.Core.Value { + return new Sanara.Core.Value(this.content.height() + Math.max(this.lefty.height(), this.righty.height())); + } + private calcNodePivot() : Sanara.Core.Value { + return new Sanara.Core.Value(this.lefty.width() + this.content.width()/2); + } + paintImplementation(context : Sanara.Core.SanaraContext) { context.save(); { @@ -88,11 +88,11 @@ module Sanara.Std { return Circle.CIRCLE; } - var diameterGetter =function(){return new Sanara.Core.Value(100)}; + var diameter = new Sanara.Core.Value(100); super(null,null,[ - {name:"width",getter: diameterGetter}, - {name:"height",getter: diameterGetter} + {name:"width",value: diameter}, + {name:"height",value: diameter} ]); } @@ -135,11 +135,19 @@ module Sanara.Std { this.child = children[0]; super(null,null,[ - {name:"width",getter: ()=>this.child.getPropertyValue("width")}, - {name:"height",getter: ()=>this.child.getPropertyValue("height")}, + {name:"width",value:this.calcWidth()}, + {name:"height",value:this.calcHeight()} ]); } + private calcWidth() { + return this.child.getPropertyValue("width"); + } + + private calcHeight() { + return this.child.getPropertyValue("height"); + } + paintImplementation(context: Sanara.Core.SanaraContext) { context.fillStyle = this.color.toColor(); this.child.paint(context); diff --git a/components/declareFiles/coreLib.d.ts b/components/declareFiles/coreLib.d.ts index 68542cc..0b24ce8 100644 --- a/components/declareFiles/coreLib.d.ts +++ b/components/declareFiles/coreLib.d.ts @@ -28,7 +28,6 @@ declare module Sanara.Core { /** */ declare module Sanara.Core { - type ValueGetter = () => Value; type ValueInput = number | boolean | string; class Value { static ZERO: Value; @@ -46,12 +45,9 @@ declare module Sanara.Core { } } declare module Sanara.Core { - /** - valueGetter because properties will change over time!. - */ type Property = { name: string; - getter: ValueGetter; + value: Value; }; /** Type of Fragment classes. FragmentTemplate is a class that extends Fragment @@ -81,11 +77,10 @@ declare module Sanara.Core { constructor(children: Fragment[], parameters: Value[], properties?: Property[]); paint(context: SanaraContext): void; paintImplementation(context: SanaraContext): void; + private getPropertyByName(name); hasProperty(name: string): boolean; - /** - fragment must have the property Or unexpected results - */ - getPropertyValue(name: string): Value; + getPropertyValue(name: string, onError?: (string?) => Value): Value; + setPropertyValue(name: string, value: Value): void; width(): number; height(): number; } diff --git a/components/declareFiles/stdLib.d.ts b/components/declareFiles/stdLib.d.ts index 33228c0..396fd94 100644 --- a/components/declareFiles/stdLib.d.ts +++ b/components/declareFiles/stdLib.d.ts @@ -5,6 +5,9 @@ declare module Sanara.Std { private lefty; private righty; constructor(children: Sanara.Core.Fragment[]); + private calcWidth(); + private calcHeight(); + private calcNodePivot(); paintImplementation(context: Sanara.Core.SanaraContext): void; } class Circle extends Sanara.Core.Fragment { @@ -18,6 +21,8 @@ declare module Sanara.Std { private color; private child; constructor(children: Sanara.Core.Fragment[], parameters: Sanara.Core.Value[]); + private calcWidth(); + private calcHeight(); paintImplementation(context: Sanara.Core.SanaraContext): void; } } diff --git a/index.html b/index.html index e350f16..8bdc690 100644 --- a/index.html +++ b/index.html @@ -10,8 +10,9 @@ var dict = Sanara.Std.dictionary; console.log(dict.listNames()); // - var c = Sanara.Std.Circle(); - var btree = new Sanara.Std.Bitree([c,c,c]); + var c = new Sanara.Std.Circle(); + var redC = new Sanara.Std.FillColor([c],[new Sanara.Core.Value("#ff0000")]); + var btree = new Sanara.Std.Bitree([c,c,redC]); btree = new Sanara.Std.Bitree([c,btree,btree]); paper.root = btree; paper.repaint();