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

@ -30,8 +30,7 @@ import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.Log;
import org.newdawn.slick.util.ResourceLoader;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
import yugecin.opsudance.skinning.SkinService;
import yugecin.opsudance.utils.SlickUtil;
@ -733,7 +732,7 @@ public enum GameImage {
String err = String.format("Could not find default image '%s'.", filename);
Log.warn(err);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(err, BubNotifListener.COMMONCOLOR_RED);
}
/**
@ -776,31 +775,34 @@ public enum GameImage {
* @return an array of the loaded images, or null if not found
*/
private Image[] loadImageArray(File dir) {
if (filenameFormat != null) {
for (String suffix : getSuffixes()) {
List<Image> list = new ArrayList<Image>();
int i = 0;
while (true) {
// look for next image
String filenameFormatted = String.format(filenameFormat + suffix, i++);
String name = getImageFileName(filenameFormatted, dir, type, true);
if (name == null)
break;
if (filenameFormat == null) {
return null;
}
for (String suffix : getSuffixes()) {
List<Image> list = new ArrayList<Image>();
int i = 0;
while (true) {
// look for next image
String filenameFormatted = String.format(filenameFormat + suffix, i++);
String name = getImageFileName(filenameFormatted, dir, type, true);
if (name == null)
break;
// add image to list
try {
Image img = new Image(name);
if (suffix.equals(HD_SUFFIX))
img = img.getScaledCopy(0.5f);
list.add(img);
} catch (SlickException e) {
EventBus.post(new BubbleNotificationEvent(String.format("Failed to set image '%s'.", name), BubbleNotificationEvent.COMMONCOLOR_RED));
break;
}
// add image to list
try {
Image img = new Image(name);
if (suffix.equals(HD_SUFFIX))
img = img.getScaledCopy(0.5f);
list.add(img);
} catch (SlickException e) {
BubNotifListener.EVENT.make().onBubNotif(
String.format("Failed to set image '%s'.", name),
BubNotifListener.COMMONCOLOR_RED);
break;
}
if (!list.isEmpty())
return list.toArray(new Image[list.size()]);
}
if (!list.isEmpty())
return list.toArray(new Image[list.size()]);
}
return null;
}
@ -813,15 +815,18 @@ public enum GameImage {
private Image loadImageSingle(File dir) {
for (String suffix : getSuffixes()) {
String name = getImageFileName(filename + suffix, dir, type, true);
if (name != null) {
try {
Image img = new Image(name);
if (suffix.equals(HD_SUFFIX))
img = img.getScaledCopy(0.5f);
return img;
} catch (SlickException e) {
EventBus.post(new BubbleNotificationEvent(String.format("Failed to set image '%s'.", filename), BubbleNotificationEvent.COMMONCOLOR_RED));
}
if (name == null) {
continue;
}
try {
Image img = new Image(name);
if (suffix.equals(HD_SUFFIX))
img = img.getScaledCopy(0.5f);
return img;
} catch (SlickException e) {
BubNotifListener.EVENT.make().onBubNotif(
String.format("Failed to set image '%s'.", filename),
BubNotifListener.COMMONCOLOR_RED);
}
}
return null;

View File

@ -47,6 +47,7 @@ import com.sun.jna.platform.FileUtils;
import yugecin.opsudance.core.NotNull;
import yugecin.opsudance.core.Nullable;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.events.BubNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;

View File

@ -45,9 +45,8 @@ import org.newdawn.slick.util.Log;
import org.newdawn.slick.util.ResourceLoader;
import org.tritonus.share.sampled.file.TAudioFileFormat;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.events.BubNotifListener;
import static yugecin.opsudance.options.Options.*;
@ -103,7 +102,8 @@ public class MusicController {
if (lastBeatmap == null || !beatmap.audioFilename.equals(lastBeatmap.audioFilename)) {
final File audioFile = beatmap.audioFilename;
if (!audioFile.isFile() && !ResourceLoader.resourceExists(audioFile.getPath())) {
EventBus.post(new BarNotificationEvent(String.format("Could not find track '%s'.", audioFile.getName())));
BarNotifListener.EVENT.make().onBarNotif(String.format("Could not find track '%s'.",
audioFile.getName()));
return;
}
@ -158,7 +158,7 @@ public class MusicController {
} catch (Exception e) {
String err = String.format("Could not play track '%s'.", file.getName());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(err, BubNotifListener.COMMONCOLOR_RED);
}
}

View File

@ -39,9 +39,8 @@ import javax.sound.sampled.LineUnavailableException;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.ResourceLoader;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.events.BubNotifListener;
import yugecin.opsudance.options.Configuration;
import yugecin.opsudance.skinning.SkinService;
@ -220,7 +219,9 @@ public class SoundController {
// menu and game sounds
for (SoundEffect s : SoundEffect.values()) {
if ((currentFileName = getSoundFileName(s.getFileName())) == null) {
EventBus.post(new BubbleNotificationEvent("Could not find sound file " + s.getFileName(), BubbleNotificationEvent.COLOR_ORANGE));
BubNotifListener.EVENT.make().onBubNotif(
"Could not find sound file " + s.getFileName(),
BubNotifListener.COLOR_ORANGE);
continue;
}
MultiClip newClip = loadClip(currentFileName, currentFileName.endsWith(".mp3"));
@ -239,7 +240,9 @@ public class SoundController {
for (HitSound s : HitSound.values()) {
String filename = String.format("%s-%s", ss.getName(), s.getFileName());
if ((currentFileName = getSoundFileName(filename)) == null) {
EventBus.post(new BubbleNotificationEvent("Could not find hit sound file " + filename, BubbleNotificationEvent.COLOR_ORANGE));
BubNotifListener.EVENT.make().onBubNotif(
"Could not find hit sound file " + filename,
BubNotifListener.COLOR_ORANGE);
continue;
}
MultiClip newClip = loadClip(currentFileName, false);
@ -396,7 +399,8 @@ public class SoundController {
@Override
public void error() {
EventBus.post(new BarNotificationEvent("Failed to download track preview."));
BarNotifListener.EVENT.make().onBarNotif(
"Failed to download track preview");
}
});
try {

View File

@ -33,9 +33,9 @@ import java.util.Map;
import org.newdawn.slick.Color;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.Nullable;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
import yugecin.opsudance.skinning.SkinService;
import static yugecin.opsudance.core.InstanceContainer.*;
@ -93,8 +93,8 @@ public class BeatmapParser {
* @param dirs the array of directories to parse
* @return the last BeatmapSetNode parsed, or null if none
*/
public BeatmapSetNode parseDirectories(File[] dirs) {
if (dirs == null)
public BeatmapSetNode parseDirectories(@Nullable File[] dirs) {
if (dirs == null || dirs.length == 0)
return null;
// progress tracking
@ -159,8 +159,8 @@ public class BeatmapParser {
try {
beatmap = parseFile(file, dir, beatmaps, false);
} catch(Exception e) {
Log.error("could not parse beatmap " + file.getName() + ": " + e.getMessage());
EventBus.post(new BubbleNotificationEvent("Could not parse beatmap " + file.getName(), BubbleNotificationEvent.COLOR_ORANGE));
logAndShowErrorNotification(e, "Could not parse beatmap %s: %s",
file.getName(), e.getMessage());
}
// add to parsed beatmap list
@ -247,9 +247,7 @@ public class BeatmapParser {
}
map.timingPoints.trimToSize();
} catch (IOException e) {
String err = String.format("Failed to read file '%s'.", map.getFile().getAbsolutePath());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
logAndShowErrorNotification(e, "Failed to read file '%s'.", map.getFile().getAbsolutePath());
} catch (NoSuchAlgorithmException e) {
ErrorHandler.error("Failed to get MD5 hash stream.", e).show();
@ -652,9 +650,7 @@ public class BeatmapParser {
if (md5stream != null)
beatmap.md5Hash = md5stream.getMD5();
} catch (IOException e) {
String err = String.format("Failed to read file '%s'.", file.getAbsolutePath());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
logAndShowErrorNotification(e, "Failed to read file '%s'.", file.getAbsolutePath());
} catch (NoSuchAlgorithmException e) {
ErrorHandler.error("Failed to get MD5 hash stream.", e).show();
@ -735,9 +731,8 @@ public class BeatmapParser {
}
beatmap.timingPoints.trimToSize();
} catch (IOException e) {
String err = String.format("Failed to read file '%s'.", beatmap.getFile().getAbsolutePath());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
logAndShowErrorNotification(e, "Failed to read file '%s'.",
beatmap.getFile().getAbsolutePath());
}
}
@ -818,9 +813,8 @@ public class BeatmapParser {
ErrorHandler.error(String.format("Parsed %d objects for beatmap '%s', %d objects expected.",
objectIndex, beatmap.toString(), beatmap.objects.length), new Exception("no")).show();
} catch (IOException e) {
String err = String.format("Failed to read file '%s'.", beatmap.getFile().getAbsolutePath());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
logAndShowErrorNotification(e, "Failed to read file '%s'.",
beatmap.getFile().getAbsolutePath());
}
}
@ -891,4 +885,10 @@ public class BeatmapParser {
return DBString;
}
private static void logAndShowErrorNotification(Exception e, String message, Object... formatArgs) {
message = String.format(message, formatArgs);
Log.error(message, e);
BubNotifListener.EVENT.make().onBubNotif(message, BubNotifListener.COMMONCOLOR_RED);
}
}

View File

@ -21,8 +21,7 @@ package itdelatrisu.opsu.beatmap;
import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.db.BeatmapDB;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
import java.io.File;
import java.io.IOException;
@ -215,7 +214,8 @@ public class BeatmapSetList {
try {
Utils.deleteToTrash(dir);
} catch (IOException e) {
EventBus.post(new BubbleNotificationEvent("Could not delete song group", BubbleNotificationEvent.COLOR_ORANGE));
BubNotifListener.EVENT.make().onBubNotif("Could not delete song group",
BubNotifListener.COLOR_ORANGE);
}
if (ws != null)
ws.resume();
@ -271,7 +271,7 @@ public class BeatmapSetList {
try {
Utils.deleteToTrash(file);
} catch (IOException e) {
EventBus.post(new BubbleNotificationEvent("Could not delete song", BubbleNotificationEvent.COLOR_ORANGE));
BubNotifListener.EVENT.make().onBubNotif("Could not delete song", BubNotifListener.COLOR_ORANGE);
}
if (ws != null)
ws.resume();

View File

@ -39,8 +39,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.events.BubNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;
import static yugecin.opsudance.options.Options.*;
@ -100,7 +100,8 @@ public class BeatmapWatchService {
ws.register(config.beatmapDir.toPath());
} catch (IOException e) {
Log.error("Could not create watch service", e);
EventBus.post(new BubbleNotificationEvent("Could not create watch service", BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif("Could not create watch service",
BubNotifListener.COMMONCOLOR_RED);
return;
}
@ -121,8 +122,9 @@ public class BeatmapWatchService {
ws.service.shutdownNow();
ws = null;
} catch (IOException e) {
Log.error("An I/O exception occurred while closing the previous watch service.", e);
EventBus.post(new BubbleNotificationEvent("An I/O exception occurred while closing the previous watch service.", BubbleNotificationEvent.COMMONCOLOR_RED));
String msg = "An I/O exception occurred while closing the previous watch service.";
Log.error(msg, e);
BarNotifListener.EVENT.make().onBarNotif(msg);
ws = null;
}
}
@ -137,7 +139,10 @@ public class BeatmapWatchService {
return ws;
}
/** Watch service listener interface. */
/**
* Watch service listener interface.
* TODO: replace by event system?
*/
public interface BeatmapWatchServiceListener {
/**
* Indication that an event was received.

View File

@ -26,8 +26,7 @@ import java.util.List;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;
@ -97,7 +96,7 @@ public class OszUnpacker {
} catch (ZipException e) {
String err = String.format("Failed to unzip file %s to dest %s.", file.getAbsolutePath(), dest.getAbsolutePath());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(err, BubNotifListener.COMMONCOLOR_RED);
}
}

View File

@ -35,8 +35,7 @@ import java.nio.file.StandardCopyOption;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
/**
* File download.
@ -219,7 +218,7 @@ public class Download {
else if (redirectCount > MAX_REDIRECTS)
error = String.format("Download for URL '%s' is attempting too many redirects (over %d).", base.toString(), MAX_REDIRECTS);
if (error != null) {
EventBus.post(new BubbleNotificationEvent(error, BubbleNotificationEvent.COLOR_ORANGE));
BubNotifListener.EVENT.make().onBubNotif(error, BubNotifListener.COLOR_ORANGE);
throw new IOException();
}

View File

@ -33,9 +33,8 @@ import java.io.File;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.events.BubNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;
import static yugecin.opsudance.options.Options.*;
@ -281,12 +280,14 @@ public class DownloadNode {
download.setListener(new DownloadListener() {
@Override
public void completed() {
EventBus.post(new BarNotificationEvent(String.format("Download complete: %s", getTitle())));
BarNotifListener.EVENT.make().onBarNotif(
String.format("Download complete: %s", getTitle()));
}
@Override
public void error() {
EventBus.post(new BarNotificationEvent("Download failed due to a connection error."));
BarNotifListener.EVENT.make().onBarNotif(
"Download failed due to a connection error.");
}
});
this.download = download;
@ -408,7 +409,9 @@ public class DownloadNode {
public void drawDownload(Graphics g, float position, int id, boolean hover) {
Download download = this.download; // in case clearDownload() is called asynchronously
if (download == null) {
EventBus.post(new BubbleNotificationEvent("Trying to draw download information for button without Download object", BubbleNotificationEvent.COLOR_ORANGE));
BubNotifListener.EVENT.make().onBubNotif(
"Trying to draw download information for button without Download object",
BubNotifListener.COLOR_ORANGE);
return;
}

View File

@ -37,8 +37,7 @@ import org.newdawn.slick.util.Log;
import org.newdawn.slick.util.ResourceLoader;
import yugecin.opsudance.core.Constants;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;
@ -242,13 +241,14 @@ public class Updater {
@Override
public void completed() {
status = Status.UPDATE_DOWNLOADED;
EventBus.post(new BarNotificationEvent("Update has finished downloading"));
BarNotifListener.EVENT.make().onBarNotif("Update has finished downloading");
}
@Override
public void error() {
status = Status.CONNECTION_ERROR;
EventBus.post(new BarNotificationEvent("Update failed due to a connection error."));
BarNotifListener.EVENT.make().onBarNotif(
"Update failed due to a connection error.");
}
});
}

View File

@ -44,8 +44,7 @@ import org.newdawn.slick.util.Log;
import lzma.streams.LzmaOutputStream;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;
@ -275,7 +274,7 @@ public class Replay {
public void save() {
// create replay directory
if (!config.replayDir.isDirectory() && !config.replayDir.mkdir()) {
EventBus.post(new BubbleNotificationEvent("Failed to create replay directory.", BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif("Failed to create replay directory", BubNotifListener.COMMONCOLOR_RED);
return;
}

View File

@ -29,8 +29,7 @@ import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;
@ -69,7 +68,7 @@ public class ReplayImporter {
if (!config.replayDir.isDirectory() && !config.replayDir.mkdir()) {
String err = String.format("Failed to create replay directory '%s'.", config.replayDir.getAbsolutePath());
Log.error(err);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(err, BubNotifListener.COMMONCOLOR_RED);
return;
}
@ -83,7 +82,8 @@ public class ReplayImporter {
moveToFailedDirectory(file);
String err = String.format("Failed to import replay '%s'. The replay file could not be parsed.", file.getName());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(err,
BubNotifListener.COMMONCOLOR_RED);
continue;
}
Beatmap beatmap = BeatmapSetList.get().getBeatmapFromHash(r.beatmapHash);
@ -102,7 +102,7 @@ public class ReplayImporter {
moveToFailedDirectory(file);
String err = String.format("Failed to import replay '%s'. The associated beatmap could not be found.", file.getName());
Log.error(err);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(err, BubNotifListener.COMMONCOLOR_RED);
}
}

View File

@ -31,8 +31,7 @@ import java.util.LinkedList;
import org.newdawn.slick.Color;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
/**
* Loads skin configuration files.
@ -293,7 +292,8 @@ public class SkinLoader {
} catch (IOException e) {
String err = String.format("Failed to read file '%s'.", skinFile.getAbsolutePath());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(err,
BubNotifListener.COMMONCOLOR_RED);
}
return skin;

View File

@ -52,9 +52,8 @@ import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.state.ComplexOpsuState;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;
@ -266,16 +265,20 @@ public class DownloadsMenu extends ComplexOpsuState {
private void importBeatmaps() {
// invoke unpacker and parser
File[] dirs = oszunpacker.unpackAll();
if (dirs != null && dirs.length > 0) {
this.importedNode = beatmapParser.parseDirectories(dirs);
if (importedNode != null) {
// send notification
EventBus.post(new BarNotificationEvent((dirs.length == 1) ? "Imported 1 new song." :
String.format("Imported %d new songs.", dirs.length)));
}
}
this.importedNode = beatmapParser.parseDirectories(dirs);
DownloadList.get().clearDownloads(Download.Status.COMPLETE);
if (this.importedNode == null) {
return;
}
String msg;
if (dirs.length == 1) {
msg = "Imported 1 new song.";
} else {
msg = String.format("Imported %d new songs.", dirs.length);
}
BarNotifListener.EVENT.make().onBarNotif(msg);
}
}
@ -682,7 +685,7 @@ public class DownloadsMenu extends ComplexOpsuState {
if (playing)
previewID = node.getID();
} catch (SlickException e) {
EventBus.post(new BarNotificationEvent("Failed to load track preview. See log for details."));
BarNotifListener.EVENT.make().onBarNotif("Failed to load track preview. See log for details.");
Log.error(e);
}
}
@ -705,7 +708,7 @@ public class DownloadsMenu extends ComplexOpsuState {
if (!DownloadList.get().contains(node.getID())) {
node.createDownload(serverMenu.getSelectedItem());
if (node.getDownload() == null) {
EventBus.post(new BarNotificationEvent("The download could not be started"));
BarNotifListener.EVENT.make().onBarNotif("The download could not be started");
} else {
DownloadList.get().addNode(node);
node.getDownload().start();
@ -947,7 +950,7 @@ public class DownloadsMenu extends ComplexOpsuState {
pageDir = Page.RESET;
previewID = -1;
if (barNotificationOnLoad != null) {
EventBus.post(new BarNotificationEvent(barNotificationOnLoad));
BarNotifListener.EVENT.make().onBarNotif(barNotificationOnLoad);
barNotificationOnLoad = null;
}
}

View File

@ -24,7 +24,6 @@ import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.audio.SoundController;
import itdelatrisu.opsu.audio.SoundEffect;
import itdelatrisu.opsu.beatmap.Beatmap;
import itdelatrisu.opsu.beatmap.BeatmapParser;
import itdelatrisu.opsu.beatmap.HitObject;
import itdelatrisu.opsu.beatmap.TimingPoint;
import itdelatrisu.opsu.db.BeatmapDB;
@ -57,13 +56,11 @@ import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.*;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.state.ComplexOpsuState;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.events.BubNotifListener;
import yugecin.opsudance.objects.curves.FakeCombinedCurve;
import yugecin.opsudance.options.OptionGroups;
import yugecin.opsudance.options.Options;
import yugecin.opsudance.sbv2.MoveStoryboard;
import yugecin.opsudance.skinning.SkinService;
import yugecin.opsudance.ui.OptionsOverlay;
@ -337,7 +334,9 @@ public class Game extends ComplexOpsuState {
gOffscreen.setBackground(Color.black);
} catch (SlickException e) {
Log.error("could not create offscreen graphics", e);
EventBus.post(new BubbleNotificationEvent("Exception while creating offscreen graphics. See logfile for details.", BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(
"Exception while creating offscreen graphics. See logfile for details.",
BubNotifListener.COMMONCOLOR_RED);
}
// initialize music position bar location
@ -1172,7 +1171,7 @@ public class Game extends ComplexOpsuState {
if (0 <= time && time < 3600) {
OPTION_CHECKPOINT.setValue(time);
SoundController.playSound(SoundEffect.MENUCLICK);
EventBus.post(new BarNotificationEvent("Checkpoint saved."));
BarNotifListener.EVENT.make().onBarNotif("Checkpoint saved.");
}
}
break;
@ -1184,7 +1183,7 @@ public class Game extends ComplexOpsuState {
break; // invalid checkpoint
loadCheckpoint(checkpoint);
SoundController.playSound(SoundEffect.MENUHIT);
EventBus.post(new BarNotificationEvent("Checkpoint loaded."));
BarNotifListener.EVENT.make().onBarNotif("Checkpoint loaded.");
}
break;
case Input.KEY_F:
@ -1227,12 +1226,12 @@ public class Game extends ComplexOpsuState {
break;
case Input.KEY_MINUS:
currentMapMusicOffset += 5;
EventBus.post(new BarNotificationEvent("Current map offset: " + currentMapMusicOffset));
BarNotifListener.EVENT.make().onBarNotif("Current map offset: " + currentMapMusicOffset);
break;
}
if (key == Input.KEY_ADD || c == '+') {
currentMapMusicOffset -= 5;
EventBus.post(new BarNotificationEvent("Current map offset: " + currentMapMusicOffset));
BarNotifListener.EVENT.make().onBarNotif("Current map offset: " + currentMapMusicOffset);
}
return true;
@ -1454,7 +1453,8 @@ public class Game extends ComplexOpsuState {
}
if (beatmap == null || beatmap.objects == null) {
EventBus.post(new BubbleNotificationEvent("Game was running without a beatmap", BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif("Game was running without a beatmap",
BubNotifListener.COMMONCOLOR_RED);
displayContainer.switchStateInstantly(songMenuState);
}
@ -1561,7 +1561,8 @@ public class Game extends ComplexOpsuState {
} catch (Exception e) {
String message = String.format("Failed to create %s at index %d:\n%s", hitObject.getTypeName(), i, hitObject.toString());
Log.error(message, e);
EventBus.post(new BubbleNotificationEvent(message, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(message,
BubNotifListener.COMMONCOLOR_RED);
gameObjects[i] = new DummyObject(hitObject);
}
}
@ -2147,7 +2148,8 @@ public class Game extends ComplexOpsuState {
this.replay = null;
} else {
if (replay.frames == null) {
EventBus.post(new BubbleNotificationEvent("Attempting to set a replay with no frames.", BubbleNotificationEvent.COLOR_ORANGE));
BubNotifListener.EVENT.make().onBubNotif("Attempting to set a replay with no frames.",
BubNotifListener.COLOR_ORANGE);
return;
}
this.isReplay = true;

View File

@ -36,9 +36,8 @@ import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.state.BaseOpsuState;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;
@ -157,13 +156,14 @@ public class GameRanking extends BaseOpsuState {
gameState.setRestart((data.isGameplay()) ? Game.Restart.REPLAY : Game.Restart.NEW);
returnToGame = true;
} catch (FileNotFoundException e) {
EventBus.post(new BarNotificationEvent("Replay file not found."));
BarNotifListener.EVENT.make().onBarNotif("Replay file not found.");
} catch (IOException e) {
Log.error("Failed to load replay data.", e);
EventBus.post(new BarNotificationEvent("Failed to load replay data. See log for details."));
BarNotifListener.EVENT.make().onBarNotif(
"Failed to load replay data. See log for details.");
}
} else
EventBus.post(new BarNotificationEvent("Replay file not found."));
BarNotifListener.EVENT.make().onBarNotif("Replay file not found.");
}
// retry

View File

@ -46,11 +46,10 @@ import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.Constants;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.state.BaseOpsuState;
import yugecin.opsudance.core.state.OpsuState;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.events.BubNotifListener;
import static yugecin.opsudance.core.InstanceContainer.*;
import static yugecin.opsudance.options.Options.*;
@ -466,12 +465,11 @@ public class MainMenu extends BaseOpsuState {
UI.enter();
if (!enterNotification) {
if (updater.getStatus() == Updater.Status.UPDATE_AVAILABLE) {
EventBus.post(new BarNotificationEvent("An opsu! update is available."));
enterNotification = true;
BarNotifListener.EVENT.make().onBarNotif("An opsu! update is available.");
} else if (updater.justUpdated()) {
EventBus.post(new BarNotificationEvent("opsu! is now up to date!"));
enterNotification = true;
BarNotifListener.EVENT.make().onBarNotif("opsu! is now up to date!");
}
enterNotification = true;
}
// reset measure info
@ -532,15 +530,15 @@ public class MainMenu extends BaseOpsuState {
if (musicPlay.contains(x, y)) {
if (MusicController.isPlaying()) {
MusicController.pause();
EventBus.post(new BarNotificationEvent("Pause"));
BarNotifListener.EVENT.make().onBarNotif("Pause");
} else if (!MusicController.isTrackLoading()) {
MusicController.resume();
EventBus.post(new BarNotificationEvent("Play"));
BarNotifListener.EVENT.make().onBarNotif("Play");
}
return true;
} else if (musicNext.contains(x, y)) {
nextTrack(true);
EventBus.post(new BarNotificationEvent(">> Next"));
BarNotifListener.EVENT.make().onBarNotif(">> Next");
return true;
} else if (musicPrevious.contains(x, y)) {
lastMeasureProgress = 0f;
@ -552,7 +550,7 @@ public class MainMenu extends BaseOpsuState {
} else {
MusicController.setPosition(0);
}
EventBus.post(new BarNotificationEvent("<< Previous"));
BarNotifListener.EVENT.make().onBarNotif("<< Previous");
return true;
}
@ -568,10 +566,12 @@ public class MainMenu extends BaseOpsuState {
try {
Desktop.getDesktop().browse(Constants.REPOSITORY_URI);
} catch (UnsupportedOperationException e) {
EventBus.post(new BarNotificationEvent("The repository web page could not be opened."));
BarNotifListener.EVENT.make().onBarNotif(
"The repository web page could not be opened.");
} catch (IOException e) {
Log.error("could not browse to repo", e);
EventBus.post(new BubbleNotificationEvent("Could not browse to repo", BubbleNotificationEvent.COLOR_ORANGE));
BubNotifListener.EVENT.make().onBubNotif("Could not browse to repo",
BubNotifListener.COLOR_ORANGE);
}
return true;
}
@ -580,10 +580,12 @@ public class MainMenu extends BaseOpsuState {
try {
Desktop.getDesktop().browse(Constants.DANCE_REPOSITORY_URI);
} catch (UnsupportedOperationException e) {
EventBus.post(new BarNotificationEvent("The repository web page could not be opened."));
BarNotifListener.EVENT.make().onBarNotif(
"The repository web page could not be opened.");
} catch (IOException e) {
Log.error("could not browse to repo", e);
EventBus.post(new BubbleNotificationEvent("Could not browse to repo", BubbleNotificationEvent.COLOR_ORANGE));
BubNotifListener.EVENT.make().onBubNotif("Could not browse to repo",
BubNotifListener.COLOR_ORANGE);
}
return true;
}

View File

@ -35,7 +35,6 @@ import itdelatrisu.opsu.beatmap.BeatmapSortOrder;
import itdelatrisu.opsu.beatmap.BeatmapWatchService;
import itdelatrisu.opsu.beatmap.BeatmapWatchService.BeatmapWatchServiceListener;
import itdelatrisu.opsu.beatmap.LRUCache;
import itdelatrisu.opsu.beatmap.OszUnpacker;
import itdelatrisu.opsu.db.BeatmapDB;
import itdelatrisu.opsu.db.ScoreDB;
import itdelatrisu.opsu.states.ButtonMenu.MenuState;
@ -63,9 +62,8 @@ import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.gui.TextField;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.core.state.ComplexOpsuState;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.options.OptionGroups;
import yugecin.opsudance.ui.OptionsOverlay;
@ -439,12 +437,15 @@ public class SongMenu extends ComplexOpsuState {
BeatmapWatchService.addListener(new BeatmapWatchServiceListener() {
@Override
public void eventReceived(Kind<?> kind, Path child) {
if (!songFolderChanged && kind != StandardWatchEventKinds.ENTRY_MODIFY) {
songFolderChanged = true;
if (displayContainer.isInState(SongMenu.class)) {
EventBus.post(new BarNotificationEvent("Changed is Songs folder detected. Hit F5 to refresh."));
}
if (songFolderChanged || kind == StandardWatchEventKinds.ENTRY_MODIFY) {
return;
}
songFolderChanged = true;
if (!displayContainer.isInState(SongMenu.class)) {
return;
}
BarNotifListener.EVENT.make().onBarNotif(
"Changed is Songs folder detected. Hit F5 to refresh.");
}
});
@ -929,30 +930,32 @@ public class SongMenu extends ComplexOpsuState {
// group tabs
for (BeatmapGroup group : BeatmapGroup.values()) {
if (group.contains(x, y)) {
if (group != BeatmapGroup.current()) {
BeatmapGroup.set(group);
SoundController.playSound(SoundEffect.MENUCLICK);
startNode = focusNode = null;
oldFocusNode = null;
randomStack = new Stack<SongNode>();
songInfo = null;
scoreMap = null;
focusScores = null;
searchTextField.setText("");
searchTimer = SEARCH_DELAY;
searchTransitionTimer = SEARCH_TRANSITION_TIME;
searchResultString = null;
BeatmapSetList.get().reset();
BeatmapSetList.get().init();
setFocus(BeatmapSetList.get().getRandomNode(), -1, true, true);
if (BeatmapSetList.get().size() < 1 && group.getEmptyMessage() != null) {
EventBus.post(new BarNotificationEvent(group.getEmptyMessage()));
}
}
if (!group.contains(x, y)) {
continue;
}
if (group == BeatmapGroup.current()) {
return true;
}
BeatmapGroup.set(group);
SoundController.playSound(SoundEffect.MENUCLICK);
startNode = focusNode = null;
oldFocusNode = null;
randomStack = new Stack<SongNode>();
songInfo = null;
scoreMap = null;
focusScores = null;
searchTextField.setText("");
searchTimer = SEARCH_DELAY;
searchTransitionTimer = SEARCH_TRANSITION_TIME;
searchResultString = null;
BeatmapSetList.get().reset();
BeatmapSetList.get().init();
setFocus(BeatmapSetList.get().getRandomNode(), -1, true, true);
if (BeatmapSetList.get().size() < 1 && group.getEmptyMessage() != null) {
BarNotifListener.EVENT.make().onBarNotif(group.getEmptyMessage());
}
return true;
}
if (focusNode == null) {
@ -1760,7 +1763,7 @@ public class SongMenu extends ComplexOpsuState {
Beatmap beatmap = MusicController.getBeatmap();
if (focusNode == null || beatmap != focusNode.getSelectedBeatmap()) {
EventBus.post(new BarNotificationEvent("Unable to load the beatmap audio."));
BarNotifListener.EVENT.make().onBarNotif("Unable to load the beatmap audio.");
return;
}

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

@ -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;

View File

@ -17,12 +17,12 @@
*/
package yugecin.opsudance.events;
public class BarNotificationEvent {
import yugecin.opsudance.core.events.Event;
public final String message;
public interface BarNotifListener {
public BarNotificationEvent(String message) {
this.message = message;
}
Event<BarNotifListener> EVENT = new Event<>(BarNotifListener.class);
void onBarNotif(String message);
}

View File

@ -18,8 +18,12 @@
package yugecin.opsudance.events;
import org.newdawn.slick.Color;
import yugecin.opsudance.core.events.Event;
public class BubbleNotificationEvent {
@SuppressWarnings({"UnnecessaryInterfaceModifier", "unused"})
public interface BubNotifListener {
Event<BubNotifListener> EVENT = new Event<>(BubNotifListener.class);
public static final Color COMMONCOLOR_GREEN = new Color(98, 131, 59);
public static final Color COMMONCOLOR_WHITE = new Color(220, 220, 220);
@ -27,12 +31,6 @@ public class BubbleNotificationEvent {
public static final Color COMMONCOLOR_RED = new Color(141, 49, 16);
public static final Color COLOR_ORANGE = new Color(138, 72, 51);
public final String message;
public final Color borderColor;
public BubbleNotificationEvent(String message, Color borderColor) {
this.message = message;
this.borderColor = borderColor;
}
void onBubNotif(String message, Color borderColor);
}

View File

@ -17,16 +17,12 @@
*/
package yugecin.opsudance.events;
public class ResolutionOrSkinChangedEvent {
import yugecin.opsudance.core.events.Event;
public final String skin;
public final int width;
public final int height;
public interface ResolutionChangedListener {
public ResolutionOrSkinChangedEvent(String skin, int width, int height) {
this.skin = skin;
this.width = width;
this.height = height;
}
Event<ResolutionChangedListener> EVENT = new Event<>(ResolutionChangedListener.class);
void onResolutionChanged(int w, int h);
}

View File

@ -15,10 +15,14 @@
* 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;
package yugecin.opsudance.events;
public interface EventListener<T> {
import yugecin.opsudance.core.events.Event;
void onEvent(T event);
public interface SkinChangedListener {
Event<SkinChangedListener> EVENT = new Event<>(SkinChangedListener.class);
void onSkinChanged(String stringName);
}

View File

@ -28,8 +28,7 @@ import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import yugecin.opsudance.core.errorhandling.ErrorHandler;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
import yugecin.opsudance.utils.ManifestWrapper;
import javax.imageio.ImageIO;
@ -154,7 +153,7 @@ public class Configuration {
}
if (!defaultDir.isDirectory() && !defaultDir.mkdir()) {
String msg = String.format("Failed to create %s directory at '%s'.", kind, defaultDir.getAbsolutePath());
EventBus.post(new BubbleNotificationEvent(msg, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(msg, BubNotifListener.COMMONCOLOR_RED);
}
return defaultDir;
}
@ -213,7 +212,10 @@ public class Configuration {
// TODO: get a decent place for this
// create the screenshot directory
if (!screenshotDir.isDirectory() && !screenshotDir.mkdir()) {
EventBus.post(new BubbleNotificationEvent(String.format("Failed to create screenshot directory at '%s'.", screenshotDir.getAbsolutePath()), BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(
String.format( "Failed to create screenshot directory at '%s'.",
screenshotDir.getAbsolutePath()),
BubNotifListener.COMMONCOLOR_RED);
return;
}
@ -247,7 +249,8 @@ public class Configuration {
}
}
ImageIO.write(image, OPTION_SCREENSHOT_FORMAT.getValueString().toLowerCase(), file);
EventBus.post(new BubbleNotificationEvent("Created " + fileName, BubbleNotificationEvent.COMMONCOLOR_PURPLE));
BubNotifListener.EVENT.make().onBubNotif("Created " + fileName,
BubNotifListener.COMMONCOLOR_PURPLE);
} catch (Exception e) {
ErrorHandler.error("Failed to take a screenshot.", e).show();
}

View File

@ -18,8 +18,7 @@
package yugecin.opsudance.options;
import itdelatrisu.opsu.Utils;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
public class NumericOption extends Option {
@ -53,7 +52,8 @@ public class NumericOption extends Option {
try {
val = Utils.clamp(Integer.parseInt(s), min, max);
} catch (Exception ignored) {
EventBus.post(new BubbleNotificationEvent("Failed to parse " + configurationName + " option", BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif("Failed to parse " + configurationName + " option",
BubNotifListener.COMMONCOLOR_RED);
}
}

View File

@ -28,8 +28,7 @@ import org.newdawn.slick.SlickException;
import org.newdawn.slick.openal.SoundStore;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.*;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.movers.factories.ExgonMoverFactory;
import yugecin.opsudance.movers.factories.QuadraticBezierMoverFactory;
import yugecin.opsudance.movers.slidermovers.DefaultSliderMoverController;
@ -439,7 +438,8 @@ public class Options {
public static final ToggleOption OPTION_DISABLE_MOUSE_BUTTONS = new ToggleOption("Disable mouse buttons in play mode", "MouseDisableButtons", "This option will disable all mouse buttons. Specifically for people who use their keyboard to click.", false) {
@Override
public void toggle() {
EventBus.post(new BarNotificationEvent(state ? "Mouse buttons are disabled." : "Mouse buttons are enabled."));
BarNotifListener.EVENT.make().onBarNotif(state ?
"Mouse buttons are disabled." : "Mouse buttons are enabled.");
}
};
public static final ToggleOption OPTION_DISABLE_CURSOR = new ToggleOption("Disable Cursor", "DisableCursor", "Hide the cursor sprite.", false);
@ -608,7 +608,7 @@ public class Options {
public void clickListItem(int index){
if (Game.isInGame && Dancer.moverFactories[index] instanceof PolyMoverFactory) {
// TODO remove this when #79 is fixed
EventBus.post(new BarNotificationEvent("This mover is disabled in the storyboard right now"));
BarNotifListener.EVENT.make().onBarNotif("This mover is disabled in the storyboard right now");
return;
}
Dancer.instance.setMoverFactoryIndex(index);

View File

@ -18,8 +18,7 @@
package yugecin.opsudance.options;
import org.newdawn.slick.util.Log;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.BubbleNotificationEvent;
import yugecin.opsudance.events.BubNotifListener;
import java.io.*;
import java.text.SimpleDateFormat;
@ -80,7 +79,7 @@ public class OptionsService {
} catch (IOException e) {
String err = String.format("Failed to read option file '%s'.", config.OPTIONS_FILE.getAbsolutePath());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(err, BubNotifListener.COMMONCOLOR_RED);
}
config.loadDirectories();
}
@ -109,7 +108,7 @@ public class OptionsService {
} catch (IOException e) {
String err = String.format("Failed to write to file '%s'.", config.OPTIONS_FILE.getAbsolutePath());
Log.error(err, e);
EventBus.post(new BubbleNotificationEvent(err, BubbleNotificationEvent.COMMONCOLOR_RED));
BubNotifListener.EVENT.make().onBubNotif(err, BubNotifListener.COMMONCOLOR_RED);
}
}

View File

@ -24,9 +24,8 @@ import itdelatrisu.opsu.ui.animations.AnimationEquation;
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.state.OverlayOpsuState;
import yugecin.opsudance.events.BarNotificationEvent;
import yugecin.opsudance.events.BarNotifListener;
import yugecin.opsudance.sbv2.movers.CubicStoryboardMover;
import yugecin.opsudance.sbv2.movers.LinearStoryboardMover;
import yugecin.opsudance.sbv2.movers.QuadraticStoryboardMover;
@ -186,7 +185,7 @@ public class MoveStoryboard extends OverlayOpsuState{
private StoryboardMove getCurrentMoveOrCreateNew() {
if (gameObjects[objectIndex].isSlider() && trackPosition > gameObjects[objectIndex].getTime() && trackPosition < gameObjects[objectIndex].getEndTime()) {
EventBus.post(new BarNotificationEvent("Wait until the slider ended"));
BarNotifListener.EVENT.make().onBarNotif("Wait until the slider ended");
return dummyMove;
}
if (moves[objectIndex] == null) {

View File

@ -23,8 +23,7 @@ import itdelatrisu.opsu.skins.SkinLoader;
import org.newdawn.slick.util.ClasspathLocation;
import org.newdawn.slick.util.FileSystemLocation;
import org.newdawn.slick.util.ResourceLoader;
import yugecin.opsudance.core.events.EventBus;
import yugecin.opsudance.events.ResolutionOrSkinChangedEvent;
import yugecin.opsudance.events.SkinChangedListener;
import java.io.File;
@ -41,7 +40,7 @@ public class SkinService {
public void reloadSkin() {
loadSkin();
SoundController.init();
EventBus.post(new ResolutionOrSkinChangedEvent(usedSkinName, -1, -1));
SkinChangedListener.EVENT.make().onSkinChanged(usedSkinName);
}
/**