[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 tracy_client::{frame_mark, set_thread_name, span};
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;
use script::graphics::GraphicsCommand;
@ -472,15 +477,19 @@ impl State {
}
}
enum OdenEvent {
Close,
}
struct UIEvent {
winit: Event<'static, ()>,
winit: Event<'static, OdenEvent>,
#[allow(unused)]
time: Instant,
}
// 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 script = script::ScriptContext::new();
script.init();
@ -500,6 +509,10 @@ fn main_thread(state: State, reciever: Receiver<UIEvent>) {
} if window_id == state.window().id() => {
if !script.input(event) {
match event {
WindowEvent::CloseRequested => {
let _ = event_loop.send_event(OdenEvent::Close);
}
WindowEvent::CursorMoved { position, .. } => {
state.mouse_x = position.x;
state.mouse_y = position.y;
@ -557,27 +570,35 @@ pub async fn run() {
set_thread_name!("ui thread");
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 event_loop_proxy = event_loop.create_proxy();
let state = State::new(window).await;
let (sender, reciever) = std::sync::mpsc::channel();
std::thread::spawn(move || {
set_thread_name!("game thread");
main_thread(state, reciever);
main_thread(event_loop_proxy, state, reciever);
});
event_loop.run(move |event, _, control_flow| {
control_flow.set_wait();
if let Some(e) = event.to_static() {
sender
.send(UIEvent {
winit: e,
time: Instant::now(),
})
.unwrap();
match event {
Event::UserEvent(OdenEvent::Close) => {
control_flow.set_exit();
}
_ => {
if let Some(e) = event.to_static() {
sender
.send(UIEvent {
winit: e,
time: Instant::now(),
})
.unwrap();
}
}
}
});
}