2017-04-04 09:49:18 +08:00
|
|
|
#include "MiniEngine_Event.h"
|
2017-05-02 12:59:57 +08:00
|
|
|
|
2017-07-01 15:58:34 +08:00
|
|
|
namespace MiniEngine
|
|
|
|
{
|
|
|
|
|
2017-05-02 12:59:57 +08:00
|
|
|
int PollEvent(Event& refEvent)
|
|
|
|
{
|
|
|
|
return SDL_PollEvent(&refEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WaitEvent(Event& refEvent)
|
|
|
|
{
|
|
|
|
return SDL_WaitEvent(&refEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WaitEventTimeout(Event& refEvent,int ms)
|
|
|
|
{
|
|
|
|
return SDL_WaitEventTimeout(&refEvent,ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PushEvent(const Event& refEvent)
|
|
|
|
{
|
|
|
|
return SDL_PushEvent(const_cast<Event*>(&refEvent));
|
|
|
|
}
|
2017-05-02 13:37:03 +08:00
|
|
|
|
2017-05-05 17:28:11 +08:00
|
|
|
void PumpEvents()
|
|
|
|
{
|
|
|
|
SDL_PumpEvents();
|
|
|
|
}
|
|
|
|
|
2017-05-08 15:42:54 +08:00
|
|
|
bool HasEvent(_SDLEventType_ EventType)
|
2017-05-05 17:28:11 +08:00
|
|
|
{
|
|
|
|
return ( SDL_HasEvent(EventType)==SDL_TRUE );
|
|
|
|
}
|
|
|
|
|
2017-05-08 15:42:54 +08:00
|
|
|
bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax)
|
2017-05-05 17:28:11 +08:00
|
|
|
{
|
|
|
|
return ( SDL_HasEvents(EventTypeMin,EventTypeMax)==SDL_TRUE );
|
|
|
|
}
|
|
|
|
|
2017-05-22 19:07:26 +08:00
|
|
|
_SDLEventType_ RegisterEvent(int howMuch)
|
|
|
|
{
|
|
|
|
return SDL_RegisterEvents(howMuch);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValidEventType(_SDLEventType_ EventType)
|
|
|
|
{
|
|
|
|
return (EventType > SDL_FIRSTEVENT) && (EventType < SDL_LASTEVENT);
|
|
|
|
}
|
|
|
|
|
2017-05-02 19:05:48 +08:00
|
|
|
bool operator == (const LooperID& a,const LooperID& b)
|
|
|
|
{
|
|
|
|
return a._type_id==b._type_id && a._looper_cnt==b._looper_cnt ;
|
|
|
|
}
|
2017-05-02 13:37:03 +08:00
|
|
|
|
2017-05-08 14:17:42 +08:00
|
|
|
bool operator != (const LooperID& a,const LooperID& b)
|
|
|
|
{
|
|
|
|
return !(a==b);
|
|
|
|
}
|
|
|
|
|
2017-05-02 13:37:03 +08:00
|
|
|
Looper::Looper()
|
|
|
|
{
|
|
|
|
_update=_running=true;
|
2017-05-02 19:05:48 +08:00
|
|
|
_loop_cnt=0;
|
2017-05-02 20:51:56 +08:00
|
|
|
updater=[](){};
|
2017-05-02 19:05:48 +08:00
|
|
|
}
|
|
|
|
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Looper&,Event&)>& event_callback)
|
|
|
|
{
|
|
|
|
_evmap[event_type].push_front(std::make_pair(_loop_cnt,event_callback));
|
2017-07-01 20:34:34 +08:00
|
|
|
return _getNextID(event_type);
|
2017-05-02 19:05:48 +08:00
|
|
|
}
|
|
|
|
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Event&)>& event_callback)
|
|
|
|
{
|
2017-05-02 22:38:37 +08:00
|
|
|
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(ev);}));
|
2017-07-01 20:34:34 +08:00
|
|
|
return _getNextID(event_type);
|
2017-05-02 19:05:48 +08:00
|
|
|
}
|
|
|
|
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Looper&)>& event_callback)
|
|
|
|
{
|
2017-05-02 22:38:37 +08:00
|
|
|
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(lp);}));
|
2017-07-01 20:34:34 +08:00
|
|
|
return _getNextID(event_type);
|
2017-05-02 13:37:03 +08:00
|
|
|
}
|
2017-05-02 19:05:48 +08:00
|
|
|
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int()>& event_callback)
|
2017-05-02 13:37:03 +08:00
|
|
|
{
|
2017-05-02 22:38:37 +08:00
|
|
|
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback();}));
|
2017-07-01 20:34:34 +08:00
|
|
|
return _getNextID(event_type);
|
2017-05-02 13:37:03 +08:00
|
|
|
}
|
2017-05-02 19:05:48 +08:00
|
|
|
|
|
|
|
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Looper&,Event&)>& event_callback)
|
|
|
|
{
|
2017-05-02 22:38:37 +08:00
|
|
|
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;}));
|
2017-05-02 19:05:48 +08:00
|
|
|
}
|
|
|
|
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Event&)>& event_callback)
|
2017-05-02 13:37:03 +08:00
|
|
|
{
|
2017-05-02 22:38:37 +08:00
|
|
|
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback(ev); return 0;}));
|
2017-05-02 13:37:03 +08:00
|
|
|
}
|
2017-05-02 19:05:48 +08:00
|
|
|
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Looper&)>& event_callback)
|
2017-05-02 13:37:03 +08:00
|
|
|
{
|
2017-05-02 22:38:37 +08:00
|
|
|
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback(lp); return 0;}));
|
2017-05-02 13:37:03 +08:00
|
|
|
}
|
2017-05-02 19:05:48 +08:00
|
|
|
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void()>& event_callback)
|
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback(); return 0;}));
|
2017-05-02 19:05:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<int(Looper&,Event&)>>& event_callback)
|
2017-05-02 13:37:03 +08:00
|
|
|
{
|
2017-05-02 19:05:48 +08:00
|
|
|
return add(event_callback.first,event_callback.second);
|
2017-05-02 13:37:03 +08:00
|
|
|
}
|
2017-05-02 19:05:48 +08:00
|
|
|
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(),
|
2017-07-01 20:34:34 +08:00
|
|
|
endIter=_evmap[looperid._type_id].end(),
|
|
|
|
iter=beginIter;
|
|
|
|
iter!=endIter;
|
|
|
|
++iter)
|
2017-05-02 19:05:48 +08:00
|
|
|
{
|
|
|
|
if(iter->first==looperid._looper_cnt)
|
|
|
|
{
|
|
|
|
_evmap[looperid._type_id].erase(iter);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 20:51:56 +08:00
|
|
|
bool Looper::operator -(const LooperID& looperid)
|
|
|
|
{
|
|
|
|
return remove(looperid);
|
|
|
|
}
|
|
|
|
|
2017-05-02 13:37:03 +08:00
|
|
|
void Looper::dispatch()
|
|
|
|
{
|
2017-05-02 19:05:48 +08:00
|
|
|
for(auto callbackPack:_evmap[_e.type])
|
2017-05-02 13:37:03 +08:00
|
|
|
{
|
2017-05-02 19:05:48 +08:00
|
|
|
if(callbackPack.second(*this,_e)) break;
|
2017-05-02 13:37:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void Looper::run()
|
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
do
|
2017-05-02 13:37:03 +08:00
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
if(_update)
|
|
|
|
{
|
|
|
|
updater();
|
|
|
|
_update=false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 13:37:03 +08:00
|
|
|
while(!_update&&WaitEvent(_e))
|
|
|
|
{
|
|
|
|
dispatch();
|
|
|
|
}
|
|
|
|
}
|
2017-07-01 20:34:34 +08:00
|
|
|
while(_running);
|
2017-05-02 13:37:03 +08:00
|
|
|
}
|
|
|
|
Event Looper::GetLastEvent()
|
|
|
|
{
|
|
|
|
return _e;
|
|
|
|
}
|
|
|
|
void Looper::needupdate()
|
|
|
|
{
|
|
|
|
_update=true;
|
|
|
|
}
|
|
|
|
void Looper::stop()
|
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
_running=false;
|
|
|
|
_update=true;
|
2017-05-02 13:37:03 +08:00
|
|
|
}
|
|
|
|
void Looper::reset()
|
|
|
|
{
|
|
|
|
_running=true;
|
|
|
|
_update=true;
|
|
|
|
_evmap.clear();
|
2017-05-02 19:05:48 +08:00
|
|
|
_loop_cnt=0;
|
2017-05-02 20:51:56 +08:00
|
|
|
updater=[](){};
|
|
|
|
}
|
|
|
|
|
2017-05-02 22:38:37 +08:00
|
|
|
LooperID Looper::_getNextID(const _SDLEventType_& event_type)
|
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
LooperID id;
|
|
|
|
id._looper_cnt = _loop_cnt;
|
|
|
|
id._type_id = event_type;
|
2017-05-02 22:38:37 +08:00
|
|
|
|
2017-07-01 20:34:34 +08:00
|
|
|
++_loop_cnt;
|
|
|
|
return id;
|
2017-05-02 22:38:37 +08:00
|
|
|
}
|
|
|
|
|
2017-05-02 20:51:56 +08:00
|
|
|
Poller::Poller()
|
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
idler=[]() {};
|
2017-05-02 20:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Poller::reset()
|
|
|
|
{
|
|
|
|
Looper::reset();
|
2017-07-01 20:34:34 +08:00
|
|
|
idler=[]() {};
|
2017-05-02 20:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Poller::run()
|
|
|
|
{
|
|
|
|
int pollret=1;
|
2017-07-01 20:34:34 +08:00
|
|
|
|
|
|
|
do
|
2017-05-02 20:51:56 +08:00
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
if(_update)
|
|
|
|
{
|
|
|
|
updater();
|
|
|
|
_update=false;
|
|
|
|
}
|
|
|
|
|
2017-05-02 20:51:56 +08:00
|
|
|
while(!_update&&(pollret=PollEvent(_e)))
|
|
|
|
{
|
|
|
|
dispatch();
|
|
|
|
}
|
|
|
|
|
2017-05-02 22:38:37 +08:00
|
|
|
/// If pollret is not 0 (new event requests update), or pollret is 0 (No New Event) but Idle function requests update, then call updater.
|
2017-05-02 20:51:56 +08:00
|
|
|
if(!pollret) idler();
|
2017-07-01 20:34:34 +08:00
|
|
|
|
2017-05-02 20:51:56 +08:00
|
|
|
}
|
2017-07-01 20:34:34 +08:00
|
|
|
while(_running);
|
2017-05-02 13:37:03 +08:00
|
|
|
}
|
2017-05-02 22:38:37 +08:00
|
|
|
|
|
|
|
LooperWithTime::LooperWithTime(int Timeout_ms)
|
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
_timeout_ms = Timeout_ms;
|
2017-05-02 22:38:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LooperWithTime::setTimeout(int ms)
|
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
_timeout_ms = ms;
|
2017-05-02 22:38:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int LooperWithTime::getTimeout() const
|
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
return _timeout_ms;
|
2017-05-02 22:38:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LooperWithTime::run()
|
|
|
|
{
|
2017-07-01 20:34:34 +08:00
|
|
|
int timeret = 1;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (_update)
|
|
|
|
{
|
|
|
|
updater();
|
|
|
|
_update = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms)))
|
|
|
|
{
|
|
|
|
dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If timeret is not 0 (new event request update), or timeret is 0 (Time out) but Idle function requests update, then call updater.
|
|
|
|
if (!timeret) idler();
|
|
|
|
}
|
|
|
|
while (_running);
|
2017-05-02 22:38:37 +08:00
|
|
|
}
|
2017-07-01 15:58:34 +08:00
|
|
|
|
|
|
|
}/// End of namespace MiniEngine
|