Refactoring of subsystems and eventbus
This commit is contained in:
@@ -2,10 +2,10 @@ package mightypork.rogue.input;
|
||||
|
||||
|
||||
import mightypork.rogue.AppAccess;
|
||||
import mightypork.rogue.AppSubsystem;
|
||||
import mightypork.rogue.input.events.KeyboardEvent;
|
||||
import mightypork.rogue.input.events.MouseButtonEvent;
|
||||
import mightypork.rogue.input.events.MouseMotionEvent;
|
||||
import mightypork.rogue.bus.DelegatingBusClient;
|
||||
import mightypork.rogue.bus.events.KeyboardEvent;
|
||||
import mightypork.rogue.bus.events.MouseButtonEvent;
|
||||
import mightypork.rogue.bus.events.MouseMotionEvent;
|
||||
import mightypork.utils.math.coord.Coord;
|
||||
|
||||
import org.lwjgl.LWJGLException;
|
||||
@@ -14,7 +14,7 @@ import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
|
||||
|
||||
public class InputSystem extends AppSubsystem implements KeyBinder {
|
||||
public class InputSystem extends DelegatingBusClient implements KeyBinder {
|
||||
|
||||
// listeners
|
||||
private KeyBindingPool keybindings;
|
||||
@@ -60,9 +60,9 @@ public class InputSystem extends AppSubsystem implements KeyBinder {
|
||||
|
||||
private void initChannels()
|
||||
{
|
||||
msgbus().createChannel(KeyboardEvent.class, KeyboardEvent.Listener.class);
|
||||
msgbus().createChannel(MouseMotionEvent.class, MouseMotionEvent.Listener.class);
|
||||
msgbus().createChannel(MouseButtonEvent.class, MouseButtonEvent.Listener.class);
|
||||
bus().createChannel(KeyboardEvent.class, KeyboardEvent.Listener.class);
|
||||
bus().createChannel(MouseMotionEvent.class, MouseMotionEvent.Listener.class);
|
||||
bus().createChannel(MouseButtonEvent.class, MouseButtonEvent.Listener.class);
|
||||
}
|
||||
|
||||
|
||||
@@ -80,32 +80,10 @@ public class InputSystem extends AppSubsystem implements KeyBinder {
|
||||
}
|
||||
|
||||
|
||||
private void onMouseEvent()
|
||||
{
|
||||
int button = Mouse.getEventButton();
|
||||
boolean down = Mouse.getEventButtonState();
|
||||
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) msgbus().broadcast(new MouseButtonEvent(pos, button, down, wheeld));
|
||||
if (!move.isZero()) msgbus().broadcast(new MouseMotionEvent(pos, move));
|
||||
}
|
||||
|
||||
|
||||
private void onKeyEvent()
|
||||
{
|
||||
int key = Keyboard.getEventKey();
|
||||
boolean down = Keyboard.getEventKeyState();
|
||||
char c = Keyboard.getEventCharacter();
|
||||
msgbus().broadcast(new KeyboardEvent(key, c, down));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update(double delta)
|
||||
{
|
||||
Display.processMessages(); // redundant if Display.update() is called in main loop
|
||||
Display.processMessages();
|
||||
|
||||
while (Mouse.next()) {
|
||||
onMouseEvent();
|
||||
@@ -115,4 +93,26 @@ public class InputSystem extends AppSubsystem implements KeyBinder {
|
||||
onKeyEvent();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void onMouseEvent()
|
||||
{
|
||||
int button = Mouse.getEventButton();
|
||||
boolean down = Mouse.getEventButtonState();
|
||||
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();
|
||||
boolean down = Keyboard.getEventKeyState();
|
||||
char c = Keyboard.getEventCharacter();
|
||||
bus().broadcast(new KeyboardEvent(key, c, down));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mightypork.rogue.input;
|
||||
|
||||
|
||||
import mightypork.rogue.input.events.KeyboardEvent;
|
||||
import mightypork.rogue.bus.events.KeyboardEvent;
|
||||
|
||||
|
||||
public class KeyBinding implements KeyboardEvent.Listener {
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import mightypork.rogue.input.events.KeyboardEvent;
|
||||
import mightypork.rogue.bus.events.KeyboardEvent;
|
||||
import mightypork.utils.logging.Log;
|
||||
|
||||
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
package mightypork.rogue.input.events;
|
||||
|
||||
|
||||
import mightypork.utils.patterns.subscription.Handleable;
|
||||
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
|
||||
/**
|
||||
* A keyboard event
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class KeyboardEvent implements Handleable<KeyboardEvent.Listener> {
|
||||
|
||||
private int key;
|
||||
private boolean down;
|
||||
private char c;
|
||||
|
||||
|
||||
public KeyboardEvent(int key, char c, boolean down) {
|
||||
this.key = key;
|
||||
this.c = c;
|
||||
this.down = down;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return key code (see {@link org.lwjgl.input.Keyboard})
|
||||
*/
|
||||
public int getKey()
|
||||
{
|
||||
return key;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return true if key was just pressed
|
||||
*/
|
||||
public boolean isDown()
|
||||
{
|
||||
return down;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return true if key was just released
|
||||
*/
|
||||
public boolean isUp()
|
||||
{
|
||||
return !down;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return event character (if any)
|
||||
*/
|
||||
public char getChar()
|
||||
{
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void handleBy(Listener keh)
|
||||
{
|
||||
keh.receive(this);
|
||||
}
|
||||
|
||||
public interface Listener {
|
||||
|
||||
/**
|
||||
* Handle an event
|
||||
*
|
||||
* @param event event
|
||||
*/
|
||||
public void receive(KeyboardEvent event);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return Keyboard.getKeyName(key) + ":" + (down ? "DOWN" : "UP");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
package mightypork.rogue.input.events;
|
||||
|
||||
|
||||
import mightypork.utils.math.coord.Coord;
|
||||
import mightypork.utils.patterns.subscription.Handleable;
|
||||
|
||||
|
||||
/**
|
||||
* Mouse button / wheel event
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class MouseButtonEvent implements Handleable<MouseButtonEvent.Listener> {
|
||||
|
||||
public static final int BUTTON_LEFT = 0;
|
||||
public static final int BUTTON_MIDDLE = 1;
|
||||
public static final int BUTTON_RIGHT = 2;
|
||||
|
||||
private int button;
|
||||
private int wheeld;
|
||||
private Coord pos;
|
||||
private boolean down;
|
||||
|
||||
|
||||
/**
|
||||
* Mouse button event
|
||||
*
|
||||
* @param pos event position
|
||||
* @param button button id
|
||||
* @param down button pressed
|
||||
* @param wheeld wheel change
|
||||
*/
|
||||
public MouseButtonEvent(Coord pos, int button, boolean down, int wheeld) {
|
||||
this.button = button;
|
||||
this.down = down;
|
||||
this.pos = pos;
|
||||
this.wheeld = wheeld;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return true if the event was caused by a button state change
|
||||
*/
|
||||
public boolean isButtonEvent()
|
||||
{
|
||||
return button != -1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return true if the event was caused by a wheel change
|
||||
*/
|
||||
public boolean isWheelEvent()
|
||||
{
|
||||
return wheeld != 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return button id or -1 if none was pressed
|
||||
*/
|
||||
public int getButton()
|
||||
{
|
||||
return button;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return number of steps the wheel changed since last event
|
||||
*/
|
||||
public int getWheelDelta()
|
||||
{
|
||||
return wheeld;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mouse position when the event occurred
|
||||
*/
|
||||
public Coord getPos()
|
||||
{
|
||||
return pos;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return true if button was just pressed
|
||||
*/
|
||||
public boolean isDown()
|
||||
{
|
||||
return button != -1 && down;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return true if button was just released
|
||||
*/
|
||||
public boolean isUp()
|
||||
{
|
||||
return button != -1 && !down;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void handleBy(Listener handler)
|
||||
{
|
||||
handler.receive(this);
|
||||
}
|
||||
|
||||
public interface Listener {
|
||||
|
||||
/**
|
||||
* Handle an event
|
||||
*
|
||||
* @param event event
|
||||
*/
|
||||
public void receive(MouseButtonEvent event);
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package mightypork.rogue.input.events;
|
||||
|
||||
|
||||
import mightypork.utils.math.coord.Coord;
|
||||
import mightypork.utils.patterns.subscription.Handleable;
|
||||
|
||||
|
||||
public class MouseMotionEvent implements Handleable<MouseMotionEvent.Listener> {
|
||||
|
||||
private Coord move;
|
||||
private Coord pos;
|
||||
|
||||
|
||||
public MouseMotionEvent(Coord pos, Coord move) {
|
||||
this.move = move;
|
||||
this.pos = pos;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return movement since last {@link MouseMotionEvent}
|
||||
*/
|
||||
public Coord getPosDelta()
|
||||
{
|
||||
return move;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return current mouse position
|
||||
*/
|
||||
public Coord getPos()
|
||||
{
|
||||
return pos;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void handleBy(Listener keh)
|
||||
{
|
||||
keh.receive(this);
|
||||
}
|
||||
|
||||
public interface Listener {
|
||||
|
||||
/**
|
||||
* Handle an event
|
||||
*
|
||||
* @param event event
|
||||
*/
|
||||
public void receive(MouseMotionEvent event);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user