parent
0860836f85
commit
d67b382f43
@ -1,32 +0,0 @@ |
||||
package mightypork.gamecore.control.events; |
||||
|
||||
|
||||
import mightypork.util.control.eventbus.events.Event; |
||||
import mightypork.util.control.eventbus.events.flags.ImmediateEvent; |
||||
|
||||
|
||||
/** |
||||
* Intended use is to notify UI component sub-clients that they should poll |
||||
* their cached constraints. |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
@ImmediateEvent |
||||
public class LayoutChangeEvent implements Event<LayoutChangeEvent.Listener> { |
||||
|
||||
public LayoutChangeEvent() |
||||
{ |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(Listener handler) |
||||
{ |
||||
handler.onLayoutChanged(); |
||||
} |
||||
|
||||
public interface Listener { |
||||
|
||||
public void onLayoutChanged(); |
||||
} |
||||
} |
@ -1,48 +0,0 @@ |
||||
package mightypork.gamecore.control.events; |
||||
|
||||
|
||||
import mightypork.util.control.eventbus.events.Event; |
||||
import mightypork.util.control.eventbus.events.flags.SingleReceiverEvent; |
||||
|
||||
|
||||
/** |
||||
* Request to execute given {@link Runnable} in main loop. |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
@SingleReceiverEvent |
||||
public class MainLoopTaskRequest implements Event<MainLoopTaskRequest.Listener> { |
||||
|
||||
private final Runnable task; |
||||
|
||||
|
||||
/** |
||||
* @param task task to run on main thread in rendering context |
||||
*/ |
||||
public MainLoopTaskRequest(Runnable task) |
||||
{ |
||||
this.task = task; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(Listener handler) |
||||
{ |
||||
handler.queueTask(task); |
||||
} |
||||
|
||||
/** |
||||
* {@link MainLoopTaskRequest} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface Listener { |
||||
|
||||
/** |
||||
* Perform the requested action |
||||
* |
||||
* @param request |
||||
*/ |
||||
void queueTask(Runnable request); |
||||
} |
||||
} |
@ -1,49 +0,0 @@ |
||||
package mightypork.gamecore.control.events; |
||||
|
||||
|
||||
import mightypork.gamecore.loading.Deferred; |
||||
import mightypork.util.control.eventbus.events.Event; |
||||
import mightypork.util.control.eventbus.events.flags.SingleReceiverEvent; |
||||
|
||||
|
||||
/** |
||||
* Request to load a deferred resource. |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
@SingleReceiverEvent |
||||
public class ResourceLoadRequest implements Event<ResourceLoadRequest.Listener> { |
||||
|
||||
private final Deferred resource; |
||||
|
||||
|
||||
/** |
||||
* @param resource resource to load |
||||
*/ |
||||
public ResourceLoadRequest(Deferred resource) |
||||
{ |
||||
this.resource = resource; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(Listener handler) |
||||
{ |
||||
handler.loadResource(resource); |
||||
} |
||||
|
||||
/** |
||||
* {@link ResourceLoadRequest} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface Listener { |
||||
|
||||
/** |
||||
* Load a resource |
||||
* |
||||
* @param resource |
||||
*/ |
||||
void loadResource(Deferred resource); |
||||
} |
||||
} |
@ -1,47 +0,0 @@ |
||||
package mightypork.gamecore.control.events; |
||||
|
||||
|
||||
import mightypork.util.control.eventbus.events.Event; |
||||
import mightypork.util.control.eventbus.events.flags.SingleReceiverEvent; |
||||
|
||||
|
||||
/** |
||||
* Request to change screen |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
@SingleReceiverEvent |
||||
public class ScreenRequestEvent implements Event<ScreenRequestEvent.Listener> { |
||||
|
||||
private final String scrName; |
||||
|
||||
|
||||
/** |
||||
* @param screenKey screen name |
||||
*/ |
||||
public ScreenRequestEvent(String screenKey) |
||||
{ |
||||
scrName = screenKey; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(Listener handler) |
||||
{ |
||||
handler.showScreen(scrName); |
||||
} |
||||
|
||||
/** |
||||
* {@link ScreenRequestEvent} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface Listener { |
||||
|
||||
/** |
||||
* @param key screen to show |
||||
*/ |
||||
void showScreen(String key); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,28 @@ |
||||
package mightypork.gamecore.control.events.gui; |
||||
|
||||
|
||||
import mightypork.util.control.eventbus.BusEvent; |
||||
import mightypork.util.control.eventbus.events.flags.ImmediateEvent; |
||||
import mightypork.util.control.eventbus.events.flags.NonConsumableEvent; |
||||
|
||||
|
||||
/** |
||||
* Intended use is to notify UI component sub-clients that they should poll |
||||
* their cached constraints. |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
@ImmediateEvent |
||||
@NonConsumableEvent |
||||
public class LayoutChangeEvent extends BusEvent<LayoutChangeListener> { |
||||
|
||||
public LayoutChangeEvent() { |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(LayoutChangeListener handler) |
||||
{ |
||||
handler.onLayoutChanged(); |
||||
} |
||||
} |
@ -0,0 +1,6 @@ |
||||
package mightypork.gamecore.control.events.gui; |
||||
|
||||
public interface LayoutChangeListener { |
||||
|
||||
public void onLayoutChanged(); |
||||
} |
@ -0,0 +1,16 @@ |
||||
package mightypork.gamecore.control.events.gui; |
||||
|
||||
/** |
||||
* {@link ViewportChangeEvent} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface ViewportChangeListener { |
||||
|
||||
/** |
||||
* Handle event |
||||
* |
||||
* @param event |
||||
*/ |
||||
void onViewportChanged(ViewportChangeEvent event); |
||||
} |
@ -0,0 +1,16 @@ |
||||
package mightypork.gamecore.control.events.input; |
||||
|
||||
/** |
||||
* {@link KeyEvent} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface KeyListener { |
||||
|
||||
/** |
||||
* Handle an event |
||||
* |
||||
* @param event event |
||||
*/ |
||||
void receive(KeyEvent event); |
||||
} |
@ -0,0 +1,16 @@ |
||||
package mightypork.gamecore.control.events.input; |
||||
|
||||
/** |
||||
* {@link MouseButtonEvent} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface MouseButtonListener { |
||||
|
||||
/** |
||||
* Handle an event |
||||
* |
||||
* @param event event |
||||
*/ |
||||
void receive(MouseButtonEvent event); |
||||
} |
@ -0,0 +1,16 @@ |
||||
package mightypork.gamecore.control.events.input; |
||||
|
||||
/** |
||||
* {@link MouseMotionEvent} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface MouseMotionListener { |
||||
|
||||
/** |
||||
* Handle an event |
||||
* |
||||
* @param event event |
||||
*/ |
||||
void receive(MouseMotionEvent event); |
||||
} |
@ -0,0 +1,32 @@ |
||||
package mightypork.gamecore.control.events.requests; |
||||
|
||||
|
||||
import mightypork.util.control.eventbus.BusEvent; |
||||
import mightypork.util.control.eventbus.events.flags.SingleReceiverEvent; |
||||
|
||||
|
||||
/** |
||||
* Request to execute given {@link Runnable} in main loop. |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
@SingleReceiverEvent |
||||
public class MainLoopRequest extends BusEvent<MainLoopRequestListener> { |
||||
|
||||
private final Runnable task; |
||||
|
||||
|
||||
/** |
||||
* @param task task to run on main thread in rendering context |
||||
*/ |
||||
public MainLoopRequest(Runnable task) { |
||||
this.task = task; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(MainLoopRequestListener handler) |
||||
{ |
||||
handler.queueTask(task); |
||||
} |
||||
} |
@ -0,0 +1,16 @@ |
||||
package mightypork.gamecore.control.events.requests; |
||||
|
||||
/** |
||||
* {@link MainLoopRequest} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface MainLoopRequestListener { |
||||
|
||||
/** |
||||
* Perform the requested action |
||||
* |
||||
* @param request |
||||
*/ |
||||
void queueTask(Runnable request); |
||||
} |
@ -0,0 +1,33 @@ |
||||
package mightypork.gamecore.control.events.requests; |
||||
|
||||
|
||||
import mightypork.gamecore.loading.Deferred; |
||||
import mightypork.util.control.eventbus.BusEvent; |
||||
import mightypork.util.control.eventbus.events.flags.SingleReceiverEvent; |
||||
|
||||
|
||||
/** |
||||
* Request to load a deferred resource. |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
@SingleReceiverEvent |
||||
public class ResourceLoadRequest extends BusEvent<ResourceLoadRequestListener> { |
||||
|
||||
private final Deferred resource; |
||||
|
||||
|
||||
/** |
||||
* @param resource resource to load |
||||
*/ |
||||
public ResourceLoadRequest(Deferred resource) { |
||||
this.resource = resource; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(ResourceLoadRequestListener handler) |
||||
{ |
||||
handler.loadResource(resource); |
||||
} |
||||
} |
@ -0,0 +1,18 @@ |
||||
package mightypork.gamecore.control.events.requests; |
||||
|
||||
import mightypork.gamecore.loading.Deferred; |
||||
|
||||
/** |
||||
* {@link ResourceLoadRequest} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface ResourceLoadRequestListener { |
||||
|
||||
/** |
||||
* Load a resource |
||||
* |
||||
* @param resource |
||||
*/ |
||||
void loadResource(Deferred resource); |
||||
} |
@ -0,0 +1,33 @@ |
||||
package mightypork.gamecore.control.events.requests; |
||||
|
||||
|
||||
import mightypork.util.control.eventbus.BusEvent; |
||||
import mightypork.util.control.eventbus.events.flags.SingleReceiverEvent; |
||||
|
||||
|
||||
/** |
||||
* Request to change screen |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
@SingleReceiverEvent |
||||
public class ScreenRequestEvent extends BusEvent<ScreenRequestListener> { |
||||
|
||||
private final String scrName; |
||||
|
||||
|
||||
/** |
||||
* @param screenKey screen name |
||||
*/ |
||||
public ScreenRequestEvent(String screenKey) { |
||||
scrName = screenKey; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handleBy(ScreenRequestListener handler) |
||||
{ |
||||
handler.showScreen(scrName); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,14 @@ |
||||
package mightypork.gamecore.control.events.requests; |
||||
|
||||
/** |
||||
* {@link ScreenRequestEvent} listener |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
public interface ScreenRequestListener { |
||||
|
||||
/** |
||||
* @param key screen to show |
||||
*/ |
||||
void showScreen(String key); |
||||
} |
@ -1,115 +0,0 @@ |
||||
package mightypork.rogue.screens.gamescreen; |
||||
|
||||
|
||||
import mightypork.rogue.world.PlayerControl; |
||||
import mightypork.rogue.world.World; |
||||
import mightypork.rogue.world.WorldPos; |
||||
import mightypork.rogue.world.entity.Entity; |
||||
import mightypork.rogue.world.entity.models.EntityMoveListener; |
||||
import mightypork.rogue.world.level.Level; |
||||
import mightypork.util.constraints.vect.Vect; |
||||
import mightypork.util.math.Polar; |
||||
import mightypork.util.math.Calc.Deg; |
||||
import mightypork.gamecore.control.events.KeyEvent; |
||||
import mightypork.gamecore.control.events.MouseButtonEvent; |
||||
import mightypork.gamecore.input.InputSystem; |
||||
import mightypork.gamecore.input.Keys; |
||||
|
||||
|
||||
public class WRBasicControls extends WorldRenderComponent implements KeyEvent.Listener, MouseButtonEvent.Listener, EntityMoveListener { |
||||
|
||||
private final PlayerControl pc; |
||||
|
||||
|
||||
public WRBasicControls(World world) { |
||||
super(world); |
||||
pc = world.getPlayerControl(); |
||||
pc.addMoveListener(this); |
||||
} |
||||
|
||||
|
||||
private void handleHeldKey() |
||||
{ |
||||
if (InputSystem.isKeyDown(Keys.LEFT)) { |
||||
pc.walkWest(); |
||||
} else if (InputSystem.isKeyDown(Keys.RIGHT)) { |
||||
pc.walkEast(); |
||||
} else if (InputSystem.isKeyDown(Keys.UP)) { |
||||
pc.walkNorth(); |
||||
} else if (InputSystem.isKeyDown(Keys.DOWN)) { |
||||
pc.walkSouth(); |
||||
} |
||||
|
||||
if(InputSystem.isMouseButtonDown(0)) { |
||||
walkByMouse(InputSystem.getMousePos()); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onStepFinished(Entity entity, World world, Level level) |
||||
{ |
||||
handleHeldKey(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onPathFinished(Entity entity, World world, Level level) |
||||
{ |
||||
handleHeldKey(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onPathInterrupted(Entity entity, World world, Level level) |
||||
{ |
||||
handleHeldKey(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void receive(MouseButtonEvent event) |
||||
{ |
||||
if (!event.isDown()) return; |
||||
|
||||
walkByMouse(event.getPos()); |
||||
} |
||||
|
||||
|
||||
private void walkByMouse(Vect mouse) |
||||
{ |
||||
|
||||
WorldPos clicked = toWorldPos(mouse); |
||||
WorldPos plpos = pc.getPos(); |
||||
|
||||
Polar p = Polar.fromCoord(clicked.x - plpos.x, clicked.y - plpos.y); |
||||
|
||||
int dir = Deg.round90(p.getAngleDeg()) / 90; |
||||
|
||||
switch (dir) { |
||||
case 0: |
||||
pc.walkEast(); |
||||
return; |
||||
|
||||
case 1: |
||||
pc.walkSouth(); |
||||
return; |
||||
|
||||
case 2: |
||||
pc.walkWest(); |
||||
return; |
||||
|
||||
case 3: |
||||
pc.walkNorth(); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void receive(KeyEvent event) |
||||
{ |
||||
handleHeldKey(); |
||||
} |
||||
|
||||
} |
@ -1,44 +0,0 @@ |
||||
package mightypork.rogue.screens.gamescreen; |
||||
|
||||
|
||||
import mightypork.gamecore.gui.components.InputComponent; |
||||
import mightypork.rogue.world.World; |
||||
import mightypork.rogue.world.WorldPos; |
||||
import mightypork.rogue.world.WorldRenderer; |
||||
import mightypork.util.constraints.vect.Vect; |
||||
|
||||
|
||||
public class WorldRenderComponent extends InputComponent { |
||||
|
||||
protected final WorldRenderer worldRenderer; |
||||
protected final World world; |
||||
|
||||
|
||||
public WorldRenderComponent(World world) { |
||||
this.world = world; |
||||
this.worldRenderer = new WorldRenderer(world, this, 8, 6, 72); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void renderComponent() |
||||
{ |
||||
worldRenderer.render(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void updateLayout() |
||||
{ |
||||
worldRenderer.poll(); // update sizing
|
||||
} |
||||
|
||||
|
||||
/** |
||||
* Get tile coord at a screen position |
||||
*/ |
||||
public WorldPos toWorldPos(Vect pos) |
||||
{ |
||||
return worldRenderer.getClickedTile(pos); |
||||
} |
||||
} |
@ -1,4 +1,4 @@ |
||||
package mightypork.rogue.screens.gamescreen; |
||||
package mightypork.rogue.screens.gamescreen.gui; |
||||
|
||||
|
||||
import mightypork.gamecore.gui.AlignX; |
@ -0,0 +1,45 @@ |
||||
package mightypork.rogue.screens.gamescreen.world; |
||||
|
||||
|
||||
import mightypork.gamecore.input.InputSystem; |
||||
import mightypork.gamecore.input.Keys; |
||||
import mightypork.rogue.world.PlayerControl; |
||||
import mightypork.util.constraints.vect.Vect; |
||||
|
||||
|
||||
public class MIPKeyWalk implements MapInteractionPlugin { |
||||
|
||||
@Override |
||||
public void onStepEnd(MapView wv, PlayerControl player) |
||||
{ |
||||
walkByKey(player); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onClick(MapView wv, PlayerControl player, Vect mouse) |
||||
{ |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onKey(MapView wv, PlayerControl player, int key) |
||||
{ |
||||
walkByKey(player); |
||||
} |
||||
|
||||
|
||||
private void walkByKey(PlayerControl player) |
||||
{ |
||||
if (InputSystem.isKeyDown(Keys.LEFT)) { |
||||
player.goWest(); |
||||
} else if (InputSystem.isKeyDown(Keys.RIGHT)) { |
||||
player.goEast(); |
||||
} else if (InputSystem.isKeyDown(Keys.UP)) { |
||||
player.goNorth(); |
||||
} else if (InputSystem.isKeyDown(Keys.DOWN)) { |
||||
player.goSouth(); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,59 @@ |
||||
package mightypork.rogue.screens.gamescreen.world; |
||||
|
||||
|
||||
import mightypork.gamecore.input.InputSystem; |
||||
import mightypork.rogue.world.PlayerControl; |
||||
import mightypork.rogue.world.WorldPos; |
||||
import mightypork.util.constraints.vect.Vect; |
||||
import mightypork.util.math.Polar; |
||||
import mightypork.util.math.Calc.Deg; |
||||
|
||||
|
||||
public class MIPMouseWalk implements MapInteractionPlugin { |
||||
|
||||
@Override |
||||
public void onStepEnd(MapView wv, PlayerControl player) |
||||
{ |
||||
if (InputSystem.isMouseButtonDown(0)) { |
||||
// walk by holding btn
|
||||
onClick(wv, player, InputSystem.getMousePos()); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onClick(MapView wv, PlayerControl player, Vect mouse) |
||||
{ |
||||
WorldPos plpos = player.getPos(); |
||||
WorldPos clicked = wv.toWorldPos(mouse); |
||||
|
||||
Polar p = Polar.fromCoord(clicked.x - plpos.x, clicked.y - plpos.y); |
||||
|
||||
int dir = Deg.round90(p.getAngleDeg()) / 90; |
||||
|
||||
switch (dir) { |
||||
case 0: |
||||
player.goEast(); |
||||
return; |
||||
|
||||
case 1: |
||||
player.goSouth(); |
||||
return; |
||||
|
||||
case 2: |
||||
player.goWest(); |
||||
return; |
||||
|
||||
case 3: |
||||
player.goNorth(); |
||||
return; |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onKey(MapView wv, PlayerControl player, int key) |
||||
{ |
||||
} |
||||
|
||||
} |
@ -0,0 +1,18 @@ |
||||
package mightypork.rogue.screens.gamescreen.world; |
||||
|
||||
|
||||
import mightypork.rogue.world.PlayerControl; |
||||
import mightypork.util.constraints.vect.Vect; |
||||
|
||||
|
||||
public interface MapInteractionPlugin { |
||||
|
||||
void onStepEnd(MapView wv, PlayerControl player); |
||||
|
||||
|
||||
void onClick(MapView wv, PlayerControl player, Vect mouse); |
||||
|
||||
|
||||
void onKey(MapView wv, PlayerControl player, int key); |
||||
|
||||
} |
@ -0,0 +1,124 @@ |
||||
package mightypork.rogue.screens.gamescreen.world; |
||||
|
||||
|
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
|
||||
import mightypork.gamecore.control.events.input.KeyEvent; |
||||
import mightypork.gamecore.control.events.input.KeyListener; |
||||
import mightypork.gamecore.control.events.input.MouseButtonEvent; |
||||
import mightypork.gamecore.control.events.input.MouseButtonListener; |
||||
import mightypork.gamecore.gui.components.InputComponent; |
||||
import mightypork.rogue.world.PlayerControl; |
||||
import mightypork.rogue.world.World; |
||||
import mightypork.rogue.world.WorldPos; |
||||
import mightypork.rogue.world.WorldRenderer; |
||||
import mightypork.rogue.world.entity.Entity; |
||||
import mightypork.rogue.world.entity.models.EntityMoveListener; |
||||
import mightypork.rogue.world.level.Level; |
||||
import mightypork.util.constraints.vect.Vect; |
||||
|
||||
|
||||
public class MapView extends InputComponent implements KeyListener, MouseButtonListener, EntityMoveListener { |
||||
|
||||
protected final WorldRenderer worldRenderer; |
||||
protected final World world; |
||||
private final PlayerControl pc; |
||||
|
||||
private final Set<MapInteractionPlugin> plugins = new HashSet<>(); |
||||
|
||||
|
||||
public MapView(World world) { |
||||
this.world = world; |
||||
this.worldRenderer = new WorldRenderer(world, this, 8, 6, 72); |
||||
pc = world.getPlayerControl(); |
||||
pc.addMoveListener(this); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected void renderComponent() |
||||
{ |
||||
worldRenderer.render(); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void updateLayout() |
||||
{ |
||||
worldRenderer.poll(); // update sizing
|
||||
} |
||||
|
||||
|
||||
/** |
||||
* Get tile coord at a screen position |
||||
* |
||||
* @param pos position on screen (px) |
||||
* @return position on map (tiles) |
||||
*/ |
||||
public WorldPos toWorldPos(Vect pos) |
||||
{ |
||||
return worldRenderer.getClickedTile(pos); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onStepFinished(Entity entity, World world, Level level) |
||||
{ |
||||
for (MapInteractionPlugin p : plugins) { |
||||
p.onStepEnd(this, pc); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onPathFinished(Entity entity, World world, Level level) |
||||
{ |
||||
for (MapInteractionPlugin p : plugins) { |
||||
p.onStepEnd(this, pc); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void onPathInterrupted(Entity entity, World world, Level level) |
||||
{ |
||||
for (MapInteractionPlugin p : plugins) { |
||||
p.onStepEnd(this, pc); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void receive(MouseButtonEvent event) |
||||
{ |
||||
if (!event.isOver(this)) return; |
||||
|
||||
if (!event.isUp()) return; // only btn-release evt
|
||||
|
||||
for (MapInteractionPlugin p : plugins) { |
||||
p.onClick(this, pc, event.getPos()); |
||||
} |
||||
|
||||
event.consume(); // only our clicks.
|
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void receive(KeyEvent event) |
||||
{ |
||||
if (!event.isDown()) return; |
||||
|
||||
for (MapInteractionPlugin p : plugins) { |
||||
p.onKey(this, pc, event.getKey()); |
||||
} |
||||
|
||||
// don't consume key events, can be useful for others.
|
||||
} |
||||
|
||||
|
||||
public void addPlugin(MapInteractionPlugin plugin) |
||||
{ |
||||
plugins.add(plugin); |
||||
} |
||||
} |
@ -0,0 +1,115 @@ |
||||
package mightypork.util.control.eventbus; |
||||
|
||||
|
||||
import mightypork.util.control.eventbus.events.flags.DelayedEvent; |
||||
import mightypork.util.control.eventbus.events.flags.ImmediateEvent; |
||||
import mightypork.util.control.eventbus.events.flags.NonConsumableEvent; |
||||
import mightypork.util.control.eventbus.events.flags.SingleReceiverEvent; |
||||
import mightypork.util.control.eventbus.events.flags.UnloggedEvent; |
||||
|
||||
|
||||
/** |
||||
* <p> |
||||
* Event that can be handled by HANDLER, subscribing to the event bus. |
||||
* </p> |
||||
* <p> |
||||
* Can be annotated as {@link SingleReceiverEvent} to be delivered once only, |
||||
* and {@link DelayedEvent} or {@link ImmediateEvent} to specify default sending |
||||
* mode. When marked as {@link UnloggedEvent}, it will not appear in detailed |
||||
* bus logging (useful for very frequent events, such as UpdateEvent). |
||||
* </p> |
||||
* <p> |
||||
* Events annotated as {@link NonConsumableEvent} will throw an exception upon |
||||
* an attempt to consume them. |
||||
* </p> |
||||
* <p> |
||||
* Default sending mode (if not changed by annotations) is <i>queued</i> with |
||||
* zero delay. |
||||
* </p> |
||||
* |
||||
* @author MightyPork |
||||
* @param <HANDLER> handler type |
||||
*/ |
||||
public abstract class BusEvent<HANDLER> { |
||||
|
||||
private boolean consumed; |
||||
private boolean served; |
||||
|
||||
|
||||
/** |
||||
* Ask handler to handle this message. |
||||
* |
||||
* @param handler handler instance |
||||
*/ |
||||
protected abstract void handleBy(HANDLER handler); |
||||
|
||||
|
||||
/** |
||||
* Consume the event, so no other clients will receive it. |
||||
* |
||||
* @throws UnsupportedOperationException if the {@link NonConsumableEvent} |
||||
* annotation is present. |
||||
*/ |
||||
public final void consume() |
||||
{ |
||||
System.out.println("consume "+getClass()); |
||||
if (consumed) throw new IllegalStateException("Already consumed."); |
||||
|
||||
if (getClass().isAnnotationPresent(NonConsumableEvent.class)) { |
||||
throw new UnsupportedOperationException("Not consumable."); |
||||
} |
||||
|
||||
consumed = true; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Deliver to a handler using the handleBy method. |
||||
* |
||||
* @param handler handler instance |
||||
*/ |
||||
final void deliverTo(HANDLER handler) |
||||
{ |
||||
handleBy(handler); |
||||
|
||||
if (!served) { |
||||
if (getClass().isAnnotationPresent(SingleReceiverEvent.class)) { |
||||
consumed = true; |
||||
} |
||||
|
||||
served = true; |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Check if the event is consumed. Consumed event is not served to other |
||||
* clients. |
||||
* |
||||
* @return true if consumed |
||||
*/ |
||||
final boolean isConsumed() |
||||
{ |
||||
return consumed; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* @return true if the event was served to at least 1 client |
||||
*/ |
||||
final boolean wasServed() |
||||
{ |
||||
return served; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Clear "served" and "consumed" flags before dispatching. |
||||
*/ |
||||
final void clearFlags() |
||||
{ |
||||
served = false; |
||||
consumed = false; |
||||
} |
||||
|
||||
} |
@ -1,36 +0,0 @@ |
||||
package mightypork.util.control.eventbus.events; |
||||
|
||||
|
||||
import mightypork.util.control.eventbus.events.flags.DelayedEvent; |
||||
import mightypork.util.control.eventbus.events.flags.ImmediateEvent; |
||||
import mightypork.util.control.eventbus.events.flags.SingleReceiverEvent; |
||||
import mightypork.util.control.eventbus.events.flags.UnloggedEvent; |
||||
|
||||
|
||||
/** |
||||
* <p> |
||||
* Something that can be handled by HANDLER, subscribing to the event bus. |
||||
* </p> |
||||
* <p> |
||||
* Can be annotated as {@link SingleReceiverEvent} to be delivered once only, |
||||
* and {@link DelayedEvent} or {@link ImmediateEvent} to specify default sending |
||||
* mode. When marked as {@link UnloggedEvent}, it will not appear in detailed |
||||
* bus logging (useful for very frequent events, such as UpdateEvent). |
||||
* </p> |
||||
* <p> |
||||
* Default sending mode (if not changed by annotations) is <i>queued</i> with |
||||
* zero delay. |
||||
* </p> |
||||
* |
||||
* @author MightyPork |
||||
* @param <HANDLER> handler type |
||||
*/ |
||||
public interface Event<HANDLER> { |
||||
|
||||
/** |
||||
* Ask handler to handle this message. |
||||
* |
||||
* @param handler handler instance |
||||
*/ |
||||
public void handleBy(HANDLER handler); |
||||
} |
@ -0,0 +1,20 @@ |
||||
package mightypork.util.control.eventbus.events.flags; |
||||
|
||||
import java.lang.annotation.Documented; |
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
|
||||
/** |
||||
* Event that cannot be consumed |
||||
* |
||||
* @author MightyPork |
||||
*/ |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Documented |
||||
@Target(ElementType.TYPE) |
||||
public @interface NonConsumableEvent { |
||||
|
||||
} |
Loading…
Reference in new issue