1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
use crate::state::State;
use log::{info, warn};
use std::net::TcpStream;
use winit::{
application::ApplicationHandler,
event::WindowEvent,
event_loop::ActiveEventLoop,
window::{Window, WindowAttributes, WindowId},
};
pub struct WindowState {
init: Option<TcpStream>,
window: Option<(Window, State<'static>)>,
}
impl WindowState {
pub fn new(init: TcpStream) -> Self {
Self {
window: None,
init: Some(init),
}
}
}
impl ApplicationHandler for WindowState {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
info!("app resumed");
let win = event_loop
.create_window(WindowAttributes::default().with_maximized(true))
.unwrap();
let sta = State::new(self.init.take().unwrap(), unsafe {
std::mem::transmute::<&Window, &'static Window>(&win)
})
.unwrap();
self.window = Some((win, sta))
}
fn window_event(
&mut self,
event_loop: &ActiveEventLoop,
_window_id: WindowId,
event: WindowEvent,
) {
if let Some((_win, sta)) = &mut self.window {
match event {
WindowEvent::Resized(size) => {
sta.renderer.resize(size.width, size.height);
}
WindowEvent::RedrawRequested => sta.renderer.draw().unwrap(),
WindowEvent::CloseRequested => {
event_loop.exit();
}
_ => (),
}
}
}
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
if let Some((_win, sta)) = &mut self.window {
if let Err(e) = sta.update() {
warn!("update failed: {e}")
}
}
}
}
|