Rogue: Savage Rats, a retro-themed dungeon crawler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
rogue-savage-rats/src/mightypork/gamecore/input/KeyBindingPool.java

70 lines
1.3 KiB

package mightypork.gamecore.input;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import mightypork.gamecore.control.bus.events.KeyEvent;
import mightypork.utils.logging.Log;
/**
* Key binding pool
*
* @author MightyPork
*/
public class KeyBindingPool implements KeyBinder, KeyEvent.Listener {
private final Set<KeyBinding> bindings = new HashSet<>();
/**
* Bind handler to a keystroke, replace current handler if any
*
* @param stroke trigger keystroke
* @param task handler
*/
@Override
public void bindKeyStroke(KeyStroke stroke, Runnable task)
{
for (final KeyBinding kb : bindings) {
if (kb.matches(stroke)) {
Log.w("Duplicate KeyBinding (" + stroke + "), using newest handler.");
kb.setHandler(task);
return;
}
}
bindings.add(new KeyBinding(stroke, task));
}
/**
* Remove handler from keystroke (id any)
*
* @param stroke stroke
*/
@Override
public void unbindKeyStroke(KeyStroke stroke)
{
final Iterator<KeyBinding> iter = bindings.iterator();
while (iter.hasNext()) {
final KeyBinding kb = iter.next();
if (kb.matches(stroke)) {
iter.remove();
return;
}
}
}
@Override
public void receive(KeyEvent event)
{
for (final KeyBinding kb : bindings) {
kb.receive(event);
}
}
}