slight optimization; fixed issue with directory selection displaying incorrect song count

This commit is contained in:
Harrison Deng 2018-01-29 20:13:27 -06:00
parent c3a13d20fa
commit 1b7f3233e8
6 changed files with 54 additions and 40 deletions

View File

@ -44,7 +44,8 @@ public class MusicList extends Observable {
* refreshes song list, notifies any observers of the refresh while passing no argument to the notifier. * refreshes song list, notifies any observers of the refresh while passing no argument to the notifier.
* Blocking. * Blocking.
*/ */
public void refresh(boolean notify) { public void refresh(boolean notifyOnCompletion) {
searched = false;
musicList.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()) {
@ -58,7 +59,7 @@ public class MusicList extends Observable {
Sort.instance().sort(musicList, fhac); Sort.instance().sort(musicList, fhac);
searched = true; searched = true;
if (notify) { if (notifyOnCompletion) {
notifyObservers(); notifyObservers();
} }
} }
@ -98,7 +99,7 @@ public class MusicList extends Observable {
return null; return null;
} }
public MusicManager getMusicInfoFromIndex(int index) { public MusicManager getMusicManagerFromIndex(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...");
return getAudioData(musicList.get(index)); return getAudioData(musicList.get(index));
} }

View File

@ -73,20 +73,10 @@ public class MusicListController extends Observable implements OnCompletionListe
public void onCompletion(Music music) { public void onCompletion(Music music) {
if (autoPlay) { if (autoPlay) {
if (shuffle) { if (shuffle) {
if (musicList.getTotal() != 0) { shuffle(false);
currentPlaybackID = rand.nextInt(musicList.getTotal());
} else {
currentPlaybackID = 0;
}
} else { } else {
currentPlaybackID++; currentPlaybackID++;
} }
if (currentPlaybackID >= musicList.getTotal()) {
currentPlaybackID = 0;
} else if (currentPlaybackID < 0) {
currentPlaybackID = musicList.getTotal()-1;
}
loadMusic(); loadMusic();
play(); play();
} }
@ -97,6 +87,7 @@ public class MusicListController extends Observable implements OnCompletionListe
* @param load whether this method should also make sure to load the music, dispose of the last one, etc. Normally called unless you plan to manually call it elswhere. * @param load whether this method should also make sure to load the music, dispose of the last one, etc. Normally called unless you plan to manually call it elswhere.
*/ */
public void shuffle(boolean load) { public void shuffle(boolean load) {
Gdx.app.debug("MusicListController", "shuffled.");
if (musicList.getTotal() == 0) { if (musicList.getTotal() == 0) {
currentPlaybackID = 0; currentPlaybackID = 0;
} else { } else {
@ -137,7 +128,7 @@ public class MusicListController extends Observable implements OnCompletionListe
if (currentPlaybackID >= musicList.getTotal()) { if (currentPlaybackID >= musicList.getTotal()) {
currentPlaybackID = 0; currentPlaybackID = 0;
} }
this.mm = musicList.getMusicInfoFromIndex(currentPlaybackID); this.mm = musicList.getMusicManagerFromIndex(currentPlaybackID);
mm.setOnCompletionListener(this); mm.setOnCompletionListener(this);
setChanged(); setChanged();

View File

@ -111,16 +111,17 @@ public class MainPage extends Page implements Observer {
@Override @Override
public void update(Observable o, Object arg) { public void update(Observable o, Object arg) {
if (o == mlc.getMusicList() && mlc.getMusicList().isSearched()) { if (o == mlc.getMusicList() && mlc.getMusicList().isSearched()) {
mlc.shuffle(true); mlc.shuffle(true);
MusicManager mm = mlc.getCurrentMusicManager(); MusicManager mm = mlc.getCurrentMusicManager();
updateVisualsForDifferentSong(mm); updateVisualsForDifferentSong(mm);
mMenu.getMusicSelectionPage().refreshUIList(); mMenu.getMusicSelectionPage().refreshUIList();
} else if (o == mlc) { } else if (o == mlc) {
MusicManager mm = mlc.getCurrentMusicManager(); MusicManager mm = mlc.getCurrentMusicManager();
mlc.play(); mlc.play();
updateVisualsForDifferentSong(mm); updateVisualsForDifferentSong(mm);
} }
} }
public void updateVisualsForDifferentSong(MusicManager mm) { public void updateVisualsForDifferentSong(MusicManager mm) {

View File

@ -180,7 +180,7 @@ public class MusicSelectionPage extends Page implements Observer {
} }
private void attemptRefreshUpdate() { private void attemptRefreshUpdate() {
if (uiSongInfoCount != mc.getMusicList().getTotal()) { if (mc.getMusicList().isSearched() && uiSongInfoCount != mc.getMusicList().getTotal()) {
if (uiSongCount < mc.getMusicList().getTotal()) { if (uiSongCount < mc.getMusicList().getTotal()) {
MusicSelectable selectable = new MusicSelectable(mc.getMusicList().getSongFileHandleFromIndex(uiSongCount), skin, this); MusicSelectable selectable = new MusicSelectable(mc.getMusicList().getSongFileHandleFromIndex(uiSongCount), skin, this);
selectables.add(selectable); selectables.add(selectable);

View File

@ -1,5 +1,8 @@
package zero1hd.rhythmbullet.desktop.graphics.ui.pages; package zero1hd.rhythmbullet.desktop.graphics.ui.pages;
import java.util.Observable;
import java.util.Observer;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences; import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
@ -16,15 +19,19 @@ import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import zero1hd.rhythmbullet.RhythmBullet; import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.desktop.audio.MusicListController; import zero1hd.rhythmbullet.desktop.audio.MusicListController;
public class OptionsPage extends Page { public class OptionsPage extends Page implements Observer {
Table optionsTable; Table optionsTable;
private ProgressBar musicVolSlider; private ProgressBar musicVolSlider;
private ProgressBar fxVolSlider; private ProgressBar fxVolSlider;
private TextField directoryField; private TextField directoryField;
private MusicListController mlc;
private float musicSearchTimer; private float musicSearchTimer;
public OptionsPage(final RhythmBullet core, final Vector3 targetPosition, KeybindOptionsPage moreOptionsPage, final MusicListController sc) {
private Label songCount;
public OptionsPage(final RhythmBullet core, final Vector3 targetPosition, KeybindOptionsPage moreOptionsPage, final MusicListController mlc) {
super("General", core.getDefaultSkin()); super("General", core.getDefaultSkin());
this.mlc = mlc;
//Back button //Back button
TextButton backButton = new TextButton("Back", core.getDefaultSkin()); TextButton backButton = new TextButton("Back", core.getDefaultSkin());
backButton.addListener(new ChangeListener() { backButton.addListener(new ChangeListener() {
@ -52,7 +59,7 @@ public class OptionsPage extends Page {
@Override @Override
public void changed(ChangeEvent event, Actor actor) { public void changed(ChangeEvent event, Actor actor) {
musicVolPercentage.setText(MathUtils.round(musicVolSlider.getValue()) + "%"); musicVolPercentage.setText(MathUtils.round(musicVolSlider.getValue()) + "%");
sc.getCurrentMusicManager().setVolume(musicVolSlider.getPercent()); mlc.getCurrentMusicManager().setVolume(musicVolSlider.getPercent());
core.getPrefs().putFloat("music vol", musicVolSlider.getPercent()); core.getPrefs().putFloat("music vol", musicVolSlider.getPercent());
} }
@ -81,16 +88,15 @@ public class OptionsPage extends Page {
Label musicDirectoryLabel = new Label("Music Directory: ", core.getDefaultSkin()); Label musicDirectoryLabel = new Label("Music Directory: ", core.getDefaultSkin());
optionsTable.add(musicDirectoryLabel); optionsTable.add(musicDirectoryLabel);
final Label songCount = new Label("Songs: " + sc.getMusicList().getTotal(), core.getDefaultSkin(), "sub-font", core.getDefaultSkin().getColor("default")); songCount = new Label("Songs: " + mlc.getMusicList().getTotal(), core.getDefaultSkin(), "sub-font", core.getDefaultSkin().getColor("default"));
directoryField = new TextField(null, core.getDefaultSkin() ) { directoryField = new TextField(null, core.getDefaultSkin() ) {
@Override @Override
public void act(float delta) { public void act(float delta) {
if (musicSearchTimer > 0) { if (musicSearchTimer > 0) {
musicSearchTimer -= delta; musicSearchTimer -= delta;
if (musicSearchTimer <= 0) { if (musicSearchTimer <= 0) {
sc.getMusicList().setSearchPath(directoryField.getText()); mlc.getMusicList().setSearchPath(directoryField.getText());
sc.getMusicList().asynchRefresh(); mlc.getMusicList().asynchRefresh();
songCount.setText("Songs: " + sc.getMusicList().getTotal());
} }
} }
super.act(delta); super.act(delta);
@ -162,4 +168,16 @@ public class OptionsPage extends Page {
prefs.putString("music dir", directoryField.getText()); prefs.putString("music dir", directoryField.getText());
Gdx.app.debug("Preferences", "Saved all basic options page values."); Gdx.app.debug("Preferences", "Saved all basic options page values.");
} }
@Override
public void update(Observable arg0, Object arg1) {
if (arg0 == mlc.getMusicList()) {
songCount.setText(mlc.getMusicList().getTotal() + " songs");
}
}
@Override
public void dispose() {
super.dispose();
}
} }

View File

@ -137,12 +137,12 @@ public class MainMenu extends ScreenAdapter implements AdvancedResizeScreen {
super.clicked(event, x, y); super.clicked(event, x, y);
} }
}); });
mlc.getMusicList().deleteObservers();
mlc.deleteObservers();
mlc.addObserver(musicSelectionPage);
mlc.addObserver(mainPage); mlc.addObserver(mainPage);
mlc.getMusicList().addObserver(mainPage); mlc.getMusicList().addObserver(mainPage);
mlc.getMusicList().addObserver(optionsPage);
mlc.addObserver(musicSelectionPage);
if (mlc.getMusicList().isSearched() && mlc.getCurrentMusicManager() != null) { if (mlc.getMusicList().isSearched() && mlc.getCurrentMusicManager() != null) {
MusicManager mManager = mlc.getCurrentMusicManager(); MusicManager mManager = mlc.getCurrentMusicManager();
mainPage.updateVisualsForDifferentSong(mManager); mainPage.updateVisualsForDifferentSong(mManager);
@ -160,7 +160,7 @@ public class MainMenu extends ScreenAdapter implements AdvancedResizeScreen {
// Begin drawing a normal version of screen // Begin drawing a normal version of screen
normalBuffer.begin(); normalBuffer.begin();
stage.getViewport().apply(); stage.getViewport().apply();
Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 0f); Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
draw(); draw();
normalBuffer.end(); normalBuffer.end();
@ -170,7 +170,7 @@ public class MainMenu extends ScreenAdapter implements AdvancedResizeScreen {
// Begin light filtering // Begin light filtering
lightFilterBuffer.begin(); lightFilterBuffer.begin();
Gdx.gl.glClearColor(0f, 0f, 0f, 0f); Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
fboRegion.setTexture(normalBuffer.getColorBufferTexture()); fboRegion.setTexture(normalBuffer.getColorBufferTexture());
screenBatch.setShader(brightFilterShader); screenBatch.setShader(brightFilterShader);
@ -237,6 +237,9 @@ public class MainMenu extends ScreenAdapter implements AdvancedResizeScreen {
@Override @Override
public void preAssetLoad() { public void preAssetLoad() {
mlc.getMusicList().deleteObservers();
mlc.deleteObservers();
stage.clear(); stage.clear();
mainPage.dispose(); mainPage.dispose();
screenBatch.dispose(); screenBatch.dispose();