2014-07-19 02:33:41 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2014-07-19 02:33:41 +02:00
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-05-29 08:34:08 +02:00
|
|
|
package itdelatrisu.opsu.beatmap;
|
2014-07-19 02:33:41 +02:00
|
|
|
|
2015-05-29 08:34:08 +02:00
|
|
|
import itdelatrisu.opsu.GameImage;
|
2015-05-29 07:55:57 +02:00
|
|
|
import itdelatrisu.opsu.ui.MenuButton;
|
|
|
|
import itdelatrisu.opsu.ui.UI;
|
2015-05-17 03:25:19 +02:00
|
|
|
|
2014-07-19 02:33:41 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
|
|
|
import org.newdawn.slick.Image;
|
|
|
|
|
|
|
|
/**
|
2015-05-29 08:34:08 +02:00
|
|
|
* Beatmap sorting orders.
|
2014-07-19 02:33:41 +02:00
|
|
|
*/
|
2015-05-29 08:34:08 +02:00
|
|
|
public enum BeatmapSortOrder {
|
2014-07-19 02:33:41 +02:00
|
|
|
TITLE (0, "Title", new TitleOrder()),
|
|
|
|
ARTIST (1, "Artist", new ArtistOrder()),
|
|
|
|
CREATOR (2, "Creator", new CreatorOrder()),
|
|
|
|
BPM (3, "BPM", new BPMOrder()),
|
|
|
|
LENGTH (4, "Length", new LengthOrder());
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The ID of the sort (used for tab positioning). */
|
2015-08-21 04:11:55 +02:00
|
|
|
private final int id;
|
2014-07-19 02:33:41 +02:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The name of the sort. */
|
2015-08-21 04:11:55 +02:00
|
|
|
private final String name;
|
2014-07-19 02:33:41 +02:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The comparator for the sort. */
|
2015-08-21 04:11:55 +02:00
|
|
|
private final Comparator<BeatmapSetNode> comparator;
|
2014-07-19 02:33:41 +02:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The tab associated with the sort (displayed in Song Menu screen). */
|
2014-12-30 06:00:58 +01:00
|
|
|
private MenuButton tab;
|
2014-07-19 02:33:41 +02:00
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Total number of sorts. */
|
|
|
|
private static final int SIZE = values().length;
|
2014-07-19 02:33:41 +02:00
|
|
|
|
2015-05-29 08:34:08 +02:00
|
|
|
/** Array of BeatmapSortOrder objects in reverse order. */
|
|
|
|
public static final BeatmapSortOrder[] VALUES_REVERSED;
|
2014-07-19 02:33:41 +02:00
|
|
|
static {
|
2015-01-22 06:44:45 +01:00
|
|
|
VALUES_REVERSED = values();
|
2014-07-19 02:33:41 +02:00
|
|
|
Collections.reverse(Arrays.asList(VALUES_REVERSED));
|
|
|
|
}
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Current sort. */
|
2015-05-29 08:34:08 +02:00
|
|
|
private static BeatmapSortOrder currentSort = TITLE;
|
2014-07-19 02:33:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current sort.
|
|
|
|
* @return the current sort
|
|
|
|
*/
|
2015-05-29 08:34:08 +02:00
|
|
|
public static BeatmapSortOrder getSort() { return currentSort; }
|
2014-07-19 02:33:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a new sort.
|
|
|
|
* @param sort the new sort
|
|
|
|
*/
|
2015-05-29 08:34:08 +02:00
|
|
|
public static void setSort(BeatmapSortOrder sort) { BeatmapSortOrder.currentSort = sort; }
|
2014-07-19 02:33:41 +02:00
|
|
|
|
|
|
|
/**
|
2015-05-17 03:25:19 +02:00
|
|
|
* Compares two BeatmapSetNode objects by title.
|
2014-07-19 02:33:41 +02:00
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
private static class TitleOrder implements Comparator<BeatmapSetNode> {
|
2014-07-19 02:33:41 +02:00
|
|
|
@Override
|
2015-05-17 03:25:19 +02:00
|
|
|
public int compare(BeatmapSetNode v, BeatmapSetNode w) {
|
2015-05-17 04:49:18 +02:00
|
|
|
return v.getBeatmapSet().get(0).title.compareToIgnoreCase(w.getBeatmapSet().get(0).title);
|
2014-07-19 02:33:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-17 03:25:19 +02:00
|
|
|
* Compares two BeatmapSetNode objects by artist.
|
2014-07-19 02:33:41 +02:00
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
private static class ArtistOrder implements Comparator<BeatmapSetNode> {
|
2014-07-19 02:33:41 +02:00
|
|
|
@Override
|
2015-05-17 03:25:19 +02:00
|
|
|
public int compare(BeatmapSetNode v, BeatmapSetNode w) {
|
2015-05-17 04:49:18 +02:00
|
|
|
return v.getBeatmapSet().get(0).artist.compareToIgnoreCase(w.getBeatmapSet().get(0).artist);
|
2014-07-19 02:33:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-17 03:25:19 +02:00
|
|
|
* Compares two BeatmapSetNode objects by creator.
|
2014-07-19 02:33:41 +02:00
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
private static class CreatorOrder implements Comparator<BeatmapSetNode> {
|
2014-07-19 02:33:41 +02:00
|
|
|
@Override
|
2015-05-17 03:25:19 +02:00
|
|
|
public int compare(BeatmapSetNode v, BeatmapSetNode w) {
|
2015-05-17 04:49:18 +02:00
|
|
|
return v.getBeatmapSet().get(0).creator.compareToIgnoreCase(w.getBeatmapSet().get(0).creator);
|
2014-07-19 02:33:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-17 03:25:19 +02:00
|
|
|
* Compares two BeatmapSetNode objects by BPM.
|
2014-07-19 02:33:41 +02:00
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
private static class BPMOrder implements Comparator<BeatmapSetNode> {
|
2014-07-19 02:33:41 +02:00
|
|
|
@Override
|
2015-05-17 03:25:19 +02:00
|
|
|
public int compare(BeatmapSetNode v, BeatmapSetNode w) {
|
2015-05-17 04:49:18 +02:00
|
|
|
return Integer.compare(v.getBeatmapSet().get(0).bpmMax, w.getBeatmapSet().get(0).bpmMax);
|
2014-07-19 02:33:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-17 03:25:19 +02:00
|
|
|
* Compares two BeatmapSetNode objects by length.
|
2014-07-19 02:33:41 +02:00
|
|
|
* Uses the longest beatmap in each set for comparison.
|
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
private static class LengthOrder implements Comparator<BeatmapSetNode> {
|
2014-07-19 02:33:41 +02:00
|
|
|
@Override
|
2015-05-17 03:25:19 +02:00
|
|
|
public int compare(BeatmapSetNode v, BeatmapSetNode w) {
|
2014-07-19 02:33:41 +02:00
|
|
|
int vMax = 0, wMax = 0;
|
2015-09-04 05:16:52 +02:00
|
|
|
for (Beatmap beatmap : v.getBeatmapSet()) {
|
2015-05-17 03:25:19 +02:00
|
|
|
if (beatmap.endTime > vMax)
|
|
|
|
vMax = beatmap.endTime;
|
2014-07-19 02:33:41 +02:00
|
|
|
}
|
2015-09-04 05:16:52 +02:00
|
|
|
for (Beatmap beatmap : w.getBeatmapSet()) {
|
2015-05-17 03:25:19 +02:00
|
|
|
if (beatmap.endTime > wMax)
|
|
|
|
wMax = beatmap.endTime;
|
2014-07-19 02:33:41 +02:00
|
|
|
}
|
|
|
|
return Integer.compare(vMax, wMax);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param id the ID of the sort (for tab positioning)
|
|
|
|
* @param name the sort name
|
|
|
|
* @param comparator the comparator for the sort
|
|
|
|
*/
|
2015-05-29 08:34:08 +02:00
|
|
|
BeatmapSortOrder(int id, String name, Comparator<BeatmapSetNode> comparator) {
|
2014-07-19 02:33:41 +02:00
|
|
|
this.id = id;
|
|
|
|
this.name = name;
|
|
|
|
this.comparator = comparator;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the sort tab.
|
2015-02-16 03:38:54 +01:00
|
|
|
* @param containerWidth the container width
|
|
|
|
* @param bottomY the bottom y coordinate
|
2014-07-19 02:33:41 +02:00
|
|
|
*/
|
2015-02-16 03:38:54 +01:00
|
|
|
public void init(int containerWidth, float bottomY) {
|
2015-01-08 05:45:21 +01:00
|
|
|
Image tab = GameImage.MENU_TAB.getImage();
|
2015-01-11 04:49:23 +01:00
|
|
|
int tabWidth = tab.getWidth();
|
2015-02-16 03:38:54 +01:00
|
|
|
float buttonX = containerWidth / 2f;
|
|
|
|
float tabOffset = (containerWidth - buttonX - tabWidth) / (SIZE - 1);
|
2015-01-11 04:49:23 +01:00
|
|
|
if (tabOffset > tabWidth) { // prevent tabs from being spaced out
|
|
|
|
tabOffset = tabWidth;
|
2015-02-16 03:38:54 +01:00
|
|
|
buttonX = (containerWidth * 0.99f) - (tabWidth * SIZE);
|
2015-01-11 04:49:23 +01:00
|
|
|
}
|
2015-01-08 05:45:21 +01:00
|
|
|
this.tab = new MenuButton(tab,
|
2015-01-11 04:49:23 +01:00
|
|
|
(buttonX + (tabWidth / 2f)) + (id * tabOffset),
|
2015-02-16 03:38:54 +01:00
|
|
|
bottomY - (tab.getHeight() / 2f)
|
2014-07-19 02:33:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the comparator for the sort.
|
|
|
|
* @return the comparator
|
|
|
|
*/
|
2015-05-17 03:25:19 +02:00
|
|
|
public Comparator<BeatmapSetNode> getComparator() { return comparator; }
|
2014-07-19 02:33:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the coordinates are within the image bounds.
|
|
|
|
* @param x the x coordinate
|
|
|
|
* @param y the y coordinate
|
|
|
|
* @return true if within bounds
|
|
|
|
*/
|
|
|
|
public boolean contains(float x, float y) { return tab.contains(x, y); }
|
2015-01-11 04:49:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws the sort tab.
|
|
|
|
* @param selected whether the tab is selected (white) or not (red)
|
2015-01-11 05:14:22 +01:00
|
|
|
* @param isHover whether to include a hover effect (unselected only)
|
2015-01-11 04:49:23 +01:00
|
|
|
*/
|
2015-01-11 05:14:22 +01:00
|
|
|
public void draw(boolean selected, boolean isHover) {
|
2015-03-05 19:27:45 +01:00
|
|
|
UI.drawTab(tab.getX(), tab.getY(), name, selected, isHover);
|
2015-01-11 04:49:23 +01:00
|
|
|
}
|
2014-07-19 02:33:41 +02:00
|
|
|
}
|