Esempio n. 1
0
  /** Pauses the polling and sending of input events. */
  public void pause() {
    paused = true;

    // Reset all polling arrays
    clearKeyPressedRecord();
    clearMousePressedRecord();
    clearControlPressedRecord();
  }
Esempio n. 2
0
  /**
   * Poll the state of the input
   *
   * @param width The width of the game view
   * @param height The height of the game view
   */
  public void poll(int width, int height) {
    if (paused) {
      clearControlPressedRecord();
      clearKeyPressedRecord();
      clearMousePressedRecord();

      while (Keyboard.next()) {}
      while (Mouse.next()) {}
      return;
    }

    if (!Display.isActive()) {
      clearControlPressedRecord();
      clearKeyPressedRecord();
      clearMousePressedRecord();
    }

    // add any listeners requested since last time
    for (int i = 0; i < keyListenersToAdd.size(); i++) {
      addKeyListenerImpl((KeyListener) keyListenersToAdd.get(i));
    }
    keyListenersToAdd.clear();
    for (int i = 0; i < mouseListenersToAdd.size(); i++) {
      addMouseListenerImpl((MouseListener) mouseListenersToAdd.get(i));
    }
    mouseListenersToAdd.clear();

    if (doubleClickTimeout != 0) {
      if (System.currentTimeMillis() > doubleClickTimeout) {
        doubleClickTimeout = 0;
      }
    }

    this.height = height;

    Iterator allStarts = allListeners.iterator();
    while (allStarts.hasNext()) {
      ControlledInputReciever listener = (ControlledInputReciever) allStarts.next();
      listener.inputStarted();
    }

    while (Keyboard.next()) {
      if (Keyboard.getEventKeyState()) {
        int eventKey = resolveEventKey(Keyboard.getEventKey(), Keyboard.getEventCharacter());

        keys[eventKey] = Keyboard.getEventCharacter();
        pressed[eventKey] = true;
        nextRepeat[eventKey] = System.currentTimeMillis() + keyRepeatInitial;

        consumed = false;
        for (int i = 0; i < keyListeners.size(); i++) {
          KeyListener listener = (KeyListener) keyListeners.get(i);

          if (listener.isAcceptingInput()) {
            listener.keyPressed(eventKey, Keyboard.getEventCharacter());
            if (consumed) {
              break;
            }
          }
        }
      } else {
        int eventKey = resolveEventKey(Keyboard.getEventKey(), Keyboard.getEventCharacter());
        nextRepeat[eventKey] = 0;

        consumed = false;
        for (int i = 0; i < keyListeners.size(); i++) {
          KeyListener listener = (KeyListener) keyListeners.get(i);
          if (listener.isAcceptingInput()) {
            listener.keyReleased(eventKey, keys[eventKey]);
            if (consumed) {
              break;
            }
          }
        }
      }
    }

    while (Mouse.next()) {
      if (Mouse.getEventButton() >= 0 && Mouse.getEventButton() < mousePressed.length) {
        if (Mouse.getEventButtonState()) {
          consumed = false;
          mousePressed[Mouse.getEventButton()] = true;

          pressedX = (int) (xoffset + (Mouse.getEventX() * scaleX));
          pressedY = (int) (yoffset + ((height - Mouse.getEventY()) * scaleY));

          for (int i = 0; i < mouseListeners.size(); i++) {
            MouseListener listener = (MouseListener) mouseListeners.get(i);
            if (listener.isAcceptingInput()) {
              listener.mousePressed(Mouse.getEventButton(), pressedX, pressedY);
              if (consumed) {
                break;
              }
            }
          }
        } else {
          consumed = false;
          mousePressed[Mouse.getEventButton()] = false;

          int releasedX = (int) (xoffset + (Mouse.getEventX() * scaleX));
          int releasedY = (int) (yoffset + ((height - Mouse.getEventY()) * scaleY));
          if ((pressedX != -1)
              && (pressedY != -1)
              && (Math.abs(pressedX - releasedX) < mouseClickTolerance)
              && (Math.abs(pressedY - releasedY) < mouseClickTolerance)) {
            considerDoubleClick(Mouse.getEventButton(), releasedX, releasedY);
            pressedX = pressedY = -1;
          }

          for (int i = 0; i < mouseListeners.size(); i++) {
            MouseListener listener = (MouseListener) mouseListeners.get(i);
            if (listener.isAcceptingInput()) {
              listener.mouseReleased(Mouse.getEventButton(), releasedX, releasedY);
              if (consumed) {
                break;
              }
            }
          }
        }
      } else {
        if (Mouse.isGrabbed() && displayActive) {
          if ((Mouse.getEventDX() != 0) || (Mouse.getEventDY() != 0)) {
            consumed = false;
            for (int i = 0; i < mouseListeners.size(); i++) {
              MouseListener listener = (MouseListener) mouseListeners.get(i);
              if (listener.isAcceptingInput()) {
                if (anyMouseDown()) {
                  listener.mouseDragged(0, 0, Mouse.getEventDX(), -Mouse.getEventDY());
                } else {
                  listener.mouseMoved(0, 0, Mouse.getEventDX(), -Mouse.getEventDY());
                }

                if (consumed) {
                  break;
                }
              }
            }
          }
        }

        int dwheel = Mouse.getEventDWheel();
        wheel += dwheel;
        if (dwheel != 0) {
          consumed = false;
          for (int i = 0; i < mouseListeners.size(); i++) {
            MouseListener listener = (MouseListener) mouseListeners.get(i);
            if (listener.isAcceptingInput()) {
              listener.mouseWheelMoved(dwheel);
              if (consumed) {
                break;
              }
            }
          }
        }
      }
    }

    if (!displayActive || Mouse.isGrabbed()) {
      lastMouseX = getMouseX();
      lastMouseY = getMouseY();
    } else {
      if ((lastMouseX != getMouseX()) || (lastMouseY != getMouseY())) {
        consumed = false;
        for (int i = 0; i < mouseListeners.size(); i++) {
          MouseListener listener = (MouseListener) mouseListeners.get(i);
          if (listener.isAcceptingInput()) {
            if (anyMouseDown()) {
              listener.mouseDragged(lastMouseX, lastMouseY, getMouseX(), getMouseY());
            } else {
              listener.mouseMoved(lastMouseX, lastMouseY, getMouseX(), getMouseY());
            }
            if (consumed) {
              break;
            }
          }
        }
        lastMouseX = getMouseX();
        lastMouseY = getMouseY();
      }
    }

    if (controllersInited) {
      for (int i = 0; i < getControllerCount(); i++) {
        int count = ((Controller) controllers.get(i)).getButtonCount() + 3;
        count = Math.min(count, 24);
        for (int c = 0; c <= count; c++) {
          try {
            if (controls[i][c] && !isControlDwn(c, i)) {
              controls[i][c] = false;
              fireControlRelease(c, i);
            } else if (!controls[i][c] && isControlDwn(c, i)) {
              controllerPressed[i][c] = true;
              controls[i][c] = true;
              fireControlPress(c, i);
            }
          } catch (ArrayIndexOutOfBoundsException e) {
            break;
          }
        }
      }
    }

    if (keyRepeat) {
      for (int i = 0; i < 1024; i++) {
        if (pressed[i] && (nextRepeat[i] != 0)) {
          if (System.currentTimeMillis() > nextRepeat[i]) {
            nextRepeat[i] = System.currentTimeMillis() + keyRepeatInterval;
            consumed = false;
            for (int j = 0; j < keyListeners.size(); j++) {
              KeyListener listener = (KeyListener) keyListeners.get(j);

              if (listener.isAcceptingInput()) {
                listener.keyPressed(i, keys[i]);
                if (consumed) {
                  break;
                }
              }
            }
          }
        }
      }
    }

    Iterator all = allListeners.iterator();
    while (all.hasNext()) {
      ControlledInputReciever listener = (ControlledInputReciever) all.next();
      listener.inputEnded();
    }

    if (Display.isCreated()) {
      displayActive = Display.isActive();
    }
  }