mirror of
https://github.com/Kiritow/MiniEngine.git
synced 2024-03-22 13:11:22 +08:00
Rewrite Event Looper
This commit is contained in:
parent
64ec1cfa42
commit
d18fcf385d
@ -20,32 +20,140 @@ int PushEvent(const Event& refEvent)
|
||||
return SDL_PushEvent(const_cast<Event*>(&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<int(Looper&,Event&)>& event_callback)
|
||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Looper&,Event&)>& 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<int(Event&)>& event_callback)
|
||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Event&)>& 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<void(Looper&,Event&)>& event_callback)
|
||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Looper&)>& 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<void(Event&)>& event_callback)
|
||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int()>& 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<void(Looper&,Event&)>& event_callback)
|
||||
{
|
||||
return add(event_type,std::function<int(Looper&,Event&)>([&](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;}));
|
||||
}
|
||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Event&)>& event_callback)
|
||||
{
|
||||
return add(event_type,std::function<int(Looper&,Event&)>([&](Looper& lp,Event& ev)->int{event_callback(ev); return 0;}));
|
||||
}
|
||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Looper&)>& event_callback)
|
||||
{
|
||||
return add(event_type,std::function<int(Looper&,Event&)>([&](Looper& lp,Event& ev)->int{event_callback(lp); return 0;}));
|
||||
}
|
||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void()>& event_callback)
|
||||
{
|
||||
return add(event_type,std::function<int(Looper&,Event&)>([&](Looper& lp,Event& ev)->int{event_callback();return 0;}));
|
||||
}
|
||||
|
||||
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<int(Looper&,Event&)>>& event_callback)
|
||||
{
|
||||
return add(event_callback.first,event_callback.second);
|
||||
}
|
||||
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<int(Event&)>>& event_callback)
|
||||
{
|
||||
return add(event_callback.first,event_callback.second);
|
||||
}
|
||||
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<int(Looper&)>>& event_callback)
|
||||
{
|
||||
return add(event_callback.first,event_callback.second);
|
||||
}
|
||||
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<int()>>& event_callback)
|
||||
{
|
||||
return add(event_callback.first,event_callback.second);
|
||||
}
|
||||
|
||||
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<void(Looper&,Event&)>>& event_callback)
|
||||
{
|
||||
return add(event_callback.first,event_callback.second);
|
||||
}
|
||||
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<void(Event&)>>& event_callback)
|
||||
{
|
||||
return add(event_callback.first,event_callback.second);
|
||||
}
|
||||
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<void(Looper&)>>& event_callback)
|
||||
{
|
||||
return add(event_callback.first,event_callback.second);
|
||||
}
|
||||
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<void()>>& 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;
|
||||
}
|
||||
|
@ -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<int(Looper&,Event&)>& 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<int(Event&)>& event_callback);
|
||||
/// If Callback has no return (void), then we assume it return 0.
|
||||
void add(decltype(Event::type) event_type,const std::function<void(Looper&,Event&)>& 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<void(Event&)>& event_callback);
|
||||
LooperID add(_SDLEventType_ event_type,const std::function<int(Looper&,Event&)>& event_callback);
|
||||
LooperID add(_SDLEventType_ event_type,const std::function<int(Event&)>& event_callback);
|
||||
LooperID add(_SDLEventType_ event_type,const std::function<int(Looper&)>& event_callback);
|
||||
LooperID add(_SDLEventType_ event_type,const std::function<int()>& event_callback);
|
||||
|
||||
/// If Callback has no return (void), then we assume it returns 0.
|
||||
LooperID add(_SDLEventType_ event_type,const std::function<void(Looper&,Event&)>& event_callback);
|
||||
LooperID add(_SDLEventType_ event_type,const std::function<void(Event&)>& event_callback);
|
||||
LooperID add(_SDLEventType_ event_type,const std::function<void(Looper&)>& event_callback);
|
||||
LooperID add(_SDLEventType_ event_type,const std::function<void()>& event_callback);
|
||||
|
||||
LooperID operator + (const std::pair<_SDLEventType_,std::function<int(Looper&,Event&)>>& event_callback);
|
||||
LooperID operator + (const std::pair<_SDLEventType_,std::function<int(Event&)>>& event_callback);
|
||||
LooperID operator + (const std::pair<_SDLEventType_,std::function<int(Looper&)>>& event_callback);
|
||||
LooperID operator + (const std::pair<_SDLEventType_,std::function<int()>>& event_callback);
|
||||
|
||||
LooperID operator + (const std::pair<_SDLEventType_,std::function<void(Looper&,Event&)>>& event_callback);
|
||||
LooperID operator + (const std::pair<_SDLEventType_,std::function<void(Event&)>>& event_callback);
|
||||
LooperID operator + (const std::pair<_SDLEventType_,std::function<void(Looper&)>>& event_callback);
|
||||
LooperID operator + (const std::pair<_SDLEventType_,std::function<void()>>& 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<decltype(Event::type),std::list<std::function<int(Looper&,Event&)>>> _evmap;
|
||||
std::map<_SDLEventType_,std::list<std::pair<int,std::function<int(Looper&,Event&)>>>> _evmap;
|
||||
int _loop_cnt;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user