Skip to content

Commit 42220f1

Browse files
author
Avaer Kazmer
authored
Merge pull request #1331 from exokitxr/mouse-movement
Add GLFW movementX/movementY tracking
2 parents c147cce + c00a164 commit 42220f1

File tree

1 file changed

+15
-5
lines changed
  • deps/exokit-bindings/glfw/src

1 file changed

+15
-5
lines changed

deps/exokit-bindings/glfw/src/glfw.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ std::mutex windowHandleMutex;
1515
NATIVEwindow *sharedWindow = nullptr;
1616
InjectionHandler mainThreadInjectionHandler;
1717
std::mutex injectionHandlerMapMutex;
18-
// int lastX = 0, lastY = 0; // XXX track this per-window
18+
int lastX = -1, lastY = -1; // XXX track this per-window
1919
uint64_t lastClickTime = 0;
2020
#ifdef MAIN_THREAD_POLLING
2121
std::thread::id mainThreadId;
@@ -675,18 +675,28 @@ void APIENTRY cursorPosCB(NATIVEwindow* window, double x, double y) {
675675
if(x<0 || x>=w) return;
676676
if(y<0 || y>=h) return;
677677

678+
int xi = static_cast<int>(x);
679+
int yi = static_cast<int>(y);
678680
int movementX, movementY;
679681

680682
int mode = glfwGetInputMode(window, GLFW_CURSOR);
681683
if (mode == GLFW_CURSOR_DISABLED) {
682-
movementX = x - (w / 2);
683-
movementY = y - (h / 2);
684+
movementX = xi - (w / 2);
685+
movementY = yi - (h / 2);
684686

685687
glfwSetCursorPos(window, w / 2, h / 2);
686688
} else {
687-
movementX = 0;
688-
movementY = 0;
689+
if (lastX != -1 && lastY != -1) {
690+
movementX = xi - lastX;
691+
movementY = yi - lastY;
692+
} else {
693+
movementX = 0;
694+
movementY = 0;
695+
}
689696
}
697+
698+
lastX = xi;
699+
lastY = yi;
690700

691701
QueueEvent(window, [=](std::function<void(int, Local<Value> *)> eventHandlerFn) -> void {
692702
Local<Object> evt = Nan::New<Object>();

0 commit comments

Comments
 (0)