Rework the EventManager to use CoreSignal.

This commit is contained in:
Maxim Poliakovski 2023-07-27 02:40:32 +02:00
parent 6efe6f13a9
commit 6fa6b4d4dc
4 changed files with 46 additions and 46 deletions

View File

@ -42,7 +42,7 @@ void EventManager::poll_events()
WindowEvent we; WindowEvent we;
we.sub_type = event.window.event; we.sub_type = event.window.event;
we.window_id = event.window.windowID; we.window_id = event.window.windowID;
this->post_event(we); this->_window_signal.emit(we);
} }
break; break;
@ -54,8 +54,13 @@ void EventManager::poll_events()
key_ups++; key_ups++;
break; break;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION: {
mouse_motions++; MouseEvent me;
me.xrel = event.motion.xrel;
me.yrel = event.motion.yrel;
me.flags = MOUSE_EVENT_MOTION;
this->_mouse_signal.emit(me);
}
break; break;
default: default:

View File

@ -22,33 +22,34 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef EVENT_MANAGER_H #ifndef EVENT_MANAGER_H
#define EVENT_MANAGER_H #define EVENT_MANAGER_H
#include <core/coresignal.h>
#include <cinttypes> #include <cinttypes>
#include <functional>
#include <list>
#include <map>
#include <utility>
enum EventType : uint16_t { class WindowEvent {
EVENT_UNKNOWN = 0,
EVENT_WINDOW = 100,
EVENT_KEYBOARD = 200,
EVENT_MOUSE = 300,
};
class BaseEvent {
public: public:
BaseEvent(EventType type) { this->type = type; }; WindowEvent() = default;
EventType type; ~WindowEvent() = default;
};
class WindowEvent : public BaseEvent {
public:
WindowEvent() : BaseEvent(EventType::EVENT_WINDOW) {};
uint16_t sub_type; uint16_t sub_type;
uint32_t window_id; uint32_t window_id;
}; };
enum : uint32_t {
MOUSE_EVENT_MOTION = 1 << 0,
MOUSE_EVENT_BUTTON = 1 << 1,
};
class MouseEvent {
public:
MouseEvent() = default;
~MouseEvent() = default;
uint32_t flags;
uint32_t xrel;
uint32_t yrel;
};
class EventManager { class EventManager {
public: public:
static EventManager* get_instance() { static EventManager* get_instance() {
@ -58,31 +59,24 @@ public:
return event_manager; return event_manager;
}; };
using EventHandler = std::function<void(const BaseEvent&)>;
void poll_events(); void poll_events();
void register_handler(const EventType event_type, EventHandler&& eh) { template <typename T>
this->_handlers[event_type].emplace_back(std::move(eh)); void add_window_handler(T *inst, void (T::*func)(const WindowEvent&)) {
}; _window_signal.connect_method(inst, func);
}
void post_event(const BaseEvent& event) { template <typename T>
auto type = event.type; void add_mouse_handler(T *inst, void (T::*func)(const MouseEvent&)) {
_mouse_signal.connect_method(inst, func);
if( _handlers.find(type) == _handlers.end() )
return;
auto&& handlers = _handlers.at(type);
for(auto&& handler : handlers)
handler(event);
} }
private: private:
static EventManager* event_manager; static EventManager* event_manager;
EventManager() {}; // private constructor to implement a singleton EventManager() {}; // private constructor to implement a singleton
std::map<EventType, std::list<EventHandler>> _handlers; CoreSignal<const WindowEvent&> _window_signal;
CoreSignal<const MouseEvent&> _mouse_signal;
uint64_t events_captured = 0; uint64_t events_captured = 0;
uint64_t unhandled_events = 0; uint64_t unhandled_events = 0;

View File

@ -21,7 +21,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
/** @file Video Conroller base class implementation. */ /** @file Video Conroller base class implementation. */
#include <core/hostevents.h>
#include <devices/video/videoctrl.h> #include <devices/video/videoctrl.h>
#include <loguru.hpp> #include <loguru.hpp>
#include <memaccess.h> #include <memaccess.h>
@ -32,13 +31,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
VideoCtrlBase::VideoCtrlBase(int width, int height) VideoCtrlBase::VideoCtrlBase(int width, int height)
{ {
EventManager::get_instance()->register_handler(EventType::EVENT_WINDOW, EventManager::get_instance()->add_window_handler(this, &VideoCtrlBase::handle_events);
[this](const BaseEvent& event) {
const WindowEvent& wnd_event = static_cast<const WindowEvent&>(event);
if (wnd_event.sub_type == SDL_WINDOWEVENT_SIZE_CHANGED &&
wnd_event.window_id == this->disp_wnd_id)
this->resizing = false;
});
this->create_display_window(width, height); this->create_display_window(width, height);
} }
@ -62,6 +55,12 @@ VideoCtrlBase::~VideoCtrlBase()
} }
} }
void VideoCtrlBase::handle_events(const WindowEvent& wnd_event) {
if (wnd_event.sub_type == SDL_WINDOWEVENT_SIZE_CHANGED &&
wnd_event.window_id == this->disp_wnd_id)
this->resizing = false;
}
void VideoCtrlBase::create_display_window(int width, int height) void VideoCtrlBase::create_display_window(int width, int height)
{ {
if (!this->display_wnd) { // create display window if (!this->display_wnd) { // create display window

View File

@ -24,6 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef VIDEO_CTRL_H #ifndef VIDEO_CTRL_H
#define VIDEO_CTRL_H #define VIDEO_CTRL_H
#include <core/hostevents.h>
#include <SDL.h> #include <SDL.h>
#include <cinttypes> #include <cinttypes>
@ -34,6 +35,7 @@ public:
VideoCtrlBase(int width = 640, int height = 480); VideoCtrlBase(int width = 640, int height = 480);
~VideoCtrlBase(); ~VideoCtrlBase();
void handle_events(const WindowEvent& wnd_event);
void create_display_window(int width, int height); void create_display_window(int width, int height);
void blank_display(); void blank_display();
void update_screen(void); void update_screen(void);