-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwtk.ts
96 lines (83 loc) · 2.55 KB
/
wtk.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
93
94
95
96
namespace SpriteKind {
//% isKind
export const Widget = SpriteKind.create();
}
namespace wtk {
class WidgetStyle {
public constructor(
public foregroundColor: number = 0,
public backgroundColor: number = 0,
public border: number = 0,
public borderColor: number = 0,
public padding: number = 0,
public paddingColor: number = 0,
public margin: number = 0,
public marginColor: number = 0
) { }
}
class Widget extends Sprite {
protected _parent: Widget | null;
protected _children: Widget[];
protected _style: WidgetStyle;
public constructor() {
super(img`
.
`);
this.setKind(SpriteKind.Widget);
this._parent = null;
this._children = [];
this._style = new WidgetStyle();
}
public addChild(w: Widget): boolean {
if (this._children.indexOf(w) === -1) {
if (w._parent != null && !w._parent.removeChild(w)) {
return false;
}
w._parent = this;
this._children.push(w);
return true;
} else {
return false;
}
}
public addChildren(ws: Widget[]): boolean {
let success = true;
ws.forEach((w: Widget) => {
if (!this.addChild(w)) {
success = false;
}
})
return success;
}
public removeChild(w: Widget): boolean {
w._parent = null;
return this._children.removeElement(w);
}
public removeChildren(ws: Widget[]): boolean {
let success = true;
ws.forEach((w: Widget) => {
if (!this.removeChild(w)) {
success = false;
}
})
return success;
}
public get children(): Widget[] {
return this._children;
}
public destroy(effect?: effects.ParticleEffect, duration?: number): void {
super.destroy(effect, duration);
this._children.forEach((w: Widget) => {
w.destroy(effect, duration);
})
}
public get style(): WidgetStyle {
return this._style;
}
public set style(s: WidgetStyle) {
this._style = s;
this.rerender();
}
public rerender(): void { }
}
}