forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.ts
69 lines (58 loc) · 1.71 KB
/
component.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
namespace microcode {
let id_sequence = 0
export type ComponentHandler = (comp: Component) => void
export interface IKindable {
kind: string
}
export abstract class Component implements IKindable {
private id_: number
private data_: any
private _destroyHandlers: ComponentHandler[]
//% blockCombine block="id" callInDebugger
get id() {
return this.id_
}
get data(): any {
if (!this.data_) {
this.data_ = {}
}
return this.data_
}
constructor(public kind: string) {
this.id_ = id_sequence++
}
onDestroy(handler: ComponentHandler) {
this._destroyHandlers = this._destroyHandlers || []
this._destroyHandlers.push(handler)
}
/* virtual */ destroy() {
const handlers = this._destroyHandlers || []
this._destroyHandlers = undefined
for (const handler of handlers) {
handler(this)
}
this.data_ = undefined
}
/* abstract */ update() {}
/* abstract */ draw() {}
}
export interface IPlaceable {
xfrm: Affine
}
export interface ISizable {
width: number
height: number
}
export class Placeable extends Component implements IPlaceable {
private xfrm_: Affine
//% blockCombine block="xfrm" callInDebugger
public get xfrm() {
return this.xfrm_
}
constructor(parent?: IPlaceable) {
super("placeable")
this.xfrm_ = new Affine()
this.xfrm_.parent = parent && parent.xfrm
}
}
}