implementing music directory search thread (cont. music selection ui)
This commit is contained in:
		@@ -5,12 +5,10 @@ import com.badlogic.gdx.ScreenAdapter;
 | 
			
		||||
import com.badlogic.gdx.graphics.GL20;
 | 
			
		||||
import com.badlogic.gdx.graphics.Texture;
 | 
			
		||||
import com.badlogic.gdx.math.Vector3;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.InputEvent;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.Stage;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.Image;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.Label;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
 | 
			
		||||
 | 
			
		||||
import zero1hd.polyjet.Polyjet;
 | 
			
		||||
import zero1hd.polyjet.TransitionAdapter;
 | 
			
		||||
@@ -32,7 +30,6 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter {
 | 
			
		||||
	
 | 
			
		||||
	Label lastStatement;
 | 
			
		||||
	
 | 
			
		||||
	MusicSelectionPage musicSelection;
 | 
			
		||||
	private MainMenu mainMenu;
 | 
			
		||||
	
 | 
			
		||||
	public PreGameScreen(final Polyjet core) {
 | 
			
		||||
@@ -52,7 +49,7 @@ public class PreGameScreen extends ScreenAdapter implements TransitionAdapter {
 | 
			
		||||
	public void postTransition() {
 | 
			
		||||
		stage.clear();
 | 
			
		||||
		//draw music selector
 | 
			
		||||
		musicSelection = new MusicSelectionPage(core, mainMenu);
 | 
			
		||||
		MusicSelectionPage musicSelection = new MusicSelectionPage(core, mainMenu);
 | 
			
		||||
		stage.addActor(musicSelection);
 | 
			
		||||
		
 | 
			
		||||
		statusText = new Label(null, core.defaultSkin);
 | 
			
		||||
 
 | 
			
		||||
@@ -7,8 +7,8 @@ import com.badlogic.gdx.Preferences;
 | 
			
		||||
import com.badlogic.gdx.files.FileHandle;
 | 
			
		||||
import com.badlogic.gdx.graphics.Pixmap;
 | 
			
		||||
import com.badlogic.gdx.graphics.Texture;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.Button;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.Image;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
 | 
			
		||||
import com.mpatric.mp3agic.ID3v2;
 | 
			
		||||
import com.mpatric.mp3agic.InvalidDataException;
 | 
			
		||||
@@ -17,7 +17,7 @@ import com.mpatric.mp3agic.UnsupportedTagException;
 | 
			
		||||
 | 
			
		||||
import zero1hd.wavedecoder.WavInfo;
 | 
			
		||||
 | 
			
		||||
public class MusicSelectable extends ImageButton {
 | 
			
		||||
public class MusicSelectable extends Button {
 | 
			
		||||
	Image imageIcon;
 | 
			
		||||
	FileHandle musicFile;
 | 
			
		||||
	boolean invalidMusic;
 | 
			
		||||
@@ -25,9 +25,10 @@ public class MusicSelectable extends ImageButton {
 | 
			
		||||
	String songName;
 | 
			
		||||
	
 | 
			
		||||
	WavInfo wavinfo;
 | 
			
		||||
	private byte[] albumWorkBytes;
 | 
			
		||||
	
 | 
			
		||||
	public MusicSelectable(FileHandle musicFile, Preferences musicData, Skin skin) {
 | 
			
		||||
		super(skin, "info-pane");
 | 
			
		||||
		super(skin, "info-button");
 | 
			
		||||
		
 | 
			
		||||
		this.musicFile = musicFile;
 | 
			
		||||
		
 | 
			
		||||
@@ -38,24 +39,15 @@ public class MusicSelectable extends ImageButton {
 | 
			
		||||
				
 | 
			
		||||
				if (mp3File.hasId3v2Tag()) {
 | 
			
		||||
					ID3v2 id3v2tag = mp3File.getId3v2Tag();
 | 
			
		||||
					byte[] albumWorkBytes = id3v2tag.getAlbumImage();
 | 
			
		||||
					
 | 
			
		||||
					Pixmap albumArt = new Pixmap(albumWorkBytes, 0, albumWorkBytes.length);
 | 
			
		||||
					Texture albumArtTexture = new Texture(albumArt);
 | 
			
		||||
					imageIcon = new Image(albumArtTexture);
 | 
			
		||||
					float scale = 0.25f*Gdx.graphics.getHeight()/imageIcon.getHeight();
 | 
			
		||||
					
 | 
			
		||||
					imageIcon.setScale(scale);
 | 
			
		||||
					
 | 
			
		||||
					add(imageIcon);
 | 
			
		||||
					
 | 
			
		||||
					albumArtTexture.dispose();
 | 
			
		||||
					albumWorkBytes = id3v2tag.getAlbumImage();
 | 
			
		||||
					
 | 
			
		||||
					songName = id3v2tag.getTitle();
 | 
			
		||||
				}
 | 
			
		||||
			} catch (UnsupportedTagException | InvalidDataException | IOException e) {
 | 
			
		||||
				e.printStackTrace();
 | 
			
		||||
			}
 | 
			
		||||
			
 | 
			
		||||
			setSize(0.2f*Gdx.graphics.getWidth(), 0.8f*Gdx.graphics.getHeight());
 | 
			
		||||
		} else {
 | 
			
		||||
			wavinfo = new WavInfo(musicFile.file());
 | 
			
		||||
			durationInSeconds = wavinfo.getDurationInSeconds();
 | 
			
		||||
@@ -68,5 +60,19 @@ public class MusicSelectable extends ImageButton {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void renderAlbumCover() {
 | 
			
		||||
		if (albumWorkBytes != null) {
 | 
			
		||||
			Pixmap albumArt = new Pixmap(albumWorkBytes, 0, albumWorkBytes.length);
 | 
			
		||||
			Texture albumArtTexture = new Texture(albumArt);
 | 
			
		||||
			imageIcon = new Image(albumArtTexture);
 | 
			
		||||
			float scale = 0.25f*getHeight()/imageIcon.getHeight();
 | 
			
		||||
			imageIcon.setScale(scale);
 | 
			
		||||
			
 | 
			
		||||
			add(imageIcon);
 | 
			
		||||
			albumArtTexture.dispose();
 | 
			
		||||
		} else {
 | 
			
		||||
			//TODO load default album cover texture
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,19 +1,36 @@
 | 
			
		||||
package zero1hd.polyjet.ui.pages;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.FilenameFilter;
 | 
			
		||||
 | 
			
		||||
import com.badlogic.gdx.Gdx;
 | 
			
		||||
import com.badlogic.gdx.Preferences;
 | 
			
		||||
import com.badlogic.gdx.files.FileHandle;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.Actor;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.Image;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.Table;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
 | 
			
		||||
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
 | 
			
		||||
 | 
			
		||||
import zero1hd.polyjet.Polyjet;
 | 
			
		||||
import zero1hd.polyjet.screens.MainMenu;
 | 
			
		||||
import zero1hd.polyjet.ui.builders.MusicSelectable;
 | 
			
		||||
 | 
			
		||||
public class MusicSelectionPage extends Page {
 | 
			
		||||
	Image loading;
 | 
			
		||||
	volatile Table musicChoices;
 | 
			
		||||
	Preferences musicFileAnnotation;
 | 
			
		||||
	private Polyjet core;
 | 
			
		||||
	
 | 
			
		||||
	public MusicSelectionPage(final Polyjet core, final MainMenu mainMenu) {
 | 
			
		||||
		super("Select music", core.defaultSkin);
 | 
			
		||||
		this.core = core;
 | 
			
		||||
		
 | 
			
		||||
		musicFileAnnotation = Gdx.app.getPreferences("music_file_annotation");
 | 
			
		||||
		
 | 
			
		||||
		
 | 
			
		||||
		TextButton back = new TextButton("Back", core.defaultSkin);
 | 
			
		||||
		back.setPosition(getWidth()-back.getWidth()-15f, getHeight()-back.getHeight()-15f);
 | 
			
		||||
@@ -30,5 +47,58 @@ public class MusicSelectionPage extends Page {
 | 
			
		||||
		loading.setOrigin(loading.getWidth()/2, loading.getHeight()/2);
 | 
			
		||||
		loading.addAction(Actions.forever(Actions.rotateBy(-360f, 2f)));
 | 
			
		||||
		addActor(loading);
 | 
			
		||||
		
 | 
			
		||||
		musicChoices = new Table();
 | 
			
		||||
		ScrollPane musicChoiceScroller = new ScrollPane(musicChoices);
 | 
			
		||||
		musicChoiceScroller.setSize(getHeight(), getWidth());
 | 
			
		||||
		addActor(musicChoiceScroller);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public void act(float delta) {
 | 
			
		||||
		if (musicChoices.hasChildren()) {
 | 
			
		||||
			loading.remove();
 | 
			
		||||
		}
 | 
			
		||||
		super.act(delta);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void beginMusicSearch() {
 | 
			
		||||
		Thread musicFinder = new Thread(new MusicFinderRunnable(Gdx.files.absolute(core.prefs.getString("music dir")), core.defaultSkin, 0));
 | 
			
		||||
		musicFinder.start();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	class MusicFinderRunnable implements Runnable {
 | 
			
		||||
		private FileHandle[] musicFiles;
 | 
			
		||||
		private int startingID;
 | 
			
		||||
		private Skin skin;
 | 
			
		||||
		private FileHandle musicDir;
 | 
			
		||||
		
 | 
			
		||||
		public MusicFinderRunnable(FileHandle musicDir, Skin skin, int startingID) {
 | 
			
		||||
			super();
 | 
			
		||||
			
 | 
			
		||||
			this.startingID = startingID;
 | 
			
		||||
			this.skin = skin;
 | 
			
		||||
			this.musicDir = musicDir;
 | 
			
		||||
			
 | 
			
		||||
			System.out.println(musicDir.exists());
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		
 | 
			
		||||
		@Override
 | 
			
		||||
		public void run() {
 | 
			
		||||
			
 | 
			
		||||
			musicFiles = musicDir.list(new FilenameFilter() {
 | 
			
		||||
				@Override
 | 
			
		||||
				public boolean accept(File dir, String name) {
 | 
			
		||||
					if (name.endsWith("mp3") || name.endsWith("wav")) {
 | 
			
		||||
						return true;
 | 
			
		||||
					}
 | 
			
		||||
					return false;
 | 
			
		||||
				}
 | 
			
		||||
			});
 | 
			
		||||
			for (int music = startingID; music < musicFiles.length && music < 15; music++) {
 | 
			
		||||
				musicChoices.add(new MusicSelectable(musicFiles[music], musicFileAnnotation, skin));
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user