diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2019-12-24 13:53:16 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2019-12-24 13:53:16 +0100 |
commit | 3c51a1994f41b625823c4f15e92396b5498ce23c (patch) | |
tree | 7a96310ec32df82ac87039ea555300bcab510a5e /src/renderer/view/input | |
parent | 33926af829848050c54c698ed22da9fe2b912aea (diff) | |
download | rpgedit-3c51a1994f41b625823c4f15e92396b5498ce23c.tar rpgedit-3c51a1994f41b625823c4f15e92396b5498ce23c.zip |
Move renderer into "runtime" subdirectory
Diffstat (limited to 'src/renderer/view/input')
-rw-r--r-- | src/renderer/view/input/gameinput.ts | 78 | ||||
-rw-r--r-- | src/renderer/view/input/inputhandler.ts | 44 |
2 files changed, 0 insertions, 122 deletions
diff --git a/src/renderer/view/input/gameinput.ts b/src/renderer/view/input/gameinput.ts deleted file mode 100644 index 67fbe0c..0000000 --- a/src/renderer/view/input/gameinput.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { InputHandler } from './inputhandler'; - -import { Listenable } from '../../util'; - -import { vec2 } from 'gl-matrix'; - -export enum ButtonCode { - Action, - Back, - Menu, -} - -const buttonMapping: Record<string, ButtonCode> = { - KeyZ: ButtonCode.Action, - KeyX: ButtonCode.Back, - KeyC: ButtonCode.Menu, -}; - -export interface DirectionInput { - type: 'direction'; - direction: vec2; -} - -export interface ButtonInput { - type: 'button'; - button: ButtonCode; -} - -export type GameInput = DirectionInput | ButtonInput; - -export class GameInputHandler extends Listenable<[GameInput]> { - private readonly input: InputHandler; - - constructor() { - super(); - - this.input = new InputHandler( - new Set([ - 'ArrowLeft', - 'ArrowUp', - 'ArrowRight', - 'ArrowDown', - ...Object.keys(buttonMapping), - ])); - - this.input.addListener((key: string, pressed: boolean) => { - const button = buttonMapping[key]; - if (button !== undefined) { - if (pressed) - this.runListeners({ - type: 'button', - button, - }); - - return; - } - - const dir = vec2.create(); - - if (this.input.has('ArrowLeft')) - vec2.add(dir, dir, [-1, 0]); - if (this.input.has('ArrowUp')) - vec2.add(dir, dir, [0, -1]); - if (this.input.has('ArrowRight')) - vec2.add(dir, dir, [1, 0]); - if (this.input.has('ArrowDown')) - vec2.add(dir, dir, [0, 1]); - - if (vec2.sqrLen(dir) > 0) - vec2.normalize(dir, dir); - - this.runListeners({ - type: 'direction', - direction: dir, - }); - }); - } -} diff --git a/src/renderer/view/input/inputhandler.ts b/src/renderer/view/input/inputhandler.ts deleted file mode 100644 index 17abfe6..0000000 --- a/src/renderer/view/input/inputhandler.ts +++ /dev/null @@ -1,44 +0,0 @@ -import { Listenable } from '../../util'; - -export class InputHandler extends Listenable<[string, boolean]> { - private readonly keys: Set<string> = new Set(); - - constructor(relevantKeys: Set<string>) { - super(); - - window.addEventListener('keydown', (ev) => { - if (!relevantKeys.has(ev.code)) - return; - - ev.preventDefault(); - - if (ev.repeat) - return; - - this.keys.add(ev.code); - this.runListeners(ev.code, true); - }); - - window.addEventListener('keyup', (ev) => { - if (!relevantKeys.has(ev.code)) - return; - - ev.preventDefault(); - - if (!this.keys.has(ev.code)) - return; - - this.keys.delete(ev.code); - this.runListeners(ev.code, false); - }); - - window.addEventListener('blur', () => { - this.keys.clear(); - this.runListeners('', false); - }); - } - - public has(key: string): boolean { - return this.keys.has(key); - } -} |