assassin-bug/framework/input/index.js

35 lines
982 B
JavaScript

import { createInput } from './input-factory';
export class Input {
constructor(InputIDs, element) {
this.InputIDs = InputIDs;
this.element = element;
this.inputs = new Map();
this.init();
}
init() {
this.InputIDs.forEach((inputID) => {
const thing = createInput(inputID);
const instance = new thing(this.element);
this.inputs.set(inputID, instance);
});
}
addInput(id, input) {
this.inputs.set(id, input);
return this;
}
capture(preventDefault = true) {
this.inputs.forEach((input) => input.capture(preventDefault));
}
release() {
this.inputs.forEach((input) => input.release());
}
getState() {
const state = {};
this.inputs.forEach((input, inputID) => {
if (input)
state[inputID] = input.getState();
});
return state;
}
}