Temporary Input namespace which will become part of ReInput subproject later.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m54s

This commit is contained in:
2024-12-11 01:36:56 -05:00
parent 1950aee948
commit 74d5f13c06
3 changed files with 97 additions and 28 deletions

View File

@@ -11,10 +11,9 @@ namespace ReWindow {
using precision_timestamp = precision_clock::time_point;
class TimestampedEvent {
private:
public:
precision_timestamp Timestamp;
explicit TimestampedEvent(precision_timestamp timestamp) : Timestamp(timestamp) {}
TimestampedEvent() : Timestamp(precision_clock::now())
{ }
};
@@ -24,15 +23,43 @@ namespace ReWindow {
RWindowEvent() : TimestampedEvent() { }
};
class KeyboardEvent : public RWindowEvent {
class InputDeviceEvent : public RWindowEvent {
public:
InputDeviceEvent() : RWindowEvent() { }
};
class KeyboardEvent : public InputDeviceEvent {
public:
Key key;
KeyState state;
KeyboardEvent(Key key, KeyState state) : RWindowEvent(), key(key), state(state) {}
KeyboardEvent(const Key& key, KeyState state) : InputDeviceEvent(), key(key), state(state) {}
};
class MouseEvent : public RWindowEvent {};
class MouseEvent : public InputDeviceEvent {};
class GamepadEvent : public RWindowEvent {};
class GamepadEvent : public InputDeviceEvent {};
class MouseButtonEvent : public MouseEvent {
public:
MouseButton Button;
bool Pressed; // TODO: replace with MouseButtonState enum
MouseButtonEvent() = default;
MouseButtonEvent(const MouseButton& button, bool pressed) : MouseEvent(), Button(button), Pressed(pressed) {}
};
class MouseButtonDownEvent : public MouseButtonEvent {
public:
MouseButton Button;
MouseButtonDownEvent() = default;
MouseButtonDownEvent(const MouseButton& button) : MouseButtonEvent(button, true) {}
};
class MouseButtonUpEvent : public MouseButtonEvent {
public:
MouseButtonUpEvent() = default;
MouseButtonUpEvent(const MouseButton& button) : MouseButtonEvent(button, true) {}
};
class MouseMoveEvent : public MouseEvent {
public:
@@ -46,12 +73,12 @@ namespace ReWindow {
class KeyDownEvent : public KeyboardEvent {
public:
KeyDownEvent(Key key) : KeyboardEvent(key, KeyState::Pressed) {}
explicit KeyDownEvent(const Key& key) : KeyboardEvent(key, KeyState::Pressed) {}
};
class KeyUpEvent : public KeyboardEvent {
public:
KeyUpEvent(Key key) : KeyboardEvent(key, KeyState::Released) {}
explicit KeyUpEvent(const Key& key) : KeyboardEvent(key, KeyState::Released) {}
};
class MouseWheelEvent : public MouseEvent
@@ -62,22 +89,9 @@ namespace ReWindow {
MouseWheelEvent(int wheel) : MouseEvent(), WheelMovement(wheel) {}
};
class MouseButtonDownEvent : public MouseEvent {
public:
MouseButton Button;
MouseButtonDownEvent() = default;
MouseButtonDownEvent(MouseButton button) : MouseEvent(), Button(button) {}
};
class MouseButtonUpEvent : public MouseEvent {
public:
MouseButton Button;
MouseButtonUpEvent() = default;
MouseButtonUpEvent(MouseButton button) : MouseEvent(), Button(button) {}
};
class WindowResizeRequestEvent : public RWindowEvent
class WindowResizeRequestEvent : public MouseButtonEvent
{
public:
Vector2 Size;

View File

@@ -100,6 +100,7 @@ namespace ReWindow
}
};
// TODO: Refactor RenderingAPI into a polymorphic class interface for greater reusability.
class IRenderer {
public:
virtual void Initialize();
@@ -115,7 +116,25 @@ namespace ReWindow
class GLXRenderer : public GLRenderBase {};
class WGLRenderer : public GLRenderBase {};
// TODO: Refactor RenderingAPI into a polymorphic class interface for greater reusability.
// Temporary static input service namespace, TODO: this will be refactored as part of ReInput later.
namespace Input {
inline Event<KeyboardEvent> OnKeyboardEvent;
inline Event<KeyboardEvent> OnKeyEvent;
inline Event<KeyDownEvent> OnKeyDown;
inline Event<KeyUpEvent> OnKeyUp;
inline Event<MouseMoveEvent> OnMouseEvent;
inline Event<MouseButtonEvent> OnMouseButtonEvent;
inline Event<MouseMoveEvent> OnMouseMove;
inline Event<MouseButtonDownEvent> OnMouseDown;
inline Event<MouseButtonUpEvent> OnMouseUp;
inline Event<MouseWheelEvent> OnMouseWheel;
bool IsKeyDown(const Key& key);
bool IsMouseButtonDown(const MouseButton& button);
Vector2 GetMousePosition();
Vector2 GetWindowSize();
}
/// 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.
@@ -145,6 +164,10 @@ namespace ReWindow
#pragma endregion
#pragma region Static Accessors
static RWindow* GetExtant();
#pragma endregion
/// Bindables are provided for hooking into window instances conveniently, without the need to derive and override for simple use cases.
#pragma region Bindable Events
@@ -213,7 +236,6 @@ namespace ReWindow
/// Closes the window immediately, potentially without allowing finalization to occur.
void ForceClose();
void ForceCloseAndTerminateProgram();
void CloseAndReopenInPlace();

View File

@@ -15,10 +15,13 @@ std::string RWindowFlagToStr(RWindowFlags flag) {
using namespace ReWindow;
static RWindow* extant;
RWindow::RWindow(const std::string& wTitle, int wWidth, int wHeight, RenderingAPI wRenderer, bool wFullscreen, bool wResizable, bool wVsync)
: title(wTitle), width(wWidth), height(wHeight), renderer(wRenderer), fullscreen_mode(wFullscreen), resizable(wResizable), vsync(wVsync),
flags{false,wFullscreen,wResizable,wVsync} {
extant = this;
}
RWindow::~RWindow() {
@@ -70,8 +73,9 @@ void RWindow::processMousePress(const MouseButton& btn)
auto event = MouseButtonDownEvent(btn);
OnMouseButtonDown(event);
OnMouseButtonDownEvent(event);
Input::OnMouseButtonEvent(MouseButtonEvent(btn, true));
Input::OnMouseDown(event);
LogEvent(event);
}
void RWindow::processMouseMove(Vector2 last_pos, Vector2 new_pos)
@@ -80,6 +84,7 @@ void RWindow::processMouseMove(Vector2 last_pos, Vector2 new_pos)
auto event = MouseMoveEvent(new_pos);
OnMouseMove(event);
OnMouseMoveEvent(event);
Input::OnMouseMove(event);
LogEvent(event);
}
@@ -89,6 +94,8 @@ void RWindow::processMouseRelease(const MouseButton& btn)
auto event = MouseButtonUpEvent(btn);
OnMouseButtonUp(event);
OnMouseButtonUpEvent(event);
Input::OnMouseButtonEvent(MouseButtonEvent(btn, false));
Input::OnMouseUp(event);
LogEvent(event);
}
@@ -97,7 +104,10 @@ void RWindow::processKeyRelease(Key key) {
currentKeyboard.PressedKeys[key] = false;
auto event = KeyUpEvent(key);
OnKeyUp(event);
OnKeyUpEvent(key);
OnKeyUpEvent(event);
Input::OnKeyboardEvent(KeyboardEvent(key, KeyState::Released));
Input::OnKeyEvent(KeyboardEvent(key, KeyState::Released));
Input::OnKeyUp(event);
LogEvent(event);
}
@@ -105,7 +115,10 @@ void RWindow::processKeyPress(Key key) {
currentKeyboard.PressedKeys[key] = true;
auto event = KeyDownEvent(key);
OnKeyDown(event);
OnKeyDownEvent(key);
OnKeyDownEvent(event);
Input::OnKeyDown(event);
Input::OnKeyboardEvent(KeyboardEvent(key, KeyState::Pressed));
Input::OnKeyEvent(KeyboardEvent(key, KeyState::Pressed));
LogEvent(event);
}
@@ -272,10 +285,30 @@ bool RWindow::IsFocused() const {
DestroyOSWindowHandle();
}
RWindow * RWindow::GetExtant() {
return extant;
}
void RWindow::ForceCloseAndTerminateProgram() {
ForceClose();
exit(0);
}
bool Input::IsKeyDown(const Key &key) {
return extant->IsKeyDown(key);
}
bool Input::IsMouseButtonDown(const MouseButton &button) {
return extant->IsMouseButtonDown(button);
}
Vector2 Input::GetMousePosition() {
return extant->GetMouseCoordinates();
}
Vector2 Input::GetWindowSize() {
return extant->GetSize();
}