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 & HelpSyntax Questions › Detecting cursor over objects
Page Index Toggle Pages: 1
Detecting cursor over objects (Read 714 times)
Detecting cursor over objects
Apr 27th, 2010, 7:53am
 
I've found an explanation of the solution to my problem provided to someone else by wsx: Quote:
Code:


void mouseReleased() {
 if (chart.contains(mouseX, mouseY)) {
   chart.setEnlarged(true);
 }
}

void draw() {
 pushMatrix();
 if (chart.isEnlarged()) {
   scale(10);
 }
 chart.draw();
 popMatrix();
}




The above code assumes that you have an class which the variable chart is an instance of. That class would have a method contains(int x, int y) which would return whether or not that position was in the bounds of the object, as well as the methods setEnlarged(boolean b) and isEnlarged() which would probably just flip an internal boolean variable.

Finally the draw method would check if the chart was enlarged, and if it was scale before drawing it. The pushMatrix() and popMatrix() are to ensure that it doesn't scale everything, but just the chart in question.


I have been trying to apply this solution to my own sketch, & so-far I think I have  made it so that my objects know their x & y positions, but I'm not sure how to create the method that returns a boolean value for 'mouse = over" or "mouse = not over".

Could someone give me a hint please? - I've tried variations on this:
Code:
 boolean containsCheck(){
   if((mouseX>thisBlockXValue) && (mouseX<(thisBlockXValue+blockWidth))){
     println("mouse is over! - block: "+num);
     return (true);
   }
}

..but I've got the syntax wrong I think. Thanks!

My sketch folder (with data folder) is here: www.samhumphrey.co.uk/public_html/testArea/voterPowerUK.zip
Re: Detecting cursor over objects
Reply #1 - Apr 27th, 2010, 10:40am
 
This simple example with a class should make it the process clearer.

Code:

Foo f1;

void setup(){
 size(300,300);
 cursor(CROSS);
 f1 = new Foo(60,90,150,100);

}

void draw(){
 background(200);
 f1.display();
 if(f1.contains(mouseX, mouseY)){
   f1.setColor(color(255,0,0));
 }
 else {
   f1.setColor(color(0,0,255));
 }
}

class Foo {
 // Top left
 public int x,y;
 // width and height
 public int w,h;

 private int fillCol;

 public Foo(int xx, int yy, int ww, int hh){
   x = xx;
   y = yy;
   w = ww;
   h = hh;
   fillCol = color(0,0,255);
 }

 public void display(){
   fill(fillCol);
   noStroke();
   rect(x,y,w,h);
 }

 public void setColor(int col){
   fillCol = col;
 }
 
 // See if point px,py is inside the shape
 public boolean contains(int px, int py){
   if(px >= x && px <= x + w && py>=y && py<= y+h){
     return true;
   }
   else {
     return false;
   }
 }
}
Re: Detecting cursor over objects
Reply #2 - Apr 27th, 2010, 3:01pm
 
Thanks Quark, I'll give that a read through now, it looks like it should sort out everything all being well!
Re: Detecting cursor over objects
Reply #3 - Apr 28th, 2010, 6:48am
 
That solution has worked, & I've been able to apply it to my sketch, but even after I strippe everything out other than the bare minimum it seems to be using every scrap of my processor's power.

Have I got it stuck going in loops somewhere or is this just a really taxing task for the applet to perform?

L.

Edit: I reduced the arrayList into chunks of 100 & it seems fine now. If there IS a way to scan through a large number of objects without using as much processor power I'd still be interested!

Code:
import de.bezier.data.*;

XlsReader reader;
PFont font;

AreaObject areaObjectItem;//my object.

int arraysLength = 651;
int alongValue = 0;
int upValue = 1;
int columnValue;
int rowValue;

int sortMode = 0;
int objectColumnValue;
int objectRowValue;

int blockWidth = 50;
float howManyColumnsFloat;
int howManyColumns;
float numOfRowsFloat;
int blockHeight;

int blockNumber;
int blockNumber2;

int zoomToMouseXValue;
int zoomToMouseYValue;
String zoomMode = "zoomed in";

int shiftDownRowValue = 0;
int blockNumberInRow = 0;
int updateEverything;

ArrayList areaItemsArray = new ArrayList();//arrayList of objects.

class AreaObject{//define my class.

 //next list the object's attributes.
 String constituencyValue;
 float voterPowerValue;
 String party;
 int popSizeValue;
 int num;
 String mode;
 float thisBlockXValue;
 float thisBlockYValue;
 public int x,y;//top left corner of my object(rectangle)
 public int w,h;//width & height of my object's width & height.
 private int fillCol;
 
 //this is the constructor.
 public AreaObject(String tempConstituencyValue, float tempVoterPowerValue, String tempParty, int tempPopSizeValue, int tempNum, String tempMode, int xx, int yy, int ww, int hh) {
   constituencyValue = tempConstituencyValue;
   voterPowerValue = tempVoterPowerValue;
   party = tempParty;
   popSizeValue = tempPopSizeValue;
   num = tempNum;
   mode = tempMode;
   
   x = xx;
   y = yy;
   w = ww;
   h = hh;
   
   if(party.equals("LD") == true){
     fillCol = color(247, 207, 0, map(voterPowerValue, 0, 1.3, 0, 255));
   }else if(party.equals("Lab") == true){
     fillCol = color(216, 59, 31);
   }else if(party.equals("C") == true){
     fillCol = color(61, 9, 234);
   }else{
     fillCol = (255);
   }
   
 }
 
 void display(int i){
   num = i;
   
   rectMode(CENTER);
   noStroke();
   fill(fillCol);
   rect(x, y, blockWidth-5, blockHeight-5);
   
   if(mode.equals("blockSelectedMode") == true){// blockSelectedMode means "if cursor is over block"...
     fill(fillCol);

   }
 }
 
 
 public void setColor(int col){
   fillCol = col;
 }
 
 
 public boolean contains(int px, int py){
   if((px >= (x-(w/2)) && (px <= (x + (w/2))) && (py>=y && py<= y+h))){
     return true;
   }
   else {
     return false;
   }
 }
 
 
 void updateBlockXYPos(){
    x = round(screenX(x, y));
    y = round(screenY(x, y));
 }
 
 
}//end of class.


void setup(){
 //println(PGraphicsPDF.listFonts());
 size(1000, 800);
 frameRate(30);
 //background(0);
 reader = new XlsReader( this, "voterPower.xls");
 font = createFont("Futura", 10);
 textFont(font);
 
howManyColumnsFloat = (width-200)/blockWidth;
howManyColumns = ceil(howManyColumnsFloat);
numOfRowsFloat = arraysLength/howManyColumns;
blockHeight = floor((height-200)/(1.5*numOfRowsFloat));
 
  for(int i=1; i<arraysLength; i++){
   areaItemsArray.add(new AreaObject(reader.getString(i, 1), reader.getFloat(i, 2), reader.getString(i, 6), round(reader.getFloat(i, 11)), i, "", 0, 0, blockWidth, blockHeight));
  }
 
  mouseX = width/2;
  mouseY = height/2;
}//end of setup.



void draw(){
 background(0);

blockNumberInRow = 0;
shiftDownRowValue = 0;
for(int i=0; i<areaItemsArray.size(); i++){//draws all blocks out.
 blockNumberInRow++;

   AreaObject currentAreaObjectItem = (AreaObject) areaItemsArray.get(i);//get my object..
if( (blockNumberInRow*blockWidth) < (width-blockWidth)){
 
   if(updateEverything<2){
         currentAreaObjectItem.x = blockNumberInRow*blockWidth;//translate it like this instead of translate()..
             currentAreaObjectItem.y = 100+(shiftDownRowValue*blockHeight);
   currentAreaObjectItem.updateBlockXYPos();//update the object's x & y (or it thinks it is at 0,0).

   }
   
   if(currentAreaObjectItem.contains(mouseX, mouseY)){//change attributes/modes
     currentAreaObjectItem.mode = "blockSelectedMode";
     currentAreaObjectItem.setColor(255);
   }else{
     currentAreaObjectItem.setColor(color(0,90,255));
   }
   currentAreaObjectItem.display(i);//display the object!
   
 if(blockNumberInRow>15){
   blockNumberInRow = 0;
   shiftDownRowValue++;
 }
   
}

   println("shiftDownRowValue "+shiftDownRowValue);
   println("blockNumberInRow "+blockNumberInRow);
  }//end of all blocks loop.
     updateEverything++;

}//end of draw.
Page Index Toggle Pages: 1