Package ponkOut.input

Source Code of ponkOut.input.InputManager

/* Copyright 2010, 2012 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut.  If not, see <http://www.gnu.org/licenses/>.
*/

package ponkOut.input;

import java.util.ArrayList;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Controller;
import org.lwjgl.input.Controllers;
import org.lwjgl.input.Mouse;
import org.lwjgl.util.vector.Vector2f;

import ponkOut.Settings;

/**
* handles input related stuff
*/
public class InputManager {
  private static InputManager inputManager = new InputManager();

  private Settings settings;

  /** movement of mouse since last frame */
  private Vector2f mouseD;

  private InputDevice[] device;
  private int controllerCount;

  /**
   * list of controllers, excluding controllers with suspicious number of axes
   * (they are probably not real controllers)
   */
  private ArrayList<Controller> controllers;

  /**
   * private constructor (singleton)
   */
  private InputManager() {
    settings = Settings.getInstance();
    device = new InputDevice[2];

    mouseD = new Vector2f(0.0f, 0.0f);

    // grab mouse
    Mouse.setGrabbed(true);

    // init controllers
    try {
      Controllers.create();
      controllerCount = Controllers.getControllerCount();
    } catch (LWJGLException e) {
      e.printStackTrace();
      controllerCount = 0;
    }

    controllers = new ArrayList<Controller>(controllerCount);

    // add all controllers with reasonable number of axes to list
    for (int i = 0; i < controllerCount; ++i) {
      Controller cur = Controllers.getController(i);
      if (cur.getAxisCount() > 0 && cur.getAxisCount() <= 32)
        controllers.add(cur);
    }

    // adjust controllerCount
    controllerCount = controllers.size();
  }

  public static InputManager getInstance() {
    return inputManager;
  }

  /**
   * updates attributes; must be called every frame
   */
  public void newFrame() {
    mouseD.x = Mouse.getDX();
    mouseD.y = Mouse.getDY();
  }

  public void initInputDevices() {
    for (int player = 0; player < 2; ++player) {
      switch (settings.getInputDeviceType(player)) {
      case KEYBOARD:
        device[player] = new KeyboardInput(player);
        break;

      case MOUSE:
        device[player] = new MouseInput(player, this);
        break;

      case CONTROLLER:
        device[player] = new ControllerInput(player);
        break;
      }
    }
  }

  /**
   * @param player
   *            0 or 1
   * @param elapsedTime
   *            time elapsed since last frame
   * @return desired velocity as 2D vector
   */
  public Vector2f getMovement(int player, float elapsedTime) {
    return device[player].getMovement(elapsedTime);
  }

  public boolean isButtonDown(int player) {
    return device[player].isButtonDown();
  }

  public float getMouseMovementX() {
    return mouseD.x;
  }

  public float getMouseMovementY() {
    return mouseD.y;
  }

  public int getControllerCount() {
    return controllerCount;
  }

  public Controller getController(int index) {
    return controllers.get(index);
  }

  public String getControllerName(int index) {
    return controllers.get(index).getName();
  }

  public ControllerAction getNextControllerEvent(int controllerIndex) {
    Controller controller = controllers.get(controllerIndex);

    while (Controllers.next()) {
      int eventControllerIndex = Controllers.getEventSource().getIndex();

      if (eventControllerIndex == controller.getIndex()) {
        int eventControlIndex = Controllers.getEventControlIndex();
        ControllerAction.Type type;
        int direction = 0;
        if (Controllers.isEventAxis()) {
          type = ControllerAction.Type.AXIS;
          if (controller.getAxisValue(eventControlIndex) > 0.5f)
            direction = 1;
          else if (controller.getAxisValue(eventControlIndex) < -0.5f)
            direction = -1;
          else
            continue;
        } else if (Controllers.isEventButton()) {
          type = ControllerAction.Type.BUTTON;
        } else if (Controllers.isEventPovX()) {
          type = ControllerAction.Type.POV_X;
          if (controller.getPovX() > 0.5f)
            direction = 1;
          else if (controller.getPovX() < -0.5f)
            direction = -1;
          else
            continue;
        } else if (Controllers.isEventPovY()) {
          type = ControllerAction.Type.POV_Y;
          if (controller.getPovY() > 0.5f)
            direction = 1;
          else if (controller.getPovY() < -0.5f)
            direction = -1;
          else
            continue;
        } else
          continue;

        return new ControllerAction(type, eventControlIndex, direction);
      }
    }
    return null;
  }
}
TOP

Related Classes of ponkOut.input.InputManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.