1
0
Fork 0
spacetac/src/ui/common/InputManager.ts

116 lines
3.5 KiB
TypeScript
Raw Normal View History

2017-02-09 00:00:35 +00:00
module TS.SpaceTac.UI {
2017-05-03 18:12:13 +00:00
type KeyPressedCallback = (key: string) => void;
2015-04-23 22:36:57 +00:00
2017-05-03 18:12:13 +00:00
/**
* Manager for keyboard/mouse/touch events.
*/
export class InputManager {
private view: BaseView
private game: MainUI
private input: Phaser.Input
2015-04-23 22:36:57 +00:00
2017-05-03 18:12:13 +00:00
private cheats_allowed: boolean
private cheat: boolean
2015-04-23 22:36:57 +00:00
2017-05-03 18:12:13 +00:00
private binds: { [key: string]: KeyPressedCallback } = {}
2015-04-23 22:36:57 +00:00
2017-05-03 18:12:13 +00:00
private keyboard_grabber: any = null
private keyboard_callback: KeyPressedCallback | null = null
2015-04-23 22:36:57 +00:00
constructor(view: BaseView) {
this.view = view;
this.game = view.gameui;
this.input = view.input;
2017-05-03 18:12:13 +00:00
this.cheats_allowed = true;
2015-04-23 22:36:57 +00:00
this.cheat = false;
2017-02-12 18:54:09 +00:00
this.input.reset(true);
2015-04-23 22:36:57 +00:00
// Default mappings
2017-05-03 18:12:13 +00:00
this.bind("s", "Quick save", () => {
2015-04-23 22:36:57 +00:00
this.game.saveGame();
});
2017-05-03 18:12:13 +00:00
this.bind("l", "Quick load", () => {
2015-04-23 22:36:57 +00:00
this.game.loadGame();
this.game.state.start("router");
});
2017-05-03 18:12:13 +00:00
this.bind("m", "Toggle sound", () => {
2015-04-23 22:36:57 +00:00
this.game.audio.toggleMute();
});
2017-05-15 23:15:07 +00:00
this.bind("f", "Toggle fullscreen", () => {
view.toggleFullscreen();
});
2017-05-03 18:12:13 +00:00
this.bind("+", "", () => {
if (this.cheats_allowed) {
2015-04-23 22:36:57 +00:00
this.cheat = !this.cheat;
2017-02-12 18:54:09 +00:00
this.game.displayMessage(this.cheat ? "Cheats enabled" : "Cheats disabled");
2015-04-23 22:36:57 +00:00
}
});
2017-05-03 18:12:13 +00:00
this.input.keyboard.addCallbacks(this, undefined, (event: KeyboardEvent) => {
// console.debug(event);
if (!contains(["Control", "Shift", "Alt", "Meta"], event.key)) {
this.keyPress(event.key);
if (event.code != event.key) {
this.keyPress(event.code);
}
}
});
2015-04-23 22:36:57 +00:00
}
2017-05-03 18:12:13 +00:00
/**
* Bind a key to a specific action.
*/
bind(key: string, desc: string, action: Function): void {
this.binds[key] = (key) => action();
2015-04-23 22:36:57 +00:00
}
2017-05-03 18:12:13 +00:00
/**
* Bind a key to a cheat action.
*
* The action will only be applied if cheat mode is activated.
*/
bindCheat(key: string, desc: string, action: Function): void {
2017-03-09 17:11:00 +00:00
this.bind(key, `Cheat: ${desc}`, () => {
2015-04-23 22:36:57 +00:00
if (this.cheat) {
2017-03-09 17:11:00 +00:00
console.warn(`Cheat ! ${desc}`);
2015-04-23 22:36:57 +00:00
action();
}
});
}
2017-05-03 18:12:13 +00:00
/**
* Apply a key press
*/
keyPress(key: string): void {
if (this.keyboard_callback) {
this.keyboard_callback(key);
} else if (this.binds[key]) {
this.binds[key](key);
}
}
/**
* Grab the keyboard to receive next key presses.
*
* Release will happen if another grab is made, or if releaseKeyboard is called.
*
* *handle* is used to identify the grabber.
*/
grabKeyboard(handle: any, callback: KeyPressedCallback): void {
this.keyboard_grabber = handle;
this.keyboard_callback = callback;
}
/**
* Release the keyboard.
*/
releaseKeyboard(handle: any): void {
if (handle === this.keyboard_grabber) {
this.keyboard_grabber = null;
this.keyboard_callback = null;
}
}
2015-04-23 22:36:57 +00:00
}
}