Android Open Source - jmini3d Input Controller






From Project

Back to project page jmini3d.

License

The source code is released under:

Copyright 2012 Mobialia http://www.mobialia.com/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to ...

If you think the Android project jmini3d listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jmini3d.android.input;
//www.j av a 2 s  . c  o  m
import android.os.Build;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import java.util.HashMap;

import jmini3d.android.compat.CompatibilityWrapper5;
import jmini3d.input.KeyListener;
import jmini3d.input.TouchListener;
import jmini3d.input.TouchPointer;

public class InputController implements OnTouchListener, View.OnKeyListener {

  TouchListener touchListener;
  KeyListener keyListener;

  HashMap<Integer, TouchPointer> pointers = new HashMap<Integer, TouchPointer>();
  HashMap<Integer, TouchPointer> pointersAux = new HashMap<Integer, TouchPointer>();

  View view;

  public InputController(View view) {
    this.view = view;
  }

  public void setTouchListener(TouchListener touchListener) {
    this.touchListener = touchListener;
    view.setOnTouchListener(this);
  }

  public void setKeyListener(KeyListener keyListener) {
    this.keyListener = keyListener;
    view.setOnKeyListener(this);
  }

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    if (touchListener == null) {
      return false;
    }
    // = event.getActionMasked();
    int action = event.getAction();
    // = event.getActionIndex();
    int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    int pointerId = 0;
    if (Build.VERSION.SDK_INT >= 5) {
      pointerId = CompatibilityWrapper5.getPointerId(event, pointerIndex);
    }

    switch (action & MotionEvent.ACTION_MASK) {
      case MotionEvent.ACTION_DOWN:
      case MotionEvent.ACTION_POINTER_DOWN:
        TouchPointer touchPointer = pointersAux.get(Integer.valueOf(pointerId));
        if (touchPointer == null) {
          touchPointer = new TouchPointer();
        }

        new TouchPointer();
        if (Build.VERSION.SDK_INT >= 5) {
          touchPointer.x = (int) CompatibilityWrapper5.getX(event, pointerIndex);
          touchPointer.y = (int) CompatibilityWrapper5.getY(event, pointerIndex);
        } else {
          touchPointer.x = (int) event.getX();
          touchPointer.y = (int) event.getY();
        }
        touchPointer.status = TouchPointer.TOUCH_DOWN;
        pointers.put(pointerId, touchPointer);
        if (touchListener != null) {
          touchListener.onTouch(pointers);
        }
        touchPointer.status = TouchPointer.TOUCH_MOVE;
        break;
      case MotionEvent.ACTION_MOVE:
        for (int i = 0; i < event.getPointerCount(); i++) {
          if (Build.VERSION.SDK_INT >= 5) {
            int curPointerId = CompatibilityWrapper5.getPointerId(event, i);
            if (pointers.containsKey(Integer.valueOf(curPointerId))) {
              TouchPointer movePointer = pointers.get(Integer.valueOf(curPointerId));
              movePointer.x = (int) CompatibilityWrapper5.getX(event, i);
              movePointer.y = (int) CompatibilityWrapper5.getY(event, i);
              movePointer.status = TouchPointer.TOUCH_MOVE;
            }
          } else {
            TouchPointer movePointer = pointers.get(Integer.valueOf(0));
            movePointer.x = (int) event.getX();
            movePointer.y = (int) event.getY();
            movePointer.status = TouchPointer.TOUCH_MOVE;
          }
        }
        if (touchListener != null) {
          touchListener.onTouch(pointers);
        }
        break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_POINTER_UP:
        TouchPointer upPointer = pointers.get(Integer.valueOf(pointerId));
        upPointer.status = TouchPointer.TOUCH_UP;
        if (touchListener != null) {
          touchListener.onTouch(pointers);
        }
        pointers.remove(Integer.valueOf(pointerId));
        pointersAux.put(pointerId, upPointer);
        break;
      case MotionEvent.ACTION_OUTSIDE:
        break;
    }

    try {
      // Avoid event flood
      Thread.sleep(16);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return true;
  }

  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyListener == null) {
      return false;
    }

    int key;

    switch (keyCode) {
      case KeyEvent.KEYCODE_DPAD_UP:
        key = KeyListener.KEY_UP;
        break;
      case KeyEvent.KEYCODE_DPAD_DOWN:
        key = KeyListener.KEY_DOWN;
        break;
      case KeyEvent.KEYCODE_DPAD_LEFT:
        key = KeyListener.KEY_LEFT;
        break;
      case KeyEvent.KEYCODE_DPAD_RIGHT:
        key = KeyListener.KEY_RIGHT;
        break;
      case KeyEvent.KEYCODE_DPAD_CENTER:
        key = KeyListener.KEY_CENTER;
        break;
      case KeyEvent.KEYCODE_BACK:
        key = KeyListener.KEY_BACK;
        break;
      case KeyEvent.KEYCODE_ENTER:
        key = KeyListener.KEY_ENTER;
        break;
      default:
        return false;
    }

    boolean managed = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
      managed = keyListener.onKeyDown(key);
    }

    if (event.getAction() == KeyEvent.ACTION_UP) {
      managed = keyListener.onKeyUp(key);
    }
    return managed;
  }

}




Java Source Code List

cocoonjs.CocoonJsLinker.java
jmini3d.Blending.java
jmini3d.Camera.java
jmini3d.Color4.java
jmini3d.CubeMapTexture.java
jmini3d.Font.java
jmini3d.GpuObjectStatus.java
jmini3d.MatrixUtils.java
jmini3d.Object3d.java
jmini3d.Rect.java
jmini3d.SceneController.java
jmini3d.Scene.java
jmini3d.Texture.java
jmini3d.Utils.java
jmini3d.Vector3.java
jmini3d.android.Activity3d.java
jmini3d.android.GeometryBuffers.java
jmini3d.android.GlSurfaceView3d.java
jmini3d.android.GpuUploader.java
jmini3d.android.Program.java
jmini3d.android.Renderer3d.java
jmini3d.android.ResourceLoader.java
jmini3d.android.compat.CompatibilityWrapper5.java
jmini3d.android.demo.DemoActivity.java
jmini3d.android.input.InputController.java
jmini3d.demo.ArialFont.java
jmini3d.demo.CubeScene.java
jmini3d.demo.CubesScene.java
jmini3d.demo.DemoSceneController.java
jmini3d.demo.EnvMapCubeScene.java
jmini3d.demo.NormalMapScene.java
jmini3d.demo.ParentScene.java
jmini3d.demo.TeapotGeometry.java
jmini3d.demo.TeapotScene.java
jmini3d.geometry.BoxGeometry.java
jmini3d.geometry.Geometry.java
jmini3d.geometry.PlaneGeometry.java
jmini3d.geometry.SkyboxGeometry.java
jmini3d.geometry.SpriteGeometry.java
jmini3d.geometry.VariableGeometry.java
jmini3d.gwt.Canvas3d.java
jmini3d.gwt.EngineResources.java
jmini3d.gwt.EntryPoint3d.java
jmini3d.gwt.GeometryBuffers.java
jmini3d.gwt.GpuUploader.java
jmini3d.gwt.MyInt16Array.java
jmini3d.gwt.Program.java
jmini3d.gwt.Renderer3d.java
jmini3d.gwt.ResourceLoader.java
jmini3d.gwt.TextureLoadedListener.java
jmini3d.gwt.demo.DemoEntryPoint.java
jmini3d.gwt.input.InputController.java
jmini3d.input.KeyListener.java
jmini3d.input.TouchListener.java
jmini3d.input.TouchPointer.java
jmini3d.light.AmbientLight.java
jmini3d.light.DirectionalLight.java
jmini3d.light.Light.java
jmini3d.light.PointLight.java
jmini3d.material.Material.java
jmini3d.material.PhongMaterial.java
jmini3d.material.SpriteMaterial.java
jmini3d.utils.Fnt2Class.java
jmini3d.utils.Obj2Class.java