Răsfoiți Sursa

Implemented all csf handling

Kenneth van Ewijk 10 ani în urmă
părinte
comite
c2674ae382

+ 58 - 0
audio/ButtonInstance.java

@@ -0,0 +1,58 @@
+package audio;
+
+import java.awt.Color;
+
+import control.button.Button;
+import control.button.ButtonHandler;
+
+public class ButtonInstance {
+	
+	private long time;
+	private int buttonID;
+	private Color color;
+	private Button button;
+	
+	public ButtonInstance()
+	{
+		time = 0;
+		buttonID = 0;
+		
+		color = null;
+		
+		button = null;
+	}
+	
+	public Button getButton() {
+		return button;
+	}
+
+	public long getTime() {
+		return time;
+	}
+
+	public void setTime(long time) {
+		this.time = time;
+	}
+
+	public int getButtonID() {
+		return buttonID;
+	}
+
+	public void setButtonID(int buttonID) {
+		if(buttonID < ButtonHandler.getButtons().size() - 1)
+		{
+			this.buttonID = buttonID;
+			this.button = ButtonHandler.getButton(buttonID);
+		}
+	}
+
+	public Color getColor() {
+		return color;
+	}
+
+	public void setColor(Color color) {
+		this.color = color;
+	}
+	
+	
+}

+ 18 - 11
audio/ObjectInstance.java

@@ -1,13 +1,15 @@
 package audio;
 
-import java.awt.Color;
+import control.button.Button;
+import control.button.ButtonHandler;
 
 public class ObjectInstance {
 	
 	private long time;
 	private int direction;
 	
-	private Color color;
+	private int buttonID;
+	private Button button;
 	
 	private boolean hold;
 	private long length;
@@ -17,7 +19,8 @@ public class ObjectInstance {
 		time = 0;
 		direction = 0;
 		
-		color = null;
+		buttonID = 0;
+		button = null;
 		
 		hold = false;
 		length = 0;
@@ -39,12 +42,20 @@ public class ObjectInstance {
 		this.direction = direction;
 	}
 
-	public Color getColor() {
-		return color;
+	public int getButtonID() {
+		return buttonID;
 	}
 
-	public void setColor(Color color) {
-		this.color = color;
+	public void setButtonID(int buttonID) {
+		if(buttonID < ButtonHandler.getButtons().size() - 1)
+		{
+			this.buttonID = buttonID;
+			this.button = ButtonHandler.getButton(buttonID);
+		}
+	}
+
+	public Button getButton() {
+		return button;
 	}
 
 	public boolean isHold() {
@@ -62,8 +73,4 @@ public class ObjectInstance {
 	public void setLength(long length) {
 		this.length = length;
 	}
-	
-	
-	
-	
 }

+ 6 - 6
audio/Song.java

@@ -9,7 +9,7 @@ public class Song {
 	private String title;
 	private String subtitle;
 	private String author;
-	private String creator;
+	private double sample_start;
 	private double BPM;
 	
 	private File audio;
@@ -24,7 +24,7 @@ public class Song {
 		title = "";
 		subtitle = "";
 		author = "";
-		creator = "";
+		sample_start = 0;
 		BPM = 0.0;
 		
 		audio = null;
@@ -64,12 +64,12 @@ public class Song {
 		this.author = author;
 	}
 
-	public String getCreator() {
-		return creator;
+	public double getSampleStart() {
+		return sample_start;
 	}
 
-	public void setCreator(String creator) {
-		this.creator = creator;
+	public void setSampleStart(double sample_start) {
+		this.sample_start = sample_start;
 	}
 
 	public double getBPM() {

+ 10 - 0
audio/SongInstance.java

@@ -8,22 +8,32 @@ public class SongInstance {
 	private String difficulty;
 	
 	private List<ObjectInstance> objects;
+	private List<ButtonInstance> buttons;
 	
 	public SongInstance(String difficulty)
 	{
 		this.difficulty = difficulty;
 		
 		objects = new ArrayList<ObjectInstance>();
+		buttons = new ArrayList<ButtonInstance>();
 	}
 	
 	public void addObjectInstance(ObjectInstance obj)
 	{
 		objects.add(obj);
 	}
+	public void addButtonInstance(ButtonInstance btn)
+	{
+		buttons.add(btn);
+	}
 
 	public List<ObjectInstance> getObjects() {
 		return objects;
 	}
+	
+	public List<ButtonInstance> getButtons() {
+		return buttons;
+	}
 
 	public String getDifficulty() {
 		return difficulty;

+ 54 - 0
audio/io/DirScanner.java

@@ -0,0 +1,54 @@
+package audio.io;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import audio.Song;
+
+public class DirScanner {
+	
+	public static List<Song> scanDirectories(File f)
+	{
+		if(!f.isDirectory())
+			return null;
+		
+		List<Song> songs = new ArrayList<Song>();
+		
+		FileFilter dirs = new FileFilter()
+		{
+			public boolean accept(File e) {
+				if(e.isDirectory())
+					return true;
+				else
+					return false;
+			}
+		};
+		FileFilter csf = new FileFilter()
+		{
+			public boolean accept(File e) {
+				if(e.isFile() && e.getName().endsWith(".csf"))
+					return true;
+				else
+					return false;
+			}
+		};
+		
+		for(File file : f.listFiles(dirs))
+		{
+			for(File file2 : file.listFiles(csf))
+			{
+				try {
+					Song s = JSONReader.readSong(file2);
+					songs.add(s);
+				} catch (IOException e) {
+					e.printStackTrace();
+				}
+			}
+		}
+		
+		return songs;
+	}
+}

+ 32 - 19
audio/io/JSONReader.java

@@ -6,13 +6,14 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.lang.reflect.Field;
 
 import javax.json.Json;
 import javax.json.JsonArray;
 import javax.json.JsonObject;
 import javax.json.JsonReader;
 
+import model.GameModel;
+import audio.ButtonInstance;
 import audio.ObjectInstance;
 import audio.Song;
 import audio.SongInstance;
@@ -31,6 +32,7 @@ public class JSONReader {
 		JsonReader rdr = Json.createReader(is);
 		JsonObject obj = rdr.readObject();
 		rdr.close();
+		is.close();
 		
 		if(!obj.containsKey("meta") || !obj.containsKey("file") || !obj.containsKey("data"))
 			throw new IOException("Corrupt CSF File");
@@ -44,25 +46,25 @@ public class JSONReader {
 		s.setTitle(meta.getString("title"));
 		s.setSubtitle(meta.getString("subtitle"));
 		s.setAuthor(meta.getString("author"));
-		s.setCreator(meta.getString("creator"));
+		s.setSampleStart(meta.getInt("sample_start"));
 		s.setBPM(meta.getInt("BPM"));
 		
 		//Read FILE data
 		JsonObject file = obj.getJsonObject("file");
 		
-		File audio = new File(file.getString("audio"));
+		File audio = new File(f.getParent() + File.separator + file.getString("audio"));
 		if(!audio.exists() || !audio.getName().endsWith(".mp3"))
-			throw new FileNotFoundException("Audio file does not exist");
+			throw new FileNotFoundException("Audio file does not exist: " + audio.getPath());
 		s.setAudio(audio);
 		
-		File background = new File(file.getString("background"));
+		File background = new File(f.getParent() + File.separator + file.getString("background"));
 		if(!background.exists() || !(background.getName().endsWith(".jpg") || background.getName().endsWith(".png")))
-			throw new FileNotFoundException("Background image does not exist");
+			throw new FileNotFoundException("Background image does not exist: " + background.getPath());
 		s.setBackground(background);
 
-		File banner = new File(file.getString("banner"));
+		File banner = new File(f.getParent() + File.separator + file.getString("banner"));
 		if(!banner.exists() || !(banner.getName().endsWith(".jpg") || banner.getName().endsWith(".png")))
-			throw new FileNotFoundException("Banner image does not exist");
+			throw new FileNotFoundException("Banner image does not exist: " + banner.getPath());
 		s.setBanner(banner);
 		
 		s.setFile(f);
@@ -87,24 +89,22 @@ public class JSONReader {
 			si.addObjectInstance( readObjectInstance(object.getJsonObject(i)) );
 		}
 		
+		JsonArray button = obj.getJsonArray("button");
+		for(int i = 0; i < button.size(); i++)
+		{
+			si.addButtonInstance( readButtonInstance(button.getJsonObject(i)) );
+		}
+		
 		return si;
 	}
-	
-	private static ObjectInstance readObjectInstance(JsonObject obj) throws IOException
+
+	private static ObjectInstance readObjectInstance(JsonObject obj)
 	{
 		ObjectInstance oi = new ObjectInstance();
 		
 		oi.setTime(obj.getInt("time"));
 		oi.setDirection(obj.getInt("direction"));
-		
-		Color color;
-		try {
-		    Field field = Color.class.getField(obj.getString("color").toLowerCase());
-		    color = (Color)field.get(null);
-		} catch (Exception e) {
-		    throw new IOException("Cannot read property color from " + obj);
-		}
-		oi.setColor(color);
+		oi.setButtonID(obj.getInt("button"));
 		
 		if(obj.containsKey("hold") && obj.getBoolean("hold"))
 		{
@@ -114,4 +114,17 @@ public class JSONReader {
 		
 		return oi;
 	}
+	
+	private static ButtonInstance readButtonInstance(JsonObject obj)
+	{
+		ButtonInstance bi = new ButtonInstance();
+		
+		bi.setTime(obj.getInt("time"));
+		bi.setButtonID(obj.getInt("button"));
+		
+		Color color = GameModel.colors[obj.getInt("color") % GameModel.colors.length];
+		bi.setColor(color);
+		
+		return bi;
+	}
 }

+ 1 - 0
audio/io/Main.java

@@ -14,6 +14,7 @@ public class Main {
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
+		System.out.println("---");
 		System.out.println(s);
 	}
 

+ 15 - 7
control/button/ButtonHandler.java

@@ -6,6 +6,8 @@ import java.awt.event.KeyListener;
 import java.util.ArrayList;
 import java.util.List;
 
+import main.Window;
+
 import com.pi4j.io.gpio.GpioController;
 import com.pi4j.io.gpio.GpioFactory;
 import com.pi4j.io.gpio.GpioPinDigitalInput;
@@ -14,7 +16,6 @@ import com.pi4j.io.gpio.RaspiPin;
 import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
 import com.pi4j.io.gpio.event.GpioPinListenerDigital;
 
-import main.Window;
 import control.LedHandler;
 
 public class ButtonHandler implements KeyListener{
@@ -37,12 +38,7 @@ public class ButtonHandler implements KeyListener{
 		buttons.add(new Button(4, 3, led));
 		buttons.add(new Button(5, 4, led));
 		buttons.add(new Button(6, 5, led));
-		
-		for(Button b : buttons)
-		{
-			b.setColor(new Color((int)(Math.random()*254+1),(int)(Math.random()*254+1),(int)(Math.random()*254+1)));
-		}
-		System.out.println(Window.ON_RASP);
+	
 		if (Window.ON_RASP)
 			addGpioListeners();
 		
@@ -158,4 +154,16 @@ public class ButtonHandler implements KeyListener{
 	{
 		return buttons;
 	}
+	
+	public static Button getButton(int id)
+	{
+		for(Button b : buttons)
+		{
+			if(b.getButtonID() == id)
+			{
+				return b;
+			}
+		}
+		return null;
+	}
 }

+ 6 - 1
main/Main.java

@@ -1,5 +1,11 @@
 package main;
 
+import java.io.File;
+import java.io.IOException;
+
+import audio.Song;
+import audio.io.JSONReader;
+
 public class Main {
 
 	public static void main(String[] args) {
@@ -12,5 +18,4 @@ public class Main {
 			new Window(true);
 		}		
 	}
-
 }

+ 4 - 0
model/GameModel.java

@@ -15,11 +15,15 @@ public class GameModel implements ActionListener{
 	private Timer update;
 	public static Color[] colors = {Color.MAGENTA,Color.RED,Color.GREEN,Color.YELLOW,Color.CYAN,Color.WHITE};
 	private GameStateManager gsm;
+	private SongHandler sh;
 	
 	public GameModel(GameStateManager gsm)
 	{
 			
 		this.gsm = gsm;
+		
+		sh = new SongHandler();
+		
 		update = new Timer(1000/30, this);
 		update.start();
 		

+ 54 - 0
model/SongHandler.java

@@ -0,0 +1,54 @@
+package model;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import main.Window;
+import audio.Song;
+import audio.io.DirScanner;
+
+public class SongHandler {
+	
+	private List<Song> songs;
+	
+	private Song currentSong;
+	private int currentIndex;
+	
+	private File dir;
+	
+	public SongHandler()
+	{
+		songs = new ArrayList<Song>();
+		
+		currentSong = null;
+		currentIndex = 0;
+		
+		if(Window.ON_RASP)
+			dir = new File(System.getProperty( "user.home" ) + "/ColorStrike/Songs");
+		else
+			dir = new File("Songs");
+		
+		songs = DirScanner.scanDirectories(dir);
+		
+		currentSong = songs.get(currentIndex);
+		
+		for(Song s : songs)
+		{
+			System.out.println(s.getTitle());
+		}
+	}
+	
+	public void next()
+	{
+		currentIndex++;
+		currentIndex%=songs.size();
+		currentSong = songs.get(currentIndex);
+	}
+	public void previous()
+	{
+		currentIndex--;
+		currentIndex%=songs.size();
+		currentSong = songs.get(currentIndex);
+	}
+}