refactor: input
This commit is contained in:
parent
be607b25a3
commit
9d5910ebb8
|
@ -21,7 +21,8 @@
|
|||
"@latus/core": "2.0.0",
|
||||
"@latus/socket": "2.0.0",
|
||||
"autoprefixer": "^9.8.6",
|
||||
"debug": "4.3.1"
|
||||
"debug": "4.3.1",
|
||||
"gamepads": "^1.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@neutrinojs/airbnb": "^9.4.0",
|
||||
|
|
|
@ -6,36 +6,38 @@ const decorate = compose(
|
|||
|
||||
export default class ActionRegistry extends decorate(Class) {
|
||||
|
||||
constructor() {
|
||||
#listen = {
|
||||
axis: {},
|
||||
key: {},
|
||||
button: {},
|
||||
};
|
||||
|
||||
#stream = [];
|
||||
|
||||
#transformers = {};
|
||||
|
||||
constructor(actions) {
|
||||
super();
|
||||
this.actions = {};
|
||||
this.actionKeys = new Map();
|
||||
this.normalizer = undefined;
|
||||
this._actionIds = {};
|
||||
this._stream = [];
|
||||
this._actionTransformers = {};
|
||||
const entries = Object.entries(this.constructor.keyMap);
|
||||
const entries = Object.entries(actions);
|
||||
for (let i = 0; i < entries.length; i++) {
|
||||
const [action, key] = entries[i];
|
||||
this.actions[key] = action;
|
||||
this.actionKeys[action] = key;
|
||||
const [action, candidates] = entries[i];
|
||||
for (let j = 0; j < candidates.length; ++j) {
|
||||
const {index, type} = candidates[j];
|
||||
this.#listen[type][index] = action;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
actionForKey(key) {
|
||||
return this.actions[key];
|
||||
}
|
||||
|
||||
drain() {
|
||||
const stream = this._stream;
|
||||
this._stream = [];
|
||||
const stream = this.#stream;
|
||||
this.#stream = [];
|
||||
return stream;
|
||||
}
|
||||
|
||||
listen(normalizer) {
|
||||
// Only listen once.
|
||||
if (this.normalizer) {
|
||||
return;
|
||||
this.stopListening();
|
||||
}
|
||||
this.normalizer = normalizer;
|
||||
this.normalizer.on('keyDown', this.onKeyDown, this);
|
||||
|
@ -44,56 +46,48 @@ export default class ActionRegistry extends decorate(Class) {
|
|||
this.normalizer.on('wheelUp', this.onWheelUp, this);
|
||||
}
|
||||
|
||||
keyForAction(action) {
|
||||
return this.actionKeys[action];
|
||||
onButtonPress(button) {
|
||||
this.onInput('button', button, 1, 'buttonPress');
|
||||
}
|
||||
|
||||
static setKeyMap(keyMap) {
|
||||
this.keyMap = keyMap;
|
||||
onButtonRelease(button) {
|
||||
this.onInput('button', button, 0, 'buttonRelease');
|
||||
}
|
||||
|
||||
onInput(type, input, value, transforming) {
|
||||
const action = this.#listen[type][input] || {};
|
||||
if (action) {
|
||||
this.#stream.push({
|
||||
action,
|
||||
value: this.transform(action, value, transforming),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
onKeyDown(key) {
|
||||
if (this.actions[key]) {
|
||||
const action = this.actions[key];
|
||||
const value = this._actionTransformers[action]
|
||||
? this._actionTransformers[action]('keyDown')
|
||||
: 1;
|
||||
this._stream.push({action, value});
|
||||
}
|
||||
this.onInput('key', key, 1, 'keyDown');
|
||||
}
|
||||
|
||||
onKeyUp(key) {
|
||||
if (this.actions[key]) {
|
||||
const action = this.actions[key];
|
||||
const value = this._actionTransformers[action]
|
||||
? this._actionTransformers[action]('keyUp')
|
||||
: 0;
|
||||
this._stream.push({action, value});
|
||||
}
|
||||
this.onInput('key', key, 0, 'keyUp');
|
||||
}
|
||||
|
||||
onWheelDown(delta) {
|
||||
if (this.actions.WheelDown) {
|
||||
const action = this.actions.WheelDown;
|
||||
const value = this._actionTransformers[action]
|
||||
? this._actionTransformers[action]('wheel', delta)
|
||||
: delta;
|
||||
this._stream.push({action, value});
|
||||
}
|
||||
this.#stream.push({
|
||||
action: 'WheelDown',
|
||||
value: this.transform('WheelDown', delta, 'wheel'),
|
||||
});
|
||||
}
|
||||
|
||||
onWheelUp(delta) {
|
||||
if (this.actions.WheelUp) {
|
||||
const action = this.actions.WheelUp;
|
||||
const value = this._actionTransformers[action]
|
||||
? this._actionTransformers[action]('wheel', delta)
|
||||
: delta;
|
||||
this._stream.push({action, value});
|
||||
}
|
||||
this.#stream.push({
|
||||
action: 'WheelUp',
|
||||
value: this.transform('WheelUp', delta, 'wheel'),
|
||||
});
|
||||
}
|
||||
|
||||
setActionTransformerFor(action, transformer) {
|
||||
this._actionTransformers[action] = transformer;
|
||||
setTransformerFor(action, transformer) {
|
||||
this.#transformers[action] = transformer;
|
||||
}
|
||||
|
||||
stopListening() {
|
||||
|
@ -107,4 +101,8 @@ export default class ActionRegistry extends decorate(Class) {
|
|||
this.normalizer = undefined;
|
||||
}
|
||||
|
||||
transform(action, value, transforming) {
|
||||
return this.#transformers[action] ? this.#transformers[action](transforming, value) : value;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,14 +11,13 @@ const debug = D('@avocado/input');
|
|||
export default {
|
||||
hooks: {
|
||||
'@latus/core/config': () => ({
|
||||
keyMap: {},
|
||||
actions: [],
|
||||
}),
|
||||
'@latus/core/starting': (latus) => {
|
||||
const {keyMap} = latus.get('@avocado/input');
|
||||
const ids = Object.fromEntries(Object.keys(keyMap).map((action, i) => [action, i]));
|
||||
debug('got key map: %O, generated IDs: %O', keyMap, ids);
|
||||
const {actions} = latus.get('@avocado/input');
|
||||
const ids = Object.fromEntries(actions.map((action, i) => [action, i]));
|
||||
debug('action id map: %O', ids);
|
||||
InputPacket.setActionIds(ids);
|
||||
ActionRegistry.setKeyMap(keyMap);
|
||||
},
|
||||
'@latus/socket/packets': () => ({
|
||||
Input: InputPacket,
|
||||
|
|
|
@ -3929,6 +3929,11 @@ functional-red-black-tree@^1.0.1:
|
|||
resolved "http://npm.cha0sdev/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
|
||||
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=
|
||||
|
||||
gamepads@^1.2.2:
|
||||
version "1.2.2"
|
||||
resolved "http://npm.cha0sdev/gamepads/-/gamepads-1.2.2.tgz#6bfbbbded499f66db94c1fe1c0038a28ba1d2733"
|
||||
integrity sha512-m0DihXzHh1+bLoex4Bz0tX3IaJ1OyIKaw/71Cj2lFRsTESMJpjLvmc+BluU3QE0UsMjMFiIK2j7c1Q08O/DYfg==
|
||||
|
||||
gauge@~2.7.3:
|
||||
version "2.7.4"
|
||||
resolved "http://npm.cha0sdev/gauge/-/gauge-2.7.4.tgz#2c03405c7538c39d7eb37b317022e325fb018bf7"
|
||||
|
|
Loading…
Reference in New Issue
Block a user