/*
 TUIO C++ Library for OpenFrameworks
 http://www.openframeworks.cc
 
 Copyright (c) 2008 by Matthias Dörfelt based on the Classes by Martin Kaltenbrunner
 which can be found at http://reactivision.sourceforge.net/
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef _TUIO_EVENTS
#define _TUIO_EVENTS

#include "Poco/BasicEvent.h"
#include "Poco/Delegate.h"
#include "ofTypes.h"
#include "ofMain.h"


class ofxTuioEventArgs: public ofEventArgs {
public:
    int x;
    int y;
	float xspeed;
	float yspeed;
	long id;
};


class ofxTuioEventListener {
	
protected:
	
  	virtual void tuioMoved(int x, int y, float xspeed, float yspeed, long id){};
	virtual void tuioDown(int x, int y, float xspeed, float yspeed, long id){};
    virtual void tuioUp(int x, int y, float xspeed, float yspeed, long id){};
	
	void tuioMoved(const void* sender, ofxTuioEventArgs& eventArgs){
		tuioMoved(eventArgs.x, eventArgs.y, eventArgs.xspeed, eventArgs.yspeed, (long)eventArgs.id);
	}
    
	void tuioDown(const void* sender, ofxTuioEventArgs& eventArgs){
		tuioDown(eventArgs.x, eventArgs.y, eventArgs.xspeed, eventArgs.yspeed, (long)eventArgs.id);
	}
	
	void tuioUp( const void* sender, ofxTuioEventArgs& eventArgs){
		tuioUp(eventArgs.x, eventArgs.y, eventArgs.xspeed, eventArgs.yspeed, (long)eventArgs.id);
	}
	
	
};



class ofxTuioEventManager {
	
public:
	
    ofxTuioEventArgs tuioEventArgs;
	
	
	void addListener(ofxTuioEventListener* listener){
		addMovedListener(listener);
		addDownListener(listener);
		addUpListener(listener);
	}
	
	void removeListener(ofxTuioEventListener* listener){
		removeMovedListener(listener);
		removeDownListener(listener);
		removeUpListener(listener);
    }  
	
	
	void addMovedListener(ofxTuioEventListener* listener){
		tuioMovedEvent += Poco::Delegate<ofxTuioEventListener, ofxTuioEventArgs>(listener, &ofxTuioEventListener::tuioMoved);
	}
	
	void addDownListener(ofxTuioEventListener* listener){
		tuioDownEvent += Poco::Delegate<ofxTuioEventListener, ofxTuioEventArgs>(listener, &ofxTuioEventListener::tuioDown);
	}
	
	void addUpListener(ofxTuioEventListener* listener){
		tuioUpEvent += Poco::Delegate<ofxTuioEventListener, ofxTuioEventArgs>(listener, &ofxTuioEventListener::tuioUp);
	}
	
	
	void removeMovedListener(ofxTuioEventListener* listener){
		tuioMovedEvent -= Poco::Delegate<ofxTuioEventListener, ofxTuioEventArgs>(listener, &ofxTuioEventListener::tuioMoved);
	}
	
	void removeDownListener(ofxTuioEventListener* listener){
		tuioDownEvent -= Poco::Delegate<ofxTuioEventListener, ofxTuioEventArgs>(listener, &ofxTuioEventListener::tuioDown);
	}
	
	void removeUpListener(ofxTuioEventListener* listener){
		tuioUpEvent -= Poco::Delegate<ofxTuioEventListener, ofxTuioEventArgs>(listener, &ofxTuioEventListener::tuioUp);
	}
	
	
	void notifyMoved(void* sender, ofxTuioEventArgs& eventArgs){
		tuioMovedEvent.notify(sender, eventArgs);
	}
	
	void notifyDown(void* sender, ofxTuioEventArgs& eventArgs){
		tuioDownEvent.notify(sender, eventArgs);
	}
	
	void notifyUp(void* sender, ofxTuioEventArgs& eventArgs){
		tuioUpEvent.notify(sender, eventArgs);
	}
	
	
private:
	
	Poco::BasicEvent<ofxTuioEventArgs> tuioMovedEvent;
	Poco::BasicEvent<ofxTuioEventArgs> tuioDownEvent;
	Poco::BasicEvent<ofxTuioEventArgs> tuioUpEvent;
	
};

extern ofxTuioEventManager ofxTuioEvents;

#endif