Cleanup of annotations, fixed some bus logic.

v5stable
Ondřej Hruška 11 years ago
parent 6b69dabbb9
commit 6d7ee87ae6
  1. 16
      src/mightypork/rogue/App.java
  2. 27
      src/mightypork/rogue/MainLoop.java
  3. 4
      src/mightypork/rogue/Res.java
  4. 6
      src/mightypork/rogue/bus/events/ActionRequest.java
  5. 2
      src/mightypork/rogue/bus/events/KeyboardEvent.java
  6. 6
      src/mightypork/rogue/bus/events/MainLoopTaskRequest.java
  7. 2
      src/mightypork/rogue/bus/events/MouseButtonEvent.java
  8. 2
      src/mightypork/rogue/bus/events/MouseMotionEvent.java
  9. 6
      src/mightypork/rogue/bus/events/ResourceLoadRequest.java
  10. 2
      src/mightypork/rogue/bus/events/ScreenChangeEvent.java
  11. 6
      src/mightypork/rogue/bus/events/ScreenRequestEvent.java
  12. 4
      src/mightypork/rogue/gui/constraints/ColumnHolder.java
  13. 10
      src/mightypork/rogue/gui/constraints/ElementHolder.java
  14. 7
      src/mightypork/rogue/gui/constraints/PluggableRenderable.java
  15. 4
      src/mightypork/rogue/gui/constraints/RowHolder.java
  16. 2
      src/mightypork/rogue/gui/screens/LayeredScreen.java
  17. 2
      src/mightypork/rogue/gui/screens/Screen.java
  18. 2
      src/mightypork/rogue/gui/screens/ScreenLayer.java
  19. 6
      src/mightypork/rogue/gui/screens/ScreenRegistry.java
  20. 4
      src/mightypork/rogue/gui/screens/test_bouncyboxes/BouncyBox.java
  21. 4
      src/mightypork/rogue/gui/screens/test_bouncyboxes/LayerBouncyBoxes.java
  22. 2
      src/mightypork/rogue/gui/screens/test_bouncyboxes/ScreenTestBouncy.java
  23. 4
      src/mightypork/rogue/gui/screens/test_cat_sound/LayerFlyingCat.java
  24. 2
      src/mightypork/rogue/gui/screens/test_cat_sound/ScreenTestCat.java
  25. 2
      src/mightypork/rogue/gui/screens/test_font/ScreenTestFont.java
  26. 58
      src/mightypork/rogue/util/SlickLogRedirector.java
  27. 23
      src/mightypork/utils/control/bus/EventBus.java
  28. 4
      src/mightypork/utils/control/bus/EventChannel.java
  29. 16
      src/mightypork/utils/control/bus/events/Event.java
  30. 22
      src/mightypork/utils/control/bus/events/types/DelayedEvent.java
  31. 5
      src/mightypork/utils/control/bus/events/types/ImmediateEvent.java
  32. 16
      src/mightypork/utils/control/bus/events/types/SingleReceiverEvent.java
  33. 2
      src/mightypork/utils/math/constraints/SettableContext.java

@ -4,14 +4,13 @@ package mightypork.rogue;
import java.io.File; import java.io.File;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.util.ArrayList;
import java.util.logging.Level; import java.util.logging.Level;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import mightypork.rogue.bus.events.*; import mightypork.rogue.bus.events.*;
import mightypork.rogue.bus.events.ActionRequest.RequestType; import mightypork.rogue.bus.events.ActionRequest.RequestType;
import mightypork.rogue.gui.ScreenRegistry; import mightypork.rogue.gui.screens.ScreenRegistry;
import mightypork.rogue.gui.screens.test_bouncyboxes.ScreenTestBouncy; import mightypork.rogue.gui.screens.test_bouncyboxes.ScreenTestBouncy;
import mightypork.rogue.gui.screens.test_cat_sound.ScreenTestCat; import mightypork.rogue.gui.screens.test_cat_sound.ScreenTestCat;
import mightypork.rogue.gui.screens.test_font.ScreenTestFont; import mightypork.rogue.gui.screens.test_font.ScreenTestFont;
@ -181,18 +180,7 @@ public class App implements AppAccess {
* Prepare main loop * Prepare main loop
*/ */
Log.f1("Preparing main loop..."); Log.f1("Preparing main loop...");
final ArrayList<Runnable> loopTasks = new ArrayList<Runnable>(); mainLoop = new MainLoop(this, screens);
loopTasks.add(new Runnable() {
@Override
public void run()
{
screens.render();
}
});
mainLoop = new MainLoop(this, loopTasks);
} }

@ -1,16 +1,15 @@
package mightypork.rogue; package mightypork.rogue;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
import mightypork.rogue.bus.Subsystem; import mightypork.rogue.bus.Subsystem;
import mightypork.rogue.bus.events.ActionRequest; import mightypork.rogue.bus.events.ActionRequest;
import mightypork.rogue.bus.events.UpdateEvent;
import mightypork.rogue.bus.events.ActionRequest.RequestType; import mightypork.rogue.bus.events.ActionRequest.RequestType;
import mightypork.rogue.bus.events.MainLoopTaskRequest; import mightypork.rogue.bus.events.MainLoopTaskRequest;
import mightypork.rogue.bus.events.UpdateEvent;
import mightypork.rogue.render.Renderable;
import mightypork.rogue.tasks.TaskTakeScreenshot; import mightypork.rogue.tasks.TaskTakeScreenshot;
import mightypork.rogue.util.Utils; import mightypork.rogue.util.Utils;
import mightypork.utils.control.timing.TimerDelta; import mightypork.utils.control.timing.TimerDelta;
@ -19,13 +18,17 @@ import mightypork.utils.control.timing.TimerDelta;
public class MainLoop extends Subsystem implements ActionRequest.Listener, MainLoopTaskRequest.Listener { public class MainLoop extends Subsystem implements ActionRequest.Listener, MainLoopTaskRequest.Listener {
private final Queue<Runnable> taskQueue = new ConcurrentLinkedQueue<Runnable>(); private final Queue<Runnable> taskQueue = new ConcurrentLinkedQueue<Runnable>();
private final List<Runnable> regularTasks; private final Renderable renderable;
public MainLoop(App app, ArrayList<Runnable> loopTasks) { public MainLoop(App app, Renderable masterRenderable) {
super(app); super(app);
this.regularTasks = loopTasks; if (masterRenderable == null) {
throw new NullPointerException("Master renderable must not be null.");
}
this.renderable = masterRenderable;
} }
/** timer */ /** timer */
@ -38,19 +41,17 @@ public class MainLoop extends Subsystem implements ActionRequest.Listener, MainL
timer = new TimerDelta(); timer = new TimerDelta();
while (running) { while (running) {
Runnable r;
while ((r = taskQueue.poll()) != null) {
r.run();
}
disp().beginFrame(); disp().beginFrame();
bus().send(new UpdateEvent(timer.getDelta())); bus().send(new UpdateEvent(timer.getDelta()));
for (final Runnable r2 : regularTasks) { Runnable r;
r2.run(); while ((r = taskQueue.poll()) != null) {
r.run();
} }
renderable.render();
disp().endFrame(); disp().endFrame();
} }
} }

@ -9,10 +9,10 @@ import mightypork.rogue.loading.AsyncResourceLoader;
import mightypork.rogue.sound.SoundBank; import mightypork.rogue.sound.SoundBank;
import mightypork.rogue.sound.players.EffectPlayer; import mightypork.rogue.sound.players.EffectPlayer;
import mightypork.rogue.sound.players.LoopPlayer; import mightypork.rogue.sound.players.LoopPlayer;
import mightypork.rogue.textures.TextureBank;
import mightypork.rogue.textures.TxQuad;
import mightypork.rogue.textures.FilteredTexture.Filter; import mightypork.rogue.textures.FilteredTexture.Filter;
import mightypork.rogue.textures.FilteredTexture.Wrap; import mightypork.rogue.textures.FilteredTexture.Wrap;
import mightypork.rogue.textures.TextureBank;
import mightypork.rogue.textures.TxQuad;
import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.Texture;

@ -2,8 +2,7 @@ package mightypork.rogue.bus.events;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.QueuedEvent; import mightypork.utils.control.bus.events.types.SingleReceiverEvent;
import mightypork.utils.control.bus.events.types.SingularEvent;
/** /**
@ -11,8 +10,7 @@ import mightypork.utils.control.bus.events.types.SingularEvent;
* *
* @author MightyPork * @author MightyPork
*/ */
@SingularEvent @SingleReceiverEvent
@QueuedEvent
public class ActionRequest implements Event<ActionRequest.Listener> { public class ActionRequest implements Event<ActionRequest.Listener> {
private final RequestType type; private final RequestType type;

@ -2,7 +2,6 @@ package mightypork.rogue.bus.events;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.QueuedEvent;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
@ -12,7 +11,6 @@ import org.lwjgl.input.Keyboard;
* *
* @author MightyPork * @author MightyPork
*/ */
@QueuedEvent
public class KeyboardEvent implements Event<KeyboardEvent.Listener> { public class KeyboardEvent implements Event<KeyboardEvent.Listener> {
private final int key; private final int key;

@ -2,8 +2,7 @@ package mightypork.rogue.bus.events;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.QueuedEvent; import mightypork.utils.control.bus.events.types.SingleReceiverEvent;
import mightypork.utils.control.bus.events.types.SingularEvent;
/** /**
@ -11,8 +10,7 @@ import mightypork.utils.control.bus.events.types.SingularEvent;
* *
* @author MightyPork * @author MightyPork
*/ */
@SingularEvent @SingleReceiverEvent
@QueuedEvent
public class MainLoopTaskRequest implements Event<MainLoopTaskRequest.Listener> { public class MainLoopTaskRequest implements Event<MainLoopTaskRequest.Listener> {
private final Runnable task; private final Runnable task;

@ -2,7 +2,6 @@ package mightypork.rogue.bus.events;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.QueuedEvent;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
@ -11,7 +10,6 @@ import mightypork.utils.math.coord.Coord;
* *
* @author MightyPork * @author MightyPork
*/ */
@QueuedEvent
public class MouseButtonEvent implements Event<MouseButtonEvent.Listener> { public class MouseButtonEvent implements Event<MouseButtonEvent.Listener> {
public static final int BUTTON_LEFT = 0; public static final int BUTTON_LEFT = 0;

@ -2,7 +2,6 @@ package mightypork.rogue.bus.events;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.QueuedEvent;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
@ -11,7 +10,6 @@ import mightypork.utils.math.coord.Coord;
* *
* @author MightyPork * @author MightyPork
*/ */
@QueuedEvent
public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> { public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
private final Coord move; private final Coord move;

@ -3,8 +3,7 @@ package mightypork.rogue.bus.events;
import mightypork.rogue.loading.DeferredResource; import mightypork.rogue.loading.DeferredResource;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.QueuedEvent; import mightypork.utils.control.bus.events.types.SingleReceiverEvent;
import mightypork.utils.control.bus.events.types.SingularEvent;
/** /**
@ -12,8 +11,7 @@ import mightypork.utils.control.bus.events.types.SingularEvent;
* *
* @author MightyPork * @author MightyPork
*/ */
@SingularEvent @SingleReceiverEvent
@QueuedEvent
public class ResourceLoadRequest implements Event<ResourceLoadRequest.Listener> { public class ResourceLoadRequest implements Event<ResourceLoadRequest.Listener> {
private final DeferredResource resource; private final DeferredResource resource;

@ -2,7 +2,6 @@ package mightypork.rogue.bus.events;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.QueuedEvent;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
@ -11,7 +10,6 @@ import mightypork.utils.math.coord.Coord;
* *
* @author MightyPork * @author MightyPork
*/ */
@QueuedEvent
public class ScreenChangeEvent implements Event<ScreenChangeEvent.Listener> { public class ScreenChangeEvent implements Event<ScreenChangeEvent.Listener> {
private final boolean fullscreen; private final boolean fullscreen;

@ -2,8 +2,7 @@ package mightypork.rogue.bus.events;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.QueuedEvent; import mightypork.utils.control.bus.events.types.SingleReceiverEvent;
import mightypork.utils.control.bus.events.types.SingularEvent;
/** /**
@ -11,8 +10,7 @@ import mightypork.utils.control.bus.events.types.SingularEvent;
* *
* @author MightyPork * @author MightyPork
*/ */
@SingularEvent @SingleReceiverEvent
@QueuedEvent
public class ScreenRequestEvent implements Event<ScreenRequestEvent.Listener> { public class ScreenRequestEvent implements Event<ScreenRequestEvent.Listener> {
private final String scrName; private final String scrName;

@ -29,7 +29,7 @@ public class ColumnHolder extends ElementHolder {
* *
* @param elem * @param elem
*/ */
public void addRow(RenderableWithContext elem) public void addRow(PluggableRenderable elem)
{ {
if (elem == null) return; if (elem == null) return;
add(elem, c_column(null, cols, col++)); add(elem, c_column(null, cols, col++));
@ -37,7 +37,7 @@ public class ColumnHolder extends ElementHolder {
@Override @Override
public void remove(RenderableWithContext elem) public void remove(PluggableRenderable elem)
{ {
throw new UnsupportedOperationException("Can't remove from ColumnHolder."); throw new UnsupportedOperationException("Can't remove from ColumnHolder.");
} }

@ -13,14 +13,14 @@ import mightypork.utils.math.coord.Rect;
/** /**
* Bag for {@link RenderableWithContext} elements with constraints.<br> * Bag for {@link PluggableRenderable} elements with constraints.<br>
* Elements are exposed to {@link EventBus}. * Elements are exposed to {@link EventBus}.
* *
* @author MightyPork * @author MightyPork
*/ */
public class ElementHolder extends ChildClient implements ConstraintContext, RenderableWithContext { public class ElementHolder extends ChildClient implements ConstraintContext, PluggableRenderable {
private final LinkedList<RenderableWithContext> elements = new LinkedList<RenderableWithContext>(); private final LinkedList<PluggableRenderable> elements = new LinkedList<PluggableRenderable>();
private ConstraintContext context; private ConstraintContext context;
@ -65,7 +65,7 @@ public class ElementHolder extends ChildClient implements ConstraintContext, Ren
* @param constraint Constraint to be used for the element. It's context * @param constraint Constraint to be used for the element. It's context
* will be set to this {@link ElementHolder} * will be set to this {@link ElementHolder}
*/ */
public void add(RenderableWithContext elem, RectConstraint constraint) public void add(PluggableRenderable elem, RectConstraint constraint)
{ {
if (elem == null) return; if (elem == null) return;
@ -82,7 +82,7 @@ public class ElementHolder extends ChildClient implements ConstraintContext, Ren
* *
* @param elem * @param elem
*/ */
public void remove(RenderableWithContext elem) public void remove(PluggableRenderable elem)
{ {
if (elem == null) return; if (elem == null) return;
elements.remove(elem); elements.remove(elem);

@ -2,7 +2,6 @@ package mightypork.rogue.gui.constraints;
import mightypork.rogue.render.Renderable; import mightypork.rogue.render.Renderable;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.constraints.SettableContext; import mightypork.utils.math.constraints.SettableContext;
@ -11,8 +10,6 @@ import mightypork.utils.math.constraints.SettableContext;
* *
* @author MightyPork * @author MightyPork
*/ */
public interface RenderableWithContext extends Renderable, SettableContext { public interface PluggableRenderable extends Renderable, SettableContext {
// methods from both interfaces
@Override
void setContext(ConstraintContext context);
} }

@ -29,7 +29,7 @@ public class RowHolder extends ElementHolder {
* *
* @param elem * @param elem
*/ */
public void addRow(RenderableWithContext elem) public void addRow(PluggableRenderable elem)
{ {
if (elem == null) return; if (elem == null) return;
add(elem, c_row(null, rows, row++)); add(elem, c_row(null, rows, row++));
@ -37,7 +37,7 @@ public class RowHolder extends ElementHolder {
@Override @Override
public void remove(RenderableWithContext elem) public void remove(PluggableRenderable elem)
{ {
throw new UnsupportedOperationException("Can't remove from RowHolder."); throw new UnsupportedOperationException("Can't remove from RowHolder.");
} }

@ -1,4 +1,4 @@
package mightypork.rogue.gui; package mightypork.rogue.gui.screens;
import java.util.Collection; import java.util.Collection;

@ -1,4 +1,4 @@
package mightypork.rogue.gui; package mightypork.rogue.gui.screens;
import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL11.*;

@ -1,4 +1,4 @@
package mightypork.rogue.gui; package mightypork.rogue.gui.screens;
import mightypork.rogue.bus.ChildClient; import mightypork.rogue.bus.ChildClient;

@ -1,4 +1,4 @@
package mightypork.rogue.gui; package mightypork.rogue.gui.screens;
import java.util.HashMap; import java.util.HashMap;
@ -6,10 +6,11 @@ import java.util.HashMap;
import mightypork.rogue.AppAccess; import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.Subsystem; import mightypork.rogue.bus.Subsystem;
import mightypork.rogue.bus.events.ScreenRequestEvent; import mightypork.rogue.bus.events.ScreenRequestEvent;
import mightypork.rogue.render.Renderable;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
public class ScreenRegistry extends Subsystem implements ScreenRequestEvent.Listener { public class ScreenRegistry extends Subsystem implements ScreenRequestEvent.Listener, Renderable {
private final HashMap<String, Screen> screens = new HashMap<String, Screen>(); private final HashMap<String, Screen> screens = new HashMap<String, Screen>();
private Screen active = null; private Screen active = null;
@ -43,6 +44,7 @@ public class ScreenRegistry extends Subsystem implements ScreenRequestEvent.List
} }
@Override
public void render() public void render()
{ {
if (active != null) active.render(); if (active != null) active.render();

@ -5,7 +5,7 @@ import static mightypork.utils.math.constraints.ConstraintFactory.*;
import java.util.Random; import java.util.Random;
import mightypork.rogue.gui.constraints.RenderableWithContext; import mightypork.rogue.gui.constraints.PluggableRenderable;
import mightypork.rogue.render.Render; import mightypork.rogue.render.Render;
import mightypork.utils.control.interf.Updateable; import mightypork.utils.control.interf.Updateable;
import mightypork.utils.math.animation.AnimDouble; import mightypork.utils.math.animation.AnimDouble;
@ -17,7 +17,7 @@ import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
public class BouncyBox implements RenderableWithContext, Updateable, ConstraintContext { public class BouncyBox implements PluggableRenderable, Updateable, ConstraintContext {
private final Random rand = new Random(); private final Random rand = new Random();

@ -6,9 +6,9 @@ import static mightypork.utils.math.constraints.ConstraintFactory.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import mightypork.rogue.gui.Screen;
import mightypork.rogue.gui.ScreenLayer;
import mightypork.rogue.gui.constraints.RowHolder; import mightypork.rogue.gui.constraints.RowHolder;
import mightypork.rogue.gui.screens.Screen;
import mightypork.rogue.gui.screens.ScreenLayer;
import mightypork.utils.math.constraints.RectConstraint; import mightypork.utils.math.constraints.RectConstraint;

@ -3,7 +3,7 @@ package mightypork.rogue.gui.screens.test_bouncyboxes;
import mightypork.rogue.AppAccess; import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.events.ScreenRequestEvent; import mightypork.rogue.bus.events.ScreenRequestEvent;
import mightypork.rogue.gui.LayeredScreen; import mightypork.rogue.gui.screens.LayeredScreen;
import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.KeyStroke;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;

@ -7,8 +7,8 @@ import java.util.Random;
import mightypork.rogue.Res; import mightypork.rogue.Res;
import mightypork.rogue.bus.events.MouseButtonEvent; import mightypork.rogue.bus.events.MouseButtonEvent;
import mightypork.rogue.gui.Screen; import mightypork.rogue.gui.screens.Screen;
import mightypork.rogue.gui.ScreenLayer; import mightypork.rogue.gui.screens.ScreenLayer;
import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.render.Render; import mightypork.rogue.render.Render;
import mightypork.utils.control.interf.Updateable; import mightypork.utils.control.interf.Updateable;

@ -6,7 +6,7 @@ import mightypork.rogue.Res;
import mightypork.rogue.bus.events.ActionRequest; import mightypork.rogue.bus.events.ActionRequest;
import mightypork.rogue.bus.events.ActionRequest.RequestType; import mightypork.rogue.bus.events.ActionRequest.RequestType;
import mightypork.rogue.bus.events.ScreenRequestEvent; import mightypork.rogue.bus.events.ScreenRequestEvent;
import mightypork.rogue.gui.LayeredScreen; import mightypork.rogue.gui.screens.LayeredScreen;
import mightypork.rogue.input.KeyStroke; import mightypork.rogue.input.KeyStroke;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;

@ -4,7 +4,7 @@ package mightypork.rogue.gui.screens.test_font;
import mightypork.rogue.AppAccess; import mightypork.rogue.AppAccess;
import mightypork.rogue.Res; import mightypork.rogue.Res;
import mightypork.rogue.fonts.GLFont; import mightypork.rogue.fonts.GLFont;
import mightypork.rogue.gui.Screen; import mightypork.rogue.gui.screens.Screen;
import mightypork.rogue.render.Render; import mightypork.rogue.render.Render;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;

@ -0,0 +1,58 @@
package mightypork.rogue.util;
import mightypork.utils.logging.LogInstance;
import org.newdawn.slick.util.LogSystem;
public class SlickLogRedirector implements LogSystem {
LogInstance l;
public SlickLogRedirector(LogInstance log) {
this.l = log;
}
@Override
public void error(String msg, Throwable e)
{
l.e(msg, e);
}
@Override
public void error(Throwable e)
{
l.e(e);
}
@Override
public void error(String msg)
{
l.e(msg);
}
@Override
public void warn(String msg)
{
l.w(msg);
}
@Override
public void warn(String msg, Throwable e)
{
l.e(msg, e);
}
@Override
public void info(String msg)
{
l.i(msg);
}
@Override
public void debug(String msg)
{
l.f3(msg);
}
}

@ -6,9 +6,9 @@ import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.DelayedEvent;
import mightypork.utils.control.bus.events.types.ImmediateEvent; import mightypork.utils.control.bus.events.types.ImmediateEvent;
import mightypork.utils.control.bus.events.types.QueuedEvent; import mightypork.utils.control.bus.events.types.SingleReceiverEvent;
import mightypork.utils.control.bus.events.types.SingularEvent;
import mightypork.utils.control.interf.Destroyable; import mightypork.utils.control.interf.Destroyable;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
@ -27,7 +27,7 @@ final public class EventBus implements Destroyable {
private final BufferedHashSet<Object> clients = new BufferedHashSet<Object>(); private final BufferedHashSet<Object> clients = new BufferedHashSet<Object>();
/** Messages queued for delivery */ /** Messages queued for delivery */
private final DelayQueue<DelayedEvent> sendQueue = new DelayQueue<DelayedEvent>(); private final DelayQueue<DelayQueueEntry> sendQueue = new DelayQueue<DelayQueueEntry>();
/** Queue polling thread */ /** Queue polling thread */
private final QueuePollingThread busThread; private final QueuePollingThread busThread;
@ -110,8 +110,9 @@ final public class EventBus implements Destroyable {
{ {
assertLive(); assertLive();
if(event.getClass().isAnnotationPresent(QueuedEvent.class)) { DelayedEvent adelay = event.getClass().getAnnotation(DelayedEvent.class);
sendQueued(event); if (adelay != null) {
sendDelayed(event, adelay.delay());
return; return;
} }
@ -120,7 +121,7 @@ final public class EventBus implements Destroyable {
return; return;
} }
dispatch(event); sendQueued(event);
} }
@ -147,7 +148,7 @@ final public class EventBus implements Destroyable {
{ {
assertLive(); assertLive();
final DelayedEvent dm = new DelayedEvent(delay, event); final DelayQueueEntry dm = new DelayQueueEntry(delay, event);
if (logSending) Log.f3("<bus> Q " + Log.str(event) + ", t = +" + delay + "s"); if (logSending) Log.f3("<bus> Q " + Log.str(event) + ", t = +" + delay + "s");
@ -190,7 +191,7 @@ final public class EventBus implements Destroyable {
boolean sent = false; boolean sent = false;
boolean accepted = false; boolean accepted = false;
final boolean singular = event.getClass().isAnnotationPresent(SingularEvent.class); final boolean singular = event.getClass().isAnnotationPresent(SingleReceiverEvent.class);
for (final EventChannel<?, ?> b : channels) { for (final EventChannel<?, ?> b : channels) {
if (b.canBroadcast(event)) { if (b.canBroadcast(event)) {
@ -253,13 +254,13 @@ final public class EventBus implements Destroyable {
return false; return false;
} }
private class DelayedEvent implements Delayed { private class DelayQueueEntry implements Delayed {
private final long due; private final long due;
private Event<?> evt = null; private Event<?> evt = null;
public DelayedEvent(double seconds, Event<?> event) { public DelayQueueEntry(double seconds, Event<?> event) {
super(); super();
this.due = System.currentTimeMillis() + (long) (seconds * 1000); this.due = System.currentTimeMillis() + (long) (seconds * 1000);
this.evt = event; this.evt = event;
@ -300,7 +301,7 @@ final public class EventBus implements Destroyable {
@Override @Override
public void run() public void run()
{ {
DelayedEvent evt; DelayQueueEntry evt;
while (!stopped) { while (!stopped) {
evt = null; evt = null;

@ -7,7 +7,7 @@ import java.util.HashSet;
import mightypork.utils.control.bus.clients.DelegatingClient; import mightypork.utils.control.bus.clients.DelegatingClient;
import mightypork.utils.control.bus.clients.ToggleableClient; import mightypork.utils.control.bus.clients.ToggleableClient;
import mightypork.utils.control.bus.events.Event; import mightypork.utils.control.bus.events.Event;
import mightypork.utils.control.bus.events.types.SingularEvent; import mightypork.utils.control.bus.events.types.SingleReceiverEvent;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
@ -68,7 +68,7 @@ final public class EventChannel<EVENT extends Event<CLIENT>, CLIENT> {
private boolean doBroadcast(final EVENT event, final Collection<Object> clients, final Collection<Object> processed) private boolean doBroadcast(final EVENT event, final Collection<Object> clients, final Collection<Object> processed)
{ {
boolean sent = false; boolean sent = false;
final boolean singular = event.getClass().isAnnotationPresent(SingularEvent.class); final boolean singular = event.getClass().isAnnotationPresent(SingleReceiverEvent.class);
for (final Object client : clients) { for (final Object client : clients) {

@ -1,8 +1,24 @@
package mightypork.utils.control.bus.events; package mightypork.utils.control.bus.events;
import mightypork.utils.control.bus.events.types.DelayedEvent;
import mightypork.utils.control.bus.events.types.ImmediateEvent;
import mightypork.utils.control.bus.events.types.SingleReceiverEvent;
/** /**
* <p>
* Something that can be handled by HANDLER. * Something that can be handled by HANDLER.
* </p>
* <p>
* Can be annotated as {@link SingleReceiverEvent} to be delivered once only,
* and {@link DelayedEvent} or {@link ImmediateEvent} to specify default sending
* mode.
* </p>
* <p>
* Default sending mode (if not changed by annotations) is <i>queued</i> with
* zero delay.
* </p>
* *
* @author MightyPork * @author MightyPork
* @param <HANDLER> handler type * @param <HANDLER> handler type

@ -0,0 +1,22 @@
package mightypork.utils.control.bus.events.types;
import java.lang.annotation.*;
/**
* Event that should be queued with given delay (default: 0);
*
* @author MightyPork
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@Documented
public @interface DelayedEvent {
/**
* @return event dispatch delay [seconds]
*/
double delay() default 0;
}

@ -5,8 +5,7 @@ import java.lang.annotation.*;
/** /**
* Event that is handled by only single client, and then discarded (ie. only one * Event that should not be queued.
* client receives it when it's broadcasted).
* *
* @author MightyPork * @author MightyPork
*/ */
@ -14,4 +13,4 @@ import java.lang.annotation.*;
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Inherited @Inherited
@Documented @Documented
public @interface SingularEvent {} public @interface ImmediateEvent {}

@ -0,0 +1,16 @@
package mightypork.utils.control.bus.events.types;
import java.lang.annotation.*;
/**
* Handled only by the first client, then discarded.
*
* @author MightyPork
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@Documented
public @interface SingleReceiverEvent {}

@ -13,5 +13,5 @@ public interface SettableContext {
* *
* @param context context * @param context context
*/ */
public void setContext(ConstraintContext context); void setContext(ConstraintContext context);
} }

Loading…
Cancel
Save