/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


#ifndef	__NUIEVENTHANDLER__
#define	__NUIEVENTHANDLER__


		#include "script/script_vm.h"
		#include "script/script_object.h"
		#include "memory/nauto_ptr.h"


namespace GS {
	namespace S2D {

enum EventCode
{
	Event_None		=	0,

	Event_CursorDown,
	Event_CursorHit,
	Event_CursorUp,

	Event_CursorEnter,
	Event_CursorLeave,
	Event_CursorMove,
};

/// 2D event handler.
class	EventHandler
{
		friend class EventTable;

protected:

		EventCode		code;			
		AutoPtr <Script::Object> handler, context;

public:

		EventCode		GetCode() const { return code; }
		Script::Object *GetHandler() { return handler; }
		Script::Object *GetContext() { return context; }

		EventHandler(EventCode c = Event_None) : code(c) {}
};

/*!
	@short	UI event table.
	@author	Emmanuel Julien (ejulien@nworks.fr)
*/
class	EventTable
{
		bool			enabled;
		AutoList <EventHandler *> handler_list;

public:

		void			Reset() { handler_list.Clear(); }

		/// Returns true if the table has any handler set.
		bool			HasHandler() const { return handler_list.GetCount() > 0; }

		EventHandler	*GetHandler(EventCode) const;
		EventHandler	*SetHandler(EventCode, Script::Object *handler, Script::Object *context = 0);
		void			DeleteHandler(EventCode);

		bool			HasHandler(EventCode code) const { return asbool(GetHandler(code)); }
		bool			HasHandlerContext(EventCode) const;

		void			Enable(bool v) { enabled = v; }
		bool			IsEnabled() const { return enabled; }

		EventTable() : enabled(true) {}
};

	} // S2D
} // GS


#endif	// __NUIEVENTHANDLER__
