|
|
@ -79,13 +79,21 @@ namespace CoreEvent
|
|
|
|
class CallbackCollection // todo: add sort by dependencies
|
|
|
|
class CallbackCollection // todo: add sort by dependencies
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
public:
|
|
|
|
typedef std::vector<std::pair<sol::environment, sol::function>> Container;
|
|
|
|
struct CBType
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
sol::environment env;
|
|
|
|
|
|
|
|
sol::function fn;
|
|
|
|
|
|
|
|
bool needsOldState;
|
|
|
|
|
|
|
|
CBType(sol::environment _env, sol::function _fn, bool _needsOldState): env(_env), fn(_fn), needsOldState(_needsOldState) {}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef std::vector<CBType> Container;
|
|
|
|
typedef Container::iterator Iterator;
|
|
|
|
typedef Container::iterator Iterator;
|
|
|
|
typedef Container::const_iterator CIterator;
|
|
|
|
typedef Container::const_iterator CIterator;
|
|
|
|
|
|
|
|
|
|
|
|
template<typename> struct type_tag {};
|
|
|
|
template<typename> struct type_tag {};
|
|
|
|
|
|
|
|
|
|
|
|
void push(sol::environment &env, sol::function &func) { functions.emplace_back(env, func); }
|
|
|
|
void push(sol::environment &env, sol::function &func, bool needsOldState) { functions.emplace_back(env, func, needsOldState); }
|
|
|
|
CIterator begin() const { return functions.begin(); }
|
|
|
|
CIterator begin() const { return functions.begin(); }
|
|
|
|
CIterator end() const { return functions.end(); }
|
|
|
|
CIterator end() const { return functions.end(); }
|
|
|
|
|
|
|
|
|
|
|
@ -93,6 +101,30 @@ public:
|
|
|
|
bool isStoped() const {return _stop;}
|
|
|
|
bool isStoped() const {return _stop;}
|
|
|
|
CIterator stopedAt() const { return lastCalled; }
|
|
|
|
CIterator stopedAt() const { return lastCalled; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename... Args, typename OldData>
|
|
|
|
|
|
|
|
void callWOld(const OldData &oldData, Args&&... args)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
lastCalled = functions.end();
|
|
|
|
|
|
|
|
_stop = false;
|
|
|
|
|
|
|
|
for (CIterator iter = functions.begin(); iter != functions.end(); ++iter)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!_stop)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!iter->needsOldState)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
iter->fn.call(std::forward<Args>(args)...);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->fn.call(std::forward<Args>(args)..., oldData);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
lastCalled = iter;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
template<typename... Args>
|
|
|
|
void call(type_tag<void>, Args&&... args)
|
|
|
|
void call(type_tag<void>, Args&&... args)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -101,14 +133,13 @@ public:
|
|
|
|
for (CIterator iter = functions.begin(); iter != functions.end(); ++iter)
|
|
|
|
for (CIterator iter = functions.begin(); iter != functions.end(); ++iter)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!_stop)
|
|
|
|
if (!_stop)
|
|
|
|
iter->second.call(std::forward<Args>(args)...);
|
|
|
|
iter->fn.call(std::forward<Args>(args)...);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lastCalled = iter;
|
|
|
|
lastCalled = iter;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename R, typename... Args>
|
|
|
|
template<typename R, typename... Args>
|
|
|
@ -121,7 +152,7 @@ public:
|
|
|
|
for (CIterator iter = functions.begin(); iter != functions.end(); ++iter)
|
|
|
|
for (CIterator iter = functions.begin(); iter != functions.end(); ++iter)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (!_stop)
|
|
|
|
if (!_stop)
|
|
|
|
ret = iter->second.call(std::forward<Args>(args)...);
|
|
|
|
ret = iter->fn.call(std::forward<Args>(args)...);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
lastCalled = iter;
|
|
|
|
lastCalled = iter;
|
|
|
@ -145,17 +176,26 @@ public:
|
|
|
|
explicit EventController(LuaState *luaCtrl);
|
|
|
|
explicit EventController(LuaState *luaCtrl);
|
|
|
|
typedef std::unordered_map<int, CallbackCollection> Container;
|
|
|
|
typedef std::unordered_map<int, CallbackCollection> Container;
|
|
|
|
|
|
|
|
|
|
|
|
void registerEvent(int event, sol::environment &env, sol::function& func);
|
|
|
|
void registerEvent(int event, sol::environment &env, sol::function& func, bool needsOldState);
|
|
|
|
CallbackCollection& GetEvents(Event event);
|
|
|
|
CallbackCollection& GetEvents(Event event);
|
|
|
|
Event createEvent();
|
|
|
|
Event createEvent();
|
|
|
|
void raiseEvent(Event id, sol::table data, const std::string &moduleName = "");
|
|
|
|
void raiseEvent(Event id, sol::table data, const std::string &moduleName = "");
|
|
|
|
void stop(int event);
|
|
|
|
void stop(int event);
|
|
|
|
|
|
|
|
|
|
|
|
template<Event event, typename R = void, typename... Args>
|
|
|
|
template<Event event, bool canProvideOldState = false, typename R = void, typename... Args>
|
|
|
|
R Call(Args&&... args)
|
|
|
|
R Call(Args&&... args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
if(canProvideOldState)
|
|
|
|
|
|
|
|
events.at(event).callWOld(std::forward<Args>(args)...);
|
|
|
|
|
|
|
|
else
|
|
|
|
return events.at(event).call(CallbackCollection::type_tag<R>{}, std::forward<Args>(args)...);
|
|
|
|
return events.at(event).call(CallbackCollection::type_tag<R>{}, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<Event event, bool canProvideOldState = false, typename R = void>
|
|
|
|
|
|
|
|
R Call()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return events.at(event).call(CallbackCollection::type_tag<R>{});
|
|
|
|
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
Container events;
|
|
|
|
Container events;
|
|
|
|
Event lastEvent = CoreEvent::LAST;
|
|
|
|
Event lastEvent = CoreEvent::LAST;
|
|
|
|