fix selecting wrong window for key events
This commit is contained in:
parent
709247869d
commit
0333e1f676
5 changed files with 119 additions and 15 deletions
|
@ -5,7 +5,7 @@ use std::time::Instant;
|
|||
use cgmath::{Matrix4, Point3, Rad, Vector3, perspective};
|
||||
use futures::executor::block_on;
|
||||
use thiserror::Error;
|
||||
use wgpu::util::DeviceExt;
|
||||
use wgpu::{Backends, InstanceDescriptor, util::DeviceExt};
|
||||
use winit::window::Window;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
|
@ -240,7 +240,10 @@ pub struct WgpuCtx<'window> {
|
|||
|
||||
impl<'window> WgpuCtx<'window> {
|
||||
pub async fn new(window: Arc<Window>) -> Result<WgpuCtx<'window>, ContextError> {
|
||||
let instance = wgpu::Instance::default();
|
||||
let instance = wgpu::Instance::new(&InstanceDescriptor {
|
||||
backends: Backends::from_comma_list("dx12,metal,opengl,webgpu"),
|
||||
..Default::default()
|
||||
});
|
||||
let surface = instance.create_surface(Arc::clone(&window))?;
|
||||
let adapter = instance
|
||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||
|
@ -255,8 +258,7 @@ impl<'window> WgpuCtx<'window> {
|
|||
&wgpu::DeviceDescriptor {
|
||||
label: None,
|
||||
required_features: wgpu::Features::empty(),
|
||||
required_limits: wgpu::Limits::downlevel_webgl2_defaults()
|
||||
.using_resolution(adapter.limits()),
|
||||
required_limits: wgpu::Limits::default().using_resolution(adapter.limits()),
|
||||
memory_hints: wgpu::MemoryHints::Performance,
|
||||
},
|
||||
None,
|
||||
|
|
|
@ -31,7 +31,13 @@ impl ApplicationHandler for App<'_> {
|
|||
);
|
||||
let window_id = window.id();
|
||||
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
|
||||
self.windows.insert(window_id, WindowContext { window, ctx: wgpu_ctx });
|
||||
self.windows.insert(
|
||||
window_id,
|
||||
WindowContext {
|
||||
window,
|
||||
ctx: wgpu_ctx,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,7 +51,7 @@ impl ApplicationHandler for App<'_> {
|
|||
WindowEvent::CloseRequested => {
|
||||
if let Some(window_context) = self.windows.remove(&window_id) {
|
||||
drop(window_context);
|
||||
println!("Window: {:?} closed, exiting", window_id);
|
||||
debug!("Window: {:?} closed, exiting", window_id);
|
||||
}
|
||||
if self.windows.is_empty() {
|
||||
event_loop.exit();
|
||||
|
@ -63,10 +69,14 @@ impl ApplicationHandler for App<'_> {
|
|||
match code {
|
||||
winit::keyboard::KeyCode::Space => {
|
||||
debug!("Space key pressed");
|
||||
if let Some(window_context) = self.windows.values_mut().next() {
|
||||
if let Some(window_context) = self.windows.get_mut(&window_id){
|
||||
match window_context.ctx.bg_color() {
|
||||
wgpu::Color::WHITE => window_context.ctx.change_bg_color(wgpu::Color::BLACK),
|
||||
wgpu::Color::BLACK => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
|
||||
wgpu::Color::WHITE => {
|
||||
window_context.ctx.change_bg_color(wgpu::Color::BLACK)
|
||||
}
|
||||
wgpu::Color::BLACK => {
|
||||
window_context.ctx.change_bg_color(wgpu::Color::WHITE)
|
||||
}
|
||||
_ => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
|
||||
}
|
||||
}
|
||||
|
@ -82,7 +92,13 @@ impl ApplicationHandler for App<'_> {
|
|||
);
|
||||
let window_id = new_window.id();
|
||||
let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap();
|
||||
self.windows.insert(window_id, WindowContext { window: new_window, ctx: wgpu_ctx });
|
||||
self.windows.insert(
|
||||
window_id,
|
||||
WindowContext {
|
||||
window: new_window,
|
||||
ctx: wgpu_ctx,
|
||||
},
|
||||
);
|
||||
}
|
||||
_ => info!("Unimplemented keycode: {:?}", code),
|
||||
}
|
||||
|
@ -99,8 +115,7 @@ impl ApplicationHandler for App<'_> {
|
|||
if let Some(window_context) = self.windows.get_mut(&window_id) {
|
||||
window_context.ctx.resize(size.into());
|
||||
window_context.window.request_redraw();
|
||||
let size_str: String =
|
||||
size.height.to_string() + "x" + &size.width.to_string();
|
||||
let size_str: String = size.height.to_string() + "x" + &size.width.to_string();
|
||||
debug!("Window resized to {:?}", size_str);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue