We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › slow mousepressed lag time
Page Index Toggle Pages: 1
slow mousepressed lag time (Read 206 times)
slow mousepressed lag time
Aug 5th, 2008, 4:57pm
 
Hi,
I am making a bubble diagram that people will be able to interact with by mouse press and drag on the bubbles. Can anyone recommend a more efficent way to code this so that I won't have such a large lag time between the mouse and the object when I use mousepressed and drag.
Thanks!
//michelle swanson 07/28/08

int s = 4; //**scale factor
float rx;
float ry;
spaces[] space;
void setup(){
//size (1600,1000);
 smooth();
 //import space prgram data, it must be located in the data folder
 String lines[] = loadStrings("test11.txt");
 space= new spaces[lines.length]; //to store space info in an array
 for (int j=0; j < lines.length; j++) {
   String[] group = (split(lines[j],",/,"));//to split into 2 groups and separate out the adjacency info by the ,/, deliminator
   String[] groupN = (split(group[0],","));
   // println(groupN[0]); //name,area,dept [0] all one line;
   //println(group[1]); //adj list [1] dept1adj,dept2adj,dept3adj...through lines.length all one line
   float[] group0 = float(split(group[0],",")); // to split and convert group0 by comma into separate int values
   //println(group0); //(name[0],area[1],dept[2]) to store in array, department adjacent value matrix, single split int values
   float[] group1 = float(split(group[1],",")); //to separate dept1adj, dept2adj, dept3adj...through group1.length
   // println (group1);
  space[j] = new spaces(groupN[0], group0[1], (sqrt(group0[2]))*s, group0[4]*(j+50), random(100,(width-100)), random(150,(height-150)), j, group1);  // to store in array

}
}
void draw()
{
 background(255);

 // loop to call behavior
 for (int i = 0; i < space.length; i++){
   space[i].render();
   space[i].connect(space);
 }
}  
// loops for mouse interaction behavior
void mousePressed(){
 for (int i=0; i<space.length; i++){
   space[i].press();
 }
}
void mouseDragged(){
 for (int i=0; i<space.length; i++) {
   space[i].drag();  
    }
}
void mouseReleased() {
 for (int i=0; i<space.length; i++){  
   space[i].release();
   }
}

class spaces
{
 // define variables - states
 float nm;
 float ar;
 float dt;
 float bc;
 float xp;
 float yp;
 float id;
 float s;
 String tnm;
 boolean hover = false;
 boolean locked = false;
 float dia;
 float vx = 0;
 float vy = 0;
 float[] ad;
 
 // constructor
 spaces(String tname, float name, float area, float dept, float xpos, float ypos, float ident, float[] adjc)
 {
   tnm = tname;
   nm = name;
   ar = area;
   dt = dept;
   xp = xpos;
   yp = ypos;
   id = ident;
   ad = adjc;

 }
 //space behaviors-------------------------------------
 
 void render(){
   fill(255,dt,0,150); // color by department(space[i].dt)
   stroke (50);
   strokeWeight(.5);
   ellipse (xp, yp, ar, ar); //organize by dept so add dt into xp or yp location
       PFont font;
   font = loadFont("Arial-BoldMT-12.vlw");
   textFont(font);
   fill(0);
   textAlign(CENTER);
   text((tnm+" "+nm), xp, yp+(ar/1.25));
 }

 //--------------------------------------------
 void press(){
     float r = (ar/2);
     if (mouseX > xp-ar/2 && mouseX < xp+ar/2 &&
       mouseY > yp-ar/2 && mouseY < yp+ar/2) {
       hover = true;  
     }
     else {
       hover = false;
     }
  if(hover) {
       locked = true;
       //fill(255, 255, 255);
     }
     else {
       locked = false;
     }
   }
 //--------------------------------------------
 void drag() {

     if (locked){
       xp = mouseX;
       yp = mouseY;
    }
   
 }
 //--------------------------------------------
 void release() {
     locked = false;
   }  
 //--------------------------------------------  
 void connect (spaces[] space) { // draw line from current i to proper adjacency space
   for (int i = 0; i < ad.length; i++) {
     if (ad[i] == 1) {
       if (i ==  space[i].id){
         strokeWeight(4);
         stroke (50);  
         line (xp, yp, space[i].xp, space[i].yp);      //dist between = 0 //draw bold line and connect
       }
     }
     else if  (ad[i]==2) {
       if (i ==  space[i].id){
         strokeWeight(2);
         stroke (50);
         line (xp, yp, space[i].xp, space[i].yp);      //dist between < 100 //draw medium line and connect
       }  
     }
     else if  (ad[i]==1) {
       if (i ==  space[i].id){
         strokeWeight(.1);
         stroke (50);
         line (xp, yp, space[i].xp, space[i].yp);     //dist between < 200 //draw thin line and connect
       }
     }

     else if  (ad[i]==(-1)) {
       if (i ==  space[i].id){
         strokeWeight(.01);
         noFill();
         stroke (255,dt,0);
         ellipse (xp, yp, ar*2, ar*2);
         ellipse (space[i].xp, space[i].yp, space[i].ar*2, space[i].ar*2);      //dist between < 200 //draw thin line and connect
       }
     }
   }
 }
}
//(here is the text11.txt file to save in the data folder to import)
office,1,200,mrkt,1,,/,,,,,,,,,,,,,,,,
office,2,200,design,2,,/,1,,,,,,,,,,,,,,,
open office,3,1000,design,2,,/,0,3,,,,,,,,,,,,,,
office,4,200,tech,3,,/,0,0,0,,,,,,,,,,,,,
office,5,200,tech,3,,/,0,0,0,2,,,,,,,,,,,,
open office,6,1200,tech,3,many windows,/,0,0,1,3,0,,,,,,,,,,,
Page Index Toggle Pages: 1