2017-04-04 09:49:18 +08:00
|
|
|
#pragma once
|
|
|
|
#include "MiniEngine.h"
|
2017-05-02 13:37:03 +08:00
|
|
|
#include <map>
|
|
|
|
#include <list>
|
2017-05-02 12:59:57 +08:00
|
|
|
|
|
|
|
typedef SDL_Event Event;
|
|
|
|
|
|
|
|
int PollEvent(Event& refEvent);
|
|
|
|
int WaitEvent(Event& refEvent);
|
|
|
|
int WaitEventTimeout(Event& refEvent,int ms);
|
|
|
|
int PushEvent(const Event& refEvent);
|
2017-05-05 17:28:11 +08:00
|
|
|
void PumpEvents();
|
|
|
|
bool HasEvent(decltype(Event::type) EventType);
|
|
|
|
bool HasEvent(decltype(Event::type) EventTypeMin,decltype(Event::type) EventTypeMax);
|
2017-05-08 14:17:42 +08:00
|
|
|
bool EnableEvent(decltype(Event::type) EventType);
|
|
|
|
bool DisableEvent(decltype(Event::type) EventType);
|
|
|
|
bool IsEventEnabled(decltype(Event::type) EventType);
|
2017-05-02 13:37:03 +08:00
|
|
|
|
2017-05-02 19:05:48 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
decltype(Event::type) _type_id;
|
|
|
|
int _looper_cnt;
|
|
|
|
}LooperID;
|
|
|
|
|
|
|
|
bool operator == (const LooperID&,const LooperID&);
|
2017-05-08 14:17:42 +08:00
|
|
|
bool operator != (const LooperID&,const LooperID&);
|
2017-05-02 19:05:48 +08:00
|
|
|
|
2017-05-02 13:37:03 +08:00
|
|
|
class Looper
|
|
|
|
{
|
|
|
|
public:
|
2017-05-02 19:05:48 +08:00
|
|
|
typedef decltype(Event::type) _SDLEventType_;
|
2017-05-02 13:37:03 +08:00
|
|
|
Looper();
|
|
|
|
/// If Callback does not return 0, then stop transferring this event.
|
2017-05-02 19:05:48 +08:00
|
|
|
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);
|
|
|
|
|
2017-05-02 13:37:03 +08:00
|
|
|
void dispatch();
|
|
|
|
void run();
|
|
|
|
Event GetLastEvent();
|
|
|
|
void needupdate();
|
|
|
|
void needstop();
|
|
|
|
void stop();
|
|
|
|
void reset();
|
2017-05-02 20:51:56 +08:00
|
|
|
|
|
|
|
std::function<void()> updater;
|
|
|
|
|
|
|
|
protected:
|
2017-05-02 22:38:37 +08:00
|
|
|
LooperID _getNextID(const _SDLEventType_ & event_type);
|
2017-05-02 13:37:03 +08:00
|
|
|
Event _e;
|
|
|
|
bool _running,_update;
|
2017-05-02 19:05:48 +08:00
|
|
|
std::map<_SDLEventType_,std::list<std::pair<int,std::function<int(Looper&,Event&)>>>> _evmap;
|
|
|
|
int _loop_cnt;
|
2017-05-02 13:37:03 +08:00
|
|
|
};
|
2017-05-02 20:51:56 +08:00
|
|
|
|
|
|
|
class Poller : public Looper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Poller();
|
|
|
|
void run();
|
|
|
|
void reset();
|
|
|
|
|
|
|
|
std::function<void()> idler;
|
|
|
|
};
|
2017-05-02 22:38:37 +08:00
|
|
|
|
|
|
|
class LooperWithTime : public Poller
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LooperWithTime(int Timeout_ms=10);
|
|
|
|
void setTimeout(int ms);
|
|
|
|
int getTimeout() const;
|
|
|
|
void run();
|
|
|
|
protected:
|
|
|
|
int _timeout_ms;
|
2017-05-05 17:28:11 +08:00
|
|
|
};
|