WindowEvents

This commit is contained in:
2025-04-19 15:08:39 -05:00
parent 600d0f3c81
commit 87afa3a0ec
5 changed files with 289 additions and 5 deletions

View File

@@ -9,7 +9,9 @@ namespace Archimedes {
public:
virtual ~Event() {}
virtual ~Event() {
std::cout << (std::string)*this;
}
virtual operator std::string() const = 0;

View File

@@ -1,6 +1,7 @@
#ifndef WINDOW_H
#define WINDOW_H
#include "WindowEvents.h"
#include "utils/Renderer/Renderer.h"
#include "WindowGLFW/WindowGLFW.h"
@@ -11,7 +12,7 @@ namespace Archimedes {
public:
Window() {}
Window(const std::function<void(Event*)>& sendEventFn) : window(this, sendEventFn) {}
~Window() {}
@@ -19,7 +20,7 @@ namespace Archimedes {
void doFrame() {
window.pollEvents();
//window.pollEvents();
window.getSize(renderer->w, renderer->h);

View File

@@ -0,0 +1,209 @@
#ifndef WINDOWEVENTS_H
#define WINDOWEVENTS_H
#include "utils/Events/Event.h"
//implimentation independent events
namespace Archimedes {
class Window;
struct WindowData {
Window* window;
std::list<Event*> eventList;
std::function<void(Event*)> sendEvent;
};
class WindowResizeEvent : public Event {
public:
WindowResizeEvent() : width(0), height(0) {}
WindowResizeEvent(int w, int h) : width(w), height(h) {}
~WindowResizeEvent() {
}
operator std::string() const { return "Archimedes::WindowResizeEvent"; }
int width, height;
};
class WindowCloseEvent : public Event {
public:
WindowCloseEvent() : window(nullptr) {}
WindowCloseEvent(const Window* w) : window(w) {}
~WindowCloseEvent() {
}
operator std::string() const { return "Archimedes::WindowCloseEvent"; }
const Window* window;
};
class WindowKeyPressedEvent : public Event {
public:
WindowKeyPressedEvent() : key(0), repeat(0) {}
WindowKeyPressedEvent(unsigned int k, unsigned int r) : key(k), repeat(r) {}
~WindowKeyPressedEvent() {
}
operator std::string() const { return "Archimedes::WindowKeyPressedEvent"; }
unsigned int key;
unsigned int repeat;
};
class WindowKeyReleasedEvent : public Event {
public:
WindowKeyReleasedEvent() : key(0) {}
WindowKeyReleasedEvent(unsigned int k) : key(k) {}
~WindowKeyReleasedEvent() {
}
operator std::string() const { return "Archimedes::WindowKeyReleasedEvent"; }
unsigned int key;
};
class WindowMouseButtonPressedEvent : public Event {
public:
WindowMouseButtonPressedEvent() : button(0) {}
WindowMouseButtonPressedEvent(unsigned int b) : button(b) {}
~WindowMouseButtonPressedEvent() {
}
operator std::string() const { return "Archimedes::WindowMouseButtonPressedEvent"; }
unsigned int button;
};
class WindowMouseButtonReleasedEvent : public Event {
public:
WindowMouseButtonReleasedEvent() : button(0) {}
WindowMouseButtonReleasedEvent(unsigned int b) : button(b) {}
~WindowMouseButtonReleasedEvent() {
}
operator std::string() const { return "Archimedes::WindowMouseButtonReleasedEvent"; }
unsigned int button;
};
class WindowScrollEvent : public Event {
public:
WindowScrollEvent() : dx(0), dy(0) {}
WindowScrollEvent(double x, double y) : dx(x), dy(y) {}
~WindowScrollEvent() {
}
operator std::string() const { return "Archimedes::WindowScrollEvent"; }
double dx, dy;
};
class WindowMouseMovedEvent : public Event {
public:
WindowMouseMovedEvent() : x(0), y(0) {}
WindowMouseMovedEvent(int x, int y) : x(x), y(y) {}
~WindowMouseMovedEvent() {
}
operator std::string() const { return "Archimedes::WindowMouseMovedEvent"; }
int x, y;
};
class WindowFocusedEvent : public Event {
public:
WindowFocusedEvent() : window(nullptr) {}
WindowFocusedEvent(const Window* w) : window(w) {}
~WindowFocusedEvent() {
}
operator std::string() const { return "Archimedes::WindowFocusedEvent"; }
const Window* window;
};
class WindowFocusLostEvent : public Event {
public:
WindowFocusLostEvent() : window(nullptr) {}
WindowFocusLostEvent(const Window* w) : window(w) {}
~WindowFocusLostEvent() {
}
operator std::string() const { return "Archimedes::WindowFocusLostEvent"; }
const Window* window;
};
class WindowMovedEvent : public Event {
public:
WindowMovedEvent() : x(0), y(0) {}
WindowMovedEvent(int x, int y) : x(x), y(y) {}
~WindowMovedEvent() {
}
operator std::string() const { return "Archimedes::WindowMovedEvent"; }
int x, y;
};
}
#endif

View File

@@ -3,16 +3,23 @@
#ifdef WINDOW_GLFW
#undef WINDOW_GLFW
#include "utils/Window/WindowEvents.h"
#include <GLFW/glfw3.h>
namespace Archimedes {
class Window;
class WindowGLFW {
public:
WindowGLFW() {
WindowGLFW(Window* p, const std::function<void(Event*)>& sendEvent) {
data.window = p;
data.sendEvent = sendEvent;
glfwSetErrorCallback([](int e, const char* m){
std::cout << "GLFW Error: " << m << std::endl;
@@ -31,6 +38,60 @@ namespace Archimedes {
glfwMakeContextCurrent(w);
glfwSwapInterval(1);
glfwSetWindowUserPointer(w, &data);
glfwSetWindowSizeCallback(w, [](GLFWwindow* window, int w, int h){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new WindowResizeEvent(w, h));
});
glfwSetWindowCloseCallback(w, [](GLFWwindow* window){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new WindowCloseEvent(d.window));
});
glfwSetKeyCallback(w, [](GLFWwindow* window, int key, int scancode, int action, int mods){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
switch(action) {
case GLFW_PRESS:
d.sendEvent(new WindowKeyPressedEvent(key, 0));
break;
case GLFW_RELEASE:
d.sendEvent(new WindowKeyReleasedEvent(key));
break;
case GLFW_REPEAT:
d.sendEvent(new WindowKeyPressedEvent(key, 1));
break;
}
});
glfwSetMouseButtonCallback(w, [](GLFWwindow* window, int button, int action, int mods){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
switch(action) {
case GLFW_PRESS:
d.sendEvent(new WindowMouseButtonPressedEvent(button));
break;
case GLFW_RELEASE:
d.sendEvent(new WindowMouseButtonReleasedEvent(button));
break;
}
});
glfwSetScrollCallback(w, [](GLFWwindow* window, double dx, double dy){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new WindowScrollEvent(dx, dy));
});
glfwSetCursorPosCallback(w, [](GLFWwindow* window, double dx, double dy){
WindowData& d = *(WindowData*) glfwGetWindowUserPointer(window);
d.sendEvent(new WindowMouseMovedEvent(dx, dy));
});
}
~WindowGLFW() {
@@ -54,8 +115,11 @@ namespace Archimedes {
GLFWwindow* getWindow() { return w; }
WindowData data;
private:
GLFWwindow* w;
};
typedef WindowGLFW WindowImpl;

View File

@@ -14,7 +14,9 @@ WindowModule::~WindowModule() {
void WindowModule::onLoad() {
window = new Archimedes::Window();
window = new Archimedes::Window([this](Archimedes::Event* e){
app->emitEvent(e);
});
renderer = new Archimedes::Renderer();
@@ -34,6 +36,12 @@ void WindowModule::run() {
app->end();
}
for(auto e : window->getWindowImpl().data.eventList) {
app->emitEvent(e);
}
window->getWindowImpl().data.eventList.clear();
window->doFrame();
}