public float getDPadPosition() {
   if (xboxController != null) {
     xboxController.poll();
     return dPad.getPollData();
   }
   return 0.0f;
 }
Exemple #2
0
  public static float getDeadZone(Component component) {
    float deadZone = component.getDeadZone();
    if (deadZone < minimumDeadZone) {
      deadZone = minimumDeadZone;
    }

    return deadZone;
  }
  /**
   * 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);
      }
    }
  }
Exemple #5
0
  private void processControllerEvent(Component component, float value) {
    Component.Identifier id = component.getIdentifier();
    if (log.isDebugEnabled()) {
      log.debug(
          String.format(
              "Controller Event on %s(%s): %f", component.getName(), id.getName(), value));
    }

    Integer button = buttonComponents.get(id);
    if (button != null) {
      if (id instanceof Axis) {
        // An Axis has been mapped to a PSP button.
        // E.g. for a foot pedal:
        //        value == 1.f when the pedal is not pressed
        //        value == 0.f when the pedal is halfway pressed
        //        value == -1.f when the pedal is pressed down
        if (!isInDeadZone(component, value)) {
          if (value >= 0.f) {
            // Axis is pressed less than halfway, assume the PSP button is not pressed
            Buttons &= ~button;
          } else {
            // Axis is pressed more than halfway, assume the PSP button is pressed
            Buttons |= button;
          }
        }
      } else {
        if (value == 0.f) {
          Buttons &= ~button;
        } else if (value == 1.f) {
          Buttons |= button;
        } else {
          log.warn(
              String.format(
                  "Unknown Controller Button Event on %s(%s): %f",
                  component.getName(), id.getName(), value));
        }
      }
    } else if (id == analogLXAxis) {
      if (isInDeadZone(component, value)) {
        Lx = analogCenter;
      } else {
        Lx = convertAnalogValue(value);
      }
    } else if (id == analogLYAxis) {
      if (isInDeadZone(component, value)) {
        Ly = analogCenter;
      } else {
        Ly = convertAnalogValue(value);
      }
    } else if (id == analogRXAxis) {
      if (isInDeadZone(component, value)) {
        Rx = analogCenter;
      } else {
        Rx = convertAnalogValue(value);
      }
    } else if (id == analogRYAxis) {
      if (isInDeadZone(component, value)) {
        Ry = analogCenter;
      } else {
        Ry = convertAnalogValue(value);
      }
    } else if (id == digitalXAxis) {
      if (isInDeadZone(component, value)) {
        Buttons &= ~(PSP_CTRL_LEFT | PSP_CTRL_RIGHT);
      } else if (value < 0.f) {
        Buttons |= PSP_CTRL_LEFT;
      } else {
        Buttons |= PSP_CTRL_RIGHT;
      }
    } else if (id == digitalYAxis) {
      if (isInDeadZone(component, value)) {
        Buttons &= ~(PSP_CTRL_DOWN | PSP_CTRL_UP);
      } else if (value < 0.f) {
        Buttons |= PSP_CTRL_UP;
      } else {
        Buttons |= PSP_CTRL_DOWN;
      }
    } else if (id == povArrows) {
      if (value == POV.CENTER) {
        Buttons &= ~(PSP_CTRL_RIGHT | PSP_CTRL_LEFT | PSP_CTRL_DOWN | PSP_CTRL_UP);
      } else if (value == POV.UP) {
        Buttons &= ~(PSP_CTRL_RIGHT | PSP_CTRL_LEFT | PSP_CTRL_DOWN | PSP_CTRL_UP);
        Buttons |= PSP_CTRL_UP;
      } else if (value == POV.RIGHT) {
        Buttons &= ~(PSP_CTRL_RIGHT | PSP_CTRL_LEFT | PSP_CTRL_DOWN | PSP_CTRL_UP);
        Buttons |= PSP_CTRL_RIGHT;
      } else if (value == POV.DOWN) {
        Buttons &= ~(PSP_CTRL_RIGHT | PSP_CTRL_LEFT | PSP_CTRL_DOWN | PSP_CTRL_UP);
        Buttons |= PSP_CTRL_DOWN;
      } else if (value == POV.LEFT) {
        Buttons &= ~(PSP_CTRL_RIGHT | PSP_CTRL_LEFT | PSP_CTRL_DOWN | PSP_CTRL_UP);
        Buttons |= PSP_CTRL_LEFT;
      } else if (value == POV.DOWN_LEFT) {
        Buttons &= ~(PSP_CTRL_RIGHT | PSP_CTRL_LEFT | PSP_CTRL_DOWN | PSP_CTRL_UP);
        Buttons |= PSP_CTRL_DOWN | PSP_CTRL_LEFT;
      } else if (value == POV.DOWN_RIGHT) {
        Buttons &= ~(PSP_CTRL_RIGHT | PSP_CTRL_LEFT | PSP_CTRL_DOWN | PSP_CTRL_UP);
        Buttons |= PSP_CTRL_DOWN | PSP_CTRL_RIGHT;
      } else if (value == POV.UP_LEFT) {
        Buttons &= ~(PSP_CTRL_RIGHT | PSP_CTRL_LEFT | PSP_CTRL_DOWN | PSP_CTRL_UP);
        Buttons |= PSP_CTRL_UP | PSP_CTRL_LEFT;
      } else if (value == POV.UP_RIGHT) {
        Buttons &= ~(PSP_CTRL_RIGHT | PSP_CTRL_LEFT | PSP_CTRL_DOWN | PSP_CTRL_UP);
        Buttons |= PSP_CTRL_UP | PSP_CTRL_RIGHT;
      } else {
        log.warn(
            String.format(
                "Unknown Controller Arrows Event on %s(%s): %f",
                component.getName(), id.getName(), value));
      }
    } else {
      // Unknown Axis components are allowed to move inside their dead zone
      // (e.g. due to small vibrations)
      if (id instanceof Axis && (isInDeadZone(component, value) || id == Axis.Z || id == Axis.RZ)) {
        if (log.isDebugEnabled()) {
          log.debug(
              String.format(
                  "Unknown Controller Event in DeadZone on %s(%s): %f",
                  component.getName(), id.getName(), value));
        }
      } else if (isKeyboardController(inputController)) {
        if (log.isDebugEnabled()) {
          log.debug(
              String.format(
                  "Unknown Keyboard Controller Event on %s(%s): %f",
                  component.getName(), id.getName(), value));
        }
      } else {
        if (log.isInfoEnabled()) {
          log.warn(
              String.format(
                  "Unknown Controller Event on %s(%s): %f",
                  component.getName(), id.getName(), value));
        }
      }
    }
  }
Exemple #6
0
  private void onInputControllerChanged() {
    buttonComponents = new HashMap<Component.Identifier, Integer>();
    for (Map.Entry<keyCode, String> entry : controllerComponents.entrySet()) {
      keyCode key = entry.getKey();
      String controllerName = entry.getValue();
      Component component = getControllerComponentByName(controllerName);
      if (component != null) {
        Identifier identifier = component.getIdentifier();
        boolean isAxis = identifier instanceof Axis;

        if (isAxis && identifier == Axis.POV) {
          povArrows = identifier;
        } else {
          int keyCode = -1;
          switch (key) {
              //
              // PSP directional buttons can be mapped
              // to a controller Axis or to a controller Button
              //
            case DOWN:
              if (isAxis) {
                digitalYAxis = identifier;
              } else {
                keyCode = PSP_CTRL_DOWN;
              }
              break;
            case UP:
              if (isAxis) {
                digitalYAxis = identifier;
              } else {
                keyCode = PSP_CTRL_UP;
              }
              break;
            case LEFT:
              if (isAxis) {
                digitalXAxis = identifier;
              } else {
                keyCode = PSP_CTRL_LEFT;
              }
              break;
            case RIGHT:
              if (isAxis) {
                digitalXAxis = identifier;
              } else {
                keyCode = PSP_CTRL_RIGHT;
              }
              break;
              //
              // PSP analog controller can only be mapped to a controller Axis
              //
            case LANDOWN:
            case LANUP:
              if (isAxis) {
                analogLYAxis = identifier;
              }
              break;
            case LANLEFT:
            case LANRIGHT:
              if (isAxis) {
                analogLXAxis = identifier;
              }
              break;
            case RANDOWN:
            case RANUP:
              if (isAxis) {
                analogRYAxis = identifier;
              }
              break;
            case RANLEFT:
            case RANRIGHT:
              if (isAxis) {
                analogRXAxis = identifier;
              }
              break;
              //
              // PSP buttons can be mapped either to a controller Button
              // or to a controller Axis (e.g. a foot pedal)
              //
            case TRIANGLE:
              keyCode = PSP_CTRL_TRIANGLE;
              break;
            case SQUARE:
              keyCode = PSP_CTRL_SQUARE;
              break;
            case CIRCLE:
              keyCode = PSP_CTRL_CIRCLE;
              break;
            case CROSS:
              keyCode = PSP_CTRL_CROSS;
              break;
            case L1:
              keyCode = PSP_CTRL_LTRIGGER;
              break;
            case R1:
              keyCode = PSP_CTRL_RTRIGGER;
              break;
            case START:
              keyCode = PSP_CTRL_START;
              break;
            case SELECT:
              keyCode = PSP_CTRL_SELECT;
              break;
            case HOME:
              keyCode = PSP_CTRL_HOME;
              break;
            case HOLD:
              keyCode = PSP_CTRL_HOLD;
              break;
            case VOLMIN:
              keyCode = PSP_CTRL_VOLDOWN;
              break;
            case VOLPLUS:
              keyCode = PSP_CTRL_VOLUP;
              break;
            case SCREEN:
              keyCode = PSP_CTRL_SCREEN;
              break;
            case MUSIC:
              keyCode = PSP_CTRL_NOTE;
              break;
            case RELEASED:
              break;
          }
          if (keyCode != -1) {
            buttonComponents.put(component.getIdentifier(), keyCode);
          }
        }
      }
    }
  }