Made OsuGroupList a singleton.

(This makes a lot more sense than storing the single pointer in Opsu.groups.)

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-01-11 14:17:33 -05:00
parent af8512c080
commit 4152af4977
6 changed files with 41 additions and 30 deletions

View File

@ -54,11 +54,6 @@ import org.newdawn.slick.util.ResourceLoader;
* Creates game container, adds all other states, and initializes song data.
*/
public class Opsu extends StateBasedGame {
/**
* Song group structure (each group contains of an ArrayList of OsuFiles).
*/
public static OsuGroupList groups = new OsuGroupList();
/**
* Game states.
*/
@ -93,6 +88,9 @@ public class Opsu extends StateBasedGame {
addState(new Options(STATE_OPTIONS));
}
/**
* Launches opsu!.
*/
public static void main(String[] args) {
// log all errors to a file
Log.setVerbose(false);

View File

@ -30,6 +30,11 @@ import java.util.regex.Pattern;
* Indexed, expanding, doubly-linked list data type for song groups.
*/
public class OsuGroupList {
/**
* Song group structure (each group contains of an ArrayList of OsuFiles).
*/
private static OsuGroupList list = new OsuGroupList();
/**
* Search pattern for conditional expressions.
*/
@ -63,11 +68,16 @@ public class OsuGroupList {
* The last search query.
*/
private String lastQuery = "";
/**
* Returns the single instance of this class.
*/
public static OsuGroupList get() { return list; }
/**
* Constructor.
*/
public OsuGroupList() {
private OsuGroupList() {
parsedNodes = new ArrayList<OsuGroupNode>();
nodes = parsedNodes;
}

View File

@ -93,7 +93,7 @@ public class OsuParser {
}
if (!osuFiles.isEmpty()) { // add entry if non-empty
Collections.sort(osuFiles);
Opsu.groups.addSongGroup(osuFiles);
OsuGroupList.get().addSongGroup(osuFiles);
}
}

View File

@ -22,6 +22,7 @@ import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.MenuButton;
import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuFile;
import itdelatrisu.opsu.OsuGroupList;
import itdelatrisu.opsu.OsuGroupNode;
import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.audio.MusicController;
@ -217,7 +218,7 @@ public class MainMenu extends BasicGameState {
g.setFont(Utils.FONT_MEDIUM);
int lineHeight = Utils.FONT_MEDIUM.getLineHeight();
g.drawString(String.format("Loaded %d songs and %d beatmaps.",
Opsu.groups.size(), Opsu.groups.getMapCount()), 25, 25);
OsuGroupList.get().size(), OsuGroupList.get().getMapCount()), 25, 25);
if (MusicController.isTrackLoading())
g.drawString("Track loading...", 25, 25 + lineHeight);
else if (MusicController.trackExists()) {
@ -349,7 +350,7 @@ public class MainMenu extends BasicGameState {
} else if (musicNext.contains(x, y)) {
boolean isTheme = MusicController.isThemePlaying();
SongMenu menu = (SongMenu) game.getState(Opsu.STATE_SONGMENU);
OsuGroupNode node = menu.setFocus(Opsu.groups.getRandomNode(), -1, true);
OsuGroupNode node = menu.setFocus(OsuGroupList.get().getRandomNode(), -1, true);
boolean sameAudio = false;
if (node != null) {
sameAudio = MusicController.getOsuFile().audioFilename.equals(node.osuFiles.get(0).audioFilename);
@ -361,7 +362,7 @@ public class MainMenu extends BasicGameState {
} 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);
menu.setFocus(OsuGroupList.get().getBaseNode(previous.pop()), -1, true);
if (Options.isDynamicBackgroundEnabled())
bgAlpha = 0f;
} else

View File

@ -22,6 +22,7 @@ import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.MenuButton;
import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuFile;
import itdelatrisu.opsu.OsuGroupList;
import itdelatrisu.opsu.OsuGroupNode;
import itdelatrisu.opsu.OsuParser;
import itdelatrisu.opsu.SongSort;
@ -287,7 +288,7 @@ public class SongMenu extends BasicGameState {
g.setColor(Utils.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);
g.fillRoundRect(width - 10, scrollStartY + (scrollEndY * startNode.index / OsuGroupList.get().size()), 5, 20, 4);
}
// back button
@ -313,30 +314,30 @@ public class SongMenu extends BasicGameState {
// store the start/focus nodes
if (focusNode != null) {
oldFocusNode = Opsu.groups.getBaseNode(focusNode.index);
oldFocusNode = OsuGroupList.get().getBaseNode(focusNode.index);
oldFileIndex = focusNode.osuFileIndex;
}
if (Opsu.groups.search(search.getText())) {
if (OsuGroupList.get().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();
if (OsuGroupList.get().size() > 0) {
OsuGroupList.get().init();
if (search.getText().isEmpty()) { // cleared search
// use previous start/focus if possible
if (oldFocusNode != null)
setFocus(oldFocusNode, oldFileIndex, true);
else
setFocus(Opsu.groups.getRandomNode(), -1, true);
setFocus(OsuGroupList.get().getRandomNode(), -1, true);
} else {
int size = Opsu.groups.size();
int size = OsuGroupList.get().size();
searchResultString = String.format("%d match%s found!",
size, (size == 1) ? "" : "es");
setFocus(Opsu.groups.getRandomNode(), -1, true);
setFocus(OsuGroupList.get().getRandomNode(), -1, true);
}
oldFocusNode = null;
oldFileIndex = -1;
@ -362,7 +363,7 @@ public class SongMenu extends BasicGameState {
OsuGroupNode node = startNode;
boolean isHover = false;
for (int i = 0; i < MAX_BUTTONS && node != null; i++, node = node.next) {
float cx = (node.index == Opsu.groups.getExpandedIndex()) ? buttonX * 0.9f : buttonX;
float cx = (node.index == OsuGroupList.get().getExpandedIndex()) ? buttonX * 0.9f : buttonX;
if ((mouseX > cx && mouseX < cx + buttonWidth) &&
(mouseY > buttonY + (i * buttonOffset) && mouseY < buttonY + (i * buttonOffset) + buttonHeight)) {
if (i == hoverIndex) {
@ -418,10 +419,10 @@ public class SongMenu extends BasicGameState {
if (sort != SongSort.getSort()) {
SongSort.setSort(sort);
SoundController.playSound(SoundEffect.MENUCLICK);
OsuGroupNode oldFocusBase = Opsu.groups.getBaseNode(focusNode.index);
OsuGroupNode oldFocusBase = OsuGroupList.get().getBaseNode(focusNode.index);
int oldFocusFileIndex = focusNode.osuFileIndex;
focusNode = null;
Opsu.groups.init();
OsuGroupList.get().init();
setFocus(oldFocusBase, oldFocusFileIndex, true);
}
return;
@ -429,7 +430,7 @@ public class SongMenu extends BasicGameState {
}
// song buttons
int expandedIndex = Opsu.groups.getExpandedIndex();
int expandedIndex = OsuGroupList.get().getExpandedIndex();
OsuGroupNode node = startNode;
for (int i = 0; i < MAX_BUTTONS && node != null; i++, node = node.next) {
// is button at this index clicked?
@ -484,7 +485,7 @@ public class SongMenu extends BasicGameState {
game.enterState(Opsu.STATE_OPTIONS, new EmptyTransition(), new FadeInTransition(Color.black));
break;
case Input.KEY_F2:
setFocus(Opsu.groups.getRandomNode(), -1, true);
setFocus(OsuGroupList.get().getRandomNode(), -1, true);
break;
case Input.KEY_F12:
Utils.takeScreenShot();
@ -598,7 +599,7 @@ public class SongMenu extends BasicGameState {
n++;
shifted = true;
} else if (n > 0 && startNode.next != null &&
Opsu.groups.getNode(startNode, MAX_BUTTONS) != null) {
OsuGroupList.get().getNode(startNode, MAX_BUTTONS) != null) {
startNode = startNode.next;
buttonY -= buttonOffset / 4;
if (buttonY < height * 0.14f)
@ -632,9 +633,9 @@ public class SongMenu extends BasicGameState {
OsuGroupNode oldFocus = focusNode;
// expand node before focusing it
int expandedIndex = Opsu.groups.getExpandedIndex();
int expandedIndex = OsuGroupList.get().getExpandedIndex();
if (node.index != expandedIndex) {
node = Opsu.groups.expand(node.index);
node = OsuGroupList.get().expand(node.index);
// if start node was previously expanded, move it
if (startNode != null && startNode.index == expandedIndex)
@ -649,13 +650,13 @@ public class SongMenu extends BasicGameState {
// change the focus node
if (flag || (startNode.index == 0 && startNode.osuFileIndex == -1 && startNode.prev == null))
startNode = node;
focusNode = Opsu.groups.getNode(node, pos);
focusNode = OsuGroupList.get().getNode(node, pos);
OsuFile osu = focusNode.osuFiles.get(focusNode.osuFileIndex);
MusicController.play(osu, true);
Utils.loadGlyphs(osu);
// check startNode bounds
while (startNode.index >= Opsu.groups.size() + length - MAX_BUTTONS && startNode.prev != null)
while (startNode.index >= OsuGroupList.get().size() + length - MAX_BUTTONS && startNode.prev != null)
startNode = startNode.prev;
// make sure focusNode is on the screen (TODO: cleanup...)

View File

@ -20,6 +20,7 @@ package itdelatrisu.opsu.states;
import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.Opsu;
import itdelatrisu.opsu.OsuGroupList;
import itdelatrisu.opsu.OsuParser;
import itdelatrisu.opsu.OszUnpacker;
import itdelatrisu.opsu.Utils;
@ -133,8 +134,8 @@ public class Splash extends BasicGameState {
// change states when loading complete
if (finished && alpha >= 1f) {
// initialize song list
Opsu.groups.init();
((SongMenu) game.getState(Opsu.STATE_SONGMENU)).setFocus(Opsu.groups.getRandomNode(), -1, true);
OsuGroupList.get().init();
((SongMenu) game.getState(Opsu.STATE_SONGMENU)).setFocus(OsuGroupList.get().getRandomNode(), -1, true);
// play the theme song
if (Options.isThemSongEnabled())