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 & HelpOpenGL and 3D Libraries › Rotating buttons fix.
Page Index Toggle Pages: 1
Rotating buttons fix. (Read 602 times)
Rotating buttons fix.
Nov 18th, 2009, 5:49am
 
I'm adapting the buttons example from the site to controlthe backgrount colour of a 3d model I am making. I already use mousePressed events to rotate the shape (and buttons along with it), so to keep the buttons working when rotated I am trying to use the get() method for deciding whether the mouse is over a white area, and if it is return a boolean that tells my program I am over the button.

I've stripped out everything I dont need for this example, and added a println of the colour's integer value from the mouseX, mouseY, which shows that its reading the changing colour, but now the buttons have stopped working.

- Im trying to go through it to see where the problem is but thought i'd check that this is the way i shoudl be approaching this first!?

Thanks, L.

Code:

float y = -0.1;
float x = -0.4;
float z = 0;
float g = 0;
PFont font;

//BUTTON
int rectX, rectY; // Position of square button
int circleX, circleY; // Position of circle button
int rectSize = 15; // Diameter of rect
int circleSize = 15; // Diameter of circle
color rectColor, circleColor, baseColor;
color rectHighlight, circleHighlight;
color currentColor;
boolean rectOver = false;
boolean circleOver = false;




void setup(){
size(500, 500, P3D);


//BUTTON VARIABLES.

rectColor = color(0);
rectHighlight = color(51);
circleColor = color(255);
circleHighlight = color(204);
baseColor = color(0);
currentColor = baseColor;
circleX = circleSize/2+30;
circleY = 80;
rectX = circleX-8;
rectY = 90;
ellipseMode(CENTER);
}

void draw(){

color whiteBtnColour = get(mouseX, mouseY);
int colourInt = int(whiteBtnColour);
println(colourInt);
noStroke();


//BUTTON MOUSE POSITION.
update(mouseX, mouseY);
background(currentColor);

if(rectOver) {
fill(rectHighlight);
} else {
fill(rectColor);
}
//stroke(255);
pushMatrix();
rotateY(y);
rotateX(x);
rotateZ(z);
rect(rectX, rectY, rectSize, rectSize);//WHERE THE BUTTONS ARE.

if(circleOver) {
fill(circleHighlight);
} else {
fill(circleColor);
}
//stroke(0);
ellipse(circleX, circleY, circleSize, circleSize);//WHERE THE BUTTONS ARE.
popMatrix();

lights();

// pushMatrix();
//translate(20, 20);
//font = loadFont("ArialMT-12.vlw");
//textFont(font);
//fill(255);
//text("left-click to rotate (x)", 10, 10, 0);
//text("right-click to rotate (y)", 10, 25, 0);
//text("'a' to zoom in", 150, 10, 0);
//text("'s' to zoom put", 150, 25, 0);
//text("Broadcasting Place Building.", 10, 45, 0);
//popMatrix();


noStroke();
pushMatrix();
translate(width/2, height/2, 0);
rotateY(y);
rotateX(x);
rotateZ(z);
scale(1+g,1+g,1+g);



fill(255, 137, 26); //left back end.
beginShape();
vertex(-20, 0, -20);//bottom left.
vertex(30, 0, -40);//bottom right.
vertex(30, -50, -40);//top right front.
vertex(-20, -50, -20);//top left.
endShape(CLOSE);


fill(255, 137, 26);
beginShape();
vertex(30, 0, -40);//step back.
vertex(30, 0, -45);
vertex(30, -50, -45);
vertex(30, -50, -40);
endShape(CLOSE);



popMatrix();





if((mousePressed)&& (mouseButton == RIGHT)){
y = y+0.1;

}else if((mousePressed)&& (mouseButton == LEFT)){
x = x-0.1;

}


//println(y);



}





void keyPressed(){
if (key == 'a') {
g = g+0.5;
} else if(key == 's') {
g = g-0.5;
}
}



//BUTTON MOUSE UPDATE?
void update(int x, int y)
{
if( overCircle(circleX, circleY, circleSize) ) {
circleOver = true;
rectOver = false;
} else if ( overRect(rectX, rectY, rectSize, rectSize) ) {
rectOver = true;
circleOver = false;
} else {
circleOver = rectOver = false;
}
}


void mousePressed()
{
if(circleOver) {
currentColor = color(255);
}
if(rectOver) {
currentColor = color(0);
}
}

boolean overRect(int x, int y, int width, int height)
{
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}

boolean overCircle(int x, int y, int colourInt)
{
// float disX = x - mouseX;
// float disY = y - mouseY;
// if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
if(colourInt == -1) {

return true;
} else {
return false;
}
}

Re: Rotating buttons fix.
Reply #1 - Nov 18th, 2009, 1:20pm
 
Ok perhaps this is a simpler way to illustrate what I am having trouble with - this is the button example with a few lines of code to make it spin.

Quote:
/**
 * Button. 
 * 
 * Click on one of the colored squares in the 
 * center of the image to change the color of 
 * the background. 
 */
float y = -0.1;
float x = -0.4;
float z = 0;
float g = 0;
 
int rectX, rectY;      // Position of square button
int circleX, circleY;  // Position of circle button
int rectSize = 50;     // Diameter of rect
int circleSize = 53;   // Diameter of circle
color rectColor, circleColor, baseColor;
color rectHighlight, circleHighlight;
color currentColor;
boolean rectOver = false;
boolean circleOver = false;

void setup()
{
  size(200, 200, P3D);
  smooth();
  rectColor = color(0);
  rectHighlight = color(51);
  circleColor = color(255);
  circleHighlight = color(204);
  baseColor = color(102);
  currentColor = baseColor;
  circleX = width/2+circleSize/2+10;
  circleY = height/2;
  rectX = width/2-rectSize-10;
  rectY = height/2-rectSize/2;
  ellipseMode(CENTER);
}

void draw()
{
    color whiteBtnColour = get(mouseX, mouseY);
int colourInt = int(whiteBtnColour);
  println(colourInt);
  
  update(mouseX, mouseY);
  background(currentColor);
  
  if(rectOver) {
    fill(rectHighlight);
  } else {
    fill(rectColor);
  }
  //stroke(255);
  pushMatrix();
  rotateY(y);
rotateX(x);
rotateZ(z);
  rect(rectX, rectY, rectSize, rectSize);
  
  if(circleOver) {
    fill(circleHighlight);
  } else {
    fill(circleColor);
  }

  ellipse(circleX, circleY, circleSize, circleSize);
    popMatrix();
    
      if((mousePressed)&& (mouseButton == RIGHT)){
  y = y+0.1;

}else if((mousePressed)&& (mouseButton == LEFT)){
  x = x-0.1;

}
}

void update(int x, int y)
{
  if( overCircle(circleX, circleY, circleSize) ) {
    circleOver = true;
    rectOver = false;
  } else if ( overRect(rectX, rectY, rectSize, rectSize) ) {
    rectOver = true;
    circleOver = false;
  } else {
    circleOver = rectOver = false;
  }
}

void mousePressed()
{
  if(circleOver) {
    currentColor = circleColor;
  }
  if(rectOver) {
    currentColor = rectColor;
  }
}

boolean overRect(int x, int y, int width, int height)
{
  if (mouseX >= x && mouseX <= x+width &&
      mouseY >= y && mouseY <= y+height) {
    return true;
  } else {
    return false;
  }
}

boolean overCircle(int x, int y, int colourInt)
{
//  float disX = x - mouseX;
//  float disY = y - mouseY;
//  if(sqrt(sq(disX) + sq(disY)) < diameter/2 ) {
   if(colourInt == -1) {
    return true;
  } else {
    return false;
  }
}

Page Index Toggle Pages: 1