forked from microsoft/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.ts
94 lines (84 loc) · 2.66 KB
/
menu.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
namespace microcode {
export type MenuDirection = "up" | "down" | "right"
export type MenuItemDefn = {
icon: string
label: string
style?: ButtonStyle
}
export class Menu extends Component implements IPlaceable {
private xfrm_: Affine
buttons: Button[]
public get xfrm() {
return this.xfrm_
}
constructor(private items: MenuItemDefn[], private wrap: number = 4) {
super("menu")
this.xfrm_ = new Affine()
this.buttons = []
}
public show(
x: number,
y: number,
direction: MenuDirection,
onSelect: (button: Button) => void
) {
const origX = x
const origY = y
if (this.isVisible()) {
this.hide()
}
this.items.forEach((item, index) => {
const icon = icons.get(item.icon)
item.style = item.style || "white"
const button = new Button({
parent: this,
style: item.style,
icon: item.icon,
label: item.label,
x,
y,
onClick: button => onSelect(button),
})
this.buttons.push(button)
if (direction === "right") {
x += icon.width
} else if (direction === "up") {
y -= icon.height
} else if (direction === "down") {
y += icon.height
}
if (this.wrap > 0 && index > 0 && !((index + 1) % this.wrap)) {
if (direction === "right") {
y += icon.height
x = origX
} else if (direction === "up") {
x -= icon.width
y = origY
} else if (direction === "down") {
x += icon.width
y = origY
}
}
})
}
public hide() {
for (let button of this.buttons) {
button.destroy()
}
this.buttons = []
}
public isVisible() {
return this.buttons.length > 0
}
/* override */ destroy() {
this.hide()
super.destroy()
}
/* override */ update() {
this.buttons.forEach(button => button.update())
}
/* override */ draw() {
this.buttons.forEach(button => button.draw())
}
}
}