Merge pull request 'Input Class Refactoring' (#11) from litandbased_refactors into main
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 50s

Reviewed-on: #11
This commit is contained in:
2024-07-29 20:08:00 -04:00
12 changed files with 264 additions and 65 deletions

View File

@@ -0,0 +1,26 @@
/// 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 gamepad.h
/// @desc A class that models the functionality of a gamepad / controller device.
/// @edit 2024-07-29
#pragma once
namespace ReWindow
{
class InputDevice {}; // TODO: Remember to break InputDevice into it's own file and not define it twice!!!
class Gamepad : public InputDevice
{
};
class XboxGamepad : public Gamepad {};
class PS4Gamepad : public Gamepad {};
}

View File

@@ -0,0 +1,75 @@
/// 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 gamepadbutton.h
/// @desc GamepadButton class and enumerations to define standard buttons found on a Gamepad
/// @edit 2024-07-29
#include <string>
#include <J3ML/LinearAlgebra.h>
class GamepadButton {
protected:
std::string mnemonic_btn_code;
public:
explicit GamepadButton(const std::string& mnemonic) : mnemonic_btn_code(mnemonic) { }
[[nodiscard]] std::string GetMnemonicButtonCode() const { return mnemonic_btn_code; }
/// Compares two GamepadButtons by their mnemonic button codes, not their activation state.
bool operator ==(const GamepadButton& rhs) const;
};
class GamepadTrigger {
public:
/// Returns a float value between 0-1 representing how much the trigger has been pushed in by.
/// (0 being unpressed, 1 being fully pressed)
float GetActuation() const;
/// TODO: Might be more appropriate in the Gamepad class representation.
void SetActuationThreshold(float minimum = 0.01f) const;
};
class GamepadThumbstick
{
public:
/// Returns a Vector2 value representing the x,y coordinates of the joystick, with 0,0 being the center (at rest).
/// This vector ranges from length = 0 to length = 1 (i.e. the unit circle).
[[nodiscard]] Vector2 GetPosition() const;
/// Sets the deadzone range of the thumbstick.
/// Deadzone controls how far the stick must be moved before any movement is actually reported.
/// This is because the thumbstick at-rest will often still report movement.
/// If gamecode is architected to use the thumbstick position as a direction, without factoring in magnitude, this would cause problems.
void SetDeadzone(float minimum = 0.01f) const;
};
using J3ML::LinearAlgebra::Vector2;
namespace GamepadButtons {
static const GamepadButton X {"X"};
static const GamepadButton Y {"Y"};
static const GamepadButton A {"A"};
static const GamepadButton B {"B"};
static const auto Triangle = Y;
static const auto Square = X;
static const auto Circle = A;
static const auto Cross = B;
static const GamepadButton LeftBumper {"LB"};
static const GamepadButton RightBumper {"RB"};
}
namespace GamepadTriggers
{
static const GamepadTrigger Left;
static const GamepadTrigger Right;
}

View File

@@ -148,41 +148,9 @@ namespace Keys {
}
class GamepadButton {};
class MouseButton {
public:
MouseButton();
explicit MouseButton(const char* charcode);
const char* CharCode;
};
using J3ML::LinearAlgebra::Vector2;
class InputService {
public:
static Vector2 GetLeftJoystick();
static Vector2 GetRightJoystick();
};
namespace GamepadButtons {
static const GamepadButton X;
static const GamepadButton Y;
static const GamepadButton A;
static const GamepadButton B;
}
namespace MouseButtons
{
static const MouseButton Left {"l"};
static const MouseButton Right {"r"};
static const MouseButton Middle {"m"};
static const MouseButton MWheelUp {"1"};
static const MouseButton MWheelDown {"2"};
static const MouseButton Mouse4 {"4"};
static const MouseButton Mouse5 {"5"};
static const MouseButton Unimplemented {"u"};
}
MouseButton GetMouseButtonFromXButton(unsigned int button);
Key GetKeyFromX11Scancode(X11Scancode code);
Key GetKeyFromWindowsScancode(WindowsScancode code);

View File

@@ -0,0 +1,22 @@
/// 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 keyboard.h
/// @desc A class that models the functionality of a keyboard device.
/// @edit 2024-07-29
#pragma once
namespace ReWindow
{
class InputDevice {};
class Keyboard : public InputDevice
{
};
}

View File

@@ -0,0 +1,26 @@
/// 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 keyboard.h
/// @desc A class that models the functionality of a mouse / pointer device.
/// @edit 2024-07-29
#pragma once
namespace ReWindow
{
class InputDevice {}; // TODO: Remember to break InputDevice into it's own file and not define it twice!!!
class Pointer : public InputDevice {};
class Mouse : public Pointer
{
};
}

View File

@@ -0,0 +1,38 @@
/// 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 mousebutton.hpp
/// @desc MouseButton class and defined MouseButtons.
/// @edit 2024-07-29
#pragma once
class MouseButton {
public:
MouseButton();
explicit MouseButton(const char* charcode, unsigned int index);
const char* CharCode;
unsigned int ButtonIndex;
bool operator == (const MouseButton& mb) const;
};
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};
}
MouseButton GetMouseButtonFromXButton(unsigned int button);

View File

@@ -6,6 +6,8 @@
#include <thread>
#include <rewindow/types/key.h>
#include <rewindow/types/cursors.h>
#include <rewindow/types/mousebutton.h>
#include <rewindow/types/gamepadbutton.h>
using namespace std::chrono_literals;
using precision_clock = std::chrono::high_resolution_clock;
@@ -132,6 +134,8 @@ namespace ReWindow
#pragma region Callbacks
/// Bindable Non-intrusive event handlers
/// Use these when you can't override the base window class
Event<> OnOpenEvent;
Event<> OnClosingEvent;
Event<RWindowEvent> OnFocusLostEvent;
Event<RWindowEvent> OnFocusGainEvent;
Event<float> OnRefreshEvent;
@@ -145,6 +149,11 @@ namespace ReWindow
#pragma region Overrides
/// Intrusive virtual methods intended to be overridden in a derived class.
/// Do not stuff any logic into these. Someone WILL override it and forget to call the base.
/// Called upon the window requesting to open.
virtual void OnOpen() {}
/// Called right before the window closes.
virtual void OnClosing() {}
virtual void OnFocusLost(const RWindowEvent& e) {}
virtual void OnFocusGain(const RWindowEvent& e) {}
virtual void OnRefresh(float elapsed) {}
@@ -168,18 +177,10 @@ namespace ReWindow
Vector2 GetMouseCoordinates() const;
// TODO: Is this part of the API interface? Can it be moved to protected?
void liftKey (Key key) {
currentKeyboard.PressedKeys[key] = false;
auto event = ReWindow::WindowEvents::KeyUpEvent(key);
OnKeyUp(event);
}
void liftKey (Key key);
// TODO: Is this part of the API interface? Can it be moved to protected?
void pressKey (Key key) {
currentKeyboard.PressedKeys[key] = true;
auto eventData = KeyDownEvent(key);
OnKeyDown(eventData);
}
void pressKey (Key key);
/// Sets which rendering API is to be used with this window.
void setRenderer(RenderingAPI api);

View File

@@ -164,7 +164,6 @@ void RWindow::pollEvents() {
if (xev.type == ResizeRequest) {
DEBUG(std::format("Recieved event '{}'", "ResizeRequest"));
auto eventData = WindowResizeRequestEvent();
lastKnownWindowSize = eventData.Size;
eventData.Size = {(float)xev.xresizerequest.width, (float)xev.xresizerequest.height};
lastKnownWindowSize = eventData.Size;
OnResizeRequest(eventData);
@@ -350,5 +349,8 @@ void RWindow::pollEvents() {
return false;
}
// TODO: Implement ControllerButton map
// TODO: Implement ControllerButton map

View File

@@ -55,6 +55,13 @@ void RWindow::setFullscreen(bool fs) {
restoreFromFullscreen();
}
void RWindow::liftKey(Key key) {
currentKeyboard.PressedKeys[key] = false;
auto event = ReWindow::WindowEvents::KeyUpEvent(key);
OnKeyUp(event);
}
std::string RWindow::getTitle() const {
return this->title;
}
@@ -92,4 +99,16 @@ bool RWindow::isKeyDown(Key key) const {
if (pair.first == key && pair.second)
return true;
return false;
}
void RWindow::pressKey(Key key) {
currentKeyboard.PressedKeys[key] = true;
auto eventData = KeyDownEvent(key);
OnKeyDown(eventData);
}
void RWindow::Close()
{
}

View File

@@ -0,0 +1,9 @@
#include <rewindow/types/gamepadbutton.h>
bool GamepadButton::operator==(const GamepadButton &rhs) const {
return this->GetMnemonicButtonCode() == rhs.GetMnemonicButtonCode();
}
void GamepadThumbstick::SetDeadzone(float minimum) const {
}

View File

@@ -15,12 +15,7 @@ Key::Key(const char* charcode, X11Scancode scancode, WindowsScancode sc)
keyboard.push_back(*this);
}
MouseButton::MouseButton() {
}
MouseButton::MouseButton(const char* charcode) {
this->CharCode = charcode;
}
bool Key::operator==(const Key &rhs) const {
//This is not a good workaround.
@@ -31,22 +26,7 @@ bool Key::operator<(const Key &rhs) const {
return (this->CharCode < rhs.CharCode);
}
MouseButton GetMouseButtonFromXButton(unsigned int button) {
switch(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;
//For *whatever* reason. These aren't in X.h
case 8: return MouseButtons::Mouse4;
case 9: return MouseButtons::Mouse5;
default: {
FATAL("Undefined XButtonCode: " + std::to_string((int) button));
return MouseButtons::Unimplemented;
}
}
}
Key GetKeyFromX11Scancode(X11Scancode code) {
for (auto& key : Key::GetKeyboard())

33
src/types/mousebutton.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include <rewindow/types/mousebutton.h>
#include <string>
#include <jlog/jlog.hpp>
MouseButton::MouseButton() {
}
MouseButton::MouseButton(const char* charcode, unsigned int index) {
this->CharCode = charcode;
this->ButtonIndex = index;
}
bool MouseButton::operator==(const MouseButton &mb) const {
return (mb.CharCode == this->CharCode);
}
MouseButton GetMouseButtonFromXButton(unsigned int button) {
switch(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;
//For *whatever* reason. These aren't in X.h
case 8: return MouseButtons::Mouse4;
case 9: return MouseButtons::Mouse5;
default: {
FATAL("Undefined XButtonCode: " + std::to_string((int) button));
return MouseButtons::Unimplemented;
}
}
}