From 95f969f62f82f2e9ea6d78df2eba87a8505627be Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Thu, 10 Jul 2014 19:13:53 -0400 Subject: [PATCH] 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 --- src/itdelatrisu/opsu/OsuGroupList.java | 51 +++++++--- src/itdelatrisu/opsu/OsuGroupNode.java | 22 ++--- src/itdelatrisu/opsu/states/SongMenu.java | 114 ++++++++++------------ 3 files changed, 100 insertions(+), 87 deletions(-) diff --git a/src/itdelatrisu/opsu/OsuGroupList.java b/src/itdelatrisu/opsu/OsuGroupList.java index df283d0c..72277d5a 100644 --- a/src/itdelatrisu/opsu/OsuGroupList.java +++ b/src/itdelatrisu/opsu/OsuGroupList.java @@ -134,7 +134,10 @@ public class OsuGroupList { * Returns a random base node. */ 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; } /** @@ -153,25 +156,30 @@ public class OsuGroupList { } return startNode; } - + /** * Returns the index of the expanded node (or -1 if nothing is expanded). */ 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 - if (unexpand() == index) - return; // don't re-expand the same node + unexpand(); OsuGroupNode node = getBaseNode(index); if (node == null) - return; + return null; + OsuGroupNode firstInserted = null; + + // create new nodes ArrayList osuFiles = node.osuFiles; + OsuGroupNode prevNode = node.prev; OsuGroupNode nextNode = node.next; for (int i = 0; i < node.osuFiles.size(); i++) { OsuGroupNode newNode = new OsuGroupNode(osuFiles); @@ -179,6 +187,14 @@ public class OsuGroupList { newNode.osuFileIndex = i; 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 = node.next; } @@ -188,26 +204,31 @@ public class OsuGroupList { } expandedIndex = index; + return firstInserted; } /** * 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()) - return -1; + return; - // reset [base].next and [base+1].prev - OsuGroupNode eCur = getBaseNode(expandedIndex); - OsuGroupNode eNext = getBaseNode(expandedIndex + 1); + // recreate surrounding links + OsuGroupNode + 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; if (eNext != null) eNext.prev = eCur; - int oldIndex = expandedIndex; expandedIndex = -1; - return oldIndex; + return; } /** diff --git a/src/itdelatrisu/opsu/OsuGroupNode.java b/src/itdelatrisu/opsu/OsuGroupNode.java index 83c67a13..effe6ab3 100644 --- a/src/itdelatrisu/opsu/OsuGroupNode.java +++ b/src/itdelatrisu/opsu/OsuGroupNode.java @@ -120,27 +120,27 @@ public class OsuGroupNode implements Comparable { Color textColor = Color.lightGray; if (expanded) { // expanded + xOffset = bg.getWidth() / 10f; if (focus) { - xOffset = bg.getWidth() * -0.05f; - bg.draw(x + xOffset, y, Color.white); + bg.draw(x - xOffset, y, Color.white); textColor = Color.white; - } else { - xOffset = bg.getWidth() * 0.05f; - bg.draw(x + xOffset, y, Utils.COLOR_BLUE_BUTTON); - } + } else + bg.draw(x - xOffset, y, Utils.COLOR_BLUE_BUTTON); osu = osuFiles.get(osuFileIndex); } else { bg.draw(x, y, Utils.COLOR_ORANGE_BUTTON); 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; 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); - if (expanded) - Utils.FONT_BOLD.drawString(cx, cy + Utils.FONT_MEDIUM.getLineHeight() + Utils.FONT_DEFAULT.getLineHeight() - 8, osu.version, textColor); + Utils.FONT_DEFAULT.drawString(cx, cy + Utils.FONT_MEDIUM.getLineHeight() - 4, + String.format("%s // %s", osu.artist, osu.creator), textColor); + if (expanded || osuFiles.size() == 1) + Utils.FONT_BOLD.drawString(cx, cy + Utils.FONT_MEDIUM.getLineHeight() + Utils.FONT_DEFAULT.getLineHeight() - 8, + osu.version, textColor); } /** diff --git a/src/itdelatrisu/opsu/states/SongMenu.java b/src/itdelatrisu/opsu/states/SongMenu.java index 6339d1cd..406e996b 100644 --- a/src/itdelatrisu/opsu/states/SongMenu.java +++ b/src/itdelatrisu/opsu/states/SongMenu.java @@ -310,7 +310,7 @@ public class SongMenu extends BasicGameState { if (search.getText().isEmpty()) { // cleared search // use previous start/focus if possible if (oldFocusNode != null) - setFocus(oldFocusNode, oldFileIndex + 1, true); + setFocus(oldFocusNode, oldFileIndex, true); else setFocus(Opsu.groups.getRandomNode(), -1, true); } else { @@ -374,34 +374,24 @@ public class SongMenu extends BasicGameState { int oldFocusFileIndex = focusNode.osuFileIndex; focusNode = null; Opsu.groups.init(i); - setFocus(oldFocusBase, oldFocusFileIndex + 1, true); + setFocus(oldFocusBase, oldFocusFileIndex, true); } return; } } - 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(); + // song buttons + 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 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 - SoundController.playSound(SoundController.SOUND_MENUCLICK); - Opsu.groups.expand(node.index); - - } else if (node.osuFileIndex == focusNode.osuFileIndex) { + if (node.osuFileIndex == focusNode.osuFileIndex) { // if already focused, load the beatmap startGame(); @@ -413,19 +403,12 @@ public class SongMenu extends BasicGameState { 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); + // clicked node is a new group + else { + SoundController.playSound(SoundController.SOUND_MENUCLICK); + setFocus(node, -1, false); + break; } - SoundController.playSound(SoundController.SOUND_MENUCLICK); - setFocus(node, -1, false); - - break; } } } @@ -467,8 +450,7 @@ public class SongMenu extends BasicGameState { OsuGroupNode next = focusNode.next; if (next != null) { SoundController.playSound(SoundController.SOUND_MENUCLICK); - setFocus(next, (next.index == focusNode.index) ? 0 : 1, false); - changeIndex(1); + setFocus(next, 0, false); } break; case Input.KEY_LEFT: @@ -477,21 +459,7 @@ public class SongMenu extends BasicGameState { OsuGroupNode prev = focusNode.prev; if (prev != null) { SoundController.playSound(SoundController.SOUND_MENUCLICK); - 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); - } + setFocus(prev, (prev.index == focusNode.index) ? 0 : prev.osuFiles.size() - 1, false); } break; case Input.KEY_NEXT: @@ -576,35 +544,59 @@ public class SongMenu extends BasicGameState { * 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 + * @param flag if true, startNode will be set to the first node in the group * @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); + int expandedIndex = Opsu.groups.getExpandedIndex(); + 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 int length = node.osuFiles.size(); - if (pos < 0 || pos > length) // set a random pos - pos = (int) (Math.random() * length) + 1; + if (pos < 0 || pos > length - 1) // set a random pos + pos = (int) (Math.random() * length); - if (flag) + // change the focus node + if (flag || (startNode.index == 0 && startNode.prev == null)) 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) + 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); - while (startNode.index >= Opsu.groups.size() + length + 1 - MAX_BUTTONS && - startNode.prev != null) - changeIndex(-1); return oldFocus; }