Skip to content

Commit 2e02b69

Browse files
committed
Fixed timer & various bugs. The ts part is more or less already a good dropin replacement.
1 parent 36d9349 commit 2e02b69

14 files changed

Lines changed: 380 additions & 112 deletions

File tree

src/scripting/build/Host.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,24 @@ var CCLScripting = function(workerUrl){
362362
this.DOM.innerHTML = "";
363363
this.DOM.appendChild(_("text",text));
364364
};
365-
365+
this.__defineSetter__("x", function(f){
366+
this.setX(f);
367+
});
368+
this.__defineSetter__("y", function(f){
369+
this.setY(f);
370+
});
371+
this.__defineGetter__("text", function(f){
372+
return this.DOM.textContent;
373+
});
374+
this.__defineSetter__("text", function(f){
375+
this.setText(f);
376+
});
377+
this.__defineGetter__("filters", function(f){
378+
return [];
379+
});
380+
this.__defineSetter__("filters", function(f){
381+
this.setFilters([f]);
382+
});
366383
this.setFilters = function(params){
367384
for(var i = 0; i < params[0].length; i++){
368385
var filter = params[0][i];
@@ -598,7 +615,6 @@ var CCLScripting = function(workerUrl){
598615
return [];
599616
});
600617
this.__defineSetter__("filters", function(f){
601-
console.log(f);
602618
this.setFilters([f]);
603619
});
604620
this.setFilters = function(params){
@@ -679,7 +695,7 @@ var CCLScripting = function(workerUrl){
679695
stage.appendChild(this.DOM);
680696
};
681697

682-
ScriptingContext.prototype.Unpack.Canvas = function(stage, data, ctx){
698+
ScriptingContext.prototype.Unpack.Sprite = function(stage, data, ctx){
683699
this.DOM = _("div",{"style":{"position":"absolute"}});
684700

685701
this.setX = function(x){

src/scripting/build/api/Display.js

Lines changed: 176 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ var Display;
379379
DisplayObject.prototype.methodCall = function (methodName, params) {
380380
__pchannel("Runtime:CallMethod", {
381381
"id": this._id,
382-
"name": methodName,
383-
"value": params
382+
"method": methodName,
383+
"params": params
384384
});
385385
};
386386

@@ -696,6 +696,11 @@ var Display;
696696
function Sprite(id) {
697697
_super.call(this, id);
698698
}
699+
Sprite.prototype.serialize = function () {
700+
var serialized = _super.prototype.serialize.call(this);
701+
serialized["class"] = "Sprite";
702+
return serialized;
703+
};
699704
return Sprite;
700705
})(Display.DisplayObject);
701706
Display.Sprite = Sprite;
@@ -705,6 +710,7 @@ var Display;
705710
* Author: Jim Chen
706711
* Part of the CCLScripter
707712
*/
713+
/// <reference path="../Runtime.d.ts" />
708714
/// <reference path="DisplayObject.ts" />
709715
var Display;
710716
(function (Display) {
@@ -716,6 +722,7 @@ var Display;
716722
this._ttl = dur;
717723
this._dur = dur;
718724
this._parent = o;
725+
this._timer = new Runtime.Timer(41, 0);
719726
}
720727

721728
Object.defineProperty(MotionManager.prototype, "dur", {
@@ -725,6 +732,8 @@ var Display;
725732
set: function (dur) {
726733
this._dur = dur;
727734
this._ttl = dur;
735+
this._timer.stop();
736+
this._timer = new Runtime.Timer(41, 0);
728737
},
729738
enumerable: true,
730739
configurable: true
@@ -746,31 +755,28 @@ var Display;
746755
if (this._isRunning)
747756
return;
748757
this._isRunning = true;
749-
this._lastTick = Date.now();
750758
var self = this;
751-
752-
this._timer = setInterval(function () {
753-
var dur = Date.now() - self._lastTick;
754-
this._ttl -= dur;
755-
if (this._ttl <= 0) {
756-
this._ttl = 0;
757-
this.stop();
758-
if (this.oncomplete) {
759-
this.oncomplete();
759+
var _lastTime = Date.now();
760+
this._timer.addEventListener("timer", function () {
761+
var dur = Date.now() - _lastTime;
762+
self._dur -= dur;
763+
if (self._dur <= 0) {
764+
self.stop();
765+
if (self.oncomplete) {
766+
self.oncomplete();
760767
}
761-
762-
/* TODO: Update this to use remove() instead*/
763-
this._parent.unload();
768+
self._parent.unload();
764769
}
765-
self._lastTick = Date.now();
766-
}, 1000 / Display.frameRate);
770+
_lastTime = Date.now();
771+
});
772+
this._timer.start();
767773
};
768774

769775
MotionManager.prototype.stop = function () {
770776
if (!this._isRunning)
771777
return;
772778
this._isRunning = false;
773-
clearInterval(this._timer);
779+
this._timer.stop();
774780
};
775781

776782
MotionManager.prototype.forecasting = function (time) {
@@ -817,6 +823,15 @@ var Display;
817823
this.initStyle(params);
818824
Runtime.registerObject(this);
819825
}
826+
/**
827+
* Set the style for the UIComponent which this is
828+
* @param styleProp - style to set
829+
* @param value - value to set the style to
830+
*/
831+
CommentButton.prototype.setStyle = function (styleProp, value) {
832+
__trace("UIComponent.setStyle not implemented", "warn");
833+
};
834+
820835
Object.defineProperty(CommentButton.prototype, "motionManager", {
821836
get: function () {
822837
return this._mM;
@@ -829,11 +844,13 @@ var Display;
829844
});
830845

831846

832-
CommentButton.prototype.remove = function () {
833-
this.unload();
847+
CommentButton.prototype.initStyle = function (style) {
834848
};
835849

836-
CommentButton.prototype.initStyle = function (style) {
850+
CommentButton.prototype.serialize = function () {
851+
var serialized = _super.prototype.serialize.call(this);
852+
serialized["class"] = "Button";
853+
return serialized;
837854
};
838855
return CommentButton;
839856
})(Display.Sprite);
@@ -871,10 +888,6 @@ var Display;
871888
});
872889

873890

874-
CommentCanvas.prototype.remove = function () {
875-
this.unload();
876-
};
877-
878891
CommentCanvas.prototype.initStyle = function (style) {
879892
};
880893
return CommentCanvas;
@@ -1117,12 +1130,8 @@ var Display;
11171130
});
11181131

11191132

1120-
CommentShape.prototype.remove = function () {
1121-
this.unload();
1122-
};
1123-
11241133
CommentShape.prototype.initStyle = function (style) {
1125-
if (style.hasOwnProperty("lifeTime")) {
1134+
if (style["lifeTime"]) {
11261135
this._mM.dur = style["lifeTime"] * 1000;
11271136
}
11281137
this._mM.play();
@@ -1144,8 +1153,38 @@ var Display;
11441153
var Display;
11451154
(function (Display) {
11461155
var TextFormat = (function () {
1147-
function TextFormat() {
1156+
function TextFormat(font, size, color, bold, italic, underline, url, target, align, leftMargin, rightMargin, indent, leading) {
1157+
if (typeof font === "undefined") { font = "SimHei"; }
1158+
if (typeof size === "undefined") { size = 25; }
1159+
if (typeof color === "undefined") { color = 0xFFFFFF; }
1160+
if (typeof bold === "undefined") { bold = false; }
1161+
if (typeof italic === "undefined") { italic = false; }
1162+
if (typeof underline === "undefined") { underline = false; }
1163+
if (typeof url === "undefined") { url = ""; }
1164+
if (typeof target === "undefined") { target = ""; }
1165+
if (typeof align === "undefined") { align = "left"; }
1166+
if (typeof leftMargin === "undefined") { leftMargin = 0; }
1167+
if (typeof rightMargin === "undefined") { rightMargin = 0; }
1168+
if (typeof indent === "undefined") { indent = 0; }
1169+
if (typeof leading === "undefined") { leading = 0; }
1170+
this.font = font;
1171+
this.size = size;
1172+
this.color = color;
1173+
this.bold = bold;
1174+
this.italic = italic;
1175+
this.underline = underline;
11481176
}
1177+
TextFormat.prototype.serialize = function () {
1178+
return {
1179+
"class": "TextFormat",
1180+
"font": this.font,
1181+
"size": this.size,
1182+
"color": this.color,
1183+
"bold": this.bold,
1184+
"underline": this.underline,
1185+
"italic": this.italic
1186+
};
1187+
};
11491188
return TextFormat;
11501189
})();
11511190

@@ -1156,8 +1195,8 @@ var Display;
11561195
if (typeof color === "undefined") { color = 0; }
11571196
_super.call(this);
11581197
this._text = text;
1159-
this._color = color;
11601198
this._textFormat = new TextFormat();
1199+
this._textFormat.color = color;
11611200
}
11621201
Object.defineProperty(TextField.prototype, "text", {
11631202
get: function () {
@@ -1172,13 +1211,26 @@ var Display;
11721211
});
11731212

11741213

1214+
Object.defineProperty(TextField.prototype, "htmlText", {
1215+
get: function () {
1216+
return this.text;
1217+
},
1218+
set: function (text) {
1219+
__trace("TextField.htmlText is restricted due to security policy.", "warn");
1220+
this.text = text.replace(/<\/?[^>]+(>|$)/g, "");
1221+
},
1222+
enumerable: true,
1223+
configurable: true
1224+
});
1225+
1226+
11751227
Object.defineProperty(TextField.prototype, "color", {
11761228
get: function () {
1177-
return this._color;
1229+
return this._textFormat.color;
11781230
},
11791231
set: function (c) {
1180-
this._color = c;
1181-
this.propertyUpdate("color", this._color);
1232+
this._textFormat.color = c;
1233+
this.setTextFormat(this._textFormat);
11821234
},
11831235
enumerable: true,
11841236
configurable: true
@@ -1189,14 +1241,30 @@ var Display;
11891241
return this._textFormat;
11901242
};
11911243

1244+
TextField.prototype.setTextFormat = function (tf) {
1245+
this._textFormat = tf;
1246+
this.methodCall("setTextFormat", tf);
1247+
};
1248+
1249+
TextField.prototype.appendText = function (t) {
1250+
this.text = this.text + t;
1251+
};
1252+
11921253
TextField.prototype.serialize = function () {
11931254
var serialized = _super.prototype.serialize.call(this);
11941255
serialized["class"] = "TextField";
1256+
serialized["text"] = this._text;
1257+
serialized["textFormat"] = this._textFormat.serialize();
11951258
return serialized;
11961259
};
11971260
return TextField;
11981261
})(Display.DisplayObject);
11991262
Display.TextField = TextField;
1263+
1264+
function createTextFormat() {
1265+
return new TextFormat();
1266+
}
1267+
Display.createTextFormat = createTextFormat;
12001268
})(Display || (Display = {}));
12011269
/**
12021270
* Compliant CommentField Polyfill For BiliScriptEngine
@@ -1211,9 +1279,66 @@ var Display;
12111279
function CommentField(text, params) {
12121280
_super.call(this, text, 0xffffff);
12131281
this._mM = new Display.MotionManager(this);
1282+
this.setDefaults(params);
12141283
this.initStyle(params);
12151284
Runtime.registerObject(this);
12161285
}
1286+
1287+
Object.defineProperty(CommentField.prototype, "fontsize", {
1288+
get: function () {
1289+
return this.getTextFormat().fontsize;
1290+
},
1291+
set: function (size) {
1292+
var tf = this.getTextFormat();
1293+
tf.size = size;
1294+
this.setTextFormat(tf);
1295+
},
1296+
enumerable: true,
1297+
configurable: true
1298+
});
1299+
1300+
1301+
Object.defineProperty(CommentField.prototype, "font", {
1302+
get: function () {
1303+
return this.getTextFormat().font;
1304+
},
1305+
set: function (fontname) {
1306+
var tf = this.getTextFormat();
1307+
tf.font = fontname;
1308+
this.setTextFormat(tf);
1309+
},
1310+
enumerable: true,
1311+
configurable: true
1312+
});
1313+
1314+
1315+
Object.defineProperty(CommentField.prototype, "align", {
1316+
get: function () {
1317+
return this.getTextFormat().align;
1318+
},
1319+
set: function (a) {
1320+
var tf = this.getTextFormat();
1321+
tf.align = a;
1322+
this.setTextFormat(tf);
1323+
},
1324+
enumerable: true,
1325+
configurable: true
1326+
});
1327+
1328+
1329+
Object.defineProperty(CommentField.prototype, "bold", {
1330+
get: function () {
1331+
return this.getTextFormat().bold;
1332+
},
1333+
set: function (b) {
1334+
var tf = this.getTextFormat();
1335+
tf.bold = b;
1336+
this.setTextFormat(tf);
1337+
},
1338+
enumerable: true,
1339+
configurable: true
1340+
});
1341+
12171342
Object.defineProperty(CommentField.prototype, "motionManager", {
12181343
get: function () {
12191344
return this._mM;
@@ -1226,11 +1351,23 @@ var Display;
12261351
});
12271352

12281353

1229-
CommentField.prototype.remove = function () {
1230-
this.unload();
1231-
};
1232-
12331354
CommentField.prototype.initStyle = function (style) {
1355+
if (style["lifeTime"]) {
1356+
this._mM.dur = style["lifeTime"] * 1000;
1357+
}
1358+
if (style["fontsize"]) {
1359+
this.getTextFormat().size = style["fontsize"];
1360+
}
1361+
if (style["font"]) {
1362+
this.getTextFormat().font = style["font"];
1363+
}
1364+
if (style["color"]) {
1365+
this.getTextFormat().color = style["color"];
1366+
}
1367+
if (style["bold"]) {
1368+
this.getTextFormat().bold = style["bold"];
1369+
}
1370+
this._mM.play();
12341371
};
12351372
return CommentField;
12361373
})(Display.TextField);

0 commit comments

Comments
 (0)