1+ // http://www.akeric.com/blog/?p=1435
2+ // multiTouchCore.pde
3+ // Eric Pavey - 2011-01-02
4+ // Code used to get multitouch working in Processing.
5+ // Add to what's in the draw() function to implement the magic.
6+
7+ // It's all about the MotionEvent:
8+ // http://developer.android.com/reference/android/view/MotionEvent.html
9+
10+ // ------------------------------
11+ // Setup globals:
12+
13+ import android.view.MotionEvent ;
14+
15+ // Define max touch events. My phone (seems to) support 5.
16+ int maxTouchEvents = 5 ;
17+ // This array will hold all of our queryable MultiTouch data:
18+ MultiTouch [] mt;
19+
20+ // ------------------------------
21+ void setup () {
22+ fullScreen();
23+ // Populate our MultiTouch array that will track all of our touch-points:
24+ mt = new MultiTouch [maxTouchEvents];
25+ for (int i= 0 ; i < maxTouchEvents; i++ ) {
26+ mt[i] = new MultiTouch ();
27+ }
28+ noFill ();
29+ stroke (255 , 0 , 0 );
30+ strokeWeight (3 );
31+ }
32+
33+ // ------------------------------
34+ void draw () {
35+ background (0 );
36+ // If the screen is touched, start querying for MultiTouch events:
37+ if (mousePressed == true ) {
38+ // ...for each possible touch event...
39+ for (int i= 0 ; i < maxTouchEvents; i++ ) {
40+ // If that event been touched...
41+ if (mt[i]. touched == true ) {
42+ // DO SOMETHING WITH IT HERE:
43+ // Amazing mult-touch graphics implemeneted....
44+ ellipse (mt[i]. motionX, mt[i]. motionY, mt[i]. size, mt[i]. size);
45+ }
46+ }
47+ }
48+ }
49+
50+ // ------------------------------
51+ // Override parent class's surfaceTouchEvent() method to enable multi-touch.
52+ // This is what grabs the Android multitouch data, and feeds our MultiTouch
53+ // classes. Only executes on touch change (movement across screen, or initial
54+ // touch).
55+
56+ public boolean surfaceTouchEvent (MotionEvent me ) {
57+ // Find number of touch points:
58+ int pointers = me. getPointerCount();
59+ // Set all MultiTouch data to "not touched":
60+ for (int i= 0 ; i < maxTouchEvents; i++ ) {
61+ mt[i]. touched = false ;
62+ }
63+ // Update MultiTouch that 'is touched':
64+ for (int i= 0 ; i < maxTouchEvents; i++ ) {
65+ if (i < pointers) {
66+ mt[i]. update(me, i);
67+ }
68+ // Update MultiTouch that 'isn't touched':
69+ else {
70+ mt[i]. update();
71+ }
72+ }
73+
74+ // If you want the variables for motionX/motionY, mouseX/mouseY etc.
75+ // to work properly, you'll need to call super.surfaceTouchEvent().
76+ return super . surfaceTouchEvent(me);
77+ }
78+
79+ // ------------------------------
80+ // Class to store our multitouch data per touch event.
81+
82+ class MultiTouch {
83+ // Public attrs that can be queried for each touch point:
84+ float motionX, motionY;
85+ float pmotionX, pmotionY;
86+ float size, psize;
87+ int id;
88+ boolean touched = false ;
89+
90+ // Executed when this index has been touched:
91+ // void update(MotionEvent me, int index, int newId){
92+ void update (MotionEvent me , int index ) {
93+ // me : The passed in MotionEvent being queried
94+ // index : the index of the item being queried
95+ // newId : The id of the pressed item.
96+
97+ // Tried querying these via' the 'historical' methods,
98+ // but couldn't get consistent results.
99+ pmotionX = motionX;
100+ pmotionY = motionY;
101+ psize = size;
102+
103+ motionX = me. getX(index);
104+ motionY = me. getY(index);
105+ size = me. getSize(index);
106+
107+ id = me. getPointerId(index);
108+ touched = true ;
109+ }
110+
111+ // Executed if this index hasn't been touched:
112+ void update () {
113+ pmotionX = motionX;
114+ pmotionY = motionY;
115+ psize = size;
116+ touched = false ;
117+ }
118+ }
0 commit comments