Adding Listener to Gui Parameters on Second App

Hello.

I’m building an app on top of the example multiWindowExample since it divides each window with it’s own cpp and h files. Now I’m having a doubt regarding accessing a button state on the instantiation of ofApp that doesn’t have the Gui. As in the example, there is a shared_ptr<GuiApp> gui; in the header file which we can access, for example to set the color of the background using gui->color. But how about creating a function to access the boolean state of a button? I’m thinking about using void ofAddListener(EventType &event, void (*)(const void *, ArgumentsType &) but I’m not really sure if this is right.

Any ideas on how to solve this? Some code would be helpful to understand the shared variables. I would like to access everything in both places.

Thanks

My current solution is to add the following function to the GuiApp.cpp:

void GuiApp::getButtonState() {
    cout << startButton.get();
}

And evaluate the shared_ptr in the ofApp in the draw() function to see is gui->getButtonState() is 1 and not 0. Not really sure if this is the best way according to OF structure.

Hi,
If you really do need shared access between the two apps, I would rather say that it seems easier doing a single app with two windows. There is an example for that.

Besides this, it is totally safe for you to use listeners in the case of using two apps.
instead of using ofAddListener I prefer to make an instance of ofEventListener check the examples.

as for the button state I would recommend using an ofParameter as it will act as a bool and emmit an event when it changes.

so, in your ofApp.h put:

ofEventListener listener;
ofParameter<bool> buttonState = {"Button", false}
void buttonChanged(bool&);
ofxPanel gui; //assuming you are using ofxGui

then in ofApp.cpp



void ofApp::setup(){
     gui.setup();
     gui.add(buttonState);

    listener = buttonState.newListener(this, &ofApp::buttonChanged);
}
void ofApp::buttonChanged(bool&){
// this  gets called each time the button changes its state.
}
2 Likes

Thank you @roymacdonald !

1 Like