Skip to content

Instantly share code, notes, and snippets.

@jvcleave
Last active Jan 6, 2016
Embed
What would you like to do?
SyncedParameter concept
template<typename T>
class SyncedParameter
{
public:
ofParameter<T> parameter;
T value;
string name;
bool hasAddedUpdateListener;
SyncedParameter()
{
name = "UNDEFINED";
hasAddedUpdateListener = false;
T temp;
value = temp;
parameter.set(name, value);
}
template <typename ArgumentsType, class ListenerClass>
void set(string name_,
T value_,
ListenerClass * listener,
void (ListenerClass::*listenerMethod)(ArgumentsType&),
bool doUpdate = false)
{
set(name_, value_, doUpdate);
parameter.addListener(listener, listenerMethod);
}
void set(string name_, T value_, bool doUpdate = false)
{
if(!name_.empty())
{
name = name_;
}
value = value_;
parameter.set(name, value);
if(doUpdate)
{
startUpdating();
}
}
void startUpdating()
{
if(!hasAddedUpdateListener)
{
hasAddedUpdateListener = true;
ofAddListener(ofEvents().update, this, &SyncedParameter::onUpdate);
}
}
void onUpdate(ofEventArgs& event)
{
update();
}
bool update()
{
bool didChange =false;
//TODO: better way than to serialize?
if(ofToString(value) != parameter.toString())
{
parameter = value;
didChange = true;
}
return didChange;
}
};
SyncedParameter<float> timeElapsed;
SyncedParameter<int> currentFrame;
setup...
timeElapsed.set("timeElapsed", 10.f, true);
currentFrame.set("currentFrame", 0, this, &ofApp::onCurrentFrameUpdate, true);
update...
timeElapsed.value = ofGetElapsedTimef();
currentFrame.value = ofGetFrameNum();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment