changeset 1304:49428fc7921c

fix: mouse-locking, not done on windows yes
author sam <sam@basx.dev>
date Wed, 07 Aug 2024 17:26:25 +0700
parents e0326aa067c8
children 21c4e598d820
files semicongine/input.nim semicongine/rendering/platform/linux.nim semicongine/rendering/platform/windows.nim
diffstat 3 files changed, 25 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/input.nim	Wed Aug 07 15:54:22 2024 +0700
+++ b/semicongine/input.nim	Wed Aug 07 17:26:25 2024 +0700
@@ -35,8 +35,8 @@
   input.mouseMove = vec2(0, 0)
   input.windowWasResized = false
 
-  if input.lockMouse and input.hasFocus:
-    setMousePosition(vulkan.window, x=int(vulkan.swapchain.width div 2), y=int(vulkan.swapchain.height div 2))
+  if input.lockMouse:
+    lockMouse(vulkan.window, input.hasFocus)
 
   var killed = false
   for event in vulkan.window.pendingEvents():
@@ -68,7 +68,6 @@
       of RestoredWindow:
         input.windowIsMinimized = false
       of GotFocus:
-        echo "got focus again"
         input.hasFocus = true
       of LostFocus:
         input.hasFocus = false
--- a/semicongine/rendering/platform/linux.nim	Wed Aug 07 15:54:22 2024 +0700
+++ b/semicongine/rendering/platform/linux.nim	Wed Aug 07 17:26:25 2024 +0700
@@ -215,16 +215,18 @@
   if onscreen != 0:
     result = some(Vec2f([float32(winX), float32(winY)]))
 
-proc setMousePosition*(window: NativeWindow, x, y: int) =
-  checkXlibResult XWarpPointer(
-    window.display,
-    default(x11.Window),
-    window.window,
-    0, 0, 0, 0,
-    x.cint,
-    y.cint,
-  )
-  checkXlibResult XSync(window.display, false.XBool)
+proc lockMouse*(window: NativeWindow, lock: bool) =
+  if lock:
+    let s = window.size()
+    checkXlibResult XWarpPointer(
+      window.display,
+      default(x11.Window),
+      window.window,
+      0, 0, 0, 0,
+      s[0].cint div 2,
+      s[1].cint div 2,
+    )
+    checkXlibResult XSync(window.display, false.XBool)
 
 proc createNativeSurface(instance: VkInstance, window: NativeWindow): VkSurfaceKHR =
   var surfaceCreateInfo = VkXlibSurfaceCreateInfoKHR(
--- a/semicongine/rendering/platform/windows.nim	Wed Aug 07 15:54:22 2024 +0700
+++ b/semicongine/rendering/platform/windows.nim	Wed Aug 07 17:26:25 2024 +0700
@@ -186,7 +186,7 @@
 
 proc size*(window: NativeWindow): (int, int) =
   var rect: RECT
-  checkWin32Result GetWindowRect(window.hwnd, addr(rect))
+  checkWin32Result GetClientRect(window.hwnd, addr(rect))
   (int(rect.right - rect.left), int(rect.bottom - rect.top))
 
 proc pendingEvents*(window: NativeWindow): seq[Event] =
@@ -199,16 +199,20 @@
   result = currentEvents
   currentEvents.setLen(0)
 
+proc lockMouse*(window: NativeWindow, lock: bool) =
+  if lock:
+    let s = window.size()
+    var p = POINT(x: s[0].clong div 2, y: s[1].clong div 2)
+    ClientToScreen(window.hwnd, p)
+    checkWin32Result SetCursorPos(p.x.int32, p.y.int32)
+
 proc getMousePosition*(window: NativeWindow): Option[Vec2f] =
   var p: POINT
-  let res = GetCursorPos(addr(p))
-  if res:
-    return some(Vec2f([float32(p.x), float32(p.y)]))
+  if GetCursorPos(addr(p)):
+    if ScreenToClient(window.hwnd, p)
+      return some(Vec2f([float32(p.x), float32(p.y)]))
   return none(Vec2f)
 
-proc setMousePosition*(window: NativeWindow, x, y: int) =
-  checkWin32Result SetCursorPos(x.int32, y.int32)
-
 proc createNativeSurface*(instance: VkInstance, window: NativeWindow): VkSurfaceKHR =
   assert instance.Valid
   var surfaceCreateInfo = VkWin32SurfaceCreateInfoKHR(