Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@SJuliez
Copy link
Member

@SJuliez SJuliez commented Apr 9, 2024

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:

        controller.registerCommandAction(KeyCommandBind.FIRE.cmd,
                new CommandAction() {

                    @Override
                    public boolean shouldPerformAction() {
                        if (!clientgui.getClient().isMyTurn()
                                || clientgui.getBoardView().getChatterBoxActive()
                                || !display.isVisible()
                                || display.isIgnoringEvents()
                                || !buttons.get(FiringCommand.FIRE_FIRE).isEnabled()) {
                            return false;
                        } else {
                            return true;
                        }
                    }

                    @Override
                    public void performAction() {
                        fire();
                    }
                });

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.

@HammerGS HammerGS added the For New Dev Cycle This PR should be merged at the beginning of a dev cycle label Apr 10, 2024
SJuliez added 3 commits April 20, 2024 10:48
# Conflicts:
#	megamek/src/megamek/client/ui/swing/FiringDisplay.java
#	megamek/src/megamek/client/ui/swing/MovementDisplay.java
#	megamek/src/megamek/client/ui/swing/SelectArtyAutoHitHexDisplay.java
#	megamek/src/megamek/client/ui/swing/boardview/BoardView.java
@SJuliez SJuliez merged commit e182366 into MegaMek:master Apr 20, 2024
@SJuliez SJuliez deleted the keydispatcher-simpler branch May 1, 2024 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For New Dev Cycle This PR should be merged at the beginning of a dev cycle

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants