@@ -100,6 +100,8 @@ static const char cursorLUT[11][12] = {
100
100
"not-allowed" // 10 MOUSE_CURSOR_NOT_ALLOWED
101
101
};
102
102
103
+ Vector2 lockedMousePos = { 0 };
104
+
103
105
//----------------------------------------------------------------------------------
104
106
// Module Internal Functions Declaration
105
107
//----------------------------------------------------------------------------------
@@ -131,6 +133,7 @@ static EM_BOOL EmscriptenWindowResizedCallback(int eventType, const EmscriptenUi
131
133
static EM_BOOL EmscriptenResizeCallback (int eventType , const EmscriptenUiEvent * event , void * userData );
132
134
133
135
// Emscripten input callback events
136
+ static EM_BOOL EmscriptenMouseMoveCallback (int eventType , const EmscriptenMouseEvent * mouseEvent , void * userData );
134
137
static EM_BOOL EmscriptenMouseCallback (int eventType , const EmscriptenMouseEvent * mouseEvent , void * userData );
135
138
static EM_BOOL EmscriptenPointerlockCallback (int eventType , const EmscriptenPointerlockChangeEvent * pointerlockChangeEvent , void * userData );
136
139
static EM_BOOL EmscriptenTouchCallback (int eventType , const EmscriptenTouchEvent * touchEvent , void * userData );
@@ -862,6 +865,8 @@ void SetMousePosition(int x, int y)
862
865
CORE .Input .Mouse .currentPosition = (Vector2 ){ (float )x , (float )y };
863
866
CORE .Input .Mouse .previousPosition = CORE .Input .Mouse .currentPosition ;
864
867
868
+ if (CORE .Input .Mouse .cursorHidden ) lockedMousePos = CORE .Input .Mouse .currentPosition ;
869
+
865
870
// NOTE: emscripten not implemented
866
871
glfwSetCursorPos (platform .handle , CORE .Input .Mouse .currentPosition .x , CORE .Input .Mouse .currentPosition .y );
867
872
}
@@ -1270,6 +1275,9 @@ int InitPlatform(void)
1270
1275
emscripten_set_click_callback ("#canvas" , NULL , 1 , EmscriptenMouseCallback );
1271
1276
emscripten_set_pointerlockchange_callback (EMSCRIPTEN_EVENT_TARGET_WINDOW , NULL , 1 , EmscriptenPointerlockCallback );
1272
1277
1278
+ // Following the mouse delta when the mouse is locked
1279
+ emscripten_set_mousemove_callback ("#canvas" , NULL , 1 , EmscriptenMouseMoveCallback );
1280
+
1273
1281
// Support touch events
1274
1282
emscripten_set_touchstart_callback ("#canvas" , NULL , 1 , EmscriptenTouchCallback );
1275
1283
emscripten_set_touchend_callback ("#canvas" , NULL , 1 , EmscriptenTouchCallback );
@@ -1477,9 +1485,13 @@ static void MouseButtonCallback(GLFWwindow *window, int button, int action, int
1477
1485
// GLFW3 Cursor Position Callback, runs on mouse move
1478
1486
static void MouseCursorPosCallback (GLFWwindow * window , double x , double y )
1479
1487
{
1480
- CORE .Input .Mouse .currentPosition .x = (float )x ;
1481
- CORE .Input .Mouse .currentPosition .y = (float )y ;
1482
- CORE .Input .Touch .position [0 ] = CORE .Input .Mouse .currentPosition ;
1488
+ // If the pointer is not locked, follow the position
1489
+ if (!CORE .Input .Mouse .cursorHidden )
1490
+ {
1491
+ CORE .Input .Mouse .currentPosition .x = (float )x ;
1492
+ CORE .Input .Mouse .currentPosition .y = (float )y ;
1493
+ CORE .Input .Touch .position [0 ] = CORE .Input .Mouse .currentPosition ;
1494
+ }
1483
1495
1484
1496
#if defined(SUPPORT_GESTURES_SYSTEM ) && defined(SUPPORT_MOUSE_GESTURES )
1485
1497
// Process mouse events as touches to be able to use mouse-gestures
@@ -1505,6 +1517,18 @@ static void MouseCursorPosCallback(GLFWwindow *window, double x, double y)
1505
1517
#endif
1506
1518
}
1507
1519
1520
+ static EM_BOOL EmscriptenMouseMoveCallback (int eventType , const EmscriptenMouseEvent * mouseEvent , void * userData )
1521
+ {
1522
+ // To emulate the GLFW_RAW_MOUSE_MOTION property.
1523
+ if (CORE .Input .Mouse .cursorHidden )
1524
+ {
1525
+ CORE .Input .Mouse .previousPosition .x = lockedMousePos .x - mouseEvent -> movementX ;
1526
+ CORE .Input .Mouse .previousPosition .y = lockedMousePos .y - mouseEvent -> movementY ;
1527
+ }
1528
+
1529
+ return 1 ; // The event was consumed by the callback handler
1530
+ }
1531
+
1508
1532
// GLFW3 Scrolling Callback, runs on mouse wheel
1509
1533
static void MouseScrollCallback (GLFWwindow * window , double xoffset , double yoffset )
1510
1534
{
@@ -1598,6 +1622,12 @@ static EM_BOOL EmscriptenPointerlockCallback(int eventType, const EmscriptenPoin
1598
1622
{
1599
1623
CORE .Input .Mouse .cursorHidden = EM_ASM_INT ( { if (document .pointerLockElement ) return 1 ; }, 0 );
1600
1624
1625
+ if (CORE .Input .Mouse .cursorHidden )
1626
+ {
1627
+ lockedMousePos = CORE .Input .Mouse .currentPosition ;
1628
+ CORE .Input .Mouse .previousPosition = lockedMousePos ;
1629
+ }
1630
+
1601
1631
return 1 ; // The event was consumed by the callback handler
1602
1632
}
1603
1633
0 commit comments