93 lines
2.3 KiB
C++
93 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include <extra2d/core/service_interface.h>
|
|
#include <extra2d/core/types.h>
|
|
#include <extra2d/event/event_dispatcher.h>
|
|
#include <extra2d/event/event_queue.h>
|
|
#include <extra2d/services/logger_service.h>
|
|
#include <extra2d/window/window_module.h>
|
|
#include <typeindex>
|
|
#include <vector>
|
|
|
|
namespace extra2d {
|
|
|
|
/**
|
|
* @brief 事件服务接口
|
|
*/
|
|
class IEventService : public IService {
|
|
public:
|
|
virtual ~IEventService() = default;
|
|
|
|
virtual void push(const Event &event) = 0;
|
|
virtual void push(Event &&event) = 0;
|
|
virtual bool poll(Event &event) = 0;
|
|
|
|
virtual ListenerID on(EventType type, EventDispatcher::EventFn fn) = 0;
|
|
virtual void off(ListenerID id) = 0;
|
|
virtual void offAll(EventType type) = 0;
|
|
virtual void offAll() = 0;
|
|
|
|
virtual void dispatch(Event &event) = 0;
|
|
virtual void process() = 0;
|
|
|
|
virtual size_t listenerCount(EventType type) const = 0;
|
|
virtual size_t totalListeners() const = 0;
|
|
virtual size_t queueSize() const = 0;
|
|
};
|
|
|
|
/**
|
|
* @brief 事件服务实现
|
|
*/
|
|
class EventService : public IEventService {
|
|
public:
|
|
EventService();
|
|
~EventService() override = default;
|
|
|
|
ServiceInfo info() const override;
|
|
|
|
/**
|
|
* @brief 事件服务依赖日志服务
|
|
*/
|
|
std::vector<std::type_index> deps() const override {
|
|
return {std::type_index(typeid(ILogger))};
|
|
}
|
|
|
|
/**
|
|
* @brief 事件服务依赖窗口模块
|
|
*/
|
|
std::vector<std::type_index> needsModules() const override {
|
|
return {std::type_index(typeid(WindowModule))};
|
|
}
|
|
|
|
bool init() override;
|
|
void shutdown() override;
|
|
void update(f32 dt) override;
|
|
|
|
void push(const Event &event) override;
|
|
void push(Event &&event) override;
|
|
bool poll(Event &event) override;
|
|
|
|
ListenerID on(EventType type, EventDispatcher::EventFn fn) override;
|
|
void off(ListenerID id) override;
|
|
void offAll(EventType type) override;
|
|
void offAll() override;
|
|
|
|
void dispatch(Event &event) override;
|
|
void process() override;
|
|
|
|
size_t listenerCount(EventType type) const override;
|
|
size_t totalListeners() const override;
|
|
size_t queueSize() const override;
|
|
|
|
EventQueue &queue() { return queue_; }
|
|
const EventQueue &queue() const { return queue_; }
|
|
EventDispatcher &dispatcher() { return dispatcher_; }
|
|
const EventDispatcher &dispatcher() const { return dispatcher_; }
|
|
|
|
private:
|
|
EventQueue queue_;
|
|
EventDispatcher dispatcher_;
|
|
};
|
|
|
|
} // namespace extra2d
|