I wrote a small class to implement jinput for joysticks into my code, its picking up the joysticks but the poll data never updates. I have run jtest and it works just fine. Can someone point out whats going wrong before this lib makes me what to hurl an input device? ???
import net.java.games.input.Controller;
import net.java.games.input.ControllerListener;
import net.java.games.input.ControllerEvent;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Component;
public class Joystick implements ControllerListener
{
private Controller Joystick_Controller = null;
private Component[] Joystick_Parts;
private float[] Joystick_oldState;
public Joystick()
{
// add this module to controller listeners
ControllerEnvironment.getDefaultEnvironment()
.addControllerListener(this);
// find a joystick
processfindJoystick();
}
public void processfindJoystick()
{
this.Joystick_Controller = null;
// poll a list of present controllers.
Controller[] tempList = ControllerEnvironment.getDefaultEnvironment()
.getControllers();
for(Controller currentController: tempList)
if(checkisJoystick(currentController))
{
setJoystick(currentController);
return;
}
System.out.println("No joystick found.");
}
public void setJoystick(Controller newJoy)
{
this.Joystick_Controller = newJoy;
this.Joystick_Parts = newJoy.getComponents();
this.Joystick_oldState = new float[Joystick_Parts.length];
System.out.println("Active Joystick set to: "
+ this.Joystick_Controller.getName());
this.Joystick_Controller.poll();
for(int currentState = 0; currentState <
this.Joystick_oldState.length; currentState++)
this.Joystick_oldState[currentState] =
this.Joystick_Parts[currentState].getPollData();
}
public boolean checkisJoystick(Controller thisController)
{
if(thisController.getType() == Controller.Type.GAMEPAD ||
thisController.getType() == Controller.Type.STICK)
return true;
return false;
}
public void controllerAdded(ControllerEvent ev)
{
// attach to new joystick if theres not one already
if(Joystick_Controller == null)
if(checkisJoystick(ev.getController()))
this.setJoystick(ev.getController());
}
public void controllerRemoved(ControllerEvent ev)
{
// if we loose the joystick, try to find another one.
if(ev.getController() == Joystick_Controller)
this.processfindJoystick();
}
public void Poll()
{
if(this.Joystick_Controller != null)
if(!this.Joystick_Controller.poll())
this.processfindJoystick();
else
return;
for(int currentState = 0; currentState <
this.Joystick_oldState.length; currentState++)
if(this.Joystick_oldState[currentState] !=
this.Joystick_Controller.getComponents()[currentState].getPollData())
System.out.println(this.Joystick_Parts[currentState]
.getName() + " " +
this.Joystick_Controller.getComponents()[currentState]
.getPollData());
}
}