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

View File

@ -2,6 +2,7 @@ package zero1hd.rhythmbullet.audio;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.util.Observable; import java.util.Observable;
import java.util.Observer;
import java.util.Random; import java.util.Random;
import com.badlogic.gdx.Gdx; 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;
import com.badlogic.gdx.audio.Music.OnCompletionListener; 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 MusicList musicList;
private MusicManager mm; private MusicManager mm;
private int currentPlaybackID; 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 (prefs == null) throw new NullPointerException("preferences can't be null...");
if (musicList == null) throw new NullPointerException("music list 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."); if (!musicList.isSearched()) throw new InvalidParameterException("music list has to be searched already.");
musicList.addObserver(this);
this.prefs = prefs; this.prefs = prefs;
this.musicList = musicList; this.musicList = musicList;
rand = new Random(); rand = new Random();
@ -32,7 +33,6 @@ public class MusicListController extends Observable implements OnCompletionListe
public void play() { public void play() {
mm.play(); mm.play();
mm.setVolume(prefs.getFloat("music vol", 1f)); mm.setVolume(prefs.getFloat("music vol", 1f));
} }
public void setMusicByIndex(int index) { public void setMusicByIndex(int index) {
@ -67,9 +67,6 @@ public class MusicListController extends Observable implements OnCompletionListe
} }
} else { } else {
currentPlaybackID++; currentPlaybackID++;
if (currentPlaybackID > musicList.getAmountOfMusic()) {
currentPlaybackID = 0;
}
} }
changeMusic(); changeMusic();
play(); play();
@ -114,13 +111,10 @@ public class MusicListController extends Observable implements OnCompletionListe
if (currentPlaybackID < 0) { if (currentPlaybackID < 0) {
currentPlaybackID = musicList.getAmountOfMusic(); currentPlaybackID = musicList.getAmountOfMusic();
} }
if (currentPlaybackID > musicList.getAmountOfMusic()) {
currentPlaybackID = 0;
}
this.mm = musicList.getMusicInfoFromIndex(currentPlaybackID); 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); mm.setOnCompletionListener(this);
setChanged(); setChanged();
@ -135,4 +129,12 @@ public class MusicListController extends Observable implements OnCompletionListe
public MusicManager getCurrentMusicManager() { public MusicManager getCurrentMusicManager() {
return mm; 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")); previousTop = new Label(null, skin, "sub-font", skin.getColor("default"));
ratedDifficulty = 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)); albumCover = new Image(assetManager.get("defaultCover.png", Texture.class));
refresh();
} }
@Override @Override
@ -261,9 +258,11 @@ public class MusicSelectionPage extends Page implements Observer {
@Override @Override
public void update(Observable o, Object arg) { public void update(Observable o, Object arg) {
MusicManager mm = (MusicManager) arg;
if (o == mc) { if (o == mc) {
MusicManager mm = (MusicManager) arg;
selectSong(mm); selectSong(mm);
} else if (o == mc.getMusicList()) {
refresh();
} }
} }
@ -288,7 +287,7 @@ public class MusicSelectionPage extends Page implements Observer {
private void playSelectedMusic() { private void playSelectedMusic() {
if (currentlySelected.getMusicFile() != mc.getCurrentMusicManager().getMusicFile()) { 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.setMusicByIndex(index);
mc.play(); mc.play();
} }

View File

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

View File

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