
#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

//---------------------------------------------------------------------

#include <EventManager.h>

//---------------------------------------------------------------------

EventManager* EventManager::instance = NULL;

//---------------------------------------------------------------------

class MyEventReceiver : public IEventReceiver
{
public:
	MyEventReceiver(EventManager* parManager)
	{
		manager = parManager;
	}

    // here we get the events
	virtual bool OnEvent(const SEvent& event)
	{
	    if(event.EventType == EET_KEY_INPUT_EVENT)
	    {
       	    if(event.KeyInput.PressedDown)
            {
                manager->keys[event.KeyInput.Key] = true;
            }
            else if(!event.KeyInput.PressedDown)
            {
                manager->keys[event.KeyInput.Key] = false;
            }
        }
        else if(event.EventType == EET_MOUSE_INPUT_EVENT)
        {
            if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
                manager->mouseLeft = true;
            else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
                manager->mouseRight = true;
            else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
                manager->mouseMiddle = true;
            else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
                manager->mouseLeft = false;
            else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
                manager->mouseRight = false;
            else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
                manager->mouseMiddle = false;
		    else if(event.MouseInput.Event == EMIE_MOUSE_WHEEL)
            {
                if (event.MouseInput.Wheel>0)
	       	        manager->mouseWheel++;
	   	        else
				    manager->mouseWheel--;
            }

            if(event.MouseInput.Event == EMIE_MOUSE_MOVED)
            {
                manager->mouseDX = event.MouseInput.X - manager->mouseX;
                manager->mouseDY = event.MouseInput.Y - manager->mouseY;
                manager->mouseX = event.MouseInput.X;
                manager->mouseY = event.MouseInput.Y;
            }
        }
        return false;
    }
private:
	EventManager* manager;
};

//---------------------------------------------------------------------

EventManager* EventManager::getInstance(irr::IrrlichtDevice* parDevice)
{
	if (instance == NULL)
	{
		instance = new EventManager(parDevice);
	}
	return instance;
}

//---------------------------------------------------------------------

EventManager::EventManager(irr::IrrlichtDevice* parDevice)
{
	device = parDevice;
	if (parDevice->getEventReceiver() == NULL)
	{
		IEventReceiver* receiver = reinterpret_cast<IEventReceiver*>(new MyEventReceiver(this));
		parDevice->setEventReceiver(receiver);
	}
	for (int i = 0; i < KEY_KEY_CODES_COUNT; ++i)
		keys[i] = false;
}

//---------------------------------------------------------------------
