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.
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:
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)
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…
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