changeset 1305:21c4e598d820

did: work on cursor issues, but now sync unfinished things to notebook
author sam <sam@basx.dev>
date Wed, 07 Aug 2024 19:09:03 +0700
parents 49428fc7921c
children 7be3628298f5
files semicongine/input.nim semicongine/rendering/platform/linux.nim semicongine/rendering/platform/windows.nim tests/test_gltf.nim
diffstat 4 files changed, 26 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/input.nim	Wed Aug 07 17:26:25 2024 +0700
+++ b/semicongine/input.nim	Wed Aug 07 19:09:03 2024 +0700
@@ -14,8 +14,8 @@
     mouseIsDown: set[MouseButton]
     mouseWasPressed: set[MouseButton]
     mouseWasReleased: set[MouseButton]
-    mousePosition: Vec2f
-    mouseMove: Vec2f
+    mousePosition: Vec2i
+    mouseMove: Vec2i
     mouseWheel: float32
     windowWasResized: bool = true
     windowIsMinimized: bool = false
@@ -35,8 +35,12 @@
   input.mouseMove = vec2(0, 0)
   input.windowWasResized = false
 
-  if input.lockMouse:
-    lockMouse(vulkan.window, input.hasFocus)
+  # if input.lockMouse and input.hasFocus:
+    # setMousePosition(vulkan.window, x=int(vulkan.swapchain.width div 2), y=int(vulkan.swapchain.height div 2))
+
+  let newMousePos = getMousePosition(vulkan.window)
+  input.mouseMove = newPos - input.mousePosition
+  input.mousePosition = newPos
 
   var killed = false
   for event in vulkan.window.pendingEvents():
@@ -57,10 +61,6 @@
       of MouseReleased:
         input.mouseWasReleased.incl event.button
         input.mouseIsDown.excl event.button
-      of MouseMoved:
-        let newPos = vec2(event.x, event.y)
-        input.mouseMove = newPos - input.mousePosition
-        input.mousePosition = newPos
       of MouseWheel:
         input.mouseWheel = event.amount
       of MinimizedWindow:
@@ -85,10 +85,10 @@
 proc mouseWasReleased*(): bool = input.mouseWasReleased.len > 0
 proc mouseWasReleased*(button: MouseButton): bool = button in input.mouseWasReleased
 proc mouseReleasedButtons*(): set[MouseButton] = input.mouseWasReleased
-proc mousePosition*(): Vec2f = input.mousePosition
-proc mousePositionNormalized*(size: (int, int)): Vec2f =
-  result.x = (input.mousePosition.x / float32(size[0])) * 2.0 - 1.0
-  result.y = (input.mousePosition.y / float32(size[1])) * 2.0 - 1.0
+proc mousePositionPixel*(): Vec2i = input.mousePosition
+proc mousePosition*(size: (int, int)): Vec2f =
+  result.x = (input.mousePosition.x.float32 / float32(size[0])) * 2.0 - 1.0
+  result.y = (input.mousePosition.y.float32 / float32(size[1])) * 2.0 - 1.0
 proc mouseMove*(): Vec2f = input.mouseMove
 proc mouseWheel*(): float32 = input.mouseWheel
 proc windowWasResized*(): auto = input.windowWasResized
--- a/semicongine/rendering/platform/linux.nim	Wed Aug 07 17:26:25 2024 +0700
+++ b/semicongine/rendering/platform/linux.nim	Wed Aug 07 19:09:03 2024 +0700
@@ -181,7 +181,8 @@
       result.add Event(eventType: MouseReleased, button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN))
     of MotionNotify:
       let motion = cast[PXMotionEvent](addr(event))
-      result.add Event(eventType: MouseMoved, x: motion.x, y: motion.y)
+      if motion.x > 0 or motion.y > 0:
+        result.add Event(eventType: MouseMoved, x: motion.x, y: motion.y)
     of FocusIn:
       result.add Event(eventType: GotFocus)
     of FocusOut:
@@ -192,7 +193,7 @@
       discard
 
 
-proc getMousePosition*(window: NativeWindow): Option[Vec2f] =
+proc getMousePosition*(window: NativeWindow): Option[Vec2i] =
   var
     root: x11.Window
     win: x11.Window
@@ -213,7 +214,7 @@
       addr(mask),
     )
   if onscreen != 0:
-    result = some(Vec2f([float32(winX), float32(winY)]))
+    result = some(vec2(winX, winY))
 
 proc lockMouse*(window: NativeWindow, lock: bool) =
   if lock:
--- a/semicongine/rendering/platform/windows.nim	Wed Aug 07 17:26:25 2024 +0700
+++ b/semicongine/rendering/platform/windows.nim	Wed Aug 07 19:09:03 2024 +0700
@@ -186,7 +186,7 @@
 
 proc size*(window: NativeWindow): (int, int) =
   var rect: RECT
-  checkWin32Result GetClientRect(window.hwnd, addr(rect))
+  checkWin32Result GetWindowRect(window.hwnd, addr(rect))
   (int(rect.right - rect.left), int(rect.bottom - rect.top))
 
 proc pendingEvents*(window: NativeWindow): seq[Event] =
@@ -199,19 +199,15 @@
   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[Vec2i] =
+  var p: POINT
+  let res = GetCursorPos(addr(p))
+  if res:
+    return some(vec2i(p.x, p.y))
+  return none(Vec2i)
 
-proc getMousePosition*(window: NativeWindow): Option[Vec2f] =
-  var p: POINT
-  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
--- a/tests/test_gltf.nim	Wed Aug 07 17:26:25 2024 +0700
+++ b/tests/test_gltf.nim	Wed Aug 07 19:09:03 2024 +0700
@@ -206,7 +206,7 @@
   var renderpass = createDirectPresentationRenderPass(depthBuffer = true, samples = VK_SAMPLE_COUNT_4_BIT)
   setupSwapchain(renderpass = renderpass)
   lockMouse(true)
-  showSystemCursor(false)
+  # showSystemCursor(false)
 
   # tests a simple triangle with minimalistic shader and vertex format
   test_gltf(time)