From 9da723c50bc585940b99513c2923df9f2840e5c9 Mon Sep 17 00:00:00 2001 From: yugecin Date: Tue, 17 Jan 2017 11:25:36 +0100 Subject: [PATCH] simple bar notifications --- .../opsudance/core/DisplayContainer.java | 4 + .../core/inject/OpsuDanceInjector.java | 2 + .../specialstates/BarNotificationState.java | 86 +++++++++++++++++++ .../events/BarNotificationEvent.java | 28 ++++++ .../opsudance/states/EmptyRedState.java | 2 + 5 files changed, 122 insertions(+) create mode 100644 src/yugecin/opsudance/core/state/specialstates/BarNotificationState.java create mode 100644 src/yugecin/opsudance/events/BarNotificationEvent.java diff --git a/src/yugecin/opsudance/core/DisplayContainer.java b/src/yugecin/opsudance/core/DisplayContainer.java index 2cb71121..1e918fc3 100644 --- a/src/yugecin/opsudance/core/DisplayContainer.java +++ b/src/yugecin/opsudance/core/DisplayContainer.java @@ -37,6 +37,7 @@ import yugecin.opsudance.core.events.EventBus; import yugecin.opsudance.core.errorhandling.ErrorDumpable; import yugecin.opsudance.core.inject.InstanceContainer; import yugecin.opsudance.core.state.OpsuState; +import yugecin.opsudance.core.state.specialstates.BarNotificationState; import yugecin.opsudance.core.state.specialstates.FpsRenderState; import yugecin.opsudance.core.state.transitions.*; import yugecin.opsudance.events.ResolutionChangedEvent; @@ -57,6 +58,7 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen private final InstanceContainer instanceContainer; private FpsRenderState fpsState; + private BarNotificationState barNotifState; private TransitionState outTransitionState; private TransitionState inTransitionState; @@ -123,6 +125,7 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen state.enter(); fpsState = instanceContainer.provide(FpsRenderState.class); + barNotifState = instanceContainer.provide(BarNotificationState.class); } @@ -155,6 +158,7 @@ public class DisplayContainer implements ErrorDumpable, KeyListener, MouseListen state.preRenderUpdate(timeSinceLastRender); state.render(graphics); fpsState.render(graphics); + barNotifState.render(graphics, timeSinceLastRender); realRenderInterval = timeSinceLastRender; timeSinceLastRender = 0; diff --git a/src/yugecin/opsudance/core/inject/OpsuDanceInjector.java b/src/yugecin/opsudance/core/inject/OpsuDanceInjector.java index 525e8fae..9a772786 100644 --- a/src/yugecin/opsudance/core/inject/OpsuDanceInjector.java +++ b/src/yugecin/opsudance/core/inject/OpsuDanceInjector.java @@ -20,6 +20,7 @@ package yugecin.opsudance.core.inject; import yugecin.opsudance.PreStartupInitializer; import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.events.EventBus; +import yugecin.opsudance.core.state.specialstates.BarNotificationState; import yugecin.opsudance.core.state.specialstates.FpsRenderState; import yugecin.opsudance.core.state.transitions.EmptyTransitionState; import yugecin.opsudance.core.state.transitions.FadeInTransitionState; @@ -39,6 +40,7 @@ public class OpsuDanceInjector extends Injector { bind(ErrorHandler.class).asEagerSingleton(); bind(FpsRenderState.class).asEagerSingleton(); + bind(BarNotificationState.class).asEagerSingleton(); bind(EmptyTransitionState.class).asEagerSingleton(); bind(FadeInTransitionState.class).asEagerSingleton(); diff --git a/src/yugecin/opsudance/core/state/specialstates/BarNotificationState.java b/src/yugecin/opsudance/core/state/specialstates/BarNotificationState.java new file mode 100644 index 00000000..4350552a --- /dev/null +++ b/src/yugecin/opsudance/core/state/specialstates/BarNotificationState.java @@ -0,0 +1,86 @@ +/* + * 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 . + */ +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.DisplayContainer; +import yugecin.opsudance.core.events.EventBus; +import yugecin.opsudance.core.events.EventListener; +import yugecin.opsudance.events.BarNotificationEvent; +import yugecin.opsudance.events.ResolutionChangedEvent; + +public class BarNotificationState implements EventListener { + + private final int NOTIFICATION_TIME = 5000; + + private final DisplayContainer displayContainer; + private final Color bgcol; + + private int timeShown; + + private String message; + private int textX; + private int textY; + private int barY; + private int barHeight; + + public BarNotificationState(DisplayContainer displayContainer, EventBus eventBus) { + this.displayContainer = displayContainer; + this.bgcol = new Color(0f, 0f, 0f, 0f); + this.timeShown = NOTIFICATION_TIME; + eventBus.subscribe(BarNotificationEvent.class, this); + eventBus.subscribe(ResolutionChangedEvent.class, new EventListener() { + @Override + public void onEvent(ResolutionChangedEvent event) { + if (timeShown >= NOTIFICATION_TIME) { + return; + } + calculatePosition(); + } + }); + } + + public void render(Graphics g, int delta) { + if (timeShown >= NOTIFICATION_TIME) { + return; + } + timeShown += delta; + g.setColor(bgcol); + g.fillRect(0, barY, displayContainer.width, barHeight); + Fonts.LARGE.drawString(textX, textY, message); + } + + private void calculatePosition() { + int textHeight = Fonts.LARGE.getHeight(message); + int textWidth = Fonts.LARGE.getWidth(message); + textX = (displayContainer.width - textWidth) / 2; + textY = (displayContainer.height - textHeight) / 2; + barY = textY - 5; // TODO uiscale stuff? + barHeight = textHeight + 10; + } + + @Override + public void onEvent(BarNotificationEvent event) { + this.message = event.message; + calculatePosition(); + timeShown = 0; + } + +} diff --git a/src/yugecin/opsudance/events/BarNotificationEvent.java b/src/yugecin/opsudance/events/BarNotificationEvent.java new file mode 100644 index 00000000..f3fc6182 --- /dev/null +++ b/src/yugecin/opsudance/events/BarNotificationEvent.java @@ -0,0 +1,28 @@ +/* + * 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 . + */ +package yugecin.opsudance.events; + +public class BarNotificationEvent { + + public final String message; + + public BarNotificationEvent(String message) { + this.message = message; + } + +} diff --git a/src/yugecin/opsudance/states/EmptyRedState.java b/src/yugecin/opsudance/states/EmptyRedState.java index 2747d09c..dd132f32 100644 --- a/src/yugecin/opsudance/states/EmptyRedState.java +++ b/src/yugecin/opsudance/states/EmptyRedState.java @@ -21,6 +21,7 @@ import org.newdawn.slick.Color; import org.newdawn.slick.Graphics; import yugecin.opsudance.core.DisplayContainer; import yugecin.opsudance.core.state.OpsuState; +import yugecin.opsudance.events.BarNotificationEvent; import java.io.StringWriter; @@ -93,6 +94,7 @@ public class EmptyRedState implements OpsuState { @Override public boolean mouseReleased(int button, int x, int y) { + displayContainer.eventBus.post(new BarNotificationEvent("this is a\nbar notification")); return false; }