changed to integer state system for notifier in music controller; music

selection page rewritten making better use of framework objects.
Untested (still)
This commit is contained in:
2018-08-03 13:17:01 -05:00
parent 45da676d0d
commit b857ffe4bd
9 changed files with 276 additions and 124 deletions

View File

@@ -18,14 +18,14 @@ import com.badlogic.gdx.files.FileHandle;
*
*/
public class MusicController extends Observable implements OnCompletionListener, Observer {
public enum States {
Loaded, Playing;
public final class States {
public final Integer LOADED = 0, PLAYING = 1;
}
public final States states = new States();
private MusicList musicList;
private MinimalAudioHeader musicHeader;
private Music music;
private int currentPlaybackIndex;
private int currentlyPlayingIndex;
private boolean autoPlay;
private boolean shuffle;
private Random rand;
@@ -48,7 +48,7 @@ public class MusicController extends Observable implements OnCompletionListener,
Gdx.app.debug("MusicListController", "Playing from MLC.");
music.play();
music.setVolume(prefs.getFloat("music vol", 1f));
notifyObservers(States.Playing);
notifyObservers(states.PLAYING);
} else {
Gdx.app.debug("MusicListController", "failed to begin playing. Load the music!!!");
}
@@ -68,15 +68,24 @@ public class MusicController extends Observable implements OnCompletionListener,
* @param index of music to play
*/
public void setMusicByIndex(int index) {
this.currentPlaybackIndex = index;
this.currentlyPlayingIndex = index;
loadMusic();
}
/**
* Loads music using the given file. The given file must be found in the {@link MusicList}.
* This function gets the index of the given file within {@link MusicList} and passes that index to {@link #setMusicByIndex(index)}.
* @param fileHandle to use.
*/
public void setMusicByFileHandle(FileHandle fileHandle) {
setMusicByIndex(musicList.getMusicArray().indexOf(fileHandle, true));
}
/**
* Goes to the next track
*/
public void skip() {
currentPlaybackIndex++;
currentlyPlayingIndex++;
if (shuffle) {
shuffle(false);
}
@@ -87,7 +96,7 @@ public class MusicController extends Observable implements OnCompletionListener,
* Goes to the previous track
*/
public void previous() {
currentPlaybackIndex--;
currentlyPlayingIndex--;
if (shuffle) {
shuffle(false);
}
@@ -100,7 +109,7 @@ public class MusicController extends Observable implements OnCompletionListener,
if (shuffle) {
shuffle(false);
} else {
currentPlaybackIndex++;
currentlyPlayingIndex++;
}
loadMusic();
play();
@@ -114,9 +123,9 @@ public class MusicController extends Observable implements OnCompletionListener,
public void shuffle(boolean load) {
Gdx.app.debug("MusicListController", "shuffled.");
if (musicList.getTotal() == 0) {
currentPlaybackIndex = 0;
currentlyPlayingIndex = 0;
} else {
currentPlaybackIndex = rand.nextInt(musicList.getTotal());
currentlyPlayingIndex = rand.nextInt(musicList.getTotal());
}
if (load) {
loadMusic();
@@ -148,13 +157,13 @@ public class MusicController extends Observable implements OnCompletionListener,
if (music != null) {
music.dispose();
}
if (currentPlaybackIndex < 0) {
currentPlaybackIndex = musicList.getTotal()-1;
if (currentlyPlayingIndex < 0) {
currentlyPlayingIndex = musicList.getTotal()-1;
}
if (currentPlaybackIndex >= musicList.getTotal()) {
currentPlaybackIndex = 0;
if (currentlyPlayingIndex >= musicList.getTotal()) {
currentlyPlayingIndex = 0;
}
this.music = Gdx.audio.newMusic(musicList.getMusicArray().get(currentPlaybackIndex));
this.music = Gdx.audio.newMusic(musicList.getMusicArray().get(currentlyPlayingIndex));
music.setOnCompletionListener(this);
setChanged();
@@ -162,7 +171,7 @@ public class MusicController extends Observable implements OnCompletionListener,
if (autoPlay) {
play();
}
notifyObservers(States.Loaded);
notifyObservers(states.LOADED);
}
public MusicList getMusicList() {
@@ -170,7 +179,7 @@ public class MusicController extends Observable implements OnCompletionListener,
}
public FileHandle getCurrentMusicFileHandle() {
return musicList.getSongFileHandleFromIndex(currentPlaybackIndex);
return musicList.getSongFileHandleFromIndex(currentlyPlayingIndex);
}
public MinimalAudioHeader getCurrentMusicHeader() {
@@ -220,4 +229,8 @@ public class MusicController extends Observable implements OnCompletionListener,
public Music getCurrentMusic() {
return music;
}
public int getCurrentlyPlayingIndex() {
return currentlyPlayingIndex;
}
}

View File

@@ -92,6 +92,11 @@ public class MusicList extends Observable {
searched = true;
}
/**
*
* @return the amount of audio files discovered.
*/
public int getTotal() {
return musicList.size;
}

View File

@@ -51,7 +51,9 @@ public class MusicMetadataController extends Observable implements Disposable, O
}
public int size() {
return metadataArray.size;
synchronized (loadingThread) {
return metadataArray.size;
}
}
public AudioMetadata getMetadata(int index) {

View File

@@ -1,6 +1,8 @@
package zero1hd.rhythmbullet.graphics.ui.components;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
@@ -9,6 +11,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;
import zero1hd.rhythmbullet.audio.metadata.AudioMetadata;
@@ -21,16 +24,17 @@ public class MusicSelectable extends Button {
private float timeSinceOnScreen;
private AudioMetadata metadata;
private Texture defaultAlbumArt;
public MusicSelectable(Skin skin, Texture defaultAlbumArt, AudioMetadata metadata) {
private Array<MusicSelectable> queueList;
public MusicSelectable(Skin skin, Texture defaultAlbumArt, AudioMetadata metadata,
Array<MusicSelectable> queueList) {
super(skin, "music-selectable");
this.metadata = metadata;
this.defaultAlbumArt = defaultAlbumArt;
this.queueList = queueList;
album = new Image(defaultAlbumArt);
add(album).expand().left();
informationTable = new Table();
name = new ShortenedLabel(metadata.getTitle(), skin, "default-font", skin.getColor("default"));
informationTable.add(name).colspan(2).expandX();
@@ -41,34 +45,60 @@ public class MusicSelectable extends Button {
informationTable.add(time).expandX();
add(informationTable).expand().fill();
}
@Override
public void act(float delta) {
actualCoords.x = getX() + getParent().getX();
actualCoords.y = getY() + getParent().getY();
if (actualCoords.y < 0-getHeight() || actualCoords.y > getStage().getHeight() || actualCoords.x < 0-getWidth() || actualCoords.x > getStage().getWidth()) {
if (actualCoords.y < 0 - getHeight() || actualCoords.y > getStage().getHeight()
|| actualCoords.x < 0 - getWidth() || actualCoords.x > getStage().getWidth()) {
offScreenAct(delta);
} else {
onScreenAct(delta);
}
super.act(delta);
}
public void onScreenAct(float delta) {
timeSinceOnScreen = 0;
if (metadata.getAlbumCover() == null) {
metadata.loadAlbumCover();
album.setDrawable(new TextureRegionDrawable(new TextureRegion(metadata.getAlbumCover())));
@Override
public void draw(Batch batch, float parentAlpha) {
synchronized (album) {
super.draw(batch, parentAlpha);
}
}
public void onScreenAct(float delta) {
timeSinceOnScreen = 0;
if (!queueList.contains(this, true)) {
synchronized (queueList) {
queueList.add(this);
notify();
}
}
}
public void offScreenAct(float delta) {
if (metadata.getAlbumCover() != null) {
timeSinceOnScreen += delta;
if (timeSinceOnScreen >= 2) {
album.setDrawable(new TextureRegionDrawable(new TextureRegion(defaultAlbumArt)));
metadata.unloadAlbumCover();
}
}
}
public void loadAlbumCover() {
metadata.loadAlbumCover();
synchronized (album) {
album.setDrawable(new TextureRegionDrawable(new TextureRegion(metadata.getAlbumCover())));
}
}
public AudioMetadata getMetadata() {
return metadata;
}
public FileHandle getFileHandle() {
return metadata.getFileHandle();
}
}

View File

@@ -0,0 +1,60 @@
package zero1hd.rhythmbullet.util;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.scenes.scene2d.ui.ButtonGroup;
import com.badlogic.gdx.utils.Array;
import zero1hd.rhythmbullet.audio.metadata.AudioMetadata;
import zero1hd.rhythmbullet.graphics.ui.components.MusicSelectable;
public class MusicSelectableButtonGroup extends ButtonGroup<MusicSelectable> {
private Array<MusicSelectable> buttons;
public MusicSelectableButtonGroup() {
buttons = getButtons();
}
public void setChecked(AudioMetadata metadata) {
if (metadata == null) throw new IllegalArgumentException("metadata can't be null.");
MusicSelectable button;
for (int i = 0; i < buttons.size; i++) {
button = buttons.get(i);
if (button.getMetadata() == metadata) {
button.setChecked(true);
return;
}
}
}
public void setChecked(FileHandle fileHandle) {
if (fileHandle == null) throw new IllegalArgumentException("fileHandle can't be null.");
MusicSelectable button;
for (int i = 0; i < buttons.size; i++) {
button = buttons.get(i);
if (button.getFileHandle() == fileHandle) {
button.setChecked(true);
return;
}
}
}
public void selectNext() {
int index = getCheckedIndex() + 1;
if (index == buttons.size) {
index = 0;
}
buttons.get(index).setChecked(true);
}
public void selectPrevious() {
int index = getCheckedIndex() - 1;
if (index == -1) {
index = buttons.size -1;
}
buttons.get(index).setChecked(true);
}
public void setChecked(int index) {
buttons.get(index).setChecked(true);
}
}