Skip to content

Commit 42ee86f

Browse files
committed
Deprecated old scripting engine and worked on new engine. Supports text glow.
1 parent e09078b commit 42ee86f

File tree

12 files changed

+340
-60
lines changed

12 files changed

+340
-60
lines changed

experimental/scripting/ccl.htm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
<meta http-equiv="X-UA-Compatible" value="IE=9">
66
<link rel="stylesheet" href="../../demo/default.css" />
77
<link rel="stylesheet" href="../../build/base.css" />
8+
<link rel="stylesheet" href="fontalias.css" />
89
<!-- Run 'make' to build the file -->
910
<script src="../../build/CommentCoreLibrary.js"></script>
1011

1112
<!-- A few helpers to do some decoding/fetching below-->
1213
<script src="../../demo/libxml.js"></script>
1314

1415
<!-- Scripting Extensions -->
15-
<script src="bscript.js"></script>
16+
<script src="../../src/scripting/Host.js"></script>
1617

1718
<title>Testrun Sandbox For CCL /w Scripting Enabled</title>
1819
</head>
@@ -62,8 +63,8 @@
6263
}
6364

6465
var cm = new CommentManager($('commentCanvas'));
65-
var bscripter = new CCLScripting.BridgedSandbox(cm, $("commentCanvas"));
66-
cm.scripting = bscripter;
66+
var bscripter = new CCLScripting("../../src/scripting/Worker.js");
67+
cm.scripting = bscripter.getSandbox($("commentCanvas"));
6768
cm.init();
6869

6970
var tmr=0;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/** Here because some UP-ers use chinese font names, and some of us are on Linux **/
2+
@font-face{
3+
font-family: "\9ED1\4F53";
4+
src:local('SimHei');
5+
}
6+
7+
@font-face{
8+
font-family: "\5B8B\4F53";
9+
src:local('SimSun');
10+
}
11+
12+
@font-face{
13+
font-family: "\5E7C\5706";
14+
src:local('YouYuan');
15+
}

experimental/scripting/index.htm

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
<style>
88
#codediv{width:48%; float:left;}
99
#playerdiv{width:48%; float:right;}
10-
#output{font-family:Consolas, 'Courier New', monospace; font-size:20px;padding:10px; background:#020;
11-
position:fixed; bottom:0; left:0; right:0; height:150px; overflow:auto; }
10+
#output{font-family:Consolas, 'Courier New', monospace; font-size:12px;padding:10px; background:#000;
11+
position:fixed; bottom:0; left:0; right:0; height:150px; overflow:auto; color:#ccc;border-top:1px dotted #fff;}
1212
#player{border:1px solid #f88; width:100%;background-color:#100;position:relative;}
1313
#code-input{width:100%;height:340px;display:block; border:1px solid #f88; padding:10px;background:#000;color:#f88;font-size:20px;}
1414
.s-button, .button{display:block; border:1px solid #f88; padding:10px; background:#000; color:#f88; float:left;-moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; -o-user-select: none; user-select: none; cursor:default;}
1515
.s-button:hover,.button:hover{background:#f88; color:#000;}
1616
.button{font-size:12px;z-index:99;}
17+
pre{margin:0;}
18+
pre.error{color:#f00;}
19+
pre.warning{color:#FFC500;}
1720
</style>
1821
<script type="text/javascript">var $ = function(e){return window.document.getElementById(e);}</script>
1922
<script src="../../src/scripting/Host.js" type="text/javascript"></script>
@@ -29,9 +32,20 @@ <h2 style="color:#fff">CCL Scripting Demo</h2>
2932
<div id="playerdiv">
3033
<div id="player" style="height:400px;clear:both;overflow:hidden;"></div>
3134
</div>
32-
<div id="output" style="color:#fff;">[Msg] 有关具体API信息请阅读 docs/scripting</div>
35+
<div id="output">[Msg] 有关具体API信息请阅读 docs/scripting</div>
3336
<script type="text/javascript">
3437
bscripter = new CCLScripting("../../src/scripting/Worker.js");
38+
bscripter.logger = new function(){
39+
this.log = function(t){
40+
$("output").innerHTML = "<pre>" + t.toString() + "</pre>" + $("output").innerHTML;
41+
};
42+
this.error = function(t){
43+
$("output").innerHTML = "<pre class='error'>" + t.toString() + "</pre>" + $("output").innerHTML;
44+
};
45+
this.warn = function(t){
46+
$("output").innerHTML = "<pre class='warning'>" + t.toString() + "</pre>" + $("output").innerHTML;
47+
};
48+
};
3549
sandbox = bscripter.getSandbox($("player"));
3650
$("evaluate").addEventListener("click", function(){
3751
try{

src/scripting/Host.js

Lines changed: 76 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ var CCLScripting = function(workerUrl){
232232
});
233233
this.addListener("Player::action", function(msg){
234234
try{
235+
if(self.getPlayer() == null){
236+
self.getLogger().warn("Player not initialized!");
237+
return;
238+
};
235239
switch(msg.action){
236240
default:return;
237241
case "play": self.getPlayer().play();break;
@@ -292,37 +296,69 @@ var CCLScripting = function(workerUrl){
292296
/** Define some unpackers **/
293297
var ScriptingContext = CCLScripting.prototype.ScriptingContext;
294298
ScriptingContext.prototype.Unpack.Comment = function(stage, data, ctx){
295-
this.DOM = _("div",{});
296-
/** Load the text format **/
299+
this.DOM = _("div",{
300+
"style":{
301+
"position":"absolute",
302+
},
303+
"className":"cmt"
304+
});
305+
/** Load the text **/
297306
this.DOM.appendChild(document.createTextNode(data.text));
298-
this.DOM.style.fontFamily = data.textFormat.font;
299-
this.DOM.style.fontSize = data.textFormat.size + "px";
300-
this.DOM.style.color = "#" + data.textFormat.color.toString(16);
301-
if(data.textFormat.bold)
302-
this.DOM.style.fontWeight = "bold";
303-
if(data.textFormat.underline)
304-
this.DOM.style.textDecoration = "underline";
305-
if(data.textFormat.italic)
306-
this.DOM.style.fontStyle = "italic";
307-
this.DOM.style.margin = data.textFormat.margin;
307+
var getColor = function(c){
308+
var color = c.toString(16);
309+
while(color.length < 6){
310+
color = "0" + color;
311+
}
312+
return "#" + color;
313+
};
314+
this.setTextFormat = function(textFormat){
315+
this.DOM.style.fontFamily = textFormat.font;
316+
this.DOM.style.fontSize = textFormat.size + "px";
317+
this.DOM.style.color = getColor(textFormat.color);
318+
if(textFormat.color <= 16){
319+
this.DOM.style.textShadow = "0 0 1px #fff";
320+
};
321+
if(textFormat.bold)
322+
this.DOM.style.fontWeight = "bold";
323+
if(textFormat.underline)
324+
this.DOM.style.textDecoration = "underline";
325+
if(textFormat.italic)
326+
this.DOM.style.fontStyle = "italic";
327+
this.DOM.style.margin = textFormat.margin;
328+
};
329+
/** Load the text format **/
330+
this.setTextFormat(data.textFormat);
331+
332+
this.setX = function(x){
333+
data.x = x;
334+
this.DOM.style.left = data.x + "px";
335+
};
336+
337+
this.setY = function(y){
338+
data.y = y;
339+
this.DOM.style.top = data.y + "px";
340+
};
341+
/** Load x,y **/
342+
this.setX(data.x);
343+
this.setY(data.y);
308344

309345
this.setFilters = function(params){
310346
for(var i = 0; i < params[0].length; i++){
311347
var filter = params[0][i];
312348
if(filter.type === "blur"){
313349
this.DOM.style.color = "transparent";
314-
this.DOM.style.textShadow = [-filter.params.blurX + "px",
315-
-filter.params.blurY + "px", Math.max(
350+
this.DOM.style.textShadow = [0,0, Math.max(
316351
filter.params.blurX, filter.params.blurY) +
317352
"px"].join(" ");
318353
}else if(filter.type === "glow"){
319-
this.DOM.style.textShadow = [-filter.params.blurX + "px",
320-
-filter.params.blurY + "px", Math.max(
354+
this.DOM.style.textShadow = [0,0, Math.max(
321355
filter.params.blurX, filter.params.blurY) +
322-
"px", "#" + filter.params.color.toString(16)].join(" ");
356+
"px", getColor(filter.params.color)].join(" ");
323357
}
324358
};
325359
};
360+
361+
/** Common **/
326362
this.unload = function(){
327363
try{
328364
stage.removeChild(this.DOM);
@@ -343,8 +379,8 @@ var CCLScripting = function(workerUrl){
343379
"width":"100%",
344380
"height":"100%"
345381
}});
346-
this.x = data.x;
347-
this.y = data.y;
382+
this.x = data.x ? data.x : 0;
383+
this.y = data.y ? data.y : 0;
348384
this.alpha = data.alpha ? data.alpha : 1;
349385
// Helpers
350386
var __ = function(e, attr){
@@ -362,12 +398,15 @@ var CCLScripting = function(workerUrl){
362398
return elem;
363399
};
364400
var defaultEffects = __("defs");
365-
var defaultGroup = __("g",{"x":this.x, "y":this.y, "opacity":this.alpha});
401+
var defaultGroup = __("g",{
402+
"transform":"translate(" + this.x + "," + this.y + ")",
403+
"opacity":this.alpha,
404+
});
366405
this.DOM.appendChild(defaultEffects);
367406
this.DOM.appendChild(defaultGroup);
368407

369408
this.line = {
370-
width:1,
409+
width:0,
371410
color:"#ffffff",
372411
alpha:1
373412
};
@@ -404,10 +443,20 @@ var CCLScripting = function(workerUrl){
404443

405444
/** Public methods **/
406445
this.setX = function(x){
407-
__(defaultGroup,{"x":x});
446+
if(!x)
447+
return;
448+
this.x = x;
449+
__(defaultGroup,{
450+
"transform":"translate(" + this.x + "," + this.y + ")"
451+
});
408452
};
409453
this.setY = function(y){
410-
__(defaultGroup,{"y":y});
454+
if(!y)
455+
return;
456+
this.y = y;
457+
__(defaultGroup,{
458+
"transform":"translate(" + this.x + "," + this.y + ")"
459+
});
411460
};
412461
this.moveTo = function(params){
413462
var p = __("path",{
@@ -528,10 +577,10 @@ var CCLScripting = function(workerUrl){
528577
var filter = filters[i];
529578
var dFilter = __("filter",{
530579
"id":"fe" + filter.type + i,
531-
"x":"-50%",
532-
"y":"-50%",
533-
"width":"200%",
534-
"height":"200%"
580+
"x":"-100%",
581+
"y":"-100%",
582+
"width":"400%",
583+
"height":"400%"
535584
});
536585
switch(filter.type){
537586
default:break;

0 commit comments

Comments
 (0)