alphabetically ordered files

This commit is contained in:
Harrison Deng 2017-11-26 13:50:35 -06:00
parent 6aa03bd344
commit 247885d5cf
6 changed files with 71 additions and 65 deletions

View File

@ -5,29 +5,38 @@ import java.util.Observable;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Sort;
import zero1hd.rhythmbullet.util.FileHandleAlphabeticalComparator;
public class MusicList extends Observable {
private Array<FileHandle> songList;
private Array<FileHandle> musicList;
private String searchPath;
private boolean searched;
private FileHandleAlphabeticalComparator fhac;
public MusicList() {
songList = new Array<>();
musicList = new Array<>();
fhac = new FileHandleAlphabeticalComparator();
}
public void refresh() {
searched = true;
songList.clear();
musicList.clear();
Gdx.app.debug("SongController", "Searching path: " + searchPath);
if (Gdx.files.absolute(searchPath).exists() && Gdx.files.absolute(searchPath).isDirectory()) {
songList.addAll(Gdx.files.absolute(searchPath).list((dir, name) -> {
musicList.addAll(Gdx.files.absolute(searchPath).list((dir, name) -> {
if (name.endsWith("mp3") || name.endsWith("wav")) {
return true;
}
return false;
}));
}
if (!Gdx.files.external("RhythmBullet/Alan Walker - Spectre.mp3").exists()) {
Gdx.files.internal("music/Alan Walker - Spectre.mp3").copyTo(Gdx.files.external("RhythmBullet/Alan Walker - Spectre.mp3"));
}
musicList.add(Gdx.files.external("RhythmBullet/Alan Walker - Spectre.mp3"));
setChanged();
Sort.instance().sort(musicList, fhac);
notifyObservers();
}
@ -47,26 +56,20 @@ public class MusicList extends Observable {
public MusicManager getMusicInfoFromIndex(int index) {
if (!searched) Gdx.app.debug("SongList", "Warning, this list hasn't even searched yet...");
if (songList.size == 0) {
return null;
}
return getAudioData(songList.get(index));
return getAudioData(musicList.get(index));
}
public int getAmountOfMusic() {
return songList.size;
return musicList.size;
}
public FileHandle getSongFileHandleFromIndex(int index) {
if (!searched) Gdx.app.debug("SongList", "Warning, this list hasn't even searched yet...");
if (songList.size == 0) {
return null;
}
return songList.get(index);
return musicList.get(index);
}
public Array<FileHandle> getSongList() {
return songList;
public Array<FileHandle> getMusicList() {
return musicList;
}
public boolean isSearched() {

View File

@ -2,6 +2,7 @@ package zero1hd.rhythmbullet.audio;
import java.security.InvalidParameterException;
import java.util.Observable;
import java.util.Observer;
import java.util.Random;
import com.badlogic.gdx.Gdx;
@ -9,7 +10,7 @@ import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Music.OnCompletionListener;
public class MusicListController extends Observable implements OnCompletionListener {
public class MusicListController extends Observable implements OnCompletionListener, Observer {
private MusicList musicList;
private MusicManager mm;
private int currentPlaybackID;
@ -22,7 +23,7 @@ public class MusicListController extends Observable implements OnCompletionListe
if (prefs == null) throw new NullPointerException("preferences can't be null...");
if (musicList == null) throw new NullPointerException("music list can't be null...");
if (!musicList.isSearched()) throw new InvalidParameterException("music list has to be searched already.");
musicList.addObserver(this);
this.prefs = prefs;
this.musicList = musicList;
rand = new Random();
@ -32,7 +33,6 @@ public class MusicListController extends Observable implements OnCompletionListe
public void play() {
mm.play();
mm.setVolume(prefs.getFloat("music vol", 1f));
}
public void setMusicByIndex(int index) {
@ -67,9 +67,6 @@ public class MusicListController extends Observable implements OnCompletionListe
}
} else {
currentPlaybackID++;
if (currentPlaybackID > musicList.getAmountOfMusic()) {
currentPlaybackID = 0;
}
}
changeMusic();
play();
@ -114,13 +111,10 @@ public class MusicListController extends Observable implements OnCompletionListe
if (currentPlaybackID < 0) {
currentPlaybackID = musicList.getAmountOfMusic();
}
if (currentPlaybackID > musicList.getAmountOfMusic()) {
currentPlaybackID = 0;
}
this.mm = musicList.getMusicInfoFromIndex(currentPlaybackID);
if (mm == null) {
if (!Gdx.files.external("RhythmBullet/Alan Walker - Spectre.mp3").exists()) {
Gdx.files.internal("music/Alan Walker - Spectre.mp3").copyTo(Gdx.files.external("RhythmBullet/Alan Walker - Spectre.mp3"));
}
mm = musicList.getAudioData(Gdx.files.external("RhythmBullet/Alan Walker - Spectre.mp3"));
}
mm.setOnCompletionListener(this);
setChanged();
@ -135,4 +129,12 @@ public class MusicListController extends Observable implements OnCompletionListe
public MusicManager getCurrentMusicManager() {
return mm;
}
@Override
public void update(Observable o, Object arg) {
if (o == musicList) {
changeMusic();
play();
}
}
}

View File

@ -124,9 +124,6 @@ public class MusicSelectionPage extends Page implements Observer {
previousTop = new Label(null, skin, "sub-font", skin.getColor("default"));
ratedDifficulty = new Label(null, skin, "sub-font", skin.getColor("default"));
albumCover = new Image(assetManager.get("defaultCover.png", Texture.class));
refresh();
}
@Override
@ -261,9 +258,11 @@ public class MusicSelectionPage extends Page implements Observer {
@Override
public void update(Observable o, Object arg) {
MusicManager mm = (MusicManager) arg;
if (o == mc) {
MusicManager mm = (MusicManager) arg;
selectSong(mm);
} else if (o == mc.getMusicList()) {
refresh();
}
}
@ -288,7 +287,7 @@ public class MusicSelectionPage extends Page implements Observer {
private void playSelectedMusic() {
if (currentlySelected.getMusicFile() != mc.getCurrentMusicManager().getMusicFile()) {
int index = mc.getMusicList().getSongList().indexOf(currentlySelected.getMusicFile(), true);
int index = mc.getMusicList().getMusicList().indexOf(currentlySelected.getMusicFile(), true);
mc.setMusicByIndex(index);
mc.play();
}

View File

@ -72,7 +72,7 @@ public class MusicSelector extends Window {
public void refresh() {
songList.refresh();
musicList.setItems(songList.getSongList());
musicList.setItems(songList.getMusicList());
}

View File

@ -1,8 +1,5 @@
package zero1hd.rhythmbullet.screens;
import java.util.Observable;
import java.util.Observer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.ScreenAdapter;
@ -32,7 +29,7 @@ import zero1hd.rhythmbullet.graphics.ui.pages.OptionsPage;
import zero1hd.rhythmbullet.graphics.ui.pages.VideoOptionsPage;
import zero1hd.rhythmbullet.util.TransitionAdapter;
public class MainMenu extends ScreenAdapter implements TransitionAdapter, Observer {
public class MainMenu extends ScreenAdapter implements TransitionAdapter {
public Stage stage;
private Vector3 cameraPosition;
@ -45,7 +42,7 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter, Observ
private RhythmBullet core;
private MusicListController mc;
private MusicListController mlc;
private float lerpAlpha;
private ShaderProgram gaussianBlurShader;
@ -65,13 +62,13 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter, Observ
stage = new Stage(new ScreenViewport());
cameraPosition = new Vector3(stage.getCamera().position);
MusicList songList = new MusicList();
songList.setSearchPath(core.getPrefs().getString("music dir"));
songList.refresh();
mc = new MusicListController(songList, core.getPrefs());
mc.setAutoPlay(true);
mc.setShuffle(true);
mc.shuffle(true);
MusicList musicList = new MusicList();
musicList.setSearchPath(core.getPrefs().getString("music dir"));
musicList.refresh();
mlc = new MusicListController(musicList, core.getPrefs());
mlc.setAutoPlay(true);
mlc.setShuffle(true);
mlc.shuffle(true);
postTransition();
}
@ -80,7 +77,7 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter, Observ
public void postTransition() {
attemptLoadShaders();
mainPage = new MainPage(core, cameraPosition, mc, this);
mainPage = new MainPage(core, cameraPosition, mlc, this);
mainPage.setPosition(0, 0);
stage.addActor(mainPage);
//End main menu
@ -94,7 +91,7 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter, Observ
graphicsPage.setPosition(-1f*Gdx.graphics.getWidth(), 1f*Gdx.graphics.getHeight());
stage.addActor(graphicsPage);
optionsPage = new OptionsPage(core, cameraPosition, keybindPage, mc);
optionsPage = new OptionsPage(core, cameraPosition, keybindPage, mlc);
optionsPage.setPosition(-Gdx.graphics.getWidth(), 0);
stage.addActor(optionsPage);
@ -102,7 +99,7 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter, Observ
creditsPage.setPosition(0, Gdx.graphics.getHeight());
stage.addActor(creditsPage);
musicSelectionPage = new MusicSelectionPage(core.getDefaultSkin(), mc, core.getAssetManager(), cameraPosition);
musicSelectionPage = new MusicSelectionPage(core.getDefaultSkin(), mlc, core.getAssetManager(), cameraPosition);
musicSelectionPage.setPosition(1f*Gdx.graphics.getWidth(), 0f);
stage.addActor(musicSelectionPage);
@ -131,13 +128,13 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter, Observ
super.clicked(event, x, y);
}
});
mc.deleteObservers();
mc.addObserver(mainPage);
mc.addObserver(musicSelectionPage);
mainPage.updateVisualsForDifferentSong(mc.getCurrentMusicManager());
mc.getMusicList().addObserver(this);
musicSelectionPage.selectSong(mc.getCurrentMusicManager());
mlc.deleteObservers();
mlc.addObserver(mainPage);
mlc.addObserver(musicSelectionPage);
mlc.getMusicList().addObserver(musicSelectionPage);
musicSelectionPage.refresh();
mainPage.updateVisualsForDifferentSong(mlc.getCurrentMusicManager());
musicSelectionPage.selectSong(mlc.getCurrentMusicManager());
}
public void attemptLoadShaders() {
if (core.getPrefs().getBoolean("glow shader", true)) {
@ -293,8 +290,7 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter, Observ
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
mc.play();
musicSelectionPage.selectSong(mc.getCurrentMusicManager());
mlc.play();
calcLerpAlpha(Gdx.graphics.getWidth());
super.show();
}
@ -368,13 +364,6 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter, Observ
return cameraPosition;
}
@Override
public void update(Observable o, Object arg) {
if (o == mc.getMusicList()) {
musicSelectionPage.refresh();
}
}
public MainPage getMainPage() {
return mainPage;
}

View File

@ -0,0 +1,13 @@
package zero1hd.rhythmbullet.util;
import java.util.Comparator;
import com.badlogic.gdx.files.FileHandle;
public class FileHandleAlphabeticalComparator implements Comparator<FileHandle>{
@Override
public int compare(FileHandle o1, FileHandle o2) {
return o1.nameWithoutExtension().compareTo(o2.nameWithoutExtension());
}
}