From 54b1b3bb1cc3e10343ad2163050f24b8ebaa9ce9 Mon Sep 17 00:00:00 2001 From: yugecin Date: Sun, 15 Jan 2017 22:46:09 +0100 Subject: [PATCH] use EventBus instead of ResolutionChangeListener --- .../opsudance/core/DisplayContainer.java | 19 +++++++------------ .../opsudance/core/state/BaseOpsuState.java | 9 +++++---- .../ResolutionChangedEvent.java} | 12 +++++++++--- .../opsudance/kernel/OpsuDanceModule.java | 3 +++ 4 files changed, 24 insertions(+), 19 deletions(-) rename src/yugecin/opsudance/{core/ResolutionChangeListener.java => events/ResolutionChangedEvent.java} (76%) diff --git a/src/yugecin/opsudance/core/DisplayContainer.java b/src/yugecin/opsudance/core/DisplayContainer.java index 7a49b91d..78b030ee 100644 --- a/src/yugecin/opsudance/core/DisplayContainer.java +++ b/src/yugecin/opsudance/core/DisplayContainer.java @@ -30,12 +30,12 @@ import org.newdawn.slick.opengl.InternalTextureLoader; import org.newdawn.slick.opengl.renderer.Renderer; import org.newdawn.slick.opengl.renderer.SGL; import org.newdawn.slick.util.Log; +import yugecin.opsudance.core.events.EventBus; import yugecin.opsudance.errorhandling.ErrorDumpable; +import yugecin.opsudance.events.ResolutionChangedEvent; import yugecin.opsudance.utils.GLHelper; import java.io.StringWriter; -import java.util.LinkedList; -import java.util.List; import static yugecin.opsudance.kernel.Entrypoint.sout; @@ -46,9 +46,10 @@ public class DisplayContainer implements ErrorDumpable { private static SGL GL = Renderer.get(); + public final EventBus eventBus; public final Demux demux; + private final DisplayMode nativeDisplayMode; - private final List resolutionChangeListeners; private Graphics graphics; private Input input; @@ -70,19 +71,15 @@ public class DisplayContainer implements ErrorDumpable { private String glVendor; @Inject - public DisplayContainer(Demux demux) { + public DisplayContainer(Demux demux, EventBus eventBus) { this.demux = demux; + this.eventBus = eventBus; this.nativeDisplayMode = Display.getDisplayMode(); - this.resolutionChangeListeners = new LinkedList<>(); targetRenderInterval = 16; // ~60 fps targetBackgroundRenderInterval = 41; // ~24 fps lastFrame = getTime(); } - public void addResolutionChangeListener(ResolutionChangeListener listener) { - resolutionChangeListeners.add(listener); - } - public void run() throws LWJGLException { while(!(Display.isCloseRequested() && demux.onCloseRequest())) { delta = getDelta(); @@ -170,9 +167,7 @@ public class DisplayContainer implements ErrorDumpable { initGL(); - for (ResolutionChangeListener resolutionChangeListener : resolutionChangeListeners) { - resolutionChangeListener.onDisplayResolutionChanged(width, height); - } + eventBus.post(new ResolutionChangedEvent(this.width, this.height)); if (displayMode.getBitsPerPixel() == 16) { InternalTextureLoader.get().set16BitMode(); diff --git a/src/yugecin/opsudance/core/state/BaseOpsuState.java b/src/yugecin/opsudance/core/state/BaseOpsuState.java index 2d6e7755..3777ba3e 100644 --- a/src/yugecin/opsudance/core/state/BaseOpsuState.java +++ b/src/yugecin/opsudance/core/state/BaseOpsuState.java @@ -18,11 +18,12 @@ package yugecin.opsudance.core.state; import yugecin.opsudance.core.DisplayContainer; -import yugecin.opsudance.core.ResolutionChangeListener; +import yugecin.opsudance.core.events.EventListener; +import yugecin.opsudance.events.ResolutionChangedEvent; import java.io.StringWriter; -public abstract class BaseOpsuState implements OpsuState, ResolutionChangeListener { +public abstract class BaseOpsuState implements OpsuState, EventListener { protected final DisplayContainer displayContainer; @@ -34,14 +35,14 @@ public abstract class BaseOpsuState implements OpsuState, ResolutionChangeListen public BaseOpsuState(DisplayContainer displayContainer) { this.displayContainer = displayContainer; - displayContainer.addResolutionChangeListener(this); + displayContainer.eventBus.subscribe(ResolutionChangedEvent.class, this); } protected void revalidate() { } @Override - public void onDisplayResolutionChanged(int width, int height) { + public void onEvent(ResolutionChangedEvent event) { if (isCurrentState) { revalidate(); return; diff --git a/src/yugecin/opsudance/core/ResolutionChangeListener.java b/src/yugecin/opsudance/events/ResolutionChangedEvent.java similarity index 76% rename from src/yugecin/opsudance/core/ResolutionChangeListener.java rename to src/yugecin/opsudance/events/ResolutionChangedEvent.java index 9c9b30f8..6aa91f23 100644 --- a/src/yugecin/opsudance/core/ResolutionChangeListener.java +++ b/src/yugecin/opsudance/events/ResolutionChangedEvent.java @@ -15,10 +15,16 @@ * You should have received a copy of the GNU General Public License * along with opsu!dance. If not, see . */ -package yugecin.opsudance.core; +package yugecin.opsudance.events; -public interface ResolutionChangeListener { +public class ResolutionChangedEvent { - void onDisplayResolutionChanged(int width, int height); + public final int width; + public final int height; + + public ResolutionChangedEvent(int width, int height) { + this.width = width; + this.height = height; + } } diff --git a/src/yugecin/opsudance/kernel/OpsuDanceModule.java b/src/yugecin/opsudance/kernel/OpsuDanceModule.java index 19897f19..ce3950d7 100644 --- a/src/yugecin/opsudance/kernel/OpsuDanceModule.java +++ b/src/yugecin/opsudance/kernel/OpsuDanceModule.java @@ -21,6 +21,7 @@ import com.google.inject.AbstractModule; import yugecin.opsudance.PreStartupInitializer; import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.Demux; +import yugecin.opsudance.core.events.EventBus; import yugecin.opsudance.core.state.transitions.EmptyTransitionState; import yugecin.opsudance.errorhandling.ErrorHandler; import yugecin.opsudance.states.EmptyRedState; @@ -31,6 +32,8 @@ import yugecin.opsudance.core.state.transitions.FadeOutTransitionState; public class OpsuDanceModule extends AbstractModule { protected void configure() { + bind(EventBus.class).asEagerSingleton(); + bind(InstanceContainer.class).to(InstanceResolver.class); bind(PreStartupInitializer.class).asEagerSingleton(); bind(Demux.class).asEagerSingleton();