disabled right click on canvas and improved handling of blur
This commit is contained in:
parent
67bcba2c5f
commit
91fcfd0a0d
|
@ -10,8 +10,12 @@ export default class InputHandler{
|
||||||
|
|
||||||
canvas.onmousedown = (event) => this.handleMouseDown(event, canvas);
|
canvas.onmousedown = (event) => this.handleMouseDown(event, canvas);
|
||||||
canvas.onmouseup = (event) => this.handleMouseUp(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.onkeydown = this.handleKeyDown;
|
||||||
document.onkeyup = this.handleKeyUp;
|
document.onkeyup = this.handleKeyUp;
|
||||||
|
document.onblur = this.handleBlur;
|
||||||
|
document.oncontextmenu = this.handleBlur;
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleMouseDown = (event: MouseEvent, canvas: HTMLCanvasElement): void => {
|
private handleMouseDown = (event: MouseEvent, canvas: HTMLCanvasElement): void => {
|
||||||
|
@ -26,6 +30,12 @@ export default class InputHandler{
|
||||||
this.eventQueue.addEvent(gameEvent);
|
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 => {
|
private handleKeyDown = (event: KeyboardEvent): void => {
|
||||||
let key = this.getKey(event);
|
let key = this.getKey(event);
|
||||||
let gameEvent = new GameEvent("key_down", {key: key});
|
let gameEvent = new GameEvent("key_down", {key: key});
|
||||||
|
@ -38,6 +48,16 @@ export default class InputHandler{
|
||||||
this.eventQueue.addEvent(gameEvent);
|
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){
|
private getKey(keyEvent: KeyboardEvent){
|
||||||
return keyEvent.key.toLowerCase();
|
return keyEvent.key.toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ export default class InputReceiver{
|
||||||
private mouseJustPressed: boolean;
|
private mouseJustPressed: boolean;
|
||||||
private keyJustPressed: Map<boolean>;
|
private keyJustPressed: Map<boolean>;
|
||||||
private keyPressed: Map<boolean>;
|
private keyPressed: Map<boolean>;
|
||||||
|
private mousePosition: Vec2;
|
||||||
private mousePressPosition: Vec2;
|
private mousePressPosition: Vec2;
|
||||||
private eventQueue: EventQueue;
|
private eventQueue: EventQueue;
|
||||||
private receiver: Receiver;
|
private receiver: Receiver;
|
||||||
|
@ -23,7 +24,7 @@ export default class InputReceiver{
|
||||||
this.mousePressPosition = null;
|
this.mousePressPosition = null;
|
||||||
|
|
||||||
this.eventQueue = EventQueue.getInstance();
|
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{
|
static getInstance(): InputReceiver{
|
||||||
|
@ -50,6 +51,10 @@ export default class InputReceiver{
|
||||||
this.mousePressed = false;
|
this.mousePressed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(event.type === "mouse_move"){
|
||||||
|
this.mousePosition = event.data.get("position");
|
||||||
|
}
|
||||||
|
|
||||||
if(event.type === "key_down"){
|
if(event.type === "key_down"){
|
||||||
let key = event.data.get("key")
|
let key = event.data.get("key")
|
||||||
this.keyJustPressed.set(key, true);
|
this.keyJustPressed.set(key, true);
|
||||||
|
@ -60,8 +65,17 @@ export default class InputReceiver{
|
||||||
let key = event.data.get("key")
|
let key = event.data.get("key")
|
||||||
this.keyPressed.set(key, false);
|
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 {
|
isJustPressed(key: string): boolean {
|
||||||
if(this.keyJustPressed.has(key)){
|
if(this.keyJustPressed.has(key)){
|
||||||
|
@ -87,6 +101,10 @@ export default class InputReceiver{
|
||||||
return this.mousePressed;
|
return this.mousePressed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getMousePosition(): Vec2 {
|
||||||
|
return this.mousePosition;
|
||||||
|
}
|
||||||
|
|
||||||
getMousePressPosition(): Vec2 {
|
getMousePressPosition(): Vec2 {
|
||||||
return this.mousePressPosition;
|
return this.mousePressPosition;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user