public float getDPadPosition() { if (xboxController != null) { xboxController.poll(); return dPad.getPollData(); } return 0.0f; }
/** * Check and save current controller state (controller components values). Must be called every * time before using controller state methods (eg. method for x axis value), so that you get * latest controller components values. * * @return True if controller is connected/valid, false otherwise. */ public boolean pollController() { boolean isControllerValid; // Clear previous values of buttons. buttonsValues.clear(); isControllerValid = controller.poll(); if (!isControllerValid) return false; Component[] components = controller.getComponents(); for (int i = 0; i < components.length; i++) { Component component = components[i]; // Add states of the buttons if (component.getName().contains("Button")) if (component.getPollData() == 1.0f) buttonsValues.add(Boolean.TRUE); else buttonsValues.add(Boolean.FALSE); } return isControllerValid; }
public static void getInstance(Controller controller, JoystickInstance toUpdate) { // X axis and Y axis int xAxisPercentage = 0; int yAxisPercentage = 0; HashMap<String, Boolean> buttons = new HashMap<>(); // @TODO convert to int float hatSwitchPosition = 0; HashMap<String, Integer> otherAxis = new HashMap<>(); // Go trough all components of the controller. controller.poll(); Component[] components = controller.getComponents(); for (int i = 0; i < components.length; i++) { Component component = components[i]; Identifier componentIdentifier = component.getIdentifier(); // Buttons // if(component.getName().contains("Button")){ // If the language is not english, this won't // work. if (componentIdentifier .getName() .matches( "^[0-9]*$")) { // If the component identifier name contains only numbers, then this is // a button. // Is button pressed? boolean isItPressed = true; if (component.getPollData() == 0.0f) isItPressed = false; // Button index String buttonIndex; buttonIndex = component.getIdentifier().toString(); // @TODO convert to int toUpdate.setButton(buttonIndex, isItPressed); // We know that this component was button so we can skip to next component. continue; } // Hat switch if (componentIdentifier == Component.Identifier.Axis.POV) { hatSwitchPosition = component.getPollData(); toUpdate.setHatSwitchPosition(hatSwitchPosition); // We know that this component was hat switch so we can skip to next component. continue; } // Axes if (component.isAnalog()) { float axisValue = component.getPollData(); int axisValueInPercentage = getAxisValueInPercentage(axisValue); // X axis if (componentIdentifier == Component.Identifier.Axis.X) { xAxisPercentage = axisValueInPercentage; toUpdate.setX(xAxisPercentage); continue; // Go to next component. } // Y axis if (componentIdentifier == Component.Identifier.Axis.Y) { yAxisPercentage = axisValueInPercentage; toUpdate.setY(yAxisPercentage); continue; // Go to next component. } // Other axis toUpdate.setOtherAxis(component.getName(), axisValueInPercentage); } } }