mirror of
https://github.com/dingusdev/dingusppc.git
synced 2024-12-23 06:29:38 +00:00
Rework the EventManager to use CoreSignal.
This commit is contained in:
parent
6efe6f13a9
commit
6fa6b4d4dc
@ -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:
|
||||
|
@ -22,33 +22,34 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#ifndef EVENT_MANAGER_H
|
||||
#define EVENT_MANAGER_H
|
||||
|
||||
#include <core/coresignal.h>
|
||||
|
||||
#include <cinttypes>
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <utility>
|
||||
|
||||
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(const BaseEvent&)>;
|
||||
|
||||
void poll_events();
|
||||
|
||||
void register_handler(const EventType event_type, EventHandler&& eh) {
|
||||
this->_handlers[event_type].emplace_back(std::move(eh));
|
||||
};
|
||||
template <typename T>
|
||||
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 <typename T>
|
||||
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<EventType, std::list<EventHandler>> _handlers;
|
||||
CoreSignal<const WindowEvent&> _window_signal;
|
||||
CoreSignal<const MouseEvent&> _mouse_signal;
|
||||
|
||||
uint64_t events_captured = 0;
|
||||
uint64_t unhandled_events = 0;
|
||||
|
@ -21,7 +21,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
/** @file Video Conroller base class implementation. */
|
||||
|
||||
#include <core/hostevents.h>
|
||||
#include <devices/video/videoctrl.h>
|
||||
#include <loguru.hpp>
|
||||
#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)
|
||||
{
|
||||
EventManager::get_instance()->register_handler(EventType::EVENT_WINDOW,
|
||||
[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;
|
||||
});
|
||||
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
|
||||
|
@ -24,6 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
#ifndef VIDEO_CTRL_H
|
||||
#define VIDEO_CTRL_H
|
||||
|
||||
#include <core/hostevents.h>
|
||||
#include <SDL.h>
|
||||
|
||||
#include <cinttypes>
|
||||
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user