Пример #1
0
 private void pollController() {
   if (inputController != null && inputController.poll()) {
     EventQueue eventQueue = inputController.getEventQueue();
     Event event = new Event();
     while (eventQueue.getNextEvent(event)) {
       Component component = event.getComponent();
       float value = event.getValue();
       processControllerEvent(component, value);
     }
   }
 }
Пример #2
0
  public static Controller getInstance() {
    if (instance == null) {
      // Disable JInput messages sent to stdout...
      java.util.logging.Logger.getLogger("net.java.games.input.DefaultControllerEnvironment")
          .setLevel(Level.WARNING);

      ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
      net.java.games.input.Controller[] controllers = ce.getControllers();
      net.java.games.input.Controller inputController = null;

      // Reuse the controller from the settings
      String controllerName = Settings.getInstance().readString("controller.controllerName");
      // The controllerNameIndex is the index when several controllers have
      // the same name. 0 to use the first controller with the given name,
      // 1, to use the second...
      int controllerNameIndex = Settings.getInstance().readInt("controller.controllerNameIndex", 0);
      if (controllerName != null) {
        for (int i = 0; controllers != null && i < controllers.length; i++) {
          if (controllerName.equals(controllers[i].getName())) {
            inputController = controllers[i];
            if (controllerNameIndex <= 0) {
              break;
            }
            controllerNameIndex--;
          }
        }
      }

      if (inputController == null) {
        // Use the first KEYBOARD controller
        for (int i = 0; controllers != null && i < controllers.length; i++) {
          if (isKeyboardController(controllers[i])) {
            inputController = controllers[i];
            break;
          }
        }
      }

      if (inputController == null) {
        log.info(String.format("No KEYBOARD controller found"));
        for (int i = 0; controllers != null && i < controllers.length; i++) {
          log.info(String.format("    Controller: '%s'", controllers[i].getName()));
        }
      } else {
        log.info(String.format("Using default controller '%s'", inputController.getName()));
      }
      instance = new Controller(inputController);
      instance.init();
    }

    return instance;
  }
Пример #3
0
 public void setInputController(net.java.games.input.Controller inputController) {
   if (inputController != null) {
     log.info(String.format("Using controller '%s'", inputController.getName()));
   }
   this.inputController = inputController;
   onInputControllerChanged();
 }
 public float getDPadPosition() {
   if (xboxController != null) {
     xboxController.poll();
     return dPad.getPollData();
   }
   return 0.0f;
 }
 public float backPressed() {
   if (xboxController != null) {
     xboxController.poll();
     return buttons[BACK_BUTTON].getPollData();
   }
   return 0.0f;
 }
 /**
  * Checks if the controller is connected/valid. It also poll the controller for data, but it
  * doesn't save states of the buttons into buttons array list that is used by getButtonsValues()
  * and getButtonValue(int index) methods.
  *
  * @see joystick.JInputJoystick#pollController()
  * @return True if controller is connected, false otherwise.
  */
 public boolean isControllerConnected() {
   try {
     return controller.poll();
   } catch (Exception e) {
     return false;
   }
 }
  private Controller getXboxController() {
    // Detect if we have an Xbox controller present

    System.out.println("Checking Controller Support....");
    ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    Controller[] cs = ce.getControllers();

    for (Controller c : cs) {
      if (c.getName().contains("XBOX 360")) {
        if (c.getType() == Controller.Type.GAMEPAD) {
          return c;
        }
      }
    }

    return null;
  }
  /**
   * Get a list of controllers connected to the computer
   *
   * @return All controllers linked to the computer
   */
  public static ArrayList<Controller> getControllers() {
    ArrayList<Controller> foundControllers = new ArrayList<>();
    Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();

    for (int i = 0; i < controllers.length; i++) {
      Controller controller = controllers[i];

      if (controller.getType() == Controller.Type.STICK
          || controller.getType() == Controller.Type.GAMEPAD
          || controller.getType() == Controller.Type.WHEEL
          || controller.getType() == Controller.Type.FINGERSTICK) {
        // Add new controller to the list of all controllers.
        foundControllers.add(controller);
      }
    }

    return foundControllers;
  }
  public XboxControllerSupport() {
    System.out.println("JInput version: " + Version.getVersion());

    xboxController = getXboxController();
    if (xboxController == null) {
      System.out.println("No Xbox controller detected, disabling game pad support");
    } else {
      System.out.println("360 Controller Found");

      dPad = xboxController.getComponent(Component.Identifier.Axis.POV);
      buttons = new Component[10];
      buttons[A_BUTTON] = xboxController.getComponent(Component.Identifier.Button._0);
      buttons[B_BUTTON] = xboxController.getComponent(Component.Identifier.Button._1);
      buttons[X_BUTTON] = xboxController.getComponent(Component.Identifier.Button._2);
      buttons[Y_BUTTON] = xboxController.getComponent(Component.Identifier.Button._3);
      buttons[START_BUTTON] = xboxController.getComponent(Component.Identifier.Button._7);
      buttons[BACK_BUTTON] = xboxController.getComponent(Component.Identifier.Button._6);
    }
  }
Пример #10
0
 private void setController() {
   Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
   for (int i = 0; i < controllers.length && controller == null; i++) {
     if (controllers[i].getType() == Controller.Type.STICK
         || controllers[i].getType() == Controller.Type.GAMEPAD) {
       this.controller = controllers[i];
     }
   }
   if (controller == null) {
     throw new NoControllerFoundException();
   } else {
     for (int i = 0; i < ComponentWrapper.componentWrappers.length; i++) {
       ComponentWrapper component = ComponentWrapper.componentWrappers[i];
       if (component.getComponentNumber() < controller.getComponents().length) {
         component.setComponent(controller.getComponents()[component.getComponentNumber()]);
       } else {
         component.setComponent(controller.getComponents()[0]);
         System.err.println("Component number out of range please reconfigure your joystick!");
       }
     }
   }
 }
  /**
   * 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;
  }
Пример #12
0
 public void run() {
   try {
     setChanged();
     Event event = new Event();
     net.java.games.input.EventQueue queue = controller.getEventQueue();
     while (Main.isRunning()) {
       for (int count = 0; count < 5; count++) {
         Thread.sleep(10);
         controller.poll();
         for (ComponentWrapper aComponentsToSave : ComponentWrapper.componentWrappers) {
           if (currentState == State.Recording) {
             if (aComponentsToSave.setDataAverage()) {
               setChanged();
             }
           } else {
             if (aComponentsToSave.setData()) {
               setChanged();
             }
           }
         }
         while (queue.getNextEvent(event)) {
           Component component = event.getComponent();
           ComponentWrapper c = ComponentWrapper.getComponentWrapper(component);
           if (c != null && c.isButton()) {
             c.isPressed();
           }
         }
         notifyObservers(currentState == State.Recording);
       }
       setChanged();
       notifyObservers();
     }
   } catch (InterruptedException e) {
     Main.stopRunning();
     e.printStackTrace();
   }
 }
Пример #13
0
  private Component getControllerComponentByName(String name) {
    Component[] components = inputController.getComponents();
    if (components != null) {
      // First search for the identifier name
      for (int i = 0; i < components.length; i++) {
        if (name.equals(components[i].getIdentifier().getName())) {
          return components[i];
        }
      }

      // Second search for the component name
      for (int i = 0; i < components.length; i++) {
        if (name.equals(components[i].getName())) {
          return components[i];
        }
      }
    }

    return null;
  }
 /**
  * Gets the controller type. Throws exception if controller doesn't exists.
  *
  * @return Type of the controller.
  */
 public Controller.Type getControllerType() {
   return controller.getType();
 }
 /**
  * Gets position of the Hat Switch. Float number that is returned by this method correspond with
  * positions in the JInput class Component.POV.
  *
  * @return Float number that corresponds with the Hat Switch position.
  */
 public float getHatSwitchPosition() {
   Identifier identifier = Component.Identifier.Axis.POV;
   return controller.getComponent(identifier).getPollData();
 }
 /**
  * Value of axis named Y Rotation.
  *
  * @return Y Rotation value.
  */
 public float getYRotationValue() {
   Identifier identifier = Component.Identifier.Axis.RY;
   return controller.getComponent(identifier).getPollData();
 }
 /**
  * Value of axis named Z Axis.
  *
  * @return Z Axis value.
  */
 public float getZAxisValue() {
   Identifier identifier = Component.Identifier.Axis.Z;
   return controller.getComponent(identifier).getPollData();
 }
 /**
  * Gets value of component with given identifier.
  *
  * @param identifier Identifier that correspond to component from which we need value.
  * @return Component value.
  */
 public float getComponentValue(Identifier identifier) {
   return controller.getComponent(identifier).getPollData();
 }
  /**
   * Checks if component with given identifier exists.
   *
   * @param identifier Identifier that correspond to component.
   * @return True if component exists or false if not exists.
   */
  public boolean componentExists(Identifier identifier) {
    Component component = controller.getComponent(identifier);

    if (component != null) return true;
    else return false;
  }
Пример #20
0
 remoteController(SimpleWrite2 writer, Controller ctr, int startbyte) {
   this.controller = ctr;
   this.outwrite = writer;
   this.mybyte = startbyte;
   System.out.println("Hello " + ctr.getName());
 }
 /**
  * Gets the human readable controller name. Throws exception if controller doesn't exists.
  *
  * @return Controller name.
  */
 public String getControllerName() {
   return controller.getName();
 }
Пример #22
0
  public void run() {
    while (true) {
      controller.poll();

      cps = controller.getComponents();

      // Green triangle = 33
      // Red Circle = 34
      // Purple X = 35
      // Square  = 36

      // Forward = 4
      // Right = 5
      // Back = 6
      // Left = 7

      // Start = 3

      if (cps[33].getPollData() == 1) // Green Triangle
      {
        // System.out.println("Gree Button Pressed");
        // System.out.println(mybyte);
        outwrite.writeByte((byte) mybyte);
      }
      if (cps[34].getPollData() == 1) {
        outwrite.writeByte((byte) (mybyte + 1));
        // System.out.println(mybyte);
      }

      if (cps[35].getPollData() == 1) {
        outwrite.writeByte((byte) (mybyte + 2));
        // System.out.println(mybyte);
      }

      if (cps[36].getPollData() == 1) {
        outwrite.writeByte((byte) (mybyte + 3));
        // System.out.println(mybyte);
      }

      if (cps[4].getPollData() == 1) {
        outwrite.writeByte((byte) (mybyte + 4));
        // System.out.println(mybyte);
      }

      if (cps[5].getPollData() == 1) {
        outwrite.writeByte((byte) (mybyte + 5));
        // System.out.println(mybyte);
      }

      if (cps[6].getPollData() == 1) {
        outwrite.writeByte((byte) (mybyte + 6));
        // System.out.println(mybyte);
      }

      if (cps[7].getPollData() == 1) {
        outwrite.writeByte((byte) (mybyte + 7));
        // System.out.println(mybyte);
      }

      if (cps[3].getPollData() == 1) {
        outwrite.writeByte((byte) (mybyte + 8));
        // System.out.println(mybyte);
      }
    }
  }
  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);
      }
    }
  }
Пример #24
0
 public static boolean isKeyboardController(net.java.games.input.Controller inputController) {
   return inputController == null || inputController.getType() == Type.KEYBOARD;
 }