In general anything specifically related to the object should happen within the class definition; so something like a state should be a class property rather than a global property...
The global 'activeCell' variable in my previous code is a good example to work with. That's probably the best solution if there's only ever going to be a single active cell; but what if each cell can be 'active' as you want? You could create a global array to contain active cells; but you'd be digging yourself a very big hole.... It's way simpler to give the Cell an 'active' property and change this when it is clicked:
Code://la grilla tiene cuadrados externos e internos,
//la diferencia de tamaño entre estos es de 2 veces el espesor de borde definido
//
int numrows = 8;
int numcols=8;
int rectWidthext = 50;
int rectHeightext = 50;
int rectborder=3;
int rectWidth=rectWidthext-2*rectborder;
int rectHeight=rectHeightext-2*rectborder;
// 2D Array of objects
Cell[][] grid;
void setup() {
size(rectWidthext*numcols+2*rectborder,rectHeightext*numrows+2*rectborder);
grid = new Cell[numcols][numrows];
for (int i = 0; i < numcols; i++) {
for (int j = 0; j < numrows; j++) {
// Initialize each object
grid[i][j] = new Cell(i*50,j*50,50,50,i+j,0);
}
}
}
void draw() {
background(0);
fill(255);
// The counter variables i and j are also the column and row numbers and
// are used as arguments to the constructor for each object in the grid.
for (int i = 0; i < numcols; i++) {
for (int j = 0; j < numrows; j++) {
// Oscillate and display each object
grid[i][j].display();
//grid[i][j].mouseover();
}
}
}
void mousePressed() {
// With mousePressed outside of a class you just need to iterate over all relevant objects
for (int i = 0; i < numcols; i++) {
for (int j = 0; j < numrows; j++) {
// this would be tedious to write if I had to pass all the parameters
if(grid[i][j].mouseover()){
// toggle the active state
grid[i][j].active = !grid[i][j].active;
}
}
}
}
// A Cell object
class Cell {
// A cell object knows about its location in the grid as well as its size with the variables x,y,w,h.
float x,y; // x,y location
float w,h; // width and height
float angle; // angle for oscillating brightness
int ccolor;//color de la celda para transmitir 0-255
boolean active;
// Cell Constructor
Cell(float tempX, float tempY, float tempW, float tempH, float tempAngle,int tempcolor) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
ccolor = tempcolor;
angle = tempAngle;
boolean over =false;
}
// Oscillation means increase angle
void oscillate() {
angle += 0.02;
}
void display() {
stroke(255);
// Color calculated using sine wave
//fill(127+127*sin(angle));
// not too happy with duplicating the call to mouseover here...
if(active && mouseover()){
ccolor = #ffcc00;
}
else if (mouseover()) {
ccolor=255;
}
else if(active) {
ccolor = #ff9900;
}
else {
ccolor =0 ;
}
fill(ccolor);
rect(x+3,y+3,w,h);
//print (x+" ");
}
//mouse over detection
// No need to pass parameters: Since this is a class method
// it already has access to the class properties
boolean mouseover() {
// not sure why you were using pmouseX/Y - that's the mouse position on the previous frame
if (mouseX > x && mouseX < x+w && //verifica if mouse over
mouseY > y && mouseY < y+h) {
return true;
}
return false;
}
}
Now when the mouse is pressed we toggle the value of the current object's active property... Simples! <[sorry bad 'cultural' reference that will be lost on anyone outsise the UK].
Just to clarify this line:
grid[i][j].active = !grid[i][j].active;
This is a handy shortcut for toggling boolean variables. It says "assign the variable to the opposite of itself".
Note: I wasn't too happy about making a repeated call to mouseover() when assiging the colour of the cell in the display() method. If you find you use it a lot there's a good argument to make 'mouseover' a property and then just call the function once each frame to update it...