forked from nonsensical-dev/zenyx-engine
feat: add the ability to open and close new windows
This commit is contained in:
parent
7e82eba93e
commit
bb16554097
1 changed files with 32 additions and 2 deletions
34
src/main.rs
34
src/main.rs
|
@ -10,8 +10,10 @@ use wgpu::{
|
|||
PipelineCompilationOptions, Surface,
|
||||
};
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event_loop::EventLoop;
|
||||
use winit::event::{ElementState, MouseButton};
|
||||
use winit::event_loop::{ActiveEventLoop, EventLoop};
|
||||
use winit::window::{Window, WindowAttributes, WindowId};
|
||||
use zlog::LogLevel;
|
||||
use zlog::{config::LoggerConfig, query::LogQuery};
|
||||
struct WindowContext<'window> {
|
||||
window: Arc<Window>,
|
||||
|
@ -47,6 +49,21 @@ impl App<'_> {
|
|||
windows: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
pub fn spawn_window(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let attr = WindowAttributes::default()
|
||||
.with_title("Zenyx - SubWindow")
|
||||
.with_min_inner_size(winit::dpi::LogicalSize::new(1, 1));
|
||||
|
||||
let window = event_loop.create_window(attr).unwrap();
|
||||
let window = Arc::new(window);
|
||||
let renderer = self.state.create_renderer(window.clone());
|
||||
let window_ctx = WindowContext {
|
||||
renderer: smol::block_on(renderer),
|
||||
window: window.clone(),
|
||||
};
|
||||
let window_id = window.id();
|
||||
self.windows.insert(window_id, window_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
struct WgpuState {
|
||||
|
@ -282,7 +299,19 @@ impl<'window> ApplicationHandler for App<'window> {
|
|||
surface_texture.present()
|
||||
}
|
||||
winit::event::WindowEvent::CloseRequested => {
|
||||
event_loop.exit();
|
||||
let _ = self.windows.remove(&window_id);
|
||||
if self.windows.is_empty() {
|
||||
event_loop.exit();
|
||||
}
|
||||
}
|
||||
winit::event::WindowEvent::MouseInput {
|
||||
device_id,
|
||||
state,
|
||||
button,
|
||||
} => {
|
||||
if button == MouseButton::Left && state == ElementState::Pressed {
|
||||
self.spawn_window(event_loop);
|
||||
}
|
||||
}
|
||||
winit::event::WindowEvent::Resized(size) => {
|
||||
if let Some(window_ctx) = self.windows.get(&window_id) {
|
||||
|
@ -327,6 +356,7 @@ fn main() -> Result<(), terminator::Terminator> {
|
|||
.log_to_stdout(true)
|
||||
.file_include_time(true)
|
||||
.log_to_file(true)
|
||||
.level(LogLevel::Info)
|
||||
.log_path("zenyx.log");
|
||||
let _logger = zlog::Logger::new(config);
|
||||
let event_loop = EventLoop::new()?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue