Skip to content

Commit 0a47af3

Browse files
committed
Merge in dev branch
2 parents 4d21b28 + 01ac853 commit 0a47af3

24 files changed

Lines changed: 1272 additions & 250 deletions

demo/scripting/ccl.htm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ <h2>Tests</h2>
8686
bscripter.logger = new function(){
8787
this.log = function(t){
8888
var pre = document.createElement("pre");
89-
pre.textContent = t.toString();
89+
pre.textContent = "[" + (window.playhead / 1000) + "] " + t.toString();
9090
$("debug-console").appendChild(pre);
9191
while($("debug-console").children.length > 200){
9292
$("debug-console").removeChild($("debug-console").children[0]);
@@ -95,7 +95,7 @@ <h2>Tests</h2>
9595
};
9696
this.error = function(t){
9797
var pre = document.createElement("pre");
98-
pre.textContent = t.toString();
98+
pre.textContent = "[" + (window.playhead / 1000) + "] " + t.toString();
9999
pre.className = "error";
100100
$("debug-console").appendChild(pre);
101101
while($("debug-console").children.length > 200){
@@ -105,7 +105,7 @@ <h2>Tests</h2>
105105
};
106106
this.warn = function(t){
107107
var pre = document.createElement("pre");
108-
pre.textContent = t.toString();
108+
pre.textContent = "[" + (window.playhead / 1000) + "] " + t.toString();
109109
pre.className = "warning";
110110
$("debug-console").appendChild(pre);
111111
while($("debug-console").children.length > 200){

docs/scripting/Display/Root.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Root Element 根(舞台)对象
2+
==================================
3+
请尽可能避免使用舞台对象。
4+
5+
舞台对象(Root Object)是 BSE 开放的一套用于操作弹幕舞台的函数入口。它在
6+
[官方文档](http://docs.bilibili.cn/wiki/Display)__并没有__ 被给出,但是由于大量的高级
7+
弹幕使用了此接口,KagerouEngine也实现了部分支持。该支持可以通过 `$.root` 或者
8+
`Display.root` 访问。
9+
10+
请尽可能避免使用舞台对象。(因为很重要所以又说了一次)
11+
12+
事件监听的潜在问题(Listener Problems)
13+
----------------------------------
14+
Root元素由于和其他的元素在监听器构造上有截然不同的设计需求,因为弹幕舞台很可能会由多个播放器同时
15+
占用。在其余的API中,我们都可以认为 KagerouEngine 显示端可以独占舞台,但是在 root 元素上我们
16+
不能这样认为。这一限制条件,加之 [影子对象](../Instances.md) 的设计,由控制外播放元件产生的
17+
对象,我们没有沙箱内绑定。在触发诸如 `Event.ADDED` 事件时,我们就没有办法有效的体现这些对象的
18+
代码类。即使是有沙箱内绑定的,对于如 `Event.ADDED` 事件的触发也非常困难,因为这个Event需要
19+
寻找对应的 ID 并发起脚本端寻找实例的操作。
20+
21+
对于外部添加的非受控对象,KagerouEngine的Host端可能采取两种措施:
22+
23+
1. 对于不认识的可还原对象,如果自己报告了自己,试图绑定为一个临时 DisplayObject。
24+
注意:对这个临时DisplayObject的操作未必会真的有正确的效果,因为我们不知道原 DO 的类绑定,
25+
也无法擅自解除绑定。我们更不能确保初始化的这个临时 DO 的属性都正确,甚至连
26+
`x,y,width,height,boundingbox` 都无法完美的确保。任何依赖这个临时 DO 的操作都不被正式API
27+
支持。
28+
29+
2. 不理睬不被报告的对象。
30+
31+
### 报告对象
32+
对象如果希望触发 `Event.ADDED`__必须__ 在添加的同时触发 stage 的一个监听器。
33+
34+
```JavaScript
35+
// This is the [[HOST SIDE]].
36+
var stage = SomeHTMLDOMElement;
37+
stage.appendChild(myNewElement);
38+
stage.dispatchEvent(new CustomEvent("registerKagerou",{
39+
"details":{
40+
"classType":"DisplayObject",
41+
"id": "PleaseMakeYourOwnUNIQUEid",
42+
"serialized":{
43+
"methods":["myMethodName"],
44+
"properties":[{
45+
"name":"myPropertyName",
46+
"value":initialValue,
47+
}]
48+
},
49+
"dom": DOMObject
50+
},
51+
"bubbles": false,
52+
"cancelable": true
53+
}));
54+
```
55+
56+
在对象被删除的时候还需要回收!如下:
57+
58+
```JavaScript
59+
// This is the [[HOST SIDE]].
60+
var stage = SomeHTMLDOMElement;
61+
stage.removeChild(myNewElement);
62+
stage.dispatchEvent(new CustomEvent("deregisterKagerou",{
63+
"details":{
64+
"id": "PleaseMakeYourOwnUNIQUEid"
65+
},
66+
"bubbles": false,
67+
"cancelable": true
68+
}));
69+
```
70+
71+
注意两次的ID必须一致。至于具体怎么编这个ID留给各个程序,不过为了避免各个程序的命名空间冲突,建议
72+
采取如下格式(注意只是建议):
73+
74+
myApplicatioName:ThisObjectName
75+
76+
这样就不容易产生命名冲突了。在两个name里面避免使用“:”,如果需要进一步划分命名空间则可以考虑在
77+
`ThisObjectName` 那个字段里面发挥。
78+
79+
### 使用临时的DisplayObject
80+
使用的时候要小心,

src/scripting/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ clean:
77
definitions:
88
tsc -d --target ES5 src/api/Runtime/Runtime.ts --out src/api/Runtime.d.ts
99
tsc -d --target ES5 src/api/Player/Player.ts --out src/api/Player.d.ts
10+
tsc -d --target ES5 src/api/Tween/Tween.ts --out src/api/Tween.d.ts
1011

1112
compile: compile-ts compile-host
1213

1314
compile-host:
1415
cp src/*.js build/
1516
cp src/api/*.js build/api/
16-
cp src/api/Tween/Tween.js build/api/
1717
cat build/Host.js build/Unpacker.js > build/_Host.js
1818
rm -f build/Unpacker.js
1919
rm -f build/Host.js
@@ -24,3 +24,4 @@ compile-ts:
2424
tsc --target ES5 src/api/Runtime/Runtime.ts --out build/api/Runtime.js
2525
tsc --target ES5 src/api/Player/Player.ts --out build/api/Player.js
2626
tsc --target ES5 src/api/Utils/Utils.ts --out build/api/Utils.js
27+
tsc --target ES5 src/api/Tween/Tween.ts --out build/api/Tween.js

src/scripting/build/Host.js

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var CCLScripting = function(workerUrl){
4848
};
4949
this.updateProperty = function(objectId, propName, value){
5050
if(!objects[objectId]){
51-
scripter.logger.error("Object not found.");
51+
scripter.logger.error("Object (" + objectId + ") not found.");
5252
return;
5353
}
5454
if(objects[objectId][propName] === undefined){
@@ -61,7 +61,7 @@ var CCLScripting = function(workerUrl){
6161
};
6262
this.callMethod = function(objectId, methodName, params){
6363
if(!objects[objectId]){
64-
scripter.logger.error("Object not found.");
64+
scripter.logger.error("Object (" + objectId + ") not found.");
6565
return;
6666
}
6767
if(!objects[objectId][methodName]){
@@ -80,7 +80,27 @@ var CCLScripting = function(workerUrl){
8080
};
8181
}
8282
};
83-
83+
this.getObject = function(objectId){
84+
if(!objects.hasOwnProperty(objectId)){
85+
scripter.logger.error("Object (" + objectId + ") not found.");
86+
return objects[objectId];
87+
}
88+
return objects[objectId];
89+
};
90+
this.invokeError = function(msg, mode){
91+
switch(mode){
92+
case "err":
93+
scripter.logger.error(msg);
94+
break;
95+
case "warn":
96+
scripter.logger.warn(msg);
97+
break;
98+
default:
99+
case "log":
100+
scripter.logger.log(msg);
101+
break;
102+
}
103+
};
84104
this.clear = function(){
85105

86106
};
@@ -821,6 +841,19 @@ var CCLScripting = function(workerUrl){
821841
ScriptingContext.prototype.Unpack.Sprite = function(stage, data, ctx){
822842
this.DOM = _("div",{"style":{"position":"absolute"}});
823843

844+
this.__defineSetter__("x", function(f){
845+
this.setX(f);
846+
});
847+
this.__defineSetter__("y", function(f){
848+
this.setY(f);
849+
});
850+
this.__defineGetter__("x", function(f){
851+
return this.DOM.offsetLeft;
852+
});
853+
this.__defineGetter__("y", function(f){
854+
return this.DOM.offsetTop;
855+
});
856+
824857
this.setX = function(x){
825858
this.DOM.style.left = x + "px";
826859
};
@@ -837,6 +870,28 @@ var CCLScripting = function(workerUrl){
837870
this.DOM.style.height = height + "px";
838871
};
839872

873+
this.addChild = function(childitem){
874+
var child = ctx.getObject(childitem);
875+
if(!child)
876+
return;
877+
if(child.DOM){
878+
this.DOM.appendChild(child.DOM);
879+
}else{
880+
ctx.invokeError("Sprite.addChild failed. Attempted to add non object","err");
881+
}
882+
};
883+
884+
this.removeChild = function(childitem){
885+
var child = ctx.getObject(childitem);
886+
if(!child)
887+
return;
888+
try{
889+
this.DOM.removeChild(child.DOM);
890+
}catch(e){
891+
ctx.invokeError(e.stack, "err");
892+
}
893+
};
894+
840895
this.unload = function(){
841896
try{
842897
stage.removeChild(this.DOM);

src/scripting/build/Worker.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ importScripts('api/Runtime.js', 'api/ScriptManager.js', 'api/Player.js', 'api/Di
1616

1717
/** Immediately Hook into the eval channel, blocking future hooks **/
1818
__schannel("::eval", function(msg){
19+
if(Tween && Tween.extendWithEasingFunctions){
20+
Tween.extendWithEasingFunctions(this);
21+
}
1922
eval(msg);
2023
});
2124
__schannel("::debug", function(msg){

0 commit comments

Comments
 (0)