Package megamek.client.ui.util
Class MegaMekController
java.lang.Object
megamek.client.ui.util.MegaMekController
- All Implemented Interfaces:
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
This is an interface for a parameter-less method without a return value. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Map<String,
ArrayList<CommandAction>> Maps command strings to CommandAction objects.protected boolean
Should we ignore key presses?protected Set<KeyCommandBind>
Maps a key code to a command string.protected Timer
Timer for repeating commands for key presses.protected Map<KeyCommandBind,
TimerTask> Keeps track of the tasks that are currently repeating. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
dispatchKeyEvent
(KeyEvent evt) void
registerCommandAction
(String cmd, CommandAction action) Registers an action to a keybind given as the cmd parameter (e.g.void
registerCommandAction
(KeyCommandBind commandBind, Supplier<Boolean> shouldPerform, MegaMekController.KeyBindAction performer, MegaMekController.KeyBindAction releaseAction) Registers an action to a keybind, e.g.void
registerCommandAction
(KeyCommandBind commandBind, KeyBindReceiver receiver, MegaMekController.KeyBindAction performer) Registers an action to a keybind, e.g.void
void
void
void
setIgnoreKeyPresses
(boolean ignoreKeyPresses) Set whether key presses should be ignored or not.protected void
startRepeating
(KeyCommandBind kcb, CommandAction action) Start a new repeating timer task for the given KeyCommandBind.void
Stop all repeat timers.void
Stops the repeat timer task for the given KeyCommandBind.
-
Field Details
-
boardEditor
-
clientgui
-
keyCmdSet
Maps a key code to a command string. -
cmdActionMap
Maps command strings to CommandAction objects. -
keyRepeatTimer
Timer for repeating commands for key presses. This is necessary to override the default key repeat delay. -
repeatingTasks
Keeps track of the tasks that are currently repeating. -
ignoreKeyPresses
protected boolean ignoreKeyPressesShould we ignore key presses?
-
-
Constructor Details
-
MegaMekController
public MegaMekController()
-
-
Method Details
-
dispatchKeyEvent
- Specified by:
dispatchKeyEvent
in interfaceKeyEventDispatcher
-
registerKeyCommandBind
-
removeAllKeyCommandBinds
public void removeAllKeyCommandBinds() -
registerCommandAction
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 KeyCommandBindaction
- 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 KeyCommandBindreceiver
- TheKeyBindReceiver
that receives this keypressperformer
- 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 KeyCommandBindshouldPerform
- A method that should return true when the performer is allowed to take actionperformer
- A method that takes action upon the keypressreleaseAction
- A method that takes action when the key is released again
-
removeAllActions
public void removeAllActions() -
startRepeating
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
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.
-