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,60 +43,53 @@ 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, self.manual_mouse_lock=false;
}; match self.window.set_cursor_position(self.get_middle_of_screen()){
match logical_key{ Ok(())=>(),
winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab)=>{ Err(e)=>println!("Could not set cursor position: {:?}",e),
if s{ }
self.manual_mouse_lock=false; match self.window.set_cursor_grab(winit::window::CursorGrabMode::None){
match self.window.set_cursor_position(self.get_middle_of_screen()){ Ok(())=>(),
Ok(())=>(), Err(e)=>println!("Could not release cursor: {:?}",e),
Err(e)=>println!("Could not set cursor position: {:?}",e), }
} self.window.set_cursor_visible(state.is_pressed());
match self.window.set_cursor_grab(winit::window::CursorGrabMode::None){ },
Ok(())=>(), (winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab),winit::event::ElementState::Released)=>{
Err(e)=>println!("Could not release cursor: {:?}",e), //if cursor is outside window don't lock but apparently there's no get pos function
} //let pos=window.get_cursor_pos();
}else{ match self.window.set_cursor_grab(winit::window::CursorGrabMode::Locked){
//if cursor is outside window don't lock but apparently there's no get pos function Ok(())=>(),
//let pos=window.get_cursor_pos(); Err(_)=>{
match self.window.set_cursor_grab(winit::window::CursorGrabMode::Locked){ match self.window.set_cursor_grab(winit::window::CursorGrabMode::Confined){
Ok(())=>(), Ok(())=>(),
Err(_)=>{ Err(e)=>{
match self.window.set_cursor_grab(winit::window::CursorGrabMode::Confined){ self.manual_mouse_lock=true;
Ok(())=>(), println!("Could not confine cursor: {:?}",e)
Err(e)=>{ },
self.manual_mouse_lock=true;
println!("Could not confine cursor: {:?}",e)
},
}
} }
} }
} }
self.window.set_cursor_visible(s); self.window.set_cursor_visible(state.is_pressed());
}, },
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(){