Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add drag and drop callback #21

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/game_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class GameWindow {
using TouchEndCallback = std::function<void (int, double, double)>;
using KeyboardCallback = std::function<void (KeyCode, KeyAction)>;
using KeyboardTextCallback = std::function<void (std::string const&)>;
using DropCallback = std::function<void (std::string const&)>;
using PasteCallback = std::function<void (std::string const&)>;
using GamepadStateCallback = std::function<void (int, bool)>;
using GamepadButtonCallback = std::function<void (int, GamepadButtonId, bool)>;
Expand All @@ -58,6 +59,7 @@ class GameWindow {
TouchEndCallback touchEndCallback;
KeyboardCallback keyboardCallback;
KeyboardTextCallback keyboardTextCallback;
DropCallback dropCallback;
PasteCallback pasteCallback;
GamepadStateCallback gamepadStateCallback;
GamepadButtonCallback gamepadButtonCallback;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/window_eglut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/window_eglut.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/window_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/window_glfw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/window_sdl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down