window: match on key and state

This commit is contained in:
Quaternions 2025-01-03 02:20:09 -08:00
parent f73f45128e
commit 0a515f423d

View File

@ -43,13 +43,8 @@ impl WindowContext<'_>{
event:winit::event::KeyEvent{state,logical_key,repeat:false,..}, event:winit::event::KeyEvent{state,logical_key,repeat:false,..},
.. ..
}=>{ }=>{
let s=match state{ match (logical_key,state){
winit::event::ElementState::Pressed=>true, (winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab),winit::event::ElementState::Pressed)=>{
winit::event::ElementState::Released=>false,
};
match logical_key{
winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab)=>{
if s{
self.manual_mouse_lock=false; self.manual_mouse_lock=false;
match self.window.set_cursor_position(self.get_middle_of_screen()){ match self.window.set_cursor_position(self.get_middle_of_screen()){
Ok(())=>(), Ok(())=>(),
@ -59,7 +54,9 @@ impl WindowContext<'_>{
Ok(())=>(), Ok(())=>(),
Err(e)=>println!("Could not release cursor: {:?}",e), Err(e)=>println!("Could not release cursor: {:?}",e),
} }
}else{ self.window.set_cursor_visible(state.is_pressed());
},
(winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab),winit::event::ElementState::Released)=>{
//if cursor is outside window don't lock but apparently there's no get pos function //if cursor is outside window don't lock but apparently there's no get pos function
//let pos=window.get_cursor_pos(); //let pos=window.get_cursor_pos();
match self.window.set_cursor_grab(winit::window::CursorGrabMode::Locked){ match self.window.set_cursor_grab(winit::window::CursorGrabMode::Locked){
@ -74,29 +71,25 @@ impl WindowContext<'_>{
} }
} }
} }
} self.window.set_cursor_visible(state.is_pressed());
self.window.set_cursor_visible(s);
}, },
winit::keyboard::Key::Named(winit::keyboard::NamedKey::F11)=>{ (winit::keyboard::Key::Named(winit::keyboard::NamedKey::F11),winit::event::ElementState::Pressed)=>{
if s{
if self.window.fullscreen().is_some(){ if self.window.fullscreen().is_some(){
self.window.set_fullscreen(None); self.window.set_fullscreen(None);
}else{ }else{
self.window.set_fullscreen(Some(winit::window::Fullscreen::Borderless(None))); self.window.set_fullscreen(Some(winit::window::Fullscreen::Borderless(None)));
} }
}
}, },
winit::keyboard::Key::Named(winit::keyboard::NamedKey::Escape)=>{ (winit::keyboard::Key::Named(winit::keyboard::NamedKey::Escape),winit::event::ElementState::Pressed)=>{
if s{
self.manual_mouse_lock=false; self.manual_mouse_lock=false;
match self.window.set_cursor_grab(winit::window::CursorGrabMode::None){ match self.window.set_cursor_grab(winit::window::CursorGrabMode::None){
Ok(())=>(), Ok(())=>(),
Err(e)=>println!("Could not release cursor: {:?}",e), Err(e)=>println!("Could not release cursor: {:?}",e),
} }
self.window.set_cursor_visible(true); self.window.set_cursor_visible(true);
}
}, },
keycode=>{ (keycode,state)=>{
let s=state.is_pressed();
if let Some(input_instruction)=match keycode{ if let Some(input_instruction)=match keycode{
winit::keyboard::Key::Named(winit::keyboard::NamedKey::Space)=>Some(InputInstruction::Jump(s)), winit::keyboard::Key::Named(winit::keyboard::NamedKey::Space)=>Some(InputInstruction::Jump(s)),
winit::keyboard::Key::Character(key)=>match key.as_str(){ winit::keyboard::Key::Character(key)=>match key.as_str(){