2019-11-21 22:14:27 +00:00
|
|
|
import { Timer } from "../../common/Timer"
|
|
|
|
import { contains, nop } from "../../common/Tools"
|
|
|
|
import { MainUI } from "../../MainUI"
|
|
|
|
import { BaseView } from "../BaseView"
|
|
|
|
import { UIButton } from "./UIButton"
|
|
|
|
import { UIContainer } from "./UIContainer"
|
|
|
|
import { UIImage } from "./UIImage"
|
|
|
|
import { UITools } from "./UITools"
|
|
|
|
|
|
|
|
export type KeyPressedCallback = (key: string) => void
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manager for keyboard/mouse/touch events.
|
|
|
|
*/
|
|
|
|
export class InputManager {
|
|
|
|
private debug = false
|
|
|
|
private view: BaseView
|
|
|
|
private game: MainUI
|
|
|
|
|
|
|
|
private cheats_allowed: boolean
|
|
|
|
private cheat: boolean
|
|
|
|
|
|
|
|
private hovered: UIButton | UIContainer | UIImage | null = null
|
|
|
|
|
|
|
|
private binds: { [key: string]: KeyPressedCallback } = {}
|
|
|
|
|
|
|
|
private keyboard_grabber: any = null
|
|
|
|
private keyboard_callback: KeyPressedCallback | null = null
|
|
|
|
|
|
|
|
constructor(view: BaseView) {
|
|
|
|
this.view = view;
|
|
|
|
this.game = view.gameui;
|
|
|
|
this.cheats_allowed = true;
|
|
|
|
this.cheat = false;
|
|
|
|
|
|
|
|
// Default mappings
|
|
|
|
this.bind("s", "Quick save", () => {
|
|
|
|
this.game.saveGame();
|
|
|
|
});
|
|
|
|
this.bind("l", "Quick load", () => {
|
|
|
|
this.game.loadGame();
|
|
|
|
this.view.backToRouter();
|
|
|
|
});
|
|
|
|
this.bind("m", "Toggle sound", () => {
|
|
|
|
this.game.options.setNumberValue("mainvolume", this.game.options.getNumberValue("mainvolume") > 0 ? 0 : 1);
|
|
|
|
});
|
|
|
|
this.bind("f", "Toggle fullscreen", () => {
|
|
|
|
this.game.options.setBooleanValue("fullscreen", !this.game.options.getBooleanValue("fullscreen"));
|
|
|
|
});
|
|
|
|
this.bind("+", "", () => {
|
|
|
|
if (this.cheats_allowed) {
|
|
|
|
this.cheat = !this.cheat;
|
|
|
|
this.game.displayMessage(this.cheat ? "Cheats enabled" : "Cheats disabled");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!this.game.isTesting) {
|
|
|
|
this.view.input.keyboard.on("keyup", (event: KeyboardEvent) => {
|
|
|
|
if (this.debug) {
|
|
|
|
console.log(event);
|
2015-04-23 22:36:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:14:27 +00:00
|
|
|
this.forceLeaveHovered();
|
2018-05-15 14:57:45 +00:00
|
|
|
|
2019-11-21 22:14:27 +00:00
|
|
|
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
|
|
|
}
|
2019-11-21 22:14:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the bindings
|
|
|
|
*/
|
|
|
|
destroy(): void {
|
|
|
|
this.view.input.keyboard.removeAllListeners("keyup");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bind a key to a specific action.
|
|
|
|
*/
|
|
|
|
bind(key: string, desc: string, action: Function): void {
|
|
|
|
this.binds[key] = (key) => action();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
this.bind(key, `Cheat: ${desc}`, () => {
|
|
|
|
if (this.cheat) {
|
|
|
|
this.game.displayMessage(`Cheat ! ${desc}`);
|
|
|
|
action();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force the cursor out of currently hovered object
|
|
|
|
*/
|
|
|
|
private forceLeaveHovered() {
|
|
|
|
if (this.hovered && this.hovered.data) {
|
|
|
|
let pointer = this.hovered.data.get("pointer");
|
|
|
|
if (pointer) {
|
|
|
|
this.hovered.emit("pointerout", pointer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup hover/click handlers on an UI element
|
|
|
|
*
|
|
|
|
* This is done in a way that should be compatible with touch-enabled screen
|
|
|
|
*/
|
|
|
|
setHoverClick(obj: UIButton | UIContainer | UIImage, enter: Function = nop, leave: Function = nop, click: Function = nop, hovertime = 300, holdtime = 600, sound = false): void {
|
|
|
|
let holdstart = Timer.nowMs();
|
|
|
|
let enternext: Function | null = null;
|
|
|
|
let entercalled = false;
|
|
|
|
let cursorinside = false;
|
|
|
|
let leftbutton = false;
|
|
|
|
let destroyed = false;
|
|
|
|
|
|
|
|
obj.setDataEnabled();
|
|
|
|
|
|
|
|
if (obj instanceof UIImage) {
|
|
|
|
obj.setInteractive();
|
|
|
|
} else if (!(obj instanceof UIButton)) {
|
|
|
|
let bounds = obj.getBounds();
|
|
|
|
bounds.x -= obj.x;
|
|
|
|
bounds.y -= obj.y;
|
|
|
|
obj.setInteractive({
|
|
|
|
hitArea: bounds,
|
|
|
|
hitAreaCallback: Phaser.Geom.Rectangle.Contains,
|
|
|
|
});
|
|
|
|
}
|
2015-04-23 22:36:57 +00:00
|
|
|
|
2019-11-21 22:14:27 +00:00
|
|
|
let prevententer = () => {
|
|
|
|
if (enternext != null) {
|
|
|
|
Timer.global.cancel(enternext);
|
|
|
|
enternext = null;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let effectiveenter = () => {
|
|
|
|
if (!destroyed) {
|
|
|
|
enternext = null;
|
|
|
|
entercalled = true;
|
|
|
|
enter();
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 18:12:13 +00:00
|
|
|
|
2019-11-21 22:14:27 +00:00
|
|
|
let effectiveleave = () => {
|
|
|
|
prevententer();
|
|
|
|
if (entercalled) {
|
|
|
|
entercalled = false;
|
|
|
|
leave();
|
|
|
|
}
|
|
|
|
}
|
2017-05-03 18:12:13 +00:00
|
|
|
|
2019-11-21 22:14:27 +00:00
|
|
|
obj.on("destroy", () => {
|
|
|
|
destroyed = true;
|
|
|
|
effectiveleave();
|
|
|
|
});
|
|
|
|
|
|
|
|
obj.on("pointerover", (pointer: Phaser.Input.Pointer) => {
|
|
|
|
if (destroyed || !UITools.isVisible(obj)) return;
|
|
|
|
|
|
|
|
if (this.hovered) {
|
|
|
|
if (this.hovered === obj) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (this.debug) {
|
|
|
|
console.log("Pointer force out", this.hovered);
|
|
|
|
}
|
|
|
|
this.forceLeaveHovered();
|
2017-05-03 18:12:13 +00:00
|
|
|
}
|
2019-11-21 22:14:27 +00:00
|
|
|
}
|
|
|
|
this.hovered = obj;
|
|
|
|
this.hovered.data.set("pointer", pointer);
|
|
|
|
|
|
|
|
if (this.debug) {
|
|
|
|
console.log("Pointer over", obj);
|
|
|
|
}
|
2017-05-03 18:12:13 +00:00
|
|
|
|
2019-11-21 22:14:27 +00:00
|
|
|
cursorinside = true;
|
|
|
|
enternext = Timer.global.schedule(hovertime, effectiveenter);
|
|
|
|
});
|
|
|
|
|
|
|
|
obj.on("pointerout", () => {
|
|
|
|
if (destroyed) return;
|
|
|
|
|
|
|
|
if (this.hovered === obj) {
|
|
|
|
this.hovered = null;
|
|
|
|
if (this.debug) {
|
|
|
|
console.log("Pointer out", obj);
|
2017-05-03 18:12:13 +00:00
|
|
|
}
|
2019-11-21 22:14:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cursorinside = false;
|
|
|
|
effectiveleave();
|
|
|
|
});
|
2017-10-01 20:52:50 +00:00
|
|
|
|
2019-11-21 22:14:27 +00:00
|
|
|
obj.on("pointerdown", (pointer?: Phaser.Input.Pointer) => {
|
|
|
|
if (destroyed || (pointer && pointer.buttons != 1)) return;
|
|
|
|
leftbutton = true;
|
|
|
|
|
|
|
|
if (UITools.isVisible(obj)) {
|
|
|
|
holdstart = Timer.nowMs();
|
|
|
|
if (sound) {
|
|
|
|
this.view.audio.playOnce("ui-button-down");
|
|
|
|
}
|
|
|
|
if (!cursorinside && !enternext) {
|
|
|
|
enternext = Timer.global.schedule(holdtime, effectiveenter);
|
2017-10-11 22:28:33 +00:00
|
|
|
}
|
2019-11-21 22:14:27 +00:00
|
|
|
}
|
|
|
|
});
|
2017-10-11 22:28:33 +00:00
|
|
|
|
2019-11-21 22:14:27 +00:00
|
|
|
obj.on("pointerup", (pointer?: Phaser.Input.Pointer) => {
|
|
|
|
if (destroyed || !leftbutton) return;
|
|
|
|
leftbutton = false;
|
|
|
|
|
|
|
|
if (!cursorinside) {
|
|
|
|
effectiveleave();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Timer.fromMs(holdstart) < holdtime) {
|
|
|
|
if (!cursorinside) {
|
|
|
|
effectiveenter();
|
2017-10-01 20:52:50 +00:00
|
|
|
}
|
2019-11-21 22:14:27 +00:00
|
|
|
if (sound) {
|
|
|
|
this.view.audio.playOnce("ui-button-up");
|
|
|
|
}
|
|
|
|
click();
|
|
|
|
if (!cursorinside) {
|
|
|
|
effectiveleave();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-04-23 22:36:57 +00:00
|
|
|
}
|