Merge remote-tracking branch 'arcade-controls/develop' into feature/controls

Conflicts:
	README.md
	control/GameControl.java
	main/Main.java
	main/Window.java
	model/GameModel.java
	view/GameView.java
This commit is contained in:
jancoow
2015-05-24 20:09:58 +02:00
19 changed files with 671 additions and 46 deletions
+12 -31
View File
@@ -1,52 +1,33 @@
package control;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import model.GameModel;
import view.GameView;
import control.button.ButtonEvent;
import control.button.ButtonListener;
import control.joystick.JoystickEvent;
import control.joystick.JoystickListener;
public class GameControl implements MouseListener, KeyListener, WindowFocusListener{
public class GameControl implements JoystickListener, ButtonListener{
GameModel model;
GameView view;
public GameControl(GameModel model, GameView view)
{
this.model = model;
this.view = view;
}
public void keyPressed(KeyEvent e) {}
@Override
public void buttonPressed(ButtonEvent e) {
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}
public void keyTyped(KeyEvent e) {}
@Override
public void buttonReleased(ButtonEvent e) {}
public void mouseClicked(MouseEvent e) {}
@Override
public void onJoystickMoved(JoystickEvent e) {
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void windowGainedFocus(WindowEvent e) {}
public void windowLostFocus(WindowEvent e) {}
}
}
+98
View File
@@ -0,0 +1,98 @@
package control;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class LedHandler {
private BufferedReader inp;
private BufferedWriter out;
private Process p;
public LedHandler(){
try {
p = Runtime.getRuntime().exec("sudo python led.py");
inp = new BufferedReader( new InputStreamReader(p.getInputStream()) );
out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) );
//setLed(15, 100, 100, 100);
//strobo();
}
catch (Exception err) {
err.printStackTrace();
}
}
public void close(){
try {
inp.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
p.destroy();
}
/**
* Set the color of a specific led
* @param Pixel number of the led
* @param Red value
* @param Green value
* @param Blue value
* Remember: Button leds are GRB, normal ledstrip is RGB
*/
public void setLed(int led, int r, int g, int b) {
if(led<0)
return;
try {
System.out.println("Set the led with " + r);
out.write( "1|" + led + "|" + r + "|" + g + "|" + b + "\n" );
out.flush();
}
catch (Exception err) {
err.printStackTrace();
}
}
/**
* Shows the (new) colors
*/
public void show(){
try {
out.write( "0\n" );
out.flush();
}
catch (Exception err) {
err.printStackTrace();
}
}
public void strobo(){
boolean on = true;
while(true){
if(on){
for(int i = 1; i < 66; i++){
setLed(i, 0, 0, 0);
}
}else{
for(int i = 1; i < 66; i++){
setLed(i, 255, 255, 255);
}
}
on = !on;
show();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
+46
View File
@@ -0,0 +1,46 @@
package control.button;
import java.awt.Color;
import main.Window;
import control.LedHandler;
public class Button {
Color color;
int ledID;
int buttonID;
LedHandler led;
public Button(int buttonID, int ledID, LedHandler led)
{
color = new Color(255,255,255);
this.ledID = ledID;
this.buttonID = buttonID;
this.led = led;
setLed();
}
private void setLed()
{
if(Window.ON_RASP)
{
led.setLed(ledID, color.getGreen(), color.getRed(), color.getBlue());
led.show();
}
}
public void setColor(Color newColor)
{
color = newColor;
setLed();
}
public Color getColor(){
return color;
}
public int getButtonID()
{
return buttonID;
}
}
+19
View File
@@ -0,0 +1,19 @@
package control.button;
public class ButtonEvent {
private Button button;
private Long now;
public ButtonEvent(Button button, Long now){
this.button = button;
this.now = now;
}
public Button getButton() {
return button;
}
public Long getNow() {
return now;
}
}
+136
View File
@@ -0,0 +1,136 @@
package control.button;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinState;
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{
List<ButtonListener> listeners;
static List<Button> buttons;
LedHandler led;
public ButtonHandler(LedHandler led)
{
this.led = led;
listeners = new ArrayList<ButtonListener>();
buttons = new ArrayList<Button>();
buttons.add(new Button(0, -1, led));
buttons.add(new Button(1, 2, led));
buttons.add(new Button(2, 1, led));
buttons.add(new Button(3, 0, led));
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();
}
private void addGpioListeners(){
ArrayList<GpioPinDigitalInput> inputpins = new ArrayList<GpioPinDigitalInput>();
final GpioController gpio = GpioFactory.getInstance();
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, "1")); //button 1 to 6 + start button
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_03, "2"));
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_13, "3"));
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_14, "4"));
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, "5"));
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_12, "6"));
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, "0"));
for(GpioPinDigitalInput p:inputpins){
p.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent e) {
if(e.getState() == PinState.HIGH){
buttonRelease(buttons.get(Integer.parseInt(e.getPin().getName())));
System.out.println(e.getPin().getName() + " Released");
}else{
buttonPress(buttons.get(Integer.parseInt(e.getPin().getName())));
System.out.println(e.getPin().getName() + " Pressed");
}
}
});
}
}
public void addButtonListener(ButtonListener toAdd) {
listeners.add(toAdd);
}
public void buttonPress(Button b) {
ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
for (ButtonListener bt : listeners)
bt.buttonPressed(e);
}
public void buttonRelease(Button b) {
ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
for (ButtonListener bt : listeners)
bt.buttonReleased(e);
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_0:
buttonPress(buttons.get(0));
break;
case KeyEvent.VK_1:
buttonPress(buttons.get(1));
break;
case KeyEvent.VK_2:
buttonPress(buttons.get(2));
break;
case KeyEvent.VK_3:
buttonPress(buttons.get(3));
break;
case KeyEvent.VK_4:
buttonPress(buttons.get(4));
break;
case KeyEvent.VK_5:
buttonPress(buttons.get(5));
break;
case KeyEvent.VK_6:
buttonPress(buttons.get(6));
break;
case KeyEvent.VK_ESCAPE:
if(!Window.ON_RASP)
System.exit(0);
break;
}
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
public static List<Button> getButtons()
{
return buttons;
}
}
+6
View File
@@ -0,0 +1,6 @@
package control.button;
public interface ButtonListener {
public void buttonPressed(ButtonEvent e);
public void buttonReleased(ButtonEvent e);
}
+32
View File
@@ -0,0 +1,32 @@
package control.joystick;
public class Joystick {
public enum Position {
UP,
LEFT,
DOWN,
RIGHT,
UP_RIGHT,
DOWN_RIGHT,
UP_LEFT,
DOWN_LEFT,
CENTER
}
private Position pos;
public Joystick()
{
pos = Position.CENTER;
}
public void setPosition(Position pos)
{
this.pos = pos;
}
public Position getPos() {
return pos;
}
}
+19
View File
@@ -0,0 +1,19 @@
package control.joystick;
public class JoystickEvent {
private Joystick joystick;
private Long now;
public JoystickEvent(Joystick joystick, Long now){
this.joystick = joystick;
this.now = now;
}
public Joystick getJoystick() {
return joystick;
}
public Long getNow() {
return now;
}
}
+175
View File
@@ -0,0 +1,175 @@
package control.joystick;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import main.Window;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
import control.joystick.Joystick.Position;
public class JoystickHandler implements KeyListener{
List<JoystickListener> listeners;
Set<Integer> keys;
Joystick j;
public JoystickHandler()
{
listeners = new ArrayList<JoystickListener>();
keys = new HashSet<Integer>();
j = new Joystick();
if(Window.ON_RASP)
addGpioListeners();
}
private void addGpioListeners(){
ArrayList<GpioPinDigitalInput> inputpins = new ArrayList<GpioPinDigitalInput>();
final GpioController gpio = GpioFactory.getInstance();
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_04, "UP")); //button 1 to 6 + start button
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, "LEFT"));
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_11, "RIGHT"));
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_10, "DOWN"));
for(GpioPinDigitalInput p:inputpins){
p.addListener(new GpioPinListenerDigital() {
@Override
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent e) {
if(e.getState() == PinState.HIGH){
keyReleased(new KeyEvent(
new java.awt.Component(){},
KeyEvent.KEY_RELEASED,
System.nanoTime(),
0,
stringToKeyevent(e.getPin().getName()),
KeyEvent.CHAR_UNDEFINED)
);
System.out.println(e.getPin().getName() + " Released");
}else{
keyPressed(new KeyEvent(
new java.awt.Component(){},
KeyEvent.KEY_PRESSED,
System.nanoTime(),
0,
stringToKeyevent(e.getPin().getName()),
KeyEvent.CHAR_UNDEFINED)
);
System.out.println(e.getPin().getName() + " Pressed");
}
}
});
}
}
public void addJoystickListener(JoystickListener toAdd) {
listeners.add(toAdd);
}
public void onJoystickMoved(Joystick j) {
JoystickEvent e = new JoystickEvent(j, System.currentTimeMillis());
for (JoystickListener yst : listeners)
yst.onJoystickMoved(e);
}
private void updateJoystickPosition()
{
if(keys.contains(KeyEvent.VK_UP) && keys.contains(KeyEvent.VK_RIGHT))
{
j.setPosition(Position.UP_RIGHT);
}
else if(keys.contains(KeyEvent.VK_UP) && keys.contains(KeyEvent.VK_LEFT))
{
j.setPosition(Position.UP_LEFT);
}
else if(keys.contains(KeyEvent.VK_DOWN) && keys.contains(KeyEvent.VK_RIGHT))
{
j.setPosition(Position.DOWN_RIGHT);
}
else if(keys.contains(KeyEvent.VK_DOWN) && keys.contains(KeyEvent.VK_LEFT))
{
j.setPosition(Position.DOWN_LEFT);
}
else if(keys.contains(KeyEvent.VK_UP))
{
j.setPosition(Position.UP);
}
else if(keys.contains(KeyEvent.VK_LEFT))
{
j.setPosition(Position.LEFT);
}
else if(keys.contains(KeyEvent.VK_RIGHT))
{
j.setPosition(Position.RIGHT);
}
else if(keys.contains(KeyEvent.VK_DOWN))
{
j.setPosition(Position.DOWN);
}
else
{
j.setPosition(Position.CENTER);
}
}
private int stringToKeyevent(String s){
switch(s){
case "UP":
return KeyEvent.VK_UP;
case "DOWN":
return KeyEvent.VK_DOWN;
case "LEFT":
return KeyEvent.VK_LEFT;
case "RIGHT":
return KeyEvent.VK_RIGHT;
}
return -1;
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT)
{
Set<Integer> keysCopy = new HashSet<Integer>(keys);
keys.add(e.getKeyCode());
updateJoystickPosition();
if(!keys.equals(keysCopy))
{
onJoystickMoved(j);
}
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT)
{
Set<Integer> keysCopy = new HashSet<Integer>(keys);
keys.remove(e.getKeyCode());
updateJoystickPosition();
if(!keys.equals(keysCopy))
{
onJoystickMoved(j);
}
}
}
public void keyTyped(KeyEvent e) {}
}
+5
View File
@@ -0,0 +1,5 @@
package control.joystick;
public interface JoystickListener {
public void onJoystickMoved(JoystickEvent e);
}