-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
most of base code for implementing a standard dictionary
- Loading branch information
Showing
12 changed files
with
497 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
|
||
|
||
all: coreLib symbolLib editorLib testsLib | ||
all: coreLib | ||
|
||
#symbolLib editorLib testsLib | ||
|
||
|
||
coreLib: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
/// <reference path="src/Core.ts" /> | ||
/// <reference path="src/Fragment.ts" /> | ||
/// <reference path="src/Paper.ts" /> | ||
/// <reference path="src/Value.ts" /> | ||
/// <reference path="src/StandardLibrary.ts"/> | ||
/// <reference path="src/Dictionary.ts"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
|
||
module Sanara.Core { | ||
export type SanaraContext = CanvasRenderingContext2D; | ||
export class Exception { | ||
static FEW_CHILDREN = new Exception("Few children"); | ||
static FEW_PARAMETERS = new Exception("Few parameters"); | ||
|
||
constructor(public message:string) {}; | ||
toString() : string { | ||
return this.message; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/// <reference path="Fragment.ts" /> | ||
|
||
module Sanara.Core{ | ||
export type DictionaryEntry = Sanara.Core.FragmentClass// | Sanara.Core.Transformer; | ||
export class Dictionary { | ||
constructor(private entries : DictionaryEntry[]) {} | ||
hasEntry(name:string) : boolean { | ||
for(var i = 0 ; i < this.entries.length ; i++) { | ||
if(this.entries[i].doc.name === name){ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
getEntry(name:string) : Sanara.Core.DictionaryEntry { | ||
for(var i = 0 ; i < this.entries.length ; i++) { | ||
if(this.entries[i].doc.name === name){ | ||
return this.entries[i]; | ||
} | ||
} | ||
return null; | ||
} | ||
listNames() : string[] { | ||
var list = <string[]>[]; | ||
for(var i = 0 ; i < this.entries.length ; i++) { | ||
list.push(this.entries[i].doc.name) | ||
} | ||
return list; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,83 @@ | ||
/// <reference path="Paper.ts"/> | ||
/// <reference path="Core.ts"/> | ||
/// <reference path="Value.ts"/> | ||
|
||
|
||
module Sanara.Core { | ||
export interface Fragment { | ||
paint(paper: Paper) : void; | ||
paintMe(paper : Paper) : void; | ||
} | ||
export class BaseFragment implements Fragment { | ||
children : [Fragment]; | ||
constructor (children : [Fragment] = <[Fragment]>[]) { | ||
this.children = children; | ||
} | ||
paint(paper: Paper) { | ||
paper.context.save(); | ||
this.paintMe(paper); | ||
paper.context.restore(); | ||
} | ||
paintMe(paper : Paper) { | ||
this.children.forEach(function (v) { | ||
v.paint(paper); | ||
}) | ||
} | ||
} | ||
/** | ||
valueGetter because properties will change over time!. | ||
*/ | ||
export type Property = {name:string, getter:ValueGetter}; | ||
/** | ||
Type of Fragment classes. FragmentTemplate is a class that extends Fragment | ||
*/ | ||
export type FragmentClass = typeof Fragment; | ||
export interface FragmentClassDoc { | ||
name : string, | ||
discription : string, | ||
parameters? : {name:string, discription:string}[], | ||
properties? : {name:string, discription:string}[], | ||
children? : {name:string, discription:string, propertiesRequired?:string[]}[] | ||
} | ||
export class Fragment { | ||
static doc:FragmentClassDoc = { | ||
name : "fragment-base-class", | ||
discription : "SHOULD NOT BE INSTANTIATED: base class for all fragment classes" | ||
} | ||
|
||
private static ZERO_GETTER = ()=>Value.ZERO; | ||
|
||
private properties : Property[]; | ||
|
||
constructor (children:Fragment[], parameters:Value[], properties? : Property[]) { | ||
// children and parameters are ignored in baseclass but should be | ||
// implemented in classes that extends fragment class | ||
if(properties) { | ||
this.properties = properties; | ||
} else { | ||
this.properties = <Property[]>[]; | ||
} | ||
|
||
if(! this.hasProperty("width")) { | ||
this.properties.push({name:"width", getter:Fragment.ZERO_GETTER}); | ||
} | ||
if(! this.hasProperty("height")) { | ||
this.properties.push({name:"height", getter:Fragment.ZERO_GETTER}) | ||
} | ||
} | ||
|
||
paint(context : SanaraContext) { | ||
context.save(); | ||
this.paintImplementation(context); | ||
context.restore(); | ||
} | ||
paintImplementation(context : SanaraContext) {} | ||
|
||
hasProperty(name:string) : boolean{ | ||
for(var i = 0 ; i < this.properties.length ; i++) { | ||
if(this.properties[i].name === name) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
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(); | ||
} | ||
} | ||
return Value.ZERO; | ||
} | ||
|
||
width() : number { | ||
return this.getPropertyValue("width").toNumber(); | ||
} | ||
height() : number { | ||
return this.getPropertyValue("height").toNumber(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/// <reference path="Fragment.ts"/> | ||
/// <reference path="Dictionary.ts"/> | ||
|
||
|
||
module Sanara.Std { | ||
|
||
export class Bitree extends Sanara.Core.Fragment { | ||
static doc:Sanara.Core.FragmentClassDoc = { | ||
name : "bitree", | ||
discription : "binary tree", | ||
properties : [ | ||
{ | ||
name:"node-pivot", | ||
discription:"place to hold" | ||
} | ||
], | ||
children : [ | ||
{ | ||
name: "main-node", | ||
discription : "main node", | ||
}, { | ||
name: "lefty", | ||
discription : "left node", | ||
}, { | ||
name: "righty", | ||
discription : "right node", | ||
} | ||
] | ||
} | ||
|
||
private content : Sanara.Core.Fragment; | ||
private lefty : Sanara.Core.Fragment; | ||
private righty : Sanara.Core.Fragment; | ||
|
||
constructor(children : Sanara.Core.Fragment[]) { | ||
if(children.length < 3) { | ||
throw Sanara.Core.Exception.FEW_CHILDREN; | ||
} | ||
this.content = children[0]; | ||
this.lefty = children[1]; | ||
this.righty = children[2]; | ||
|
||
var properties = <Sanara.Core.Property[]>[]; | ||
|
||
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}); | ||
|
||
super(null,null,properties); | ||
} | ||
|
||
paintImplementation(context : Sanara.Core.SanaraContext) { | ||
context.save(); | ||
{ | ||
context.translate((this.width()-this.content.width())/2,0); | ||
this.content.paint(context); | ||
} | ||
context.restore(); | ||
|
||
context.translate(0,this.content.height()); | ||
this.lefty.paint(context); | ||
|
||
context.translate(this.lefty.width(),0); | ||
this.righty.paint(context); | ||
} | ||
} | ||
export class Circle extends Sanara.Core.Fragment { | ||
static doc:Sanara.Core.FragmentClassDoc = { | ||
name: "circle", | ||
discription: "circle of diameter 100" | ||
} | ||
|
||
private static CIRCLE : Circle = new Circle(); | ||
|
||
constructor() { | ||
if(typeof Circle.CIRCLE === "undefined") { | ||
Circle.CIRCLE = this; | ||
} else { | ||
return Circle.CIRCLE; | ||
} | ||
|
||
var diameterGetter =function(){return new Sanara.Core.Value(100)}; | ||
|
||
super(null,null,[ | ||
{name:"width",getter: diameterGetter}, | ||
{name:"height",getter: diameterGetter} | ||
]); | ||
} | ||
|
||
|
||
paintImplementation(context: Sanara.Core.SanaraContext) { | ||
context.beginPath(); | ||
context.arc(50, 50, 50, 0, 2 * Math.PI, false); | ||
context.fill(); | ||
} | ||
} | ||
export class FillColor extends Sanara.Core.Fragment { | ||
static doc:Sanara.Core.FragmentClassDoc = { | ||
name: "fill-color", | ||
discription: "Changes fill color for child environment", | ||
children : [ | ||
{ | ||
name : "colored", | ||
discription : "fragment that will recieve new fill color" | ||
} | ||
], | ||
parameters : [ | ||
{ | ||
name: "color", | ||
discription : "New color. e.g. \"#f3422e\"" | ||
} | ||
] | ||
} | ||
|
||
private color : Sanara.Core.Value; | ||
private child : Sanara.Core.Fragment; | ||
|
||
constructor(children:Sanara.Core.Fragment[], parameters: Sanara.Core.Value[]) { | ||
if(children.length < 1) { | ||
throw Sanara.Core.Exception.FEW_CHILDREN; | ||
} | ||
if(parameters.length < 1) { | ||
throw Sanara.Core.Exception.FEW_PARAMETERS; | ||
} | ||
this.color = parameters[0]; | ||
this.child = children[0]; | ||
|
||
super(null,null,[ | ||
{name:"width",getter: ()=>this.child.getPropertyValue("width")}, | ||
{name:"height",getter: ()=>this.child.getPropertyValue("height")}, | ||
]); | ||
} | ||
|
||
paintImplementation(context: Sanara.Core.SanaraContext) { | ||
context.fillStyle = this.color.toColor(); | ||
this.child.paint(context); | ||
} | ||
} | ||
|
||
export var dictionary : Sanara.Core.Dictionary = (function() { | ||
var entries = <Sanara.Core.DictionaryEntry[]>[]; | ||
// | ||
entries.push(Sanara.Std.Circle); | ||
entries.push(Sanara.Std.Bitree); | ||
entries.push(Sanara.Std.FillColor); | ||
// | ||
return new Sanara.Core.Dictionary(entries); | ||
})(); | ||
} |
Oops, something went wrong.