Subversion Repositories AndroidProjects

Rev

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

Rev Author Line No. Line
32 chris 1
package com.gebauz.pingK;
2
 
3
import java.util.Vector;
4
 
5
import android.view.MotionEvent;
6
 
42 chris 7
/** Multitouch Handling Class.
8
 *  Note that all internal coordinates are converted from Android screen space into virtual screen space
9
 *  (depending on the virtual width/height ratio set).
10
 */
32 chris 11
public class MultitouchInput
12
{
13
        public class Finger
14
        {
15
                private int mID;
42 chris 16
                private boolean mIsNew = true;  // newly pressed
32 chris 17
                public float x = 0;
18
                public float y = 0;
19
 
20
                public Finger(int id, float _x, float _y)
21
                {
22
                        mID = id;
23
                        x = _x;
24
                        y = _y;
25
                }
26
 
42 chris 27
                /** true when touch has been added in the last frame. */
28
                public final boolean isNew()
32 chris 29
                {
42 chris 30
                        return mIsNew;
32 chris 31
                }
32
 
45 chris 33
                public final void update(float deltaTime)
32 chris 34
                {
42 chris 35
                        mIsNew = false;
32 chris 36
                }
37
 
38
                public final void setID(int id)
39
                {
40
                        mID = id;
41
                }
42
 
43
                public final int getID()
44
                {
45
                        return mID;
46
                }
42 chris 47
 
48
                public final boolean isInside(float x1, float y1, float x2, float y2)
49
                {
43 chris 50
                        boolean isInsideX = ((x >= x1) && (x <= x2)) /*|| ((x >= x2) && (x <= x1))*/;
51
                        boolean isInsideY = ((y >= y1) && (y <= y2)) /*|| ((y >= y2) && (y <= y1))*/;
42 chris 52
 
43 chris 53
                        return isInsideX && isInsideY;
42 chris 54
                }
32 chris 55
        }
56
 
57
        public static final int MAX_TOUCH_POINTS = 10;
58
 
59
        private static MultitouchInput mInstance = new MultitouchInput();
60
 
61
        private Vector<Finger> mTouchPoints = new Vector<Finger>();
62
 
42 chris 63
        /** The ratio of virtual width to actual screen width. */
64
        private float mVirtualWidthRatio = 1.0f;
65
 
66
        /** The ratio of virtual width to actual screen width. */
67
        private float mVirtualHeightRatio = 1.0f;
68
 
32 chris 69
        private MultitouchInput()
70
        {
71
        }
72
 
73
        public static MultitouchInput getInstance()
74
        {
75
                return mInstance;
76
        }
77
 
78
        public void clearTouchPoints()
79
        {
80
                mTouchPoints.clear();
81
        }
82
 
83
        public void addTouchPoint(int id, float x, float y)
84
        {              
85
                mTouchPoints.add(new Finger(id, x, y));                        
86
        }
87
 
88
        public void removeTouchPoint(int id)
89
        {
90
                for (int i = 0; i < mTouchPoints.size(); i++)
91
                {
92
                        if (mTouchPoints.get(i).getID() == id)
93
                        {
94
                                mTouchPoints.remove(i);
95
                        }
96
                }
97
        }
98
 
99
        public Finger getTouchPoint(int id)
100
        {
101
                for (int i = 0; i < mTouchPoints.size(); i++)
102
                {
103
                        if (mTouchPoints.get(i).getID() == id)
104
                        {
105
                                return mTouchPoints.get(i);
106
                        }
107
 
108
                }
109
                return null;
110
        }
111
 
42 chris 112
        /** Get a touchpoint inside a rectangle spanned by (x1, y1) to (x2, y2) */
113
        public Finger getTouchPointInside(float x1, float y1, float x2, float y2)
114
        {
115
                for (int i = 0; i < mTouchPoints.size(); i++)
116
                {
117
                        Finger finger = mTouchPoints.get(i);
118
                        if (finger.isInside(x1, y1, x2, y2))
119
                        {
120
                                return finger;
121
                        }
122
                }
123
 
124
                // none found
125
                return null;
126
        }
127
 
32 chris 128
        public void handleInput(MotionEvent event)
129
        {
130
                int action = event.getAction() & MotionEvent.ACTION_MASK;
131
                //int id = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
132
                int index = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
133
                switch (action)
134
                {
135
                case MotionEvent.ACTION_DOWN:
136
                        // start new touch operation
137
 
138
                        clearTouchPoints();
42 chris 139
                        addTouchPoint(event.getPointerId(index), event.getX() * mVirtualWidthRatio, event.getY() * mVirtualWidthRatio);
32 chris 140
 
141
                        break;
142
                case MotionEvent.ACTION_UP:
143
                case MotionEvent.ACTION_CANCEL:
144
                        // stop all touch operations
145
 
146
                        clearTouchPoints();
147
 
148
                        break;
149
                case MotionEvent.ACTION_MOVE:
150
                        // update positions
151
 
152
                        for (int i = 0; i < event.getPointerCount(); i++)
153
                        {
154
                                Finger finger = getTouchPoint(event.getPointerId(i));
42 chris 155
                                finger.x = event.getX(i) * mVirtualWidthRatio;
156
                                finger.y = event.getY(i) * mVirtualHeightRatio;
32 chris 157
                        }
158
 
159
                        break;
160
                case MotionEvent.ACTION_POINTER_DOWN:
161
                        // add new touch point
162
                        addTouchPoint(event.getPointerId(index), event.getX(), event.getY());
163
 
164
                        break;
165
                case MotionEvent.ACTION_POINTER_UP:
166
                        // remove touch point
167
                        removeTouchPoint(event.getPointerId(index));
168
 
169
                        break;                 
170
                }
171
 
172
/*              if ((event.getAction() == MotionEvent.ACTION_UP) ||
173
                        (event.getAction() == MotionEvent.ACTION_CANCEL))
174
                {
175
                        // all up -> clear points
176
                        for (int i = 0; i < MAX_TOUCH_POINTS; i++)
177
                        {
178
                                mFingers[i].setState(false, -1, -1);
179
                                mFingers[i].setID(-1);
180
                        }
181
                }
182
                else
183
                {
184
                        // assign points
185
                        int pointerCount = event.getPointerCount();
186
 
187
                        for (int i = 0; i < pointerCount; i++)
188
                        {
189
                                int id = event.getPointerId(i);
190
 
191
                                int indexForId = findIndexForID(id);
192
 
193
                                {
194
 
195
                                }
196
                        }
197
                }       */     
198
        }
199
 
200
        /** Find the index in our own array that corresponds to a certain pointer ID */
201
/*      private int findIndexForID(int id)
202
        {
203
                for (int i = 0; i < MAX_TOUCH_POINTS; i++)
204
                {
205
                        if (mFingers[i].getID() == id)
206
                                return i;
207
                }
208
                return -1;
209
        }*/
210
 
211
        /** Find the next free index that has no ID yet */     
212
/*      private int getNextFreeIndex()
213
        {
214
                return findIndexForID(-1);
215
        }*/
216
 
45 chris 217
        public void update(float deltaTime)
32 chris 218
        {
43 chris 219
                for (int i = 0; i < mTouchPoints.size(); i++)
220
                {
221
                        Finger finger = mTouchPoints.get(i);
45 chris 222
                        finger.update(deltaTime);
43 chris 223
                }
32 chris 224
        }      
225
 
226
        public void render()
227
        {
228
 
42 chris 229
        }
32 chris 230
 
42 chris 231
        /** Set the ratio (virtual screen size)/(actual screen size) */
232
        public void setVirtualScreenRatio(float widthRatio, float heightRatio)
233
        {
234
                mVirtualWidthRatio = widthRatio;
235
                mVirtualHeightRatio = heightRatio;
236
        }
237
 
32 chris 238
        String getDebugText()
239
        {              
240
                StringBuffer str = new StringBuffer();
241
                str.append("TOUCH STATISTICS:\n");
242
                for (int i = 0; i < mTouchPoints.size(); i++)
243
                {
244
                        Finger finger = mTouchPoints.get(i);
245
                        str.append("[" + finger.getID() + "] - (" + finger.x + ", " + finger.y + ")\n");
246
                }
247
 
248
                return str.toString();
249
        }
250
 
251
}
252
 
253
 
254