std::pair
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m50s

This commit is contained in:
2025-06-06 09:56:34 -04:00
parent c3c6a29dc6
commit c354b1deef
12 changed files with 68 additions and 249 deletions

View File

@@ -18,6 +18,6 @@ namespace InputService {
inline Event<MouseWheelEvent> OnMouseWheel;
bool IsKeyDown(const Key& key);
bool IsMouseButtonDown(const MouseButton& button);
IPair GetMousePosition();
IPair GetWindowSize();
std::pair<int, int> GetMousePosition();
std::pair<int, int> GetWindowSize();
};

View File

@@ -10,7 +10,6 @@
#include <string>
#include <utility>
#include <ReWindow/types/Pair.h>
namespace ReWindow {
class GamepadButton;
class GamepadTrigger;
@@ -49,12 +48,12 @@ 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.
FPair position = {0.0f, 0.0f};
std::pair<float, float> position = {0.0f, 0.0f};
// The minimum possible movement from the center in any direction.
float dead_zone = 0.0f;
public:
[[nodiscard]] FPair GetPosition() const;
[[nodiscard]] std::pair<float, float> GetPosition() const;
void SetDeadzone(float minimum = 0.01f);
public:
explicit GamepadThumbstick(std::string mnemonic) : mnemonic_stick_code(std::move(mnemonic)){}

View File

@@ -1,53 +0,0 @@
#pragma once
namespace ReWindow {
class IPair;
class FPair;
}
class ReWindow::IPair {
public:
int x, y;
public:
IPair operator +(const IPair& rhs) const;
IPair operator -(const IPair& rhs) const;
IPair operator *(const IPair& rhs) const;
IPair operator /(const IPair& rhs) const;
public:
IPair& operator +=(const IPair& rhs);
IPair& operator -=(const IPair& rhs);
IPair& operator *=(const IPair& rhs);
IPair& operator /=(const IPair& rhs);
public:
bool operator ==(const IPair& rhs) const;
bool operator !=(const IPair& rhs) const;
public:
IPair(const IPair& rhs);
IPair() : x(0), y(0) {};
IPair(int x, int y) : x(x), y(y) {};
~IPair() = default;
};
class ReWindow::FPair {
public:
float x, y;
public:
FPair operator +(const FPair& rhs) const;
FPair operator -(const FPair& rhs) const;
FPair operator *(const FPair& rhs) const;
FPair operator /(const FPair& rhs) const;
public:
FPair& operator +=(const FPair& rhs);
FPair& operator -=(const FPair& rhs);
FPair& operator *=(const FPair& rhs);
FPair& operator /=(const FPair& rhs);
public:
bool operator ==(const FPair& rhs) const;
bool operator !=(const FPair& rhs) const;
public:
FPair(const FPair& rhs);
explicit FPair(const IPair& rhs);
FPair() : x(0), y(0) {};
FPair(float x, float y) : x(x), y(y) {};
~FPair() = default;
};

View File

@@ -8,7 +8,6 @@
#include <ReWindow/types/Cursors.h>
#include <ReWindow/types/MouseButton.h>
#include <ReWindow/types/GamepadButton.h>
#include <ReWindow/types/Pair.h>
#include <ReWindow/types/WindowEvents.h>
#include <queue>
@@ -37,7 +36,7 @@ public:
}
Buttons;
IPair Position;
std::pair<int, int> Position;
int Wheel = 0;
public:
[[nodiscard]] bool IsDown(const MouseButton& btn) const;
@@ -68,10 +67,10 @@ protected:
float delta_time = 0.f;
float refresh_rate = 0.f;
unsigned int refresh_count = 0;
IPair render_area_position = {0, 0};
std::pair<int, int> render_area_position = {0, 0};
std::string title = "Redacted Window";
IPair lastKnownWindowSize {0, 0};
std::pair<int, int> lastKnownWindowSize {0, 0};
std::vector<RWindowEvent> eventLog; // history of all logged window events.
std::queue<RWindowEvent> eventQueue; //
MouseState currentMouse; // purrent frame mouse state.
@@ -88,7 +87,7 @@ protected:
/// Returns the most accurate and recent available mouse coordinates.
/// @note Call this version at most **once** per-frame. It polls the X-Window server and therefore is quite slow.
/// @see getCursorPos();
[[nodiscard]] IPair GetAccurateMouseCoordinates() const;
[[nodiscard]] std::pair<int, int> GetAccurateMouseCoordinates() const;
protected:
void LogEvent(const RWindowEvent& e) { eventLog.push_back(e);}
//void EnqueueEvent(const RWindowEvent& e) { eventQueue.push(e);}
@@ -156,10 +155,10 @@ public:
virtual void OnMouseButtonUp(const MouseButtonUpEvent&) {}
virtual void OnMouseWheel(const MouseWheelEvent&) {}
/// Returns an IPair representing mouse coordinates relative to the top-left corner of the window.
/// Returns an std::pair<int, int> 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().
[[nodiscard]] IPair GetMouseCoordinates() const;
[[nodiscard]] std::pair<int, int> GetMouseCoordinates() const;
[[nodiscard]] int GetMouseWheelPersistent() const { return currentMouse.Wheel; }
[[nodiscard]] bool IsOpen() const { return open; }
@@ -224,8 +223,8 @@ public:
[[nodiscard]] int GetHeight() const;
/// This is unfortunately here because of the poor design of windows. Maybe once interfaces are done this won't be required anymore.
void SetSizeWithoutEvent(const IPair& size);
void SetLastKnownWindowSize(const IPair& size);
void SetSizeWithoutEvent(const std::pair<int, int>& size);
void SetLastKnownWindowSize(const std::pair<int, int>& size);
/// Tells the underlying window manager to destroy this window and drop the handle.
/// The window, in theory, can not be re-opened after this.
@@ -247,25 +246,25 @@ public:
void SetSize(int width, int height);
/// Requests the operating system to change the window size.
/// @param size
void SetSize(const IPair& size);
void SetSize(const std::pair<int, int>& size);
/// Returns the position of the window's top-left corner relative to the display
IPair GetPos() const;
std::pair<int, int> GetPos() const;
/// Returns the known size of the window, in {x,y} pixel measurement.
IPair GetSize() const;
std::pair<int, int> 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).
IPair GetPositionOfRenderableArea() const;
std::pair<int, int> GetPositionOfRenderableArea() const;
/// Requests the operating system to move the window to the specified coordinates on the display.
/// @param x The horizontal screen position to place the window at.
/// @param y The vertical screen position to place the window at.
void SetPos(int x, int y);
/// Requests the operating system to move the window to the specified coordinates on the display.
/// @param pos A IPair representing the x,y coordinates of the desired window destination. Fractional values are ignored.
void SetPos(const IPair& pos);
/// @param pos A std::pair<int, int> representing the x,y coordinates of the desired window destination. Fractional values are ignored.
void SetPos(const std::pair<int, int>& pos);
/// Pull the window to the top, such that it is displayed on top of everything else.
/// NOTE: The implementation is defined per-OS, and thus there is no guarantee of it always working.
@@ -310,7 +309,7 @@ public:
void processMouseRelease(const MouseButton& btn);
void processFocusIn();
void processFocusOut();
void processMouseMove(IPair last_pos, IPair new_pos);
void processMouseMove(const std::pair<int, int>& last_pos, const std::pair<int, int>& new_pos);
void processMouseWheel(int scrolls);
/// Virtual functions which *must* be overridden based on the Renderer.

View File

@@ -3,7 +3,6 @@
#include <chrono>
#include <ReWindow/types/Key.h>
#include <ReWindow/types/MouseButton.h>
#include <ReWindow/types/Pair.h>
namespace ReWindow {
@@ -62,11 +61,11 @@ namespace ReWindow {
class MouseMoveEvent : public MouseEvent {
public:
IPair Position;
IPair Delta;
MouseMoveEvent(const IPair &pos) : MouseEvent(), Position(pos)
std::pair<int, int> Position;
std::pair<int, int> Delta;
MouseMoveEvent(const std::pair<int, int> &pos) : MouseEvent(), Position(pos)
{}
MouseMoveEvent(int x, int y) : MouseEvent(), Position(IPair(x, y))
MouseMoveEvent(int x, int y) : MouseEvent(), Position(std::pair<int, int>(x, y))
{}
};
@@ -93,6 +92,6 @@ namespace ReWindow {
class WindowResizeRequestEvent : public MouseButtonEvent
{
public:
IPair Size;
std::pair<int, int> Size;
};
}

View File

@@ -3,8 +3,8 @@
#include <ReWindow/Logger.h>
using namespace ReWindow;
std::ostream& operator<<(std::ostream& os, const IPair& v) {
return os << "{" << v.x << ", " << v.y << "}";
std::ostream& operator<<(std::ostream& os, const std::pair<int, int>& v) {
return os << "{" << v.first << ", " << v.second << "}";
}
class MyWindow : public OpenGLWindow {

View File

@@ -8,10 +8,10 @@ namespace InputService
bool IsMouseButtonDown(const MouseButton &button) {
return RWindow::GetExtant()->IsMouseButtonDown(button);
}
IPair GetMousePosition() {
std::pair<int, int> GetMousePosition() {
return RWindow::GetExtant()->GetMouseCoordinates();
}
IPair GetWindowSize() {
std::pair<int, int> GetWindowSize() {
return RWindow::GetExtant()->GetSize();
}
}

View File

@@ -22,7 +22,7 @@ public:
Cursor invisible_cursor = 0;
XWMHints* wm_hints = nullptr;
bool window_visible = true;
IPair position = {0, 0};
std::pair<int, int> position = {0, 0};
};
@@ -71,13 +71,13 @@ void RWindow::UniqueFunctionNameForMessageBoxBecauseMicrosoftUsesMacros(const st
XFontStruct* font = XLoadQueryFont(platform->display, "6x13");
int text_width = XTextWidth(font, message.c_str(), message.size());
int button_text_width = XTextWidth(font, "OK", 2);
IPair button_size = {80, 30};
IPair dimensions = { std::max(text_width + 2 * padding, button_size.x + 2 * padding), 82};
IPair text_pos = {(dimensions.x - text_width) / 2, padding + font->ascent};
IPair button_pos = { (dimensions.x - button_size.x) / 2, text_pos.y + font->ascent + padding };
std::pair<int, int> button_size = {80, 30};
std::pair<int, int> dimensions = { std::max(text_width + 2 * padding, button_size.first + 2 * padding), 82};
std::pair<int, int> text_pos = {(dimensions.first - text_width) / 2, padding + font->ascent};
std::pair<int, int> button_pos = { (dimensions.first - button_size.first) / 2, text_pos.second + font->ascent + padding };
Window window = XCreateSimpleWindow(platform->display, RootWindow(platform->display, platform->defaultScreen),
100, 100, dimensions.x, dimensions.y, 1,
100, 100, dimensions.first, dimensions.second, 1,
BlackPixel(platform->display, platform->defaultScreen),
WhitePixel(platform->display, platform->defaultScreen));
@@ -87,8 +87,8 @@ void RWindow::UniqueFunctionNameForMessageBoxBecauseMicrosoftUsesMacros(const st
// No resizing.
XSizeHints hints;
hints.flags = PMinSize | PMaxSize;
hints.min_width = hints.max_width = dimensions.x;
hints.min_height = hints.max_height = dimensions.y;
hints.min_width = hints.max_width = dimensions.first;
hints.min_height = hints.max_height = dimensions.second;
XSetWMNormalHints(platform->display, window, &hints);
XMapWindow(platform->display, window);
@@ -100,7 +100,7 @@ void RWindow::UniqueFunctionNameForMessageBoxBecauseMicrosoftUsesMacros(const st
if (platform->window) {
XWindowAttributes parent_window_attributes;
XGetWindowAttributes(platform->display, platform->window, &parent_window_attributes);
XMoveWindow(platform->display, window, (parent_window_attributes.width - dimensions.x) / 2, (parent_window_attributes.height - dimensions.y) / 2);
XMoveWindow(platform->display, window, (parent_window_attributes.width - dimensions.first) / 2, (parent_window_attributes.height - dimensions.second) / 2);
}
*/
@@ -109,14 +109,14 @@ void RWindow::UniqueFunctionNameForMessageBoxBecauseMicrosoftUsesMacros(const st
XNextEvent(platform->display, &event);
if (event.type == Expose) {
XDrawString(platform->display, window, gc, text_pos.x, text_pos.y, message.c_str(), message.size());
XDrawString(platform->display, window, gc, button_pos.x + (button_size.x - button_text_width) / 2, button_pos.y + 20, "OK", 2);
XDrawRectangle(platform->display, window, gc, button_pos.x, button_pos.y, button_size.x, button_size.y);
XDrawString(platform->display, window, gc, text_pos.first, text_pos.second, message.c_str(), message.size());
XDrawString(platform->display, window, gc, button_pos.first + (button_size.first - button_text_width) / 2, button_pos.second + 20, "OK", 2);
XDrawRectangle(platform->display, window, gc, button_pos.first, button_pos.second, button_size.first, button_size.second);
}
else if (event.type == ButtonPress)
if (event.xbutton.x >= button_pos.x && event.xbutton.x <= button_pos.x + button_size.x
&& event.xbutton.y >= button_pos.y && event.xbutton.y <= button_pos.y + button_size.y)
if (event.xbutton.x >= button_pos.first && event.xbutton.x <= button_pos.first + button_size.first
&& event.xbutton.y >= button_pos.second && event.xbutton.y <= button_pos.second + button_size.second)
break;
}
@@ -289,7 +289,7 @@ void RWindow::PollEvents() {
OnResizeRequestEvent(eventData);
}
//Window Moved.
if (platform->position.x != platform->xev.xconfigurerequest.x || platform->position.y != platform->xev.xconfigurerequest.y)
if (platform->position.first != platform->xev.xconfigurerequest.x || platform->position.second != platform->xev.xconfigurerequest.y)
platform->position = { platform->xev.xconfigurerequest.x, platform->xev.xconfigurerequest.y };
}
@@ -317,7 +317,7 @@ void RWindow::SetSize(int newWidth, int newHeight) {
Logger::Info(std::format("Set size for '{}' to {} x {}", this->title, newWidth, newHeight));
}
IPair RWindow::GetAccurateMouseCoordinates() const {
std::pair<int, int> RWindow::GetAccurateMouseCoordinates() const {
Window root_return, child_return;
int root_x_ret, root_y_ret;
@@ -330,7 +330,7 @@ IPair RWindow::GetAccurateMouseCoordinates() const {
if (mouseAvailable) {
// TODO: normalize coordinates from platform->displaySpace to windowSpace
// TODO: fire mouse movement event
IPair m_coords = { win_x_ret, win_y_ret };
std::pair<int, int> m_coords = { win_x_ret, win_y_ret };
return m_coords;
}
return {};
@@ -341,17 +341,17 @@ bool RWindow::IsVisible() const {
}
IPair RWindow::GetSize() const {
std::pair<int, int> RWindow::GetSize() const {
return { this->width, this->height};
}
//IPair RWindow::getLastKnownResize() const
//std::pair<int, int> RWindow::getLastKnownResize() const
//{
// return lastKnownWindowSize;
//}
// TODO: implement integer IPair/3 types
IPair RWindow::GetPos() const {
// TODO: implement integer std::pair<int, int>/3 types
std::pair<int, int> RWindow::GetPos() const {
return platform->position;
}
@@ -360,8 +360,8 @@ void RWindow::SetPos(int x, int y) {
platform->position = {x, y};
}
void RWindow::SetPos(const IPair& pos) {
SetPos(pos.x, pos.y);
void RWindow::SetPos(const std::pair<int, int>& pos) {
SetPos(pos.first, pos.second);
}
void RWindow::Fullscreen() {
@@ -416,7 +416,7 @@ void RWindow::SetTitle(const std::string& title) {
IPair RWindow::GetPositionOfRenderableArea() const {
std::pair<int, int> RWindow::GetPositionOfRenderableArea() const {
return render_area_position;
}

View File

@@ -10,7 +10,7 @@ RWindow::~RWindow() {
IPair RWindow::GetMouseCoordinates() const {
std::pair<int, int> RWindow::GetMouseCoordinates() const {
return currentMouse.Position;
}
@@ -53,7 +53,7 @@ void RWindow::processMousePress(const MouseButton& btn)
LogEvent(event);
}
void RWindow::processMouseMove(IPair last_pos, IPair new_pos)
void RWindow::processMouseMove(const std::pair<int, int>& last_pos, const std::pair<int, int>& new_pos)
{
currentMouse.Position = new_pos;
auto event = MouseMoveEvent(new_pos);
@@ -120,7 +120,7 @@ std::string RWindow::GetTitle() const {
}
/*
IPair RWindow::GetSize() const
std::pair<int, int> RWindow::GetSize() const
{
return {this->width, this->height};
}
@@ -136,19 +136,19 @@ int RWindow::GetHeight() const
return this->height;
}
void RWindow::SetSizeWithoutEvent(const IPair& size) {
width = size.x;
height = size.y;
void RWindow::SetSizeWithoutEvent(const std::pair<int, int>& size) {
width = size.first;
height = size.second;
}
void RWindow::SetLastKnownWindowSize(const IPair& size) {
void RWindow::SetLastKnownWindowSize(const std::pair<int, int>& size) {
lastKnownWindowSize = size;
}
void RWindow::SetSize(const IPair& size) {
this->width = size.x;
this->height = size.y;
this->SetSize(size.x, size.y);
void RWindow::SetSize(const std::pair<int, int>& size) {
this->width = size.first;
this->height = size.second;
this->SetSize(size.first, size.second);
}
bool RWindow::IsKeyDown(Key key) const {

View File

@@ -209,7 +209,7 @@ void RWindow::SetSize(int newWidth, int newHeight) {
SetWindowPos(platform->hwnd, nullptr, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER);
}
IPair RWindow::GetAccurateMouseCoordinates() const {
std::pair<int, int> RWindow::GetAccurateMouseCoordinates() const {
POINT point;
GetCursorPos(&point);
ScreenToClient(platform->hwnd, &point);
@@ -225,7 +225,7 @@ bool RWindow::GetCursorVisible() {
}
IPair RWindow::GetSize() const {
std::pair<int, int> RWindow::GetSize() const {
RECT rect;
GetClientRect(platform->hwnd, &rect);
return { (rect.right - rect.left), (rect.bottom - rect.top) };
@@ -235,8 +235,8 @@ void RWindow::SetPos(int x, int y) {
SetWindowPos(platform->hwnd, nullptr, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
void RWindow::SetPos(const IPair& pos) {
SetPos(pos.x, pos.y);
void RWindow::SetPos(const std::pair<int, int>& pos) {
SetPos(pos.first, pos.second);
}
void RWindow::Fullscreen() {

View File

@@ -1,125 +0,0 @@
#include <ReWindow/types/Pair.h>
#include <stdexcept>
using namespace ReWindow;
IPair& IPair::operator +=(const IPair& rhs) {
x += rhs.x;
y += rhs.y;
return *this;
}
IPair& IPair::operator -=(const IPair& rhs) {
x -= rhs.x;
y -= rhs.y;
return *this;
}
IPair& IPair::operator *=(const IPair& rhs) {
x *= rhs.x;
y *= rhs.y;
return *this;
}
IPair& IPair::operator /=(const IPair& rhs) {
if (rhs.x == 0 || rhs.y == 0)
throw std::invalid_argument("Division by zero in IPair::operator /=");
x /= rhs.x;
y /= rhs.y;
return *this;
}
IPair::IPair(const IPair& rhs) {
x = rhs.x;
y = rhs.y;
}
IPair IPair::operator +(const IPair& rhs) const {
return { x + rhs.x, y + rhs.y };
}
IPair IPair::operator -(const IPair& rhs) const {
return { x - rhs.x, y - rhs.y };
}
IPair IPair::operator *(const IPair& rhs) const {
return { x * rhs.x, y * rhs.y };
}
IPair IPair::operator /(const IPair& rhs) const {
if (rhs.x == 0 || rhs.y == 0)
throw std::invalid_argument("Division by zero in IPair::operator /");
return { x / rhs.x, y * rhs.y};
}
bool IPair::operator ==(const IPair& rhs) const {
return x == rhs.x && y == rhs.y;
}
bool IPair::operator !=(const IPair& rhs) const {
return !(*this == rhs);
}
FPair FPair::operator +(const FPair& rhs) const {
return { x + rhs.x, y + rhs.y };
}
FPair FPair::operator -(const FPair& rhs) const {
return {x - rhs.x, y - rhs.y };
}
FPair FPair::operator *(const FPair& rhs) const {
return { x * rhs.x, y * rhs.y };
}
FPair FPair::operator /(const FPair& rhs) const {
if (rhs.x == 0 || rhs.y == 0)
throw std::invalid_argument("Division by zero in FPair::operator /");
return { x / rhs.x, y / rhs.y};
}
FPair& FPair::operator +=(const FPair& rhs) {
x += rhs.x;
y += rhs.y;
return *this;
}
FPair& FPair::operator -=(const FPair& rhs) {
x -= rhs.x;
y -= rhs.y;
return *this;
}
FPair& FPair::operator *=(const FPair& rhs) {
x *= rhs.x;
y *= rhs.y;
return *this;
}
FPair& FPair::operator /=(const FPair& rhs) {
if (rhs.x == 0 || rhs.y == 0)
throw std::invalid_argument("Division by zero in FPair::operator /=");
x /= rhs.x;
y /= rhs.y;
return *this;
}
bool FPair::operator ==(const FPair& rhs) const {
return x == rhs.x && y == rhs.y;
}
bool FPair::operator !=(const FPair& rhs) const {
return !(*this == rhs);
}
FPair::FPair(const FPair& rhs) {
x = rhs.x;
y = rhs.y;
}
FPair::FPair(const IPair& rhs) {
x = rhs.x;
y = rhs.y;
}

View File

@@ -13,7 +13,7 @@ void GamepadThumbstick::SetDeadzone(float minimum) {
dead_zone = minimum;
}
FPair GamepadThumbstick::GetPosition() const {
std::pair<float, float> GamepadThumbstick::GetPosition() const {
return position;
}