From 9f2e4e6996fb96d23e1ca29667130a2111a59de5 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 1 Nov 2018 00:59:13 +0100 Subject: Do not use default exports --- src/view/input/DirectionHandler.ts | 36 ----------------------------------- src/view/input/InputHandler.ts | 39 -------------------------------------- src/view/input/directionhandler.ts | 37 ++++++++++++++++++++++++++++++++++++ src/view/input/inputhandler.ts | 39 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 75 deletions(-) delete mode 100644 src/view/input/DirectionHandler.ts delete mode 100644 src/view/input/InputHandler.ts create mode 100644 src/view/input/directionhandler.ts create mode 100644 src/view/input/inputhandler.ts (limited to 'src/view/input') diff --git a/src/view/input/DirectionHandler.ts b/src/view/input/DirectionHandler.ts deleted file mode 100644 index 691ba01..0000000 --- a/src/view/input/DirectionHandler.ts +++ /dev/null @@ -1,36 +0,0 @@ -import {Listenable} from '../../util'; -import InputHandler from './InputHandler'; - -import {vec2} from 'gl-matrix'; - -export const enum Keycode { - Left = 37, - Up = 38, - Right = 39, - Down = 40, -} - -export default class DirectionHandler extends Listenable<[vec2]> { - private readonly input: InputHandler; - - constructor() { - super(); - - this.input = new InputHandler(new Set([Keycode.Left, Keycode.Up, Keycode.Right, Keycode.Down])); - - this.input.addListener(() => { - const dir = vec2.create(); - - if (this.input.has(Keycode.Left)) - vec2.add(dir, dir, [-1, 0]); - if (this.input.has(Keycode.Up)) - vec2.add(dir, dir, [0, -1]); - if (this.input.has(Keycode.Right)) - vec2.add(dir, dir, [1, 0]); - if (this.input.has(Keycode.Down)) - vec2.add(dir, dir, [0, 1]); - - this.runListeners(dir); - }); - } -} diff --git a/src/view/input/InputHandler.ts b/src/view/input/InputHandler.ts deleted file mode 100644 index a6f1293..0000000 --- a/src/view/input/InputHandler.ts +++ /dev/null @@ -1,39 +0,0 @@ -import {Listenable} from '../../util'; - -export default class InputHandler extends Listenable<[]> { - private readonly keys: Set = new Set(); - - constructor(relevantKeys: Set) { - super(); - - window.addEventListener('keydown', (ev) => { - if (!relevantKeys.has(ev.keyCode)) - return; - - ev.preventDefault(); - - if (this.keys.has(ev.keyCode)) - return; - - this.keys.add(ev.keyCode); - this.runListeners(); - }); - - window.addEventListener('keyup', (ev) => { - if (!relevantKeys.has(ev.keyCode)) - return; - - ev.preventDefault(); - - if (!this.keys.has(ev.keyCode)) - return; - - this.keys.delete(ev.keyCode); - this.runListeners(); - }); - } - - public has(key: number): boolean { - return this.keys.has(key); - } -} diff --git a/src/view/input/directionhandler.ts b/src/view/input/directionhandler.ts new file mode 100644 index 0000000..0a3687e --- /dev/null +++ b/src/view/input/directionhandler.ts @@ -0,0 +1,37 @@ +import { InputHandler } from './inputhandler'; + +import { Listenable } from '../../util'; + +import { vec2 } from 'gl-matrix'; + +export const enum Keycode { + Left = 37, + Up = 38, + Right = 39, + Down = 40, +} + +export class DirectionHandler extends Listenable<[vec2]> { + private readonly input: InputHandler; + + constructor() { + super(); + + this.input = new InputHandler(new Set([Keycode.Left, Keycode.Up, Keycode.Right, Keycode.Down])); + + this.input.addListener(() => { + const dir = vec2.create(); + + if (this.input.has(Keycode.Left)) + vec2.add(dir, dir, [-1, 0]); + if (this.input.has(Keycode.Up)) + vec2.add(dir, dir, [0, -1]); + if (this.input.has(Keycode.Right)) + vec2.add(dir, dir, [1, 0]); + if (this.input.has(Keycode.Down)) + vec2.add(dir, dir, [0, 1]); + + this.runListeners(dir); + }); + } +} diff --git a/src/view/input/inputhandler.ts b/src/view/input/inputhandler.ts new file mode 100644 index 0000000..96f15dd --- /dev/null +++ b/src/view/input/inputhandler.ts @@ -0,0 +1,39 @@ +import { Listenable } from '../../util'; + +export class InputHandler extends Listenable<[]> { + private readonly keys: Set = new Set(); + + constructor(relevantKeys: Set) { + super(); + + window.addEventListener('keydown', (ev) => { + if (!relevantKeys.has(ev.keyCode)) + return; + + ev.preventDefault(); + + if (this.keys.has(ev.keyCode)) + return; + + this.keys.add(ev.keyCode); + this.runListeners(); + }); + + window.addEventListener('keyup', (ev) => { + if (!relevantKeys.has(ev.keyCode)) + return; + + ev.preventDefault(); + + if (!this.keys.has(ev.keyCode)) + return; + + this.keys.delete(ev.keyCode); + this.runListeners(); + }); + } + + public has(key: number): boolean { + return this.keys.has(key); + } +} -- cgit v1.2.3