Skip to content

Commit 73f163c

Browse files
committed
Modified Unpacker for porting to ts
1 parent 0de1e11 commit 73f163c

File tree

5 files changed

+203
-9
lines changed

5 files changed

+203
-9
lines changed

build/scripting/Host.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,8 @@ var CCLScripting = function(workerUrl){
489489
"top":"0px",
490490
"left":"0px",
491491
"width":(stage.offsetWidth * 2) + "px",
492-
"height":(stage.offsetWidth * 2) + "px"
492+
"height":(stage.offsetWidth * 2) + "px",
493+
"transform":"matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)"
493494
}
494495
});
495496
this._x = data.x ? data.x : 0;
Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,83 @@
1-
/**
2-
* Created by jim on 6/23/14.
3-
*/
1+
/// <reference path="DisplayObject.ts" />
2+
module Unpacker{
3+
export class DisplayObject{
4+
public DOM:HTMLDivElement;
5+
private _x:number;
6+
private _y:number;
7+
private _alpha:number;
8+
private _transform:Object = {
9+
"mode":"3d",
10+
"matrix":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]
11+
};
12+
13+
constructor(stage:any, data:Object, context:any){
14+
Unpacker.sensibleDefaults(data,{
15+
"x":0,
16+
"y":0,
17+
"alpha":1
18+
});
19+
this.DOM = <HTMLDivElement> Unpacker._("div",{
20+
"style": {
21+
"position":"absolute",
22+
"transform":"matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)",
23+
"transformOrigin":"0 0 0",
24+
"opacity": data["alpha"],
25+
"top": data["y"],
26+
"left": data["x"]
27+
}
28+
});
29+
this._x = data["x"];
30+
this._y = data["y"];
31+
this._alpha = data["alpha"];
32+
}
33+
34+
set x(x:number){
35+
this._x = x;
36+
this.DOM.style.left = x + "px";
37+
}
38+
39+
set y(y:number){
40+
this._y = y;
41+
this.DOM.style.top = y + "px";
42+
}
43+
44+
set alpha(alpha:number){
45+
this._alpha = alpha;
46+
this.DOM.style.opacity = alpha + "";
47+
}
48+
49+
set visible(v:boolean){
50+
this.DOM.style.visibility = v ? "visible" : "hidden";
51+
}
52+
53+
get x():number{
54+
return this._x;
55+
}
56+
57+
get y():number{
58+
return this._y;
59+
}
60+
61+
get alpha():number{
62+
return this._alpha;
63+
}
64+
65+
get visible():boolean{
66+
return this.DOM.style.visibility !== "hidden";
67+
}
68+
69+
/** Transform **/
70+
set transform(t:Object){
71+
this._transform = t;
72+
if(this._transform["mode"] === "2d"){
73+
74+
}else{
75+
76+
}
77+
}
78+
79+
get transform():Object{
80+
return this._transform;
81+
}
82+
}
83+
}
Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,45 @@
1-
/**
2-
* Created by jim on 6/23/14.
3-
*/
1+
/// <reference path="DisplayObject.ts" />
42
module Unpacker{
5-
export class TextField{
3+
export class TextField extends DisplayObject{
4+
constructor(stage:any, data:Object, context:any){
5+
super(stage, data, context);
6+
Unpacker.sensibleDefaults(data,{
7+
"text":"",
8+
"textFormat":{}
9+
});
10+
this.DOM.className = "cmt";
11+
this.setTextFormat(data["textFormat"]);
12+
this.DOM.appendChild(document.createTextNode(data["text"]));
13+
}
614

15+
public setTextFormat(tf:Object):void{
16+
Unpacker.sensibleDefaults(tf,{
17+
"font":"SimHei",
18+
"size":25,
19+
"color":0xffffff,
20+
"bold":false,
21+
"underline":false,
22+
"italic":false,
23+
"margin":0
24+
});
25+
this.DOM.style.fontFamily = tf["font"];
26+
this.DOM.style.fontSize = tf["size"];
27+
this.DOM.style.color = Unpacker.color(tf["color"]);
28+
if(tf["bold"])
29+
this.DOM.style.fontWeight = "bold";
30+
if(tf["italic"])
31+
this.DOM.style.fontStyle = "italic";
32+
if(tf["underline"])
33+
this.DOM.style.textDecoration = "underline";
34+
this.DOM.style.margin = tf["margin"] + "px";
35+
}
36+
37+
set text(t:string){
38+
this.DOM.textContent = t;
39+
}
40+
41+
get text():string{
42+
return this.DOM.textContent;
43+
}
744
}
845
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
module Unpacker{
2+
export function _(type:string, props:Object = {}, children:Array<HTMLElement> = [], callback:Function = null):Element{
3+
var elem:Element = null;
4+
if(type !== "svg"){
5+
elem = document.createElement(type);
6+
}else{
7+
elem = document.createElementNS("http://www.w3.org/2000/svg", "svg");
8+
}
9+
for(var key in props){
10+
if(props.hasOwnProperty(key)){
11+
if(key === "style"){
12+
props[key] = Unpacker.modernize(props[key]);
13+
for(var style in props[key]) {
14+
elem["style"][style] = props[key][style];
15+
}
16+
}else if(key === "className"){
17+
elem["className"] = props[key];
18+
}else {
19+
elem.setAttribute(key, props[key]);
20+
}
21+
}
22+
}
23+
for(var c = 0; c < children.length; c++){
24+
if(children[c] !== null)
25+
elem.appendChild(children[c]);
26+
}
27+
if(typeof callback === "function" && callback !== null){
28+
callback(elem);
29+
}
30+
return elem;
31+
}
32+
33+
export function modernize(styles:Object):Object{
34+
var modernizeLibrary:Object = {
35+
"transform":["webkitTransform"],
36+
"transformOrigin":["webkitTransformOrigin"],
37+
"transformStyle":["webkitTransformStyle"],
38+
"perspective":["webkitPerspective"],
39+
"perspectiveOrigin":["webkitPerspectiveOrigin"]
40+
};
41+
for(var key in modernizeLibrary){
42+
if(styles.hasOwnProperty(key)){
43+
for(var i = 0; i < modernizeLibrary[key].length; i++){
44+
styles[modernizeLibrary[key][i]] = styles[key];
45+
}
46+
}
47+
}
48+
return styles;
49+
}
50+
51+
export function sensibleDefaults(objectA:Object, defaults:Object):Object{
52+
for(var prop in defaults){
53+
if(!objectA.hasOwnProperty(prop)){
54+
objectA[prop] = defaults[prop]
55+
}
56+
}
57+
return objectA;
58+
}
59+
60+
export function color(color:number):string{
61+
if(typeof color === "string"){
62+
color = parseInt("" + color);
63+
if(color === NaN){
64+
color = 0;
65+
}
66+
}
67+
var code:string = color.toString(16);
68+
while(code.length < 6){
69+
code = "0" + code;
70+
}
71+
return "#" + code;
72+
}
73+
}
74+
75+
/// <reference path="TextField.ts" />

src/scripting/Unpacker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@
188188
"top":"0px",
189189
"left":"0px",
190190
"width":(stage.offsetWidth * 2) + "px",
191-
"height":(stage.offsetWidth * 2) + "px"
191+
"height":(stage.offsetWidth * 2) + "px",
192+
"transform":"matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)"
192193
}
193194
});
194195
this._x = data.x ? data.x : 0;

0 commit comments

Comments
 (0)