-
-
Save moebiussurfing/729ca0efb497ce7523600dcbd4a5faed to your computer and use it in GitHub Desktop.
ofApp.cpp grouped callbacks pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ofApp.h | |
ofParameterGroup params{"Params"}; | |
void Changed_Params(ofAbstractParameter &e); | |
ofParameter<bool> bAnimate; | |
ofParameter<void> bRefresh; | |
ofParameter<float> size; | |
bool bAttending = false; // flag for a bit of flow control and avoid "feedback callbacks" exception crashes.. | |
bool bFlagDoRefresh = false // flag to perform long functions out of the callback or only once per frame when overflow calback trigs.. | |
//-- | |
// ofApp.cpp | |
void ofApp::setup() { | |
bAnimate.set("bAnimate", true); | |
bRefresh.set("bRefresh"); | |
size.set("Size", 0.f, 0.f, 1.f); | |
params.setName("myCallbacksAndSettings"); | |
params.add(bAnimate); | |
params.add(bRefresh); | |
params.add(size); | |
ofAddListener(params.parameterChangedE(), this, &ofApp::Changed_Params); // setup() | |
} | |
void ofApp::Changed_Params(ofAbstractParameter &e) | |
{ | |
if(bAttending) return; | |
string name = e.getName(); | |
ofLog() << name << ": " << e; | |
if (name == bRefresh.getName()) | |
{ | |
bFlagDoRefresh = true; | |
// flag to compute reducing commands on the callback. | |
// also limited once per frame. | |
} | |
else if (name == bAnimate.getName()) | |
{ | |
bAttending=true; | |
// perform actions having the callbacks | |
// by-passed to avoid collide/crashes | |
// with other param callbacks. | |
bAttending=false; | |
} | |
else if (name == size.getName()) | |
{ | |
// check correct type for sanety | |
if(e.type() == typeid(ofParameter<float>).name()) | |
{ | |
ofParameter<float> p = e.cast<float>(); | |
ofLog() << p; | |
ofLog() << size; | |
} | |
} | |
} | |
void ofApp::update() | |
{ | |
(bFlagDoRefresh) | |
{ | |
bFlagDoRefresh = false; | |
// ... | |
// compute much stuff here | |
// out of the callback. | |
} | |
} | |
void ofApp::exit() { | |
ofRemoveListener(params.parameterChangedE(), this, &ofApp::Changed_Params); // exit() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment