From 95d16271de06a5c728d4346da8ec3067af3ba25b Mon Sep 17 00:00:00 2001
From: Quaternions <krakow20@gmail.com>
Date: Sun, 1 Oct 2023 15:54:10 -0700
Subject: [PATCH] add cursor grab

---
 src/framework.rs |  4 ++--
 src/main.rs      | 26 ++++++++++++++++++++++++--
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/src/framework.rs b/src/framework.rs
index 5ce606b3..5256e731 100644
--- a/src/framework.rs
+++ b/src/framework.rs
@@ -51,7 +51,7 @@ pub trait Example: 'static + Sized {
 		device: &wgpu::Device,
 		queue: &wgpu::Queue,
 	);
-	fn update(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, event: WindowEvent);
+	fn update(&mut self, window: &winit::window::Window, device: &wgpu::Device, queue: &wgpu::Queue, event: WindowEvent);
 	fn device_event(&mut self, event: DeviceEvent);
 	fn render(
 		&mut self,
@@ -367,7 +367,7 @@ fn start<E: Example>(
 					println!("{:#?}", instance.generate_report());
 				}
 				_ => {
-					example.update(&device,&queue,event);
+					example.update(&window,&device,&queue,event);
 				}
 			},
 			event::Event::DeviceEvent {
diff --git a/src/main.rs b/src/main.rs
index 42993c12..2c597507 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -775,8 +775,7 @@ impl framework::Example for GraphicsData {
 	}
 
 	#[allow(clippy::single_match)]
-	fn update(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, event: winit::event::WindowEvent) {
-		//nothing atm
+	fn update(&mut self, window: &winit::window::Window, device: &wgpu::Device, queue: &wgpu::Queue, event: winit::event::WindowEvent) {
 		match event {
 			winit::event::WindowEvent::DroppedFile(path) => {
 				println!("opening file: {:?}", &path);
@@ -838,6 +837,29 @@ impl framework::Example for GraphicsData {
 					println!("Could not open file");
 				}
 			},
+			winit::event::WindowEvent::KeyboardInput {
+				input:winit::event::KeyboardInput{state, virtual_keycode,..},
+				..
+			}=>{
+				let s=match state {
+					winit::event::ElementState::Pressed => true,
+					winit::event::ElementState::Released => false,
+				};
+				match virtual_keycode{
+					Some(winit::event::VirtualKeyCode::Tab)=>{
+						if s{
+							if let Ok(())=window.set_cursor_grab(winit::window::CursorGrabMode::None){
+								window.set_cursor_visible(true);
+							}
+						}else{
+							if let Ok(())=window.set_cursor_grab(winit::window::CursorGrabMode::Locked){
+								window.set_cursor_visible(false);
+							}
+						}
+					},
+					_=>(),
+				}
+			},
 			_=>(),
 		}
 	}