Added track previews to the downloads menu.
Currently uses the osu! server to load MP3 previews. Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
@@ -39,6 +39,11 @@ import itdelatrisu.opsu.downloads.DownloadServer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.sound.sampled.LineEvent;
|
||||
import javax.sound.sampled.LineListener;
|
||||
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.GameContainer;
|
||||
@@ -51,6 +56,7 @@ import org.newdawn.slick.state.BasicGameState;
|
||||
import org.newdawn.slick.state.StateBasedGame;
|
||||
import org.newdawn.slick.state.transition.FadeInTransition;
|
||||
import org.newdawn.slick.state.transition.FadeOutTransition;
|
||||
import org.newdawn.slick.util.Log;
|
||||
|
||||
/**
|
||||
* Downloads menu.
|
||||
@@ -137,6 +143,9 @@ public class DownloadsMenu extends BasicGameState {
|
||||
/** Beatmap importing thread. */
|
||||
private Thread importThread;
|
||||
|
||||
/** Beatmap set ID of the current beatmap being previewed, or -1 if none. */
|
||||
private int previewID = -1;
|
||||
|
||||
// game-related variables
|
||||
private GameContainer container;
|
||||
private StateBasedGame game;
|
||||
@@ -248,7 +257,8 @@ public class DownloadsMenu extends BasicGameState {
|
||||
int index = startResult + i;
|
||||
if (index >= nodes.length)
|
||||
break;
|
||||
nodes[index].drawResult(g, i, DownloadNode.resultContains(mouseX, mouseY, i), (index == focusResult));
|
||||
nodes[index].drawResult(g, i, DownloadNode.resultContains(mouseX, mouseY, i),
|
||||
(index == focusResult), (previewID == nodes[index].getID()));
|
||||
}
|
||||
|
||||
// scroll bar
|
||||
@@ -441,10 +451,61 @@ public class DownloadsMenu extends BasicGameState {
|
||||
if (index >= nodes.length)
|
||||
break;
|
||||
if (DownloadNode.resultContains(x, y, i)) {
|
||||
DownloadNode node = nodes[index];
|
||||
final DownloadNode node = nodes[index];
|
||||
|
||||
// check if map is already loaded
|
||||
if (OsuGroupList.get().containsBeatmapSetID(node.getID()))
|
||||
boolean isLoaded = OsuGroupList.get().containsBeatmapSetID(node.getID());
|
||||
|
||||
// track preview
|
||||
if (DownloadNode.resultIconContains(x, y, i)) {
|
||||
// set focus
|
||||
if (!isLoaded) {
|
||||
SoundController.playSound(SoundEffect.MENUCLICK);
|
||||
focusResult = index;
|
||||
focusTimer = FOCUS_DELAY;
|
||||
}
|
||||
|
||||
if (previewID == node.getID()) {
|
||||
// stop preview
|
||||
previewID = -1;
|
||||
SoundController.stopTrack();
|
||||
} else {
|
||||
// play preview
|
||||
try {
|
||||
final URL url = new URL(server.getPreviewURL(node.getID()));
|
||||
MusicController.pause();
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
previewID = -1;
|
||||
SoundController.playTrack(url, true, new LineListener() {
|
||||
@Override
|
||||
public void update(LineEvent event) {
|
||||
if (event.getType() == LineEvent.Type.STOP) {
|
||||
if (previewID != -1) {
|
||||
SoundController.stopTrack();
|
||||
previewID = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
previewID = node.getID();
|
||||
} catch (SlickException e) {
|
||||
UI.sendBarNotification("Failed to load track preview.");
|
||||
Log.error(e);
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
} catch (MalformedURLException e) {
|
||||
UI.sendBarNotification("Could not load track preview (bad URL).");
|
||||
Log.error(e);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (isLoaded)
|
||||
return;
|
||||
|
||||
SoundController.playSound(SoundEffect.MENUCLICK);
|
||||
@@ -677,12 +738,15 @@ public class DownloadsMenu extends BasicGameState {
|
||||
startResult = 0;
|
||||
startDownloadIndex = 0;
|
||||
pageDir = Page.RESET;
|
||||
previewID = -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void leave(GameContainer container, StateBasedGame game)
|
||||
throws SlickException {
|
||||
search.setFocus(false);
|
||||
SoundController.stopTrack();
|
||||
MusicController.resume();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1099,9 +1099,8 @@ public class Game extends BasicGameState {
|
||||
UI.hideCursor();
|
||||
|
||||
// replays
|
||||
if (isReplay) {
|
||||
if (isReplay)
|
||||
GameMod.loadModState(previousMods);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user