diff semiconginev2/input.nim @ 1256:bfb75c934f4e

add: window focus handling, improve window api a bit
author sam <sam@basx.dev>
date Sun, 28 Jul 2024 17:33:41 +0700
parents b0f4c8ccd49a
children
line wrap: on
line diff
--- a/semiconginev2/input.nim	Sun Jul 28 00:17:34 2024 +0700
+++ b/semiconginev2/input.nim	Sun Jul 28 17:33:41 2024 +0700
@@ -12,9 +12,10 @@
     windowWasResized: bool = true
     windowIsMinimized: bool = false
     lockMouse: bool = false
+    hasFocus: bool = false
 
 # warning, shit is not thread safe
-var input: Input
+var input = Input()
 
 proc UpdateInputs*(): bool =
   # reset input states
@@ -26,7 +27,7 @@
   input.mouseMove = NewVec2f()
   input.windowWasResized = false
 
-  if input.lockMouse:
+  if input.lockMouse and input.hasFocus:
     SetMousePosition(vulkan.window, x=int(vulkan.swapchain.width div 2), y=int(vulkan.swapchain.height div 2))
 
   var killed = false
@@ -58,6 +59,10 @@
         input.windowIsMinimized = true
       of RestoredWindow:
         input.windowIsMinimized = false
+      of GotFocus:
+        input.hasFocus = true
+      of LostFocus:
+        input.hasFocus = false
 
   return not killed
 
@@ -81,6 +86,7 @@
 proc WindowWasResized*(): auto = input.windowWasResized
 proc WindowIsMinimized*(): auto = input.windowIsMinimized
 proc LockMouse*(value: bool) = input.lockMouse = value
+proc HasFocus*(): bool = input.hasFocus
 
 
 # actions as a slight abstraction over raw input