Java Code Examples for net.java.games.input.ControllerEnvironment

The following are top voted examples for showing how to use net.java.games.input.ControllerEnvironment. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: halfnes   File: ControllerImpl.java View source code 6 votes vote down vote up
/**
 * This method detects the available joysticks / gamepads on the computer
 * and return them in a list.
 *
 * @return List of available joysticks / gamepads connected to the computer
 */
private static Controller[] getAvailablePadControllers() {
    List<Controller> gameControllers = new ArrayList<Controller>();
    // Get a list of the controllers JInput knows about and can interact
    // with
    Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
    // Check the useable controllers (gamepads or joysticks with at least 2
    // axis and 2 buttons)
    for (Controller controller : controllers) {
        if ((controller.getType() == Controller.Type.GAMEPAD) || (controller.getType() == Controller.Type.STICK)) {
            int nbOfAxis = 0;
            // Get this controllers components (buttons and axis)
            Component[] components = controller.getComponents();
            // Check the availability of X/Y axis and at least 2 buttons
            // (for A and B, because select and start can use the keyboard)
            for (Component component : components) {
                if ((component.getIdentifier() == Component.Identifier.Axis.X)
                        || (component.getIdentifier() == Component.Identifier.Axis.Y)) {
                    nbOfAxis++;
                }
            }
            if ((nbOfAxis >= 2) && (getButtons(controller).length >= 2)) {
                // Valid game controller
                gameControllers.add(controller);
            }
        }
    }
    return gameControllers.toArray(new Controller[0]);
}
 
Example 2
Project: earthsci   File: SpaceMouse.java View source code 6 votes vote down vote up
private Controller getSpaceMouse()
{
	ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
	for (Controller c : ce.getControllers())
	{
		if (c.getName().toLowerCase().contains("spacemouse"))
		{
			return c;
		}
	}
	return null;
}
 
Example 3
Project: BlackbeardsRedemption   File: GameplayState.java View source code 6 votes vote down vote up
@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
	//backgroundTest = new Image("res/levels/lvl1.png");
	//gc.getGraphics().setBackground(new Color(128,128,128));
	statebg = sbg;
	zone = new Zone(this);
	curLevel = Level.getFirstLevel();
	curLevel.loadInto(zone);
	p = zone.getPlayer();
	player = p;
	health = new HealthController("Heart", "BlackHeart", p);
	p.setGameplayState(this);
	
	// Initialize controller shit
	ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment(); 
	Controller[] cs = ce.getControllers(); 
	for (int i = 0; i < cs.length; i++) {
		if (cs[i].getType() == Controller.Type.GAMEPAD) {
			xl = new XboxListener(cs[i], player);
			break;
		}
	}
	
	if (xl != null)
		xl.start();
	
	// testInit(zone);
}
 
Example 4
Project: jpcsp   File: ControlsGUI.java View source code 6 votes vote down vote up
private Controller getSelectedController() {
    if (controllerBox != null) {
        int controllerIndex = controllerBox.getSelectedIndex();
        ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
        Controller[] controllers = ce.getControllers();
        if (controllers != null && controllerIndex >= 0 && controllerIndex < controllers.length) {
            return controllers[controllerIndex];
        }
    }

    return null;
}
 
Example 5
Project: myrobotlab   File: Joystick.java View source code 6 votes vote down vote up
public HashMap<String, Integer> getControllers() {
	log.info(String.format("%s getting controllers", getName()));
	controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
	info(String.format("found %d controllers", controllers.length));
	controllerNames.clear();
	for (int i = 0; i < controllers.length; i++) {
		log.info(String.format("Found input device: %d %s", i, controllers[i].getName()));
		controllerNames.put(String.format("%d - %s", i, controllers[i].getName()), i);
	}
	return controllerNames;
}
 
Example 6
Project: Awesome-Kart   File: ControllerManager.java View source code 6 votes vote down vote up
public GameController addController(ControllerType controllerType) {
	GameController gameController = null;
	if(ControllerEnvironment.getDefaultEnvironment().getControllers().length > gameControllers.size()){
		for(Controller c : ControllerEnvironment.getDefaultEnvironment().getControllers()){
			if(gameControllers.get(c) == null && 
						c.getName().toLowerCase().contains(controllerType.getName())){
					switch(controllerType){
						case XBOX: 		gameController = new XboxController(currentId++); 		break;
						case KEYBOARD: 	gameController = new KeyboardController(currentId++); 	break;
					}
					if(gameController != null){
						gameControllers.put(c, gameController);
						return gameController;
					} else {
						System.out.println("No controllers found!!! Nyeaguh!");
					}
				}	
			}
		}
	return null;
}
 
Example 7
Project: glimpse   File: SpaceNavDevice.java View source code 6 votes vote down vote up
public synchronized static SpaceNavDevice getDevice( ) throws SpaceNavException
{
    JInputDriver.init( );

    if ( instance == null )
    {
        ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment( );
        Controller[] cs = ce.getControllers( );

        Controller c = findSpaceNavigator( cs );
        if ( c != null )
        {
            // not quite right, needs to run "findCompIndices" fist to be
            // sure
            instance = new SpaceNavDevice( c );
            instanceInUse = false;
        }
        else
        {
            throw new SpaceNavException( "Space Navigator not found." );
        }
    }
    else if ( instanceInUse )
    {
        throw new SpaceNavException( "Device already in use." );
    }

    instanceInUse = true;
    return instance;
}
 
Example 8
Project: LD22   File: Engine.java View source code 6 votes vote down vote up
public void prepareControllers () {
	Controller[] tmpControllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
	for(Controller c : tmpControllers) {
		if(c.getType() == Controller.Type.GAMEPAD && c.getName().contains("360")) {
			//360 controller
			controllers.add(c);
		}
		if(c.getType() == Controller.Type.KEYBOARD) {
			controllers.add(c);
		} 
		if(c.getType() == Controller.Type.STICK && c.getName().contains("Logitech RumblePad")) {
			controllers.add(c);
		}
	}
}
 
Example 9
Project: SuperSmashProject   File: InputDetector.java View source code 6 votes vote down vote up
private static void init(){
	ArrayList<Input> inputs = new ArrayList<Input>();
	
	// Add keyboard
	inputs.add(new InputKeyboard());

	// Add Controllers
	Controller[] con = ControllerEnvironment.getDefaultEnvironment().getControllers();
	for(Controller controller : con){
		String controllerName = controller.toString();
		controllerName = controllerName.toLowerCase();
		
		System.out.println(controllerName);
		
		//.indexOf("mouse") != -1
		if(controllerName.contains("mouse")){
			// We don't want the mouse
		}
		// Xbox 360 controller
		else if(controllerName.contains("xbox")){
			inputs.add(new InputXbox360(controller));
		}
		// Super nintendo controller
		else if(controllerName.contains("super")){
			inputs.add(new InputSuperNintendo(controller));
		}
	}
	
	controllers = inputs.toArray(new Input[]{});
}
 
Example 10
Project: common-chicken-runtime-engine   File: JoystickHandler.java View source code 6 votes vote down vote up
/**
 * Get a Joystick that currently has a button held down.
 *
 * @return the detected Joystick, or null if none are found.
 */
public JoystickWrapper getActivelyPressedJoystick() {
    for (Controller ctrl : ControllerEnvironment.getDefaultEnvironment().getControllers()) {
        if (isIgnored(ctrl)) {
            continue;
        }
        ctrl.poll();
        for (Component comp : ctrl.getComponents()) {
            if (comp.getIdentifier() instanceof Component.Identifier.Button) {
                if (comp.getPollData() > 0.5f) {
                    return getOrWrap(ctrl);
                }
            }
        }
    }
    return null;
}
 
Example 11
Project: emul   File: SwtLwjglKeyboardHandler.java View source code 6 votes vote down vote up
/**
 * 
 */
private synchronized void updateControllers() {
	joystick1Handler = joystick2Handler = null;
	
	for (Controller controller : ControllerEnvironment.getDefaultEnvironment().getControllers()) {
		String name = controller.getName();
		System.out.println("... controller: " + name);

		if (controller.getComponent(Identifier.Axis.X) == null || 
				controller.getComponent(Identifier.Axis.Y) == null ||
				controller.getType() == Controller.Type.MOUSE)
			continue;
			
		System.out.println("Using controller: " + name);
		if (joystick1Handler == null) {
			joystick1Handler = new StupidControllerHandler(controller);
		} else if (joystick2Handler == null) {
			joystick2Handler = new StupidControllerHandler(controller);
			break;
		}
	}
	
}
 
Example 12
Project: soarrobot   File: GamepadJI.java View source code 6 votes vote down vote up
public GamepadJI()
{
    // TODO: save/restore old calibration
    ControllerEnvironment ce = ControllerEnvironment
            .getDefaultEnvironment();
    for (Controller c : ce.getControllers())
    {
        if (c.getType() == Controller.Type.GAMEPAD)
        {
            System.out.println("Set controller: " + c);

            this.controller = c;

            break;
        }
    }

    if (controller == null)
        throw new IllegalStateException("No gamepad.");

    schexec.scheduleAtFixedRate(thread, 500, 50, TimeUnit.MILLISECONDS);
}
 
Example 13
Project: Kaliber   File: JInputDevice.java View source code 6 votes vote down vote up
public static ArrayList<JInputDevice> getDevices() {
	if (jInputDevices != null) {
		return jInputDevices;
	}
	jInputDevices = new ArrayList<JInputDevice>();

	ControllerEnvironment ce = ControllerEnvironment
			.getDefaultEnvironment();
	Controller[] cs = ce.getControllers();

	if ((cs == null) || (cs.length == 0))
		return jInputDevices;

	for (Controller c : cs)
		if (!c.getName().equals("Motion Controller"))
			jInputDevices.add(new JInputDevice(c));

	return jInputDevices;
}
 
Example 14
Project: jpcsp   File: ControlsGUI.java View source code 6 votes vote down vote up
public ComboBoxModel makeControllerComboBoxModel() {
    MutableComboBoxModel comboBox = new DefaultComboBoxModel();
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] controllers = ce.getControllers();
    for (Controller c : controllers) {
        comboBox.addElement(c);
    }
    return comboBox;
}