Loading...
Logo
Processing Forum

Multitouch

in Android Processing  •  3 months ago  
Hello,

i found this example for multitouch:
import android.view.MotionEvent;
 float x,y;
String[] fontList;
PFont androidFont;
int circleBaseSize = 512; // change this to make the touche circles bigger\smaller.
 
void setup() {
  size(displayWidth, displayHeight);
  // Fix the orientation so the sketch won't reset when rotated.
  orientation(PORTRAIT);
  stroke(255);
  smooth();
  // Setup Fonts:
  fontList = PFont.list();
  androidFont = createFont(fontList[0], 16, true);
  textFont(androidFont);
  textAlign(LEFT);
}
 
void draw() {
  background(0);
 
}
 
void infoCircle(float x, float y, float siz, int id) {
  // What is drawn on sceen when touched.
  float diameter = circleBaseSize * siz;
  noFill();
  ellipse(x, y, diameter, diameter);
  fill(0, 255, 0);
  ellipse(x, y, 8, 8);
  text( ("ID:"+ id + " " + int(x) + ", " + int(y) ), x-128, y-64);
}
 
//-----------------------------------------------------------------------------------------
// Override Processing's surfaceTouchEvent, which will intercept all
// screen touch events.  This code only runs when the screen is touched.
 
 boolean surfaceTouchEvent(MotionEvent me) {
  // Number of places on the screen being touched:
  int numPointers = me.getPointerCount();
  for (int i=0; i < numPointers; i++) {
    int pointerId = me.getPointerId(i);
    float x = me.getX(i);
    float y = me.getY(i);

    
    // Added the .5 to make the outer ellipse noticeable
    float siz = me.getSize(i) + .5;
    infoCircle(x, y, siz, pointerId);
  }
  // If you want the variables for motionX/motionY, mouseX/mouseY etc.
  // to work properly, you'll need to call super.surfaceTouchEvent().
  return super.surfaceTouchEvent(me);
}

Is there a way to send the x,y variables to the draw() function? Or to call them in draw()?

Replies(2)

Re: Multitouch

3 months ago
Callback functions like setup(), draw(), mousePressed(), surfaceTouchEvent() can't be changed.
They are automatically invoked by something else w/ fixed number of parameters.

To exchange variables, use global ones!

Re: Multitouch

3 months ago
Thanks! I did it like this:

import android.view.MotionEvent;
 float x,y,x1,y1;
String[] fontList;
PFont androidFont;
int circleBaseSize = 512; // change this to make the touche circles bigger\smaller.
 
void setup() {
  size(displayWidth, displayHeight);
  // Fix the orientation so the sketch won't reset when rotated.
  orientation(PORTRAIT);
  stroke(255);
  smooth();
  // Setup Fonts:
  fontList = PFont.list();
  androidFont = createFont(fontList[0], 16, true);
  textFont(androidFont);
  textAlign(LEFT);
}
 
void draw() {
  background(0);
  textSize(30);
  text(x+"   "+y,100,100);
   text(x1+"   "+y1,100,200);

}
 
void infoCircle(float x, float y, float siz, int id) {
  // What is drawn on sceen when touched.
  float diameter = circleBaseSize * siz;
  noFill();
  ellipse(x, y, diameter, diameter);
  fill(0, 255, 0);
  ellipse(x, y, 8, 8);
  text( ("ID:"+ id + " " + int(x) + ", " + int(y) ), x-128, y-64);
}
 
//-----------------------------------------------------------------------------------------
// Override Processing's surfaceTouchEvent, which will intercept all
// screen touch events.  This code only runs when the screen is touched.
 
public boolean surfaceTouchEvent(MotionEvent me) {
  // Number of places on the screen being touched:
  int numPointers = me.getPointerCount();
  for (int i=0; i < numPointers; i++) {
    int pointerId = me.getPointerId(i);
    if(i==0){
     x = me.getX(i);
    y = me.getY(i);
    }
    if(i==1){
    x1= me.getX(i);
    y1=me.getY(i);
    }
    // Added the .5 to make the outer ellipse noticeable
    float siz = me.getSize(i) + .5;
    infoCircle(x, y, siz, pointerId);
  }
  // If you want the variables for motionX/motionY, mouseX/mouseY etc.
  // to work properly, you'll need to call super.surfaceTouchEvent().
  return super.surfaceTouchEvent(me);
}

Another problem is that the x,y,x1 and y1 will not be set to 0 if you release the touch field. I'd like to have "if mousePressed" for booth x,y and x1,y1.