Compare commits

...

6 Commits

Author SHA1 Message Date
4b2aeea2ca Temp Work
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m28s
2024-12-06 11:50:20 -05:00
feacea61b1 Fix Mouse Button errors, implement MouseWheel events
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 6m30s
2024-12-04 10:17:10 -05:00
a3adbb6266 Fix Mouse Button errors, implement MouseWheel events
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m42s
2024-12-03 21:24:50 -05:00
b75f44f1ee Fix IsMouseButtonDown infinite loop
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m39s
2024-12-03 19:39:58 -05:00
e4e860564c Fix RWindow::SetResizable on linux/X-server
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m38s
2024-12-03 19:19:51 -05:00
fb6825224f Fix RWindow::SetResizable on linux/X-server 2024-12-03 19:16:43 -05:00
19 changed files with 1006 additions and 108 deletions

View File

@@ -0,0 +1,273 @@
/// ReWindowLibrary
/// A C++20 Library for creating and managing windows in a platform-independent manner
/// Developed and Maintained by the boys @ Redacted Software.
/// (c) 2024 Redacted Software
/// This work is dedicated to the public domain.
/// @file scancode.h
/// @desc Abstraction layer for input device scancodes
/// @edit 2024-12-03
#pragma once
namespace ReWindow {
enum class ReScanCodeIndex : unsigned int {
Unimplemented = 0,
MLeft = 1,
MRight = 2,
MMiddle = 3,
MWheel_Up = 4,
MWheel_Down = 5,
MSide_A = 8,
MSide_B = 9,
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,
MENU = 135,
END = 115,
INSERT = 118,
DEL = 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,
F1 = 67,
F2 = 68,
F3 = 69,
F4 = 70,
F5 = 71,
F6 = 72,
F7 = 73,
F8 = 74,
F9 = 75,
F10 = 76,
F11 = 95,
F12 = 96,
PRINT = 107,
SCROLL_LOCK = 78,
BREAK = 127,
};
class ReScanCode
{
public:
std::string Mnemonic;
unsigned int Sindex;
ReScanCode() = default;
explicit ReScanCode(const std::string& string, ReScanCodeIndex sindex);
explicit ReScanCode(const std::string& string, unsigned int sindex);
bool operator == (const ReScanCode& rs) const;
bool operator<(const ReScanCode& rhs) const;
ReScanCode(const ReScanCode&) = default;
};
namespace ReScanCodes {
static const ReScanCode Unimplemented ("?", 0);
static const ReScanCode MouseButtonLeft ("ML", ReScanCodeIndex::MLeft);
static const ReScanCode MouseButtonRight ("MR", ReScanCodeIndex::MRight);
static const ReScanCode MouseButtonMiddle ("MM", ReScanCodeIndex::MMiddle);
static const ReScanCode MouseButtonSideA ("M4", ReScanCodeIndex::MSide_A);
static const ReScanCode MouseButtonSideB ("M5", ReScanCodeIndex::MSide_B);
/// NOTE: IsMouseButtonDown will not return correctly for the mouse-wheel-buttons, because the action is effectively instantaneous.
static const ReScanCode MWheelUp ("MWU", ReScanCodeIndex::MWheel_Up);
/// NOTE: IsMouseButtonDown will not return correctly for the mouse-wheel-buttons, because the action is effectively instantaneous.
static const ReScanCode MWheelDown ("MWD", ReScanCodeIndex::MWheel_Down);
// TODO: Encode both Uppercase and Lowercase version for each keymap
static const ReScanCode Escape ("ESC", ReScanCodeIndex::ESCAPE);
static const ReScanCode F1 ("F1", ReScanCodeIndex::F1);
static const ReScanCode F2 ("F2", ReScanCodeIndex::F2);
static const ReScanCode F3 ("F3", ReScanCodeIndex::F3);
static const ReScanCode F4 ("F4", ReScanCodeIndex::F4);
static const ReScanCode F5 {"F5", ReScanCodeIndex::F5};
static const ReScanCode F6 {"F6", ReScanCodeIndex::F6};
static const ReScanCode F7 {"F7", ReScanCodeIndex::F7};
static const ReScanCode F8 {"F8", ReScanCodeIndex::F8};
static const ReScanCode F9 {"F9", ReScanCodeIndex::F9};
static const ReScanCode F10 {"F10", ReScanCodeIndex::F10};
static const ReScanCode F11 {"F11", ReScanCodeIndex::F11};
static const ReScanCode F12 {"F12", ReScanCodeIndex::F12};
static const ReScanCode Print {"PRINT", ReScanCodeIndex::PRINT};
static const ReScanCode ScrollLock {"SCROLL_LOCK", ReScanCodeIndex::SCROLL_LOCK};
static const ReScanCode Break {"BREAK", ReScanCodeIndex::BREAK};
//TODO On Windows, Return & KP_RETURN are the same thing.
static const ReScanCode NumPadReturn {"KP_↵", ReScanCodeIndex::KP_RETURN};
static const ReScanCode NumPadPlus {"KP_+", ReScanCodeIndex::KP_PLUS};
static const ReScanCode NumPadMinus {"KP_-", ReScanCodeIndex::KP_MINUS};
static const ReScanCode NumPadAsterisk {"KP_*", ReScanCodeIndex::KP_ASTERISK};
static const ReScanCode NumPadForwardSlash {"KP_/", ReScanCodeIndex::KP_SLASH};
static const ReScanCode NumPadPeriod {"KP_.", ReScanCodeIndex::KP_PERIOD};
static const ReScanCode NumPadNumLock {"KP_NUMLOCK", ReScanCodeIndex::KP_NUMLOCK};
static const ReScanCode NumPad1 {"KP_1", ReScanCodeIndex::KP_1};
static const ReScanCode NumPad2 {"KP_2", ReScanCodeIndex::KP_2};
static const ReScanCode NumPad3 {"KP_3", ReScanCodeIndex::KP_3};
static const ReScanCode NumPad4 {"KP_4", ReScanCodeIndex::KP_4};
static const ReScanCode NumPad5 {"KP_5", ReScanCodeIndex::KP_5};
static const ReScanCode NumPad6 {"KP_6", ReScanCodeIndex::KP_6};
static const ReScanCode NumPad7 {"KP_7", ReScanCodeIndex::KP_7};
static const ReScanCode NumPad8 {"kP_8", ReScanCodeIndex::KP_8};
static const ReScanCode NumPad9 {"KP_9", ReScanCodeIndex::KP_9};
static const ReScanCode NumPad0 {"KP_0", ReScanCodeIndex::KP_0};
static const ReScanCode Grave {"`", ReScanCodeIndex::GRAVE};
static const ReScanCode One {"1", ReScanCodeIndex::ONE};
static const ReScanCode Two {"2", ReScanCodeIndex::TWO};
static const ReScanCode Three {"3", ReScanCodeIndex::THREE};
static const ReScanCode Four {"4", ReScanCodeIndex::FOUR};
static const ReScanCode Five {"5", ReScanCodeIndex::FIVE};
static const ReScanCode Six {"6", ReScanCodeIndex::SIX};
static const ReScanCode Seven {"7", ReScanCodeIndex::SEVEN};
static const ReScanCode Eight {"8", ReScanCodeIndex::EIGHT};
static const ReScanCode Nine {"9", ReScanCodeIndex::NINE};
static const ReScanCode Zero {"0", ReScanCodeIndex::ZERO};
static const ReScanCode Minus {"-", ReScanCodeIndex::MINUS};
static const ReScanCode Equals {"+", ReScanCodeIndex::EQUALS};
static const ReScanCode Backspace {"", ReScanCodeIndex::BACKSPACE};
static const ReScanCode Tab {"", ReScanCodeIndex::TAB};
static const ReScanCode Q {"Q", ReScanCodeIndex::Q};
static const ReScanCode W {"W", ReScanCodeIndex::W};
static const ReScanCode E {"E", ReScanCodeIndex::E};
static const ReScanCode R {"R", ReScanCodeIndex::R};
static const ReScanCode T {"T", ReScanCodeIndex::T};
static const ReScanCode Y {"Y", ReScanCodeIndex::Y};
static const ReScanCode U {"U", ReScanCodeIndex::U};
static const ReScanCode I {"I", ReScanCodeIndex::I};
static const ReScanCode O {"O", ReScanCodeIndex::O};
static const ReScanCode P {"P", ReScanCodeIndex::P};
static const ReScanCode LeftBracket {"[", ReScanCodeIndex::OPENING_SQUARE_BRACKET};
static const ReScanCode RightBracket {"]", ReScanCodeIndex::CLOSING_SQUARE_BRACKET};
static const ReScanCode BackSlash {"\\", ReScanCodeIndex::BACKSLASH};
static const ReScanCode CapsLock {"CAPS", ReScanCodeIndex::CAPSLOCK};
static const ReScanCode A {"A", ReScanCodeIndex::A};
static const ReScanCode S {"S", ReScanCodeIndex::S};
static const ReScanCode D {"D", ReScanCodeIndex::D};
static const ReScanCode F {"F", ReScanCodeIndex::F};
static const ReScanCode G {"G", ReScanCodeIndex::G};
static const ReScanCode H {"H", ReScanCodeIndex::H};
static const ReScanCode J {"J", ReScanCodeIndex::J};
static const ReScanCode K {"K", ReScanCodeIndex::K};
static const ReScanCode L {"L", ReScanCodeIndex::L};
static const ReScanCode Semicolon {";", ReScanCodeIndex::SEMICOLON};
static const ReScanCode SingeQuote {"\'", ReScanCodeIndex::SINGLEQUOTE};
static const ReScanCode Return {"", ReScanCodeIndex::RETURN};
static const ReScanCode LeftShift {"", ReScanCodeIndex::LEFT_SHIFT};
static const ReScanCode Z {"Z", ReScanCodeIndex::Z};
static const ReScanCode X {"X", ReScanCodeIndex::X};
static const ReScanCode C {"C", ReScanCodeIndex::C};
static const ReScanCode V {"V", ReScanCodeIndex::V};
static const ReScanCode B {"B", ReScanCodeIndex::B};
static const ReScanCode N {"N", ReScanCodeIndex::N};
static const ReScanCode M {"M", ReScanCodeIndex::M};
static const ReScanCode Comma {",", ReScanCodeIndex::COMMA};
static const ReScanCode Period {".", ReScanCodeIndex::PERIOD};
static const ReScanCode ForwardSlash {"/", ReScanCodeIndex::SLASH};
static const ReScanCode LeftControl {"LCTRL", ReScanCodeIndex::LEFT_CTRL};
static const ReScanCode Super {"", ReScanCodeIndex::SUPER};
static const ReScanCode LeftAlt {"🄰", ReScanCodeIndex::LEFT_ALT};
static const ReScanCode Space {" ", ReScanCodeIndex::SPACE};
static const ReScanCode RightAlt {"R🄰", ReScanCodeIndex::RIGHT_ALT};
static const ReScanCode Menu {"", ReScanCodeIndex::MENU};
static const ReScanCode RightControl {"RCTRL", ReScanCodeIndex::RIGHT_CONTROL};
static const ReScanCode RightShift {"R⇧", ReScanCodeIndex::RIGHT_SHIFT};
static const ReScanCode Insert {"INSERT", ReScanCodeIndex::INSERT};
static const ReScanCode Home {"HOME", ReScanCodeIndex::HOME};
static const ReScanCode PageUp {"PAGEUP", ReScanCodeIndex::PAGEUP};
static const ReScanCode Delete {"DELETE", ReScanCodeIndex::DEL};
static const ReScanCode End {"End", ReScanCodeIndex::END};
static const ReScanCode PageDown {"PAGEDOWN", ReScanCodeIndex::PAGEDOWN};
static const ReScanCode UpArrow {(""), ReScanCodeIndex::UP};
static const ReScanCode DownArrow {(""), ReScanCodeIndex::DOWN};
static const ReScanCode LeftArrow {(""), ReScanCodeIndex::LEFT};
static const ReScanCode RightArrow {(""), ReScanCodeIndex::RIGHT};
}
}

View File

@@ -57,9 +57,9 @@ namespace ReWindow {
class MouseWheelEvent : public MouseEvent
{
public:
int wheel_dt;
int WheelMovement;
MouseWheelEvent() = default;
MouseWheelEvent(int wheel) : MouseEvent(), wheel_dt(wheel) {}
MouseWheelEvent(int wheel) : MouseEvent(), WheelMovement(wheel) {}
};
class MouseButtonDownEvent : public MouseEvent {
@@ -67,14 +67,14 @@ namespace ReWindow {
MouseButton Button;
MouseButtonDownEvent() = default;
MouseButtonDownEvent(MouseButton Button) : MouseEvent() {}
MouseButtonDownEvent(MouseButton button) : MouseEvent(), Button(button) {}
};
class MouseButtonUpEvent : public MouseEvent {
public:
MouseButton Button;
MouseButtonUpEvent() = default;
MouseButtonUpEvent(MouseButton Button) : MouseEvent() {}
MouseButtonUpEvent(MouseButton button) : MouseEvent(), Button(button) {}
};
class WindowResizeRequestEvent : public RWindowEvent

View File

@@ -17,6 +17,7 @@ namespace ReWindow
class Gamepad : public InputDevice
{
public:
};

View File

@@ -0,0 +1,39 @@
#pragma once
namespace ReWindow
{
enum class DeviceType {
MOUSE, KEYBOARD, GAMEPAD, TOUCHSCREEN, REMOTE, VRHEADSET
};
enum class DeviceState {
OK, LOW_BATTERY, UNAVAILABLE
};
class InputDeviceImpl { };
class InputDeviceImplLinux { };
class InputDevice {
public:
virtual void Update(double elapsed){}
/// Returns the device's index among devices of the same type.
uint GetIndex() const;
virtual DeviceType GetType() const = 0;
DeviceState GetDeviceState() const;
bool IsAnyButtonDown();
};
struct InputEvent
{
std::chrono::system_clock::time_point Timestamp;
};
struct BinaryActuatorState
{
double TimeHeld;
bool Pressed;
};
}

View File

@@ -0,0 +1,32 @@
#pragma once
#include <rewindow/types/mouse.h>
namespace ReWindow
{
namespace detail
{
static Mouse* internal_mouse;
void UpdateMouse(Mouse* mouse) {
detail::internal_mouse = mouse;
}
}
namespace InputService
{
namespace Mouse
{
Vector2 GetMousePosition()
{
return detail::internal_mouse->GetPosition();
}
bool IsButtonDown(const MouseButton &btn) {
return detail::internal_mouse->IsButtonDown(btn);
}
}
}

View File

@@ -19,9 +19,9 @@ private:
public:
static std::vector<Key> GetKeyboard();
Key();
Key() = default;
Key(const char* charcode, X11Scancode scancode, WindowsScancode wSc);
const char* CharCode;
std::string Mnemonic;
X11Scancode x11ScanCode;
WindowsScancode winScanCode;
bool operator==(const Key& rhs) const;
@@ -147,7 +147,7 @@ namespace Keys {
}
enum class KeyState {Pressed, Released};

View File

@@ -10,13 +10,40 @@
#pragma once
#include <rewindow/types/inputdevice.hpp>
#include "rewindow/data/scancode.h"
#include <Event.h>
#include <map>
namespace ReWindow
{
class InputDevice {};
using Key = ReScanCode;
using KeyState = BinaryActuatorState;
struct KeyboardState
{
std::map<Key, KeyState> Keys;
};
class KeyPressedEvent : public InputEvent {};
class KeyReleasedEvent : public InputEvent {};
class Keyboard : public InputDevice
{
public:
Keyboard() {}
~Keyboard() {}
bool IsKeyDown() const;
KeyState GetKeyState() const;
void Update(double elapsed) override;
DeviceType GetType() const override { return DeviceType::KEYBOARD;}
public:
static Event<const KeyPressedEvent&> KeyPressed;
static Event<const KeyReleasedEvent&> KeyReleased;
public:
static Keyboard* GetCurrentDevice();
static bool GetKeyState(const Key& k);
static KeyboardState GetState();
};
}

View File

@@ -10,17 +10,329 @@
#pragma once
//#include <rewindow/types/inputdevice.hpp>
#include <rewindow/types/mousebutton.h>
#include <rewindow/types/key.h>
#include <Event.h>
#include <map>
#include "rewindow/data/scancode.h"
#include <rewindow/types/inputdevice.hpp>
//#include <rewindow/types/keyboard.h>
//#include <rewindow/types/gamepad.h>
namespace ReWindow
{
class InputDevice {}; // TODO: Remember to break InputDevice into it's own file and not define it twice!!!
//using Key = ReScanCode;
class Pointer : public InputDevice {};
using Button = ReScanCode;
/*
enum class DeviceType {
MOUSE, KEYBOARD, GAMEPAD, TOUCHSCREEN, REMOTE, VRHEADSET
};
enum class DeviceState {
OK, LOW_BATTERY, UNAVAILABLE
};
*/
class Mouse : public Pointer
/*
class InputDeviceImpl { };
class InputDeviceImplLinux { };
*/
class LinuxMouseImpl { };
class LinuxKeyboardImpl {};
// TODO: Remember to break InputDevice into it's own file and not define it twice!!!
/*
class InputDevice {
public:
virtual void Update(double elapsed){}
/// Returns the device's index among devices of the same type.
uint GetIndex() const;
virtual DeviceType GetType() const = 0;
DeviceState GetDeviceState() const;
bool IsAnyButtonDown();
};
*/
/*
struct BinaryActuatorState
{
double TimeHeld;
bool Pressed;
};
*/
using ButtonState = BinaryActuatorState;
//using KeyState = BinaryActuatorState;
/*
struct KeyboardState
{
std::map<Key, KeyState> Keys;
};
*/
/*class MouseState {
public:
struct
{
bool LMB = false;
bool RMB = false;
bool MMB = false;
bool SideButton1 = false;
bool SideButton2 = false;
bool MWheelUp = false;
bool MWheelDown = false;
} Buttons;
Vector2 Position;
int Wheel = 0;
[[nodiscard]] bool IsDown(const MouseButton& btn) const
{
if (btn == MouseButtons::Left) return Buttons.LMB;
if (btn == MouseButtons::Right) return Buttons.RMB;
if (btn == MouseButtons::Middle) return Buttons.MMB;
if (btn == MouseButtons::Mouse4) return Buttons.SideButton1;
if (btn == MouseButtons::Mouse5) return Buttons.SideButton2;
//if (btn == MouseButtons::MWheelUp) return Buttons.MWheelUp;
//if (btn == MouseButtons::MWheelDown) return Buttons.MWheelDown;
return false; // Unknown button?
}
void Set(const MouseButton& btn, bool state)
{
if (btn == MouseButtons::Left) Buttons.LMB = state;
if (btn == MouseButtons::Right) Buttons.RMB = state;
if (btn == MouseButtons::Middle) Buttons.MMB = state;
if (btn == MouseButtons::Mouse4) Buttons.SideButton1 = state;
if (btn == MouseButtons::Mouse5) Buttons.SideButton2 = state;
//if (btn == MouseButtons::MWheelUp) Buttons.MWheelUp = state;
//if (btn == MouseButtons::MWheelDown) Buttons.MWheelDown = state;
}
bool& operator[](const MouseButton& btn)
{
if (btn == MouseButtons::Left) return Buttons.LMB;
if (btn == MouseButtons::Right) return Buttons.RMB;
if (btn == MouseButtons::Middle) return Buttons.MMB;
if (btn == MouseButtons::Mouse4) return Buttons.SideButton1;
if (btn == MouseButtons::Mouse5) return Buttons.SideButton2;
//if (btn == MouseButtons::MWheelUp) return Buttons.MWheelUp;
//if (btn == MouseButtons::MWheelDown) return Buttons.MWheelDown;
throw std::runtime_error("Attempted to handle unmapped mouse button");
}
};*/
struct MouseState
{
ButtonState Left;
ButtonState Right;
ButtonState Middle;
ButtonState Mouse4;
ButtonState Mouse5;
ButtonState WheelUp;
ButtonState WheelDown;
Vector2 Position;
Vector2 Delta;
Vector2 Velocity;
int Wheel;
};
struct ThumbstickState
{
Vector2 Unit;
};
class Thumbstick
{
};
struct TriggerState
{
};
class Trigger
{
};
struct GamepadState
{
ButtonState X;
ButtonState Y;
ButtonState A;
ButtonState B;
// TODO: Also have analog DPad;
ButtonState DPadLeft;
ButtonState DPadRight;
ButtonState DPadUp;
ButtonState DPadDown;
ButtonState Pause;
ButtonState Select;
ButtonState Home;
ButtonState LeftThumbButton;
ButtonState RightThumbButton;
TriggerState LeftTrigger;
TriggerState RightTrigger;
ThumbstickState LeftThumbstick;
ThumbstickState RightThumbstick;
};
class TouchscreenState
{
};
/*
* Gamepad
*/
constexpr int MaxDeviceStateHistory = 100;
class Gamepad : public InputDevice
{
public:
uint GetButtonCount() const;
uint GetRumbleMotorCount() const;
protected:
Thumbstick LeftThumb;
Thumbstick RightThumb;
Trigger RightTrigger;
Trigger LeftTrigger;
GamepadState current_state;
GamepadState previous_state;
GamepadState history[MaxDeviceStateHistory];
};
class XboxGamepad : public Gamepad {};
class PS4Gamepad : public Gamepad {};
/*
* Pointer
*/
class PointerDevice : public InputDevice {
public:
void Update(double elapsed) override {
InputDevice::Update(elapsed);
}
};
/*
struct InputEvent
{
std::chrono::system_clock::time_point Timestamp;
};
*/
struct MouseButtonEvent : public InputEvent
{
};
class MouseMoveEvent
{
};
class MouseWheelEvent
{
};
struct TouchEvent
{
int TouchID;
Vector2 Position;
Vector2 Velocity;
float Pressure;
};
class Mouse : public PointerDevice
{
public:
public:
Event<> Moved;
Event<> ButtonPressed;
Event<> ButtonReleased;
Event<> WheelMoved;
public:
Mouse();
~Mouse();
Vector2 GetPosition() const;
bool IsButtonDown(const MouseButton& btn) const;
ButtonState GetButtonState(const MouseButton& btn) const;
void Update(double elapsed) override;
DeviceType GetType() const override;
void DoButtonPress(const MouseButton& btn);
void DoButtonRelease(const MouseButton& btn);
void DoMove(const Vector2& pos);
void DoScrollWheel(int scroll);
protected:
void SetButtonPressed(const MouseButton& btn, bool pressed);
void SetButtonState(const MouseButton& btn, ButtonState state);
protected:
MouseState current_state;
MouseState previous_state;
MouseState history[MaxDeviceStateHistory];
public:
protected:
private:
};
class Touchscreen : public PointerDevice
{
public:
static Event<const TouchEvent&> TouchMoved;
static Event<const TouchEvent&> TouchPressed;
static Event<const TouchEvent&> TouchReleased;
static Touchscreen GetCurrentDevice();
int GetPosition(int touchIndex);
int GetTouchCount();
int GetMaxTouchCount();
std::vector<int> GetTouches();
/// Returns the pressure of the touch-press. Most touch screens aren't pressure sensitive, in which case the pressure will be 1.
float GetPressure(int touchIndex);
};
class VRHeadset : public InputDevice { };
class GenericTVRemote : public InputDevice { };
}

View File

@@ -10,13 +10,16 @@
#pragma once
#include <string>
class MouseButton {
public:
MouseButton();
explicit MouseButton(const char* charcode, unsigned int index);
const char* CharCode;
std::string Mnemonic;
unsigned int ButtonIndex;
MouseButton() = default;
explicit MouseButton(const std::string& charcode, unsigned int index);
bool operator == (const MouseButton& mb) const;
bool operator<(const MouseButton& rhs) const;
MouseButton(const MouseButton&) = default;
@@ -25,14 +28,17 @@ public:
namespace MouseButtons
{
static const MouseButton Left {"l", 1};
static const MouseButton Right {"r", 2};
static const MouseButton Middle {"m", 3};
static const MouseButton MWheelUp {"1", 4};
static const MouseButton MWheelDown {"2", 5};
static const MouseButton Mouse4 {"4", 8};
static const MouseButton Mouse5 {"5", 9};
static const MouseButton Unimplemented {"u", 0};
static const MouseButton Left ("L", 1);
static const MouseButton Right ("R", 2);
static const MouseButton Middle ("M", 3);
static const MouseButton Mouse4 ("4", 8);
static const MouseButton Mouse5 ("5", 9);
static const MouseButton Unimplemented ("?", 0);
/// NOTE: IsMouseButtonDown will not return correctly for the mouse-wheel-buttons, because the action is effectively instantaneous.
static const MouseButton MWheelUp ("U", 4);
/// NOTE: IsMouseButtonDown will not return correctly for the mouse-wheel-buttons, because the action is effectively instantaneous.
static const MouseButton MWheelDown ("D", 5);
}
MouseButton GetMouseButtonFromXButton(unsigned int button);

View File

@@ -4,13 +4,16 @@
#include <Event.h>
#include <map>
#include <thread>
#include <rewindow/types/key.h>
//#include <rewindow/types/key.h>
#include <rewindow/types/cursors.h>
#include <rewindow/types/mousebutton.h>
#include <rewindow/types/gamepadbutton.h>
#include <J3ML/LinearAlgebra.hpp>
#include <rewindow/types/mouse.h>
#include <rewindow/types/WindowEvents.hpp>
#include <rewindow/types/keyboard.h>
#include <rewindow/types/gamepad.h>
#include <J3ML/LinearAlgebra.hpp>
#include <format>
enum class RWindowFlags: uint8_t {
IN_FOCUS,
@@ -29,28 +32,10 @@ enum class RenderingAPI: uint8_t {
VULKAN = 1,
};
namespace ReWindow
{
using J3ML::LinearAlgebra::Vector2;
class KeyboardState {
public:
std::map<Key, bool> PressedKeys;
};
class GamepadState {
public:
std::map<GamepadButton, bool> PressedButtons;
};
class MouseState {
public:
std::map<MouseButton, bool> PressedBtns;
Vector2 Pos;
};
/// RWindow is a class implementation of a platform-independent window abstraction.
/// This library also provides abstractions for user-input devices, and their interaction with the window.
class RWindow {
@@ -79,6 +64,8 @@ namespace ReWindow
Event<MouseButtonDownEvent> OnMouseButtonDownEvent;
Event<MouseButtonUpEvent> OnMouseButtonUpEvent;
Event<MouseWheelEvent> OnMouseWheelEvent;
Event<InputDevice*, InputEvent> OnInputDeviceEvent;
#pragma endregion
/// These methods can also be overridden in derived classes.
@@ -106,11 +93,37 @@ namespace ReWindow
virtual void OnMouseWheel(const MouseWheelEvent&) {}
#pragma endregion
Mouse* GetMouse();
Keyboard* GetKeyboard();
#pragma region Input Helpers
/// Returns a Vector2 representing mouse coordinates relative to the top-left corner of the window.
/// This result is cached from the operating-system, and as such may be out-of-date.
/// @see GetAccurateMouseCoordinates().
Vector2 GetMouseCoordinates() const;
int GetMouseWheelPersistent() const { return currentMouse.Wheel;}
#pragma endregion
#pragma region Cursor Modifiers
void SetCursorStyle(CursorStyle style) const;
void SetCursorCustomIcon() const;
void SetCursorLocked();
void SetCursorCenter();
void RestoreCursorFromLastCenter(); // Feels nicer for users
/// Hides the cursor when it's inside of our window. Useful for 3D game camera.
void SetCursorVisible(bool cursor_enable);
bool GetCursorVisible();
#pragma endregion
/// Sets which rendering API is to be used with this window.
void SetRenderer(RenderingAPI api);
@@ -149,7 +162,7 @@ namespace ReWindow
[[nodiscard]] bool IsKeyDown(Key key) const;
/// Returns whether the given mouse button is currently being pressed.
[[nodiscard]] bool IsMouseButtonDown(MouseButton button) const;
[[nodiscard]] bool IsMouseButtonDown(const MouseButton &button) const;
/// Sets whether or not to make the window fullscreen.
/// @note This is implemented per-OS, and as such, it simply requests the OS to do what we want. No guarantee about follow-through can be given.
@@ -214,7 +227,7 @@ namespace ReWindow
Vector2 GetSize() const;
/// Returns the position of the "renderable area" of the window relative to it's top left corner.
/// (used to account for the width or the border & title bar).
/// (used to account for the width of the border & title bar).
Vector2 GetPositionOfRenderableArea() const;
/// Requests the operating system to move the window to the specified coordinates on the display.
@@ -233,20 +246,6 @@ namespace ReWindow
/// NOTE: The implementation is defined per-OS, and thus there is no guarantee of it always working.
void Lower() const;
void SetCursorStyle(CursorStyle style) const;
void SetCursorCustomIcon() const;
void SetCursorLocked();
void SetCursorCenter();
void RestoreCursorFromLastCenter(); // Feels nicer for users
/// Hides the cursor when it's inside of our window. Useful for 3D game camera.
void SetCursorVisible(bool cursor_enable);
bool GetCursorVisible();
/// Calls OpenGL's SwapBuffers routine.
/// NOTE: This is only used when the underlying rendering API is set to OpenGL.
@@ -277,6 +276,10 @@ namespace ReWindow
/// Requests the operating system to take the window out of fullscreen mode. Previously saved window size is restored, if possible.
void RestoreFromFullscreen();
bool DetectsMouse();
bool DetectsKeyboard();
bool DetectsGamepad();
protected:
#pragma region Data
@@ -295,10 +298,12 @@ namespace ReWindow
bool flags[5];
std::vector<RWindowEvent> eventLog;
KeyboardState currentKeyboard; // Current Frame's Keyboard State
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
MouseState currentMouse;
MouseState previousMouse;
/// Input Devices;
Mouse* mouse;
Keyboard* keyboard;
Gamepad* gamepad;
std::vector<InputDevice*> input_devices;
RenderingAPI renderer = RenderingAPI::OPENGL;
@@ -340,9 +345,9 @@ namespace ReWindow
/// @note This will be invoked **after** the window-open procedure completes.
void processOnOpen();
/// Executes event handlers for mouse press events.
void processMousePress(MouseButton btn);
void processMousePress(const MouseButton& btn);
/// Executes event handlers for mouse release events.
void processMouseRelease(MouseButton btn);
void processMouseRelease(const MouseButton& btn);
/// Executes event handlers for window focus events.
void processFocusIn();
/// Executes event handlers for window unfocus events.

View File

@@ -32,6 +32,28 @@ class MyWindow : public ReWindow::RWindow {
auto pos = GetMouseCoordinates();
//std::cout << pos.x << ", " << pos.y << std::endl;
//if (IsMouseButtonDown(MouseButtons::MWheelUp))
//std::cout << "WheelUp Mouse Button" << std::endl;
//if (IsMouseButtonDown(MouseButtons::MWheelDown))
//std::cout << "WheelDown Mouse Button" << std::endl;
if (IsMouseButtonDown(MouseButtons::Left))
std::cout << "Left Mouse Button" << std::endl;
if (IsMouseButtonDown(MouseButtons::Right))
std::cout << "Right Mouse Button" << std::endl;
if (IsMouseButtonDown(MouseButtons::Middle))
std::cout << "Middle Mouse Button" << std::endl;
if (IsMouseButtonDown(MouseButtons::Mouse4))
std::cout << "Mouse4 Mouse Button" << std::endl;
if (IsMouseButtonDown(MouseButtons::Mouse5))
std::cout << "Mouse5 Mouse Button" << std::endl;
RWindow::OnRefresh(elapsed);
}
@@ -39,6 +61,18 @@ class MyWindow : public ReWindow::RWindow {
std::cout << "resized to " << e.Size.x << ", " << e.Size.y << std::endl;
return true;
}
void OnMouseButtonDown(const ReWindow::MouseButtonDownEvent &e) override
{
std::cout << "Overload Down: " << e.Button.Mnemonic << std::endl;
RWindow::OnMouseButtonDown(e);
}
void OnMouseButtonUp(const ReWindow::MouseButtonUpEvent &e) override
{
std::cout << "Overload Up: " << e.Button.Mnemonic << std::endl;
RWindow::OnMouseButtonUp(e);
}
};
int main() {
@@ -95,14 +129,19 @@ int main() {
window->OnKeyDownEvent += [&] (ReWindow::KeyDownEvent e) {
jlog::Debug(e.key.CharCode);
jlog::Debug(e.key.Mnemonic);
};
window->OnMouseButtonDownEvent += [&] (ReWindow::MouseButtonDownEvent e) {
jlog::Debug(e.Button.CharCode);
jlog::Debug(e.Button.Mnemonic + std::to_string(e.Button.ButtonIndex));
};
window->OnMouseWheelEvent += [&, window] (ReWindow::MouseWheelEvent e)
{
std::cout << window->GetMouseWheelPersistent() << std::endl;
};
while (window->IsAlive()) {
window->PollEvents();
window->ManagedRefresh();

View File

@@ -81,13 +81,20 @@ void RWindow::SetCursorVisible(bool cursor_enable) {
}
void RWindow::SetResizable(bool sizable) {
XGetWindowAttributes(display,window,&windowAttributes);
this->resizable = sizable;
if (!sizable)
{
Logger::Debug("Once you've done this you cannot make it resizable again.");
hints.flags = PMinSize | PMaxSize;
hints.min_width = hints.max_width = windowAttributes.width;
hints.min_height = hints.max_height = windowAttributes.height;
XSetWMNormalHints(display, window, &hints);
}
//this->SetFlag(RWindowFlags::RESIZABLE, resizable);
Logger::Debug("Once you've done this you cannot make it resizable again.");
hints.flags = PMinSize | PMaxSize;
hints.min_width = hints.max_width = windowAttributes.width;
hints.min_height = hints.max_height = windowAttributes.height;
XSetWMNormalHints(display, window, &hints);
}
void RWindow::SetFlag(RWindowFlags flag, bool state) {
@@ -159,18 +166,34 @@ void RWindow::PollEvents() {
}
if (xev.type == ButtonRelease) {
Logger::Debug(std::format("Recieved event '{}'", "ButtonRelease"));
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
processMouseRelease(button);
// Mouse Wheel fires both the ButtonPress and ButtonRelease instantaneously.
// Therefore, we handle it as a specific MouseWheel event rather than a MouseButton event,
// and only call on ButtonPress, otherwise it will appear to duplicate the mouse wheel scroll.
if (xev.xbutton.button != 4 && xev.xbutton.button != 5) {
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
Logger::Debug(std::format("Recieved event '{}'", "ButtonRelease"));
processMouseRelease(button);
}
}
if (xev.type == ButtonPress) {
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
Logger::Debug(std::format("Window event: MouseButtonPress {}", button.CharCode));
// Mouse Wheel fires both the ButtonPress and ButtonRelease instantaneously.
// Therefore, we handle it as a specific MouseWheel event rather than a MouseButton event,
// and only call on ButtonPress, otherwise it will appear to duplicate the mouse wheel scroll.
if (xev.xbutton.button == 4) {
processMouseWheel(-1);
} else if (xev.xbutton.button == 5) {
processMouseWheel(1);
} else
{
MouseButton button = GetMouseButtonFromXButton(xev.xbutton.button);
processMousePress(button);
Logger::Debug(std::format("Window event: MouseButtonPress {}", button.Mnemonic));
processMousePress(button);
}
}
if (xev.type == Expose)
@@ -206,7 +229,7 @@ void RWindow::PollEvents() {
}
previousKeyboard = currentKeyboard;
previousMouse.PressedBtns = currentMouse.PressedBtns;
previousMouse.Buttons = currentMouse.Buttons;
}
@@ -350,7 +373,7 @@ void RWindow::Open() {
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap);
SubstructureNotifyMask | CWColormap );
XMapWindow(display, window);
XStoreName(display, window, title.c_str());
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);

View File

@@ -1,5 +1,7 @@
#include <rewindow/types/window.h>
#include "rewindow/logger/logger.h"
#include <rewindow/types/mouse.h>
std::string RWindowFlagToStr(RWindowFlags flag) {
switch (flag) {
case RWindowFlags::IN_FOCUS: return "IN_FOCUS";
@@ -15,10 +17,35 @@ std::string RWindowFlagToStr(RWindowFlags flag) {
using namespace ReWindow;
RWindow::RWindow() {
title = "ReWindow Application";
width = 640;
height = 480;
if (DetectsKeyboard())
{
keyboard = new Keyboard();
input_devices.push_back(keyboard);
}
if (DetectsMouse())
{
mouse = new Mouse();
input_devices.push_back(mouse);
}
if (DetectsGamepad())
{
gamepad = new Gamepad();
input_devices.push_back(gamepad);
}
//RWindow::singleton = this;
}
@@ -33,7 +60,7 @@ RWindow::RWindow(const std::string& title, int width, int height, RenderingAPI r
title(title), width(width), height(height), renderer(renderer) {}
Vector2 RWindow::GetMouseCoordinates() const {
return currentMouse.Pos;
return mouse->GetPosition();
}
bool RWindow::GetFlag(RWindowFlags flag) const {
@@ -66,9 +93,9 @@ void RWindow::processFocusOut()
OnFocusLostEvent(event);
}
void RWindow::processMousePress(MouseButton btn)
void RWindow::processMousePress(const MouseButton& btn)
{
currentMouse.PressedBtns[btn] = true;
mouse->DoButtonPress(btn); // .Set(btn, true);
auto eventData = MouseButtonDownEvent(btn);
OnMouseButtonDown(eventData);
OnMouseButtonDownEvent(eventData);
@@ -77,15 +104,15 @@ void RWindow::processMousePress(MouseButton btn)
void RWindow::processMouseMove(Vector2 last_pos, Vector2 new_pos)
{
currentMouse.Pos = new_pos;
currentMouse.Position = new_pos;
auto eventData = MouseMoveEvent(new_pos);
OnMouseMove(eventData);
OnMouseMoveEvent(eventData);
}
void RWindow::processMouseRelease(MouseButton btn)
void RWindow::processMouseRelease(const MouseButton& btn)
{
currentMouse.PressedBtns[btn] = false;
currentMouse.Set(btn, false);
auto eventData = MouseButtonUpEvent(btn);
OnMouseButtonUp(eventData);
OnMouseButtonUpEvent(eventData);
@@ -147,15 +174,16 @@ bool RWindow::IsKeyDown(Key key) const {
return false;
}
bool RWindow::IsMouseButtonDown(MouseButton button) const {
bool RWindow::IsMouseButtonDown(const MouseButton &button) const {
// TODO: Implement MouseButton map
for (const auto& pair : currentMouse.PressedBtns)
if (pair.first == button && pair.second)
return true;
return currentMouse.IsDown(button);
}
void RWindow::processKeyPress(Key key) {
currentKeyboard.PressedKeys[key] = true;
Input::
auto eventData = KeyDownEvent(key);
OnKeyDown(eventData);
OnKeyDownEvent(key);
@@ -189,14 +217,16 @@ void RWindow::Refresh() {
OnRefresh(delta_time);
// Only call once and cache the result.
currentMouse.Pos = GetAccurateMouseCoordinates();
currentMouse.Position = GetAccurateMouseCoordinates();
/// TODO: Implement optional minimum epsilon to trigger a Mouse Update.
if (currentMouse.Pos != previousMouse.Pos) {
processMouseMove(previousMouse.Pos, currentMouse.Pos);
previousMouse.Pos = currentMouse.Pos;
if (currentMouse.Position != previousMouse.Position) {
processMouseMove(previousMouse.Position, currentMouse.Position);
previousMouse.Position = currentMouse.Position;
}
detail::UpdateMouse(currentMouse);
}
float RWindow::ComputeElapsedFrameTimeSeconds(std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point end) {
@@ -225,9 +255,12 @@ void RWindow::UpdateFrameTiming(float frame_time) {
void RWindow::processMouseWheel(int scrolls)
{
currentMouse.Wheel += scrolls;
auto ev = MouseWheelEvent(scrolls);
OnMouseWheel(ev);
OnMouseWheelEvent(ev);
previousMouse.Wheel = currentMouse.Wheel;
}

View File

@@ -0,0 +1 @@
#include <rewindow/types/inputdevice.hpp>

View File

@@ -0,0 +1,29 @@
#include <rewindow/types/inputservice.hpp>
namespace ReWindow {
namespace detail
{
static Mouse* internal_mouse;
void UpdateMouse(Mouse* mouse) {
detail::internal_mouse = mouse;
}
}
namespace InputService
{
namespace Mouse
{
Vector2 GetMousePosition()
{
return detail::internal_mouse->GetPosition();
}
bool IsButtonDown(const MouseButton &btn) {
return detail::internal_mouse->IsButtonDown(btn);
}
}
}
}

View File

@@ -4,26 +4,22 @@
std::vector<Key> Key::GetKeyboard() { return keyboard; }
Key::Key() {
keyboard.push_back(*this);
}
Key::Key(const char* charcode, X11Scancode scancode, WindowsScancode sc)
: CharCode(charcode), x11ScanCode(scancode), winScanCode(sc)
: Mnemonic(charcode), x11ScanCode(scancode), winScanCode(sc)
{
//TODO doing this is what crashes the program.
keyboard.push_back(*this);
}
bool Key::operator==(const Key &rhs) const {
//This is not a good workaround.
return (this->x11ScanCode == rhs.x11ScanCode);
}
bool Key::operator<(const Key &rhs) const {
return (this->CharCode < rhs.CharCode);
return (this->Mnemonic < rhs.Mnemonic);
}

1
src/types/keyboard.cpp Normal file
View File

@@ -0,0 +1 @@
#include <rewindow/types/keyboard.h>

82
src/types/mouse.cpp Normal file
View File

@@ -0,0 +1,82 @@
#include <rewindow/types/mouse.h>
namespace ReWindow
{
namespace detail
{
static Mouse* internal_mouse;
void UpdateMouse(Mouse* mouse) {
detail::internal_mouse = mouse;
}
}
namespace InputService
{
namespace Mouse
{
Vector2 GetMousePosition()
{
return detail::internal_mouse->GetPosition();
}
bool IsButtonDown(const MouseButton &btn) {
return detail::internal_mouse->IsButtonDown(btn);
}
}
}
void Mouse::DoButtonPress(const MouseButton &btn) {
SetButtonPressed(btn, true);
}
void Mouse::DoButtonRelease(const MouseButton &btn) {
SetButtonPressed(btn, false);
}
DeviceType Mouse::GetType() const { return DeviceType::MOUSE;}
void Mouse::Update(double elapsed) {
current_state.Left.TimeHeld += elapsed;
current_state.Right.TimeHeld += elapsed;
current_state.Middle.TimeHeld += elapsed;
current_state.Mouse4.TimeHeld += elapsed;
current_state.Mouse5.TimeHeld += elapsed;
previous_state = current_state;
}
ButtonState Mouse::GetButtonState(const MouseButton &btn) const {
if (btn == MouseButtons::Left) return current_state.Left;
if (btn == MouseButtons::Right) return current_state.Right;
if (btn == MouseButtons::Middle) return current_state.Middle;
if (btn == MouseButtons::Mouse4) return current_state.Mouse4;
if (btn == MouseButtons::Mouse5) return current_state.Mouse5;
}
bool Mouse::IsButtonDown(const MouseButton &btn) const {
return GetButtonState(btn).Pressed;
}
Mouse::~Mouse() {}
Mouse::Mouse() {}
void Mouse::SetButtonPressed(const MouseButton &btn, bool pressed) {
auto state = GetButtonState(btn);
state.Pressed = pressed;
state.TimeHeld = 0;
SetButtonState(btn, state);
}
void Mouse::SetButtonState(const MouseButton &btn, ButtonState state) {
if (btn == MouseButtons::Left) current_state.Left = state;
if (btn == MouseButtons::Right) current_state.Right = state;
if (btn == MouseButtons::Middle) current_state.Middle = state;
if (btn == MouseButtons::Mouse4) current_state.Mouse4 = state;
if (btn == MouseButtons::Mouse5) current_state.Mouse5 = state;
}
}

View File

@@ -1,12 +1,11 @@
#include <rewindow/types/mousebutton.h>
#include <string>
#include <X11/X.h>
#include "rewindow/logger/logger.h"
MouseButton::MouseButton() {
}
MouseButton::MouseButton(const char* charcode, unsigned int index) {
this->CharCode = charcode;
MouseButton::MouseButton(const std::string& charcode, unsigned int index) {
this->Mnemonic = charcode;
this->ButtonIndex = index;
}
@@ -15,7 +14,7 @@ bool MouseButton::operator==(const MouseButton &mb) const {
}
bool MouseButton::operator<(const MouseButton &rhs) const {
return (this->CharCode < rhs.CharCode);
return (this->ButtonIndex < rhs.ButtonIndex);
}
@@ -24,8 +23,8 @@ MouseButton GetMouseButtonFromXButton(unsigned int button) {
case 1: return MouseButtons::Left;
case 2: return MouseButtons::Middle;
case 3: return MouseButtons::Right;
case 4: return MouseButtons::MWheelUp;
case 5: return MouseButtons::MWheelDown;
//case 4: return MouseButtons::MWheelUp;
//case 5: return MouseButtons::MWheelDown;
//For *whatever* reason. These aren't in X.h
case 8: return MouseButtons::Mouse4;
case 9: return MouseButtons::Mouse5;