Why all the instances of my class are colored at once?

Hi Forum, I came from processing and I’m a totally beginners with OF.

I have a simple class called ofSnap that is nothing more than a black rectangle (for the moment). I’m trying to implement a simple function for change the color of the rectangle when it is clicked.
Apparently every thing work, I get the right values from my var (over and selected) but ofSetColor color all the instance and not just one. I can’t understand what happen, I’m sure is something stupid but I can’t figure out it.
Here the code, any help is really appreciated.
thanks in advance
husk

ofSnap.h

#ifndef _OF_SNAP // if this class hasn't been defined, the program can define it
#define _OF_SNAP // by using this if statement you prevent the class to be called more
             // than once which would confuse the compiler
#include "ofMain.h"
class ofSnap {

public:
    // methods
    void update();
    void draw();
    void _mouseMoved(int x, int y);
    void _mousePressed(int x, int y, int button);


   ofSnap(float x, float y, int id); // constructor - used to initialize an object, if no properties are passed
              //               the program sets them to the default value

    //variables

    int snapwidth, snapheight, id, x,y;
    bool isOver, isSelected;

    ofRectangle blackSnap;


private: // place private functions or variables declarations here
}; // dont't forget the semicolon!!
#endif

ofSnap.cpp

#include "ofSnap.h"
ofSnap::ofSnap( float x, float y, int _id){
//init var
this->isOver = false;
this->isSelected = false;
this->snapos.set(x,y);
this->snapwidth = 120;
this->snapheight = 80;
this->id = _id;
this->x = 0;
this->y = 0;
//ofAddListener(ofEvents().mousePressed, this, &ofSnap::_mousePressed);
//ofAddListener(ofEvents().mouseMoved, this, &ofSnap::_mouseMoved);
}
void ofSnap::update(){

    blackSnap.x = snapos.x;
    blackSnap.y = snapos.y;
    blackSnap.width = snapwidth;
    blackSnap.height = snapheight;
    if(this->isSelected) {
        ofSetColor(0);
    } else {
        ofSe    tColor(255);
    }
}
void ofSnap::draw(){
    ofRect(blackSnap);
}
void ofSnap::_mouseMoved(int x, int y){
this->x = x;
this->y = y;
if(x > this->snapos.x && this->x < this->snapos.x+this->snapwidth && this->y > this->snapos.y && this->y < this->snapos.y+this->snapheight ){
this->isOver = true;
} else{
this->isOver = false;
}
}
void ofSnap::_mousePressed(int x, int y, int button){
if(this->isOver){
    if (!this->isSelected) isSelected =true;
    //path = video.getMoviePath();
     ofLog() << "is selected? " << this->isSelected << " id " << id;
} else{
    this->isSelected=false;
}
}

testApp.h:

#pragma once
#include "ofMain.h"
#include "ofSnap.h"
class testApp : public ofBaseApp{

public:
	void setup();
	void update();
	void draw();

	void keyPressed(int key);
	void keyReleased(int key);
	void mouseMoved(int x, int y );
	void mouseDragged(int x, int y, int button);
	void mousePressed(int x, int y, int button);
	void mouseReleased(int x, int y, int button);
	void windowResized(int w, int h);
	void dragEvent(ofDragInfo dragInfo);
	void gotMessage(ofMessage msg);

    float posy, posx;
   ofRectangle blackSnap;

    private:

    int nSnap;
    ofSnap** mySnap;
};

testApp.cpp:

#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
    nSnap= 2*2;
    mySnap = new ofSnap*[nSnap];
    int index = 0;
for (int i = 0; i < 2; i++){
        for (int j = 0; j < 2; j++){
 posx = (120*i)+(i*5);
 posy = (80*j)+(j*5);
   mySnap[index] = new ofSnap(posx,posy, index);
   index++;
    }

}
}
//--------  ------------------------------------------------------
void testApp::update(){

for (int i = 0; i < nSnap; i++){
mySnap[i]->update();
}
}
//--------------------------------------------------------------
void testApp::draw(){
 for (int i = 0; i < nSnap; i++){
mySnap[i]->draw();
}
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
}
//--------------------------------------------------------------
void testApp::keyReleased(int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y){
 for (int i = 0; i < nSnap; i++){
   mySnap[i]->_mouseMoved(x,y);
}
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
 for (int i = 0; i < nSnap; i++){
            //ofLog() << "index " << mySnap[i]->id << "+ over " << mySnap[i]->isOver;
    mySnap[i]->_mousePressed(x,y,button);}
}    //--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){
}

ofSetColor sets the color globally, since you are calling it in update, it’ll chnage the color and only the last call will have any effect, then all the calls in draw will draw using that color. try moving the logic of ofSetColor into the draw function

hey @arturo,
moving the if statement

   if(this->isSelected) {
    ofSetColor(255, 120,0);
    } else {
        ofSetColor(255);
    }

to the draw functions just one rect is being colored (as expected)…but It’s not the clicked one but the next one (for id 0 id 1 will be colored and so forth).
It seems that ofSetColor() instruction happen somehow after the render cycle (or something like that?)
How can I avoid this behavior and what is the right form to write such a basic thing in OF?

thaks for the help, is really appreciate
husk

You need to setColor() and then draw()

ofPushStyle(); 
ofSetColor(255);
//draw() you item here
ofPopStyle();

@az_rr That did the trick!
many thanks
husk