diff --git a/core/hostevents.cpp b/core/hostevents.cpp index 4ad7e78..645316e 100644 --- a/core/hostevents.cpp +++ b/core/hostevents.cpp @@ -42,7 +42,7 @@ void EventManager::poll_events() WindowEvent we; we.sub_type = event.window.event; we.window_id = event.window.windowID; - this->post_event(we); + this->_window_signal.emit(we); } break; @@ -54,8 +54,13 @@ void EventManager::poll_events() key_ups++; break; - case SDL_MOUSEMOTION: - mouse_motions++; + case SDL_MOUSEMOTION: { + MouseEvent me; + me.xrel = event.motion.xrel; + me.yrel = event.motion.yrel; + me.flags = MOUSE_EVENT_MOTION; + this->_mouse_signal.emit(me); + } break; default: diff --git a/core/hostevents.h b/core/hostevents.h index 32695a6..4e7b749 100644 --- a/core/hostevents.h +++ b/core/hostevents.h @@ -22,33 +22,34 @@ along with this program. If not, see . #ifndef EVENT_MANAGER_H #define EVENT_MANAGER_H +#include + #include -#include -#include -#include -#include -enum EventType : uint16_t { - EVENT_UNKNOWN = 0, - EVENT_WINDOW = 100, - EVENT_KEYBOARD = 200, - EVENT_MOUSE = 300, -}; - -class BaseEvent { +class WindowEvent { public: - BaseEvent(EventType type) { this->type = type; }; - EventType type; -}; - -class WindowEvent : public BaseEvent { -public: - WindowEvent() : BaseEvent(EventType::EVENT_WINDOW) {}; + WindowEvent() = default; + ~WindowEvent() = default; uint16_t sub_type; 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 { public: static EventManager* get_instance() { @@ -58,31 +59,24 @@ public: return event_manager; }; - using EventHandler = std::function; - void poll_events(); - void register_handler(const EventType event_type, EventHandler&& eh) { - this->_handlers[event_type].emplace_back(std::move(eh)); - }; + template + void add_window_handler(T *inst, void (T::*func)(const WindowEvent&)) { + _window_signal.connect_method(inst, func); + } - void post_event(const BaseEvent& event) { - auto type = event.type; - - if( _handlers.find(type) == _handlers.end() ) - return; - - auto&& handlers = _handlers.at(type); - - for(auto&& handler : handlers) - handler(event); + template + void add_mouse_handler(T *inst, void (T::*func)(const MouseEvent&)) { + _mouse_signal.connect_method(inst, func); } private: static EventManager* event_manager; EventManager() {}; // private constructor to implement a singleton - std::map> _handlers; + CoreSignal _window_signal; + CoreSignal _mouse_signal; uint64_t events_captured = 0; uint64_t unhandled_events = 0; diff --git a/devices/video/videoctrl.cpp b/devices/video/videoctrl.cpp index 6fc9d91..f8b3e02 100644 --- a/devices/video/videoctrl.cpp +++ b/devices/video/videoctrl.cpp @@ -21,7 +21,6 @@ along with this program. If not, see . /** @file Video Conroller base class implementation. */ -#include #include #include #include @@ -32,13 +31,7 @@ along with this program. If not, see . VideoCtrlBase::VideoCtrlBase(int width, int height) { - EventManager::get_instance()->register_handler(EventType::EVENT_WINDOW, - [this](const BaseEvent& event) { - const WindowEvent& wnd_event = static_cast(event); - if (wnd_event.sub_type == SDL_WINDOWEVENT_SIZE_CHANGED && - wnd_event.window_id == this->disp_wnd_id) - this->resizing = false; - }); + EventManager::get_instance()->add_window_handler(this, &VideoCtrlBase::handle_events); 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) { if (!this->display_wnd) { // create display window diff --git a/devices/video/videoctrl.h b/devices/video/videoctrl.h index 1ee258c..f2625d2 100644 --- a/devices/video/videoctrl.h +++ b/devices/video/videoctrl.h @@ -24,6 +24,7 @@ along with this program. If not, see . #ifndef VIDEO_CTRL_H #define VIDEO_CTRL_H +#include #include #include @@ -34,6 +35,7 @@ public: VideoCtrlBase(int width = 640, int height = 480); ~VideoCtrlBase(); + void handle_events(const WindowEvent& wnd_event); void create_display_window(int width, int height); void blank_display(); void update_screen(void);