[oden] now I can close the window again

This commit is contained in:
John Doty 2023-07-08 07:08:06 -07:00
parent 29b1a854c5
commit 12cc715873

View file

@ -4,7 +4,12 @@ use std::sync::mpsc::Receiver;
use std::time::Instant; use std::time::Instant;
use tracy_client::{frame_mark, set_thread_name, span}; use tracy_client::{frame_mark, set_thread_name, span};
use wgpu::util::DeviceExt; use wgpu::util::DeviceExt;
use winit::{event::*, event_loop::EventLoop, window::Window, window::WindowBuilder}; use winit::{
event::*,
event_loop::{EventLoopBuilder, EventLoopProxy},
window::Window,
window::WindowBuilder,
};
mod script; mod script;
use script::graphics::GraphicsCommand; use script::graphics::GraphicsCommand;
@ -472,15 +477,19 @@ impl State {
} }
} }
enum OdenEvent {
Close,
}
struct UIEvent { struct UIEvent {
winit: Event<'static, ()>, winit: Event<'static, OdenEvent>,
#[allow(unused)] #[allow(unused)]
time: Instant, time: Instant,
} }
// TODO: flume? (https://docs.rs/flume/latest/flume/) // TODO: flume? (https://docs.rs/flume/latest/flume/)
fn main_thread(state: State, reciever: Receiver<UIEvent>) { fn main_thread(event_loop: EventLoopProxy<OdenEvent>, state: State, reciever: Receiver<UIEvent>) {
let mut state = state; let mut state = state;
let mut script = script::ScriptContext::new(); let mut script = script::ScriptContext::new();
script.init(); script.init();
@ -500,6 +509,10 @@ fn main_thread(state: State, reciever: Receiver<UIEvent>) {
} if window_id == state.window().id() => { } if window_id == state.window().id() => {
if !script.input(event) { if !script.input(event) {
match event { match event {
WindowEvent::CloseRequested => {
let _ = event_loop.send_event(OdenEvent::Close);
}
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
state.mouse_x = position.x; state.mouse_x = position.x;
state.mouse_y = position.y; state.mouse_y = position.y;
@ -557,20 +570,26 @@ pub async fn run() {
set_thread_name!("ui thread"); set_thread_name!("ui thread");
env_logger::init(); env_logger::init();
let event_loop = EventLoop::new(); let event_loop = EventLoopBuilder::<OdenEvent>::with_user_event().build();
let window = WindowBuilder::new().build(&event_loop).unwrap(); let window = WindowBuilder::new().build(&event_loop).unwrap();
let event_loop_proxy = event_loop.create_proxy();
let state = State::new(window).await; let state = State::new(window).await;
let (sender, reciever) = std::sync::mpsc::channel(); let (sender, reciever) = std::sync::mpsc::channel();
std::thread::spawn(move || { std::thread::spawn(move || {
set_thread_name!("game thread"); set_thread_name!("game thread");
main_thread(state, reciever); main_thread(event_loop_proxy, state, reciever);
}); });
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
control_flow.set_wait(); control_flow.set_wait();
match event {
Event::UserEvent(OdenEvent::Close) => {
control_flow.set_exit();
}
_ => {
if let Some(e) = event.to_static() { if let Some(e) = event.to_static() {
sender sender
.send(UIEvent { .send(UIEvent {
@ -579,5 +598,7 @@ pub async fn run() {
}) })
.unwrap(); .unwrap();
} }
}
}
}); });
} }