Implement CursorStyles, minor refactors

This commit is contained in:
2024-02-21 20:10:06 -05:00
parent b1dfab70a1
commit 2930391ee4
5 changed files with 197 additions and 105 deletions

View File

@@ -39,6 +39,7 @@ endif()
include_directories("include")
add_library(ReWindowLibrary SHARED ${SOURCES}
include/rewindow/types/cursors.h
)
# Why god???
set_target_properties(ReWindowLibrary PROPERTIES LINKER_LANGUAGE CXX)

View File

@@ -0,0 +1,60 @@
#pragma once
#include <X11/cursorfont.h>
namespace ReWindow
{
enum class X11CursorStyle
{
Default = XC_left_ptr,
X = XC_X_cursor,
Arrow = XC_arrow,
IBeam = XC_xterm,
BottomLeftCorner = XC_bottom_left_corner,
BottomRightCorner = XC_bottom_right_corner,
BottomSide = XC_bottom_side,
Dot = XC_dot,
DoubleArrow = XC_double_arrow,
Exchange = XC_exchange,
Hand = XC_hand2,
LeftSide = XC_left_side,
Plus = XC_plus,
RightSide = XC_right_side,
Pencil = XC_pencil
};
// https://learn.microsoft.com/en-us/windows/win32/menurc/about-cursors
enum class WindowsCursorStyle
{
Arrow,
IBeam,
Wait,
Cross,
Hand,
AppStarting,
};
class CursorStyle
{
public:
X11CursorStyle X11Cursor;
CursorStyle(X11CursorStyle style) : X11Cursor(style) {}
};
namespace Cursors
{
static const CursorStyle Default {X11CursorStyle::Default};
static const CursorStyle X {X11CursorStyle::X};
static const CursorStyle Arrow {X11CursorStyle::Arrow};
static const CursorStyle IBeam {X11CursorStyle::IBeam};
static const CursorStyle BottomLeftCorner {X11CursorStyle::BottomLeftCorner};
static const CursorStyle BottomRightCorner {X11CursorStyle::BottomRightCorner};
static const CursorStyle BottomSide {X11CursorStyle::BottomSide};
static const CursorStyle Dot {X11CursorStyle::Dot};
static const CursorStyle DoubleArrow {X11CursorStyle::DoubleArrow};
static const CursorStyle Exchange {X11CursorStyle::Exchange};
static const CursorStyle Hand {X11CursorStyle::Hand};
static const CursorStyle LeftSide {X11CursorStyle::LeftSide};
static const CursorStyle Plus {X11CursorStyle::Plus};
static const CursorStyle RightSide {X11CursorStyle::RightSide};
static const CursorStyle Pencil {X11CursorStyle::Pencil};
}
}

View File

@@ -13,6 +13,8 @@
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include "cursors.h"
#endif
@@ -33,23 +35,6 @@ enum class RenderingAPI: uint8_t {
VULKAN = 1,
};
enum class CursorStyle
{
Default,
X,
Arrow,
IBeam,
BottomLeftCorner,
BottomRightCorner,
BottomSide,
Dot,
DoubleArrow,
Exchange,
Hand,
LeftSide,
Plus,
RightSide,
};
enum class KeyState { Pressed, Released };
@@ -140,37 +125,35 @@ namespace ReWindow
using namespace WindowEvents;
class RWindow {
private:
Vector2 lastKnownWindowSize {0, 0};
bool flags[4];
std::vector<RWindowEvent> eventLog;
KeyboardState currentKeyboard; // Current Frame's Keyboard State
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
bool fullscreenmode = false;
public:
Event<> focusGained;
Event<> focusLost;
// TODO: Integrate J3ML
Event<MouseMoveEvent&> mouseMoved;
Event<MouseButton> mouseBtnPressed;
Event<MouseButton> mouseBtnReleased;
Event<KeyDownEvent> onKeyboardPress;
Event<KeyUpEvent> onKeyboardRelease;
virtual void OnFocusLost(const RWindowEvent& e) {}
virtual void OnFocusGain(const RWindowEvent& e) {}
virtual void OnRefresh(float elapsed) {}
virtual void OnResizeSuccess() {}
virtual bool OnResizeRequest(const WindowResizeRequestEvent& e) {}
virtual void OnKeyDownEvent(const KeyDownEvent&) {}
virtual void OnKeyUpEvent(const KeyUpEvent&) {}
virtual void OnKeyDown(const KeyDownEvent&) {}
virtual void OnKeyUp(const KeyUpEvent&) {}
virtual void OnMouseMove(const MouseMoveEvent&) {}
virtual void OnMouseButtonDown(const MouseButtonDownEvent&) {}
virtual void OnMouseButtonUp(const MouseButtonUpEvent&) {}
RWindow()
{
title = "ReWindow Application";
width = 640;
height = 480;
}
RWindow(const std::string& title, int width, int height);
RWindow(const std::string& title, int width, int height, RenderingAPI renderer);
RWindow();
void SetRenderer(RenderingAPI api)
{
renderer = api;
}
void Open();
void Close();
void CloseAndReopenInPlace();
// TODO: Must be implemented from scratch as a Motif Window in x11
void MessageBox();
@@ -192,38 +175,43 @@ namespace ReWindow
void setResizable(bool resizable);
void setVsyncEnabled(bool);
void setTitle(const std::string& title);
std::string getTitle() const;
void fullscreen();
void restoreFromFullscreen();
bool getFlag(RWindowFlags flag) const;
void setFlag(RWindowFlags flag, bool state);
void init(RenderingAPI api, const char* title, int width, int height, bool vsync);
//void init(RenderingAPI api, const char* title, int width, int height, bool vsync);
void destroyWindow();
void pollEvents();
void refresh();
void setSize(int width, int height);
void setSize(const Vector2& size);
/// Returns the position of the window's top-left corner relative to the display
Vector2 getPos() const;
Vector2 getSize() const;
void setPos(int x, int y);
void setPos(const Vector2& pos);
Vector2 getCursorPos() const;
void raise() const;
void lower() const;
void setCursor(CursorStyle style) const;
// TODO: Implement
// void setCursor(Icon* icon) const;
void setCursorStyle(CursorStyle style) const;
void setCursorCustomIcon() const;
static void glSwapBuffers();
//Initialize to false because it's not guaranteed that they will be cleared first
private:
Vector2 lastKnownWindowSize {0, 0};
bool flags[4];
std::vector<RWindowEvent> eventLog;
KeyboardState currentKeyboard; // Current Frame's Keyboard State
KeyboardState previousKeyboard; // Previous Frame's Keyboard State
bool fullscreenmode = false;
std::string title;
int width;
int height;
RenderingAPI renderer;
bool open = false;
bool resizable;
};

View File

@@ -3,31 +3,35 @@
#if __linux__
void KeyDown(const ReWindow::WindowEvents::KeyDownEvent& e)
{
//std::cout << e.key.CharCode << " pressed" << std::endl;
}
Vector2 mouse_pos;
// TODO: Move to J3ML::LinearAlgebra::Vector2
std::ostream& operator<<(std::ostream& os, const Vector2& v)
{
return os << "{" << v.x << ", " << v.y << "}";
}
class MyWindow : public ReWindow::RWindow
{
public:
MyWindow(const std::string& title, int w, int h) : ReWindow::RWindow(title, w, h)
{}
void OnMouseMove(const ReWindow::MouseMoveEvent& e) override
{
//std::cout << "Mouse Moved: " << e.Position << " @ " << std::endl;
}
void OnKeyDown(const ReWindow::KeyDownEvent& e) override
{
if (e.key == Keys::L)
this->setCursorStyle(ReWindow::Cursors::Pencil);
}
void OnRefresh(float elapsed) override
{
glSwapBuffers();
auto pos = getCursorPos();
std::cout << pos.x << ", " << pos.y << std::endl;
//std::cout << pos.x << ", " << pos.y << std::endl;
}
bool OnResizeRequest(const ReWindow::WindowResizeRequestEvent& e) override
{
@@ -37,16 +41,15 @@ public:
};
int main() {
auto* window = new MyWindow();
window->init(RenderingAPI::OPENGL, "name",100,100, true);
window->setFlag(RWindowFlags::RESIZABLE, true);
auto* window = new MyWindow("Test Window", 600, 480);
window->onKeyboardPress += KeyDown;
window->onKeyboardRelease += [&](ReWindow::WindowEvents::KeyUpEvent e)
{
if (e.key == Keys::F)
window->setFullscreen(!window->isFullscreen());
};
window->SetRenderer(RenderingAPI::OPENGL);
window->Open();
// TODO: Cannot set flags until after window is open
// Make this work somehow
window->setFullscreen(false);
window->setVsyncEnabled(true);
window->setResizable(true);
while (window->isAlive()) {
window->pollEvents();

View File

@@ -4,10 +4,12 @@
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <cstdlib>
#include <cstring>
#include <thread>
#include "rewindow/types/cursors.h"
// TODO: Move all "global" members to be instantiated class members of Window
// Doing this would break the intended "Platform-Specific" Encapsulation
@@ -27,43 +29,21 @@ GLXContext glContext;
namespace ReWindow {
RWindow::RWindow() : flags(false,false,false,false) {
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::RWindow(const std::string& title, int width, int height, RenderingAPI renderer) :
title(title), width(width), height(height), renderer(renderer)
{ }
void RWindow::raise() const { XRaiseWindow(display, window); }
void RWindow::lower() const { XLowerWindow(display, window); }
void RWindow::init(RenderingAPI api, const char* title, int width, int height, bool vsync) {
if (api == RenderingAPI::OPENGL) {
xSetWindowAttributes.border_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.override_redirect = True;
xSetWindowAttributes.event_mask = ExposureMask;
setVsyncEnabled(vsync);
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
visual = glXChooseVisual(display, defaultScreen, glAttributes);
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
vsync = sync;
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual,
AllocNone);
window = XCreateWindow(display, RootWindow(display, defaultScreen), 0, 0, width, height, 0, visual->depth,
InputOutput, visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
&xSetWindowAttributes);
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap | ResizeRequest | ResizeRedirectMask);
XMapWindow(display, window);
XStoreName(display, window, title);
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
glXMakeCurrent(display, window, glContext);
setVsyncEnabled(vsync);
} else {exit(0);}
}
void RWindow::destroyWindow() {
XDestroySubwindows(display, window);
@@ -76,14 +56,12 @@ namespace ReWindow {
// TODO: Implement refresh time keeping
OnRefresh(0.f);
// TODO: Check if mouse coords have changed, only then fire OnMouseMove event
Vector2 mouse_coords = getCursorPos();
auto eventData = MouseMoveEvent(mouse_coords);
OnMouseMove(eventData);
std::this_thread::sleep_for(1ms);
}
@@ -120,7 +98,7 @@ namespace ReWindow {
if (xev.type == FocusIn) {
XAutoRepeatOff(display);
focusGained.Invoke();
//focusGained.Invoke();
RWindowEvent event {};
OnFocusGain(event);
setFlag(RWindowFlags::IN_FOCUS, true);
@@ -128,7 +106,7 @@ namespace ReWindow {
if (xev.type == FocusOut) {
XAutoRepeatOn(display);
focusLost.Invoke();
//focusLost.Invoke();
RWindowEvent event {};
OnFocusLost(event);
setFlag(RWindowFlags::IN_FOCUS, false);
@@ -139,8 +117,7 @@ namespace ReWindow {
auto key = GetKeyFromX11Scancode(scancode);
currentKeyboard.PressedKeys[key] = false;
KeyUpEvent eventData = KeyUpEvent(key);
OnKeyUpEvent(eventData);
OnKeyUp(eventData);
}
if (xev.type == KeyPress) {
@@ -148,7 +125,7 @@ namespace ReWindow {
auto key = GetKeyFromX11Scancode(scancode);
currentKeyboard.PressedKeys[key] = true;
KeyDownEvent eventData = KeyDownEvent(key);
OnKeyDownEvent(eventData);
OnKeyDown(eventData);
eventLog.push_back(eventData);
}
@@ -172,6 +149,7 @@ namespace ReWindow {
if (xev.type == 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)
{
//auto eventData = MouseMoveEvent(xev.xmotion.x, xev.xmotion.y);
@@ -193,8 +171,10 @@ namespace ReWindow {
}
// Might make the window go off the screen on some window managers.
void RWindow::setSize(int width, int height)
void RWindow::setSize(int newWidth, int newHeight)
{
this->width = newWidth;
this->height = newHeight;
if (!getFlag(RWindowFlags::RESIZABLE))
return;
XResizeWindow(display, window, width, height);
@@ -202,6 +182,8 @@ namespace ReWindow {
void RWindow::setSize(const Vector2& size)
{
this->width = size.x;
this->height = size.y;
this->setSize(size.x, size.y);
}
@@ -273,6 +255,7 @@ namespace ReWindow {
}
void RWindow::setResizable(bool resizable) {
this->resizable = resizable;
this->setFlag(RWindowFlags::RESIZABLE, resizable);
}
@@ -333,5 +316,62 @@ namespace ReWindow {
return fullscreenmode;
}
using J3ML::u32;
void RWindow::setCursorStyle(CursorStyle style) const {
u32 x11_cursor_resolved_enum = static_cast<u32>(style.X11Cursor);
Cursor c = XCreateFontCursor(display, x11_cursor_resolved_enum);
XDefineCursor(display, window, c);
}
void RWindow::Open() {
xSetWindowAttributes.border_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.background_pixel = BlackPixel(display, defaultScreen);
xSetWindowAttributes.override_redirect = True;
xSetWindowAttributes.event_mask = ExposureMask;
//setVsyncEnabled(vsync);
if (renderer == RenderingAPI::OPENGL)
{
GLint glAttributes[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None};
visual = glXChooseVisual(display, defaultScreen, glAttributes);
glContext = glXCreateContext(display, visual, nullptr, GL_TRUE);
}
xSetWindowAttributes.colormap = XCreateColormap(display, RootWindow(display, defaultScreen), visual->visual,
AllocNone);
window = XCreateWindow(display, RootWindow(display, defaultScreen), 0, 0, width, height, 0, visual->depth,
InputOutput, visual->visual, CWBackPixel | CWColormap | CWBorderPixel | NoEventMask,
&xSetWindowAttributes);
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask |
PointerMotionMask |
PointerMotionHintMask | FocusChangeMask | StructureNotifyMask | SubstructureRedirectMask |
SubstructureNotifyMask | CWColormap | ResizeRequest | ResizeRedirectMask);
XMapWindow(display, window);
XStoreName(display, window, title.c_str());
wmDeleteWindow = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &wmDeleteWindow, 1);
if (renderer == RenderingAPI::OPENGL)
{
glXMakeCurrent(display, window, glContext);
}
//setVsyncEnabled(vsync);
open = true;
}
void RWindow::setTitle(const std::string &title) {
this->title = title;
XStoreName(display, window, title.c_str());
}
std::string RWindow::getTitle() const {
return this->title;
}
}