Skip to content

Commit

Permalink
Core implemented for simple drawing
Browse files Browse the repository at this point in the history
most of base code for implementing a standard dictionary
  • Loading branch information
alshakh committed May 12, 2015
1 parent 6e893df commit b054093
Show file tree
Hide file tree
Showing 12 changed files with 497 additions and 48 deletions.
4 changes: 3 additions & 1 deletion Makefile
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:
Expand Down
2 changes: 1 addition & 1 deletion components/Core/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
OutputName=coreLib
SourceFiles=src/Core.ts src/Fragment.ts src/Paper.ts
SourceFiles=src/Core.ts src/Fragment.ts src/Paper.ts src/Value.ts src/StandardLibrary.ts src/Dictionary.ts
BaseDir=../..
OutputDir=$(BaseDir)/js
Out=$(OutputDir)/$(OutputName).js
Expand Down
3 changes: 3 additions & 0 deletions components/Core/lib.ts
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"/>
9 changes: 9 additions & 0 deletions components/Core/src/Core.ts
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;
}
}
}
31 changes: 31 additions & 0 deletions components/Core/src/Dictionary.ts
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;
}
}
}
99 changes: 79 additions & 20 deletions components/Core/src/Fragment.ts
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();
}
}
}
15 changes: 8 additions & 7 deletions components/Core/src/Paper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ module Sanara.Core {
export interface Paper {
domElement : HTMLCanvasElement;
context : SanaraContext;
w : number;
h : number;
root : Fragment;
repaint(time:number) : void;
}
export class BasicPaper implements Paper {
domElement : HTMLCanvasElement;
context : SanaraContext;
w : number;
h : number;
private width : number;
private height : number;
root : Fragment;
constructor (w:number = 100, h:number = 100) {
this.w = w;
this.h = h;
this.width = w;
this.height = h;

this.domElement = <HTMLCanvasElement>document.createElement("canvas");
this.domElement.width = this.width;
this.domElement.height = this.height;

this.context = <SanaraContext>this.domElement.getContext("2d");
}
repaint(time:number = 0) : void {
this.root.paint(this);
this.root.paint(this.context);
}
}
}
160 changes: 160 additions & 0 deletions components/Core/src/StandardLibrary.ts
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);
})();
}
Loading

0 comments on commit b054093

Please sign in to comment.