summaryrefslogtreecommitdiff
path: root/client/src/window.rs
blob: 8b3ee9e1237acbc6875809d70573ff2593419df5 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::state::State;
use glam::Vec3;
use log::{info, warn};
use std::net::TcpStream;
use winit::{
    application::ApplicationHandler,
    event::{DeviceEvent, ElementState, WindowEvent},
    event_loop::ActiveEventLoop,
    keyboard::{KeyCode, PhysicalKey},
    window::{CursorGrabMode, 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.resize(size.width, size.height);
                }
                WindowEvent::RedrawRequested => {
                    sta.draw();
                    win.request_redraw();
                }
                WindowEvent::KeyboardInput { event, .. } => {
                    if event.repeat {
                        return;
                    }
                    if event.state == ElementState::Pressed {
                        match event.physical_key {
                            PhysicalKey::Code(KeyCode::Escape) => {
                                win.set_cursor_grab(CursorGrabMode::Locked).unwrap();
                            }
                            _ => (),
                        }
                    }
                    sta.delta.move_dir += match event.physical_key {
                        PhysicalKey::Code(KeyCode::KeyW) => Vec3::X,
                        PhysicalKey::Code(KeyCode::KeyS) => Vec3::NEG_X,
                        PhysicalKey::Code(KeyCode::KeyA) => Vec3::NEG_Z,
                        PhysicalKey::Code(KeyCode::KeyD) => Vec3::Z,
                        _ => Vec3::ZERO,
                    } * match event.state {
                        ElementState::Pressed => 1.,
                        ElementState::Released => -1.,
                    };
                }
                WindowEvent::CloseRequested => {
                    event_loop.exit();
                }
                _ => (),
            }
        }
    }
    fn device_event(
        &mut self,
        _event_loop: &ActiveEventLoop,
        _device_id: winit::event::DeviceId,
        event: winit::event::DeviceEvent,
    ) {
        if let Some((_win, sta)) = &mut self.window {
            match event {
                DeviceEvent::MouseMotion { delta } => {
                    sta.delta.mouse_acc.x += delta.0 as f32;
                    sta.delta.mouse_acc.y += delta.1 as f32;
                }
                _ => (),
            }
        }
    }
    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:#}")
            }
        }
    }
}

impl Drop for WindowState {
    fn drop(&mut self) {
        if let Some((win, sta)) = self.window.take() {
            drop(sta);
            drop(win)
        }
    }
}