From 67a3b5c953113eb6ecfc77366c1fcf05d97e0b44 Mon Sep 17 00:00:00 2001 From: GameParrot <85067619+GameParrot@users.noreply.github.com> Date: Thu, 2 Jan 2025 15:52:20 -0500 Subject: [PATCH] add drag and drop callback --- include/game_window.h | 9 +++++++++ src/window_eglut.cpp | 7 +++++++ src/window_eglut.h | 1 + src/window_glfw.cpp | 8 ++++++++ src/window_glfw.h | 1 + src/window_sdl3.cpp | 3 +++ 6 files changed, 29 insertions(+) diff --git a/include/game_window.h b/include/game_window.h index 49baa9f..be53d9b 100644 --- a/include/game_window.h +++ b/include/game_window.h @@ -41,6 +41,7 @@ class GameWindow { using TouchEndCallback = std::function; using KeyboardCallback = std::function; using KeyboardTextCallback = std::function; + using DropCallback = std::function; using PasteCallback = std::function; using GamepadStateCallback = std::function; using GamepadButtonCallback = std::function; @@ -58,6 +59,7 @@ class GameWindow { TouchEndCallback touchEndCallback; KeyboardCallback keyboardCallback; KeyboardTextCallback keyboardTextCallback; + DropCallback dropCallback; PasteCallback pasteCallback; GamepadStateCallback gamepadStateCallback; GamepadButtonCallback gamepadButtonCallback; @@ -131,6 +133,8 @@ class GameWindow { void setKeyboardTextCallback(KeyboardTextCallback callback) { keyboardTextCallback = std::move(callback); } + void setDropCallback(DropCallback callback) { dropCallback = std::move(callback); } + void setPasteCallback(PasteCallback callback) { pasteCallback = std::move(callback); } // Used when the cursor is disabled @@ -191,6 +195,11 @@ class GameWindow { if (keyboardTextCallback != nullptr) keyboardTextCallback(c); } + void onDrop(std::string const& path) { + if (dropCallback != nullptr) { + dropCallback(path); + } + } void onPaste(std::string const& c) { if (pasteCallback != nullptr) pasteCallback(c); diff --git a/src/window_eglut.cpp b/src/window_eglut.cpp index 138dff1..9159012 100644 --- a/src/window_eglut.cpp +++ b/src/window_eglut.cpp @@ -36,6 +36,7 @@ EGLUTWindow::EGLUTWindow(const std::string& title, int width, int height, Graphi eglutTouchUpdateFunc(_eglutTouchUpdateFunc); eglutTouchEndFunc(_eglutTouchEndFunc); eglutKeyboardFunc(_eglutKeyboardFunc); + eglutDropFunc(_eglutDropFunc); eglutSpecialFunc(_eglutKeyboardSpecialFunc); eglutPasteFunc(_eglutPasteFunc); eglutFocusFunc(_eglutFocusFunc); @@ -243,6 +244,12 @@ void EGLUTWindow::_eglutKeyboardFunc(char str[5], int action) { } } +void EGLUTWindow::_eglutDropFunc(const char* path) { + if (currentWindow == nullptr) + return; + currentWindow->onDrop(path); +} + KeyCode EGLUTWindow::getKeyMinecraft(int keyCode) { if (keyCode >= XK_A && keyCode <= XK_Z) return (KeyCode) (keyCode - XK_A + (int) KeyCode::A); diff --git a/src/window_eglut.h b/src/window_eglut.h index 6646678..7c4e32c 100644 --- a/src/window_eglut.h +++ b/src/window_eglut.h @@ -38,6 +38,7 @@ class EGLUTWindow : public WindowWithLinuxJoystick { static void _eglutTouchUpdateFunc(int id, double x, double y); static void _eglutTouchEndFunc(int id, double x, double y); static void _eglutKeyboardFunc(char str[5], int action); + static void _eglutDropFunc(const char* path); static void _eglutKeyboardSpecialFunc(int key, int action); static void _eglutPasteFunc(const char* str, int len); static void _eglutFocusFunc(int action); diff --git a/src/window_glfw.cpp b/src/window_glfw.cpp index e01d4c4..34e8e49 100644 --- a/src/window_glfw.cpp +++ b/src/window_glfw.cpp @@ -50,6 +50,7 @@ GLFWGameWindow::GLFWGameWindow(const std::string& title, int width, int height, glfwSetWindowCloseCallback(window, _glfwWindowCloseCallback); glfwSetKeyCallback(window, _glfwKeyCallback); glfwSetCharCallback(window, _glfwCharCallback); + glfwSetDropCallback(window, _glfwDropCallback); glfwSetWindowFocusCallback(window, _glfwWindowFocusCallback); glfwSetWindowContentScaleCallback(window, _glfwWindowContentScaleCallback); glfwMakeContextCurrent(window); @@ -428,6 +429,13 @@ void GLFWGameWindow::_glfwCharCallback(GLFWwindow* window, unsigned int ch) { user->onKeyboardText(cvt.to_bytes(ch)); } +void GLFWGameWindow::_glfwDropCallback(GLFWwindow* window, int count, const char** paths) { + GLFWGameWindow* user = (GLFWGameWindow*) glfwGetWindowUserPointer(window); + for (int i = 0; i < count; i++) { + user->onDrop(std::string(paths[i])); + } +} + void GLFWGameWindow::_glfwWindowCloseCallback(GLFWwindow* window) { GLFWGameWindow* user = (GLFWGameWindow*) glfwGetWindowUserPointer(window); glfwSetWindowShouldClose(window, GLFW_FALSE); diff --git a/src/window_glfw.h b/src/window_glfw.h index 73b278c..630f781 100644 --- a/src/window_glfw.h +++ b/src/window_glfw.h @@ -45,6 +45,7 @@ class GLFWGameWindow : public GameWindow { static void _glfwScrollCallback(GLFWwindow* window, double x, double y); static void _glfwKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); static void _glfwCharCallback(GLFWwindow* window, unsigned int ch); + static void _glfwDropCallback(GLFWwindow* window, int count, const char** paths); static void _glfwWindowCloseCallback(GLFWwindow* window); static void _glfwWindowFocusCallback(GLFWwindow* window, int focused); static void _glfwWindowContentScaleCallback(GLFWwindow* window, float scalex, float scaley); diff --git a/src/window_sdl3.cpp b/src/window_sdl3.cpp index 7b7bb3a..8cbcab2 100644 --- a/src/window_sdl3.cpp +++ b/src/window_sdl3.cpp @@ -272,6 +272,9 @@ void SDL3GameWindow::pollEvents() { case SDL_EVENT_TEXT_INPUT: onKeyboardText(ev.text.text ? ev.text.text : ""); break; + case SDL_EVENT_DROP_FILE: + onDrop(ev.drop.data); + break; case SDL_EVENT_WINDOW_CLOSE_REQUESTED: onClose(); break;