Class MegaMekController

java.lang.Object
megamek.client.ui.util.MegaMekController
All Implemented Interfaces:
KeyEventDispatcher

public class MegaMekController extends Object implements KeyEventDispatcher
This class implements a KeyEventDispatcher, which handles all generated KeyEvents. If the KeyEvent correspondes to a registerd hotkey, the action for that hotkey will be used to consume the event otherwise the event will be dispatched as normal. The idea is that the system is split into two: keys can be bound to string commands, and string commands can be bound to CommandAction objects, which are a simple class that implements an "actionPerformed" method. The class that implements the CommandAction creates the object and registers it, agnostic to what key is bound to the command. Then, somewhere else (ie; a file) can specify what keys are bound to what string commands. The possible string commands are specified in KeyCommandBind. There are three things that need to be done to create a key binding. First, a command must exist, defined in KeyCommandBind. Then, the command must be bound to a key in the keybind XML file (mmconf/defaultKeyBinds.xml by default). Finally, a CommandAction needs to be registered somewhere.
  • Field Details

    • boardEditor

      public BoardEditorPanel boardEditor
    • clientgui

      public IClientGUI clientgui
    • keyCmdSet

      protected Set<KeyCommandBind> keyCmdSet
      Maps a key code to a command string.
    • cmdActionMap

      protected Map<String,ArrayList<CommandAction>> cmdActionMap
      Maps command strings to CommandAction objects.
    • keyRepeatTimer

      protected Timer keyRepeatTimer
      Timer for repeating commands for key presses. This is necessary to override the default key repeat delay.
    • repeatingTasks

      protected Map<KeyCommandBind,TimerTask> repeatingTasks
      Keeps track of the tasks that are currently repeating.
    • ignoreKeyPresses

      protected boolean ignoreKeyPresses
      Should we ignore key presses?
  • Constructor Details

    • MegaMekController

      public MegaMekController()
  • Method Details

    • dispatchKeyEvent

      public boolean dispatchKeyEvent(KeyEvent evt)
      Specified by:
      dispatchKeyEvent in interface KeyEventDispatcher
    • registerKeyCommandBind

      public void registerKeyCommandBind(KeyCommandBind kcb)
    • removeAllKeyCommandBinds

      public void removeAllKeyCommandBinds()
    • registerCommandAction

      public void registerCommandAction(String cmd, CommandAction action)
      Registers an action to a keybind given as the cmd parameter (e.g. KeyCommandBind.SCROLL_NORTH.cmd). For every press of the bound key, the action will be called.
      Parameters:
      cmd - The keycommand string, obtained through KeyCommandBind
      action - The CommandAction
    • registerCommandAction

      public void registerCommandAction(KeyCommandBind commandBind, KeyBindReceiver receiver, MegaMekController.KeyBindAction performer)
      Registers an action to a keybind, e.g. KeyCommandBind.SCROLL_NORTH. The necessary CommandAction is constructed from the given parameters. The given performer is called when the key is pressed if the given receiver's shouldReceiveKeyCommands() method check returns true. Note that in this case, the keybind is considered consumed even if this receiver doesn't do anything with it. For a keybind to be passed on to other receivers, this receiver's shouldPerformKeyCommands() must return false.
      Parameters:
      commandBind - The KeyCommandBind
      receiver - The KeyBindReceiver that receives this keypress
      performer - A method that takes action upon the keypress
      See Also:
    • registerCommandAction

      public void registerCommandAction(KeyCommandBind commandBind, Supplier<Boolean> shouldPerform, MegaMekController.KeyBindAction performer, MegaMekController.KeyBindAction releaseAction)
      Registers an action to a keybind, e.g. KeyCommandBind.SCROLL_NORTH. The necessary CommandAction is constructed from the given method references. The given performer will be called when the key is pressed if the given shouldPerform check returns true. Note that in this case, the keybind is considered consumed even if this receiver doesn't do anything with it. For a keybind to be passed on to other receivers, this receiver's shouldPerform must return false. Additionally, the given releaseAction is called when the pressed key is released again (also, only when shouldPerform allows it).
      Parameters:
      commandBind - The KeyCommandBind
      shouldPerform - A method that should return true when the performer is allowed to take action
      performer - A method that takes action upon the keypress
      releaseAction - A method that takes action when the key is released again
    • removeAllActions

      public void removeAllActions()
    • startRepeating

      protected void startRepeating(KeyCommandBind kcb, CommandAction action)
      Start a new repeating timer task for the given KeyCommandBind. If the given KeyCommandBind already has a repeating task, a new one is not added. Also, if there is no mapped CommandAction for the given KeyCommandBind no task is scheduled.
    • stopRepeating

      public void stopRepeating(KeyCommandBind kcb)
      Stops the repeat timer task for the given KeyCommandBind.
    • stopAllRepeating

      public void stopAllRepeating()
      Stop all repeat timers.
    • setIgnoreKeyPresses

      public void setIgnoreKeyPresses(boolean ignoreKeyPresses)
      Set whether key presses should be ignored or not.