Static event handlers can create/register/unregister other static event handlers.

This commit is contained in:
ZZYZX 2017-01-30 09:28:27 +02:00
parent e8a0eda476
commit c7e3ff2356
2 changed files with 46 additions and 1 deletions

View file

@ -289,7 +289,30 @@ DEFINE_ACTION_FUNCTION(DEventHandler, CreateOnce)
// check if there are already registered handlers of this type.
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
if (handler->GetClass() == t) // check precise class
ACTION_RETURN_OBJECT(nullptr);
ACTION_RETURN_OBJECT(handler);
// generate a new object of this type.
ACTION_RETURN_OBJECT(t->CreateNew());
}
// for static
DEFINE_ACTION_FUNCTION(DStaticEventHandler, Create)
{
PARAM_PROLOGUE;
PARAM_CLASS(t, DStaticEventHandler);
// static handlers can create any type of object.
// generate a new object of this type.
ACTION_RETURN_OBJECT(t->CreateNew());
}
DEFINE_ACTION_FUNCTION(DStaticEventHandler, CreateOnce)
{
PARAM_PROLOGUE;
PARAM_CLASS(t, DStaticEventHandler);
// static handlers can create any type of object.
// check if there are already registered handlers of this type.
for (DStaticEventHandler* handler = E_FirstEventHandler; handler; handler = handler->next)
if (handler->GetClass() == t) // check precise class
ACTION_RETURN_OBJECT(handler);
// generate a new object of this type.
ACTION_RETURN_OBJECT(t->CreateNew());
}
@ -320,6 +343,20 @@ DEFINE_ACTION_FUNCTION(DEventHandler, Unregister)
ACTION_RETURN_BOOL(E_UnregisterHandler(handler));
}
DEFINE_ACTION_FUNCTION(DStaticEventHandler, Register)
{
PARAM_PROLOGUE;
PARAM_OBJECT(handler, DStaticEventHandler);
ACTION_RETURN_BOOL(E_RegisterHandler(handler));
}
DEFINE_ACTION_FUNCTION(DStaticEventHandler, Unregister)
{
PARAM_PROLOGUE;
PARAM_OBJECT(handler, DStaticEventHandler);
ACTION_RETURN_BOOL(E_UnregisterHandler(handler));
}
#define DEFINE_EVENT_HANDLER(cls, funcname, args) DEFINE_ACTION_FUNCTION(cls, funcname) \
{ \
PARAM_SELF_PROLOGUE(cls); \

View file

@ -1,5 +1,13 @@
class StaticEventHandler : Object native
{
// static event handlers CAN register other static event handlers.
// unlike EventHandler.Create that will not create them.
protected static native StaticEventHandler Create(class<StaticEventHandler> type);
protected static native StaticEventHandler CreateOnce(class<StaticEventHandler> type);
protected static native bool Register(StaticEventHandler handler);
protected static native bool Unregister(StaticEventHandler handler);
virtual native void WorldLoaded();
virtual native void WorldUnloaded();
virtual native void WorldThingSpawned();