refactor: input normalizer to client
This commit is contained in:
parent
9d5910ebb8
commit
01c475b55d
|
@ -12,6 +12,8 @@
|
|||
"test": "yarn --silent run build --display none && mocha --colors test.js"
|
||||
},
|
||||
"files": [
|
||||
"client.js",
|
||||
"client.js.map",
|
||||
"index.js",
|
||||
"index.js.map",
|
||||
"test.js",
|
||||
|
|
|
@ -44,6 +44,8 @@ export default class ActionRegistry extends decorate(Class) {
|
|||
this.normalizer.on('keyUp', this.onKeyUp, this);
|
||||
this.normalizer.on('wheelDown', this.onWheelDown, this);
|
||||
this.normalizer.on('wheelUp', this.onWheelUp, this);
|
||||
this.normalizer.on('buttonPress', this.onButtonPress, this);
|
||||
this.normalizer.on('buttonRelease', this.onButtonRelease, this);
|
||||
}
|
||||
|
||||
onButtonPress(button) {
|
||||
|
@ -55,7 +57,7 @@ export default class ActionRegistry extends decorate(Class) {
|
|||
}
|
||||
|
||||
onInput(type, input, value, transforming) {
|
||||
const action = this.#listen[type][input] || {};
|
||||
const action = this.#listen[type][input];
|
||||
if (action) {
|
||||
this.#stream.push({
|
||||
action,
|
||||
|
@ -98,6 +100,8 @@ export default class ActionRegistry extends decorate(Class) {
|
|||
this.normalizer.off('keyDown', this.onKeyDown);
|
||||
this.normalizer.off('wheelUp', this.onWheelUp);
|
||||
this.normalizer.off('wheelDown', this.onWheelDown);
|
||||
this.normalizer.off('buttonPress', this.onButtonPress);
|
||||
this.normalizer.off('buttonRelease', this.onButtonRelease);
|
||||
this.normalizer = undefined;
|
||||
}
|
||||
|
||||
|
|
3
packages/input/src/client/index.js
Normal file
3
packages/input/src/client/index.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
export {default as InputNormalizer} from './normalizer';
|
||||
|
||||
export default {};
|
|
@ -1,4 +1,5 @@
|
|||
import {Class, compose, EventEmitter} from '@latus/core';
|
||||
import Gamepads from 'gamepads';
|
||||
|
||||
import PointerEvent from './pointer-event';
|
||||
|
||||
|
@ -18,6 +19,10 @@ export default class InputNormalizer extends decorate(Class) {
|
|||
this.keyUpDelays = {};
|
||||
// Bind event handlers.
|
||||
this.onBlur = this.onBlur.bind(this);
|
||||
this.onButtonPress = this.onButtonPress.bind(this);
|
||||
this.onButtonRelease = this.onButtonRelease.bind(this);
|
||||
this.onGamepadConnect = this.onGamepadConnect.bind(this);
|
||||
this.onGamepadDisconnect = this.onGamepadDisconnect.bind(this);
|
||||
this.onKeyDown = this.onKeyDown.bind(this);
|
||||
this.onKeyUp = this.onKeyUp.bind(this);
|
||||
this.onPointerDown = this.onPointerDown.bind(this);
|
||||
|
@ -29,10 +34,6 @@ export default class InputNormalizer extends decorate(Class) {
|
|||
this.onWheel = this.onWheel.bind(this);
|
||||
}
|
||||
|
||||
actionForKey(key) {
|
||||
return this.mapKeyToAction[key];
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.stopListening();
|
||||
}
|
||||
|
@ -42,6 +43,12 @@ export default class InputNormalizer extends decorate(Class) {
|
|||
if (this.target) {
|
||||
return;
|
||||
}
|
||||
if ('client' === process.env.SIDE) {
|
||||
Gamepads.start();
|
||||
Gamepads.addEventListener('connect', this.onGamepadConnect);
|
||||
Gamepads.addEventListener('disconnect', this.onGamepadDisconnect);
|
||||
this.Gamepads = Gamepads;
|
||||
}
|
||||
this.target = target;
|
||||
this.targetForKeyUp = targetForKeyUp;
|
||||
this.target.addEventListener('blur', this.onBlur);
|
||||
|
@ -65,6 +72,28 @@ export default class InputNormalizer extends decorate(Class) {
|
|||
this.setAllKeysUp();
|
||||
}
|
||||
|
||||
onButtonPress({index}) {
|
||||
this.emit('buttonPress', index);
|
||||
}
|
||||
|
||||
onButtonRelease({index}) {
|
||||
this.emit('buttonRelease', index);
|
||||
}
|
||||
|
||||
onGamepadDisconnect({gamepad}) {
|
||||
gamepad.removeEventListener('buttonpress', this.onButtonPress);
|
||||
gamepad.removeEventListener('buttonrelease', this.onButtonRelease);
|
||||
}
|
||||
|
||||
onGamepadConnect({gamepad}) {
|
||||
gamepad.addEventListener('buttonpress', this.onButtonPress);
|
||||
gamepad.addEventListener('buttonrelease', this.onButtonRelease);
|
||||
}
|
||||
|
||||
// onJoystickMove() {
|
||||
|
||||
// }
|
||||
|
||||
onKeyDown(event) {
|
||||
const {key} = event;
|
||||
if (this.keysDown[key]) {
|
||||
|
@ -137,6 +166,12 @@ export default class InputNormalizer extends decorate(Class) {
|
|||
if (!this.target) {
|
||||
return;
|
||||
}
|
||||
const {Gamepads} = this.constructor;
|
||||
if (Gamepads) {
|
||||
Gamepads.removeEventListener('connect', this.onGamepadConnect);
|
||||
Gamepads.removeEventListener('disconnect', this.onGamepadDisconnect);
|
||||
Gamepads.stop();
|
||||
}
|
||||
this.target.removeEventListener('blur', this.onBlur);
|
||||
this.target.removeEventListener('keydown', this.onKeyDown);
|
||||
this.targetForKeyUp.removeEventListener('keyup', this.onKeyUp);
|
|
@ -3,7 +3,6 @@ import D from 'debug';
|
|||
import ActionRegistry from './action-registry';
|
||||
import InputPacket from './packets/input';
|
||||
|
||||
export {default as InputNormalizer} from './normalizer';
|
||||
export {ActionRegistry, InputPacket};
|
||||
|
||||
const debug = D('@avocado/input');
|
||||
|
|
Loading…
Reference in New Issue
Block a user