From dd0b8fd1f0ae20a520ebca300b7deb3befab629b Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 23 Oct 2023 16:32:26 -0700 Subject: [PATCH] update code for winit 0.29.2 --- src/framework.rs | 147 ++++++++++++++++++++++------------------------- src/main.rs | 36 ++++++------ 2 files changed, 87 insertions(+), 96 deletions(-) diff --git a/src/framework.rs b/src/framework.rs index f7acef1..25a9d35 100644 --- a/src/framework.rs +++ b/src/framework.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use web_sys::{ImageBitmapRenderingContext, OffscreenCanvas}; use winit::{ event::{self, WindowEvent, DeviceEvent}, - event_loop::{ControlFlow, EventLoop}, + event_loop::EventLoop, }; #[allow(dead_code)] @@ -88,7 +88,7 @@ async fn setup(title: &str) -> Setup { env_logger::init(); }; - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let mut builder = winit::window::WindowBuilder::new(); builder = builder.with_title(title); #[cfg(windows_OFF)] // TODO @@ -302,15 +302,15 @@ fn start( let mut example = E::init(&config, &adapter, &device, &queue); 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 - *control_flow = if cfg!(feature = "metal-auto-capture") { - ControlFlow::Exit - } else { - ControlFlow::Poll - }; + // *control_flow = if cfg!(feature = "metal-auto-capture") { + // ControlFlow::Exit + // } else { + // ControlFlow::Poll + // }; match event { - event::Event::RedrawEventsCleared => { + event::Event::AboutToWait => { #[cfg(not(target_arch = "wasm32"))] spawner.run_until_stalled(); @@ -318,48 +318,44 @@ fn start( } event::Event::WindowEvent { event: - WindowEvent::Resized(size) - | WindowEvent::ScaleFactorChanged { - new_inner_size: &mut size, - .. - }, + // WindowEvent::Resized(size) + // | WindowEvent::ScaleFactorChanged { + // 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. - // https://github.com/rust-windowing/winit/issues/2876 - let max_dimension = adapter.limits().max_texture_dimension_2d; - if size.width > max_dimension || size.height > max_dimension { - log::warn!( - "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); - } + 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 { WindowEvent::KeyboardInput { - input: - event::KeyboardInput { - virtual_keycode: Some(event::VirtualKeyCode::Escape), + event: + event::KeyEvent { + logical_key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::Escape), state: event::ElementState::Pressed, .. }, .. } | WindowEvent::CloseRequested => { - *control_flow = ControlFlow::Exit; + elwt.exit(); } #[cfg(not(target_arch = "wasm32"))] WindowEvent::KeyboardInput { - input: - event::KeyboardInput { - virtual_keycode: Some(event::VirtualKeyCode::Scroll), + event: + event::KeyEvent { + logical_key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::ScrollLock), state: event::ElementState::Pressed, .. }, @@ -367,54 +363,47 @@ fn start( } => { 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); } }, - 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"))] diff --git a/src/main.rs b/src/main.rs index fc2f321..19a4240 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1098,15 +1098,15 @@ impl framework::Example for GlobalState { //recalculate pressed keys on focus }, winit::event::WindowEvent::KeyboardInput { - input:winit::event::KeyboardInput{state, virtual_keycode,..}, + event:winit::event::KeyEvent{state, logical_key,..}, .. }=>{ let s=match state { winit::event::ElementState::Pressed => true, winit::event::ElementState::Released => false, }; - match virtual_keycode{ - Some(winit::event::VirtualKeyCode::Tab)=>{ + match logical_key{ + winit::keyboard::Key::Named(winit::keyboard::NamedKey::Tab)=>{ if s{ 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)){ @@ -1135,7 +1135,7 @@ impl framework::Example for GlobalState { } window.set_cursor_visible(s); }, - Some(winit::event::VirtualKeyCode::F11)=>{ + winit::keyboard::Key::Named(winit::keyboard::NamedKey::F11)=>{ if s{ if window.fullscreen().is_some(){ 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{ self.manual_mouse_lock=false; match window.set_cursor_grab(winit::window::CursorGrabMode::None){ @@ -1154,18 +1154,21 @@ impl framework::Example for GlobalState { window.set_cursor_visible(true); } }, - Some(keycode)=>{ + keycode=>{ if let Some(input_instruction)=match keycode { - winit::event::VirtualKeyCode::W => Some(InputInstruction::MoveForward(s)), - winit::event::VirtualKeyCode::A => Some(InputInstruction::MoveLeft(s)), - winit::event::VirtualKeyCode::S => Some(InputInstruction::MoveBack(s)), - winit::event::VirtualKeyCode::D => Some(InputInstruction::MoveRight(s)), - winit::event::VirtualKeyCode::E => Some(InputInstruction::MoveUp(s)), - winit::event::VirtualKeyCode::Q => Some(InputInstruction::MoveDown(s)), - winit::event::VirtualKeyCode::Space => Some(InputInstruction::Jump(s)), - winit::event::VirtualKeyCode::Z => Some(InputInstruction::Zoom(s)), - winit::event::VirtualKeyCode::R => if s{Some(InputInstruction::Reset)}else{None}, - _ => None, + winit::keyboard::Key::Named(winit::keyboard::NamedKey::Space)=>Some(InputInstruction::Jump(s)), + winit::keyboard::Key::Character(c)=>match c.as_str(){ + "w"=>Some(InputInstruction::MoveForward(s)), + "a"=>Some(InputInstruction::MoveLeft(s)), + "s"=>Some(InputInstruction::MoveBack(s)), + "d"=>Some(InputInstruction::MoveRight(s)), + "e"=>Some(InputInstruction::MoveUp(s)), + "q"=>Some(InputInstruction::MoveDown(s)), + "z"=>Some(InputInstruction::Zoom(s)), + "r"=>if s{Some(InputInstruction::Reset)}else{None}, + _=>None, + } + _=>None, }{ self.physics_thread.send(TimedInstruction{ time, @@ -1173,7 +1176,6 @@ impl framework::Example for GlobalState { }).unwrap(); } }, - _=>(), } }, _=>(),