Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
10 changes: 10 additions & 0 deletions examples/events/notifyEventExample/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "ofApp.h"
#include "ofMain.h"

int main() {

ofGLWindowSettings settings;
auto window = ofCreateWindow(settings);
ofRunApp(window, std::make_shared<ofApp>());
ofRunMainLoop();
}
52 changes: 52 additions & 0 deletions examples/events/notifyEventExample/src/ofApp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "ofApp.h"

void ofApp::update() {

// you can check the state of a specific ofParameter
// (wrapper around it's own "hidden Event")
// no listeners/callbacks are required

if (size.didNotify()) {
ofLogNotice("new size") << size;
}

if (refresh.didNotify()) {
color = ofColor::fromHsb(ofRandom(360), 128, 128);
ofLogNotice("boosch") << color;
}

if (animate.didNotify()) { // this is the ofAbstractParameter::did_notify()
if (animate != animating) { // sanity check
animating = animate;
if (animating) {
ofLogNotice("stop animation");
} else {
ofLogNotice("begin animation");
size = 0;
}
}
}

if (auto what = params.parameterChangedE().did_notify()) { // this is the Events::did_notify()
// something happended in params, but you don't know what
}
}

void ofApp::draw() {
ofBackground(0);
if (animating) {
ofSetColor(color);
ofDrawCircle(ofGetWidth() / 2, ofGetHeight() / 2, size);
size = size + 1;
if (size > 100) size -= 100;
}
ofDrawBitmapString("<a> to toggle automation", 10, 10);
ofDrawBitmapString("<s> to randomize size", 10, 20);
ofDrawBitmapString("<c> to change color", 10, 30);
}

void ofApp::keyPressed(int key) {
if (key == 'a') animate = !animate;
if (key == 'r') size = ofRandom(50, 100);
if (key == 'c') refresh.trigger();
}
22 changes: 22 additions & 0 deletions examples/events/notifyEventExample/src/ofApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "ofMain.h"

// note: this example, inspired by simpleEvents, is kept voluntarily
// as small as possible to give a sense of the bare minimum requirements

class ofApp : public ofBaseApp {

ofParameter<bool> animate { "animate", false };
ofParameter<void> refresh { "refresh" };
ofParameter<float> size { "size", 1.0f, 0.0f, 100.0f };
ofParameterGroup params { "myCallbacksAndSettings", animate, refresh, size };

bool animating { false };
ofColor color { ofColor::white };

public:
void update() override;
void draw() override;
void keyPressed(int key) override;
};
84 changes: 47 additions & 37 deletions libs/openFrameworks/events/ofEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -563,33 +563,38 @@ class ofEvent: public of::priv::BaseEvent<of::priv::Function<T,Mutex>,Mutex>{
ofEvent<T,Mutex>::self->remove(*make_function(std::function<typename of::priv::callable_traits<TFunction>::function_type>(function), priority)->id);
}

inline bool notify(const void* sender, T & param){
if(ofEvent<T,Mutex>::self->enabled && !ofEvent<T,Mutex>::self->functions.empty()){
std::unique_lock<Mutex> lck(ofEvent<T,Mutex>::self->mtx);
auto functions_copy = ofEvent<T,Mutex>::self->functions;
lck.unlock();
for(auto & f: functions_copy){
if(f->notify(sender,param)){
return true;
}
}
/// \brief checks the state of the event
/// \returns true if the Event's state was notified since the last check
bool didNotify() {
if (notified_.load(std::memory_order_relaxed)) {
notified_.store(false, std::memory_order_seq_cst);
return true;
} else {
return false;
}
return false;
}
std::atomic<bool> notified_ { false };

inline bool notify(T & param){
if(ofEvent<T,Mutex>::self->enabled && !ofEvent<T,Mutex>::self->functions.empty()){
std::unique_lock<Mutex> lck(ofEvent<T,Mutex>::self->mtx);
auto functions_copy = ofEvent<T,Mutex>::self->functions;
lck.unlock();
for(auto & f: functions_copy){
if(f->notify(nullptr,param)){
return true;
inline bool notify(const void* sender, T & param) {
if (ofEvent<T,Mutex>::self->enabled) {
notified_.store(true, std::memory_order_relaxed);
if (!ofEvent<T,Mutex>::self->functions.empty()) {
std::unique_lock<Mutex> lck(ofEvent<T,Mutex>::self->mtx);
auto functions_copy = ofEvent<T,Mutex>::self->functions;
lck.unlock();
for (auto & f: functions_copy) {
if (f->notify(sender,param)) {
return true;
}
}
}
}
return false;
}

inline bool notify(T & param){
return this->notify(nullptr, param);
}
};


Expand Down Expand Up @@ -721,32 +726,37 @@ class ofEvent<void,Mutex>: public of::priv::BaseEvent<of::priv::Function<void,Mu
ofEvent<void,Mutex>::self->remove(*make_function(std::function<typename of::priv::callable_traits<TFunction>::function_type>(function),priority)->id);
}

/// \brief checks the state of the event
/// \returns true if the Event's state was notified since the last check
bool didNotify() {
if (notified_.load(std::memory_order_relaxed)) {
notified_.store(false, std::memory_order_seq_cst);
return true;
} else {
return false;
}
}
std::atomic<bool> notified_;

bool notify(const void* sender){
if(ofEvent<void,Mutex>::self->enabled && !ofEvent<void,Mutex>::self->functions.empty()){
std::unique_lock<Mutex> lck(ofEvent<void,Mutex>::self->mtx);
auto functions_copy = ofEvent<void,Mutex>::self->functions;
lck.unlock();
for(auto & f: functions_copy){
if(f->notify(sender)){
return true;
if(ofEvent<void,Mutex>::self->enabled) {
notified_.store(true, std::memory_order_relaxed);
if (!ofEvent<void,Mutex>::self->functions.empty()) {
std::unique_lock<Mutex> lck(ofEvent<void,Mutex>::self->mtx);
auto functions_copy = ofEvent<void,Mutex>::self->functions;
lck.unlock();
for (auto & f: functions_copy) {
if (f->notify(sender)) {
return true;
}
}
}
}
return false;
}

bool notify(){
if(ofEvent<void,Mutex>::self->enabled && !ofEvent<void,Mutex>::self->functions.empty()){
std::unique_lock<Mutex> lck(ofEvent<void,Mutex>::self->mtx);
auto functions_copy = ofEvent<void,Mutex>::self->functions;
lck.unlock();
for(auto & f: functions_copy){
if(f->notify(nullptr)){
return true;
}
}
}
return false;
return this->notify(nullptr);
}
};

Expand Down
Loading