I am trying to make an expandable ofxGui. I think I may have done this all wrong. I dont know how many cameras I will have but I want to control them all.
I used this in ofApp.h
ofxPanel gui;
vector<ofxToggle*> drawcams;
and this in my setup in ofApp.cpp
for (int i = 0; i < deviceList.size(); i++) {
ofxToggle * drawcam = new ofxToggle();
drawcams.push_back(drawcam);
gui.add(drawcams[i]->setup("Draw Cam " + ofToString(i+1), true));
}
This seems to be problematic as my settings are going on and off sometimes.
I would also like to use listener callbacks like this
I don’t know if that helps you but I have a pretty similar example within my addon ofxSortableList called example-togglelist-parameters. It adds random shapes and creates a toggle for each one. The forms are shown or hidden depending on the toggle status. You can just ignore the feature of sorting the elements.
It does not use listener callbacks but I still thought you might want to have a look at the code.
@frauzufall Thanks for the tips, I will integrate this method into my project. Nice addon by the way, I could have used it quite a few times in the past.
take a look at gui/parameterGroupExample and in general the examples in the gui folder they shows how to use ofParameter and ofParameterGroup properly. among other things you don’t need to use pointers to use ofParameter
Ok, I have switched to using parameter group for all my work. I am stuck on something in the same chain. I have a parameter group that I initialise as a vector as I don’t know how many of these I need later. I would really like to have callbacks attached to the parameter changes but I am not sure how to move forward.
Hi,
Not sure this is a best practice, but here’s what I do to create n ofxToggle (not ofParameter, sorry )
for ( int i = 0; i < n; i++ )
{
Toggle * toggle = new Toggle();
toggle->setup( "toggle " + ofToString(i), false );
gui.add( toggle );
toggle->addListener( this, &ofApp::onToggle );
}
And the listener function:
void ofApp::onToggle( const void * sender, bool & value )
{
// Warning: The sender is not actualy the ofxToggle, but the ofParameter<bool> used by
// the toggle to carry its value
ofParameter<bool> * p = ( ofParameter<bool> * ) sender;
// Fortunately, the ofParameter<bool> name is the same as the toggle name,
// so we can identify which toggle was used
string toggleName = p->getName();
ofLogNotice() << "The toggle " << toggleName << " is " << ( value ? "selected":"unselected") ;
// If you want the toggle itself
ofxToggle & toggle = gui.getToggle( name );
// Do something...
}
@lilive thanks, that works well for this case but I am still missing a link. I followed @arturo 's advice and used parameter groups, the structure is great and much more manageable for what I am doing - also for the workflow.
As my parameters are now in a separate class I cannot use this method. I run into an inheritance problem I cannot solve-
I think this is better than the previous solution because you can now change the PlotParameters code, without touching ofApp, as long as PlotParameters has an ofEvent called “changed”.
And if you want to pass some values to the listener function, it is also possible. For example if you want to know which PlotParameters has dispatched the event: