-
Notifications
You must be signed in to change notification settings - Fork 1
/
View.ts
92 lines (75 loc) · 2.5 KB
/
View.ts
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import FrostFlake from "../FrostFlake";
import Positionable from "../Positionables/Positionable";
export default class View {
children: Array<Positionable> = [];
sortNeeded = false;
initialized = false;
constructor() {
this.initialized = false;
FrostFlake.Log.trace(`View ${this.constructor.name} created.`);
}
async initialize(): Promise<void> {
FrostFlake.Log.trace(`View ${this.constructor.name} initializing...`);
}
start(): void {
(async () => {
await this.initialize();
this.initialized = true;
})();
FrostFlake.Log.trace(`View ${this.constructor.name} initialized.`);
}
update(): void {
if(this.sortNeeded) {
this.children.sort((a, b) => {
return a.layer - b.layer;
});
this.sortNeeded = false;
}
for (let i = 0; i < this.children.length; i++) {
this.children[i].update();
}
}
addChild(positionable: Positionable): void {
if (this.children.indexOf(positionable) > -1) {
throw "positionable has already been added to view."
}
positionable.parent = this;
this.children.push(positionable);
// We can't sort the list here because we could be
// in the middle of updating, make sure the list is
// sorted before the next frame
this.sortNeeded = true;
}
removeChild(positionable: Positionable): void {
if(this.tryRemoveItem(positionable, this.children)) {
positionable.parent = null;
}
}
tryRemoveItem(item: unknown, list: Array<unknown>): boolean {
const i = list.indexOf(item);
if (i > -1) {
list.splice(i, 1);
return true;
}
else {
FrostFlake.Log.warn("Tried to remove item that wasn't found in collection.");
return false;
}
}
destroyChild(positionable: Positionable): void {
this.removeChild(positionable);
positionable.destroy();
}
clearChildren(): void {
for(let i = this.children.length - 1; i >= 0; i--) {
this.removeChild(this.children[i]);
}
}
destroy(): void {
for (let i = this.children.length - 1; i > -1; i--) {
this.destroyChild(this.children[i]);
}
// TODO: clear texture cache?
FrostFlake.Log.trace(`View ${this.constructor.name} destroyed.`);
}
}