This repository has been archived by the owner on Oct 8, 2022. It is now read-only.
/
director.js
58 lines (58 loc) · 1.48 KB
/
director.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import Container from './container.js';
export default class Director extends Container {
constructor(options) {
super(options);
this.scenes = {};
this.needUpdateTransform = false;
this.widthCache = {};
this.heightCache = {};
}
get width() {
return this._width;
}
set width(w) {
this._width = w;
this.widthCache = {};
}
getX(n = 1) {
return this.widthCache[n] || (this.widthCache[n] = this._width * n);
}
get height() {
return this._height;
}
set height(h) {
this._height = h;
this.heightCache = {};
}
getY(n = 1) {
return this.heightCache[n] || (this.heightCache[n] = this._height * n);
}
get center() {
return this.getX(0.5);
}
get middle() {
return this.getY(0.5);
}
set(name, scene) {
if (Object.prototype.toString.call(scene) == '[object Function]') {
if (scene.constructor) this.scenes[name] = scene;
}
return this;
}
go(scene) {
let type = Object.prototype.toString.call(scene);
if (type == '[object String]') {
if (this.scenes[scene]) this.clear().add(new this.scenes[scene](...Array.prototype.splice.call(arguments, 1)));
} else if (type == '[object Function]') {
if (scene.constructor) {
this.clear().add(new scene(...Array.prototype.splice.call(arguments, 1)));
} else {
let res = scene(...Array.prototype.splice.call(arguments, 1));
if (res instanceof Container) this.clear().add(res);
}
} else if (type == '[object Object]') {
if (scene instanceof Container) this.clear().add(scene);
}
return this;
}
}