feat: event system
This commit is contained in:
7
Makefile
7
Makefile
@@ -1,8 +1,8 @@
|
|||||||
CFLAGS = -Wall -Wextra -I./include/ -g
|
CFLAGS = -Wall -Wextra -I./include/ -g
|
||||||
LIBS = $(shell pkg-config --libs wayland-client wayland-egl egl glesv2 wayland-cursor)
|
LIBS = $(shell pkg-config --libs wayland-client wayland-egl egl glesv2 wayland-cursor)
|
||||||
|
|
||||||
build/main: src/main.cpp build/shader.o build/state.o build/renderer.o build/window.o build/window_wayland.o build/input.o build/input_wayland.o build/event.o build/xdg-shell-protocol.o
|
build/main: src/main.cpp build/shader.o build/state.o build/renderer.o build/window.o build/window_wayland.o build/input.o build/input_wayland.o build/event.o build/event_input.o build/xdg-shell-protocol.o
|
||||||
g++ -o build/main $(CFLAGS) src/main.cpp build/shader.o build/state.o build/renderer.o build/window.o build/window_wayland.o build/input.o build/input_wayland.o build/event.o build/xdg-shell-protocol.o $(LIBS)
|
g++ -o build/main $(CFLAGS) src/main.cpp build/shader.o build/state.o build/renderer.o build/window.o build/window_wayland.o build/input.o build/input_wayland.o build/event.o build/event_input.o build/xdg-shell-protocol.o $(LIBS)
|
||||||
|
|
||||||
build/shader.o: src/shader.cpp include/shader.h
|
build/shader.o: src/shader.cpp include/shader.h
|
||||||
g++ -o build/shader.o $(CFLAGS) -c src/shader.cpp
|
g++ -o build/shader.o $(CFLAGS) -c src/shader.cpp
|
||||||
@@ -31,6 +31,9 @@ build/input_wayland.o: include/input/wayland.h src/input/wayland.cpp
|
|||||||
build/event.o: src/event.cpp include/event.h
|
build/event.o: src/event.cpp include/event.h
|
||||||
g++ -o build/event.o $(CFLAGS) -c src/event.cpp
|
g++ -o build/event.o $(CFLAGS) -c src/event.cpp
|
||||||
|
|
||||||
|
build/event_input.o: src/event/input.cpp include/event/input.h
|
||||||
|
g++ -o build/event_input.o $(CFLAGS) -c src/event/input.cpp
|
||||||
|
|
||||||
build/xdg-shell-protocol.o: src/xdg-shell-protocol.c
|
build/xdg-shell-protocol.o: src/xdg-shell-protocol.c
|
||||||
gcc -o build/xdg-shell-protocol.o -c $(CFLAGS) $(LIBS) src/xdg-shell-protocol.c
|
gcc -o build/xdg-shell-protocol.o -c $(CFLAGS) $(LIBS) src/xdg-shell-protocol.c
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,34 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
enum class EventType {
|
enum class EventType : unsigned int {
|
||||||
INPUT,
|
// ----- INPUT -----
|
||||||
|
INPUT = 1 << 0,
|
||||||
|
// Mouse
|
||||||
|
MOUSE = 1 << 1,
|
||||||
|
MOUSE_MOVED = 1 << 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
constexpr EventType operator|(EventType a, EventType b) {
|
||||||
|
return static_cast<EventType>(
|
||||||
|
static_cast<unsigned int>(a) |
|
||||||
|
static_cast<unsigned int>(b)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr EventType operator&(EventType a, EventType b) {
|
||||||
|
return static_cast<EventType>(
|
||||||
|
static_cast<unsigned int>(a) &
|
||||||
|
static_cast<unsigned int>(b)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr EventType operator~(EventType a) {
|
||||||
|
return static_cast<EventType>(
|
||||||
|
~static_cast<unsigned int>(a)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
struct Event {
|
struct Event {
|
||||||
public:
|
public:
|
||||||
Event();
|
Event();
|
||||||
|
|||||||
@@ -13,4 +13,25 @@ public:
|
|||||||
EventType GetType() const override;
|
EventType GetType() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct MouseEvent : public InputEvent {
|
||||||
|
public:
|
||||||
|
MouseEvent(EventType type);
|
||||||
|
~MouseEvent() = default;
|
||||||
|
|
||||||
|
public:
|
||||||
|
EventType GetType() const override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
EventType m_mouse_type;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MouseMoveEvent : public MouseEvent {
|
||||||
|
public:
|
||||||
|
MouseMoveEvent(size_t x, size_t y);
|
||||||
|
~MouseMoveEvent() = default;
|
||||||
|
public:
|
||||||
|
size_t x;
|
||||||
|
size_t y;
|
||||||
|
};
|
||||||
|
|
||||||
#endif // H_INPUT_EVENT_
|
#endif // H_INPUT_EVENT_
|
||||||
|
|||||||
@@ -4,8 +4,11 @@
|
|||||||
|
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
#include <wayland-client-protocol.h>
|
#include <wayland-client-protocol.h>
|
||||||
|
|
||||||
|
#include "event.h"
|
||||||
|
|
||||||
class WaylandInputHandler : public InputHandlerImpl {
|
class WaylandInputHandler : public InputHandlerImpl {
|
||||||
private:
|
private:
|
||||||
static void handle_pointer_enter(void *data,
|
static void handle_pointer_enter(void *data,
|
||||||
@@ -67,6 +70,8 @@ private:
|
|||||||
struct wl_pointer_listener m_pointer_listener;
|
struct wl_pointer_listener m_pointer_listener;
|
||||||
wl_fixed_t m_surface_x;
|
wl_fixed_t m_surface_x;
|
||||||
wl_fixed_t m_surface_y;
|
wl_fixed_t m_surface_y;
|
||||||
|
private:
|
||||||
|
std::vector<Event*> m_event_queue;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // H_WAYLAND_INPUT_
|
#endif // H_WAYLAND_INPUT_
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
#include "event/input.h"
|
#include "event/input.h"
|
||||||
|
#include "event.h"
|
||||||
|
|
||||||
EventType InputEvent::GetType() const {
|
EventType InputEvent::GetType() const {
|
||||||
return EventType::INPUT;
|
return EventType::INPUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
MouseEvent::MouseEvent(EventType type) : InputEvent(), m_mouse_type(type) {}
|
||||||
|
|
||||||
|
EventType MouseEvent::GetType() const {
|
||||||
|
return InputEvent::GetType() | EventType::MOUSE | m_mouse_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
MouseMoveEvent::MouseMoveEvent() : MouseEvent(EventType::MOUSE_MOVED) {}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "input/wayland.h"
|
#include "input/wayland.h"
|
||||||
|
#include "event/input.h"
|
||||||
#include "state/wayland.h"
|
#include "state/wayland.h"
|
||||||
#include <wayland-client-core.h>
|
#include <wayland-client-core.h>
|
||||||
#include <wayland-client-protocol.h>
|
#include <wayland-client-protocol.h>
|
||||||
@@ -69,15 +70,16 @@ void WaylandInputHandler::handle_pointer_leave(void *data,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WaylandInputHandler::handle_pointer_motion(void *data,
|
void WaylandInputHandler::handle_pointer_motion(void *data,
|
||||||
struct wl_pointer *wl_pointer,
|
struct wl_pointer *pointer,
|
||||||
uint32_t time,
|
uint32_t time,
|
||||||
wl_fixed_t surface_x,
|
wl_fixed_t surface_x,
|
||||||
wl_fixed_t surface_y) {
|
wl_fixed_t surface_y) {
|
||||||
UNUSED(wl_pointer);
|
UNUSED(pointer);
|
||||||
UNUSED(time);
|
UNUSED(time);
|
||||||
auto handler = reinterpret_cast<WaylandInputHandler*>(data);
|
auto handler = reinterpret_cast<WaylandInputHandler*>(data);
|
||||||
handler->m_surface_x = surface_x;
|
handler->m_surface_x = surface_x;
|
||||||
handler->m_surface_y = surface_y;
|
handler->m_surface_y = surface_y;
|
||||||
|
handler->m_event_queue.push_back(new MouseMoveEvent(surface_x, surface_y));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WaylandInputHandler::handle_pointer_button(void *data,
|
void WaylandInputHandler::handle_pointer_button(void *data,
|
||||||
|
|||||||
Reference in New Issue
Block a user