[solved] Vector of ofxMSAInteractiveObject

Hi!

I’m trying to make a menu with buttons using the ofxMSAInteractiveObject addon, and the buttons using images that will be inside a folder. I’m using a vector, since the number of buttons will be according to the number of images that are in the folder. I created a new class extented ofxInteractiveObject:

class Botao : public ofxMSAInteractiveObject {
    public:
          ofBaseDraws    *content;
    
          Botao() {
              content = NULL;
          }
    
          void draw() {  
              if(content) {  
                  width = content->getWidth();  
                  height = content->getHeight();  
                  content->draw(x, y, width, height);  
              }

          void onPress(int x, int y, int button){
              cout << "click!" << endl;
          }
};

In ofApp.h:

class ofApp : public ofBaseApp {
   ...
   vector<ofxInteractiveObject> objVector;
   vector<ofImage> imagensVector;
}

In ofApp.cpp:

void ofApp::setup() {

    ofDirectory dir("images/");
    dir.listDir();
    for (unsigned int i = 0; i < dir.size(); i++) {
        ofImage img;
        img.load(dir.getPath(i));
        imagensVector.push_back(img);
    }

    for (int i = 0; i < imagensVector.size(); i++){
        Botao botao;
        botao.content = &imagensVector[i];
        botao.setPosition( (i*70)+40, 100 );
        botao.setSize(50, 40);
        botao.enableMouseEvents();

        objVector.push_back(botao);
    }
}

But this does not work! Why? If I declare a single Botao, he draws and events works like a charm.
Is there a better way to do this? Thanks in advance.

obs: I using vector<ofImage> because I will need them later.

what happens if you do:

 vector < Botao > objVector;

in the ofApp.h? I think you can’t have a vector of one type and push back another type (even if it’s a subclassed type). You can do this for vectors of pointers, ie:

 vector < parentclass * > myObjects;
 ....
 derivedClass * derivedPtr = new derivedClass();     // derivedClass inherits parentclass
 myObjects.push_back(    derivedPtr  );

now, my object has an array of parentclass pointers, but really they are derived class pointers…

does that make sense?

Yes!

But sorry, objVector is already declared as vector<Botao>, I just wrote wrong in my post :sweat:

ok, it’s kind of hard to see without the full code. if that’s the case, you should make an ofxInteractiveObject, push a Botao into that, then add that to the objVector ? (it’s not clear to me why you need a vector of vectors)

sorry for my spelling errors and bad English…
I dont need a vector of vectors, here is my code, just compile with make:

not that I can draw the vector with ofApp::draw(), but I believe that this is not the goal in use the ofxMSAInteractiveObjects.

ofApp()::drawPage1(2, 3…) will be used with the buttons, when it work :stuck_out_tongue:

thank you for the help, @zach

ok is the problem that you lose the events once you put them into a vector?

yes!
I did something wrong?

ok can you try this:

Button Button;
buttonVector.push_back( Button );
buttonVector.back().content = &imageVector[i];
buttonVector.back().setPosition((i * 70) + 40, 100);
buttonVector.back().setSize(50, 40);
buttonVector.back().enableMouseEvents();	

when you use “push back” you are actually copying the object (see “pass by copy”), my intuition is this causes the events to get uncoupled, ie, the original has the event but goes out of scope while the copy doesn’t have the events and gets added to the vector…

nothing happens!
it seems that events are only added in the Buttons declared in ofApp class, not in ofApp::setup()

@memo :smiley:

I had some progress…
The problem was that when I declare an object of class derived from ofxMSAInteractiveObject, his constructor is called, that calls the method enableAppEvents(), which in turn adds listeners ofEvents().setup, update and draw.
If I remove enableAppEvents() from constructor of ofxMSAInteractiveObject, and only then I do buttonVector.back().enableAppEvents(), the events works.

BUT only works for the last two vector elements D:
It does not make any sense

(repository updated)

maybe make an issue on the addon repo on github?

well, the problem was solved
if I do buttonVector.resize() and access their elements with the operator[], everything works.

thanks @zach, you are a good man