Keydispatcher aka MegaMekController simplification #5336
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to reduce the very cluttersome registration of keybinds in the MegamekController. It does this by adding a few more registration methods, mainly one that requires the KeyBind, a KeyBindReceiver (an interface with a single method, shouldReceiveKeyCommands) and a performer method that acts on a keybind.
The thought is that the shouldReceiveKeyCommands() method is often very repetitive and can even be inherited (as is done for most of the StatusBarPhaseDisplays now). The performer method is often already present and just has to be named as a method reference, or it can be created.
This transforms this call:
into this:
controller.registerCommandAction(KeyCommandBind.FIRE, this::shouldPerformFireKeyCommand, this::fire);This line is not immediately clear all by itself. I believe I've given it ample javadoc to be read upon hovering it.
This PR also changes the CommandAction class to interface and removes the hasReleaseAction() method. Instead, the release action is always called on a keybind key release, it just does nothing by default. I had no intention of making any noticeable changes to the keybinds and I have not tried to simplify the code in some of the performers although one probably could.