fix multi window support
This commit is contained in:
parent
b7da32fe1c
commit
c9fd635316
2 changed files with 94 additions and 43 deletions
64
.vscode/launch.json
vendored
Normal file
64
.vscode/launch.json
vendored
Normal file
|
@ -0,0 +1,64 @@
|
|||
{
|
||||
// Use IntelliSense to learn about possible attributes.
|
||||
// Hover to view descriptions of existing attributes.
|
||||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug executable 'zenyx'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"build",
|
||||
"--bin=zenyx",
|
||||
"--package=zenyx"
|
||||
],
|
||||
"filter": {
|
||||
"name": "zenyx",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in executable 'zenyx'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--bin=zenyx",
|
||||
"--package=zenyx"
|
||||
],
|
||||
"filter": {
|
||||
"name": "zenyx",
|
||||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
},
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Debug unit tests in library 'zen_core'",
|
||||
"cargo": {
|
||||
"args": [
|
||||
"test",
|
||||
"--no-run",
|
||||
"--lib",
|
||||
"--package=zen_core"
|
||||
],
|
||||
"filter": {
|
||||
"name": "zen_core",
|
||||
"kind": "lib"
|
||||
}
|
||||
},
|
||||
"args": [],
|
||||
"cwd": "${workspaceFolder}"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -10,10 +10,14 @@ use winit::event_loop::{ActiveEventLoop, EventLoop};
|
|||
use winit::window::{Window, WindowId};
|
||||
pub mod ctx;
|
||||
|
||||
struct WindowContext<'window> {
|
||||
window: Arc<Window>,
|
||||
ctx: WgpuCtx<'window>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct App<'window> {
|
||||
windows: HashMap<WindowId, Arc<Window>>,
|
||||
ctx: Option<WgpuCtx<'window>>,
|
||||
windows: HashMap<WindowId, WindowContext<'window>>,
|
||||
}
|
||||
|
||||
impl ApplicationHandler for App<'_> {
|
||||
|
@ -26,12 +30,11 @@ impl ApplicationHandler for App<'_> {
|
|||
.expect("create window err."),
|
||||
);
|
||||
let window_id = window.id();
|
||||
self.windows.insert(window_id, window.clone());
|
||||
let wgpu_ctx = WgpuCtx::new_blocking(window.clone()).unwrap();
|
||||
self.ctx = Some(wgpu_ctx)
|
||||
self.windows.insert(window_id, WindowContext { window, ctx: wgpu_ctx });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop: &ActiveEventLoop,
|
||||
|
@ -40,17 +43,13 @@ impl ApplicationHandler for App<'_> {
|
|||
) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
if let Some(window) = self.windows.remove(&window_id) {
|
||||
let mut window = Arc::into_inner(window);
|
||||
window = None;
|
||||
// _ = window;
|
||||
drop(window);
|
||||
event_loop.exit();
|
||||
if let Some(window_context) = self.windows.remove(&window_id) {
|
||||
drop(window_context);
|
||||
println!("Window: {:?} closed, exiting", window_id);
|
||||
}
|
||||
// if self.windows.is_empty() {
|
||||
// event_loop.exit();
|
||||
// }
|
||||
if self.windows.is_empty() {
|
||||
event_loop.exit();
|
||||
}
|
||||
}
|
||||
WindowEvent::KeyboardInput {
|
||||
device_id,
|
||||
|
@ -64,27 +63,26 @@ impl ApplicationHandler for App<'_> {
|
|||
match code {
|
||||
winit::keyboard::KeyCode::Space => {
|
||||
debug!("Space key pressed");
|
||||
if let Some(ctx) = &mut self.ctx {
|
||||
match ctx.bg_color() {
|
||||
wgpu::Color::WHITE => ctx.change_bg_color(wgpu::Color::BLACK),
|
||||
wgpu::Color::BLACK => ctx.change_bg_color(wgpu::Color::WHITE),
|
||||
_ => ctx.change_bg_color(wgpu::Color::WHITE),
|
||||
if let Some(window_context) = self.windows.values_mut().next() {
|
||||
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),
|
||||
_ => window_context.ctx.change_bg_color(wgpu::Color::WHITE),
|
||||
}
|
||||
}
|
||||
}
|
||||
winit::keyboard::KeyCode::Escape => {
|
||||
debug!("Escape key pressed, spawning new window");
|
||||
let win_attr =
|
||||
Window::default_attributes().with_title(format!("Zenyx - New Window {}",self.windows.len()));
|
||||
let win_attr = Window::default_attributes()
|
||||
.with_title(format!("Zenyx - New Window {}", self.windows.len()));
|
||||
let new_window = Arc::new(
|
||||
event_loop
|
||||
.create_window(win_attr)
|
||||
.expect("create window err."),
|
||||
);
|
||||
let window_id = new_window.id();
|
||||
self.windows.insert(window_id, new_window.clone());
|
||||
let wgpu_ctx = WgpuCtx::new_blocking(new_window.clone()).unwrap();
|
||||
self.ctx = Some(wgpu_ctx);
|
||||
self.windows.insert(window_id, WindowContext { window: new_window, ctx: wgpu_ctx });
|
||||
}
|
||||
_ => info!("Unimplemented keycode: {:?}", code),
|
||||
}
|
||||
|
@ -92,29 +90,18 @@ impl ApplicationHandler for App<'_> {
|
|||
_ => {}
|
||||
},
|
||||
WindowEvent::RedrawRequested => {
|
||||
if let Some(ctx) = &mut self.ctx {
|
||||
ctx.draw();
|
||||
}
|
||||
match self.windows.get(&window_id) {
|
||||
Some(window) => {
|
||||
if let Some(ctx) = &mut self.ctx {
|
||||
ctx.draw();
|
||||
}
|
||||
window.request_redraw();
|
||||
}
|
||||
None => ()
|
||||
if let Some(window_context) = self.windows.get_mut(&window_id) {
|
||||
window_context.ctx.draw();
|
||||
window_context.window.request_redraw();
|
||||
}
|
||||
|
||||
}
|
||||
WindowEvent::Resized(size) => {
|
||||
if let Some(window) = self.windows.get(&window_id) {
|
||||
if let Some(wgpu_ctx) = &mut self.ctx {
|
||||
wgpu_ctx.resize(size.into());
|
||||
window.request_redraw();
|
||||
let size_str: String =
|
||||
size.height.to_string() + "x" + &size.width.to_string();
|
||||
debug!("Window resized to {:?}", size_str);
|
||||
}
|
||||
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();
|
||||
debug!("Window resized to {:?}", size_str);
|
||||
}
|
||||
}
|
||||
_ => trace!("Unhandled window event"),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue