Files
openworm/src/bin/client/app.rs
2025-11-18 02:38:23 -05:00

68 lines
1.7 KiB
Rust

use std::sync::Arc;
use winit::{
application::ApplicationHandler,
event::WindowEvent,
event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy},
window::{Window, WindowId},
};
use crate::ClientEvent;
use super::Client;
#[derive(Clone)]
pub struct AppHandle {
pub proxy: EventLoopProxy<ClientEvent>,
pub window: Arc<Window>,
}
pub struct App {
client: Option<Client>,
proxy: EventLoopProxy<ClientEvent>,
}
impl App {
pub fn run() {
let event_loop = EventLoop::with_user_event().build().unwrap();
let proxy = event_loop.create_proxy();
event_loop
.run_app(&mut App {
client: Default::default(),
proxy,
})
.unwrap();
}
}
impl ApplicationHandler<ClientEvent> for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
if self.client.is_none() {
let client = Client::new(event_loop, self.proxy.clone());
self.client = Some(client);
}
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
let client = self.client.as_mut().unwrap();
client.window_event(event, event_loop);
}
fn user_event(&mut self, event_loop: &ActiveEventLoop, event: ClientEvent) {
let client = self.client.as_mut().unwrap();
client.event(event, event_loop);
}
fn exiting(&mut self, _: &ActiveEventLoop) {
let client = self.client.as_mut().unwrap();
client.exit();
}
}
impl AppHandle {
pub fn send(&self, event: ClientEvent) {
self.proxy.send_event(event).unwrap_or_else(|_| panic!());
self.window.request_redraw();
}
}