78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <extra2d/core/service_interface.h>
|
|
#include <extra2d/core/service_locator.h>
|
|
#include <extra2d/event/event_dispatcher.h>
|
|
#include <extra2d/event/event_queue.h>
|
|
|
|
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;
|
|
|
|
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_;
|
|
|
|
// 服务注册元数据
|
|
E2D_AUTO_REGISTER_SERVICE(IEventService, EventService);
|
|
};
|
|
|
|
}
|