overhaul event system

This commit is contained in:
yugecin
2017-05-26 21:32:55 +02:00
parent b8dd507dc5
commit 1df25520e4
36 changed files with 354 additions and 324 deletions

View File

@@ -39,15 +39,14 @@ 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.core.errorhandling.ErrorDumpable;
import yugecin.opsudance.core.events.EventListener;
import yugecin.opsudance.core.state.OpsuState;
import yugecin.opsudance.core.state.specialstates.BarNotificationState;
import yugecin.opsudance.core.state.specialstates.BubbleNotificationState;
import yugecin.opsudance.core.state.specialstates.BubNotifState;
import yugecin.opsudance.core.state.specialstates.FpsRenderState;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.ResolutionOrSkinChangedEvent;
import yugecin.opsudance.events.BubNotifListener;
import yugecin.opsudance.events.ResolutionChangedListener;
import yugecin.opsudance.events.SkinChangedListener;
import yugecin.opsudance.utils.GLHelper;
import java.io.StringWriter;
@@ -59,13 +58,13 @@ import static yugecin.opsudance.options.Options.*;
/**
* based on org.newdawn.slick.AppGameContainer
*/
public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListener {
public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListener, ResolutionChangedListener, SkinChangedListener {
private static SGL GL = Renderer.get();
private FpsRenderState fpsState;
private BarNotificationState barNotifState;
private BubbleNotificationState bubNotifState;
private BubNotifState bubNotifState;
private OpsuState state;
@@ -149,13 +148,8 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
this.cursor = new Cursor();
drawCursor = true;
EventBus.subscribe(ResolutionOrSkinChangedEvent.class, new EventListener<ResolutionOrSkinChangedEvent>() {
@Override
public void onEvent(ResolutionOrSkinChangedEvent event) {
destroyImages();
reinit();
}
});
ResolutionChangedListener.EVENT.addListener(this);
SkinChangedListener.EVENT.addListener(this);
this.nativeDisplayMode = Display.getDisplayMode();
targetBackgroundRenderInterval = 41; // ~24 fps
@@ -164,6 +158,18 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
renderDelta = 1;
}
@Override
public void onResolutionChanged(int w, int h) {
destroyImages();
reinit();
}
@Override
public void onSkinChanged(String stringName) {
destroyImages();
reinit();
}
private void reinit() {
// this used to be in Utils.init
// TODO find a better place for this?
@@ -205,7 +211,7 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
state.enter();
fpsState = new FpsRenderState();
bubNotifState = new BubbleNotificationState();
bubNotifState = new BubNotifState();
barNotifState = new BarNotificationState();
}
@@ -334,13 +340,15 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
return true;
}
if (DownloadList.get().hasActiveDownloads()) {
EventBus.post(new BubbleNotificationEvent(DownloadList.EXIT_CONFIRMATION, BubbleNotificationEvent.COMMONCOLOR_PURPLE));
BubNotifListener.EVENT.make().onBubNotif(DownloadList.EXIT_CONFIRMATION,
BubNotifListener.COMMONCOLOR_RED);
exitRequested = false;
exitconfirmation = System.currentTimeMillis();
return false;
}
if (updater.getStatus() == Updater.Status.UPDATE_DOWNLOADING) {
EventBus.post(new BubbleNotificationEvent(Updater.EXIT_CONFIRMATION, BubbleNotificationEvent.COMMONCOLOR_PURPLE));
BubNotifListener.EVENT.make().onBubNotif(Updater.EXIT_CONFIRMATION,
BubNotifListener.COMMONCOLOR_PURPLE);
exitRequested = false;
exitconfirmation = System.currentTimeMillis();
return false;
@@ -379,7 +387,8 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
try {
setDisplayMode(width, height, OPTION_FULLSCREEN.state);
} catch (Exception e) {
EventBus.post(new BubbleNotificationEvent("Failed to change resolution", BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif("Failed to change resolution",
BubNotifListener.COMMONCOLOR_RED);
Log.error("Failed to set display mode.", e);
}
}
@@ -399,8 +408,9 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
displayMode = new DisplayMode(width, height);
if (fullscreen) {
fullscreen = false;
Log.warn("could not find fullscreen displaymode for " + width + "x" + height);
EventBus.post(new BubbleNotificationEvent("Fullscreen mode is not supported for " + width + "x" + height, BubbleNotificationEvent.COLOR_ORANGE));
String msg = String.format("Fullscreen mode is not supported for %sx%s", width, height);
Log.warn(msg);
BubNotifListener.EVENT.make().onBubNotif(msg, BubNotifListener.COLOR_ORANGE);
}
}
@@ -436,7 +446,7 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen
GameImage.init(width, height);
Fonts.init();
EventBus.post(new ResolutionOrSkinChangedEvent(null, width, height));
ResolutionChangedListener.EVENT.make().onResolutionChanged(width, height);
}
public void resetCursor() {

View File

@@ -17,38 +17,37 @@
*/
package yugecin.opsudance.core.events;
import java.util.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.LinkedList;
@SuppressWarnings("unchecked")
public class EventBus {
public class Event<T> {
private EventBus() {
private final Class<T> type;
private final LinkedList<T> listeners;
public Event(Class<T> type) {
this.type = type;
this.listeners = new LinkedList<>();
}
private static final List<Subscriber> subscribers = new LinkedList<>();
public static <T> void subscribe(Class<T> eventType, EventListener<T> eventListener) {
subscribers.add(new Subscriber<>(eventType, eventListener));
public void addListener(T listener) {
this.listeners.add(listener);
}
public static void post(Object event) {
for (Subscriber s : subscribers) {
if (s.eventType.isInstance(event)) {
s.listener.onEvent(event);
}
}
}
private static class Subscriber<T> {
private final Class<T> eventType;
private final EventListener<T> listener;
private Subscriber(Class<T> eventType, EventListener<T> listener) {
this.eventType = eventType;
this.listener = listener;
}
public T make() {
return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
for (T listener : listeners) {
method.invoke(listener, args);
}
return null;
}
});
}
}

View File

@@ -1,24 +0,0 @@
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2017 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.core.events;
public interface EventListener<T> {
void onEvent(T event);
}

View File

@@ -25,7 +25,7 @@ import itdelatrisu.opsu.replay.ReplayImporter;
import itdelatrisu.opsu.states.*;
import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.state.specialstates.BarNotificationState;
import yugecin.opsudance.core.state.specialstates.BubbleNotificationState;
import yugecin.opsudance.core.state.specialstates.BubNotifState;
import yugecin.opsudance.core.state.specialstates.FpsRenderState;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.options.Configuration;
@@ -52,7 +52,7 @@ public class OpsuDanceInjector extends Injector {
bind(FpsRenderState.class).asEagerSingleton();
bind(BarNotificationState.class).asEagerSingleton();
bind(BubbleNotificationState.class).asEagerSingleton();
bind(BubNotifState.class).asEagerSingleton();
bind(GameObjectRenderer.class).asEagerSingleton();

View File

@@ -21,17 +21,15 @@ import itdelatrisu.opsu.states.Game;
import itdelatrisu.opsu.ui.UI;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.events.EventListener;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.ResolutionOrSkinChangedEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.events.ResolutionChangedListener;
import java.io.StringWriter;
import static yugecin.opsudance.options.Options.*;
import static yugecin.opsudance.core.InstanceContainer.*;
public abstract class BaseOpsuState implements OpsuState, EventListener<ResolutionOrSkinChangedEvent> {
public abstract class BaseOpsuState implements OpsuState, ResolutionChangedListener {
/**
* state is dirty when resolution or skin changed but hasn't rendered yet
@@ -40,7 +38,7 @@ public abstract class BaseOpsuState implements OpsuState, EventListener<Resoluti
private boolean isCurrentState;
public BaseOpsuState() {
EventBus.subscribe(ResolutionOrSkinChangedEvent.class, this);
ResolutionChangedListener.EVENT.addListener(this);
}
protected void revalidate() {
@@ -59,7 +57,7 @@ public abstract class BaseOpsuState implements OpsuState, EventListener<Resoluti
}
@Override
public void onEvent(ResolutionOrSkinChangedEvent event) {
public void onResolutionChanged(int w, int h) {
if (isCurrentState) {
revalidate();
return;
@@ -95,7 +93,8 @@ public abstract class BaseOpsuState implements OpsuState, EventListener<Resoluti
public boolean keyReleased(int key, char c) {
if (key == Input.KEY_F7) {
OPTION_TARGET_FPS.clickListItem((targetFPSIndex + 1) % targetFPS.length);
EventBus.post(new BarNotificationEvent(String.format("Frame limiter: %s", OPTION_TARGET_FPS.getValueString())));
BarNotifListener.EVENT.make().onBarNotif(String.format("Frame limiter: %s",
OPTION_TARGET_FPS.getValueString()));
return true;
}
if (key == Input.KEY_F10) {

View File

@@ -21,16 +21,14 @@ import itdelatrisu.opsu.ui.Fonts;
import itdelatrisu.opsu.ui.animations.AnimationEquation;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.events.EventListener;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.ResolutionOrSkinChangedEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.events.ResolutionChangedListener;
import java.util.List;
import static yugecin.opsudance.core.InstanceContainer.displayContainer;
public class BarNotificationState implements EventListener<BarNotificationEvent> {
public class BarNotificationState implements BarNotifListener, ResolutionChangedListener {
private final int IN_TIME = 200;
private final int DISPLAY_TIME = 5700 + IN_TIME;
@@ -53,16 +51,8 @@ public class BarNotificationState implements EventListener<BarNotificationEvent>
this.bgcol = new Color(Color.black);
this.textCol = new Color(Color.white);
this.timeShown = TOTAL_TIME;
EventBus.subscribe(BarNotificationEvent.class, this);
EventBus.subscribe(ResolutionOrSkinChangedEvent.class, new EventListener<ResolutionOrSkinChangedEvent>() {
@Override
public void onEvent(ResolutionOrSkinChangedEvent event) {
if (timeShown >= TOTAL_TIME) {
return;
}
calculatePosition();
}
});
BarNotifListener.EVENT.addListener(this);
ResolutionChangedListener.EVENT.addListener(this);
}
public void render(Graphics g) {
@@ -107,10 +97,18 @@ public class BarNotificationState implements EventListener<BarNotificationEvent>
}
@Override
public void onEvent(BarNotificationEvent event) {
this.message = event.message;
public void onBarNotif(String message) {
this.message = message;
calculatePosition();
timeShown = 0;
}
@Override
public void onResolutionChanged(int w, int h) {
if (timeShown >= TOTAL_TIME) {
return;
}
calculatePosition();
}
}

View File

@@ -21,10 +21,9 @@ import itdelatrisu.opsu.ui.Fonts;
import itdelatrisu.opsu.ui.animations.AnimationEquation;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.events.EventListener;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.ResolutionOrSkinChangedEvent;
import yugecin.opsudance.events.BubNotifListener;
import yugecin.opsudance.events.ResolutionChangedListener;
import yugecin.opsudance.events.SkinChangedListener;
import java.util.LinkedList;
import java.util.List;
@@ -32,7 +31,7 @@ import java.util.ListIterator;
import static yugecin.opsudance.core.InstanceContainer.displayContainer;
public class BubbleNotificationState implements EventListener<BubbleNotificationEvent> {
public class BubNotifState implements BubNotifListener, ResolutionChangedListener, SkinChangedListener {
public static final int IN_TIME = 633;
public static final int DISPLAY_TIME = 7000 + IN_TIME;
@@ -44,16 +43,10 @@ public class BubbleNotificationState implements EventListener<BubbleNotification
private int addAnimationTime;
private int addAnimationHeight;
public BubbleNotificationState() {
public BubNotifState() {
this.bubbles = new LinkedList<>();
this.addAnimationTime = IN_TIME;
EventBus.subscribe(BubbleNotificationEvent.class, this);
EventBus.subscribe(ResolutionOrSkinChangedEvent.class, new EventListener<ResolutionOrSkinChangedEvent>() {
@Override
public void onEvent(ResolutionOrSkinChangedEvent event) {
calculatePositions();
}
});
BubNotifListener.EVENT.addListener(this);
}
public void render(Graphics g) {
@@ -129,9 +122,9 @@ public class BubbleNotificationState implements EventListener<BubbleNotification
}
@Override
public void onEvent(BubbleNotificationEvent event) {
public void onBubNotif(String message, Color borderColor) {
finishAddAnimation();
Notification newBubble = new Notification(event.message, event.borderColor);
Notification newBubble = new Notification(message, borderColor);
bubbles.add(0, newBubble);
addAnimationTime = 0;
addAnimationHeight = newBubble.height + Notification.paddingY;
@@ -143,6 +136,16 @@ public class BubbleNotificationState implements EventListener<BubbleNotification
}
}
@Override
public void onResolutionChanged(int w, int h) {
calculatePositions();
}
@Override
public void onSkinChanged(String stringName) {
calculatePositions();
}
private static class Notification {
private final static int HOVER_ANIM_TIME = 150;
@@ -204,7 +207,7 @@ public class BubbleNotificationState implements EventListener<BubbleNotification
Fonts.SMALLBOLD.drawString(x + fontPaddingX, y, line, textColor);
y += lineHeight;
}
return timeShown > BubbleNotificationState.TOTAL_TIME;
return timeShown > BubNotifState.TOTAL_TIME;
}
private void processAnimations(boolean mouseHovered, int delta) {
@@ -217,17 +220,17 @@ public class BubbleNotificationState implements EventListener<BubbleNotification
borderColor.r = targetBorderColor.r + (0.977f - targetBorderColor.r) * hoverProgress;
borderColor.g = targetBorderColor.g + (0.977f - targetBorderColor.g) * hoverProgress;
borderColor.b = targetBorderColor.b + (0.977f - targetBorderColor.b) * hoverProgress;
if (timeShown < BubbleNotificationState.IN_TIME) {
float progress = (float) timeShown / BubbleNotificationState.IN_TIME;
if (timeShown < BubNotifState.IN_TIME) {
float progress = (float) timeShown / BubNotifState.IN_TIME;
this.x = finalX + (int) ((1 - AnimationEquation.OUT_BACK.calc(progress)) * width / 2);
textColor.a = borderColor.a = bgcol.a = progress;
bgcol.a = borderColor.a * 0.8f;
return;
}
x = Notification.finalX;
if (timeShown > BubbleNotificationState.DISPLAY_TIME) {
if (timeShown > BubNotifState.DISPLAY_TIME) {
isFading = true;
float progress = (float) (timeShown - BubbleNotificationState.DISPLAY_TIME) / BubbleNotificationState.OUT_TIME;
float progress = (float) (timeShown - BubNotifState.DISPLAY_TIME) / BubNotifState.OUT_TIME;
textColor.a = borderColor.a = 1f - progress;
bgcol.a = borderColor.a * 0.8f;
}
@@ -235,7 +238,7 @@ public class BubbleNotificationState implements EventListener<BubbleNotification
private boolean mouseReleased(int x, int y) {
if (!isFading && isMouseHovered(x, y)) {
timeShown = BubbleNotificationState.DISPLAY_TIME;
timeShown = BubNotifState.DISPLAY_TIME;
return true;
}
return false;

View File

@@ -20,15 +20,13 @@ package yugecin.opsudance.core.state.specialstates;
import itdelatrisu.opsu.ui.Fonts;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.events.EventListener;
import yugecin.opsudance.events.ResolutionOrSkinChangedEvent;
import yugecin.opsudance.events.ResolutionChangedListener;
import yugecin.opsudance.utils.FPSMeter;
import static yugecin.opsudance.options.Options.*;
import static yugecin.opsudance.core.InstanceContainer.displayContainer;
public class FpsRenderState implements EventListener<ResolutionOrSkinChangedEvent> {
public class FpsRenderState implements ResolutionChangedListener {
private final static Color GREEN = new Color(171, 218, 25);
private final static Color ORANGE = new Color(255, 204, 34);
@@ -44,7 +42,7 @@ public class FpsRenderState implements EventListener<ResolutionOrSkinChangedEven
public FpsRenderState() {
fpsMeter = new FPSMeter(10);
upsMeter = new FPSMeter(10);
EventBus.subscribe(ResolutionOrSkinChangedEvent.class, this);
ResolutionChangedListener.EVENT.addListener(this);
}
public void update() {
@@ -91,7 +89,7 @@ public class FpsRenderState implements EventListener<ResolutionOrSkinChangedEven
}
@Override
public void onEvent(ResolutionOrSkinChangedEvent event) {
public void onResolutionChanged(int w, int h) {
singleHeight = Fonts.SMALL.getLineHeight();
x = displayContainer.width - 3;
y = displayContainer.height - 3 - singleHeight - 10;