Huge refactoring, cleanup, constraints
This commit is contained in:
@@ -16,7 +16,7 @@ import mightypork.rogue.sounds.SoundSystem;
|
||||
import mightypork.rogue.tasks.TaskTakeScreenshot;
|
||||
import mightypork.rogue.util.Utils;
|
||||
import mightypork.utils.control.Destroyable;
|
||||
import mightypork.utils.control.bus.MessageBus;
|
||||
import mightypork.utils.control.bus.EventBus;
|
||||
import mightypork.utils.control.timing.TimerDelta;
|
||||
import mightypork.utils.control.timing.UpdateEvent;
|
||||
import mightypork.utils.control.timing.Updateable;
|
||||
@@ -39,7 +39,7 @@ public class App implements Destroyable, AppAccess {
|
||||
private InputSystem input;
|
||||
private SoundSystem sounds;
|
||||
private DisplaySystem display;
|
||||
private MessageBus events;
|
||||
private EventBus events;
|
||||
|
||||
/** current screen */
|
||||
private Screen screen;
|
||||
@@ -172,7 +172,7 @@ public class App implements Destroyable, AppAccess {
|
||||
*/
|
||||
private void initBus()
|
||||
{
|
||||
events = new MessageBus();
|
||||
events = new EventBus();
|
||||
events.subscribe(this);
|
||||
|
||||
events.createChannel(UpdateEvent.class, Updateable.class);
|
||||
@@ -335,7 +335,7 @@ public class App implements Destroyable, AppAccess {
|
||||
* @return event bus
|
||||
*/
|
||||
@Override
|
||||
public MessageBus bus()
|
||||
public EventBus bus()
|
||||
{
|
||||
return events;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ package mightypork.rogue;
|
||||
import mightypork.rogue.display.DisplaySystem;
|
||||
import mightypork.rogue.input.InputSystem;
|
||||
import mightypork.rogue.sounds.SoundSystem;
|
||||
import mightypork.utils.control.bus.MessageBus;
|
||||
import mightypork.utils.control.bus.EventBus;
|
||||
|
||||
|
||||
/**
|
||||
@@ -35,7 +35,7 @@ public interface AppAccess {
|
||||
/**
|
||||
* @return event bus
|
||||
*/
|
||||
abstract MessageBus bus();
|
||||
abstract EventBus bus();
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,7 +4,7 @@ package mightypork.rogue;
|
||||
import mightypork.rogue.display.DisplaySystem;
|
||||
import mightypork.rogue.input.InputSystem;
|
||||
import mightypork.rogue.sounds.SoundSystem;
|
||||
import mightypork.utils.control.bus.MessageBus;
|
||||
import mightypork.utils.control.bus.EventBus;
|
||||
|
||||
|
||||
/**
|
||||
@@ -46,7 +46,7 @@ public class AppAdapter implements AppAccess {
|
||||
|
||||
|
||||
@Override
|
||||
public final MessageBus bus()
|
||||
public final EventBus bus()
|
||||
{
|
||||
return app.bus();
|
||||
}
|
||||
|
||||
@@ -7,13 +7,13 @@ import java.util.Set;
|
||||
|
||||
import mightypork.rogue.AppAccess;
|
||||
import mightypork.rogue.AppAdapter;
|
||||
import mightypork.utils.control.bus.MessageBus;
|
||||
import mightypork.utils.control.bus.EventBus;
|
||||
import mightypork.utils.control.bus.clients.DelegatingClient;
|
||||
import mightypork.utils.control.bus.clients.ToggleableClient;
|
||||
|
||||
|
||||
/**
|
||||
* Client that can be attached to the {@link MessageBus}, or added as a child
|
||||
* Client that can be attached to the {@link EventBus}, or added as a child
|
||||
* client to another {@link DelegatingClient}
|
||||
*
|
||||
* @author MightyPork
|
||||
@@ -51,7 +51,7 @@ public class ChildClient extends AppAdapter implements DelegatingClient, Togglea
|
||||
|
||||
|
||||
/**
|
||||
* Add a child subscriber to the {@link MessageBus}.<br>
|
||||
* Add a child subscriber to the {@link EventBus}.<br>
|
||||
*
|
||||
* @param client
|
||||
*/
|
||||
|
||||
@@ -9,8 +9,8 @@ import java.nio.ByteBuffer;
|
||||
import mightypork.rogue.AppAccess;
|
||||
import mightypork.rogue.bus.Subsystem;
|
||||
import mightypork.rogue.bus.events.ScreenChangeEvent;
|
||||
import mightypork.rogue.display.constraints.RenderContext;
|
||||
import mightypork.utils.logging.Log;
|
||||
import mightypork.utils.math.constraints.ConstraintContext;
|
||||
import mightypork.utils.math.coord.Coord;
|
||||
import mightypork.utils.math.coord.Rect;
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.DisplayMode;
|
||||
|
||||
|
||||
public class DisplaySystem extends Subsystem implements RenderContext {
|
||||
public class DisplaySystem extends Subsystem implements ConstraintContext {
|
||||
|
||||
private DisplayMode windowDisplayMode;
|
||||
private int targetFps;
|
||||
@@ -109,7 +109,7 @@ public class DisplaySystem extends Subsystem implements RenderContext {
|
||||
}
|
||||
|
||||
|
||||
public BufferedImage takeScreenshot()
|
||||
public Screenshot takeScreenshot()
|
||||
{
|
||||
glReadBuffer(GL_FRONT);
|
||||
int width = Display.getDisplayMode().getWidth();
|
||||
@@ -119,20 +119,9 @@ public class DisplaySystem extends Subsystem implements RenderContext {
|
||||
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
|
||||
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
|
||||
|
||||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
Screenshot sc = new Screenshot(width, height, bpp, buffer);
|
||||
|
||||
// convert to a buffered image
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
int i = (x + (width * y)) * bpp;
|
||||
int r = buffer.get(i) & 0xFF;
|
||||
int g = buffer.get(i + 1) & 0xFF;
|
||||
int b = buffer.get(i + 2) & 0xFF;
|
||||
image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
|
||||
}
|
||||
}
|
||||
|
||||
return image;
|
||||
return sc;
|
||||
}
|
||||
|
||||
|
||||
@@ -197,4 +186,20 @@ public class DisplaySystem extends Subsystem implements RenderContext {
|
||||
{
|
||||
return new Rect(getSize());
|
||||
}
|
||||
|
||||
public static class Screenshot {
|
||||
|
||||
public int width;
|
||||
public int height;
|
||||
public int bpp;
|
||||
public ByteBuffer bytes;
|
||||
|
||||
|
||||
public Screenshot(int width, int height, int bpp, ByteBuffer buffer) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.bpp = bpp;
|
||||
this.bytes = buffer;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@ import static org.lwjgl.opengl.GL11.*;
|
||||
import mightypork.rogue.AppAccess;
|
||||
import mightypork.rogue.bus.Subsystem;
|
||||
import mightypork.rogue.bus.events.ScreenChangeEvent;
|
||||
import mightypork.rogue.display.constraints.RenderContext;
|
||||
import mightypork.rogue.input.KeyBinder;
|
||||
import mightypork.rogue.input.KeyBindingPool;
|
||||
import mightypork.rogue.input.KeyStroke;
|
||||
import mightypork.utils.control.timing.Updateable;
|
||||
import mightypork.utils.math.constraints.ConstraintContext;
|
||||
import mightypork.utils.math.coord.Coord;
|
||||
import mightypork.utils.math.coord.Rect;
|
||||
|
||||
@@ -21,14 +21,12 @@ import mightypork.utils.math.coord.Rect;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class Screen extends Subsystem implements Updateable, KeyBinder, RenderContext, ScreenChangeEvent.Listener {
|
||||
public abstract class Screen extends Subsystem implements Updateable, KeyBinder, ConstraintContext, ScreenChangeEvent.Listener {
|
||||
|
||||
private final KeyBindingPool keybindings = new KeyBindingPool();
|
||||
|
||||
private boolean active;
|
||||
|
||||
private boolean inited = false;
|
||||
|
||||
|
||||
public Screen(AppAccess app) {
|
||||
super(app);
|
||||
|
||||
@@ -2,9 +2,9 @@ package mightypork.rogue.display;
|
||||
|
||||
|
||||
import mightypork.rogue.bus.ChildClient;
|
||||
import mightypork.rogue.display.constraints.RenderContext;
|
||||
import mightypork.rogue.display.constraints.Renderable;
|
||||
import mightypork.utils.control.timing.Updateable;
|
||||
import mightypork.utils.math.constraints.ConstraintContext;
|
||||
import mightypork.utils.math.coord.Rect;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ import mightypork.utils.math.coord.Rect;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class ScreenLayer extends ChildClient implements Renderable, Updateable, RenderContext {
|
||||
public abstract class ScreenLayer extends ChildClient implements Renderable, Updateable, ConstraintContext {
|
||||
|
||||
private Screen screen;
|
||||
|
||||
@@ -39,16 +39,6 @@ public abstract class ScreenLayer extends ChildClient implements Renderable, Upd
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* UNSUPPORTED
|
||||
*/
|
||||
@Override
|
||||
public final void setContext(RenderContext context)
|
||||
{
|
||||
throw new UnsupportedOperationException("ScreenLayer uses screen as it's context.");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Rect getRect()
|
||||
{
|
||||
|
||||
@@ -5,13 +5,15 @@ import java.util.LinkedList;
|
||||
|
||||
import mightypork.rogue.AppAccess;
|
||||
import mightypork.rogue.bus.ChildClient;
|
||||
import mightypork.utils.math.constraints.ConstraintContext;
|
||||
import mightypork.utils.math.constraints.RectConstraint;
|
||||
import mightypork.utils.math.coord.Rect;
|
||||
|
||||
|
||||
public class ElementHolder extends ChildClient implements RenderContext, Renderable {
|
||||
public class ElementHolder extends ChildClient implements ConstraintContext, RenderableWithContext {
|
||||
|
||||
private LinkedList<Renderable> elements = new LinkedList<Renderable>();
|
||||
private RenderContext context;
|
||||
private LinkedList<RenderableWithContext> elements = new LinkedList<RenderableWithContext>();
|
||||
private ConstraintContext context;
|
||||
|
||||
|
||||
public ElementHolder(AppAccess app) {
|
||||
@@ -19,64 +21,19 @@ public class ElementHolder extends ChildClient implements RenderContext, Rendera
|
||||
}
|
||||
|
||||
|
||||
public ElementHolder(AppAccess app, RenderContext context) {
|
||||
public ElementHolder(AppAccess app, ConstraintContext context) {
|
||||
super(app);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setContext(RenderContext context)
|
||||
public void setContext(ConstraintContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add element to the holder.
|
||||
*
|
||||
* @param elem
|
||||
*/
|
||||
public void add(Renderable elem)
|
||||
{
|
||||
if (elem == null) return;
|
||||
elem.setContext(this);
|
||||
elements.add(elem);
|
||||
addChildClient(elem);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add element to the holder.
|
||||
*
|
||||
* @param elem
|
||||
* @param constraint
|
||||
*/
|
||||
public void add(Renderable elem, RectConstraint constraint)
|
||||
{
|
||||
if (elem == null) return;
|
||||
|
||||
constraint.setContext(this);
|
||||
elem.setContext(constraint);
|
||||
|
||||
elements.add(elem);
|
||||
addChildClient(elem);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove element from the holder
|
||||
*
|
||||
* @param elem
|
||||
*/
|
||||
public void remove(Renderable elem)
|
||||
{
|
||||
if (elem == null) return;
|
||||
elements.remove(elem);
|
||||
removeChildClient(elem);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void render()
|
||||
{
|
||||
@@ -92,4 +49,49 @@ public class ElementHolder extends ChildClient implements RenderContext, Rendera
|
||||
return context.getRect();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add element to the holder.
|
||||
*
|
||||
* @param elem
|
||||
*/
|
||||
public void add(RenderableWithContext elem)
|
||||
{
|
||||
if (elem == null) return;
|
||||
elem.setContext(this);
|
||||
elements.add(elem);
|
||||
addChildClient(elem);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add element to the holder.
|
||||
*
|
||||
* @param elem
|
||||
* @param constraint
|
||||
*/
|
||||
public void add(RenderableWithContext elem, RectConstraint constraint)
|
||||
{
|
||||
if (elem == null) return;
|
||||
|
||||
constraint.setContext(this);
|
||||
elem.setContext(constraint);
|
||||
|
||||
elements.add(elem);
|
||||
addChildClient(elem);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove element from the holder
|
||||
*
|
||||
* @param elem
|
||||
*/
|
||||
public void remove(RenderableWithContext elem)
|
||||
{
|
||||
if (elem == null) return;
|
||||
elements.remove(elem);
|
||||
removeChildClient(elem);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
package mightypork.rogue.display.constraints;
|
||||
|
||||
|
||||
import mightypork.utils.math.coord.Rect;
|
||||
|
||||
|
||||
/**
|
||||
* Bounding box provider - context for {@link RectConstraint}
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public interface RenderContext {
|
||||
|
||||
/**
|
||||
* @return bounding rectangle
|
||||
*/
|
||||
public Rect getRect();
|
||||
}
|
||||
@@ -1,12 +1,16 @@
|
||||
package mightypork.rogue.display.constraints;
|
||||
|
||||
|
||||
public interface Renderable extends WithContext {
|
||||
/**
|
||||
* Can be rendered
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public interface Renderable {
|
||||
|
||||
/**
|
||||
* Render on screen
|
||||
*/
|
||||
public void render();
|
||||
|
||||
|
||||
@Override
|
||||
public void setContext(RenderContext context);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package mightypork.rogue.display.constraints;
|
||||
|
||||
|
||||
import mightypork.utils.math.constraints.ConstraintContext;
|
||||
import mightypork.utils.math.constraints.SettableContext;
|
||||
|
||||
|
||||
/**
|
||||
* {@link Renderable} with {@link SettableContext}
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public interface RenderableWithContext extends Renderable, SettableContext {
|
||||
|
||||
@Override
|
||||
public void render();
|
||||
|
||||
|
||||
@Override
|
||||
public void setContext(ConstraintContext context);
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package mightypork.rogue.display.constraints;
|
||||
|
||||
|
||||
public interface WithContext {
|
||||
|
||||
/**
|
||||
* Assign a context
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public void setContext(RenderContext context);
|
||||
}
|
||||
@@ -1,27 +1,27 @@
|
||||
package mightypork.rogue.display.screens.screenBouncy;
|
||||
|
||||
|
||||
import static mightypork.rogue.display.constraints.ConstraintFactory.*;
|
||||
import static mightypork.utils.math.constraints.ConstraintFactory.*;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import mightypork.rogue.display.constraints.NumConstraint;
|
||||
import mightypork.rogue.display.constraints.RectConstraint;
|
||||
import mightypork.rogue.display.constraints.RenderContext;
|
||||
import mightypork.rogue.display.constraints.Renderable;
|
||||
import mightypork.rogue.display.constraints.RenderableWithContext;
|
||||
import mightypork.rogue.textures.Render;
|
||||
import mightypork.utils.control.timing.Updateable;
|
||||
import mightypork.utils.control.timing.animation.AnimDouble;
|
||||
import mightypork.utils.math.color.RGB;
|
||||
import mightypork.utils.math.constraints.ConstraintContext;
|
||||
import mightypork.utils.math.constraints.NumConstraint;
|
||||
import mightypork.utils.math.constraints.RectConstraint;
|
||||
import mightypork.utils.math.coord.Rect;
|
||||
import mightypork.utils.math.easing.Easing;
|
||||
|
||||
|
||||
public class BouncyBox implements Renderable, Updateable, RenderContext {
|
||||
public class BouncyBox implements RenderableWithContext, Updateable, ConstraintContext {
|
||||
|
||||
private Random rand = new Random();
|
||||
|
||||
private RenderContext context;
|
||||
private ConstraintContext context;
|
||||
|
||||
private RectConstraint box;
|
||||
|
||||
@@ -30,17 +30,11 @@ public class BouncyBox implements Renderable, Updateable, RenderContext {
|
||||
|
||||
public BouncyBox() {
|
||||
NumConstraint side = c_height(this);
|
||||
|
||||
NumConstraint move_length = c_sub(c_width(this), side);
|
||||
|
||||
NumConstraint offset = c_mul(move_length, c_n(pos));
|
||||
|
||||
RectConstraint abox = c_sizedBox(this, offset, c_n(0), side, side);
|
||||
|
||||
NumConstraint margin = c_percent(side, c_n(10));
|
||||
|
||||
RectConstraint with_margin = c_shrink(abox, margin);
|
||||
|
||||
box = with_margin;
|
||||
}
|
||||
|
||||
@@ -60,7 +54,7 @@ public class BouncyBox implements Renderable, Updateable, RenderContext {
|
||||
|
||||
|
||||
@Override
|
||||
public void setContext(RenderContext context)
|
||||
public void setContext(ConstraintContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mightypork.rogue.display.screens.screenBouncy;
|
||||
|
||||
|
||||
import static mightypork.rogue.display.constraints.ConstraintFactory.*;
|
||||
import static mightypork.utils.math.constraints.ConstraintFactory.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@@ -12,22 +12,37 @@ import javax.imageio.ImageIO;
|
||||
|
||||
import mightypork.rogue.Paths;
|
||||
import mightypork.rogue.display.DisplaySystem;
|
||||
import mightypork.rogue.display.DisplaySystem.Screenshot;
|
||||
import mightypork.utils.logging.Log;
|
||||
|
||||
|
||||
public class TaskTakeScreenshot implements Runnable {
|
||||
|
||||
private BufferedImage image;
|
||||
private Screenshot scr;
|
||||
|
||||
|
||||
public TaskTakeScreenshot(DisplaySystem disp) {
|
||||
this.image = disp.takeScreenshot();
|
||||
scr = disp.takeScreenshot();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
|
||||
BufferedImage image = new BufferedImage(scr.width, scr.height, BufferedImage.TYPE_INT_RGB);
|
||||
|
||||
// convert to a buffered image
|
||||
for (int x = 0; x < scr.width; x++) {
|
||||
for (int y = 0; y < scr.height; y++) {
|
||||
int i = (x + (scr.width * y)) * scr.bpp;
|
||||
int r = scr.bytes.get(i) & 0xFF;
|
||||
int g = scr.bytes.get(i + 1) & 0xFF;
|
||||
int b = scr.bytes.get(i + 2) & 0xFF;
|
||||
image.setRGB(x, scr.height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
|
||||
}
|
||||
}
|
||||
|
||||
String fname = getUniqueScreenshotName();
|
||||
|
||||
// generate unique filename
|
||||
|
||||
+11
-11
@@ -8,13 +8,13 @@ import mightypork.utils.logging.Log;
|
||||
|
||||
|
||||
/**
|
||||
* An event bus, accommodating multiple {@link MessageChannel}s.
|
||||
* An event bus, accommodating multiple {@link EventChannel}s.
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
final public class MessageBus {
|
||||
final public class EventBus {
|
||||
|
||||
private Collection<MessageChannel<?, ?>> channels = new LinkedHashSet<MessageChannel<?, ?>>();
|
||||
private Collection<EventChannel<?, ?>> channels = new LinkedHashSet<EventChannel<?, ?>>();
|
||||
|
||||
private Collection<Object> clients = new LinkedHashSet<Object>();
|
||||
|
||||
@@ -22,16 +22,16 @@ final public class MessageBus {
|
||||
|
||||
|
||||
/**
|
||||
* Add a {@link MessageChannel} to this bus.<br>
|
||||
* Add a {@link EventChannel} to this bus.<br>
|
||||
* If a channel of matching types is already added, it is returned instead.
|
||||
*
|
||||
* @param channel channel to be added
|
||||
* @return the channel that's now in the bus
|
||||
*/
|
||||
public MessageChannel<?, ?> addChannel(MessageChannel<?, ?> channel)
|
||||
public EventChannel<?, ?> addChannel(EventChannel<?, ?> channel)
|
||||
{
|
||||
// if the channel already exists, return this instance instead.
|
||||
for (MessageChannel<?, ?> ch : channels) {
|
||||
for (EventChannel<?, ?> ch : channels) {
|
||||
if (ch.equals(channel)) {
|
||||
Log.w("Channel of type " + channel + " already registered.");
|
||||
return ch;
|
||||
@@ -45,11 +45,11 @@ final public class MessageBus {
|
||||
|
||||
|
||||
/**
|
||||
* Remove a {@link MessageChannel} from this bus
|
||||
* Remove a {@link EventChannel} from this bus
|
||||
*
|
||||
* @param channel true if channel was removed
|
||||
*/
|
||||
public void removeChannel(MessageChannel<?, ?> channel)
|
||||
public void removeChannel(EventChannel<?, ?> channel)
|
||||
{
|
||||
channels.remove(channel);
|
||||
}
|
||||
@@ -65,7 +65,7 @@ final public class MessageBus {
|
||||
{
|
||||
boolean sent = false;
|
||||
|
||||
for (MessageChannel<?, ?> b : channels) {
|
||||
for (EventChannel<?, ?> b : channels) {
|
||||
sent |= b.broadcast(message, clients);
|
||||
}
|
||||
|
||||
@@ -121,9 +121,9 @@ final public class MessageBus {
|
||||
* @param clientClass client type
|
||||
* @return the created channel instance
|
||||
*/
|
||||
public <F_MESSAGE extends Handleable<F_CLIENT>, F_CLIENT> MessageChannel<?, ?> createChannel(Class<F_MESSAGE> messageClass, Class<F_CLIENT> clientClass)
|
||||
public <F_EVENT extends Handleable<F_CLIENT>, F_CLIENT> EventChannel<?, ?> createChannel(Class<F_EVENT> messageClass, Class<F_CLIENT> clientClass)
|
||||
{
|
||||
MessageChannel<F_MESSAGE, F_CLIENT> bc = new MessageChannel<F_MESSAGE, F_CLIENT>(messageClass, clientClass);
|
||||
EventChannel<F_EVENT, F_CLIENT> bc = new EventChannel<F_EVENT, F_CLIENT>(messageClass, clientClass);
|
||||
return addChannel(bc);
|
||||
}
|
||||
|
||||
+13
-13
@@ -10,19 +10,19 @@ import mightypork.utils.logging.Log;
|
||||
|
||||
|
||||
/**
|
||||
* Message channel, module of {@link MessageBus}
|
||||
* Message channel, module of {@link EventBus}
|
||||
*
|
||||
* @author MightyPork
|
||||
* @param <MESSAGE> message type
|
||||
* @param <EVENT> message type
|
||||
* @param <CLIENT> client (subscriber) type
|
||||
*/
|
||||
final public class MessageChannel<MESSAGE extends Handleable<CLIENT>, CLIENT> {
|
||||
final public class EventChannel<EVENT extends Handleable<CLIENT>, CLIENT> {
|
||||
|
||||
private Class<CLIENT> clientClass;
|
||||
private Class<MESSAGE> messageClass;
|
||||
private Class<EVENT> messageClass;
|
||||
|
||||
|
||||
public MessageChannel(Class<MESSAGE> messageClass, Class<CLIENT> clientClass) {
|
||||
public EventChannel(Class<EVENT> messageClass, Class<CLIENT> clientClass) {
|
||||
|
||||
if (messageClass == null || clientClass == null) throw new NullPointerException("Null Message or Client class.");
|
||||
|
||||
@@ -43,7 +43,7 @@ final public class MessageChannel<MESSAGE extends Handleable<CLIENT>, CLIENT> {
|
||||
{
|
||||
if (!canBroadcast(message)) return false;
|
||||
|
||||
MESSAGE evt = messageClass.cast(message);
|
||||
EVENT evt = messageClass.cast(message);
|
||||
|
||||
doBroadcast(evt, clients, new HashSet<Object>());
|
||||
|
||||
@@ -51,7 +51,7 @@ final public class MessageChannel<MESSAGE extends Handleable<CLIENT>, CLIENT> {
|
||||
}
|
||||
|
||||
|
||||
private void doBroadcast(MESSAGE message, Collection<Object> clients, Collection<Object> processed)
|
||||
private void doBroadcast(EVENT message, Collection<Object> clients, Collection<Object> processed)
|
||||
{
|
||||
for (Object client : clients) {
|
||||
|
||||
@@ -87,7 +87,7 @@ final public class MessageChannel<MESSAGE extends Handleable<CLIENT>, CLIENT> {
|
||||
* @param message message to send
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void sendTo(Object client, MESSAGE message)
|
||||
private void sendTo(Object client, EVENT message)
|
||||
{
|
||||
if (clientClass.isInstance(client)) {
|
||||
((Handleable<CLIENT>) message).handleBy((CLIENT) client);
|
||||
@@ -97,7 +97,7 @@ final public class MessageChannel<MESSAGE extends Handleable<CLIENT>, CLIENT> {
|
||||
|
||||
/**
|
||||
* Check if the given message can be broadcasted by this
|
||||
* {@link MessageChannel}
|
||||
* {@link EventChannel}
|
||||
*
|
||||
* @param message event object
|
||||
* @return can be broadcasted
|
||||
@@ -124,8 +124,8 @@ final public class MessageChannel<MESSAGE extends Handleable<CLIENT>, CLIENT> {
|
||||
{
|
||||
if (this == obj) return true;
|
||||
if (obj == null) return false;
|
||||
if (!(obj instanceof MessageChannel)) return false;
|
||||
MessageChannel<?, ?> other = (MessageChannel<?, ?>) obj;
|
||||
if (!(obj instanceof EventChannel)) return false;
|
||||
EventChannel<?, ?> other = (EventChannel<?, ?>) obj;
|
||||
if (clientClass == null) {
|
||||
if (other.clientClass != null) return false;
|
||||
} else if (!clientClass.equals(other.clientClass)) return false;
|
||||
@@ -150,9 +150,9 @@ final public class MessageChannel<MESSAGE extends Handleable<CLIENT>, CLIENT> {
|
||||
* @param clientClass client class
|
||||
* @return the broadcaster
|
||||
*/
|
||||
public static <F_MESSAGE extends Handleable<F_CLIENT>, F_CLIENT> MessageChannel<F_MESSAGE, F_CLIENT> create(Class<F_MESSAGE> messageClass, Class<F_CLIENT> clientClass)
|
||||
public static <F_EVENT extends Handleable<F_CLIENT>, F_CLIENT> EventChannel<F_EVENT, F_CLIENT> create(Class<F_EVENT> messageClass, Class<F_CLIENT> clientClass)
|
||||
{
|
||||
return new MessageChannel<F_MESSAGE, F_CLIENT>(messageClass, clientClass);
|
||||
return new EventChannel<F_EVENT, F_CLIENT>(messageClass, clientClass);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,11 +4,19 @@ package mightypork.utils.control.timing;
|
||||
import mightypork.utils.control.bus.Handleable;
|
||||
|
||||
|
||||
/**
|
||||
* Delta timing update event
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public class UpdateEvent implements Handleable<Updateable> {
|
||||
|
||||
private final double deltaTime;
|
||||
|
||||
|
||||
/**
|
||||
* @param deltaTime time since last update (sec)
|
||||
*/
|
||||
public UpdateEvent(double deltaTime) {
|
||||
this.deltaTime = deltaTime;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package mightypork.utils.control.timing;
|
||||
|
||||
|
||||
/**
|
||||
* object supporting delta timing
|
||||
* Uses delta timing
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
|
||||
+7
-10
@@ -1,35 +1,32 @@
|
||||
package mightypork.rogue.display.constraints;
|
||||
package mightypork.utils.math.constraints;
|
||||
|
||||
|
||||
import mightypork.utils.math.coord.Coord;
|
||||
|
||||
|
||||
/**
|
||||
* A constraint based on a given {@link RenderContext}
|
||||
* A constraint based on a given {@link ConstraintContext}
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class BaseConstraint implements WithContext {
|
||||
public abstract class BaseConstraint implements SettableContext {
|
||||
|
||||
protected RenderContext context = null;
|
||||
private ConstraintContext context = null;
|
||||
|
||||
|
||||
public BaseConstraint(RenderContext context) {
|
||||
public BaseConstraint(ConstraintContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setContext(RenderContext context)
|
||||
public void setContext(ConstraintContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return context
|
||||
*/
|
||||
public RenderContext getContext()
|
||||
public ConstraintContext getContext()
|
||||
{
|
||||
return context;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package mightypork.utils.math.constraints;
|
||||
|
||||
|
||||
import mightypork.utils.math.coord.Rect;
|
||||
|
||||
|
||||
/**
|
||||
* Context for constraints, with a bounding {@link Rect}.
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public interface ConstraintContext {
|
||||
|
||||
/**
|
||||
* Get context boundary
|
||||
*
|
||||
* @return bounding rectangle
|
||||
*/
|
||||
public Rect getRect();
|
||||
}
|
||||
+22
-22
@@ -1,4 +1,4 @@
|
||||
package mightypork.rogue.display.constraints;
|
||||
package mightypork.utils.math.constraints;
|
||||
|
||||
|
||||
import mightypork.utils.control.timing.animation.AnimDouble;
|
||||
@@ -66,14 +66,14 @@ public class ConstraintFactory {
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_round(RenderContext context)
|
||||
public static RectConstraint c_round(ConstraintContext context)
|
||||
{
|
||||
return new RectConstraint(context) {
|
||||
|
||||
@Override
|
||||
public Rect getRect()
|
||||
{
|
||||
return context.getRect().round();
|
||||
return getContext().getRect().round();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -209,7 +209,7 @@ public class ConstraintFactory {
|
||||
}
|
||||
|
||||
|
||||
public static NumConstraint c_width(final RenderContext context)
|
||||
public static NumConstraint c_width(final ConstraintContext context)
|
||||
{
|
||||
return new NumConstraint(context) {
|
||||
|
||||
@@ -222,7 +222,7 @@ public class ConstraintFactory {
|
||||
}
|
||||
|
||||
|
||||
public static NumConstraint c_height(final RenderContext context)
|
||||
public static NumConstraint c_height(final ConstraintContext context)
|
||||
{
|
||||
return new NumConstraint(context) {
|
||||
|
||||
@@ -235,14 +235,14 @@ public class ConstraintFactory {
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_row(RenderContext context, final int rows, final int index)
|
||||
public static RectConstraint c_row(ConstraintContext context, final int rows, final int index)
|
||||
{
|
||||
return new RectConstraint(context) {
|
||||
|
||||
@Override
|
||||
public Rect getRect()
|
||||
{
|
||||
double height = context.getRect().getSize().y;
|
||||
double height = getContext().getRect().getSize().y;
|
||||
double perRow = height / rows;
|
||||
|
||||
return Rect.fromSize(getOrigin().add(0, perRow * (rows - index - 1)), getSize().setY(perRow));
|
||||
@@ -251,14 +251,14 @@ public class ConstraintFactory {
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_column(RenderContext context, final int columns, final int index)
|
||||
public static RectConstraint c_column(ConstraintContext context, final int columns, final int index)
|
||||
{
|
||||
return new RectConstraint(context) {
|
||||
|
||||
@Override
|
||||
public Rect getRect()
|
||||
{
|
||||
double width = context.getRect().getSize().x;
|
||||
double width = getContext().getRect().getSize().x;
|
||||
double perCol = width / columns;
|
||||
|
||||
return Rect.fromSize(getOrigin().add(perCol * index, 0), getSize().setX(perCol));
|
||||
@@ -267,57 +267,57 @@ public class ConstraintFactory {
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_shrink(RenderContext context, NumConstraint shrink)
|
||||
public static RectConstraint c_shrink(ConstraintContext context, NumConstraint shrink)
|
||||
{
|
||||
return c_shrink(context, shrink, shrink, shrink, shrink);
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_shrink(RenderContext context, NumConstraint horiz, NumConstraint vert)
|
||||
public static RectConstraint c_shrink(ConstraintContext context, NumConstraint horiz, NumConstraint vert)
|
||||
{
|
||||
return c_shrink(context, horiz, vert, horiz, vert);
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_shrink(RenderContext context, final NumConstraint left, final NumConstraint top, final NumConstraint right, final NumConstraint bottom)
|
||||
public static RectConstraint c_shrink(ConstraintContext context, final NumConstraint left, final NumConstraint top, final NumConstraint right, final NumConstraint bottom)
|
||||
{
|
||||
return new RectConstraint(context) {
|
||||
|
||||
@Override
|
||||
public Rect getRect()
|
||||
{
|
||||
return context.getRect().shrink(left.getValue(), top.getValue(), right.getValue(), bottom.getValue());
|
||||
return getContext().getRect().shrink(left.getValue(), top.getValue(), right.getValue(), bottom.getValue());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_grow(RenderContext context, NumConstraint grow)
|
||||
public static RectConstraint c_grow(ConstraintContext context, NumConstraint grow)
|
||||
{
|
||||
return c_grow(context, grow, grow, grow, grow);
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_grow(RenderContext context, NumConstraint horiz, NumConstraint vert)
|
||||
public static RectConstraint c_grow(ConstraintContext context, NumConstraint horiz, NumConstraint vert)
|
||||
{
|
||||
return c_grow(context, horiz, vert, horiz, vert);
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_grow(RenderContext context, final NumConstraint left, final NumConstraint top, final NumConstraint right, final NumConstraint bottom)
|
||||
public static RectConstraint c_grow(ConstraintContext context, final NumConstraint left, final NumConstraint top, final NumConstraint right, final NumConstraint bottom)
|
||||
{
|
||||
return new RectConstraint(context) {
|
||||
|
||||
@Override
|
||||
public Rect getRect()
|
||||
{
|
||||
return context.getRect().grow(left.getValue(), top.getValue(), right.getValue(), bottom.getValue());
|
||||
return getContext().getRect().grow(left.getValue(), top.getValue(), right.getValue(), bottom.getValue());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_tile(RenderContext context, final int rows, final int cols, final int left, final int top)
|
||||
public static RectConstraint c_tile(ConstraintContext context, final int rows, final int cols, final int left, final int top)
|
||||
{
|
||||
return new RectConstraint(context) {
|
||||
|
||||
@@ -335,7 +335,7 @@ public class ConstraintFactory {
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_sizedBox(RenderContext context, final NumConstraint left, final NumConstraint bottom, final NumConstraint width, final NumConstraint height)
|
||||
public static RectConstraint c_sizedBox(ConstraintContext context, final NumConstraint left, final NumConstraint bottom, final NumConstraint width, final NumConstraint height)
|
||||
{
|
||||
return new RectConstraint(context) {
|
||||
|
||||
@@ -357,7 +357,7 @@ public class ConstraintFactory {
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_posBox(RenderContext context, final NumConstraint left, final NumConstraint bottom, final NumConstraint right, final NumConstraint top)
|
||||
public static RectConstraint c_posBox(ConstraintContext context, final NumConstraint left, final NumConstraint bottom, final NumConstraint right, final NumConstraint top)
|
||||
{
|
||||
return new RectConstraint(context) {
|
||||
|
||||
@@ -379,14 +379,14 @@ public class ConstraintFactory {
|
||||
}
|
||||
|
||||
|
||||
public static RectConstraint c_move(RenderContext context, final NumConstraint x, final NumConstraint y)
|
||||
public static RectConstraint c_move(ConstraintContext context, final NumConstraint x, final NumConstraint y)
|
||||
{
|
||||
return new RectConstraint(context) {
|
||||
|
||||
@Override
|
||||
public Rect getRect()
|
||||
{
|
||||
return context.getRect().add(x.getValue(), y.getValue());
|
||||
return getContext().getRect().add(x.getValue(), y.getValue());
|
||||
}
|
||||
};
|
||||
}
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
package mightypork.rogue.display.constraints;
|
||||
package mightypork.utils.math.constraints;
|
||||
|
||||
|
||||
/**
|
||||
@@ -8,7 +8,7 @@ package mightypork.rogue.display.constraints;
|
||||
*/
|
||||
public abstract class NumConstraint extends BaseConstraint {
|
||||
|
||||
public NumConstraint(RenderContext context) {
|
||||
public NumConstraint(ConstraintContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
+4
-4
@@ -1,17 +1,17 @@
|
||||
package mightypork.rogue.display.constraints;
|
||||
package mightypork.utils.math.constraints;
|
||||
|
||||
|
||||
import mightypork.utils.math.coord.Rect;
|
||||
|
||||
|
||||
/**
|
||||
* Constraint that provides a rect (RenderContext)
|
||||
* Constraint that provides a rect ({@link ConstraintContext})
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public abstract class RectConstraint extends BaseConstraint implements RenderContext {
|
||||
public abstract class RectConstraint extends BaseConstraint implements ConstraintContext {
|
||||
|
||||
public RectConstraint(RenderContext context) {
|
||||
public RectConstraint(ConstraintContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package mightypork.utils.math.constraints;
|
||||
|
||||
|
||||
/**
|
||||
* Can be assigned a context / changed context
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
public interface SettableContext {
|
||||
|
||||
/**
|
||||
* Assign a context
|
||||
*
|
||||
* @param context context
|
||||
*/
|
||||
public void setContext(ConstraintContext context);
|
||||
}
|
||||
Reference in New Issue
Block a user