diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index 4e874c5..92469ca 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -20,32 +20,140 @@ int PushEvent(const Event& refEvent) return SDL_PushEvent(const_cast(&refEvent)); } +bool operator == (const LooperID& a,const LooperID& b) +{ + return a._type_id==b._type_id && a._looper_cnt==b._looper_cnt ; +} Looper::Looper() { _update=_running=true; + _loop_cnt=0; } -void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_back(event_callback); + _evmap[event_type].push_front(std::make_pair(_loop_cnt,event_callback)); + + LooperID id; + id._looper_cnt=_loop_cnt; + id._type_id=event_type; + + ++_loop_cnt; + + return id; } -void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_back([&](Looper& lp,Event& ev)->int{return event_callback(ev);}); + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback(ev);})); + + LooperID id; + id._looper_cnt=_loop_cnt; + id._type_id=event_type; + + ++_loop_cnt; + + return id; } -void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_back([&](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;}); + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback(lp);})); + + LooperID id; + id._looper_cnt=_loop_cnt; + id._type_id=event_type; + + ++_loop_cnt; + + return id; } -void Looper::add(decltype(Event::type) event_type,const std::function& event_callback) +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - _evmap[event_type].push_back([&](Looper& lp,Event& ev)->int{event_callback(ev); return 0;}); + _evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback();})); + + LooperID id; + id._looper_cnt=_loop_cnt; + id._type_id=event_type; + + ++_loop_cnt; + + return id; } + +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + return add(event_type,std::function([&](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;})); +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + return add(event_type,std::function([&](Looper& lp,Event& ev)->int{event_callback(ev); return 0;})); +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + return add(event_type,std::function([&](Looper& lp,Event& ev)->int{event_callback(lp); return 0;})); +} +LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) +{ + return add(event_type,std::function([&](Looper& lp,Event& ev)->int{event_callback();return 0;})); +} + +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} + +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} +LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) +{ + return add(event_callback.first,event_callback.second); +} + + +bool Looper::remove(const LooperID& looperid) +{ + for(auto beginIter=_evmap[looperid._type_id].begin(), + endIter=_evmap[looperid._type_id].end(), + iter=beginIter; + iter!=endIter; + ++iter) + { + if(iter->first==looperid._looper_cnt) + { + _evmap[looperid._type_id].erase(iter); + return true; + } + } + + return false; +} + void Looper::dispatch() { - for(auto callback:_evmap[_e.type]) + for(auto callbackPack:_evmap[_e.type]) { - if(callback(*this,_e)) break; + if(callbackPack.second(*this,_e)) break; } } void Looper::run() @@ -82,4 +190,5 @@ void Looper::reset() _running=true; _update=true; _evmap.clear(); + _loop_cnt=0; } diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index aa3a4f6..85ac06f 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -10,18 +10,45 @@ int WaitEvent(Event& refEvent); int WaitEventTimeout(Event& refEvent,int ms); int PushEvent(const Event& refEvent); +typedef struct +{ + decltype(Event::type) _type_id; + int _looper_cnt; +}LooperID; + +bool operator == (const LooperID&,const LooperID&); + class Looper { public: + typedef decltype(Event::type) _SDLEventType_; Looper(); /// If Callback does not return 0, then stop transferring this event. - void add(decltype(Event::type) event_type,const std::function& event_callback); - /// If Callback does not return 0, then stop transferring this event. (Discards Looper) - void add(decltype(Event::type) event_type,const std::function& event_callback); - /// If Callback has no return (void), then we assume it return 0. - void add(decltype(Event::type) event_type,const std::function& event_callback); - /// If Callback has no return (void), then we assume it return 0. (Discards Looper) - void add(decltype(Event::type) event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + + /// If Callback has no return (void), then we assume it returns 0. + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + LooperID add(_SDLEventType_ event_type,const std::function& event_callback); + + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + LooperID operator + (const std::pair<_SDLEventType_,std::function>& event_callback); + + bool remove(const LooperID& looperid); + + bool operator - (const LooperID& looperid); + void dispatch(); void run(); Event GetLastEvent(); @@ -32,5 +59,6 @@ public: private: Event _e; bool _running,_update; - std::map>> _evmap; + std::map<_SDLEventType_,std::list>>> _evmap; + int _loop_cnt; };