Initial commit.

This commit is contained in:
Jeffrey Han
2014-06-29 22:17:04 -04:00
commit 9da166f60f
3902 changed files with 974477 additions and 0 deletions

View File

@@ -0,0 +1,774 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014 Jeffrey Han
*
* 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;
import itdelatrisu.opsu.GUIMenuButton;
import itdelatrisu.opsu.GameScore;
import itdelatrisu.opsu.MusicController;
import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuFile;
import itdelatrisu.opsu.OsuHitObject;
import itdelatrisu.opsu.OsuTimingPoint;
import itdelatrisu.opsu.objects.Circle;
import itdelatrisu.opsu.objects.Slider;
import itdelatrisu.opsu.objects.Spinner;
import java.util.HashMap;
import java.util.Stack;
import org.lwjgl.input.Keyboard;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.EmptyTransition;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.util.Log;
/**
* "Game" state.
*/
public class Game extends BasicGameState {
/**
* Game restart states.
*/
public static final byte
RESTART_FALSE = 0,
RESTART_NEW = 1, // first time loading song
RESTART_MANUAL = 2, // retry
RESTART_LOSE = 3; // health is zero: no-continue/force restart
/**
* Current restart state.
*/
private static byte restart;
/**
* The associated OsuFile object.
*/
private static OsuFile osu;
/**
* The associated GameScore object (holds all score data).
*/
private static GameScore score;
/**
* Current hit object index in OsuHitObject[] array.
*/
private int objectIndex = 0;
/**
* This map's hit circles objects, keyed by objectIndex.
*/
private HashMap<Integer, Circle> circles;
/**
* This map's slider objects, keyed by objectIndex.
*/
private HashMap<Integer, Slider> sliders;
/**
* This map's spinner objects, keyed by objectIndex.
*/
private HashMap<Integer, Spinner> spinners;
/**
* Delay time, in milliseconds, before song starts.
*/
private static int leadInTime;
/**
* Hit object approach time, in milliseconds.
*/
private int approachTime;
/**
* Time offsets for obtaining each hit result (indexed by HIT_* constants).
*/
private int[] hitResultOffset;
/**
* Time, in milliseconds, between the first and last hit object.
*/
private int mapLength;
/**
* Current break index in breaks ArrayList.
*/
private int breakIndex;
/**
* Warning arrows, pointing right and left.
*/
private Image warningArrowR, warningArrowL;
/**
* Section pass and fail images (displayed at start of break, when necessary).
*/
private Image breakStartPass, breakStartFail;
/**
* Break start time (0 if not in break).
*/
private int breakTime = 0;
/**
* Skip button (displayed at song start, when necessary).
*/
private GUIMenuButton skipButton;
/**
* Minimum time before start of song, in milliseconds, to process skip-related actions.
*/
private final int skipOffsetTime = 2000;
/**
* Current timing point index in timingPoints ArrayList.
*/
private int timingPointIndex;
/**
* Current beat lengths (base value and inherited value).
*/
private float beatLengthBase, beatLength;
/**
* Countdown-related images.
*/
private Image
countdownReady, // "READY?" text
countdown3, // "3" text
countdown1, // "2" text
countdown2, // "1" text
countdownGo; // "GO!" text
/**
* Glowing hit circle outline which must be clicked when returning from pause menu.
*/
private Image hitCircleSelect;
/**
* Mouse coordinates before game paused.
*/
private int pausedMouseX = -1, pausedMouseY = -1;
/**
* Track position when game paused.
*/
private int pauseTime = -1;
/**
* Value for handling hitCircleSelect pulse effect (expanding, alpha level).
*/
private float pausePulse;
// game-related variables
private GameContainer container;
private StateBasedGame game;
private Input input;
private int state;
public Game(int state) {
this.state = state;
}
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
this.container = container;
this.game = game;
input = container.getInput();
int width = container.getWidth();
int height = container.getHeight();
// spinners have fixed properties, and only need to be initialized once
Spinner.init(container);
// breaks
breakStartPass = new Image("section-pass.png");
breakStartFail = new Image("section-fail.png");
warningArrowR = new Image("play-warningarrow.png");
warningArrowL = warningArrowR.getFlippedCopy(true, false);
// skip button
Image skip = new Image("play-skip.png");
float skipScale = (height * 0.1f) / skip.getHeight();
skip = skip.getScaledCopy(skipScale);
skipButton = new GUIMenuButton(skip,
width - (skip.getWidth() / 2f),
height - (skip.getHeight() / 2f));
// countdown
float countdownHeight = height / 3f;
countdownReady = new Image("ready.png");
countdownReady = countdownReady.getScaledCopy(countdownHeight / countdownReady.getHeight());
countdown3 = new Image("count3.png");
countdown3 = countdown3.getScaledCopy(countdownHeight / countdown3.getHeight());
countdown2 = new Image("count2.png");
countdown2 = countdown2.getScaledCopy(countdownHeight / countdown2.getHeight());
countdown1 = new Image("count1.png");
countdown1 = countdown1.getScaledCopy(countdownHeight / countdown1.getHeight());
countdownGo = new Image("go.png");
countdownGo = countdownGo.getScaledCopy(countdownHeight / countdownGo.getHeight());
// hit circle select
hitCircleSelect = new Image("hitcircleselect.png");
// create the associated GameScore object
score = new GameScore(width, height);
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
int width = container.getWidth();
int height = container.getHeight();
// background
if (!osu.drawBG(width, height, 0.7f))
g.setBackground(Color.black);
Options.drawFPS();
int trackPosition = MusicController.getPosition();
if (pauseTime > -1) // returning from pause screen
trackPosition = pauseTime;
// break periods
if (osu.breaks != null && breakIndex < osu.breaks.size()) {
if (breakTime > 0) {
int endTime = osu.breaks.get(breakIndex);
int breakLength = endTime - breakTime;
// letterbox effect (black bars on top/bottom)
if (osu.letterboxInBreaks && breakLength >= 4000) {
g.setColor(Color.black);
g.fillRect(0, 0, width, height * 0.125f);
g.fillRect(0, height * 0.875f, width, height * 0.125f);
}
score.drawGameElements(g, mapLength, true, objectIndex == 0);
if (breakLength >= 8000 &&
trackPosition - breakTime > 2000 &&
trackPosition - breakTime < 5000) {
// show break start
if (score.getHealth() >= 50)
breakStartPass.drawCentered(width / 2f, height / 2f);
else
breakStartFail.drawCentered(width / 2f, height / 2f);
} else if (breakLength >= 4000) {
// show break end (flash twice for 500ms)
int endTimeDiff = endTime - trackPosition;
if ((endTimeDiff > 1500 && endTimeDiff < 2000) ||
(endTimeDiff > 500 && endTimeDiff < 1000)) {
warningArrowR.draw(width * 0.15f, height * 0.15f);
warningArrowR.draw(width * 0.15f, height * 0.75f);
warningArrowL.draw(width * 0.75f, height * 0.15f);
warningArrowL.draw(width * 0.75f, height * 0.75f);
}
}
return;
}
}
// game elements
score.drawGameElements(g, mapLength, false, objectIndex == 0);
// first object...
if (objectIndex == 0) {
// skip beginning
if (osu.objects[objectIndex].time - skipOffsetTime > 5000 &&
trackPosition < osu.objects[objectIndex].time - skipOffsetTime)
skipButton.draw();
// mod icons
if (trackPosition < osu.objects[objectIndex].time) {
for (int i = Options.MOD_MAX - 1; i >= 0; i--) {
if (Options.isModActive(i)) {
Image modImage = Options.getModImage(i);
modImage.draw(
(width * 0.85f) + ((i - (Options.MOD_MAX / 2)) * modImage.getWidth() / 3f),
height / 10f
);
}
}
}
}
if (isLeadIn())
trackPosition = leadInTime * -1; // render approach circles during song lead-in
// countdown
if (osu.countdown > 0) { // TODO: implement half/double rate settings
int timeDiff = osu.objects[0].time - trackPosition;
if (timeDiff >= 500 && timeDiff < 3000) {
if (timeDiff >= 1500)
countdownReady.drawCentered(width / 2, height / 2);
if (timeDiff < 2000)
countdown3.draw(0, 0);
if (timeDiff < 1500)
countdown2.draw(width - countdown2.getWidth(), 0);
if (timeDiff < 1000)
countdown1.drawCentered(width / 2, height / 2);
} else if (timeDiff >= -500 && timeDiff < 500) {
countdownGo.setAlpha((timeDiff < 0) ? 1 - (timeDiff / -1000f) : 1);
countdownGo.drawCentered(width / 2, height / 2);
}
}
// draw hit objects in reverse order, or else overlapping objects are unreadable
Stack<Integer> stack = new Stack<Integer>();
for (int i = objectIndex; i < osu.objects.length && osu.objects[i].time < trackPosition + approachTime; i++)
stack.add(i);
while (!stack.isEmpty()) {
int i = stack.pop();
OsuHitObject hitObject = osu.objects[i];
if ((hitObject.type & OsuHitObject.TYPE_CIRCLE) > 0)
circles.get(i).draw(trackPosition);
else if ((hitObject.type & OsuHitObject.TYPE_SLIDER) > 0)
sliders.get(i).draw(trackPosition, stack.isEmpty());
else if ((hitObject.type & OsuHitObject.TYPE_SPINNER) > 0) {
if (stack.isEmpty()) // only draw spinner at objectIndex
spinners.get(i).draw(trackPosition, g);
else
continue;
}
}
// draw OsuHitObjectResult objects
score.drawHitResults(trackPosition);
// returning from pause screen
if (pauseTime > -1 && pausedMouseX > -1 && pausedMouseY > -1) {
// darken the screen
g.setColor(Options.COLOR_BLACK_ALPHA);
g.fillRect(0, 0, width, height);
// draw glowing hit select circle and pulse effect
int circleRadius = Circle.getHitCircle().getWidth();
Image cursorCircle = hitCircleSelect.getScaledCopy(circleRadius, circleRadius);
cursorCircle.setAlpha(1.0f);
cursorCircle.drawCentered(pausedMouseX, pausedMouseY);
Image cursorCirclePulse = cursorCircle.getScaledCopy(1f + pausePulse);
cursorCirclePulse.setAlpha(1f - pausePulse);
cursorCirclePulse.drawCentered(pausedMouseX, pausedMouseY);
}
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
if (isLeadIn()) { // stop updating during song lead-in
leadInTime -= delta;
if (!isLeadIn())
MusicController.playAt(0, false);
return;
}
// returning from pause screen: must click previous mouse position
if (pauseTime > -1) {
// paused during lead-in or break: continue immediately
if (pausedMouseX < 0 && pausedMouseY < 0) {
pauseTime = -1;
if (!isLeadIn())
MusicController.resume();
}
// focus lost: go back to pause screen
else if (!container.hasFocus()) {
game.enterState(Opsu.STATE_GAMEPAUSEMENU);
pausePulse = 0f;
}
// advance pulse animation
else {
pausePulse += delta / 750f;
if (pausePulse > 1f)
pausePulse = 0f;
}
return;
}
// map complete!
if (objectIndex >= osu.objects.length) {
game.enterState(Opsu.STATE_GAMERANKING, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
return;
}
int trackPosition = MusicController.getPosition();
// timing points
if (timingPointIndex < osu.timingPoints.size()) {
OsuTimingPoint timingPoint = osu.timingPoints.get(timingPointIndex);
if (trackPosition >= timingPoint.time) {
if (timingPoint.velocity >= 0)
beatLengthBase = beatLength = timingPoint.beatLength;
else
beatLength = beatLengthBase * (timingPoint.velocity / -100f);
timingPointIndex++;
}
}
// song beginning
if (objectIndex == 0) {
if (trackPosition < osu.objects[0].time)
return; // nothing to do here
}
// break periods
if (osu.breaks != null && breakIndex < osu.breaks.size()) {
int breakValue = osu.breaks.get(breakIndex);
if (breakTime > 0) { // in a break period
if (trackPosition < breakValue)
return;
else {
// break is over
breakTime = 0;
breakIndex++;
}
} else if (trackPosition >= breakValue) {
// start a break
breakTime = breakValue;
breakIndex++;
return;
}
}
// pause game if focus lost
if (!container.hasFocus() && !Options.isModActive(Options.MOD_AUTO)) {
if (pauseTime < 0) {
pausedMouseX = input.getMouseX();
pausedMouseY = input.getMouseY();
pausePulse = 0f;
}
if (MusicController.isPlaying() || isLeadIn())
pauseTime = trackPosition;
game.enterState(Opsu.STATE_GAMEPAUSEMENU);
}
// drain health
score.changeHealth(delta / -200f);
if (!score.isAlive()) {
// game over, force a restart
restart = RESTART_LOSE;
game.enterState(Opsu.STATE_GAMEPAUSEMENU);
}
score.updateComboBurst(delta);
// update objects (loop in unlikely event of any skipped indexes)
while (objectIndex < osu.objects.length && trackPosition > osu.objects[objectIndex].time) {
OsuHitObject hitObject = osu.objects[objectIndex];
// check if we've already passed the next object's start time
boolean overlap = (objectIndex + 1 < osu.objects.length &&
trackPosition > osu.objects[objectIndex + 1].time - hitResultOffset[GameScore.HIT_300]);
// check completion status of the hit object
boolean done = false;
if ((hitObject.type & OsuHitObject.TYPE_CIRCLE) > 0)
done = circles.get(objectIndex).update(overlap);
else if ((hitObject.type & OsuHitObject.TYPE_SLIDER) > 0)
done = sliders.get(objectIndex).update(overlap, delta, input.getMouseX(), input.getMouseY());
else if ((hitObject.type & OsuHitObject.TYPE_SPINNER) > 0)
done = spinners.get(objectIndex).update(overlap, delta, input.getMouseX(), input.getMouseY());
// increment object index?
if (done)
objectIndex++;
else
break;
}
}
@Override
public int getID() { return state; }
@Override
public void keyPressed(int key, char c) {
switch (key) {
case Input.KEY_ESCAPE:
// pause game
int trackPosition = MusicController.getPosition();
if (pauseTime < 0 && breakTime <= 0 && trackPosition >= osu.objects[0].time) {
pausedMouseX = input.getMouseX();
pausedMouseY = input.getMouseY();
pausePulse = 0f;
}
if (MusicController.isPlaying() || isLeadIn())
pauseTime = trackPosition;
game.enterState(Opsu.STATE_GAMEPAUSEMENU, new EmptyTransition(), new FadeInTransition(Color.black));
break;
case Input.KEY_SPACE:
// skip
skipIntro();
break;
case Input.KEY_Z:
// left-click
if (!Keyboard.isRepeatEvent())
mousePressed(Input.MOUSE_LEFT_BUTTON, input.getMouseX(), input.getMouseY());
break;
case Input.KEY_X:
// right-click
if (!Keyboard.isRepeatEvent())
mousePressed(Input.MOUSE_RIGHT_BUTTON, input.getMouseX(), input.getMouseY());
break;
case Input.KEY_F12:
Options.takeScreenShot();
break;
}
}
@Override
public void mousePressed(int button, int x, int y) {
if (button == Input.MOUSE_MIDDLE_BUTTON)
return;
// returning from pause screen
if (pauseTime > -1) {
double distance = Math.hypot(pausedMouseX - x, pausedMouseY - y);
int circleRadius = Circle.getHitCircle().getWidth() / 2;
if (distance < circleRadius) {
// unpause the game
pauseTime = -1;
pausedMouseX = -1;
pausedMouseY = -1;
if (!Game.isLeadIn())
MusicController.resume();
}
return;
}
if (objectIndex >= osu.objects.length) // nothing left to do here
return;
OsuHitObject hitObject = osu.objects[objectIndex];
// skip beginning
if (skipButton.contains(x, y)) {
if (skipIntro())
return; // successfully skipped
}
// "auto" mod: ignore user actions
if (Options.isModActive(Options.MOD_AUTO))
return;
// circles
if ((hitObject.type & OsuHitObject.TYPE_CIRCLE) > 0) {
boolean hit = circles.get(objectIndex).mousePressed(x, y);
if (hit)
objectIndex++;
}
// sliders
else if ((hitObject.type & OsuHitObject.TYPE_SLIDER) > 0)
sliders.get(objectIndex).mousePressed(x, y);
}
@Override
public void enter(GameContainer container, StateBasedGame game)
throws SlickException {
if (osu == null || osu.objects == null)
throw new RuntimeException("Running game with no OsuFile loaded.");
// restart the game
if (restart != RESTART_FALSE) {
// new game
if (restart == RESTART_NEW) {
setMapModifiers();
// calculate map length (TODO: end on slider?)
OsuHitObject lastObject = osu.objects[osu.objects.length - 1];
int endTime;
if ((lastObject.type & OsuHitObject.TYPE_SPINNER) > 0)
endTime = lastObject.endTime;
else
endTime = lastObject.time;
mapLength = endTime - osu.objects[0].time;
}
// initialize object maps
circles = new HashMap<Integer, Circle>();
sliders = new HashMap<Integer, Slider>();
spinners = new HashMap<Integer, Spinner>();
for (int i = 0; i < osu.objects.length; i++) {
OsuHitObject hitObject = osu.objects[i];
// is this the last note in the combo?
boolean comboEnd = false;
if (i + 1 < osu.objects.length &&
(osu.objects[i + 1].type & OsuHitObject.TYPE_NEWCOMBO) > 0)
comboEnd = true;
if ((hitObject.type & OsuHitObject.TYPE_CIRCLE) > 0) {
circles.put(i, new Circle(hitObject, this, score, osu.combo[hitObject.comboIndex], comboEnd));
} else if ((hitObject.type & OsuHitObject.TYPE_SLIDER) > 0) {
sliders.put(i, new Slider(hitObject, this, score, osu.combo[hitObject.comboIndex], comboEnd));
} else if ((hitObject.type & OsuHitObject.TYPE_SPINNER) > 0) {
spinners.put(i, new Spinner(hitObject, this, score));
}
}
// reset indexes
MusicController.setPosition(0);
MusicController.pause();
score.clear();
objectIndex = 0;
breakIndex = 0;
breakTime = 0;
timingPointIndex = 0;
pauseTime = -1;
pausedMouseX = -1;
pausedMouseY = -1;
// load the first timingPoint
if (!osu.timingPoints.isEmpty() && osu.timingPoints.get(0).velocity >= 0) {
beatLengthBase = beatLength = osu.timingPoints.get(0).beatLength;
timingPointIndex++;
}
leadInTime = osu.audioLeadIn + approachTime;
restart = RESTART_FALSE;
}
}
/**
* Skips the beginning of a track.
* @return true if skipped, false otherwise
*/
private boolean skipIntro() {
int trackPosition = MusicController.getPosition();
if (objectIndex == 0 &&
osu.objects[0].time - skipOffsetTime > 4000 &&
trackPosition < osu.objects[0].time - skipOffsetTime) {
if (isLeadIn()) {
leadInTime = 0;
MusicController.resume();
}
MusicController.setPosition(osu.objects[0].time - skipOffsetTime);
return true;
}
return false;
}
/**
* Returns true if an input key is pressed (mouse left/right, keyboard Z/X).
*/
public boolean isInputKeyPressed() {
return (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) ||
input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON) ||
input.isKeyDown(Input.KEY_Z) || input.isKeyDown(Input.KEY_X));
}
/**
* Set map modifiers.
*/
private void setMapModifiers() {
try {
// map-based properties, so re-initialize each game
byte circleSize = osu.circleSize;
byte approachRate = osu.approachRate;
byte overallDifficulty = osu.overallDifficulty;
byte HPDrainRate = osu.HPDrainRate;
if (Options.isModActive(Options.MOD_HARD_ROCK)) { // hard rock modifiers
circleSize = (byte) Math.max(circleSize - 1, 0);
approachRate = (byte) Math.min(approachRate + 3, 10);
overallDifficulty = (byte) Math.min(overallDifficulty + 3, 10);
HPDrainRate = (byte) Math.min(HPDrainRate + 3, 10);
}
Circle.init(container, circleSize);
Slider.init(container, circleSize, osu);
// approachRate (hit object approach time)
if (approachRate < 5)
approachTime = 1800 - (approachRate * 120);
else
approachTime = 1200 - ((approachRate - 5) * 150);
// overallDifficulty (hit result time offsets)
hitResultOffset = new int[GameScore.HIT_MAX];
hitResultOffset[GameScore.HIT_300] = 78 - (overallDifficulty * 6);
hitResultOffset[GameScore.HIT_100] = 138 - (overallDifficulty * 8);
hitResultOffset[GameScore.HIT_50] = 198 - (overallDifficulty * 10);
hitResultOffset[GameScore.HIT_MISS] = 500 - (overallDifficulty * 10);
// HPDrainRate (health change), overallDifficulty (scoring)
score.setDrainRate(HPDrainRate);
score.setDifficulty(overallDifficulty);
} catch (SlickException e) {
Log.error("Error while setting map modifiers.", e);
}
}
/**
* Sets/returns whether entering the state will restart it.
*/
public static void setRestart(byte restart) { Game.restart = restart; }
public static byte getRestart() { return Game.restart; }
/**
* Sets or returns the associated OsuFile.
*/
public static void setOsuFile(OsuFile osu) { Game.osu = osu; }
public static OsuFile getOsuFile() { return osu; }
/**
* Returns the associated GameScore object.
*/
public static GameScore getGameScore() { return score; }
/**
* Returns whether or not the track is in the lead-in time state.
*/
public static boolean isLeadIn() { return leadInTime > 0; }
/**
* Returns the object approach time, in milliseconds.
*/
public int getApproachTime() { return approachTime; }
/**
* Returns an array of hit result offset times, in milliseconds (indexed by GameScore.HIT_* constants).
*/
public int[] getHitResultOffsets() { return hitResultOffset; }
/**
* Returns the beat length.
*/
public float getBeatLength() { return beatLength; }
/**
* Returns the slider multiplier given by the current timing point.
*/
public float getTimingPointMultiplier() { return beatLength / beatLengthBase; }
}

View File

@@ -0,0 +1,195 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014 Jeffrey Han
*
* 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;
import itdelatrisu.opsu.GUIMenuButton;
import itdelatrisu.opsu.MusicController;
import itdelatrisu.opsu.Opsu;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
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;
/**
* "Game Paused" state.
* <ul>
* <li>[Continue] - unpause game (return to game state)
* <li>[Retry] - restart game (return to game state)
* <li>[Back] - return to song menu state
* </ul>
*/
public class GamePauseMenu extends BasicGameState {
/**
* Music fade-out time, in milliseconds.
*/
private static final int FADEOUT_TIME = 1000;
/**
* Track position when the pause menu was loaded (for FADEOUT_TIME).
*/
private long pauseStartTime;
/**
* "Continue", "Retry", and "Back" buttons.
*/
private GUIMenuButton continueButton, retryButton, backButton;
/**
* Background image for pause menu (optional).
*/
private Image backgroundImage;
/**
* Background image for fail menu (optional).
*/
private Image failImage;
// game-related variables
private StateBasedGame game;
private int state;
public GamePauseMenu(int state) {
this.state = state;
}
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
this.game = game;
int width = container.getWidth();
int height = container.getHeight();
// initialize buttons
continueButton = new GUIMenuButton(new Image("pause-continue.png"), width / 2f, height * 0.25f);
retryButton = new GUIMenuButton(new Image("pause-retry.png"), width / 2f, height * 0.5f);
backButton = new GUIMenuButton(new Image("pause-back.png"), width / 2f, height * 0.75f);
// pause background image
try {
backgroundImage = new Image("pause-overlay.png").getScaledCopy(width, height);
backgroundImage.setAlpha(0.7f);
} catch (Exception e) {
// optional
}
// fail image
try {
failImage = new Image("fail-background.png").getScaledCopy(width, height);
failImage.setAlpha(0.7f);
} catch (Exception e) {
// optional
}
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
// background
if (backgroundImage != null && Game.getRestart() != Game.RESTART_LOSE)
backgroundImage.draw();
else if (failImage != null && Game.getRestart() == Game.RESTART_LOSE)
failImage.draw();
else
g.setBackground(Color.black);
Options.drawFPS();
// draw buttons
if (Game.getRestart() != Game.RESTART_LOSE)
continueButton.draw();
retryButton.draw();
backButton.draw();
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
// empty
}
@Override
public int getID() { return state; }
@Override
public void keyPressed(int key, char c) {
switch (key) {
case Input.KEY_ESCAPE:
// 'esc' will normally unpause, but will return to song menu if health is zero
if (Game.getRestart() == Game.RESTART_LOSE) {
MusicController.stop();
MusicController.playAt(Game.getOsuFile().previewTime, true);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
} else
unPause(Game.RESTART_FALSE);
break;
case Input.KEY_F12:
Options.takeScreenShot();
break;
}
}
@Override
public void mousePressed(int button, int x, int y) {
// check mouse button
if (button != Input.MOUSE_LEFT_BUTTON)
return;
boolean loseState = (Game.getRestart() == Game.RESTART_LOSE);
// if music faded out (i.e. health is zero), don't process any actions before FADEOUT_TIME
if (loseState && System.currentTimeMillis() - pauseStartTime < FADEOUT_TIME)
return;
if (continueButton.contains(x, y) && !loseState)
unPause(Game.RESTART_FALSE);
else if (retryButton.contains(x, y)) {
unPause(Game.RESTART_MANUAL);
} else if (backButton.contains(x, y)) {
MusicController.pause(); // lose state
MusicController.playAt(Game.getOsuFile().previewTime, true);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
}
}
@Override
public void enter(GameContainer container, StateBasedGame game)
throws SlickException {
pauseStartTime = System.currentTimeMillis();
if (Game.getRestart() == Game.RESTART_LOSE)
MusicController.fadeOut(FADEOUT_TIME);
else
MusicController.pause();
}
/**
* Unpause and return to the Game state.
*/
private void unPause(byte restart) {
Game.setRestart(restart);
game.enterState(Opsu.STATE_GAME);
}
}

View File

@@ -0,0 +1,180 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014 Jeffrey Han
*
* 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;
import itdelatrisu.opsu.GUIMenuButton;
import itdelatrisu.opsu.GameScore;
import itdelatrisu.opsu.MusicController;
import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuFile;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
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;
/**
* "Game Ranking" (score card) state.
* <ul>
* <li>[Retry] - restart game (return to game state)
* <li>[Exit] - return to main menu state
* <li>[Back] - return to song menu state
* </ul>
*/
public class GameRanking extends BasicGameState {
/**
* Associated GameScore object.
*/
private static GameScore score;
/**
* "Retry" and "Exit" buttons.
*/
private GUIMenuButton retryButton, exitButton;
// game-related variables
private StateBasedGame game;
private int state;
public GameRanking(int state) {
this.state = state;
}
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
this.game = game;
score = Game.getGameScore();
int width = container.getWidth();
int height = container.getHeight();
// buttons
Image retry = new Image("ranking-retry.png");
Image exit = new Image("ranking-back.png");
float scale = (height * 0.15f) / retry.getHeight();
retry = retry.getScaledCopy(scale);
exit = exit.getScaledCopy(scale);
retryButton = new GUIMenuButton(retry,
width - (retry.getWidth() / 2f),
(height * 0.97f) - (exit.getHeight() * 1.5f)
);
exitButton = new GUIMenuButton(exit,
width - (exit.getWidth() / 2f),
(height * 0.97f) - (exit.getHeight() / 2f)
);
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
int width = container.getWidth();
int height = container.getHeight();
OsuFile osu = Game.getOsuFile();
// background
if (!osu.drawBG(width, height, 0.7f))
g.setBackground(Options.COLOR_BLACK_ALPHA);
// ranking screen elements
score.drawRankingElements(g, width, height);
// game mods
for (int i = Options.MOD_MAX - 1; i >= 0; i--) {
if (Options.isModActive(i)) {
Image modImage = Options.getModImage(i);
modImage.draw(
(width * 0.75f) + ((i - (Options.MOD_MAX / 2)) * modImage.getWidth() / 3f),
height / 2f
);
}
}
// header text
g.setColor(Color.white);
Options.FONT_LARGE.drawString(10, 0,
String.format("%s - %s [%s]", osu.artist, osu.title, osu.version));
Options.FONT_MEDIUM.drawString(10, Options.FONT_LARGE.getLineHeight() - 6,
String.format("Beatmap by %s", osu.creator));
// buttons
retryButton.draw();
exitButton.draw();
Options.getBackButton().draw();
Options.drawFPS();
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
// empty
}
@Override
public int getID() { return state; }
@Override
public void keyPressed(int key, char c) {
switch (key) {
case Input.KEY_ESCAPE:
MusicController.playAt(Game.getOsuFile().previewTime, true);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
break;
case Input.KEY_F12:
Options.takeScreenShot();
break;
}
}
@Override
public void mousePressed(int button, int x, int y) {
// check mouse button
if (button != Input.MOUSE_LEFT_BUTTON)
return;
if (retryButton.contains(x, y)) {
OsuFile osu = Game.getOsuFile();
Display.setTitle(String.format("%s - %s", game.getTitle(), osu.toString()));
Game.setRestart(Game.RESTART_MANUAL);
game.enterState(Opsu.STATE_GAME, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
} else if (exitButton.contains(x, y))
game.enterState(Opsu.STATE_MAINMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
else if (Options.getBackButton().contains(x, y)) {
MusicController.stop();
MusicController.playAt(Game.getOsuFile().previewTime, true);
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
}
}
@Override
public void enter(GameContainer container, StateBasedGame game)
throws SlickException {
Display.setTitle(game.getTitle());
}
}

View File

@@ -0,0 +1,319 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014 Jeffrey Han
*
* 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;
import itdelatrisu.opsu.GUIMenuButton;
import itdelatrisu.opsu.MusicController;
import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuGroupNode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
import java.util.concurrent.TimeUnit;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
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;
/**
* "Main Menu" state.
* <ul>
* <li>[Play] - move to song selection menu
* <li>[Exit] - move to confirm exit menu
* </ul>
*/
public class MainMenu extends BasicGameState {
/**
* Idle time, in milliseconds, before returning the logo to its original position.
*/
private static final short MOVE_DELAY = 5000;
/**
* Logo button that reveals other buttons on click.
*/
private GUIMenuButton logo;
/**
* Whether or not the logo has been clicked.
*/
private boolean logoClicked = false;
/**
* Delay timer, in milliseconds, before starting to move the logo back to the center.
*/
private int logoTimer = 0;
/**
* Main "Play" and "Exit" buttons.
*/
private GUIMenuButton playButton, exitButton;
/**
* Music control buttons.
*/
private GUIMenuButton musicPlay, musicPause, musicNext, musicPrevious;
/**
* Application start time, for drawing the total running time.
*/
private long osuStartTime;
/**
* Indexes of previous songs.
*/
private static Stack<Integer> previous;
/**
* Main menu background image (optional).
*/
private Image backgroundImage;
// game-related variables
private StateBasedGame game;
private int state;
public MainMenu(int state) {
this.state = state;
}
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
this.game = game;
osuStartTime = System.currentTimeMillis();
previous = new Stack<Integer>();
int width = container.getWidth();
int height = container.getHeight();
// initialize buttons
Image logoImg = new Image("logo.png");
float buttonScale = (height / 1.2f) / logoImg.getHeight();
Image logoImgScaled = logoImg.getScaledCopy(buttonScale);
logo = new GUIMenuButton(logoImgScaled, width / 2f, height / 2f);
Image playImg = new Image("menu-play.png");
Image exitImg = new Image("menu-exit.png");
playImg = playImg.getScaledCopy((logoImg.getWidth() * 0.83f) / playImg.getWidth());
exitImg = exitImg.getScaledCopy((logoImg.getWidth() * 0.66f) / exitImg.getWidth());
float exitOffset = (playImg.getWidth() - exitImg.getWidth()) / 3f;
playButton = new GUIMenuButton(playImg.getScaledCopy(buttonScale),
width * 0.75f, (height / 2) - (logoImgScaled.getHeight() / 5f)
);
exitButton = new GUIMenuButton(exitImg.getScaledCopy(buttonScale),
width * 0.75f - exitOffset, (height / 2) + (exitImg.getHeight() / 2f)
);
// initialize music buttons
int musicWidth = 48;
int musicHeight = 30;
musicPlay = new GUIMenuButton(new Image("music-play.png"), width - (2 * musicWidth), musicHeight);
musicPause = new GUIMenuButton(new Image("music-pause.png"), width - (2 * musicWidth), musicHeight);
musicNext = new GUIMenuButton(new Image("music-next.png"), width - musicWidth, musicHeight);
musicPrevious = new GUIMenuButton(new Image("music-previous.png"), width - (3 * musicWidth), musicHeight);
// menu background
try {
backgroundImage = new Image("menu-background.jpg").getScaledCopy(width, height);
backgroundImage.setAlpha(0.9f);
} catch (Exception e) {
// optional
}
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
if (backgroundImage != null)
backgroundImage.draw();
else
g.setBackground(Options.COLOR_BLUE_BACKGROUND);
g.setFont(Options.FONT_MEDIUM);
int width = container.getWidth();
int height = container.getHeight();
// draw buttons
if (logoTimer > 0) {
playButton.draw();
exitButton.draw();
}
logo.draw();
// draw music buttons
if (MusicController.isPlaying())
musicPause.draw();
else
musicPlay.draw();
musicNext.draw();
musicPrevious.draw();
g.setColor(Options.COLOR_BLACK_ALPHA);
g.fillRoundRect(width - 168, 54, 148, 5, 4);
g.setColor(Color.white);
if (!MusicController.isConverting())
g.fillRoundRect(width - 168, 54,
148f * MusicController.getPosition() / MusicController.getTrackLength(), 5, 4);
// draw text
int lineHeight = Options.FONT_MEDIUM.getLineHeight();
g.drawString(String.format("Loaded %d songs and %d beatmaps.",
Opsu.groups.size(), Opsu.groups.getMapCount()), 25, 25);
if (MusicController.isConverting())
g.drawString("Track loading...", 25, 25 + lineHeight);
else if (MusicController.trackExists()) {
g.drawString((MusicController.isPlaying()) ? "Now Playing:" : "Paused:", 25, 25 + lineHeight);
g.drawString(String.format("%s: %s",
MusicController.getArtistName(),
MusicController.getTrackName()),
50, 25 + (lineHeight * 2));
}
long time = System.currentTimeMillis() - osuStartTime;
g.drawString(String.format("opsu! has been running for %d minutes, %d seconds.",
TimeUnit.MILLISECONDS.toMinutes(time),
TimeUnit.MILLISECONDS.toSeconds(time) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(time))),
25, height - 25 - (lineHeight * 2));
g.drawString(String.format("The current time is %s.",
new SimpleDateFormat("h:mm a").format(new Date())),
25, height - 25 - lineHeight);
Options.drawFPS();
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
if (logoClicked) {
if (logoTimer == 0) { // shifting to left
if (logo.getX() > container.getWidth() / 3.3f)
logo.setX(logo.getX() - delta);
else
logoTimer = 1;
} else if (logoTimer >= MOVE_DELAY) // timer over: shift back to center
logoClicked = false;
else { // increment timer
logoTimer += delta;
if (logoTimer <= 500) {
// fade in buttons
playButton.getImage().setAlpha(logoTimer / 400f);
exitButton.getImage().setAlpha(logoTimer / 400f);
}
}
} else {
// fade out buttons
if (logoTimer > 0) {
float alpha = playButton.getImage().getAlpha();
if (alpha > 0f) {
playButton.getImage().setAlpha(alpha - (delta / 200f));
exitButton.getImage().setAlpha(alpha - (delta / 200f));
} else
logoTimer = 0;
}
// move back to original location
if (logo.getX() < container.getWidth() / 2) {
logo.setX(logo.getX() + (delta / 2f));
if (logo.getX() > container.getWidth() / 2)
logo.setX(container.getWidth() / 2);
}
}
}
@Override
public int getID() { return state; }
@Override
public void enter(GameContainer container, StateBasedGame game)
throws SlickException {
logoClicked = false;
logoTimer = 0;
logo.setX(container.getWidth() / 2);
}
@Override
public void mousePressed(int button, int x, int y) {
// check mouse button
if (button != Input.MOUSE_LEFT_BUTTON)
return;
// music button actions
if (musicPlay.contains(x, y)) {
if (MusicController.isPlaying())
MusicController.pause();
else if (!MusicController.isConverting())
MusicController.resume();
} else if (musicNext.contains(x, y)) {
SongMenu menu = (SongMenu) game.getState(Opsu.STATE_SONGMENU);
OsuGroupNode node = menu.setFocus(Opsu.groups.getRandomNode(), -1, true);
if (node != null)
previous.add(node.index);
} else if (musicPrevious.contains(x, y)) {
if (!previous.isEmpty()) {
SongMenu menu = (SongMenu) game.getState(Opsu.STATE_SONGMENU);
menu.setFocus(Opsu.groups.getBaseNode(previous.pop()), -1, true);
} else
MusicController.setPosition(0);
}
// start moving logo (if clicked)
else if (!logoClicked) {
if (logo.contains(x, y)) {
logoClicked = true;
logoTimer = 0;
playButton.getImage().setAlpha(0f);
exitButton.getImage().setAlpha(0f);
}
}
// other button actions (if visible)
else if (logoClicked) {
if (logo.contains(x, y))
logoTimer = MOVE_DELAY;
else if (playButton.contains(x, y))
game.enterState(Opsu.STATE_SONGMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
else if (exitButton.contains(x, y))
game.enterState(Opsu.STATE_MAINMENUEXIT);
}
}
@Override
public void keyPressed(int key, char c) {
switch (key) {
case Input.KEY_ESCAPE:
if (logoClicked)
logoTimer = MOVE_DELAY;
else
game.enterState(Opsu.STATE_MAINMENUEXIT);
break;
case Input.KEY_F12:
Options.takeScreenShot();
break;
}
}
}

View File

@@ -0,0 +1,163 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014 Jeffrey Han
*
* 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;
import itdelatrisu.opsu.GUIMenuButton;
import itdelatrisu.opsu.Opsu;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.EmptyTransition;
import org.newdawn.slick.state.transition.FadeInTransition;
/**
* "Confirm Exit" state.
* <ul>
* <li>[Yes] - quit game
* <li>[No] - return to main menu
* </ul>
*/
public class MainMenuExit extends BasicGameState {
/**
* "Yes" and "No" buttons.
*/
private GUIMenuButton yesButton, noButton;
/**
* Initial x coordinate offsets left/right of center (for shifting animation).
*/
private float centerOffset;
private GameContainer container;
private StateBasedGame game;
private int state;
public MainMenuExit(int state) {
this.state = state;
}
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
this.container = container;
this.game = game;
int width = container.getWidth();
int height = container.getHeight();
centerOffset = width / 8f;
// initialize buttons
Image button = new Image("button-middle.png");
Image buttonL = new Image("button-left.png");
Image buttonR = new Image("button-right.png");
button = button.getScaledCopy(width / 2, button.getHeight());
yesButton = new GUIMenuButton(button, buttonL, buttonR,
width / 2f - centerOffset, height * 0.2f
);
noButton = new GUIMenuButton(button, buttonL, buttonR,
width / 2f + centerOffset, height * 0.2f + (button.getHeight() * 1.25f)
);
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
g.setBackground(Color.black);
g.setColor(Color.white);
// draw text
float c = container.getWidth() * 0.02f;
Options.FONT_LARGE.drawString(c, c, "Are you sure you want to exit opsu!?");
// draw buttons
yesButton.draw(Color.green);
noButton.draw(Color.red);
g.setFont(Options.FONT_XLARGE);
g.drawString("1. Yes",
yesButton.getX() - (Options.FONT_XLARGE.getWidth("1. Yes") / 2f),
yesButton.getY() - (Options.FONT_XLARGE.getHeight() / 2f)
);
g.drawString("2. No",
noButton.getX() - (Options.FONT_XLARGE.getWidth("2. No") / 2f),
noButton.getY() - (Options.FONT_XLARGE.getHeight() / 2f)
);
Options.drawFPS();
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
// move buttons to center
float yesX = yesButton.getX(), noX = noButton.getX();
float center = container.getWidth() / 2f;
if (yesX < center)
yesButton.setX(Math.min(yesX + (delta / 2f), center));
if (noX > center)
noButton.setX(Math.max(noX - (delta / 2f), center));
}
@Override
public int getID() { return state; }
@Override
public void mousePressed(int button, int x, int y) {
// check mouse button
if (button != Input.MOUSE_LEFT_BUTTON)
return;
if (yesButton.contains(x, y)) {
Options.saveOptions();
container.exit();
} else if (noButton.contains(x, y))
game.enterState(Opsu.STATE_MAINMENU, new EmptyTransition(), new FadeInTransition(Color.black));
}
@Override
public void keyPressed(int key, char c) {
switch (key) {
case Input.KEY_1:
Options.saveOptions();
container.exit();
break;
case Input.KEY_2:
case Input.KEY_ESCAPE:
game.enterState(Opsu.STATE_MAINMENU, new EmptyTransition(), new FadeInTransition(Color.black));
break;
case Input.KEY_F12:
Options.takeScreenShot();
break;
}
}
@Override
public void enter(GameContainer container, StateBasedGame game)
throws SlickException {
float center = container.getWidth() / 2f;
yesButton.setX(center - centerOffset);
noButton.setX(center + centerOffset);
}
}

View File

@@ -0,0 +1,822 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014 Jeffrey Han
*
* 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;
import itdelatrisu.opsu.GUIMenuButton;
import itdelatrisu.opsu.Opsu;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.TrueTypeFont;
import org.newdawn.slick.imageout.ImageOut;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.EmptyTransition;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.util.Log;
/**
* "Game Options" state.
*/
public class Options extends BasicGameState {
/**
* Temporary folder for file conversions, auto-deleted upon successful exit.
*/
public static final File TMP_DIR = new File(".osu_tmp/");
/**
* Directory for screenshots (created when needed).
*/
public static final File SCREENSHOT_DIR = new File("screenshot/");
/**
* File for logging errors.
*/
public static final File LOG_FILE = new File(".opsu.log");
/**
* Beatmap directories (where to search for files).
*/
private static final String[] BEATMAP_DIRS = {
"C:/Program Files (x86)/osu!/Songs/",
"C:/Program Files/osu!/Songs/",
"songs/"
};
/**
* The current beatmap directory.
*/
private static File beatmapDir;
/**
* File for storing user options.
*/
private static final String OPTIONS_FILE = ".opsu.cfg";
/**
* Whether or not any changes were made to options.
* If false, the options file will not be modified.
*/
private static boolean optionsChanged = false;
/**
* Game colors.
*/
public static final Color
COLOR_BLACK_ALPHA = new Color(0, 0, 0, 0.5f),
COLOR_BLUE_DIVIDER = new Color(49, 94, 237),
COLOR_BLUE_BACKGROUND = new Color(74, 130, 255),
COLOR_BLUE_BUTTON = new Color(50, 189, 237),
COLOR_ORANGE_BUTTON = new Color(230, 151, 87),
COLOR_GREEN_OBJECT = new Color(26, 207, 26),
COLOR_BLUE_OBJECT = new Color(46, 136, 248),
COLOR_RED_OBJECT = new Color(243, 48, 77),
COLOR_ORANGE_OBJECT = new Color(255, 200, 32);
/**
* The default map colors, used when a map does not provide custom colors.
*/
public static final Color[] DEFAULT_COMBO = {
COLOR_GREEN_OBJECT, COLOR_BLUE_OBJECT,
COLOR_RED_OBJECT, COLOR_ORANGE_OBJECT
};
/**
* Game fonts.
*/
public static TrueTypeFont
FONT_DEFAULT, FONT_BOLD,
FONT_XLARGE, FONT_LARGE, FONT_MEDIUM, FONT_SMALL;
/**
* Game mods.
*/
public static final int
MOD_NO_FAIL = 0,
MOD_HARD_ROCK = 1,
MOD_SUDDEN_DEATH = 2,
MOD_SPUN_OUT = 3,
MOD_AUTO = 4,
MOD_MAX = 5; // not a mod
/**
* Whether a mod is active (indexed by MOD_* constants).
*/
private static boolean[] modsActive;
/**
* Mod buttons.
*/
private static GUIMenuButton[] modButtons;
/**
* Game option constants.
*/
private static final int
OPTIONS_SCREEN_RESOLUTION = 0,
// OPTIONS_FULLSCREEN = ,
OPTIONS_TARGET_FPS = 1,
OPTIONS_MUSIC_VOLUME = 2,
OPTIONS_MUSIC_OFFSET = 3,
OPTIONS_SCREENSHOT_FORMAT = 4,
OPTIONS_DISPLAY_FPS = 5,
OPTIONS_HIT_LIGHTING = 6,
OPTIONS_COMBO_BURSTS = 7,
OPTIONS_MAX = 8; // not an option
/**
* Screen resolutions.
*/
private static final int[][] resolutions = {
{ 800, 600 },
{ 1024, 600 },
{ 1024, 768 },
{ 1280, 800 },
{ 1280, 960 },
{ 1366, 768 },
{ 1440, 900 },
{ 1680, 1050 },
{ 1920, 1080 }
};
/**
* Index (row) in resolutions[][] array.
*/
private static int resolutionIndex = 3;
// /**
// * Whether or not the game should run in fullscreen mode.
// */
// private static boolean fullscreen = false;
/**
* Frame limiters.
*/
private static final int[] targetFPS = { 60, 120, 240 };
/**
* Index in targetFPS[] array.
*/
private static int targetFPSindex = 0;
/**
* Whether or not to show the FPS.
*/
private static boolean showFPS = false;
/**
* Whether or not to show hit lighting effects.
*/
private static boolean showHitLighting = true;
/**
* Whether or not to show combo burst images.
*/
private static boolean showComboBursts = true;
/**
* Default music volume.
*/
private static int musicVolume = 20;
/**
* Offset time, in milliseconds, for music position-related elements.
*/
private static int musicOffset = -150;
/**
* Screenshot file format.
*/
private static String[] screenshotFormat = { "png", "jpg", "bmp" };
/**
* Index in screenshotFormat[] array.
*/
private static int screenshotFormatIndex = 0;
/**
* Back button (shared by other states).
*/
private static GUIMenuButton backButton;
/**
* Game option coordinate modifiers (for drawing).
*/
private int textY, offsetY;
// game-related variables
private static GameContainer container;
private static StateBasedGame game;
private Input input;
private int state;
private boolean init = false;
public Options(int state) {
this.state = state;
}
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
Options.container = container;
Options.game = game;
this.input = container.getInput();
// game settings;
container.setTargetFrameRate(60);
container.setMouseCursor("cursor.png", 16, 16);
container.setMusicVolume(getMusicVolume());
container.setShowFPS(false);
container.getInput().enableKeyRepeat();
container.setAlwaysRender(true);
// create fonts
float fontBase;
if (container.getHeight() <= 600)
fontBase = 9f;
else if (container.getHeight() < 800)
fontBase = 10f;
else if (container.getHeight() <= 900)
fontBase = 12f;
else
fontBase = 14f;
Font font = new Font("Lucida Sans Unicode", Font.PLAIN, (int) (fontBase * 4 / 3));
FONT_DEFAULT = new TrueTypeFont(font, false);
FONT_BOLD = new TrueTypeFont(font.deriveFont(Font.BOLD), false);
FONT_XLARGE = new TrueTypeFont(font.deriveFont(fontBase * 4), false);
FONT_LARGE = new TrueTypeFont(font.deriveFont(fontBase * 2), false);
FONT_MEDIUM = new TrueTypeFont(font.deriveFont(fontBase * 3 / 2), false);
FONT_SMALL = new TrueTypeFont(font.deriveFont(fontBase), false);
int width = container.getWidth();
int height = container.getHeight();
// game option coordinate modifiers
textY = 10 + (FONT_XLARGE.getLineHeight() * 3 / 2);
offsetY = (int) (((height * 0.8f) - textY) / OPTIONS_MAX);
// game mods
modsActive = new boolean[MOD_MAX];
modButtons = new GUIMenuButton[MOD_MAX];
Image noFailImage = new Image("selection-mod-nofail.png");
float modScale = (height * 0.12f) / noFailImage.getHeight();
noFailImage = noFailImage.getScaledCopy(modScale);
float modButtonOffsetX = noFailImage.getWidth() * 1.5f;
float modButtonX = (width / 2f) - (modButtonOffsetX * modButtons.length / 2.75f);
float modButtonY = (height * 0.8f) + (noFailImage.getHeight() / 2);
modButtons[MOD_NO_FAIL] = new GUIMenuButton(
noFailImage, modButtonX, modButtonY
);
modButtons[MOD_HARD_ROCK] = new GUIMenuButton(
new Image("selection-mod-hardrock.png").getScaledCopy(modScale),
modButtonX + modButtonOffsetX, modButtonY
);
modButtons[MOD_SUDDEN_DEATH] = new GUIMenuButton(
new Image("selection-mod-suddendeath.png").getScaledCopy(modScale),
modButtonX + (modButtonOffsetX * 2), modButtonY
);
modButtons[MOD_SPUN_OUT] = new GUIMenuButton(
new Image("selection-mod-spunout.png").getScaledCopy(modScale),
modButtonX + (modButtonOffsetX * 3), modButtonY
);
modButtons[MOD_AUTO] = new GUIMenuButton(
new Image("selection-mod-autoplay.png").getScaledCopy(modScale),
modButtonX + (modButtonOffsetX * 4), modButtonY
);
for (int i = 0; i < modButtons.length; i++)
modButtons[i].getImage().setAlpha(0.5f);
// back button
Image back = new Image("menu-back.png");
float scale = (height * 0.1f) / back.getHeight();
back = back.getScaledCopy(scale);
backButton = new GUIMenuButton(back,
back.getWidth() / 2f,
height - (back.getHeight() / 2f));
game.enterState(Opsu.STATE_MAINMENU, new EmptyTransition(), new FadeInTransition(Color.black));
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
if (!init)
return;
g.setBackground(COLOR_BLACK_ALPHA);
g.setColor(Color.white);
int width = container.getWidth();
int height = container.getHeight();
// title
FONT_XLARGE.drawString(
(width / 2) - (FONT_XLARGE.getWidth("GAME OPTIONS") / 2),
10, "GAME OPTIONS"
);
FONT_DEFAULT.drawString(
(width / 2) - (FONT_DEFAULT.getWidth("Click or drag an option to change it.") / 2),
10 + FONT_XLARGE.getHeight(), "Click or drag an option to change it."
);
// game options
g.setLineWidth(1f);
g.setFont(FONT_LARGE);
this.drawOption(g, OPTIONS_SCREEN_RESOLUTION, "Screen Resolution",
String.format("%dx%d", resolutions[resolutionIndex][0], resolutions[resolutionIndex][1]),
"Restart to apply resolution changes."
);
// this.drawOption(g, OPTIONS_FULLSCREEN, "Fullscreen Mode",
// fullscreen ? "Yes" : "No",
// "Restart to apply changes."
// );
this.drawOption(g, OPTIONS_TARGET_FPS, "Frame Limiter",
String.format("%dfps", targetFPS[targetFPSindex]),
"Higher values may cause high CPU usage."
);
this.drawOption(g, OPTIONS_MUSIC_VOLUME, "Music Volume",
String.format("%d%%", musicVolume),
"Global music volume."
);
this.drawOption(g, OPTIONS_MUSIC_OFFSET, "Music Offset",
String.format("%dms", musicOffset),
"Adjust this value if hit objects are out of sync."
);
this.drawOption(g, OPTIONS_SCREENSHOT_FORMAT, "Screenshot Format",
screenshotFormat[screenshotFormatIndex].toUpperCase(),
"Press F12 to take a screenshot."
);
this.drawOption(g, OPTIONS_DISPLAY_FPS, "Show FPS Counter",
showFPS ? "Yes" : "No",
null
);
this.drawOption(g, OPTIONS_HIT_LIGHTING, "Show Hit Lighting",
showHitLighting ? "Yes" : "No",
null
);
this.drawOption(g, OPTIONS_COMBO_BURSTS, "Show Combo Bursts",
showComboBursts ? "Yes" : "No",
null
);
// game mods
FONT_LARGE.drawString(width * 0.02f, height * 0.8f, "Game Mods:", Color.white);
for (int i = 0; i < modButtons.length; i++)
modButtons[i].draw();
backButton.draw();
drawFPS();
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
init = true;
}
@Override
public int getID() { return state; }
@Override
public void mousePressed(int button, int x, int y) {
// check mouse button
if (button == Input.MOUSE_MIDDLE_BUTTON)
return;
// back
if (backButton.contains(x, y)) {
game.enterState(Opsu.STATE_SONGMENU, new EmptyTransition(), new FadeInTransition(Color.black));
return;
}
// game mods
for (int i = 0; i < modButtons.length; i++) {
if (modButtons[i].contains(x, y)) {
toggleMod(i);
// mutually exclusive mods
if (modsActive[MOD_AUTO]) {
if (i == MOD_AUTO) {
if (modsActive[MOD_SPUN_OUT])
toggleMod(MOD_SPUN_OUT);
if (modsActive[MOD_SUDDEN_DEATH])
toggleMod(MOD_SUDDEN_DEATH);
} else if (i == MOD_SPUN_OUT || i == MOD_SUDDEN_DEATH) {
if (modsActive[i])
toggleMod(i);
}
} else if (modsActive[MOD_SUDDEN_DEATH] && modsActive[MOD_NO_FAIL])
toggleMod((i == MOD_SUDDEN_DEATH) ? MOD_NO_FAIL : MOD_SUDDEN_DEATH);
return;
}
}
// options (click only)
if (isOptionClicked(OPTIONS_SCREEN_RESOLUTION, y)) {
resolutionIndex = (resolutionIndex + 1) % resolutions.length;
return;
}
// if (isOptionClicked(OPTIONS_FULLSCREEN, y)) {
// fullscreen = !fullscreen;
// return;
// }
if (isOptionClicked(OPTIONS_TARGET_FPS, y)) {
targetFPSindex = (targetFPSindex + 1) % targetFPS.length;
container.setTargetFrameRate(targetFPS[targetFPSindex]);
return;
}
if (isOptionClicked(OPTIONS_SCREENSHOT_FORMAT, y)) {
screenshotFormatIndex = (screenshotFormatIndex + 1) % screenshotFormat.length;
return;
}
if (isOptionClicked(OPTIONS_DISPLAY_FPS, y)) {
showFPS = !showFPS;
return;
}
if (isOptionClicked(OPTIONS_HIT_LIGHTING, y)) {
showHitLighting = !showHitLighting;
return;
}
if (isOptionClicked(OPTIONS_COMBO_BURSTS, y)) {
showComboBursts = !showComboBursts;
return;
}
}
@Override
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
// check mouse button (right click scrolls faster)
int multiplier;
if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
multiplier = 4;
else if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
multiplier = 1;
else
return;
// get direction
int diff = newx - oldx;
if (diff == 0)
return;
diff = ((diff > 0) ? 1 : -1) * multiplier;
// options (drag only)
if (isOptionClicked(OPTIONS_MUSIC_VOLUME, oldy)) {
musicVolume += diff;
if (musicVolume < 0)
musicVolume = 0;
else if (musicVolume > 100)
musicVolume = 100;
container.setMusicVolume(getMusicVolume());
return;
}
if (isOptionClicked(OPTIONS_MUSIC_OFFSET, oldy)) {
musicOffset += diff;
if (musicOffset < -500)
musicOffset = -500;
else if (musicOffset > 500)
musicOffset = 500;
return;
}
}
@Override
public void keyPressed(int key, char c) {
switch (key) {
case Input.KEY_ESCAPE:
game.enterState(Opsu.STATE_SONGMENU, new EmptyTransition(), new FadeInTransition(Color.black));
break;
case Input.KEY_F12:
Options.takeScreenShot();
break;
}
}
/**
* Draws a game option.
* @param g the graphics context
* @param pos the element position (OPTIONS_* constants)
* @param label the option name
* @param value the option value
* @param notes additional notes (optional)
*/
private void drawOption(Graphics g, int pos, String label, String value, String notes) {
int width = container.getWidth();
int textHeight = FONT_LARGE.getHeight();
float y = textY + (pos * offsetY);
g.drawString(label, width / 50, y);
g.drawString(value, width / 2, y);
g.drawLine(0, y + textHeight, width, y + textHeight);
if (notes != null)
FONT_SMALL.drawString(width / 50, y + textHeight, notes);
}
/**
* Returns whether or not an option was clicked.
* @param pos the element position (OPTIONS_* constants)
* @param y the y coordinate of the click
* @return true if clicked
*/
private boolean isOptionClicked(int pos, int y) {
if (y > textY + (offsetY * pos) - FONT_LARGE.getHeight() &&
y < textY + (offsetY * pos) + FONT_LARGE.getHeight()) {
optionsChanged = true;
return true;
}
return false;
}
/**
* Toggles the active status of a game mod.
* Note that this does not perform checks for mutual exclusivity.
* @param mod the game mod (MOD_* constants)
*/
private static void toggleMod(int mod) {
modButtons[mod].getImage().setAlpha(modsActive[mod] ? 0.5f : 1.0f);
modsActive[mod] = !modsActive[mod];
}
/**
* Returns whether or not a game mod is active.
* @param mod the game mod (MOD_* constants)
* @return true if the mod is active
*/
public static boolean isModActive(int mod) { return modsActive[mod]; }
/**
* Returns the image associated with a game mod.
* @param mod the game mod (MOD_* constants)
* @return the associated image
*/
public static Image getModImage(int mod) { return modButtons[mod].getImage(); }
/**
* Returns the 'back' GUIMenuButton.
*/
public static GUIMenuButton getBackButton() { return backButton; }
/**
* Returns the default music volume.
* @return the volume [0, 1]
*/
public static float getMusicVolume() { return musicVolume / 100f; }
/**
* Returns the music offset time.
* @return the offset (in milliseconds)
*/
public static int getMusicOffset() { return musicOffset; }
/**
* Draws the FPS at the bottom-right corner of the game container.
* If the option is not activated, this will do nothing.
*/
public static void drawFPS() {
if (showFPS) {
String fps = String.format("FPS: %d", container.getFPS());
FONT_DEFAULT.drawString(
container.getWidth() - 15 - FONT_DEFAULT.getWidth(fps),
container.getHeight() - 15 - FONT_DEFAULT.getHeight(fps),
fps, Color.white
);
}
}
/**
* Takes a screenshot.
* @return true if successful
*/
public static boolean takeScreenShot() {
// TODO: should this be threaded?
try {
// create the screenshot directory
if (!SCREENSHOT_DIR.isDirectory()) {
if (!SCREENSHOT_DIR.mkdir())
return false;
}
// create file name
SimpleDateFormat date = new SimpleDateFormat("yyyyMMdd_HHmmss");
String file = date.format(new Date());
// copy the screen
Image screen = new Image(container.getWidth(), container.getHeight());
container.getGraphics().copyArea(screen, 0, 0);
ImageOut.write(screen, String.format("%s%sscreenshot_%s.%s",
SCREENSHOT_DIR.getName(), File.separator,
file, screenshotFormat[screenshotFormatIndex]), false
);
screen.destroy();
} catch (SlickException e) {
Log.warn("Failed to take a screenshot.", e);
return false;
}
return true;
}
/**
* Returns the screen resolution.
* @return an array containing the resolution [width, height]
*/
public static int[] getContainerSize() { return resolutions[resolutionIndex]; }
// /**
// * Returns whether or not fullscreen mode is enabled.
// * @return true if enabled
// */
// public static boolean isFullscreen() { return fullscreen; }
/**
* Returns whether or not hit lighting effects are enabled.
* @return true if enabled
*/
public static boolean isHitLightingEnabled() { return showHitLighting; }
/**
* Returns whether or not combo burst effects are enabled.
* @return true if enabled
*/
public static boolean isComboBurstEnabled() { return showComboBursts; }
/**
* Returns the current beatmap directory.
* If invalid, this will attempt to search for the directory,
* and if nothing found, will create one.
*/
public static File getBeatmapDir() {
if (beatmapDir != null && beatmapDir.isDirectory())
return beatmapDir;
// search for directory
for (int i = 0; i < BEATMAP_DIRS.length; i++) {
beatmapDir = new File(BEATMAP_DIRS[i]);
if (beatmapDir.isDirectory()) {
optionsChanged = true; // force config file creation
return beatmapDir;
}
}
beatmapDir.mkdir(); // none found, create new directory
return beatmapDir;
}
/**
* Reads user options from the options file, if it exists.
*/
public static void parseOptions() {
// if no config file, use default settings
File file = new File(OPTIONS_FILE);
if (!file.isFile()) {
optionsChanged = true; // force file creation
saveOptions();
optionsChanged = false;
return;
}
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
String line;
String name, value;
int i;
while ((line = in.readLine()) != null) {
line = line.trim();
if (line.length() < 2 || line.charAt(0) == '#')
continue;
int index = line.indexOf('=');
if (index == -1)
continue;
name = line.substring(0, index).trim();
value = line.substring(index + 1).trim();
switch (name) {
case "BeatmapDirectory":
beatmapDir = new File(value);
break;
case "ScreenResolution":
i = Integer.parseInt(value);
if (i >= 0 && i < resolutions.length)
resolutionIndex = i;
break;
// case "Fullscreen":
// fullscreen = Boolean.parseBoolean(value);
// break;
case "FrameSync":
i = Integer.parseInt(value);
if (i >= 0 && i <= targetFPS.length)
targetFPSindex = i;
break;
case "VolumeMusic":
i = Integer.parseInt(value);
if (i >= 0 && i <= 100)
musicVolume = i;
break;
case "Offset":
i = Integer.parseInt(value);
if (i >= -500 && i <= 500)
musicOffset = i;
break;
case "ScreenshotFormat":
i = Integer.parseInt(value);
if (i >= 0 && i < screenshotFormat.length)
screenshotFormatIndex = i;
break;
case "FpsCounter":
showFPS = Boolean.parseBoolean(value);
break;
case "HitLighting":
showHitLighting = Boolean.parseBoolean(value);
break;
case "ComboBurst":
showComboBursts = Boolean.parseBoolean(value);
break;
}
}
} catch (IOException e) {
Log.error(String.format("Failed to read file '%s'.", OPTIONS_FILE), e);
} catch (NumberFormatException e) {
Log.warn("Format error in options file.", e);
return;
}
}
/**
* (Over)writes user options to a file.
*/
public static void saveOptions() {
// only overwrite when needed
if (!optionsChanged)
return;
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(OPTIONS_FILE), "utf-8"))) {
// header
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
String date = dateFormat.format(new Date());
writer.write("# opsu! configuration");
writer.newLine();
writer.write("# last updated on ");
writer.write(date);
writer.newLine();
writer.newLine();
// options
if (beatmapDir != null) {
writer.write(String.format("BeatmapDirectory = %s", beatmapDir.getAbsolutePath()));
writer.newLine();
}
writer.write(String.format("ScreenResolution = %d", resolutionIndex));
writer.newLine();
// writer.write(String.format("Fullscreen = %b", fullscreen));
// writer.newLine();
writer.write(String.format("FrameSync = %d", targetFPSindex));
writer.newLine();
writer.write(String.format("VolumeMusic = %d", musicVolume));
writer.newLine();
writer.write(String.format("Offset = %d", musicOffset));
writer.newLine();
writer.write(String.format("ScreenshotFormat = %d", screenshotFormatIndex));
writer.newLine();
writer.write(String.format("FpsCounter = %b", showFPS));
writer.newLine();
writer.write(String.format("HitLighting = %b", showHitLighting));
writer.newLine();
writer.write(String.format("ComboBurst = %b", showComboBursts));
writer.newLine();
writer.close();
} catch (IOException e) {
Log.error(String.format("Failed to write to file '%s'.", OPTIONS_FILE), e);
}
}
}

View File

@@ -0,0 +1,601 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014 Jeffrey Han
*
* 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;
import itdelatrisu.opsu.GUIMenuButton;
import itdelatrisu.opsu.MusicController;
import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuFile;
import itdelatrisu.opsu.OsuGroupList;
import itdelatrisu.opsu.OsuGroupNode;
import itdelatrisu.opsu.OsuParser;
import org.lwjgl.opengl.Display;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.TextField;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.EmptyTransition;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
/**
* "Song Selection" state.
* <ul>
* <li>[Song] - start game (move to game state)
* <li>[Back] - return to main menu
* </ul>
*/
public class SongMenu extends BasicGameState {
/**
* The number of buttons to be shown on each screen.
*/
private static final int MAX_BUTTONS = 6;
/**
* Delay time, in milliseconds, between each search.
*/
private static final int SEARCH_DELAY = 300;
/**
* Current start node (topmost menu entry).
*/
private OsuGroupNode startNode;
/**
* Current focused (selected) node.
*/
private OsuGroupNode focusNode;
/**
* Button coordinate values.
*/
private float
buttonX, buttonY, buttonOffset,
buttonWidth, buttonHeight;
/**
* Sorting tab buttons (indexed by SORT_* constants).
*/
private GUIMenuButton[] sortTabs;
/**
* The current sort order (SORT_* constant).
*/
private byte currentSort;
/**
* The options button (to enter the "Game Options" menu).
*/
private GUIMenuButton optionsButton;
/**
* The search textfield.
*/
private TextField search;
/**
* Delay timer, in milliseconds, before running another search.
* This is overridden by character entry (reset) and 'esc' (immediate search).
*/
private int searchTimer;
/**
* Information text to display based on the search query.
*/
private String searchResultString;
/**
* Search icon.
*/
private Image searchIcon;
/**
* Music note icon.
*/
private Image musicNote;
// game-related variables
private GameContainer container;
private StateBasedGame game;
private Input input;
private int state;
public SongMenu(int state) {
this.state = state;
}
@Override
public void init(GameContainer container, StateBasedGame game)
throws SlickException {
this.container = container;
this.game = game;
this.input = container.getInput();
// initialize song list
currentSort = OsuGroupList.SORT_TITLE;
Opsu.groups.init(currentSort);
setFocus(Opsu.groups.getRandomNode(), -1, true);
int width = container.getWidth();
int height = container.getHeight();
// song button background & graphics context
Image menuBackground = new Image("menu-button-background.png").getScaledCopy(width / 2, height / 6);
OsuGroupNode.setBackground(menuBackground);
// song button coordinates
buttonX = width * 0.6f;
buttonY = height * 0.16f;
buttonWidth = menuBackground.getWidth();
buttonHeight = menuBackground.getHeight();
buttonOffset = (height * 0.8f) / MAX_BUTTONS;
// sorting tabs
sortTabs = new GUIMenuButton[OsuGroupList.SORT_MAX];
Image tab = new Image("selection-tab.png");
float tabScale = (height * 0.033f) / tab.getHeight();
tab = tab.getScaledCopy(tabScale);
float tabX = buttonX + (tab.getWidth() / 2f);
float tabY = (height * 0.15f) - (tab.getHeight() / 2f) - 2f;
float tabOffset = (width - buttonX) / sortTabs.length;
for (int i = 0; i < sortTabs.length; i++)
sortTabs[i] = new GUIMenuButton(tab, tabX + (i * tabOffset), tabY);
// search
searchTimer = 0;
searchResultString = "Type to search!";
searchIcon = new Image("search.png");
float iconScale = Options.FONT_BOLD.getLineHeight() * 2f / searchIcon.getHeight();
searchIcon = searchIcon.getScaledCopy(iconScale);
search = new TextField(
container, Options.FONT_DEFAULT,
(int) tabX + searchIcon.getWidth(), (int) ((height * 0.15f) - (tab.getHeight() * 5 / 2f)),
(int) (buttonWidth / 2), Options.FONT_DEFAULT.getHeight()
);
search.setBackgroundColor(Color.transparent);
search.setBorderColor(Color.transparent);
search.setTextColor(Color.white);
search.setConsumeEvents(false);
search.setMaxLength(60);
// options button
Image optionsIcon = new Image("options.png").getScaledCopy(iconScale);
optionsButton = new GUIMenuButton(optionsIcon, search.getX() - (optionsIcon.getWidth() * 1.5f), search.getY());
musicNote = new Image("music-note.png");
}
@Override
public void render(GameContainer container, StateBasedGame game, Graphics g)
throws SlickException {
g.setBackground(Color.black);
int width = container.getWidth();
int height = container.getHeight();
// background
if (focusNode != null)
focusNode.osuFiles.get(focusNode.osuFileIndex).drawBG(width, height, 1.0f);
// header setup
float lowerBound = height * 0.15f;
g.setColor(Options.COLOR_BLACK_ALPHA);
g.fillRect(0, 0, width, lowerBound);
g.setColor(Options.COLOR_BLUE_DIVIDER);
g.setLineWidth(2f);
g.drawLine(0, lowerBound, width, lowerBound);
g.resetLineWidth();
// header
if (focusNode != null) {
musicNote.draw();
int musicNoteWidth = musicNote.getWidth();
int musicNoteHeight = musicNote.getHeight();
String[] info = focusNode.getInfo();
g.setColor(Color.white);
Options.FONT_LARGE.drawString(
musicNoteWidth + 5, -3, info[0]);
float y1 = -3 + Options.FONT_LARGE.getHeight() * 0.75f;
Options.FONT_DEFAULT.drawString(
musicNoteWidth + 5, y1, info[1]);
Options.FONT_BOLD.drawString(
5, Math.max(y1 + 4, musicNoteHeight - 3), info[2]);
Options.FONT_DEFAULT.drawString(
5, musicNoteHeight + Options.FONT_BOLD.getLineHeight() - 9, info[3]);
Options.FONT_SMALL.drawString(
5, musicNoteHeight + Options.FONT_BOLD.getLineHeight() + Options.FONT_DEFAULT.getLineHeight() - 13, info[4]);
}
// song buttons
OsuGroupNode node = startNode;
for (int i = 0; i < MAX_BUTTONS && node != null; i++) {
node.draw(buttonX, buttonY + (i*buttonOffset), (node == focusNode));
node = node.next;
}
// options button
optionsButton.draw();
// sorting tabs
float tabTextY = sortTabs[0].getY() - (sortTabs[0].getImage().getHeight() / 2f);
for (int i = sortTabs.length - 1; i >= 0; i--) {
sortTabs[i].getImage().setAlpha((i == currentSort) ? 1.0f : 0.7f);
sortTabs[i].draw();
float tabTextX = sortTabs[i].getX() - (Options.FONT_MEDIUM.getWidth(OsuGroupList.SORT_NAMES[i]) / 2);
Options.FONT_MEDIUM.drawString(tabTextX, tabTextY, OsuGroupList.SORT_NAMES[i], Color.white);
}
// search
Options.FONT_BOLD.drawString(
search.getX(), search.getY() - Options.FONT_BOLD.getLineHeight(),
searchResultString, Color.white
);
searchIcon.draw(search.getX() - searchIcon.getWidth(),
search.getY() - Options.FONT_DEFAULT.getLineHeight());
g.setColor(Color.white);
search.render(container, g);
// scroll bar
if (focusNode != null) {
float scrollStartY = height * 0.16f;
float scrollEndY = height * 0.82f;
g.setColor(Options.COLOR_BLACK_ALPHA);
g.fillRoundRect(width - 10, scrollStartY, 5, scrollEndY, 4);
g.setColor(Color.white);
g.fillRoundRect(width - 10, scrollStartY + (scrollEndY * startNode.index / Opsu.groups.size()), 5, 20, 4);
}
// back button
Options.getBackButton().draw();
Options.drawFPS();
}
@Override
public void update(GameContainer container, StateBasedGame game, int delta)
throws SlickException {
// search
search.setFocus(true);
searchTimer += delta;
if (searchTimer >= SEARCH_DELAY) {
searchTimer = 0;
// store the start/focus nodes
OsuGroupNode oldFocusNode = null;
int oldFileIndex = -1;
if (focusNode != null) {
oldFocusNode = Opsu.groups.getBaseNode(focusNode.index);
oldFileIndex = focusNode.osuFileIndex;
}
if (Opsu.groups.search(search.getText())) {
// empty search
if (search.getText().isEmpty())
searchResultString = "Type to search!";
// search produced new list: re-initialize it
startNode = focusNode = null;
if (Opsu.groups.size() > 0) {
Opsu.groups.init(currentSort);
if (search.getText().isEmpty()) { // cleared search
// use previous start/focus if possible
if (oldFocusNode != null)
setFocus(oldFocusNode, oldFileIndex + 1, true);
else
setFocus(Opsu.groups.getRandomNode(), -1, true);
} else {
searchResultString = String.format("%d matches found!", Opsu.groups.size());
setFocus(Opsu.groups.getRandomNode(), -1, true);
}
} else if (!search.getText().isEmpty())
searchResultString = "No matches found. Hit 'esc' to reset.";
}
}
// slide buttons
int height = container.getHeight();
float targetY = height * 0.16f;
if (buttonY > targetY) {
buttonY -= height * delta / 20000f;
if (buttonY < targetY)
buttonY = targetY;
} else if (buttonY < targetY) {
buttonY += height * delta / 20000f;
if (buttonY > targetY)
buttonY = targetY;
}
}
@Override
public int getID() { return state; }
@Override
public void mousePressed(int button, int x, int y) {
// check mouse button
if (button != Input.MOUSE_LEFT_BUTTON)
return;
// back
if (Options.getBackButton().contains(x, y)) {
game.enterState(Opsu.STATE_MAINMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
return;
}
// options
if (optionsButton.contains(x, y)) {
game.enterState(Opsu.STATE_OPTIONS, new EmptyTransition(), new FadeInTransition(Color.black));
return;
}
if (focusNode == null)
return;
// sorting buttons
for (byte i = 0; i < sortTabs.length; i++) {
if (sortTabs[i].contains(x, y) && i != currentSort) {
currentSort = i;
OsuGroupNode oldFocusBase = Opsu.groups.getBaseNode(focusNode.index);
int oldFocusFileIndex = focusNode.osuFileIndex;
focusNode = null;
Opsu.groups.init(i);
setFocus(oldFocusBase, oldFocusFileIndex + 1, true);
}
}
for (int i = 0; i < MAX_BUTTONS; i++) {
if ((x > buttonX && x < buttonX + buttonWidth) &&
(y > buttonY + (i*buttonOffset) && y < buttonY + (i*buttonOffset) + buttonHeight)) {
OsuGroupNode node = Opsu.groups.getNode(startNode, i);
if (node == null) // out of bounds
break;
int expandedIndex = Opsu.groups.getExpandedIndex();
// clicked node is already expanded
if (node.index == expandedIndex) {
if (node.osuFileIndex == -1) {
// check bounds
int max = Math.max(Opsu.groups.size() - MAX_BUTTONS, 0);
if (startNode.index > max)
startNode = Opsu.groups.getBaseNode(max);
// if group button clicked, undo expansion
Opsu.groups.expand(node.index);
} else if (node.osuFileIndex == focusNode.osuFileIndex) {
// if already focused, load the beatmap
startGame(focusNode.osuFiles.get(focusNode.osuFileIndex));
} else {
// focus the node
setFocus(node, 0, false);
}
break;
}
// if current start node is expanded,
// set it to the base node before undoing the expansion
if (startNode.index == expandedIndex) {
int max = Math.max(Opsu.groups.size() - MAX_BUTTONS, 0);
if (startNode.index > max) // check bounds
startNode = Opsu.groups.getBaseNode(max);
else
startNode = Opsu.groups.getBaseNode(startNode.index);
}
setFocus(node, -1, false);
break;
}
}
}
@Override
public void keyPressed(int key, char c) {
switch (key) {
case Input.KEY_ESCAPE:
if (!search.getText().isEmpty()) {
search.setText("");
searchTimer = SEARCH_DELAY;
} else
game.enterState(Opsu.STATE_MAINMENU, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
break;
case Input.KEY_F1:
game.enterState(Opsu.STATE_OPTIONS, new EmptyTransition(), new FadeInTransition(Color.black));
break;
case Input.KEY_F2:
setFocus(Opsu.groups.getRandomNode(), -1, true);
break;
case Input.KEY_F12:
Options.takeScreenShot();
break;
case Input.KEY_ENTER:
if (focusNode != null)
startGame(focusNode.osuFiles.get(focusNode.osuFileIndex));
break;
case Input.KEY_DOWN:
changeIndex(1);
break;
case Input.KEY_UP:
changeIndex(-1);
break;
case Input.KEY_RIGHT:
if (focusNode == null)
break;
OsuGroupNode next = focusNode.next;
if (next != null) {
setFocus(next, (next.index == focusNode.index) ? 0 : 1, false);
changeIndex(1);
}
break;
case Input.KEY_LEFT:
if (focusNode == null)
break;
OsuGroupNode prev = focusNode.prev;
if (prev != null) {
if (prev.index == focusNode.index && prev.osuFileIndex < 0) {
// skip the group node
prev = prev.prev;
if (prev == null) // this is the first node
break;
setFocus(prev, prev.osuFiles.size(), true);
// move the start node forward if off the screen
int size = prev.osuFiles.size();
while (size-- >= MAX_BUTTONS)
startNode = startNode.next;
} else {
setFocus(prev, 0, false);
changeIndex(-1);
}
}
break;
case Input.KEY_NEXT:
changeIndex(MAX_BUTTONS);
break;
case Input.KEY_PRIOR:
changeIndex(-MAX_BUTTONS);
break;
default:
// wait for user to finish typing
if (Character.isLetterOrDigit(c) || key == Input.KEY_BACK)
searchTimer = 0;
break;
}
}
@Override
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
// check mouse button (right click scrolls faster)
int multiplier;
if (input.isMouseButtonDown(Input.MOUSE_RIGHT_BUTTON))
multiplier = 4;
else if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON))
multiplier = 1;
else
return;
int diff = newy - oldy;
if (diff != 0) {
diff = ((diff < 0) ? 1 : -1) * multiplier;
changeIndex(diff);
}
}
@Override
public void mouseWheelMoved(int newValue) {
changeIndex((newValue < 0) ? 1 : -1);
}
@Override
public void enter(GameContainer container, StateBasedGame game)
throws SlickException {
Display.setTitle(game.getTitle());
}
/**
* Shifts the startNode forward (+) or backwards (-) by a given number of nodes.
* Initiates sliding "animation" by shifting the button Y position.
*/
private void changeIndex(int shift) {
while (shift != 0) {
if (startNode == null)
break;
int height = container.getHeight();
if (shift < 0 && startNode.prev != null) {
startNode = startNode.prev;
buttonY += buttonOffset / 4;
if (buttonY > height * 0.18f)
buttonY = height * 0.18f;
shift++;
} else if (shift > 0 && startNode.next != null &&
Opsu.groups.getNode(startNode, MAX_BUTTONS) != null) {
startNode = startNode.next;
buttonY -= buttonOffset / 4;
if (buttonY < height * 0.14f)
buttonY = height * 0.14f;
shift--;
} else
break;
}
return;
}
/**
* Sets a new focus node.
* @param node the base node; it will be expanded if it isn't already
* @param pos the OsuFile element to focus; if out of bounds, it will be randomly chosen
* @param flag if true, startNode will be set to the song group node
* @return the old focus node
*/
public OsuGroupNode setFocus(OsuGroupNode node, int pos, boolean flag) {
if (node == null)
return null;
OsuGroupNode oldFocus = focusNode;
// expand node before focusing it
if (node.index != Opsu.groups.getExpandedIndex())
Opsu.groups.expand(node.index);
// check pos bounds
int length = node.osuFiles.size();
if (pos < 0 || pos > length) // set a random pos
pos = (int) (Math.random() * length) + 1;
if (flag)
startNode = node;
focusNode = Opsu.groups.getNode(node, pos);
MusicController.play(focusNode.osuFiles.get(focusNode.osuFileIndex), true);
// check startNode bounds
if (focusNode.index - startNode.index == MAX_BUTTONS - 1)
changeIndex(1);
while (startNode.index >= Opsu.groups.size() + length + 1 - MAX_BUTTONS &&
startNode.prev != null)
changeIndex(-1);
return oldFocus;
}
/**
* Starts the game.
* @param osu the OsuFile to send to the game
*/
private void startGame(OsuFile osu) {
if (MusicController.isConverting())
return;
Display.setTitle(String.format("%s - %s", game.getTitle(), osu.toString()));
OsuParser.parseHitObjects(osu);
Game.setOsuFile(osu);
Game.setRestart(Game.RESTART_NEW);
game.enterState(Opsu.STATE_GAME, new FadeOutTransition(Color.black), new FadeInTransition(Color.black));
}
}