WIP Re-engineering input systems

This commit is contained in:
2024-01-03 17:18:51 -05:00
parent f1e6d00c4a
commit 5ae78b473c
9 changed files with 427 additions and 172 deletions

View File

@@ -32,7 +32,9 @@ if(WIN32)
endif()
include_directories("include")
add_library(ReWindowLibrary SHARED ${SOURCES})
add_library(ReWindowLibrary SHARED ${SOURCES}
include/rewindow/data/X11Scancodes.h
include/rewindow/types/key.h)
# Why god???
set_target_properties(ReWindowLibrary PROPERTIES LINKER_LANGUAGE CXX)

View File

@@ -2,7 +2,7 @@
# ReWindow
[![License: Unlicense](https://img.shields.io/badge/license-Unlicense-blue.svg)](http://unlicense.org/)
A library which allows easily creating and managing a window and it's events across multiple platforms *primarily for games*.
A library which allows easily creating and managing a window and it's eventLog across multiple platforms *primarily for games*.
## Run Demo

View File

@@ -0,0 +1,106 @@
#pragma once
enum class X11Scancode {
KP_0 = 90,
KP_1 = 87,
KP_2 = 88,
KP_3 = 89,
KP_4 = 83,
KP_5 = 84,
KP_6 = 85,
KP_7 = 79,
KP_8 = 80,
KP_9 = 81,
KP_PERIOD = 91,
KP_RETURN = 104,
KP_PLUS = 86,
KP_MINUS = 82,
KP_ASTERISK = 63,
KP_SLASH = 106,
KP_NUMLOCK = 77,
PAGEUP = 112,
PAGEDOWN = 117,
HOME = 110,
END = 115,
INSERT = 118,
DELETE = 119,
UP = 111,
DOWN = 116,
LEFT = 113,
RIGHT = 114,
BACKSPACE = 22,
EQUALS = 21,
MINUS = 20,
ZERO = 19,
NINE = 18,
EIGHT = 17,
SEVEN = 16,
SIX = 15,
FIVE = 14,
FOUR = 13,
THREE = 12,
TWO = 11,
ONE = 10,
GRAVE = 49,
BACKSLASH = 51,
CLOSING_SQUARE_BRACKET = 35,
OPENING_SQUARE_BRACKET = 34,
P = 33,
O = 32,
I = 31,
U = 30,
Y = 29,
T = 28,
R = 27,
E = 26,
W = 25,
Q = 24,
TAB = 23,
RETURN = 36,
SINGLEQUOTE = 48,
SEMICOLON = 47,
L = 46,
K = 45,
J = 44,
H = 43,
G = 42,
F = 41,
D = 40,
S = 39,
A = 38,
CAPSLOCK = 66,
RIGHT_SHIFT = 62,
SLASH = 61,
PERIOD = 60,
COMMA = 59,
M = 58,
N = 57,
B = 56,
V = 55,
C = 54,
X = 53,
Z = 52,
LEFT_SHIFT = 50,
RIGHT_CONTROL = 105,
RIGHT_ALT = 108,
SPACE = 65,
LEFT_ALT = 64,
SUPER = 133,
LEFT_CTRL = 37,
ESCAPE = 9,
FUNCTION_ONE = 67,
FUNCTION_TWO = 68,
FUNCTION_THREE = 69,
FUNCTION_FOUR = 70,
FUNCTION_FIVE = 71,
FUNCTION_SIX = 72,
FUNCTION_SEVEN = 73,
FUNCTION_EIGHT = 74,
FUNCTION_NINE = 75,
FUNCTION_TEN = 76,
FUNCTION_ELEVEN = 77,
FUNCTION_TWELVE = 78,
PRINT = 107,
SCROLL_LOCK = 78,
BREAK = 127,
};

View File

@@ -2,113 +2,13 @@
#if __linux__
#include <X11/Xlib.h>
#include <chrono>
#include <functional>
#include "window.h"
#endif
//This is also the x11 scancodes.
enum class SCANCODE {
KP_0 = 90,
KP_1 = 87,
KP_2 = 88,
KP_3 = 89,
KP_4 = 83,
KP_5 = 84,
KP_6 = 85,
KP_7 = 79,
KP_8 = 80,
KP_9 = 81,
KP_PERIOD = 91,
KP_RETURN = 104,
KP_PLUS = 86,
KP_MINUS = 82,
KP_ASTERISK = 63,
KP_SLASH = 106,
KP_NUMLOCK = 77,
PAGEUP = 112,
PAGEDOWN = 117,
HOME = 110,
END = 115,
INSERT = 118,
DELETE = 119,
UP = 111,
DOWN = 116,
LEFT = 113,
RIGHT = 114,
BACKSPACE = 22,
EQUALS = 21,
MINUS = 20,
ZERO = 19,
NINE = 18,
EIGHT = 17,
SEVEN = 16,
SIX = 15,
FIVE = 14,
FOUR = 13,
THREE = 12,
TWO = 11,
ONE = 10,
GRAVE = 49,
BACKSLASH = 51,
CLOSING_SQUARE_BRACKET = 35,
OPENING_SQUARE_BRACKET = 34,
P = 33,
O = 32,
I = 31,
U = 30,
Y = 29,
T = 28,
R = 27,
E = 26,
W = 25,
Q = 24,
TAB = 23,
RETURN = 36,
SINGLEQUOTE = 48,
SEMICOLON = 47,
L = 46,
K = 45,
J = 44,
H = 43,
G = 42,
F = 41,
D = 40,
S = 39,
A = 38,
CAPSLOCK = 66,
RIGHT_SHIFT = 62,
SLASH = 61,
PERIOD = 60,
COMMA = 59,
M = 58,
N = 57,
B = 56,
V = 55,
C = 54,
X = 53,
Z = 52,
LEFT_SHIFT = 50,
RIGHT_CONTROL = 105,
RIGHT_ALT = 108,
SPACE = 65,
LEFT_ALT = 64,
SUPER = 133,
LEFT_CTRL = 37,
ESCAPE = 9,
FUNCTION_ONE = 67,
FUNCTION_TWO = 68,
FUNCTION_THREE = 69,
FUNCTION_FOUR = 70,
FUNCTION_FIVE = 71,
FUNCTION_SIX = 72,
FUNCTION_SEVEN = 73,
FUNCTION_EIGHT = 74,
FUNCTION_NINE = 75,
FUNCTION_TEN = 76,
FUNCTION_ELEVEN = 77,
FUNCTION_TWELVE = 78,
PRINT = 107,
SCROLL_LOCK = 78,
BREAK = 127,
};
enum class MOUSEBUTTONCODE {
LEFT_CLICK = 1,
@@ -120,27 +20,78 @@ enum class MOUSEBUTTONCODE {
SIDE_BACK = 8
};
class RWindowEvent {
template <typename ... Args>
class Event;
template <typename ... Args>
class EventConnection {
private:
std::chrono::high_resolution_clock::time_point timePoint;
using delegate = std::function<void(Args...)>;
public:
std::chrono::high_resolution_clock::time_point timeStamp();
virtual ~RWindowEvent() = default;
virtual bool empty();
RWindowEvent() {
timePoint = std::chrono::high_resolution_clock::now();
EventConnection(Event<Args...> *creator, delegate cb) : owner(creator), callback(std::move(cb)) {}
bool Disconnect(); // Breaks the event connection, but does not destroy the instance
void Invoke(Args... e);
private:
Event<Args...> * owner;
delegate callback;
bool active = true;
};
template <typename ... Args>
class Event {
public:
using delegate = std::function<void(Args...)>;
using connection = EventConnection<Args ...>;
using event_ptr = std::shared_ptr<connection>;
public:
void Await(Args& ... arg);
void Invoke(Args... args);
void operator()(Args... args);
connection Connect(delegate callback);
void Disconnect(connection &conn);
connection operator+=(delegate callback);
private:
std::vector<event_ptr> listeners;
uint64_t listenerCounter = 0;
};
template<typename... Args>
EventConnection<Args...> Event<Args...>::operator+=(Event::delegate callback) { return Connect(callback); }
template<typename... Args>
void Event<Args...>::operator()(Args... args) { Invoke(args...);}
template <typename... Args>
void EventConnection<Args...>::Invoke(Args... e) { callback(e...); }
template <typename ... Args>
bool EventConnection<Args...>::Disconnect() {
if (active) {
owner->Disconnect(this);
active = false;
return true;
}
};
const RWindowEvent EmptyRWindowEvent;
return false;
}
class KeyDownEvent : public RWindowEvent {
public:
SCANCODE key;
};
const KeyDownEvent EmptyKeyDownEvent{};
template <typename ... Args>
void Event<Args...>::Invoke(Args... args) {
for (event_ptr &connection_ptr: this->listeners) {
connection_ptr->Invoke(args...);
}
}
template <typename ... Args>
EventConnection<Args...> Event<Args...>::Connect(delegate callback)
{
event_ptr retval(new connection(this, callback));
this->listeners.push_back(retval);
return *retval;
}
template <typename ... Args>
void Event<Args...>::Disconnect(connection &conn) {
listeners.erase(std::remove(listeners.begin(), listeners.end(), 99), listeners.end());
}
class MouseButtonDownEvent : public RWindowEvent {
public:
MOUSEBUTTONCODE button;
};
const MouseButtonDownEvent EmptyMouseButtonDownEvent{};

View File

@@ -0,0 +1,96 @@
//
// ~DAWSH
#pragma once
#include <vector>
#include "rewindow/data/X11Scancodes.h"
class Key
{
public:
static std::vector<Key> keyboard;
Key()
{
keyboard.push_back(*this);
}
Key(char charcode, X11Scancode scancode)
: CharCode(charcode), x11ScanCode(scancode)
{
keyboard.push_back(*this);
}
char CharCode;
X11Scancode x11ScanCode;
};
namespace Keys {
static const Key Escape;
static const Key F1;
static const Key F2;
static const Key F3;
static const Key F4;
static const Key F5;
static const Key F6;
static const Key F7;
static const Key F8;
static const Key F9;
static const Key F10;
static const Key F11;
static const Key F12;
static const Key NumPad1;
static const Key NumPad2;
static const Key NumPad3;
static const Key NumPad4;
static const Key NumPad5;
static const Key NumPad6;
static const Key NumPad7;
static const Key NumPad8;
static const Key NumPad9;
static const Key NumPad0;
static const Key Q {'Q', X11Scancode::Q};
static const Key W {'W', X11Scancode::W};
static const Key E {'E', X11Scancode::E};
static const Key R {'R', X11Scancode::R};
static const Key T;
static const Key Y;
static const Key U;
static const Key I;
static const Key O;
static const Key P;
static const Key A;
static const Key S;
static const Key D;
static const Key F;
static const Key G;
static const Key H;
static const Key J;
static const Key K;
static const Key L;
static const Key Semicolon;
static const Key Comma;
static const Key Enter;
static const Key Minus;
static const Key Equals;
static const Key Tilde;
static const Key Z;
static const Key X;
static const Key C;
static const Key V;
static const Key B;
static const Key N;
static const Key M;
static const Key Period;
static const Key Slash;
static const Key LShift;
static const Key RShift;
}
static Key GetKeyFromX11Scancode(X11Scancode code)
{
for (auto& key : Key::keyboard) {
if (key.x11ScanCode == code)
return key;
}
}

View File

@@ -2,20 +2,25 @@
#include <cstdint>
#include <vector>
#include <memory>
#include "event.h"
#include <rewindow/types/event.h>
#if __linux__
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <functional>
#include <rewindow/data/X11Scancodes.h>
#include <map>
#include "key.h"
#endif
enum class RWindowFlags: uint8_t {
IN_FOCUS = 0,
FULLSCREEN = 1,
RESIZABLE = 2,
VSYNC = 3
VSYNC = 4
};
enum class RenderingAPI: uint8_t {
@@ -24,11 +29,86 @@ enum class RenderingAPI: uint8_t {
VULKAN = 1,
};
using namespace std::chrono_literals;
using precision_clock = std::chrono::high_resolution_clock;
using precision_timestamp = precision_clock::time_point;
class TimestampedEvent {
private:
precision_timestamp timestamp;
public:
TimestampedEvent() : timestamp(precision_clock::now())
{ }
precision_timestamp GetTimestamp() const { return timestamp;}
};
class RWindowEvent : public TimestampedEvent {
public:
RWindowEvent() : TimestampedEvent() { }
};
const RWindowEvent EmptyRWindowEvent;
enum class KeyState { Pressed, Released };
class KeyboardState {
public:
std::map<X11Scancode, bool> PressedKeys;
};
class KeyboardEvent : public RWindowEvent {
public:
Key key;
KeyState state;
KeyboardEvent(Key key, KeyState state) : RWindowEvent(), key(key), state(state) {}
};
class MouseEvent : public RWindowEvent {};
class GamepadEvent : public RWindowEvent {};
class KeyDownEvent : public KeyboardEvent {
public:
KeyDownEvent(Key key) : KeyboardEvent(key, KeyState::Pressed) {}
};
class KeyUpEvent : public KeyboardEvent {
public:
KeyUpEvent(Key key) : KeyboardEvent(key, KeyState::Released) {}
};
class MouseButtonDownEvent : public RWindowEvent {
public:
MOUSEBUTTONCODE button;
};
const MouseButtonDownEvent EmptyMouseButtonDownEvent{};
class RWindow {
private:
bool flags[4];
std::vector<RWindowEvent*> events;
std::vector<RWindowEvent> eventLog;
KeyboardState currentKeyboard; // Current Frame's Keyboard State
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
public:
Event<> focusGained;
Event<> focusLost;
// TODO: Integrate J3ML
//Event<J3ML::Vector2> mouseMoved;
Event<MOUSEBUTTONCODE> mouseBtnPressed;
Event<MOUSEBUTTONCODE> mouseBtnReleased;
Event<KeyDownEvent> onKeyboardPress;
Event<KeyUpEvent> onKeyboardRelease;
bool isFocused() const;
bool isFullscreen() const;
bool isResizable() const;
bool isVsyncEnabled() const;
void setFocused(bool);
void setFullscreen(bool);
void setResizable(bool);
void setVsyncEnabled(bool);
bool getFlag(RWindowFlags flag);
void setFlag(RWindowFlags flag, bool state);
void init(RenderingAPI api, const char* title, int width, int height);
@@ -38,9 +118,9 @@ public:
std::unique_ptr<int[]> getPos();
std::unique_ptr<int[]> getSize();
void setPos(int x, int y);
bool keyDown(SCANCODE scancode);
bool keyDown(X11Scancode scancode);
bool mouseButtonDown(MOUSEBUTTONCODE buttoncode);
KeyDownEvent getEvent(SCANCODE scancode);
KeyDownEvent getEvent(X11Scancode scancode);
MouseButtonDownEvent getEvent(MOUSEBUTTONCODE buttoncode);
static void glSwapBuffers();
//Initialize to false because it's not guaranteed that they will be cleared first

View File

@@ -7,17 +7,17 @@ int main() {
while (true) {
window->pollEvents();
if (window->keyDown(SCANCODE::A)) {
if (window->keyDown(X11Scancode::A)) {
std::cout << "A" << std::endl;
std::cout << (int64_t) window->getEvent(SCANCODE::A).empty() << std::endl;
std::cout << (int64_t) window->getEvent(X11Scancode::A).empty() << std::endl;
}
if (window->mouseButtonDown(MOUSEBUTTONCODE::LEFT_CLICK))
std::cout << window->mouseButtonDown(MOUSEBUTTONCODE::LEFT_CLICK) << std::endl;
//std::cout << window->events.size() << std::endl;
//std::cout << window->eventLog.size() << std::endl;
//window->resize(800,600);
//std::cout << window->getPos()[1] << std::endl;
//auto* d = new(KeyDownEvent);
//d->key = SCANCODE::A;
//window->events.push_back(d);
//window->eventLog.push_back(d);
}
}

View File

@@ -1,10 +1,2 @@
#include "../include/rewindow/types/event.h"
bool RWindowEvent::empty() {
if (timePoint == EmptyRWindowEvent.timePoint || this->timePoint == EmptyKeyDownEvent.timePoint || this->timePoint == EmptyMouseButtonDownEvent.timePoint)
return true;
return false;
}
#include <rewindow/types/event.h>
std::chrono::high_resolution_clock::time_point RWindowEvent::timeStamp() {
return timePoint;
}

View File

@@ -1,5 +1,5 @@
#include <iostream>
#include "rewindow/types/window.h"
#include <rewindow/types/window.h>
Window window;
XEvent xev;
@@ -81,54 +81,70 @@ void RWindow::pollEvents() {
}
if (xev.type == FocusIn) {
XAutoRepeatOff(display);
focusGained.Invoke();
setFlag(RWindowFlags::IN_FOCUS, true);
}
if (xev.type == FocusOut) {
XAutoRepeatOn(display);
focusLost.Invoke();
setFlag(RWindowFlags::IN_FOCUS, false);
}
if (xev.type == KeyRelease) {
for (unsigned int i = 0; i < events.size(); i++) {
if (auto *e = dynamic_cast<KeyDownEvent *>(events[i])) {
auto scancode = (X11Scancode) xev.xkey.keycode;
currentKeyboard.PressedKeys[scancode] = false;
auto key = GetKeyFromX11Scancode(scancode);
KeyUpEvent eventData = KeyUpEvent(key);
onKeyboardRelease.Invoke(eventData);
/*for (unsigned int i = 0; i < eventLog.size(); i++) {
if (auto *e = dynamic_cast<KeyDownEvent *>(eventLog[i])) {
if ((int) e->key == (int) xev.xkey.keycode) {
delete events[i];
events.erase(events.begin() + i);
delete eventLog[i];
eventLog.erase(eventLog.begin() + i);
}
}
}
}*/
}
if (xev.type == KeyPress) {
//onKeyboardPress(xev.xkey.keycode);
//On Windows you'll have to do this the long way.
//The keycodes won't be the same :shrug:
auto *kD = new(KeyDownEvent);
kD->key = (SCANCODE) xev.xkey.keycode;
events.push_back(kD);
auto scancode = (X11Scancode) xev.xkey.keycode;
auto key = GetKeyFromX11Scancode(scancode);
currentKeyboard.PressedKeys[scancode] = true;
KeyDownEvent eventData = KeyDownEvent(key);
onKeyboardPress.Invoke(eventData);
eventLog.push_back(eventData);
}
if (xev.type == ButtonRelease) {
for (unsigned int i = 0; i < events.size(); i++) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(events[i])) {
/*for (unsigned int i = 0; i < eventLog.size(); i++) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(eventLog[i])) {
if ((int) e->button == (int) xev.xbutton.button) {
delete events[i];
events.erase(events.begin() + i);
delete eventLog[i];
eventLog.erase(eventLog.begin() + i);
}
}
}
}*/
}
if (xev.type == ButtonPress) {
std::cout << (int) xev.xbutton.button << std::endl;
auto *mBD = new(MouseButtonDownEvent);
mBD->button = (MOUSEBUTTONCODE) xev.xbutton.button;
events.push_back(mBD);
/*std::cout << (int) xev.xbutton.button << std::endl;
auto eventData = MouseButtonDownEvent(xev.xbutton.button);
//mBD->button = (MOUSEBUTTONCODE) xev.xbutton.button;
mouseBtnPressed.Invoke(eventData);
eventLog.push_back(eventData);*/
}
if (xev.type == Expose) {
}
}
previousKeyboard = currentKeyboard;
}
//Might make the window go off the screen on some window managers.
@@ -138,6 +154,14 @@ void RWindow::setSize(int width, int height) {
XResizeWindow(display, window, width, height);
}
struct int2d {
int x;
int y;
};
// TODO: use int2D, std::pair<int, int>, or vector2 instead!!!
// Prefer a simple, non-heap-allocated type
// TODO: Const-qualify this function
std::unique_ptr<int[]> RWindow::getSize() {
XGetWindowAttributes(display,window,&windowAttributes);
std::unique_ptr<int[]> size = std::make_unique<int[]>(2);
@@ -146,7 +170,9 @@ std::unique_ptr<int[]> RWindow::getSize() {
return size;
}
// TODO: use int2D, std::pair<int, int>, or vector2 instead!!!
//I'm unsure why this doesn't work as you'd expect.
// TODO: Const-qualify this function
std::unique_ptr<int[]> RWindow::getPos() {
XGetWindowAttributes(display,window,&windowAttributes);
std::unique_ptr<int[]> pos = std::make_unique<int[]>(2);
@@ -159,40 +185,42 @@ void RWindow::setPos(int x, int y) {
XMoveWindow(display, window, x, y);
}
bool RWindow::keyDown(SCANCODE scancode) {
for (auto & ev : events) {
bool RWindow::keyDown(X11Scancode scancode) {
return currentKeyboard.PressedKeys[scancode];
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<KeyDownEvent *>(ev)) {
if (e->key == scancode) { return true;}
}
}
return false;
return false;*/
}
KeyDownEvent RWindow::getEvent(SCANCODE scancode) {
for (auto & ev : events) {
KeyDownEvent RWindow::getEvent(X11Scancode scancode) {
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<KeyDownEvent *>(ev)) {
if (e->key == scancode) { return *e;}
}
}
return EmptyKeyDownEvent;
}*/
//return EmptyKeyDownEvent;
}
bool RWindow::mouseButtonDown(MOUSEBUTTONCODE buttoncode) {
for (auto & ev : events) {
// TODO: Implement MouseButtonState
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(ev)) {
if (e->button == buttoncode) { return true;}
}
}
return false;
return false;*/
}
MouseButtonDownEvent RWindow::getEvent(MOUSEBUTTONCODE buttoncode) {
for (auto & ev : events) {
/*for (auto & ev : eventLog) {
if (auto *e = dynamic_cast<MouseButtonDownEvent *>(ev)) {
if (e->button == buttoncode) { return *e;}
}
}
return EmptyMouseButtonDownEvent;
return EmptyMouseButtonDownEvent;*/
}
void RWindow::glSwapBuffers() {