/**
   * 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);
      }
    }
  }
Example #3
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));
        }
      }
    }
  }