Browse Source

Added network joystick handling

Kenneth van Ewijk 10 years ago
parent
commit
ce35aaf667
2 changed files with 72 additions and 6 deletions
  1. 71 4
      control/NetworkHandler.java
  2. 1 2
      control/joystick/JoystickHandler.java

+ 71 - 4
control/NetworkHandler.java

@@ -8,6 +8,7 @@ import java.net.SocketException;
 import java.net.UnknownHostException;
 
 import control.button.ButtonHandler;
+import control.joystick.Joystick.Position;
 import control.joystick.JoystickHandler;
 
 public class NetworkHandler implements Runnable{
@@ -89,19 +90,85 @@ public class NetworkHandler implements Runnable{
 			} catch (IOException e) {
 				e.printStackTrace();
 			}                   
-			String sentence = new String( receivePacket.getData());   
+			String sentence = new String( receivePacket.getData());  
+			
+			if(sentence.length() != 21)
+				return;
+			
 			System.out.println("RECEIVED: " + sentence); 
 			
 			String[] controls = sentence.split("\\|");
+			int[] control = new int[controls.length];
+			for(int i=0; i<controls.length; i++)
+				control[i] = Integer.parseInt(controls[i]);
+			
 			for(int i = 0; i < 7; i++){
-				if(Integer.parseInt(controls[i]) != ButtonHandler.getButton(i).pressed)
+				if(control[i] != ButtonHandler.getButton(i).pressed)
 				{
 					System.out.println("PRESS BITCH " + controls[i]);
-					ButtonHandler.getButton(i).pressed = Integer.parseInt(controls[i]);
-					if(Integer.parseInt(controls[i]) == 0)
+					ButtonHandler.getButton(i).pressed = control[i];
+					if(control[i] == 0)
 						bth.buttonPress(ButtonHandler.getButton(i));
 				}
 			}
+			
+			if(control[7] == 0 && control[8] == 0){
+				if(JoystickHandler.j.getPos() != Position.UP_LEFT)
+				{
+					JoystickHandler.j.setPosition(Position.UP_LEFT);
+					jth.onJoystickMoved(JoystickHandler.j);
+				}
+			}
+			else if(control[7] == 0 && control[9] == 0){
+				if(JoystickHandler.j.getPos() != Position.UP_RIGHT)
+				{
+					JoystickHandler.j.setPosition(Position.UP_RIGHT);
+					jth.onJoystickMoved(JoystickHandler.j);
+				}
+			}
+			else if(control[10] == 0 && control[8] == 0){
+				if(JoystickHandler.j.getPos() != Position.DOWN_LEFT)
+				{
+					JoystickHandler.j.setPosition(Position.DOWN_LEFT);
+					jth.onJoystickMoved(JoystickHandler.j);
+				}
+			}
+			else if(control[10] == 0 && control[9] == 0){
+				if(JoystickHandler.j.getPos() != Position.DOWN_RIGHT)
+				{
+					JoystickHandler.j.setPosition(Position.DOWN_RIGHT);
+					jth.onJoystickMoved(JoystickHandler.j);
+				}
+			}
+			
+			else if(control[7] == 0){
+				if(JoystickHandler.j.getPos() != Position.UP)
+				{
+					JoystickHandler.j.setPosition(Position.UP);
+					jth.onJoystickMoved(JoystickHandler.j);
+				}
+			}
+			else if(control[8] == 0){
+				if(JoystickHandler.j.getPos() != Position.LEFT)
+				{
+					JoystickHandler.j.setPosition(Position.LEFT);
+					jth.onJoystickMoved(JoystickHandler.j);
+				}
+			}
+			else if(control[9] == 0){
+				if(JoystickHandler.j.getPos() != Position.RIGHT)
+				{
+					JoystickHandler.j.setPosition(Position.RIGHT);
+					jth.onJoystickMoved(JoystickHandler.j);
+				}
+			}
+			else if(control[10] == 0){
+				if(JoystickHandler.j.getPos() != Position.DOWN)
+				{
+					JoystickHandler.j.setPosition(Position.DOWN);
+					jth.onJoystickMoved(JoystickHandler.j);
+				}
+			}
 		}
 			
 	}

+ 1 - 2
control/joystick/JoystickHandler.java

@@ -23,13 +23,12 @@ public class JoystickHandler implements KeyListener{
 
 	List<JoystickListener> listeners;
 	Set<Integer> keys;
-	Joystick j;
+	public static Joystick j = new Joystick();;
 	
 	public JoystickHandler()
 	{
 		listeners = new ArrayList<JoystickListener>();
 		keys = new HashSet<Integer>();
-		j = new Joystick();
 //		if(Window.ON_RASP)
 //			addGpioListeners();
 	}