Fixed a bug where sounds weren't reloaded when changing skins.

When restarting the program through the options menu, all sounds now get reloaded if the skin was changed.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-06-03 06:23:23 -04:00
parent f3825eba5e
commit 90c8c9e705
4 changed files with 67 additions and 12 deletions

View File

@ -911,15 +911,12 @@ public class Options {
* If the directory is invalid, the default skin will be loaded.
*/
public static void loadSkin() {
File root = getSkinRootDir();
File skinDir = new File(root, skinName);
if (!skinDir.isDirectory()) { // invalid skin name
File skinDir = getSkinDir();
if (skinDir == null) // invalid skin name
skinName = Skin.DEFAULT_SKIN_NAME;
skinDir = null;
}
// create available skins list
File[] dirs = SkinLoader.getSkinDirectories(root);
File[] dirs = SkinLoader.getSkinDirectories(getSkinRootDir());
skinDirs = new String[dirs.length + 1];
skinDirs[0] = Skin.DEFAULT_SKIN_NAME;
for (int i = 0; i < dirs.length; i++)
@ -953,6 +950,21 @@ public class Options {
*/
public static Skin getSkin() { return skin; }
/**
* Returns the current skin directory.
* <p>
* NOTE: This directory will differ from that of the currently loaded skin
* if {@link #loadSkin()} has not been called after a directory change.
* Use {@link Skin#getDirectory()} to get the directory of the currently
* loaded skin.
* @return the skin directory, or null for the default skin
*/
public static File getSkinDir() {
File root = getSkinRootDir();
File dir = new File(root, skinName);
return (dir.isDirectory()) ? dir : null;
}
/**
* Returns a dummy Beatmap containing the theme song.
* @return the theme song beatmap

View File

@ -105,6 +105,12 @@ public enum HitSound implements SoundController.SoundComponent {
return (currentSampleSet != null) ? clips.get(currentSampleSet) : null;
}
/**
* Returns the Clip associated with the given sample set.
* @param s the sample set
*/
public MultiClip getClip(SampleSet s) { return clips.get(s); }
/**
* Sets the hit sound Clip for the sample type.
* @param s the sample set

View File

@ -207,7 +207,14 @@ public class SoundController {
ErrorHandler.error(String.format("Could not find sound file '%s'.", s.getFileName()), null, false);
continue;
}
s.setClip(loadClip(currentFileName, currentFileName.endsWith(".mp3")));
MultiClip newClip = loadClip(currentFileName, currentFileName.endsWith(".mp3"));
if (s.getClip() != null) { // clip previously loaded (e.g. program restart)
if (newClip != null) {
s.getClip().destroy(); // destroy previous clip
s.setClip(newClip);
}
} else
s.setClip(newClip);
currentFileIndex++;
}
@ -219,7 +226,14 @@ public class SoundController {
ErrorHandler.error(String.format("Could not find hit sound file '%s'.", filename), null, false);
continue;
}
s.setClip(ss, loadClip(currentFileName, false));
MultiClip newClip = loadClip(currentFileName, false);
if (s.getClip(ss) != null) { // clip previously loaded (e.g. program restart)
if (newClip != null) {
s.getClip(ss).destroy(); // destroy previous clip
s.setClip(ss, newClip);
}
} else
s.setClip(ss, newClip);
currentFileIndex++;
}
}

View File

@ -55,6 +55,9 @@ public class Splash extends BasicGameState {
/** Number of times the 'Esc' key has been pressed. */
private int escapeCount = 0;
/** Whether the skin being loaded is a new skin (for program restarts). */
private boolean newSkin = false;
// game-related variables
private int state;
private GameContainer container;
@ -69,6 +72,10 @@ public class Splash extends BasicGameState {
throws SlickException {
this.container = container;
// check if skin changed
if (Options.getSkin() != null)
this.newSkin = (Options.getSkin().getDirectory() != Options.getSkinDir());
// load Utils class first (needed in other 'init' methods)
Utils.init(container, game);
@ -89,11 +96,27 @@ public class Splash extends BasicGameState {
if (!init) {
init = true;
if (BeatmapSetList.get() != null) {
// resources already loaded (from application restart)
if (BeatmapSetList.get() != null) {
// reload sounds if skin changed
if (newSkin) {
thread = new Thread() {
@Override
public void run() {
// TODO: only reload each sound if actually needed?
SoundController.init();
finished = true;
} else {
// load resources in a new thread
thread = null;
}
};
thread.start();
} else // don't reload anything
finished = true;
}
// load all resources in a new thread
else {
thread = new Thread() {
@Override
public void run() {