Compare commits
12 Commits
Prerelease
...
Prerelease
Author | SHA1 | Date | |
---|---|---|---|
ee408c2fe6 | |||
f50280824d | |||
74d5f13c06 | |||
1950aee948 | |||
2d9f7e08b0 | |||
561e806b5f | |||
fa807256af | |||
b10a4d1d39 | |||
a3adbb6266 | |||
b75f44f1ee | |||
e4e860564c | |||
fb6825224f |
5
include/rewindow/inputservice.hpp
Normal file
5
include/rewindow/inputservice.hpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
namespace InputService {
|
||||
|
||||
};
|
@@ -4,7 +4,11 @@
|
||||
|
||||
namespace ReWindow::Logger {
|
||||
using namespace jlog;
|
||||
using Logger = GenericLogger;
|
||||
|
||||
extern GenericLogger Fatal;
|
||||
extern GenericLogger Debug;
|
||||
extern Logger Info;
|
||||
extern Logger Fatal;
|
||||
extern Logger Error;
|
||||
extern Logger Warning;
|
||||
extern Logger Debug;
|
||||
}
|
||||
|
@@ -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,38 +73,25 @@ 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
|
||||
{
|
||||
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 {
|
||||
public:
|
||||
MouseButton Button;
|
||||
|
||||
MouseButtonDownEvent() = default;
|
||||
MouseButtonDownEvent(MouseButton Button) : MouseEvent() {}
|
||||
};
|
||||
|
||||
class MouseButtonUpEvent : public MouseEvent {
|
||||
public:
|
||||
MouseButton Button;
|
||||
MouseButtonUpEvent() = default;
|
||||
MouseButtonUpEvent(MouseButton Button) : MouseEvent() {}
|
||||
};
|
||||
|
||||
class WindowResizeRequestEvent : public RWindowEvent
|
||||
class WindowResizeRequestEvent : public MouseButtonEvent
|
||||
{
|
||||
public:
|
||||
Vector2 Size;
|
||||
|
@@ -10,17 +10,18 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
namespace ReWindow {
|
||||
class Gamepad;
|
||||
class Xbox360Gamepad;
|
||||
class XboxOneGamepad;
|
||||
class PS4Gamepad;
|
||||
class Ultimate8BitDoPro2Gamepad;
|
||||
}
|
||||
|
||||
class InputDevice {}; // TODO: Remember to break InputDevice into it's own file and not define it twice!!!
|
||||
class ReWindow::Gamepad {
|
||||
public:
|
||||
virtual ~Gamepad() = default;
|
||||
};
|
||||
|
||||
class Gamepad : public InputDevice
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
|
||||
class XboxGamepad : public Gamepad {};
|
||||
class PS4Gamepad : public Gamepad {};
|
||||
}
|
||||
class ReWindow::XboxOneGamepad : public Gamepad {};
|
||||
class ReWindow::PS4Gamepad : public Gamepad {};
|
@@ -9,67 +9,101 @@
|
||||
/// @edit 2024-07-29
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
namespace ReWindow {
|
||||
class GamepadButton;
|
||||
class GamepadTrigger;
|
||||
class GamepadThumbstick;
|
||||
}
|
||||
|
||||
class GamepadButton {
|
||||
class ReWindow::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; }
|
||||
explicit GamepadButton(std::string mnemonic) : mnemonic_btn_code(std::move(mnemonic)){}
|
||||
[[nodiscard]] std::string GetMnemonicButtonCode() const;
|
||||
|
||||
/// Compares two GamepadButtons by their mnemonic button codes, not their activation state.
|
||||
bool operator ==(const GamepadButton& rhs) const;
|
||||
};
|
||||
|
||||
class GamepadTrigger {
|
||||
class ReWindow::GamepadTrigger {
|
||||
protected:
|
||||
std::string mnemonic_trigger_code;
|
||||
/// A float value between 0 and 1 representing how much the trigger has been pushed in by.
|
||||
float position = 0.0f;
|
||||
|
||||
/// The minimum possible movement of the trigger from the at-rest position.
|
||||
/// Movements less than this won't count.
|
||||
float dead_zone = 0.0f;
|
||||
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;
|
||||
void SetDeadzone(float minimum = 0.01f);
|
||||
[[nodiscard]] float GetActuation() const;
|
||||
public:
|
||||
explicit GamepadTrigger(std::string mnemonic) : mnemonic_trigger_code(std::move(mnemonic)){}
|
||||
};
|
||||
|
||||
class GamepadThumbstick
|
||||
{
|
||||
class ReWindow::GamepadThumbstick {
|
||||
protected:
|
||||
std::string mnemonic_stick_code;
|
||||
// X -1 is all the way left, X +1 is all the way right.
|
||||
// Y -1 is all the way down, Y +1 is all the way up.
|
||||
Vector2 position = {0.0f, 0.0f};
|
||||
|
||||
// The minimum possible movement from the center in any direction.
|
||||
float dead_zone = 0.0f;
|
||||
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;
|
||||
void SetDeadzone(float minimum = 0.01f);
|
||||
public:
|
||||
explicit GamepadThumbstick(std::string mnemonic) : mnemonic_stick_code(std::move(mnemonic)){}
|
||||
};
|
||||
|
||||
|
||||
|
||||
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"};
|
||||
namespace ReWindow::GamepadButtons {
|
||||
static const GamepadButton Triangle("△");
|
||||
static const GamepadButton Square("□");
|
||||
static const GamepadButton Circle("○");
|
||||
static const GamepadButton Cross("X");
|
||||
|
||||
static const auto Triangle = Y;
|
||||
static const auto Square = X;
|
||||
static const auto Circle = A;
|
||||
static const auto Cross = B;
|
||||
// If you like xbox :shrug:
|
||||
static const GamepadButton Y = Triangle;
|
||||
static const GamepadButton X = Square;
|
||||
static const GamepadButton A = Cross;
|
||||
static const GamepadButton B = Circle;
|
||||
|
||||
static const GamepadButton LButton("LB");
|
||||
static const GamepadButton RButton("RB");
|
||||
|
||||
static const GamepadButton LeftBumper {"LB"};
|
||||
static const GamepadButton RightBumper {"RB"};
|
||||
/* For controllers where L2 & R2 is a button or counts as one when pressed all the way down.
|
||||
* Gamecube & PS2 controllers do this. - Redacted. */
|
||||
static const GamepadButton LButton2("LB2");
|
||||
static const GamepadButton RButton2("RB2");
|
||||
|
||||
// The buttons when you press in the sticks.
|
||||
static const GamepadButton LButton3("LB3");
|
||||
static const GamepadButton RButton3("RB3");
|
||||
|
||||
// Will eventually need to handle making it not possible to, for ex. Press left and right at the same time.
|
||||
static const GamepadButton DPadUp("DPU");
|
||||
static const GamepadButton DPadDown("DPD");
|
||||
static const GamepadButton DPadLeft("DPL");
|
||||
static const GamepadButton DPadRight("DPR");
|
||||
|
||||
static const GamepadButton Select("SEL");
|
||||
static const GamepadButton Start("START");
|
||||
}
|
||||
|
||||
namespace GamepadTriggers
|
||||
{
|
||||
static const GamepadTrigger Left;
|
||||
static const GamepadTrigger Right;
|
||||
namespace ReWindow::GamepadTriggers {
|
||||
static const ReWindow::GamepadTrigger Left {"LT"};
|
||||
static const ReWindow::GamepadTrigger Right {"RT"};
|
||||
}
|
||||
|
||||
namespace ReWindow::GamepadThumbsticks {
|
||||
static const GamepadThumbstick Left {"LS"};
|
||||
static const GamepadThumbstick Right {"RS"};
|
||||
}
|
@@ -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;
|
||||
|
@@ -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);
|
||||
|
@@ -10,6 +10,8 @@
|
||||
#include <rewindow/types/gamepadbutton.h>
|
||||
#include <J3ML/LinearAlgebra.hpp>
|
||||
#include <rewindow/types/WindowEvents.hpp>
|
||||
#include <format>
|
||||
#include <queue>
|
||||
|
||||
|
||||
enum class RWindowFlags: uint8_t {
|
||||
@@ -29,8 +31,6 @@ enum class RenderingAPI: uint8_t {
|
||||
VULKAN = 1,
|
||||
};
|
||||
|
||||
|
||||
|
||||
namespace ReWindow
|
||||
{
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
@@ -47,21 +47,125 @@ namespace ReWindow
|
||||
|
||||
class MouseState {
|
||||
public:
|
||||
std::map<MouseButton, bool> PressedBtns;
|
||||
Vector2 Pos;
|
||||
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");
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: Refactor RenderingAPI into a polymorphic class interface for greater reusability.
|
||||
class IRenderer {
|
||||
public:
|
||||
virtual void Initialize();
|
||||
virtual void SwapBuffers();
|
||||
virtual std::string GetDriverVendor();
|
||||
virtual void SetVsync(bool enabled);
|
||||
virtual void SetViewportSize(int width, int height);
|
||||
virtual void MakeCurrent();
|
||||
};
|
||||
|
||||
class VulkanRenderBase : public IRenderer {};
|
||||
class GLRenderBase : public IRenderer {};
|
||||
class GLXRenderer : public GLRenderBase {};
|
||||
class WGLRenderer : public GLRenderBase {};
|
||||
|
||||
|
||||
|
||||
// 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.
|
||||
class RWindow {
|
||||
public:
|
||||
#pragma region Constructors
|
||||
/// The default constructor sets a default size and window title.
|
||||
RWindow();
|
||||
/// Constructs a window by explicitly setting title, width and height.
|
||||
RWindow(const std::string& title, int width, int height);
|
||||
/// The default constructor does not set any members, and are left uninitialized.
|
||||
RWindow() = default;
|
||||
/// Constructs a window by explicitly setting the title, width, height, and optionally; rendering API, fullscreen, resizable, and vsync.
|
||||
/// @param wTitle The window title text.
|
||||
/// @param wWidth
|
||||
/// @param wHeight
|
||||
/// @param wRenderer
|
||||
/// @param wFullscreen
|
||||
/// @param wResizable
|
||||
/// @param wVsync
|
||||
explicit RWindow(const std::string& wTitle, int wWidth = 640, int wHeight = 480,
|
||||
RenderingAPI wRenderer = RenderingAPI::OPENGL,
|
||||
bool wFullscreen = false,
|
||||
bool wResizable = true,
|
||||
bool wVsync = false);
|
||||
/// Constructs a window as above with the additional argument of explicitly setting which render API is to be used.
|
||||
RWindow(const std::string& title, int width, int height, RenderingAPI renderer);
|
||||
|
||||
/// The destructor deallocates and cleans up any memory used by the RWindow program.
|
||||
/// @note If the window handle is not already destroyed, it will be done here.
|
||||
~RWindow();
|
||||
|
||||
#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.
|
||||
@@ -111,6 +215,8 @@ namespace ReWindow
|
||||
/// @see GetAccurateMouseCoordinates().
|
||||
Vector2 GetMouseCoordinates() const;
|
||||
|
||||
int GetMouseWheelPersistent() const { return currentMouse.Wheel;}
|
||||
|
||||
/// Sets which rendering API is to be used with this window.
|
||||
void SetRenderer(RenderingAPI api);
|
||||
|
||||
@@ -118,9 +224,20 @@ namespace ReWindow
|
||||
/// Calling this function, therefore, creates the real 'window' object on the operating system.
|
||||
void Open();
|
||||
|
||||
/// Cleans up and closes the window without destroying the handle.
|
||||
/// Tells the window that we want to close, without directly forcing it to. This gives us time to finalize any work we are performing.
|
||||
|
||||
[[nodiscard]] bool IsOpen() const { return open;}
|
||||
[[nodiscard]] bool IsClosing() const { return closing;}
|
||||
|
||||
|
||||
/// Cleans up and closes the window object.
|
||||
void Close();
|
||||
|
||||
/// Closes the window immediately, potentially without allowing finalization to occur.
|
||||
void ForceClose();
|
||||
|
||||
void ForceCloseAndTerminateProgram();
|
||||
|
||||
void CloseAndReopenInPlace();
|
||||
|
||||
void MessageBox(); // TODO: Must be implemented from scratch as a Motif Window in x11
|
||||
@@ -149,7 +266,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.
|
||||
@@ -187,7 +304,7 @@ namespace ReWindow
|
||||
|
||||
/// Tells the underlying window manager to destroy this window and drop the handle.
|
||||
/// The window, in theory, can not be re-opened after this.
|
||||
void DestroyWindow();
|
||||
void DestroyOSWindowHandle();
|
||||
|
||||
/// Reads events from the operating system, and processes them accordingly.
|
||||
/// TODO: Move out of public API, consumers should call Refresh or ideally an update() call.
|
||||
@@ -271,6 +388,12 @@ namespace ReWindow
|
||||
/// Returns the number of frames ran since the windows' creation.
|
||||
[[nodiscard]] float GetRefreshCounter() const;
|
||||
protected:
|
||||
|
||||
void LogEvent(const RWindowEvent& e) { eventLog.push_back(e);}
|
||||
//void EnqueueEvent(const RWindowEvent& e) { eventQueue.push(e);}
|
||||
RWindowEvent GetLastEvent() const {
|
||||
return eventLog.back();
|
||||
}
|
||||
/// Requests the operating system to make the window fullscreen. Saves the previous window size as well.
|
||||
void Fullscreen();
|
||||
|
||||
@@ -279,46 +402,44 @@ namespace ReWindow
|
||||
|
||||
protected:
|
||||
#pragma region Data
|
||||
int width = 1280;
|
||||
int height = 720;
|
||||
|
||||
bool open = false; // Is the underlying OS-Window-Handle actually open.
|
||||
bool resizable = true;
|
||||
bool fullscreen_mode = false;
|
||||
bool focused = true;
|
||||
bool vsync = false;
|
||||
bool cursor_visible = true;
|
||||
bool closing = false;
|
||||
|
||||
float delta_time = 0.f;
|
||||
float refresh_rate = 0.f;
|
||||
unsigned int refresh_count = 0;
|
||||
|
||||
std::string title = "Redacted Window";
|
||||
int width = 1280;
|
||||
int height = 720;
|
||||
|
||||
bool fullscreen_mode = false;
|
||||
bool focused = true;
|
||||
bool vsync = false;
|
||||
bool cursor_visible = true;
|
||||
bool open = false;
|
||||
bool resizable = true;
|
||||
|
||||
|
||||
Vector2 lastKnownWindowSize {0, 0};
|
||||
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;
|
||||
|
||||
std::vector<RWindowEvent> eventLog; // history of all logged window events.
|
||||
std::queue<RWindowEvent> eventQueue; //
|
||||
KeyboardState currentKeyboard; // current frame keyboard state.
|
||||
KeyboardState previousKeyboard; // previous frame keyboard state.
|
||||
MouseState currentMouse; // purrent frame mouse state.
|
||||
MouseState previousMouse; // previous frame mouse state
|
||||
|
||||
RenderingAPI renderer = RenderingAPI::OPENGL;
|
||||
|
||||
//Vector2 mouse_coords = {0, 0};
|
||||
//Vector2 last_mouse_coords = {0, 0};
|
||||
// TODO: Implement ringbuffer / circular vector class of some sort.
|
||||
float refresh_rate_prev_1 = 0.f;
|
||||
float refresh_rate_prev_2 = 0.f;
|
||||
float refresh_rate_prev_3 = 0.f;
|
||||
float refresh_rate_prev_4 = 0.f;
|
||||
float refresh_rate_prev_5 = 0.f;
|
||||
|
||||
|
||||
float delta_time = 0.f;
|
||||
float refresh_rate = 0.f;
|
||||
unsigned int refresh_count = 0;
|
||||
|
||||
// TODO: Implement ringbuffer / circular vector class of some sort.
|
||||
float refresh_rate_prev_1 = 0.f;
|
||||
float refresh_rate_prev_2 = 0.f;
|
||||
float refresh_rate_prev_3 = 0.f;
|
||||
float refresh_rate_prev_4 = 0.f;
|
||||
float refresh_rate_prev_5 = 0.f;
|
||||
|
||||
float avg_refresh_rate = 0.0f;
|
||||
float avg_refresh_rate = 0.0f;
|
||||
#pragma endregion
|
||||
#pragma region Internals
|
||||
/// Returns the most accurate and recent available mouse coordinates.
|
||||
@@ -340,9 +461,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.
|
||||
|
96
main.cpp
96
main.cpp
@@ -32,35 +32,55 @@ 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;
|
||||
|
||||
|
||||
if (IsKeyDown(Keys::N))
|
||||
std::cout << "Gotteem" << std::endl;
|
||||
|
||||
RWindow::OnRefresh(elapsed);
|
||||
}
|
||||
|
||||
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override {
|
||||
std::cout << "resized to " << e.Size.x << ", " << e.Size.y << std::endl;
|
||||
//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() {
|
||||
auto* window = new MyWindow("Test Window", 600, 480);
|
||||
jlog::Debug(std::format("New window '{}' created. width={} height={}", window->GetTitle(), window->GetWidth(),
|
||||
window->GetHeight()));
|
||||
|
||||
window->SetRenderer(RenderingAPI::OPENGL);
|
||||
jlog::Debug(std::format("Rendering API OPENGL set for window '{}'", window->GetTitle()));
|
||||
|
||||
window->Open();
|
||||
jlog::Debug(std::format("Opened window '{}'", window->GetTitle()));
|
||||
|
||||
// TODO: Cannot set flags until after window is open
|
||||
// Make this work somehow
|
||||
jlog::Debug("TODO: Cannot set flags until after window is open");
|
||||
window->SetFullscreen(false);
|
||||
window->SetVsyncEnabled(true);
|
||||
window->SetResizable(true);
|
||||
window->SetCursorVisible(false);
|
||||
|
||||
/*
|
||||
/*
|
||||
std::vector<ReWindow::Display> d = ReWindow::Display::getDisplaysFromWindowManager();
|
||||
for (ReWindow::Display& display : d) {
|
||||
|
||||
@@ -85,6 +105,26 @@ int main() {
|
||||
}
|
||||
*/
|
||||
|
||||
auto* window = new MyWindow("Test Window", 600, 480);
|
||||
jlog::Debug(std::format("New window '{}' created. width={} height={}", window->GetTitle(), window->GetWidth(),
|
||||
window->GetHeight()));
|
||||
|
||||
window->SetRenderer(RenderingAPI::OPENGL);
|
||||
jlog::Debug(std::format("Rendering API OPENGL set for window '{}'", window->GetTitle()));
|
||||
|
||||
window->Open();
|
||||
jlog::Debug(std::format("Opened window '{}'", window->GetTitle()));
|
||||
|
||||
// TODO: Cannot set flags until after window is open
|
||||
// Make this work somehow
|
||||
jlog::Debug("TODO: Cannot set flags until after window is open");
|
||||
window->SetFullscreen(false);
|
||||
window->SetVsyncEnabled(true);
|
||||
window->SetResizable(true);
|
||||
window->SetCursorVisible(false);
|
||||
|
||||
|
||||
|
||||
ReWindow::Logger::Debug(std::format("Window '{}' flags: IN_FOCUS={} FULLSCREEN={} RESIZEABLE={} VSYNC={} QUIT={}",
|
||||
window->GetTitle(),
|
||||
window->GetFlag(RWindowFlags::IN_FOCUS),
|
||||
@@ -95,18 +135,25 @@ 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));
|
||||
};
|
||||
|
||||
while (window->IsAlive()) {
|
||||
window->PollEvents();
|
||||
window->OnMouseWheelEvent += [&, window] (ReWindow::MouseWheelEvent e)
|
||||
{
|
||||
std::cout << window->GetMouseWheelPersistent() << std::endl;
|
||||
};
|
||||
|
||||
while (!window->IsClosing()) {
|
||||
//window->PollEvents();
|
||||
window->ManagedRefresh();
|
||||
}
|
||||
|
||||
delete window;
|
||||
// Alternatively:
|
||||
/*
|
||||
* while (window->IsAlive()) {
|
||||
@@ -121,6 +168,9 @@ int main() {
|
||||
* }
|
||||
*
|
||||
*/
|
||||
|
||||
//exit(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@@ -2,6 +2,10 @@
|
||||
|
||||
namespace ReWindow::Logger {
|
||||
using namespace jlog;
|
||||
GenericLogger Fatal {"ReWindow::fatal", GlobalLogFile, Colors::Reds::Crimson, Colors::Gray, Colors::Gray, Colors::Reds::Crimson, Colors::White};
|
||||
GenericLogger Debug {"ReWindow::debug", GlobalLogFile, Colors::Purples::Purple, Colors::Gray, Colors::Gray, Colors::Purples::Purple, Colors::White};
|
||||
using namespace Colors;
|
||||
using Logger = GenericLogger;
|
||||
|
||||
Logger Info {"ReWindow", GlobalLogFile, Gray, Gray, Gray, Gray};
|
||||
Logger Fatal {"ReWindow::fatal", GlobalLogFile, Reds::Crimson, Gray, Gray, Reds::Crimson, White};
|
||||
Logger Debug {"ReWindow::debug", GlobalLogFile, Purples::Purple, Gray, Gray, Purples::Purple, White};
|
||||
}
|
||||
|
@@ -54,12 +54,15 @@ void RWindow::Lower() const
|
||||
XLowerWindow(display, window);
|
||||
}
|
||||
|
||||
void RWindow::DestroyWindow() {
|
||||
void RWindow::DestroyOSWindowHandle() {
|
||||
Logger::Debug(std::format("Destroying window '{}'", this->title));
|
||||
XDestroySubwindows(display, window);
|
||||
Logger::Debug(std::format("Destroyed window '{}'", this->title));
|
||||
XDestroyWindow(display, window);
|
||||
delete this;
|
||||
|
||||
system("xset r on"); // Re-set X-server parameter to enable key-repeat.
|
||||
|
||||
open = false;
|
||||
}
|
||||
|
||||
//void RWindow::
|
||||
@@ -81,13 +84,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) {
|
||||
@@ -109,18 +119,14 @@ void RWindow::PollEvents() {
|
||||
XNextEvent(display, &xev);
|
||||
|
||||
if (xev.type == ClientMessage)
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ClientMessage"));
|
||||
Logger::Info(std::format("Event '{}'", "ClientMessage"));
|
||||
|
||||
if (xev.xclient.message_type == XInternAtom(display, "WM_PROTOCOLS", False) && static_cast<Atom>(xev.xclient.data.l[0]) == wmDeleteWindow) {
|
||||
|
||||
processOnClose();
|
||||
DestroyWindow();
|
||||
system("xset r on");
|
||||
exit(0);
|
||||
Close();
|
||||
}
|
||||
|
||||
if (xev.type == FocusIn) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusIn"));
|
||||
Logger::Debug(std::format("Event'{}'", "FocusIn"));
|
||||
XAutoRepeatOff(display);
|
||||
SetFlag(RWindowFlags::IN_FOCUS, true);
|
||||
|
||||
@@ -134,7 +140,7 @@ void RWindow::PollEvents() {
|
||||
}
|
||||
|
||||
if (xev.type == FocusOut) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "FocusOut"));
|
||||
Logger::Debug(std::format("Event '{}'", "FocusOut"));
|
||||
XAutoRepeatOn(display);
|
||||
|
||||
SetFlag(RWindowFlags::IN_FOCUS, false);
|
||||
@@ -145,48 +151,64 @@ void RWindow::PollEvents() {
|
||||
}
|
||||
|
||||
if (xev.type == KeyRelease) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "KeyRelease"));
|
||||
Logger::Debug(std::format("Event '{}'", "KeyRelease"));
|
||||
auto scancode = (X11Scancode) xev.xkey.keycode;
|
||||
auto key = GetKeyFromX11Scancode(scancode);
|
||||
processKeyRelease(key);
|
||||
}
|
||||
|
||||
if (xev.type == KeyPress) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "KeyPress"));
|
||||
Logger::Debug(std::format("Event '{}'", "KeyPress"));
|
||||
auto scancode = (X11Scancode) xev.xkey.keycode;
|
||||
auto key = GetKeyFromX11Scancode(scancode);
|
||||
processKeyPress(key);
|
||||
}
|
||||
|
||||
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("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("Event: MouseButtonPress {}", button.Mnemonic));
|
||||
|
||||
processMousePress(button);
|
||||
}
|
||||
}
|
||||
|
||||
if (xev.type == Expose)
|
||||
{
|
||||
Logger::Debug(std::format("Recieved event '{}'", "Expose"));
|
||||
Logger::Debug(std::format("Event '{}'", "Expose"));
|
||||
}
|
||||
|
||||
// NOTE: This event is functionally useless, as it only informs of the very beginning and end of a mouse movement.
|
||||
if (xev.type == MotionNotify)
|
||||
{
|
||||
Logger::Debug(std::format("Recieved event '{}'", "MotionNotify"));
|
||||
Logger::Debug(std::format("Event '{}'", "MotionNotify"));
|
||||
}
|
||||
|
||||
if (xev.type == ConfigureNotify) {
|
||||
if (this->width != xev.xconfigurerequest.width || this->height != xev.xconfigurerequest.height) {
|
||||
Logger::Debug(std::format("Recieved event '{}'", "ResizeRequest"));
|
||||
Logger::Debug(std::format("Event '{}'", "ResizeRequest"));
|
||||
|
||||
this->width = xev.xconfigurerequest.width;
|
||||
this->height = xev.xconfigurerequest.height;
|
||||
@@ -206,7 +228,7 @@ void RWindow::PollEvents() {
|
||||
|
||||
}
|
||||
previousKeyboard = currentKeyboard;
|
||||
previousMouse.PressedBtns = currentMouse.PressedBtns;
|
||||
previousMouse.Buttons = currentMouse.Buttons;
|
||||
}
|
||||
|
||||
|
||||
@@ -218,7 +240,7 @@ void RWindow::SetSize(int newWidth, int newHeight) {
|
||||
this->height = newHeight;
|
||||
XResizeWindow(display, window, newWidth, newHeight);
|
||||
XFlush(display);
|
||||
Logger::Debug(std::format("Set size for window '{}'. width={} height={}", this->title, newWidth, newHeight));
|
||||
Logger::Info(std::format("Set size for '{}' to {} x {}", this->title, newWidth, newHeight));
|
||||
}
|
||||
|
||||
Vector2 RWindow::GetAccurateMouseCoordinates() const {
|
||||
@@ -270,9 +292,8 @@ void RWindow::GLSwapBuffers() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
void RWindow::Fullscreen() {
|
||||
Logger::Debug(std::format("Fullscreening window '{}'", this->title));
|
||||
Logger::Info(std::format("Fullscreening '{}'", this->title));
|
||||
fullscreen_mode = true;
|
||||
|
||||
XEvent xev;
|
||||
@@ -289,11 +310,11 @@ void RWindow::Fullscreen() {
|
||||
xev.xclient.data.l[1] = fullscreen_mode;
|
||||
xev.xclient.data.l[2] = 0;
|
||||
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
|
||||
Logger::Debug(std::format("Fullscreened window '{}'", this->title));
|
||||
Logger::Debug(std::format("Fullscreened '{}'", this->title));
|
||||
}
|
||||
|
||||
void RWindow::RestoreFromFullscreen() {
|
||||
Logger::Debug(std::format("Restoring window '{}' from Fullscreen", this->title));
|
||||
Logger::Debug(std::format("Restoring '{}' from Fullscreen", this->title));
|
||||
fullscreen_mode = false;
|
||||
XEvent xev;
|
||||
Atom wm_state = XInternAtom(display, "_NET_WM_STATE", False);
|
||||
@@ -307,7 +328,7 @@ void RWindow::RestoreFromFullscreen() {
|
||||
xev.xclient.data.l[1] = fullscreen_mode;
|
||||
xev.xclient.data.l[2] = 0;
|
||||
XSendEvent(display, DefaultRootWindow(display), False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
|
||||
Logger::Debug(std::format("Restored window '{}' from Fullscreen", this->title));
|
||||
Logger::Debug(std::format("Restored '{}' from Fullscreen", this->title));
|
||||
}
|
||||
|
||||
void RWindow::SetVsyncEnabled(bool b) {
|
||||
@@ -350,7 +371,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);
|
||||
@@ -362,8 +383,8 @@ void RWindow::Open() {
|
||||
// Get the position of the renderable area relative to the rest of the window.
|
||||
XGetWindowAttributes(display, window, &windowAttributes);
|
||||
render_area_position = { (float) windowAttributes.x, (float) windowAttributes.y };
|
||||
open = true;
|
||||
|
||||
open = true;
|
||||
|
||||
processOnOpen();
|
||||
}
|
||||
|
@@ -15,25 +15,24 @@ std::string RWindowFlagToStr(RWindowFlags flag) {
|
||||
|
||||
using namespace ReWindow;
|
||||
|
||||
RWindow::RWindow() {
|
||||
title = "ReWindow Application";
|
||||
width = 640;
|
||||
height = 480;
|
||||
//RWindow::singleton = this;
|
||||
|
||||
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(const std::string& title, int width, int height) : flags{false,false,false,false} {
|
||||
this->title = title;
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
//RWindow::singleton = this;
|
||||
RWindow::~RWindow() {
|
||||
if (open)
|
||||
DestroyOSWindowHandle();
|
||||
}
|
||||
|
||||
RWindow::RWindow(const std::string& title, int width, int height, RenderingAPI renderer) :
|
||||
title(title), width(width), height(height), renderer(renderer) {}
|
||||
|
||||
|
||||
Vector2 RWindow::GetMouseCoordinates() const {
|
||||
return currentMouse.Pos;
|
||||
return currentMouse.Position;
|
||||
}
|
||||
|
||||
bool RWindow::GetFlag(RWindowFlags flag) const {
|
||||
@@ -41,10 +40,9 @@ bool RWindow::GetFlag(RWindowFlags flag) const {
|
||||
}
|
||||
|
||||
bool RWindow::IsAlive() const {
|
||||
return true;
|
||||
return (!closing) && open;
|
||||
}
|
||||
|
||||
|
||||
void RWindow::SetFullscreen(bool fs) {
|
||||
if (fs)
|
||||
Fullscreen();
|
||||
@@ -52,11 +50,13 @@ void RWindow::SetFullscreen(bool fs) {
|
||||
RestoreFromFullscreen();
|
||||
}
|
||||
|
||||
#pragma region Event Processors Implementation
|
||||
void RWindow::processFocusIn()
|
||||
{
|
||||
RWindowEvent event {};
|
||||
OnFocusGain(event);
|
||||
OnFocusGainEvent(event);
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processFocusOut()
|
||||
@@ -64,31 +64,39 @@ void RWindow::processFocusOut()
|
||||
RWindowEvent event {};
|
||||
OnFocusLost(event);
|
||||
OnFocusLostEvent(event);
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processMousePress(MouseButton btn)
|
||||
void RWindow::processMousePress(const MouseButton& btn)
|
||||
{
|
||||
currentMouse.PressedBtns[btn] = true;
|
||||
auto eventData = MouseButtonDownEvent(btn);
|
||||
OnMouseButtonDown(eventData);
|
||||
OnMouseButtonDownEvent(eventData);
|
||||
|
||||
currentMouse.Set(btn, true);
|
||||
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)
|
||||
{
|
||||
currentMouse.Pos = new_pos;
|
||||
auto eventData = MouseMoveEvent(new_pos);
|
||||
OnMouseMove(eventData);
|
||||
OnMouseMoveEvent(eventData);
|
||||
currentMouse.Position = new_pos;
|
||||
auto event = MouseMoveEvent(new_pos);
|
||||
OnMouseMove(event);
|
||||
OnMouseMoveEvent(event);
|
||||
Input::OnMouseMove(event);
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processMouseRelease(MouseButton btn)
|
||||
void RWindow::processMouseRelease(const MouseButton& btn)
|
||||
{
|
||||
currentMouse.PressedBtns[btn] = false;
|
||||
auto eventData = MouseButtonUpEvent(btn);
|
||||
OnMouseButtonUp(eventData);
|
||||
OnMouseButtonUpEvent(eventData);
|
||||
currentMouse.Set(btn, false);
|
||||
auto event = MouseButtonUpEvent(btn);
|
||||
OnMouseButtonUp(event);
|
||||
OnMouseButtonUpEvent(event);
|
||||
Input::OnMouseButtonEvent(MouseButtonEvent(btn, false));
|
||||
Input::OnMouseUp(event);
|
||||
LogEvent(event);
|
||||
|
||||
}
|
||||
|
||||
@@ -96,9 +104,41 @@ 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);
|
||||
}
|
||||
|
||||
void RWindow::processKeyPress(Key key) {
|
||||
currentKeyboard.PressedKeys[key] = true;
|
||||
auto event = KeyDownEvent(key);
|
||||
OnKeyDown(event);
|
||||
OnKeyDownEvent(event);
|
||||
Input::OnKeyDown(event);
|
||||
Input::OnKeyboardEvent(KeyboardEvent(key, KeyState::Pressed));
|
||||
Input::OnKeyEvent(KeyboardEvent(key, KeyState::Pressed));
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processOnClose()
|
||||
{
|
||||
auto event = RWindowEvent();
|
||||
OnClosing();
|
||||
OnClosingEvent();
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
void RWindow::processOnOpen()
|
||||
{
|
||||
auto event = RWindowEvent();
|
||||
OnOpen();
|
||||
OnOpenEvent();
|
||||
LogEvent(event);
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
std::string RWindow::GetTitle() const {
|
||||
return this->title;
|
||||
@@ -141,37 +181,17 @@ void RWindow::SetSize(const Vector2& size) {
|
||||
}
|
||||
|
||||
bool RWindow::IsKeyDown(Key key) const {
|
||||
for (const auto& pair : currentKeyboard.PressedKeys)
|
||||
if (pair.first == key && pair.second)
|
||||
return true;
|
||||
return false;
|
||||
if (currentKeyboard.PressedKeys.contains(key))
|
||||
return currentKeyboard.PressedKeys.at(key);
|
||||
|
||||
return false; // NOTE: Key may not be mapped!!
|
||||
}
|
||||
|
||||
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;
|
||||
auto eventData = KeyDownEvent(key);
|
||||
OnKeyDown(eventData);
|
||||
OnKeyDownEvent(key);
|
||||
}
|
||||
|
||||
void RWindow::processOnClose()
|
||||
{
|
||||
OnClosing();
|
||||
OnClosingEvent();
|
||||
}
|
||||
|
||||
void RWindow::processOnOpen()
|
||||
{
|
||||
OnOpen();
|
||||
OnOpenEvent();
|
||||
}
|
||||
|
||||
void RWindow::ManagedRefresh()
|
||||
{
|
||||
@@ -189,14 +209,13 @@ 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
float RWindow::ComputeElapsedFrameTimeSeconds(std::chrono::steady_clock::time_point start, std::chrono::steady_clock::time_point end) {
|
||||
@@ -225,17 +244,15 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
void RWindow::Close() {
|
||||
/// TODO: Implement closing the window without destroying the handle.
|
||||
processOnClose();
|
||||
}
|
||||
|
||||
bool RWindow::IsResizable() const {
|
||||
return resizable;
|
||||
}
|
||||
@@ -258,3 +275,40 @@ bool RWindow::IsFocused() const {
|
||||
|
||||
float RWindow::GetRefreshCounter() const { return refresh_count; }
|
||||
|
||||
void RWindow::Close() {
|
||||
closing = true;
|
||||
processOnClose();
|
||||
}
|
||||
|
||||
void RWindow::ForceClose() {
|
||||
Close();
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@@ -1,9 +1,25 @@
|
||||
#include <rewindow/types/gamepadbutton.h>
|
||||
|
||||
bool GamepadButton::operator==(const GamepadButton &rhs) const {
|
||||
bool ReWindow::GamepadButton::operator ==(const GamepadButton &rhs) const {
|
||||
return this->GetMnemonicButtonCode() == rhs.GetMnemonicButtonCode();
|
||||
}
|
||||
|
||||
void GamepadThumbstick::SetDeadzone(float minimum) const {
|
||||
|
||||
std::string ReWindow::GamepadButton::GetMnemonicButtonCode() const {
|
||||
return mnemonic_btn_code;
|
||||
}
|
||||
|
||||
void ReWindow::GamepadThumbstick::SetDeadzone(float minimum) {
|
||||
dead_zone = minimum;
|
||||
}
|
||||
|
||||
Vector2 ReWindow::GamepadThumbstick::GetPosition() const {
|
||||
return position;
|
||||
}
|
||||
|
||||
void ReWindow::GamepadTrigger::SetDeadzone(float minimum) {
|
||||
dead_zone = minimum;
|
||||
}
|
||||
|
||||
float ReWindow::GamepadTrigger::GetActuation() const {
|
||||
return position;
|
||||
}
|
||||
|
@@ -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,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;
|
||||
|
Reference in New Issue
Block a user