Skip to content

Instantly share code, notes, and snippets.

@moebiussurfing

moebiussurfing/ofApp.cpp

Last active Jan 10, 2021
Embed
What would you like to do?
openFrameworks | ofParameters callbacks using modern lambdas. Exclusive toggle bool's behaviour. ofParameterGroup different types filtetering / casting too.
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetFrameRate(30);
listenerMouse = ofEvents().mousePressed.newListener([this](ofMouseEventArgs&mouse) {
ofLogNotice() << __FUNCTION__ << "mouse:"<<ofToString(mouse);
});
//-
// group 1
g1.add(f);
g1.add(i1);
g1.add(i2);
g1.add(b);
listenerGroup1 = g1.parameterChangedE().newListener([&](ofAbstractParameter&p) {
ofLogNotice() << __FUNCTION__ << "listenerGroup1: " << p.getName();
// get the values doing casting. we check the type first
if(p.type() == typeid(ofParameter<float>).name() )
{
ofParameter<float> pm = p.cast<float>();
ofLogNotice() << " type:float value:" << pm.get();
}
else if(p.type() == typeid(ofParameter<int>).name() )
{
ofParameter<int> pm = p.cast<int>();
ofLogNotice() << " type:int value:" << pm.get();
}
else if(p.type() == typeid(ofParameter<bool>).name() )
{
ofParameter<bool> pm = p.cast<bool>();
ofLogNotice() << " type:bool state: " << (pm.get() ? " TRUE":" FALSE");
}
});
gui1.setup("group1");
gui1.add(g1);
//gui1.getFloatSlider("f").setUpdateOnReleaseOnly(true);//custom behaviour. not working..
//-
// group 2
listenerGroup2.push(checkRedCircle.newListener(this, &ofApp::checkPressed));
listenerGroup2.push(checkBlueCircle.newListener(this, &ofApp::checkPressed));
listenerGroup2.push(checkGreenCircle.newListener(this, &ofApp::checkPressed));
listenerGroup2.push(checkOrangeCircle.newListener(this, &ofApp::checkPressed));
gui2.setPosition(gui1.getShape().getTopRight()+ofPoint(10,0));// layout
}
//--------------------------------------------------------------
void ofApp::draw(){
gui1.draw();
gui2.draw();
}
//--------------------------------------------------------------
void ofApp::checkPressed(const void * sender, bool & value){
if(attendingEvent){
return;
}
attendingEvent = true;
auto param = (ofParameter<bool>*)sender;
ofLogNotice() << "checkPressed: name:"
<< (param->getName()) << " "
<< (param->get() ? "TRUE":"FALSE");
if(value == false)
{
// Don't let this parameter be deactivated so there's always
// one active
param->set(true);
}
else
{
if(param->isReferenceTo(checkRedCircle)){
checkBlueCircle.set(false);
checkGreenCircle.set(false);
checkOrangeCircle.set(false);
}
if(param->isReferenceTo(checkBlueCircle)){
checkRedCircle.set(false);
checkGreenCircle.set(false);
checkOrangeCircle.set(false);
}
if(param->isReferenceTo(checkGreenCircle)){
checkRedCircle.set(false);
checkBlueCircle.set(false);
checkOrangeCircle.set(false);
}
if(param->isReferenceTo(checkOrangeCircle)){
checkRedCircle.set(false);
checkBlueCircle.set(false);
checkGreenCircle.set(false);
}
}
attendingEvent = false;
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp{
public:
void setup();
void draw();
ofEventListener listenerMouse;// debug mouse clicks
//-
// group 1: different types params
ofParameterGroup g1{"group1"};
ofParameter<float> f{"f",0,0,1};
ofParameter<int> i1{"i1",0,-10,10};
ofParameter<int> i2{"i2",0,-10,10};
ofParameter<bool> b{"b", false};
ofEventListener listenerGroup1;
ofxPanel gui1;
//-
//group 2: toggles/bool exclusive behaviour
ofParameter<bool> checkRedCircle{"red", false};
ofParameter<bool> checkBlueCircle{"blue", false};
ofParameter<bool> checkGreenCircle{"green", false};
ofParameter<bool> checkOrangeCircle{"orange", false};
ofParameterGroup g2{
"group2",
checkRedCircle,
checkBlueCircle,
checkGreenCircle,
checkOrangeCircle,
};
ofEventListeners listenerGroup2;
void checkPressed(const void * sender, bool & value);
bool attendingEvent;// callback blocker
ofxPanel gui2{g2};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment