update code for winit 0.29.2

This commit is contained in:
Quaternions 2023-10-23 16:32:26 -07:00
parent 5493350023
commit dd0b8fd1f0
2 changed files with 87 additions and 96 deletions

View File

@ -5,7 +5,7 @@ use std::str::FromStr;
use web_sys::{ImageBitmapRenderingContext, OffscreenCanvas}; use web_sys::{ImageBitmapRenderingContext, OffscreenCanvas};
use winit::{ use winit::{
event::{self, WindowEvent, DeviceEvent}, event::{self, WindowEvent, DeviceEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::EventLoop,
}; };
#[allow(dead_code)] #[allow(dead_code)]
@ -88,7 +88,7 @@ async fn setup<E: Example>(title: &str) -> Setup {
env_logger::init(); env_logger::init();
}; };
let event_loop = EventLoop::new(); let event_loop = EventLoop::new().unwrap();
let mut builder = winit::window::WindowBuilder::new(); let mut builder = winit::window::WindowBuilder::new();
builder = builder.with_title(title); builder = builder.with_title(title);
#[cfg(windows_OFF)] // TODO #[cfg(windows_OFF)] // TODO
@ -302,15 +302,15 @@ fn start<E: Example>(
let mut example = E::init(&config, &adapter, &device, &queue); let mut example = E::init(&config, &adapter, &device, &queue);
log::info!("Entering render loop..."); log::info!("Entering render loop...");
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, elwt| {
let _ = (&instance, &adapter); // force ownership by the closure let _ = (&instance, &adapter); // force ownership by the closure
*control_flow = if cfg!(feature = "metal-auto-capture") { // *control_flow = if cfg!(feature = "metal-auto-capture") {
ControlFlow::Exit // ControlFlow::Exit
} else { // } else {
ControlFlow::Poll // ControlFlow::Poll
}; // };
match event { match event {
event::Event::RedrawEventsCleared => { event::Event::AboutToWait => {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
spawner.run_until_stalled(); spawner.run_until_stalled();
@ -318,48 +318,44 @@ fn start<E: Example>(
} }
event::Event::WindowEvent { event::Event::WindowEvent {
event: event:
WindowEvent::Resized(size) // WindowEvent::Resized(size)
| WindowEvent::ScaleFactorChanged { // | WindowEvent::ScaleFactorChanged {
new_inner_size: &mut size, // new_inner_size: &mut size,
.. // ..
}, // },
WindowEvent::Resized(size),//ignoring scale factor changed for now because mutex bruh
.. ..
} => { } => {
// Once winit is fixed, the detection conditions here can be removed. log::info!("Resizing to {:?}", size);
// https://github.com/rust-windowing/winit/issues/2876 config.width = size.width.max(1);
let max_dimension = adapter.limits().max_texture_dimension_2d; config.height = size.height.max(1);
if size.width > max_dimension || size.height > max_dimension { example.resize(&config, &device, &queue);
log::warn!( surface.configure(&device, &config);
"The resizing size {:?} exceeds the limit of {}.",
size,
max_dimension
);
} else {
log::info!("Resizing to {:?}", size);
config.width = size.width.max(1);
config.height = size.height.max(1);
example.resize(&config, &device, &queue);
surface.configure(&device, &config);
}
} }
event::Event::DeviceEvent {
event,
..
} => {
example.device_event(&window,event);
},
event::Event::WindowEvent { event, .. } => match event { event::Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
input: event:
event::KeyboardInput { event::KeyEvent {
virtual_keycode: Some(event::VirtualKeyCode::Escape), logical_key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::Escape),
state: event::ElementState::Pressed, state: event::ElementState::Pressed,
.. ..
}, },
.. ..
} }
| WindowEvent::CloseRequested => { | WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit; elwt.exit();
} }
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
WindowEvent::KeyboardInput { WindowEvent::KeyboardInput {
input: event:
event::KeyboardInput { event::KeyEvent {
virtual_keycode: Some(event::VirtualKeyCode::Scroll), logical_key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::ScrollLock),
state: event::ElementState::Pressed, state: event::ElementState::Pressed,
.. ..
}, },
@ -367,54 +363,47 @@ fn start<E: Example>(
} => { } => {
println!("{:#?}", instance.generate_report()); println!("{:#?}", instance.generate_report());
} }
WindowEvent::RedrawRequested => {
let frame = match surface.get_current_texture() {
Ok(frame) => frame,
Err(_) => {
surface.configure(&device, &config);
surface
.get_current_texture()
.expect("Failed to acquire next surface texture!")
}
};
let view = frame.texture.create_view(&wgpu::TextureViewDescriptor {
format: Some(surface_view_format),
..wgpu::TextureViewDescriptor::default()
});
example.render(&view, &device, &queue, &spawner);
frame.present();
#[cfg(target_arch = "wasm32")]
{
if let Some(offscreen_canvas_setup) = &offscreen_canvas_setup {
let image_bitmap = offscreen_canvas_setup
.offscreen_canvas
.transfer_to_image_bitmap()
.expect("couldn't transfer offscreen canvas to image bitmap.");
offscreen_canvas_setup
.bitmap_renderer
.transfer_from_image_bitmap(&image_bitmap);
log::info!("Transferring OffscreenCanvas to ImageBitmapRenderer");
}
}
}
_ => { _ => {
example.update(&window,&device,&queue,event); example.update(&window,&device,&queue,event);
} }
}, },
event::Event::DeviceEvent {
event,
..
} => {
example.device_event(&window,event);
},
event::Event::RedrawRequested(_) => {
let frame = match surface.get_current_texture() {
Ok(frame) => frame,
Err(_) => {
surface.configure(&device, &config);
surface
.get_current_texture()
.expect("Failed to acquire next surface texture!")
}
};
let view = frame.texture.create_view(&wgpu::TextureViewDescriptor {
format: Some(surface_view_format),
..wgpu::TextureViewDescriptor::default()
});
example.render(&view, &device, &queue, &spawner);
frame.present();
#[cfg(target_arch = "wasm32")]
{
if let Some(offscreen_canvas_setup) = &offscreen_canvas_setup {
let image_bitmap = offscreen_canvas_setup
.offscreen_canvas
.transfer_to_image_bitmap()
.expect("couldn't transfer offscreen canvas to image bitmap.");
offscreen_canvas_setup
.bitmap_renderer
.transfer_from_image_bitmap(&image_bitmap);
log::info!("Transferring OffscreenCanvas to ImageBitmapRenderer");
}
}
}
_ => {} _ => {}
} }
}); }).unwrap();
} }
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]

View File

@ -1098,15 +1098,15 @@ impl framework::Example for GlobalState {
//recalculate pressed keys on focus //recalculate pressed keys on focus
}, },
winit::event::WindowEvent::KeyboardInput { winit::event::WindowEvent::KeyboardInput {
input:winit::event::KeyboardInput{state, virtual_keycode,..}, event:winit::event::KeyEvent{state, logical_key,..},
.. ..
}=>{ }=>{
let s=match state { let s=match state {
winit::event::ElementState::Pressed => true, winit::event::ElementState::Pressed => true,
winit::event::ElementState::Released => false, winit::event::ElementState::Released => false,
}; };
match virtual_keycode{ match logical_key{
Some(winit::event::VirtualKeyCode::Tab)=>{ winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab)=>{
if s{ if s{
self.manual_mouse_lock=false; self.manual_mouse_lock=false;
match window.set_cursor_position(winit::dpi::PhysicalPosition::new(self.graphics.camera.screen_size.x as f32/2.0, self.graphics.camera.screen_size.y as f32/2.0)){ match window.set_cursor_position(winit::dpi::PhysicalPosition::new(self.graphics.camera.screen_size.x as f32/2.0, self.graphics.camera.screen_size.y as f32/2.0)){
@ -1135,7 +1135,7 @@ impl framework::Example for GlobalState {
} }
window.set_cursor_visible(s); window.set_cursor_visible(s);
}, },
Some(winit::event::VirtualKeyCode::F11)=>{ winit::keyboard::Key::Named(winit::keyboard::NamedKey::F11)=>{
if s{ if s{
if window.fullscreen().is_some(){ if window.fullscreen().is_some(){
window.set_fullscreen(None); window.set_fullscreen(None);
@ -1144,7 +1144,7 @@ impl framework::Example for GlobalState {
} }
} }
}, },
Some(winit::event::VirtualKeyCode::Escape)=>{ winit::keyboard::Key::Named(winit::keyboard::NamedKey::Escape)=>{
if s{ if s{
self.manual_mouse_lock=false; self.manual_mouse_lock=false;
match window.set_cursor_grab(winit::window::CursorGrabMode::None){ match window.set_cursor_grab(winit::window::CursorGrabMode::None){
@ -1154,18 +1154,21 @@ impl framework::Example for GlobalState {
window.set_cursor_visible(true); window.set_cursor_visible(true);
} }
}, },
Some(keycode)=>{ keycode=>{
if let Some(input_instruction)=match keycode { if let Some(input_instruction)=match keycode {
winit::event::VirtualKeyCode::W => Some(InputInstruction::MoveForward(s)), winit::keyboard::Key::Named(winit::keyboard::NamedKey::Space)=>Some(InputInstruction::Jump(s)),
winit::event::VirtualKeyCode::A => Some(InputInstruction::MoveLeft(s)), winit::keyboard::Key::Character(c)=>match c.as_str(){
winit::event::VirtualKeyCode::S => Some(InputInstruction::MoveBack(s)), "w"=>Some(InputInstruction::MoveForward(s)),
winit::event::VirtualKeyCode::D => Some(InputInstruction::MoveRight(s)), "a"=>Some(InputInstruction::MoveLeft(s)),
winit::event::VirtualKeyCode::E => Some(InputInstruction::MoveUp(s)), "s"=>Some(InputInstruction::MoveBack(s)),
winit::event::VirtualKeyCode::Q => Some(InputInstruction::MoveDown(s)), "d"=>Some(InputInstruction::MoveRight(s)),
winit::event::VirtualKeyCode::Space => Some(InputInstruction::Jump(s)), "e"=>Some(InputInstruction::MoveUp(s)),
winit::event::VirtualKeyCode::Z => Some(InputInstruction::Zoom(s)), "q"=>Some(InputInstruction::MoveDown(s)),
winit::event::VirtualKeyCode::R => if s{Some(InputInstruction::Reset)}else{None}, "z"=>Some(InputInstruction::Zoom(s)),
_ => None, "r"=>if s{Some(InputInstruction::Reset)}else{None},
_=>None,
}
_=>None,
}{ }{
self.physics_thread.send(TimedInstruction{ self.physics_thread.send(TimedInstruction{
time, time,
@ -1173,7 +1176,6 @@ impl framework::Example for GlobalState {
}).unwrap(); }).unwrap();
} }
}, },
_=>(),
} }
}, },
_=>(), _=>(),