Skip to content

Commit

Permalink
Fixed critical bug in event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobarcelos committed Nov 27, 2015
1 parent 82de61d commit df53a82
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/Event.cpp
Expand Up @@ -3,20 +3,22 @@
Event::Event(){}
Event::~Event(){}
void Event::add(Input* input, TypedInputHandler handler){
int pos = position(handler);
int pos = position(input, handler);
if(pos != -1) return;
typedInputs.push(input);
typedInputHandlers.push(handler);
}
void Event::remove(TypedInputHandler handler){
int pos = position(handler);
void Event::remove(Input* input, TypedInputHandler handler){
int pos = position(input, handler);
if(pos == -1) return;
typedInputs.erase(pos);
typedInputHandlers.erase(pos);
}
int Event::position(TypedInputHandler handler){
int Event::position(Input* input, TypedInputHandler handler){
for(int i=0; i<typedInputHandlers.size(); i++){
if(typedInputHandlers[i] == handler) return i;
if(typedInputs[i] == input && typedInputHandlers[i] == handler) {
return i;
}
}
return -1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Event.h
Expand Up @@ -14,11 +14,11 @@ class Event {
typedef void (Input::*TypedInputHandler)(float value);

void add(Input* input, TypedInputHandler handler);
void remove(TypedInputHandler handler);
void remove(Input* input, TypedInputHandler handler);
void dispatch(float &value);

private:
int position(TypedInputHandler handler);
int position(Input* input, TypedInputHandler handler);

VectorTypedInputHandler typedInputHandlers;
VectorInputsPointer typedInputs;
Expand Down
2 changes: 1 addition & 1 deletion src/Input.cpp
Expand Up @@ -55,7 +55,7 @@ void Input::onOutputChange(float value){
}
void Input::clearOutput(){
if(output){
output->event.remove(&Input::onOutputChange);
output->event.remove(this, &Input::onOutputChange);
output = NULL;
}
}

0 comments on commit df53a82

Please sign in to comment.