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.
* Blocking.
*/
public void refresh(boolean notify) {
public void refresh(boolean notifyOnCompletion) {
searched = false;
musicList.clear();
Gdx.app.debug("SongController", "Searching path: " + searchPath);
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);
searched = true;
if (notify) {
if (notifyOnCompletion) {
notifyObservers();
}
}
@ -98,7 +99,7 @@ public class MusicList extends Observable {
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...");
return getAudioData(musicList.get(index));
}

View File

@ -73,20 +73,10 @@ public class MusicListController extends Observable implements OnCompletionListe
public void onCompletion(Music music) {
if (autoPlay) {
if (shuffle) {
if (musicList.getTotal() != 0) {
currentPlaybackID = rand.nextInt(musicList.getTotal());
} else {
currentPlaybackID = 0;
}
shuffle(false);
} else {
currentPlaybackID++;
}
if (currentPlaybackID >= musicList.getTotal()) {
currentPlaybackID = 0;
} else if (currentPlaybackID < 0) {
currentPlaybackID = musicList.getTotal()-1;
}
loadMusic();
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.
*/
public void shuffle(boolean load) {
Gdx.app.debug("MusicListController", "shuffled.");
if (musicList.getTotal() == 0) {
currentPlaybackID = 0;
} else {
@ -137,7 +128,7 @@ public class MusicListController extends Observable implements OnCompletionListe
if (currentPlaybackID >= musicList.getTotal()) {
currentPlaybackID = 0;
}
this.mm = musicList.getMusicInfoFromIndex(currentPlaybackID);
this.mm = musicList.getMusicManagerFromIndex(currentPlaybackID);
mm.setOnCompletionListener(this);
setChanged();

View File

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

View File

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

View File

@ -1,5 +1,8 @@
package zero1hd.rhythmbullet.desktop.graphics.ui.pages;
import java.util.Observable;
import java.util.Observer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
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.desktop.audio.MusicListController;
public class OptionsPage extends Page {
public class OptionsPage extends Page implements Observer {
Table optionsTable;
private ProgressBar musicVolSlider;
private ProgressBar fxVolSlider;
private TextField directoryField;
private MusicListController mlc;
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());
this.mlc = mlc;
//Back button
TextButton backButton = new TextButton("Back", core.getDefaultSkin());
backButton.addListener(new ChangeListener() {
@ -52,7 +59,7 @@ public class OptionsPage extends Page {
@Override
public void changed(ChangeEvent event, Actor actor) {
musicVolPercentage.setText(MathUtils.round(musicVolSlider.getValue()) + "%");
sc.getCurrentMusicManager().setVolume(musicVolSlider.getPercent());
mlc.getCurrentMusicManager().setVolume(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());
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() ) {
@Override
public void act(float delta) {
if (musicSearchTimer > 0) {
musicSearchTimer -= delta;
if (musicSearchTimer <= 0) {
sc.getMusicList().setSearchPath(directoryField.getText());
sc.getMusicList().asynchRefresh();
songCount.setText("Songs: " + sc.getMusicList().getTotal());
mlc.getMusicList().setSearchPath(directoryField.getText());
mlc.getMusicList().asynchRefresh();
}
}
super.act(delta);
@ -162,4 +168,16 @@ public class OptionsPage extends Page {
prefs.putString("music dir", directoryField.getText());
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);
}
});
mlc.getMusicList().deleteObservers();
mlc.deleteObservers();
mlc.addObserver(musicSelectionPage);
mlc.addObserver(mainPage);
mlc.getMusicList().addObserver(mainPage);
mlc.getMusicList().addObserver(optionsPage);
mlc.addObserver(musicSelectionPage);
if (mlc.getMusicList().isSearched() && mlc.getCurrentMusicManager() != null) {
MusicManager mManager = mlc.getCurrentMusicManager();
mainPage.updateVisualsForDifferentSong(mManager);
@ -160,7 +160,7 @@ public class MainMenu extends ScreenAdapter implements AdvancedResizeScreen {
// Begin drawing a normal version of screen
normalBuffer.begin();
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);
draw();
normalBuffer.end();
@ -170,7 +170,7 @@ public class MainMenu extends ScreenAdapter implements AdvancedResizeScreen {
// Begin light filtering
lightFilterBuffer.begin();
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
fboRegion.setTexture(normalBuffer.getColorBufferTexture());
screenBatch.setShader(brightFilterShader);
@ -237,6 +237,9 @@ public class MainMenu extends ScreenAdapter implements AdvancedResizeScreen {
@Override
public void preAssetLoad() {
mlc.getMusicList().deleteObservers();
mlc.deleteObservers();
stage.clear();
mainPage.dispose();
screenBatch.dispose();