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;
}
}