feat: auto keyMap

This commit is contained in:
cha0s 2021-01-19 14:41:06 -06:00
parent 45630998e1
commit 3d45055640
2 changed files with 24 additions and 9 deletions

View File

@ -14,6 +14,12 @@ export default class ActionRegistry extends decorate(Class) {
this._actionIds = {};
this._stream = [];
this._actionTransformers = {};
const entries = Object.entries(this.constructor.keyMap);
for (let i = 0; i < entries.length; i++) {
const [action, key] = entries[i];
this.keyActions[key] = action;
this.actionKeys[action] = key;
}
}
actionForKey(key) {
@ -40,13 +46,8 @@ export default class ActionRegistry extends decorate(Class) {
return this.actionKeys[action];
}
mapKeysToActions(map) {
const entries = Object.entries(map);
for (let i = 0; i < entries.length; i++) {
const [action, key] = entries[i];
this.keyActions[key] = action;
this.actionKeys[action] = key;
}
static setKeyMap(keyMap) {
this.keyMap = keyMap;
}
onKeyDown(key) {

View File

@ -1,11 +1,25 @@
import D from 'debug';
import ActionRegistry from './action-registry';
import InputPacket from './packets/input';
export {default as ActionRegistry} from './action-registry';
export {default as InputNormalizer} from './normalizer';
export {InputPacket};
export {ActionRegistry, InputPacket};
const debug = D('@avocado/input');
export default {
hooks: {
'@latus/core/config': () => ({
keyMap: {},
}),
'@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);
InputPacket.setActionIds(ids);
ActionRegistry.setKeyMap(keyMap);
},
'@latus/socket/packets': () => ({
Input: InputPacket,
}),