Skip to content

Commit

Permalink
Add more fragments in Std library
Browse files Browse the repository at this point in the history
  • Loading branch information
alshakh committed May 14, 2015
1 parent b3556cf commit 6777bbc
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 65 deletions.
4 changes: 2 additions & 2 deletions components/Std/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
OutputName=stdLib
SourceFiles=src/justAnExample.ts
SourceFiles=src/justAnExample.ts src/Text.ts src/colors.ts src/position.ts

BaseDir=../..
OutputDir=$(BaseDir)/js
Expand All @@ -8,7 +8,7 @@ Out=$(OutputDir)/$(OutputName).js
all: $(Out)

$(Out): lib.ts $(SourceFiles)
tsc --removeComments --declaration --sourceMap --out $@ lib.ts
tsc --removeComments --target es5 --declaration --sourceMap --out $@ lib.ts
sed -i "/\/\/\/\ *<reference/d" $(OutputDir)/$(OutputName).d.ts #reused as source for other components
cp -f $(OutputDir)/$(OutputName).d.ts ../declareFiles/

Expand Down
23 changes: 13 additions & 10 deletions components/Std/lib.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/// <reference path="./src/justAnExample"/>
/// <reference path="./declareFiles/coreLib.d.ts"/>

/// <reference path="./src/justAnExample"/>
/// <reference path="./src/Text.ts"/>
/// <reference path="./src/colors.ts"/>
/// <reference path="./src/position.ts"/>

module Sanara.Std {
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);
})();
export var dictionary : Core.Dictionary = new Sanara.Core.Dictionary([
Circle,
Bitree,
Text,
Background,
FillColor
]);
}
59 changes: 59 additions & 0 deletions components/Std/src/Text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/// <reference path="../declareFiles/coreLib.d.ts" />

module Sanara.Std {
export class Text extends Sanara.Core.Fragment {

private static TEST_CONTEXT = <Sanara.Core.SanaraContext>document.createElement("canvas").getContext("2d");

private font : string; // SIZEpx FONT_FAMITY_NAME eg. 18px serif

private myText;

constructor (children, parameters : Sanara.Core.Value[]) {
if(parameters.length < 1) {
throw Sanara.Core.Exception.FEW_PARAMETERS;
}
this.font = parameters[0].toString();

this.myText = "";

super(null,null,[
{name:"width", value:this.calcWidth()},
{name:"height", value:this.calcHeight()}
]);
}

get text() : string {
return this.myText;
}
set text(newText) {
this.myText = newText;
this.setPropertyValue("width",this.calcWidth());
this.setPropertyValue("height",this.calcHeight());
}

private calcHeight() {
if(this.text.length === 0) return Sanara.Core.Value.ZERO;

return new Sanara.Core.Value(parseInt(this.font));
}

private calcWidth() {
if(this.text.length === 0) return Sanara.Core.Value.ZERO;

var w = 0;
Text.TEST_CONTEXT.save(); {
Text.TEST_CONTEXT.font = this.font;
w = Text.TEST_CONTEXT.measureText(this.text).width;
}; Text.TEST_CONTEXT.restore();
return new Sanara.Core.Value(w);
}

paintImplementation(context : Sanara.Core.SanaraContext) {
context.font = this.font;
context.textAlign = 'left'
context.textBaseline = 'top'
context.fillText(this.text,0,0);
}
}
}
109 changes: 109 additions & 0 deletions components/Std/src/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/// <reference path="../declareFiles/coreLib.d.ts" />



module Sanara.Std {
export class Background extends Sanara.Core.Fragment {
static doc:Sanara.Core.FragmentClassDoc = {
name: "background",
discription: "Adds background to child",
children : [
{
name : "frontground",
discription : "fragment that will have background"
}
],
parameters : [
{
name: "color",
discription : "Background 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",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.save(); {
context.fillStyle = this.color.toColor();
context.fillRect(0,0,this.child.width(), this.child.height());
}; context.restore();
this.child.paint(context);
}
}
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",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);
}
}
}
50 changes: 0 additions & 50 deletions components/Std/src/justAnExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,54 +103,4 @@ module Sanara.Std {
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",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);
}
}
}
60 changes: 60 additions & 0 deletions components/Std/src/position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

/// <reference path="../declareFiles/coreLib.d.ts" />

module Sanara.Std {
export class Translate extends Sanara.Core.Fragment {
static doc:Sanara.Core.FragmentClassDoc = {
name : "translate",
discription : "Translate a fragment",
parameters : [
{
name: "dx",
discription : "translate x"
},
{
name: "dy",
discription : "translate y"
}
],
children : [
{
name: "translated",
discription : "fragment to be translated",
}
]
}

private child : Sanara.Core.Fragment;
private dx : Sanara.Core.Value;
private dy : Sanara.Core.Value;

constructor(children : Sanara.Core.Fragment[] , parameters : Sanara.Core.Value[]) {
if(children.length < 1) {
throw Sanara.Core.Exception.FEW_CHILDREN;
}
if(parameters.length < 2) {
throw Sanara.Core.Exception.FEW_PARAMETERS;
}
this.dx = parameters[0];
this.dy = parameters[1];
this.child = children[0];

super(null,null,[
{name:"width",value:this.calcWidth()},
{name:"height",value:this.calcHeight()}
]);
}

private calcWidth() : Sanara.Core.Value {
return new Sanara.Core.Value(this.dx.toNumber() + this.child.width());
}
private calcHeight() : Sanara.Core.Value {
return new Sanara.Core.Value(this.dy.toNumber() + this.child.height());
}

paintImplementation(context : Sanara.Core.SanaraContext) {
context.translate(this.dx.toNumber(), this.dy.toNumber());
this.child.paint(context);
}
}
}
5 changes: 4 additions & 1 deletion components/Std/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"files": [
"./declareFiles/coreLib.d.ts",
"./lib.ts",
"./src/justAnExample.ts"
"./src/Text.ts",
"./src/colors.ts",
"./src/justAnExample.ts",
"./src/position.ts"
]
}
1 change: 0 additions & 1 deletion components/declareFiles/coreLib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ declare module Sanara.Core {
interface Paper {
domElement: HTMLCanvasElement;
context: SanaraContext;
root: Fragment;
repaint(time: number): void;
}
class BasicPaper implements Paper {
Expand Down
37 changes: 36 additions & 1 deletion components/declareFiles/stdLib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ declare module Sanara.Std {
constructor();
paintImplementation(context: Sanara.Core.SanaraContext): void;
}
}
declare module Sanara.Std {
class Text extends Sanara.Core.Fragment {
private static TEST_CONTEXT;
private font;
private myText;
constructor(children: any, parameters: Sanara.Core.Value[]);
text: string;
private calcHeight();
private calcWidth();
paintImplementation(context: Sanara.Core.SanaraContext): void;
}
}
declare module Sanara.Std {
class Background extends Sanara.Core.Fragment {
static doc: Sanara.Core.FragmentClassDoc;
private color;
private child;
constructor(children: Sanara.Core.Fragment[], parameters: Sanara.Core.Value[]);
private calcWidth();
private calcHeight();
paintImplementation(context: Sanara.Core.SanaraContext): void;
}
class FillColor extends Sanara.Core.Fragment {
static doc: Sanara.Core.FragmentClassDoc;
private color;
Expand All @@ -27,5 +50,17 @@ declare module Sanara.Std {
}
}
declare module Sanara.Std {
var dictionary: Sanara.Core.Dictionary;
class Translate extends Sanara.Core.Fragment {
static doc: Sanara.Core.FragmentClassDoc;
private child;
private dx;
private dy;
constructor(children: Sanara.Core.Fragment[], parameters: Sanara.Core.Value[]);
private calcWidth();
private calcHeight();
paintImplementation(context: Sanara.Core.SanaraContext): void;
}
}
declare module Sanara.Std {
var dictionary: Core.Dictionary;
}

0 comments on commit 6777bbc

Please sign in to comment.