disabled right click on canvas and improved handling of blur

This commit is contained in:
Joe Weaver 2020-08-10 13:51:33 -04:00
parent 67bcba2c5f
commit 91fcfd0a0d
2 changed files with 40 additions and 2 deletions

View File

@ -10,8 +10,12 @@ export default class InputHandler{
canvas.onmousedown = (event) => this.handleMouseDown(event, canvas);
canvas.onmouseup = (event) => this.handleMouseUp(event, canvas);
canvas.oncontextmenu = this.handleContextMenu;
canvas.onmousemove = (event) => this.handleMouseMove(event, canvas);
document.onkeydown = this.handleKeyDown;
document.onkeyup = this.handleKeyUp;
document.onblur = this.handleBlur;
document.oncontextmenu = this.handleBlur;
}
private handleMouseDown = (event: MouseEvent, canvas: HTMLCanvasElement): void => {
@ -26,6 +30,12 @@ export default class InputHandler{
this.eventQueue.addEvent(gameEvent);
}
private handleMouseMove = (event: MouseEvent, canvas: HTMLCanvasElement): void => {
let pos = this.getMousePosition(event, canvas);
let gameEvent = new GameEvent("mouse_move", {position: pos});
this.eventQueue.addEvent(gameEvent);
}
private handleKeyDown = (event: KeyboardEvent): void => {
let key = this.getKey(event);
let gameEvent = new GameEvent("key_down", {key: key});
@ -38,6 +48,16 @@ export default class InputHandler{
this.eventQueue.addEvent(gameEvent);
}
private handleBlur = (event: Event): void => {
let gameEvent = new GameEvent("canvas_blur", {});
this.eventQueue.addEvent(gameEvent);
}
private handleContextMenu = (event: Event): void => {
event.preventDefault();
event.stopPropagation();
}
private getKey(keyEvent: KeyboardEvent){
return keyEvent.key.toLowerCase();
}

View File

@ -10,6 +10,7 @@ export default class InputReceiver{
private mouseJustPressed: boolean;
private keyJustPressed: Map<boolean>;
private keyPressed: Map<boolean>;
private mousePosition: Vec2;
private mousePressPosition: Vec2;
private eventQueue: EventQueue;
private receiver: Receiver;
@ -23,7 +24,7 @@ export default class InputReceiver{
this.mousePressPosition = null;
this.eventQueue = EventQueue.getInstance();
this.eventQueue.subscribe(this.receiver, ["mouse_down", "mouse_up", "key_down", "key_up"]);
this.eventQueue.subscribe(this.receiver, ["mouse_down", "mouse_up", "mouse_move", "key_down", "key_up", "canvas_blur"]);
}
static getInstance(): InputReceiver{
@ -50,6 +51,10 @@ export default class InputReceiver{
this.mousePressed = false;
}
if(event.type === "mouse_move"){
this.mousePosition = event.data.get("position");
}
if(event.type === "key_down"){
let key = event.data.get("key")
this.keyJustPressed.set(key, true);
@ -60,8 +65,17 @@ export default class InputReceiver{
let key = event.data.get("key")
this.keyPressed.set(key, false);
}
if(event.type === "canvas_blur"){
this.clearKeyPresses()
}
}
}
clearKeyPresses(): void {
this.keyJustPressed.forEach((key: string) => this.keyJustPressed.set(key, false));
this.keyPressed.forEach((key: string) => this.keyPressed.set(key, false));
}
isJustPressed(key: string): boolean {
if(this.keyJustPressed.has(key)){
@ -87,6 +101,10 @@ export default class InputReceiver{
return this.mousePressed;
}
getMousePosition(): Vec2 {
return this.mousePosition;
}
getMousePressPosition(): Vec2 {
return this.mousePressPosition;
}