2014-07-06 03:00:52 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2014-07-06 03:00:52 +02:00
|
|
|
*
|
|
|
|
* opsu! 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! 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!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu.states;
|
|
|
|
|
2015-01-08 05:45:21 +01:00
|
|
|
import itdelatrisu.opsu.GameImage;
|
2015-01-21 05:56:10 +01:00
|
|
|
import itdelatrisu.opsu.Options;
|
2014-07-06 03:00:52 +02:00
|
|
|
import itdelatrisu.opsu.Utils;
|
2015-01-08 01:29:51 +01:00
|
|
|
import itdelatrisu.opsu.audio.MusicController;
|
|
|
|
import itdelatrisu.opsu.audio.SoundController;
|
2015-05-29 09:07:58 +02:00
|
|
|
import itdelatrisu.opsu.beatmap.BeatmapParser;
|
2015-06-30 02:22:38 +02:00
|
|
|
import itdelatrisu.opsu.beatmap.BeatmapSetList;
|
2015-08-21 17:25:52 +02:00
|
|
|
import itdelatrisu.opsu.beatmap.BeatmapWatchService;
|
2015-09-01 05:25:28 +02:00
|
|
|
import itdelatrisu.opsu.beatmap.OszUnpacker;
|
2015-06-30 02:22:38 +02:00
|
|
|
import itdelatrisu.opsu.replay.ReplayImporter;
|
2015-05-29 07:55:57 +02:00
|
|
|
import itdelatrisu.opsu.ui.UI;
|
2015-08-06 07:53:30 +02:00
|
|
|
import itdelatrisu.opsu.ui.animations.AnimatedValue;
|
|
|
|
import itdelatrisu.opsu.ui.animations.AnimationEquation;
|
2014-07-06 03:00:52 +02:00
|
|
|
|
2014-07-06 07:58:44 +02:00
|
|
|
import java.io.File;
|
|
|
|
|
2014-07-06 03:00:52 +02:00
|
|
|
import org.newdawn.slick.Color;
|
|
|
|
import org.newdawn.slick.Graphics;
|
2014-07-06 08:27:53 +02:00
|
|
|
import org.newdawn.slick.Input;
|
2017-01-18 19:44:54 +01:00
|
|
|
import org.newdawn.slick.util.Log;
|
2017-01-17 23:18:12 +01:00
|
|
|
import yugecin.opsudance.core.DisplayContainer;
|
2017-01-17 23:44:12 +01:00
|
|
|
import yugecin.opsudance.core.inject.InstanceContainer;
|
2017-01-17 23:18:12 +01:00
|
|
|
import yugecin.opsudance.core.state.BaseOpsuState;
|
2014-07-06 03:00:52 +02:00
|
|
|
|
2014-07-06 07:58:44 +02:00
|
|
|
/**
|
|
|
|
* "Splash Screen" state.
|
|
|
|
* <p>
|
|
|
|
* Loads game resources and enters "Main Menu" state.
|
|
|
|
*/
|
2017-01-17 23:18:12 +01:00
|
|
|
public class Splash extends BaseOpsuState {
|
|
|
|
|
2017-01-17 23:44:12 +01:00
|
|
|
private final InstanceContainer instanceContainer;
|
|
|
|
|
2015-08-06 07:53:30 +02:00
|
|
|
/** Minimum time, in milliseconds, to display the splash screen (and fade in the logo). */
|
2015-08-08 19:04:15 +02:00
|
|
|
private static final int MIN_SPLASH_TIME = 400;
|
2015-08-06 07:53:30 +02:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Whether or not loading has completed. */
|
2014-07-06 03:00:52 +02:00
|
|
|
private boolean finished = false;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Loading thread. */
|
2015-01-18 21:26:00 +01:00
|
|
|
private Thread thread;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Number of times the 'Esc' key has been pressed. */
|
2015-01-18 21:26:00 +01:00
|
|
|
private int escapeCount = 0;
|
|
|
|
|
2015-06-03 12:23:23 +02:00
|
|
|
/** Whether the skin being loaded is a new skin (for program restarts). */
|
|
|
|
private boolean newSkin = false;
|
|
|
|
|
2015-08-21 17:25:52 +02:00
|
|
|
/** Whether the watch service is newly enabled (for program restarts). */
|
|
|
|
private boolean watchServiceChange = false;
|
|
|
|
|
2015-08-06 07:53:30 +02:00
|
|
|
/** Logo alpha level. */
|
|
|
|
private AnimatedValue logoAlpha;
|
|
|
|
|
2014-07-06 03:00:52 +02:00
|
|
|
// game-related variables
|
2014-07-06 07:58:44 +02:00
|
|
|
private boolean init = false;
|
2014-07-06 03:00:52 +02:00
|
|
|
|
2017-01-17 23:44:12 +01:00
|
|
|
public Splash(DisplayContainer displayContainer, InstanceContainer instanceContainer) {
|
2017-01-17 23:18:12 +01:00
|
|
|
super(displayContainer);
|
2017-01-17 23:44:12 +01:00
|
|
|
this.instanceContainer = instanceContainer;
|
2014-07-06 03:00:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-01-17 23:18:12 +01:00
|
|
|
protected void revalidate() {
|
|
|
|
super.revalidate();
|
2014-07-06 08:27:53 +02:00
|
|
|
|
2017-01-29 18:22:00 +01:00
|
|
|
// TODO d check if below is needed
|
2015-06-03 12:23:23 +02:00
|
|
|
// check if skin changed
|
|
|
|
if (Options.getSkin() != null)
|
|
|
|
this.newSkin = (Options.getSkin().getDirectory() != Options.getSkinDir());
|
|
|
|
|
2015-08-21 17:25:52 +02:00
|
|
|
// check if watch service newly enabled
|
|
|
|
this.watchServiceChange = Options.isWatchServiceEnabled() && BeatmapWatchService.get() == null;
|
|
|
|
|
2014-07-06 03:00:52 +02:00
|
|
|
// load Utils class first (needed in other 'init' methods)
|
2017-01-17 23:18:12 +01:00
|
|
|
Utils.init(displayContainer);
|
2015-01-08 05:45:21 +01:00
|
|
|
|
2015-08-06 07:53:30 +02:00
|
|
|
// fade in logo
|
|
|
|
this.logoAlpha = new AnimatedValue(MIN_SPLASH_TIME, 0f, 1f, AnimationEquation.LINEAR);
|
2015-01-08 05:45:21 +01:00
|
|
|
GameImage.MENU_LOGO.getImage().setAlpha(0f);
|
2017-01-29 18:22:00 +01:00
|
|
|
|
|
|
|
// pre-revalidate some states to reduce lag between switching
|
|
|
|
instanceContainer.provide(SongMenu.class).revalidate();
|
2014-07-06 03:00:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-01-17 23:18:12 +01:00
|
|
|
public void render(Graphics g) {
|
2014-07-06 03:00:52 +02:00
|
|
|
g.setBackground(Color.black);
|
2017-01-17 23:18:12 +01:00
|
|
|
GameImage.MENU_LOGO.getImage().drawCentered(displayContainer.width / 2, displayContainer.height / 2);
|
2015-03-05 19:27:45 +01:00
|
|
|
UI.drawLoadingProgress(g);
|
2014-07-06 03:00:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-01-17 23:44:12 +01:00
|
|
|
public void preRenderUpdate() {
|
2014-07-06 07:58:44 +02:00
|
|
|
if (!init) {
|
|
|
|
init = true;
|
|
|
|
|
2015-06-03 12:23:23 +02:00
|
|
|
// resources already loaded (from application restart)
|
2015-05-17 03:25:19 +02:00
|
|
|
if (BeatmapSetList.get() != null) {
|
2015-08-21 17:25:52 +02:00
|
|
|
if (newSkin || watchServiceChange) { // need to reload resources
|
2015-06-03 12:23:23 +02:00
|
|
|
thread = new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-08-21 17:25:52 +02:00
|
|
|
// reload beatmaps if watch service newly enabled
|
|
|
|
if (watchServiceChange)
|
|
|
|
BeatmapParser.parseAllFiles(Options.getBeatmapDir());
|
|
|
|
|
|
|
|
// reload sounds if skin changed
|
2015-06-03 12:23:23 +02:00
|
|
|
// TODO: only reload each sound if actually needed?
|
2015-08-21 17:25:52 +02:00
|
|
|
if (newSkin)
|
|
|
|
SoundController.init();
|
2015-06-03 12:23:23 +02:00
|
|
|
|
|
|
|
finished = true;
|
|
|
|
thread = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
thread.start();
|
|
|
|
} else // don't reload anything
|
|
|
|
finished = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// load all resources in a new thread
|
|
|
|
else {
|
2015-01-21 01:24:59 +01:00
|
|
|
thread = new Thread() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-01-21 01:01:18 +01:00
|
|
|
File beatmapDir = Options.getBeatmapDir();
|
|
|
|
|
|
|
|
// unpack all OSZ archives
|
|
|
|
OszUnpacker.unpackAllFiles(Options.getOSZDir(), beatmapDir);
|
2015-01-21 01:24:59 +01:00
|
|
|
|
2015-01-21 01:01:18 +01:00
|
|
|
// parse song directory
|
2015-05-29 09:07:58 +02:00
|
|
|
BeatmapParser.parseAllFiles(beatmapDir);
|
2015-06-30 02:22:38 +02:00
|
|
|
|
2015-04-05 17:24:05 +02:00
|
|
|
// import replays
|
2015-04-02 04:10:36 +02:00
|
|
|
ReplayImporter.importAllReplaysFromDir(Options.getReplayImportDir());
|
2015-01-21 01:01:18 +01:00
|
|
|
|
|
|
|
// load sounds
|
|
|
|
SoundController.init();
|
2014-07-06 07:58:44 +02:00
|
|
|
|
2015-01-21 01:24:59 +01:00
|
|
|
finished = true;
|
2015-01-21 07:38:02 +01:00
|
|
|
thread = null;
|
2015-01-21 01:24:59 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
thread.start();
|
|
|
|
}
|
2014-07-06 07:58:44 +02:00
|
|
|
}
|
|
|
|
|
2014-07-06 03:00:52 +02:00
|
|
|
// fade in logo
|
2017-01-17 23:44:12 +01:00
|
|
|
if (logoAlpha.update(displayContainer.renderDelta))
|
2015-08-06 07:53:30 +02:00
|
|
|
GameImage.MENU_LOGO.getImage().setAlpha(logoAlpha.getValue());
|
2014-07-06 03:00:52 +02:00
|
|
|
|
|
|
|
// change states when loading complete
|
2015-08-06 07:53:30 +02:00
|
|
|
if (finished && logoAlpha.getValue() >= 1f) {
|
2014-08-25 05:48:52 +02:00
|
|
|
// initialize song list
|
2015-05-17 03:25:19 +02:00
|
|
|
if (BeatmapSetList.get().size() > 0) {
|
|
|
|
BeatmapSetList.get().init();
|
2017-01-17 23:44:12 +01:00
|
|
|
if (Options.isThemeSongEnabled()) {
|
2015-02-02 06:15:16 +01:00
|
|
|
MusicController.playThemeSong();
|
2017-01-17 23:44:12 +01:00
|
|
|
} else {
|
|
|
|
instanceContainer.provide(SongMenu.class).setFocus(BeatmapSetList.get().getRandomNode(), -1, true, true);
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-21 00:17:04 +01:00
|
|
|
MusicController.playThemeSong();
|
2017-01-17 23:44:12 +01:00
|
|
|
}
|
|
|
|
displayContainer.switchState(MainMenu.class);
|
2014-08-25 05:48:52 +02:00
|
|
|
}
|
2014-07-06 03:00:52 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 19:44:54 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCloseRequest() {
|
|
|
|
if (thread != null && thread.isAlive()) {
|
|
|
|
thread.interrupt();
|
|
|
|
try {
|
|
|
|
thread.join();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.warn("InterruptedException while waiting for splash thread to die", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-06 03:00:52 +02:00
|
|
|
@Override
|
2017-01-17 23:18:12 +01:00
|
|
|
public boolean keyPressed(int key, char c) {
|
2017-01-18 16:55:30 +01:00
|
|
|
if (key != Input.KEY_ESCAPE) {
|
|
|
|
return false;
|
2015-01-27 21:20:03 +01:00
|
|
|
}
|
2017-01-18 16:55:30 +01:00
|
|
|
if (++escapeCount >= 3) {
|
|
|
|
displayContainer.exitRequested = true;
|
|
|
|
} else if (thread != null) {
|
|
|
|
thread.interrupt();
|
|
|
|
}
|
|
|
|
return true;
|
2014-07-06 08:27:53 +02:00
|
|
|
}
|
2014-07-06 03:00:52 +02:00
|
|
|
}
|