Nice, looks good!
What confuses me a little bit is that both the input
and modifier
attributes are optional:
public function addKeyboardInput(?input: String, ?modifier: String) {
This allows you to call the function without any parameters which seems a bit weird to me. I would propose this (you’re right that multiple modifiers should be allowed):
public function addKeyboardInput(input: String, ?modifiers: Array<String>) {
Also, modifiers and individual inputs should be bound together, right now (didn’t test it) it seems that if any modifier of one of the “input configurations/entries” isn’t pressed the entire action isn’t executed?
So what you should do is to use a common data structure for those input configurations (or how you call them). Maybe there could be a very small class holding this data (or a typedef which would be slower on static targets), let’s call it KeyboardActionEntry for example:
class KeyboardActionEntry {
// (default, null) -> make read-only
public var key(default, null): String;
public var modifiers(default, null): Array<String>;
// This is later called in addKeyboardInput(), which creates an empty
// modifier array if no modifiers are given. This later allows you to
// iterate through all modifiers without checking if the array is null.
public function new(key: String, modifiers: Array<String>) {
this.key = key;
this.modifiers = modifiers;
}
}
There might be better ways than using a class, this is what came into my mind first.
In the InputAction
class:
public function addKeyboardInput(input: String, ?modifiers: Array<String>) {
// Create an empty modifier array if not passed
var mods = modifiers == null ? new Array<String>() : modifiers;
// keyboardInputs now needs to be declared as Array<KeyboardActionEntry>
keyboardInputs.push(new KeyboardActionEntry(input, modifiers);
}
Later you then iterate through all entries and check if one of them is actually started/down/released.
Again, just an idea
Depending on whether individual entries/configurations should be edited later it could make more sense to use haxe.ds.Vector
instead of arrays for the modifiers, vectors have a fixed size and are at least equally fast as arrays and often faster (depends on the target).
Just play around a bit with all those options, probably you come to better conclusions/solutions.