Major song menu improvements and fixes.

- Song group nodes are now hidden when the group is expanded.
- Setting a new focus will always move the focus into view.
- Left and right arrow keys now shift focus by exactly 1 node.
- Render the version string for unexpanded song group nodes containing only 1 beatmap.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2014-07-10 19:13:53 -04:00
parent 5a1972a2bd
commit 95f969f62f
3 changed files with 100 additions and 87 deletions

View File

@ -134,7 +134,10 @@ public class OsuGroupList {
* Returns a random base node. * Returns a random base node.
*/ */
public OsuGroupNode getRandomNode() { public OsuGroupNode getRandomNode() {
return getBaseNode((int) (Math.random() * size())); OsuGroupNode node = getBaseNode((int) (Math.random() * size()));
if (node.index == expandedIndex) // don't choose an expanded group node
node = node.next;
return node;
} }
/** /**
@ -160,18 +163,23 @@ public class OsuGroupList {
public int getExpandedIndex() { return expandedIndex; } public int getExpandedIndex() { return expandedIndex; }
/** /**
* Expands the node at an index by inserting a new node for each OsuFile in that node. * Expands the node at an index by inserting a new node for each OsuFile
* in that node and hiding the group node.
* @return the first of the newly-inserted nodes
*/ */
public void expand(int index) { public OsuGroupNode expand(int index) {
// undo the previous expansion // undo the previous expansion
if (unexpand() == index) unexpand();
return; // don't re-expand the same node
OsuGroupNode node = getBaseNode(index); OsuGroupNode node = getBaseNode(index);
if (node == null) if (node == null)
return; return null;
OsuGroupNode firstInserted = null;
// create new nodes
ArrayList<OsuFile> osuFiles = node.osuFiles; ArrayList<OsuFile> osuFiles = node.osuFiles;
OsuGroupNode prevNode = node.prev;
OsuGroupNode nextNode = node.next; OsuGroupNode nextNode = node.next;
for (int i = 0; i < node.osuFiles.size(); i++) { for (int i = 0; i < node.osuFiles.size(); i++) {
OsuGroupNode newNode = new OsuGroupNode(osuFiles); OsuGroupNode newNode = new OsuGroupNode(osuFiles);
@ -179,6 +187,14 @@ public class OsuGroupList {
newNode.osuFileIndex = i; newNode.osuFileIndex = i;
newNode.prev = node; newNode.prev = node;
// unlink the group node
if (i == 0) {
firstInserted = newNode;
newNode.prev = prevNode;
if (prevNode != null)
prevNode.next = newNode;
}
node.next = newNode; node.next = newNode;
node = node.next; node = node.next;
} }
@ -188,26 +204,31 @@ public class OsuGroupList {
} }
expandedIndex = index; expandedIndex = index;
return firstInserted;
} }
/** /**
* Undoes the current expansion, if any. * Undoes the current expansion, if any.
* @return the index of the previously expanded node, or -1 if no expansions
*/ */
private int unexpand() { private void unexpand() {
if (expandedIndex < 0 || expandedIndex >= size()) if (expandedIndex < 0 || expandedIndex >= size())
return -1; return;
// reset [base].next and [base+1].prev // recreate surrounding links
OsuGroupNode eCur = getBaseNode(expandedIndex); OsuGroupNode
OsuGroupNode eNext = getBaseNode(expandedIndex + 1); ePrev = getBaseNode(expandedIndex - 1),
eCur = getBaseNode(expandedIndex),
eNext = getBaseNode(expandedIndex + 1);
if (ePrev != null)
ePrev.next = eCur;
eCur.prev = ePrev;
eCur.index = expandedIndex;
eCur.next = eNext; eCur.next = eNext;
if (eNext != null) if (eNext != null)
eNext.prev = eCur; eNext.prev = eCur;
int oldIndex = expandedIndex;
expandedIndex = -1; expandedIndex = -1;
return oldIndex; return;
} }
/** /**

View File

@ -120,27 +120,27 @@ public class OsuGroupNode implements Comparable<OsuGroupNode> {
Color textColor = Color.lightGray; Color textColor = Color.lightGray;
if (expanded) { // expanded if (expanded) { // expanded
xOffset = bg.getWidth() / 10f;
if (focus) { if (focus) {
xOffset = bg.getWidth() * -0.05f; bg.draw(x - xOffset, y, Color.white);
bg.draw(x + xOffset, y, Color.white);
textColor = Color.white; textColor = Color.white;
} else { } else
xOffset = bg.getWidth() * 0.05f; bg.draw(x - xOffset, y, Utils.COLOR_BLUE_BUTTON);
bg.draw(x + xOffset, y, Utils.COLOR_BLUE_BUTTON);
}
osu = osuFiles.get(osuFileIndex); osu = osuFiles.get(osuFileIndex);
} else { } else {
bg.draw(x, y, Utils.COLOR_ORANGE_BUTTON); bg.draw(x, y, Utils.COLOR_ORANGE_BUTTON);
osu = osuFiles.get(0); osu = osuFiles.get(0);
} }
float cx = x + (bg.getWidth() * 0.05f) + xOffset; float cx = x + (bg.getWidth() * 0.05f) - xOffset;
float cy = y + (bg.getHeight() * 0.2f) - 3; float cy = y + (bg.getHeight() * 0.2f) - 3;
Utils.FONT_MEDIUM.drawString(cx, cy, osu.title, textColor); Utils.FONT_MEDIUM.drawString(cx, cy, osu.title, textColor);
Utils.FONT_DEFAULT.drawString(cx, cy + Utils.FONT_MEDIUM.getLineHeight() - 4, String.format("%s // %s", osu.artist, osu.creator), textColor); Utils.FONT_DEFAULT.drawString(cx, cy + Utils.FONT_MEDIUM.getLineHeight() - 4,
if (expanded) String.format("%s // %s", osu.artist, osu.creator), textColor);
Utils.FONT_BOLD.drawString(cx, cy + Utils.FONT_MEDIUM.getLineHeight() + Utils.FONT_DEFAULT.getLineHeight() - 8, osu.version, textColor); if (expanded || osuFiles.size() == 1)
Utils.FONT_BOLD.drawString(cx, cy + Utils.FONT_MEDIUM.getLineHeight() + Utils.FONT_DEFAULT.getLineHeight() - 8,
osu.version, textColor);
} }
/** /**

View File

@ -310,7 +310,7 @@ public class SongMenu extends BasicGameState {
if (search.getText().isEmpty()) { // cleared search if (search.getText().isEmpty()) { // cleared search
// use previous start/focus if possible // use previous start/focus if possible
if (oldFocusNode != null) if (oldFocusNode != null)
setFocus(oldFocusNode, oldFileIndex + 1, true); setFocus(oldFocusNode, oldFileIndex, true);
else else
setFocus(Opsu.groups.getRandomNode(), -1, true); setFocus(Opsu.groups.getRandomNode(), -1, true);
} else { } else {
@ -374,34 +374,24 @@ public class SongMenu extends BasicGameState {
int oldFocusFileIndex = focusNode.osuFileIndex; int oldFocusFileIndex = focusNode.osuFileIndex;
focusNode = null; focusNode = null;
Opsu.groups.init(i); Opsu.groups.init(i);
setFocus(oldFocusBase, oldFocusFileIndex + 1, true); setFocus(oldFocusBase, oldFocusFileIndex, true);
} }
return; return;
} }
} }
for (int i = 0; i < MAX_BUTTONS; i++) { // song buttons
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(); int expandedIndex = Opsu.groups.getExpandedIndex();
OsuGroupNode node = startNode;
for (int i = 0; i < MAX_BUTTONS && node != null; i++, node = node.next) {
// is button at this index clicked?
float cx = (node.index == expandedIndex) ? buttonX * 0.9f : buttonX;
if ((x > cx && x < cx + buttonWidth) &&
(y > buttonY + (i * buttonOffset) && y < buttonY + (i * buttonOffset) + buttonHeight)) {
// clicked node is already expanded // clicked node is already expanded
if (node.index == expandedIndex) { if (node.index == expandedIndex) {
if (node.osuFileIndex == -1) { if (node.osuFileIndex == focusNode.osuFileIndex) {
// 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
SoundController.playSound(SoundController.SOUND_MENUCLICK);
Opsu.groups.expand(node.index);
} else if (node.osuFileIndex == focusNode.osuFileIndex) {
// if already focused, load the beatmap // if already focused, load the beatmap
startGame(); startGame();
@ -413,22 +403,15 @@ public class SongMenu extends BasicGameState {
break; break;
} }
// if current start node is expanded, // clicked node is a new group
// set it to the base node before undoing the expansion else {
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);
}
SoundController.playSound(SoundController.SOUND_MENUCLICK); SoundController.playSound(SoundController.SOUND_MENUCLICK);
setFocus(node, -1, false); setFocus(node, -1, false);
break; break;
} }
} }
} }
}
@Override @Override
public void keyPressed(int key, char c) { public void keyPressed(int key, char c) {
@ -467,8 +450,7 @@ public class SongMenu extends BasicGameState {
OsuGroupNode next = focusNode.next; OsuGroupNode next = focusNode.next;
if (next != null) { if (next != null) {
SoundController.playSound(SoundController.SOUND_MENUCLICK); SoundController.playSound(SoundController.SOUND_MENUCLICK);
setFocus(next, (next.index == focusNode.index) ? 0 : 1, false); setFocus(next, 0, false);
changeIndex(1);
} }
break; break;
case Input.KEY_LEFT: case Input.KEY_LEFT:
@ -477,21 +459,7 @@ public class SongMenu extends BasicGameState {
OsuGroupNode prev = focusNode.prev; OsuGroupNode prev = focusNode.prev;
if (prev != null) { if (prev != null) {
SoundController.playSound(SoundController.SOUND_MENUCLICK); SoundController.playSound(SoundController.SOUND_MENUCLICK);
if (prev.index == focusNode.index && prev.osuFileIndex < 0) { setFocus(prev, (prev.index == focusNode.index) ? 0 : prev.osuFiles.size() - 1, false);
// 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; break;
case Input.KEY_NEXT: case Input.KEY_NEXT:
@ -576,7 +544,7 @@ public class SongMenu extends BasicGameState {
* Sets a new focus node. * Sets a new focus node.
* @param node the base node; it will be expanded if it isn't already * @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 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 * @param flag if true, startNode will be set to the first node in the group
* @return the old focus node * @return the old focus node
*/ */
public OsuGroupNode setFocus(OsuGroupNode node, int pos, boolean flag) { public OsuGroupNode setFocus(OsuGroupNode node, int pos, boolean flag) {
@ -586,25 +554,49 @@ public class SongMenu extends BasicGameState {
OsuGroupNode oldFocus = focusNode; OsuGroupNode oldFocus = focusNode;
// expand node before focusing it // expand node before focusing it
if (node.index != Opsu.groups.getExpandedIndex()) int expandedIndex = Opsu.groups.getExpandedIndex();
Opsu.groups.expand(node.index); if (node.index != expandedIndex) {
node = Opsu.groups.expand(node.index);
// if start node was previously expanded, move it
if (startNode != null && startNode.index == expandedIndex)
startNode = node;
}
// check pos bounds // check pos bounds
int length = node.osuFiles.size(); int length = node.osuFiles.size();
if (pos < 0 || pos > length) // set a random pos if (pos < 0 || pos > length - 1) // set a random pos
pos = (int) (Math.random() * length) + 1; pos = (int) (Math.random() * length);
if (flag) // change the focus node
if (flag || (startNode.index == 0 && startNode.prev == null))
startNode = node; startNode = node;
focusNode = Opsu.groups.getNode(node, pos); focusNode = Opsu.groups.getNode(node, pos);
MusicController.play(focusNode.osuFiles.get(focusNode.osuFileIndex), true); MusicController.play(focusNode.osuFiles.get(focusNode.osuFileIndex), true);
// check startNode bounds // check startNode bounds
if (focusNode.index - startNode.index == MAX_BUTTONS - 1) while (startNode.index >= Opsu.groups.size() + length - MAX_BUTTONS && startNode.prev != null)
startNode = startNode.prev;
// make sure focusNode is on the screen (TODO: cleanup...)
int val = focusNode.index + focusNode.osuFileIndex - (startNode.index + MAX_BUTTONS) + 1;
if (val > 0) // below screen
changeIndex(val);
else { // above screen
if (focusNode.index == startNode.index) {
val = focusNode.index + focusNode.osuFileIndex - (startNode.index + startNode.osuFileIndex);
if (val < 0)
changeIndex(val);
} else if (startNode.index > focusNode.index) {
val = focusNode.index - focusNode.osuFiles.size() + focusNode.osuFileIndex - startNode.index + 1;
if (val < 0)
changeIndex(val);
}
}
// if start node is expanded and on group node, move it
if (startNode.index == focusNode.index && startNode.osuFileIndex == -1)
changeIndex(1); changeIndex(1);
while (startNode.index >= Opsu.groups.size() + length + 1 - MAX_BUTTONS &&
startNode.prev != null)
changeIndex(-1);
return oldFocus; return oldFocus;
} }