Has anyone used the mousewheel to select between shapes onscreen

Hi Experts,

I have 3 rectangles onscreen and want to use the mouse wheel to highlight each one in turn. Has anyone done something similar? I would appreciate an example if possible.

Thank you.

KB

1 Like

Hi,

Here’s a quick and dirty example:

int selectedShape = 0;

void setup() {
  size(1280, 420);
  noStroke();
}


void draw() {
  background(20);
  
  fill(200);
  if (selectedShape == 0) {
    fill(200, 20, 20);
  }
  rect(20, 20, 1240, 80);
  
  fill(200);
  if (selectedShape == 1) {
    fill(200, 20, 20);
  }
  rect(20, 120, 1240, 80);
  
  fill(200);
  if (selectedShape == 2) {
    fill(200, 20, 20);
  }
  rect(20, 220, 1240, 80);
  
  fill(200);
  if (selectedShape == 3) {
    fill(200, 20, 20);
  }
  rect(20, 320, 1240, 80);
}


void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  
  if (e > 0) {
    selectedShape++;
  } else {
    selectedShape--;
  }
  
  if (selectedShape > 3) {
    selectedShape = 0;
  }
  
  if (selectedShape < 0) {
    selectedShape = 3;
  }
}
1 Like

Oh I see, very clean. Thank you, now I see. I was going down the road of Pshapes such that I had to name each shape. Yours is cleaner.

Thanks again.

KB

the mouse wheel can even be multiplexed for several jobs,
by using add the keyboard:
a full framework would be this

here the short example

int  e_objectTyp = 0;

void draw() {
  switch(e_objectTyp) {
  case 0: 
    user_object();
    break;
//...
 }
}

void mouseWheel(MouseEvent event) {
  float e = event.getCount();
  if ( keyPressed && key == 'o' ) {    
    e_objectTyp += e;
    e_objectTyp = constrain(e_objectTyp, 0, 3); }                     // [o]              object typ YOUR, circle, rect, triangle,
}

1 Like

Really interesting extension of the thought. Thank you for taking the time to reply.

Cheers,

KB