Example #1
0
 public PlayerInput(Game game) {
   this.game = game;
   this.gameApp = game.getGameApplication();
   inputManager = gameApp.getInputManager();
   inputManager.addRawInputListener(this);
   mouseStatus = new MouseStatus(null, 0, 0);
 }
Example #2
0
  /**
   * @param panel
   * @param id
   * @param pictureFile
   * @param width
   * @param height
   */
  public TextField(
      Panel panel, String id, String pictureFile, float width, float height, boolean lockScale) {
    super(panel.getWindow(), panel, pictureFile, width, height, lockScale);
    this.id = id;
    setName(id);

    // Init the text
    float xP = -getWidth() * 0.5f;
    float yP = getHeight() * 0.5f;
    float recWidth = getWidth();
    float factor = 1f;
    float recHeight = (getHeight() * 0.5f) * factor;
    float padding = 0f;

    bitmapText = window.getBitmapFont().createLabel(id);
    bitmapText.setText(" ");
    bitmapText.setBox(new Rectangle(xP + padding, yP, recWidth + padding, recHeight));
    bitmapText.setSize(fontSize * window.getScaleFactorHeight()); // font size
    bitmapText.setColor(ColorRGBA.DarkGray); // font color
    bitmapText.setAlignment(BitmapFont.Align.Left);
    bitmapText.setVerticalAlignment(BitmapFont.VAlign.Center);
    bitmapText.setLineWrapMode(LineWrapMode.NoWrap);
    widgetNode.attachChild(bitmapText);

    this.inputManager = window.getInputManager();
    this.cam = window.getApplication().getCamera();
    this.results = new CollisionResults();
    this.ray = new Ray(cam.getLocation(), cam.getDirection());

    actionListener =
        new ActionListener() {
          public void onAction(String name, boolean isPressed, float tpf) {
            results.clear();

            if (isVisible() && isEnabled()) {

              if ((TextField.this.id + "MOUSE").equals(name)) {

                // 1. calc direction
                Vector3f origin =
                    new Vector3f(
                        inputManager.getCursorPosition().x, inputManager.getCursorPosition().y, 1f);
                Vector3f direction = new Vector3f(0, 0, -1);

                // 2. Aim the ray from cam loc to cam direction.
                ray.setOrigin(origin);
                ray.setDirection(direction);

                // 3. Collect intersections between Ray and Shootables in results list.
                window.getWindowNode().collideWith(ray, results);

                // 5. Use the results (we mark the hit object)
                if (results.size() > 0) {

                  for (int i = 0; i < results.size(); i++) {
                    CollisionResult cr = results.getCollision(i);
                    //                                System.out.println("\t-> Hit: " +
                    // cr.getGeometry().getParent().getName());

                    if (widgetNode.hasChild(cr.getGeometry())) {
                      //                                    System.out.println("\t\t\tCollision -> "
                      // + TouchButton.this.id);
                      if (isPressed) {
                        wasDown = true;
                        getWindow().removeFocusFromFields();
                        focus = true;
                        fireFocusListener(TextField.this.id);
                      }
                    }
                  }
                }
              }
            }
          }
        };

    inputManager.addRawInputListener(
        new RawInputListener() {

          public void beginInput() {}

          public void endInput() {}

          public void onJoyAxisEvent(JoyAxisEvent evt) {}

          public void onJoyButtonEvent(JoyButtonEvent evt) {}

          public void onMouseMotionEvent(MouseMotionEvent evt) {}

          public void onMouseButtonEvent(MouseButtonEvent evt) {}

          public void onKeyEvent(KeyInputEvent evt) {
            //                System.out.println("Keyinput ***************** Key = " +
            // evt.getKeyCode());

            if (enabled && focus && evt.isReleased()) {
              String keyChar = keyNames.getName(evt.getKeyCode());
              //                    System.out.println("Keyinput ***************** Char = " +
              // keyChar);

              if (evt.getKeyCode() == 14) {
                if (getText().length() > 0) {
                  setText(getText().substring(0, getText().length() - 1));
                }
              } else if (evt.getKeyCode() == 15) {
                focus = false;

              } else if (keyChar != null && evt.getKeyCode() == 57) {
                setText(getText() + " ");

              } else if (keyChar != null && evt.getKeyCode() == 58) {
                caps = !caps;

              } else if (keyChar != null && keyChar.length() == 1) {
                if (!caps) {
                  keyChar = keyChar.toLowerCase();
                }
                setText(getText() + keyChar);
              }

              if (getText().length() > maxLength) {
                setText(getText().substring(0, maxLength));
              }

              fireKeyboardListener(evt);
            }
          }

          public void onTouchEvent(TouchEvent evt) {
            //                System.out.println("Touchinput ***************** Keycode = " +
            // evt.getKeyCode());

            if (enabled && focus && evt.getType().equals(TouchEvent.Type.KEY_DOWN)) {
              String keyChar = touchKeyNames.getName(evt.getKeyCode());
              System.out.println(
                  "\n\n\nTouchinput ***************** KeyCode = " + evt.getKeyCode());

              if (evt.getKeyCode() == 67) { // backspace
                if (getText().length() > 0) {
                  setText(getText().substring(0, getText().length() - 1));
                }

              } else if (keyChar != null && evt.getKeyCode() == 62) { // space
                setText(getText() + " ");

              } else if (keyChar != null && evt.getKeyCode() == 59) { // shift
                caps = !caps;

              } else if (keyChar != null && keyChar.length() == 1) {
                // TODO:
                //                        if (!caps) {
                keyChar = keyChar.toLowerCase();
                //                        }
                setText(getText() + keyChar);
              }

              if (getText().length() > maxLength) {
                setText(getText().substring(0, maxLength));
              }

              //                    if (evt.getKeyCode() == 67) {
              //                        if (getText().length() > 0) {
              //                            setText(getText().substring(0, getText().length()-1));
              //                        }
              //
              //                    } else if (evt.getKeyCode() == 59) {
              //                        if (getText().length() > 0) {
              //                            setText(getText().substring(0, getText().length()-1));
              //                        }
              //
              //                    } else if (keyChar != null && keyChar.length() == 1) {
              //                        setText(getText() + keyChar);
              //                    }
              //
              //                    if (getText().length() > maxLength) {
              //                        setText(getText().substring(0, maxLength));
              //                    }

            }
          }
        });

    // NICKI
    panel.add(this);

    //        bitmapText.setLocalTranslation(bitmapText.getLocalTranslation().x,
    // bitmapText.getLocalTranslation().y, 0.001f);
  }