-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu.ts
62 lines (54 loc) · 2.01 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
namespace kodu {
export type MenuDirection
= "up"
| "down"
| "right"
;
export type MenuItemDefn = {
icon: string;
label: string;
style?: ButtonStyle;
};
export class Menu extends Component {
buttons: Button[];
constructor(stage: Stage, private items: MenuItemDefn[], private hud: boolean, private wrap: number = 4) {
super(stage, "menu");
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(this.stage, item.style, item.icon, item.label, x, y, this.hud, (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 destroy() {
this.hide();
this.stage.remove(this);
}
public isVisible() {
return this.buttons.length > 0;
}
update(dt: number) {
this.buttons.forEach(button => button.update(dt));
}
}
}