Example #1
0
 public void setDeadZones(float xDeadZone, float yDeadZone) {
   for (int i = 0; i < Controllers.getControllerCount(); i++) {
     Controller c = Controllers.getController(i);
     c.setXAxisDeadZone(xDeadZone);
     c.setYAxisDeadZone(yDeadZone);
   }
 }
Example #2
0
  /**
   * Initialise the controllers system
   *
   * @throws SlickException Indicates a failure to use the hardware
   */
  public void initControllers() throws SlickException {
    if (controllersInited) {
      return;
    }

    controllersInited = true;
    try {
      Controllers.create();
      int count = Controllers.getControllerCount();

      for (int i = 0; i < count; i++) {
        Controller controller = Controllers.getController(i);

        if ((controller.getButtonCount() >= 3) && (controller.getButtonCount() < MAX_BUTTONS)) {
          controllers.add(controller);
        }
      }

      Log.info("Found " + controllers.size() + " controllers");
      for (int i = 0; i < controllers.size(); i++) {
        Log.info(i + " : " + ((Controller) controllers.get(i)).getName());
      }
    } catch (LWJGLException e) {
      if (e.getCause() instanceof ClassNotFoundException) {
        throw new SlickException(
            "Unable to create controller - no jinput found - add jinput.jar to your classpath");
      }
      throw new SlickException("Unable to create controllers");
    } catch (NoClassDefFoundError | UnsatisfiedLinkError e) {
      // forget it, no jinput availble
    }
  }
  public static void resetBindings(int joyIndex) {
    if (joyIndex >= 0 && joyIndex < Controllers.getControllerCount()) {
      currentDisplayedMap = -1;
      setDefaultJoyBindingMap(joyIndex, false);
      for (Map.Entry<String, ControllerBinding> entry : joyBindingsMap.entrySet()) {
        if (!entry.getKey().contains("user."))
          config.saveControllerBinding(
              Controllers.getController(joyIndex).getName(), entry.getValue());
      }
    }

    unpressAll();
  }
  public static boolean setController(int controllerNo) {
    LogHelper.Info("Attempting to use controller " + controllerNo);
    try {
      if (!Controllers.isCreated()) Controllers.create();

      LogHelper.Info("Controllers.getControllerCount == " + Controllers.getControllerCount());

      if (controllerNo < 0 || controllerNo >= Controllers.getControllerCount()) {
        LogHelper.Error(
            "Attempting to set controller index "
                + controllerNo
                + " there are currently "
                + Controllers.getControllerCount()
                + " controllers detected.");
        return false;
      }

      ControllerSettings.setDefaultJoyBindingMap(controllerNo, true);
      joyNo = controllerNo;
      controllerUtils.printDeadZones(Controllers.getController(controllerNo));
      inputEnabled = true;

      applySavedDeadZones(joyNo);

      config.updatePreferedJoy(controllerNo, Controllers.getController(controllerNo).getName());

      Minecraft.getMinecraft().gameSettings.pauseOnLostFocus = false;
      JoypadMouse.AxisReader.centerCrosshairs();
      checkIfBindingsNeedUpdating();
      unpressAll();
      return true;
    } catch (Exception e) {
      LogHelper.Error("Couldn't initialize Controllers: " + e.toString());
      inputEnabled = false;
    }
    return false;
  }
  private int detectControllers() {
    validControllers.clear();
    inValidControllers.clear();

    try {
      if (!Controllers.isCreated()) Controllers.create();

      if (Controllers.getControllerCount() > 0) {
        LogHelper.Info("Found " + Controllers.getControllerCount() + " controller(s) in total.");
        for (int joyIndex = 0; joyIndex < Controllers.getControllerCount(); joyIndex++) {
          Controller thisController = Controllers.getController(joyIndex);

          logControllerInfo(thisController);

          if (controllerUtils.meetsInputRequirements(
              thisController, requiredButtonCount, requiredMinButtonCount, requiredAxisCount)) {
            LogHelper.Info(
                "Controller #"
                    + joyIndex
                    + " ( "
                    + thisController.getName()
                    + ") meets the input requirements");
            addControllerToList(validControllers, thisController.getName(), joyIndex);
          } else {
            LogHelper.Info("This controller does not meet the input requirements");
            addControllerToList(inValidControllers, thisController.getName(), joyIndex);
          }
          LogHelper.Info("---");
        }
      }
    } catch (org.lwjgl.LWJGLException e) {
      System.err.println("Couldn't initialize Controllers: " + e.getMessage());
    }

    LogHelper.Info("Found " + validControllers.size() + " valid controllers!");
    return validControllers.size();
  }
Example #6
0
 public void createControllers() {
   Controllers.destroy();
   try {
     Controllers.create();
     for (int i = 0; i < Controllers.getControllerCount(); i++) {
       Controller c = Controllers.getController(i);
       c.setXAxisDeadZone(DEFAULT_DEAD_ZONE);
       c.setYAxisDeadZone(DEFAULT_DEAD_ZONE);
     }
     buttonWasPressed =
         new boolean[getControllerCount()][Controllers.getController(0).getButtonCount()];
     // c = controller index
     for (int c = 0; c < buttonWasPressed.length; c++) {
       // b = buttonID index
       for (int b = 0; b < buttonWasPressed[0].length; b++) {
         buttonWasPressed[c][b] = false;
       }
     }
   } catch (LWJGLException e) {
     // TODO Auto-generated catch block
     // Sys.alert("Something went wrong!", e.getMessage());
   }
 }
Example #7
0
  public static void main(String[] argv) {

    // Create the environment of controllers
    try {
      Controllers.create();
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(0);
    }

    // Print how many controllers there are
    int count = Controllers.getControllerCount();
    System.out.println(count + " Controllers Found");

    // Print the name of each controller
    for (int i = 0; i < count; i++) {
      Controller controller = Controllers.getController(i);
      System.out.println(controller.getName() + " ");
      System.out.println("Number of Rumblers " + controller.getRumblerCount());
      System.out.println("Index Number " + controller.getIndex());
    }

    // If there are no controllers, exit program
    if (count == 0) {
      System.exit(0);
    }

    TestControllers[] controllerWindows = new TestControllers[count];
    for (int i = 0; i < count; i++) {
      controllerWindows[i] = new TestControllers(i);
    }

    // Start the "Game"
    boolean running = true;
    while (running) {
      // Sleep for 100 milliseonds, no need to poll too fast
      try {
        Thread.sleep(100);
      } catch (Exception e) {
      }
      ;

      // Poll all the controllers
      Controllers.poll();

      /*
       * Controllers have an event listener
       * Whenever a button is pushed it add the event
       * it to the buffer of the controller instance
       * As long as the buffer has something in it
       * we print it out                                 *
       */
      while (Controllers.next()) {
        //	                                System.out.println("Event Fired: ");
        //
        // System.out.println("\t"+Controllers.getEventNanoseconds());
        //
        // System.out.println("\t"+Controllers.getEventSource()+":"+Controllers.getEventControlIndex()+":"+Controllers.isEventButton());
        //
        // System.out.println("\t"+Controllers.isEventXAxis()+":"+Controllers.isEventYAxis());

        // Controller number
        int controllerNumber = Controllers.getEventSource().getIndex();
        // Button number
        int buttonNumber = Controllers.getEventControlIndex();
        /*
         * Pressing a button on an xBox controller slightly alters the analog values
         * This causes multiple unwanted events on button push
         * By checking to see if the event was a button we can strip out the uneeded events
         */
        if (Controllers.isEventButton() == true) {
          handleButtonPush(controllerNumber, buttonNumber);
        }
      }

      for (int i = 0; i < count; i++) {
        controllerWindows[i].updateDetails();
      }
    }
  } // end main
Example #8
0
 public int getControllerCount() {
   return Controllers.getControllerCount();
 }
Example #9
0
 public void pollControllers() {
   for (int i = 0; i < Controllers.getControllerCount(); i++) {
     Controller c = Controllers.getController(i);
     c.poll();
   }
 }