Formatting, classpath.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package mightypork.rogue.input;
|
||||
|
||||
|
||||
import mightypork.rogue.AppAccess;
|
||||
import mightypork.rogue.bus.DelegatingBusClient;
|
||||
import mightypork.rogue.bus.events.KeyboardEvent;
|
||||
@@ -15,37 +14,37 @@ import org.lwjgl.opengl.Display;
|
||||
|
||||
|
||||
public class InputSystem extends DelegatingBusClient implements KeyBinder {
|
||||
|
||||
|
||||
// listeners
|
||||
private KeyBindingPool keybindings;
|
||||
|
||||
|
||||
|
||||
|
||||
public InputSystem(AppAccess app) {
|
||||
super(app, true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
protected void init()
|
||||
{
|
||||
initDevices();
|
||||
|
||||
|
||||
initChannels();
|
||||
|
||||
|
||||
// global keybindings
|
||||
keybindings = new KeyBindingPool();
|
||||
addChildSubscriber(keybindings);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void deinit()
|
||||
{
|
||||
Mouse.destroy();
|
||||
Keyboard.destroy();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void initDevices()
|
||||
{
|
||||
try {
|
||||
@@ -56,45 +55,45 @@ public class InputSystem extends DelegatingBusClient implements KeyBinder {
|
||||
throw new RuntimeException("Failed to initialize input devices.", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void initChannels()
|
||||
{
|
||||
bus().createChannel(KeyboardEvent.class, KeyboardEvent.Listener.class);
|
||||
bus().createChannel(MouseMotionEvent.class, MouseMotionEvent.Listener.class);
|
||||
bus().createChannel(MouseButtonEvent.class, MouseButtonEvent.Listener.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void bindKeyStroke(KeyStroke stroke, Runnable task)
|
||||
{
|
||||
keybindings.bindKeyStroke(stroke, task);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void unbindKeyStroke(KeyStroke stroke)
|
||||
{
|
||||
keybindings.unbindKeyStroke(stroke);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void update(double delta)
|
||||
{
|
||||
Display.processMessages();
|
||||
|
||||
|
||||
while (Mouse.next()) {
|
||||
onMouseEvent();
|
||||
}
|
||||
|
||||
|
||||
while (Keyboard.next()) {
|
||||
onKeyEvent();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void onMouseEvent()
|
||||
{
|
||||
int button = Mouse.getEventButton();
|
||||
@@ -102,12 +101,12 @@ public class InputSystem extends DelegatingBusClient implements KeyBinder {
|
||||
Coord pos = new Coord(Mouse.getEventX(), Mouse.getEventY());
|
||||
Coord move = new Coord(Mouse.getEventDX(), Mouse.getEventDY());
|
||||
int wheeld = Mouse.getEventDWheel();
|
||||
|
||||
|
||||
if (button != -1 || wheeld != 0) bus().broadcast(new MouseButtonEvent(pos, button, down, wheeld));
|
||||
if (!move.isZero()) bus().broadcast(new MouseMotionEvent(pos, move));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private void onKeyEvent()
|
||||
{
|
||||
int key = Keyboard.getEventKey();
|
||||
|
||||
@@ -1,22 +1,24 @@
|
||||
package mightypork.rogue.input;
|
||||
|
||||
|
||||
public interface KeyBinder {
|
||||
|
||||
|
||||
/**
|
||||
* Bind handler to a keystroke, replace current handler if any
|
||||
*
|
||||
* @param stroke trigger keystroke
|
||||
* @param task handler
|
||||
* @param stroke
|
||||
* trigger keystroke
|
||||
* @param task
|
||||
* handler
|
||||
*/
|
||||
abstract void bindKeyStroke(KeyStroke stroke, Runnable task);
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Remove handler from a keystroke (id any)
|
||||
*
|
||||
* @param stroke stroke
|
||||
* @param stroke
|
||||
* stroke
|
||||
*/
|
||||
abstract void unbindKeyStroke(KeyStroke stroke);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,48 +1,47 @@
|
||||
package mightypork.rogue.input;
|
||||
|
||||
|
||||
import mightypork.rogue.bus.events.KeyboardEvent;
|
||||
|
||||
|
||||
public class KeyBinding implements KeyboardEvent.Listener {
|
||||
|
||||
|
||||
private KeyStroke keystroke;
|
||||
private Runnable handler;
|
||||
private boolean wasActive = false;
|
||||
|
||||
|
||||
|
||||
|
||||
public KeyBinding(KeyStroke stroke, Runnable handler) {
|
||||
this.keystroke = stroke;
|
||||
this.handler = handler;
|
||||
|
||||
|
||||
wasActive = keystroke.isActive();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean matches(KeyStroke stroke)
|
||||
{
|
||||
return this.keystroke.equals(stroke);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setHandler(Runnable handler)
|
||||
{
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void receive(KeyboardEvent event)
|
||||
{
|
||||
// ignore unrelated events
|
||||
if (!keystroke.getKeys().contains(event.getKey())) return;
|
||||
|
||||
|
||||
// run handler when event was met
|
||||
if (keystroke.isActive() && !wasActive) {
|
||||
handler.run();
|
||||
}
|
||||
|
||||
|
||||
wasActive = keystroke.isActive();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package mightypork.rogue.input;
|
||||
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
@@ -15,15 +14,17 @@ import mightypork.utils.logging.Log;
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener {
|
||||
|
||||
|
||||
private Set<KeyBinding> bindings = new HashSet<KeyBinding>();
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Bind handler to a keystroke, replace current handler if any
|
||||
*
|
||||
* @param stroke trigger keystroke
|
||||
* @param task handler
|
||||
* @param stroke
|
||||
* trigger keystroke
|
||||
* @param task
|
||||
* handler
|
||||
*/
|
||||
@Override
|
||||
public void bindKeyStroke(KeyStroke stroke, Runnable task)
|
||||
@@ -35,21 +36,22 @@ public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bindings.add(new KeyBinding(stroke, task));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Remove handler from keystroke (id any)
|
||||
*
|
||||
* @param stroke stroke
|
||||
* @param stroke
|
||||
* stroke
|
||||
*/
|
||||
@Override
|
||||
public void unbindKeyStroke(KeyStroke stroke)
|
||||
{
|
||||
Iterator<KeyBinding> iter = bindings.iterator();
|
||||
|
||||
|
||||
while (iter.hasNext()) {
|
||||
KeyBinding kb = iter.next();
|
||||
if (kb.matches(stroke)) {
|
||||
@@ -58,8 +60,8 @@ public class KeyBindingPool implements KeyBinder, KeyboardEvent.Listener {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void receive(KeyboardEvent event)
|
||||
{
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package mightypork.rogue.input;
|
||||
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
@@ -9,16 +8,18 @@ import org.lwjgl.input.Keyboard;
|
||||
|
||||
|
||||
public class KeyStroke {
|
||||
|
||||
|
||||
private Set<Integer> keys = new LinkedHashSet<Integer>();
|
||||
private boolean down = true;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* KeyStroke
|
||||
*
|
||||
* @param down true for falling edge, up for rising edge
|
||||
* @param keys keys that must be pressed
|
||||
* @param down
|
||||
* true for falling edge, up for rising edge
|
||||
* @param keys
|
||||
* keys that must be pressed
|
||||
*/
|
||||
public KeyStroke(boolean down, int... keys) {
|
||||
this.down = down;
|
||||
@@ -26,8 +27,8 @@ public class KeyStroke {
|
||||
this.keys.add(k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Falling edge keystroke
|
||||
*
|
||||
@@ -38,25 +39,25 @@ public class KeyStroke {
|
||||
this.keys.add(k);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean isActive()
|
||||
{
|
||||
boolean st = true;
|
||||
for (int k : keys) {
|
||||
st &= Keyboard.isKeyDown(k);
|
||||
}
|
||||
|
||||
|
||||
return down ? st : !st;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void setKeys(Set<Integer> keys)
|
||||
{
|
||||
this.keys = keys;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
@@ -65,8 +66,8 @@ public class KeyStroke {
|
||||
result = prime * result + ((keys == null) ? 0 : keys.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
@@ -74,39 +75,39 @@ public class KeyStroke {
|
||||
if (obj == null) return false;
|
||||
if (!(obj instanceof KeyStroke)) return false;
|
||||
KeyStroke other = (KeyStroke) obj;
|
||||
|
||||
|
||||
if (keys == null) {
|
||||
if (other.keys != null) return false;
|
||||
} else if (!keys.equals(other.keys)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if (down != other.down) return false;
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String s = "(";
|
||||
|
||||
|
||||
int cnt = 0;
|
||||
Iterator<Integer> i = keys.iterator();
|
||||
for (; i.hasNext(); cnt++) {
|
||||
if (cnt > 0) s += "+";
|
||||
s += Keyboard.getKeyName(i.next());
|
||||
}
|
||||
|
||||
|
||||
s += down ? ",DOWN" : "UP";
|
||||
|
||||
|
||||
s += ")";
|
||||
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public Set<Integer> getKeys()
|
||||
{
|
||||
return keys;
|
||||
|
||||
Reference in New Issue
Block a user