Subversion Repositories AndroidProjects

Rev

Rev 43 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.pingK;

import java.util.Vector;

import android.view.MotionEvent;

/** Multitouch Handling Class.
 *  Note that all internal coordinates are converted from Android screen space into virtual screen space
 *  (depending on the virtual width/height ratio set).
 */

public class MultitouchInput
{
        public class Finger
        {
                private int mID;
                private boolean mIsNew = true;  // newly pressed
                public float x = 0;
                public float y = 0;
               
                public Finger(int id, float _x, float _y)
                {
                        mID = id;
                        x = _x;
                        y = _y;
                }
               
                /** true when touch has been added in the last frame. */
                public final boolean isNew()
                {
                        return mIsNew;
                }
               
                public final void update(float deltaTime)
                {
                        mIsNew = false;
                }
               
                public final void setID(int id)
                {
                        mID = id;
                }
               
                public final int getID()
                {
                        return mID;
                }
               
                public final boolean isInside(float x1, float y1, float x2, float y2)
                {
                        boolean isInsideX = ((x >= x1) && (x <= x2)) /*|| ((x >= x2) && (x <= x1))*/;
                        boolean isInsideY = ((y >= y1) && (y <= y2)) /*|| ((y >= y2) && (y <= y1))*/;
                       
                        return isInsideX && isInsideY;
                }
        }
       
        public static final int MAX_TOUCH_POINTS = 10;
       
        private static MultitouchInput mInstance = new MultitouchInput();
       
        private Vector<Finger> mTouchPoints = new Vector<Finger>();
       
        /** The ratio of virtual width to actual screen width. */
        private float mVirtualWidthRatio = 1.0f;
       
        /** The ratio of virtual width to actual screen width. */
        private float mVirtualHeightRatio = 1.0f;
       
        private MultitouchInput()
        {
        }
       
        public static MultitouchInput getInstance()
        {
                return mInstance;
        }
       
        public void clearTouchPoints()
        {
                mTouchPoints.clear();
        }
       
        public void addTouchPoint(int id, float x, float y)
        {              
                mTouchPoints.add(new Finger(id, x, y));                        
        }
       
        public void removeTouchPoint(int id)
        {
                for (int i = 0; i < mTouchPoints.size(); i++)
                {
                        if (mTouchPoints.get(i).getID() == id)
                        {
                                mTouchPoints.remove(i);
                        }
                }
        }
       
        public Finger getTouchPoint(int id)
        {
                for (int i = 0; i < mTouchPoints.size(); i++)
                {
                        if (mTouchPoints.get(i).getID() == id)
                        {
                                return mTouchPoints.get(i);
                        }
       
                }
                return null;
        }
       
        /** Get a touchpoint inside a rectangle spanned by (x1, y1) to (x2, y2) */
        public Finger getTouchPointInside(float x1, float y1, float x2, float y2)
        {
                for (int i = 0; i < mTouchPoints.size(); i++)
                {
                        Finger finger = mTouchPoints.get(i);
                        if (finger.isInside(x1, y1, x2, y2))
                        {
                                return finger;
                        }
                }
               
                // none found
                return null;
        }
       
        public void handleInput(MotionEvent event)
        {
                int action = event.getAction() & MotionEvent.ACTION_MASK;
                //int id = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                int index = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
                switch (action)
                {
                case MotionEvent.ACTION_DOWN:
                        // start new touch operation

                        clearTouchPoints();
                        addTouchPoint(event.getPointerId(index), event.getX() * mVirtualWidthRatio, event.getY() * mVirtualWidthRatio);
                       
                        break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                        // stop all touch operations
                       
                        clearTouchPoints();
                       
                        break;
                case MotionEvent.ACTION_MOVE:
                        // update positions
                       
                        for (int i = 0; i < event.getPointerCount(); i++)
                        {
                                Finger finger = getTouchPoint(event.getPointerId(i));
                                finger.x = event.getX(i) * mVirtualWidthRatio;
                                finger.y = event.getY(i) * mVirtualHeightRatio;
                        }

                        break;
                case MotionEvent.ACTION_POINTER_DOWN:
                        // add new touch point
                        addTouchPoint(event.getPointerId(index), event.getX(), event.getY());
                       
                        break;
                case MotionEvent.ACTION_POINTER_UP:
                        // remove touch point
                        removeTouchPoint(event.getPointerId(index));
                       
                        break;                 
                }
               
/*              if ((event.getAction() == MotionEvent.ACTION_UP) ||
                        (event.getAction() == MotionEvent.ACTION_CANCEL))
                {
                        // all up -> clear points
                        for (int i = 0; i < MAX_TOUCH_POINTS; i++)
                        {
                                mFingers[i].setState(false, -1, -1);
                                mFingers[i].setID(-1);
                        }
                }
                else
                {
                        // assign points
                        int pointerCount = event.getPointerCount();
                       
                        for (int i = 0; i < pointerCount; i++)
                        {
                                int id = event.getPointerId(i);
                               
                                int indexForId = findIndexForID(id);
                                                               
                                {
                                       
                                }
                        }
                }       */
     
        }
       
        /** Find the index in our own array that corresponds to a certain pointer ID */
/*      private int findIndexForID(int id)
        {
                for (int i = 0; i < MAX_TOUCH_POINTS; i++)
                {
                        if (mFingers[i].getID() == id)
                                return i;
                }
                return -1;
        }*/

       
        /** Find the next free index that has no ID yet */     
/*      private int getNextFreeIndex()
        {
                return findIndexForID(-1);
        }*/

       
        public void update(float deltaTime)
        {
                for (int i = 0; i < mTouchPoints.size(); i++)
                {
                        Finger finger = mTouchPoints.get(i);
                        finger.update(deltaTime);
                }
        }      
       
        public void render()
        {
               
        }
       
        /** Set the ratio (virtual screen size)/(actual screen size) */
        public void setVirtualScreenRatio(float widthRatio, float heightRatio)
        {
                mVirtualWidthRatio = widthRatio;
                mVirtualHeightRatio = heightRatio;
        }
       
        String getDebugText()
        {              
                StringBuffer str = new StringBuffer();
                str.append("TOUCH STATISTICS:\n");
                for (int i = 0; i < mTouchPoints.size(); i++)
                {
                        Finger finger = mTouchPoints.get(i);
                        str.append("[" + finger.getID() + "] - (" + finger.x + ", " + finger.y + ")\n");
                }
               
                return str.toString();
        }

}