hadean-old/src/term-ui/EscapeMenu.ts

79 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-07-19 01:53:30 -04:00
import { Game } from "@game";
import { boxStyle, getTheme } from "@themes";
2021-07-20 16:45:49 -04:00
import { panels } from "./UI.js";
2021-07-19 01:53:30 -04:00
import blessed from 'neo-blessed';
2021-07-23 00:12:02 -04:00
import { quit, restart } from "../ProcessManager.js";
2021-07-19 01:53:30 -04:00
// TODO convert all these popup-y things to be View based
// make them be boxes that have a view
export class EscapeMenu {
options = [
'RESUME',
2021-07-20 16:37:38 -04:00
'RELOAD',
2021-07-19 01:53:30 -04:00
'QUIT'
];
selected = 0;
box;
static show() {
new EscapeMenu();
}
protected constructor() {
this.box = blessed.box({
top: 3,
left: 'center',
2021-07-20 16:37:38 -04:00
width: 20,
2021-07-19 01:53:30 -04:00
height: 'shrink',
content: '',
tags: true,
...boxStyle(),
});
this.box.on('keypress', (evt: {}, key: {full: string}) => {
if(key.full === 'up') {
this.selected --;
if(this.selected === -1) this.selected = this.options.length - 1;
} else if (key.full === 'down') {
this.selected ++;
if(this.selected === this.options.length) this.selected = 0;
} else if (key.full === 'enter') {
switch(this.selected) {
case 0: {
Game.current.clock.start();
panels.screen.remove(this.box);
break;
}
case 1: {
2021-07-20 16:37:38 -04:00
restart();
break;
}
case 2: {
2021-07-19 01:53:30 -04:00
Game.current.sync();
2021-07-20 16:37:38 -04:00
quit();
2021-07-19 01:53:30 -04:00
}
}
} else if(key.full === 'escape') {
Game.current.clock.start();
panels.screen.remove(this.box);
}
this.render();
});
panels.screen.append(this.box);
this.box.focus();
Game.current.clock.pause();
this.render();
}
render() {
2021-07-20 16:37:38 -04:00
const space = ' '.repeat(this.box.width / 2 - 4);
this.box.setContent(space + 'Paused\n\n' + this.options.map((v, i) => {
2021-07-19 01:53:30 -04:00
if(i === this.selected) {
2021-07-20 16:37:38 -04:00
return ` ${getTheme().bright(v)} `;
2021-07-19 01:53:30 -04:00
} else {
2021-07-20 16:37:38 -04:00
return ` ${getTheme().normal(v)} `;
2021-07-19 01:53:30 -04:00
}
}).join('\n'));
panels.screen.render();
}
}