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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use self::builder::WindowConfig;
use crate::gl::Gl;
use crate::{Context, ContextBackend};
use input::Input;

#[cfg(feature = "glutin")]
pub use glutin;
#[cfg(feature = "winit")]
pub use winit;

mod builder;
mod event_loop;
pub(crate) mod input;

pub use builder::WindowBuilder;
pub use event_loop::EventLoop;
pub use input::cursor::{CursorPosition, DesktopOrigin, WindowCenter, WindowOrigin};

#[derive(Debug)]
pub struct Window {
    #[cfg(feature = "winit")]
    event_loop: EventLoop,
    #[cfg(feature = "winit")]
    pub(crate) window: winit::window::Window,
    #[cfg(feature = "winit")]
    input: Input,
    should_stop: bool,
}

impl Window {
    #[cfg(feature = "winit")]
    pub(crate) fn new(config: WindowConfig) -> Self {
        let event_loop = EventLoop::new();
        let window = winit::window::WindowBuilder::new()
            .with_title(config.title.unwrap_or_else(|| "ReverieEngine".to_owned()))
            .with_inner_size(winit::dpi::LogicalSize::new(
                config.width as f32,
                config.height as f32,
            ))
            .with_maximized(config.maximize)
            .build(&event_loop.event_loop)
            .unwrap();
        let input = Input::new();

        Self {
            event_loop,
            window,
            input,
            should_stop: false,
        }
    }

    #[cfg(not(feature = "winit"))]
    pub(crate) fn new() -> Self {
        Self { should_stop: false }
    }

    #[cfg(feature = "raw_gl_context")]
    pub fn create_context_raw_gl_context(&self) -> Context<raw_gl_context::GlContext> {
        self.create_context_with_backend::<raw_gl_context::GlContext>()
    }

    #[cfg(feature = "glutin")]
    pub fn create_context_glutin(&self) -> Context<glutin::RawContext<glutin::PossiblyCurrent>> {
        self.create_context_with_backend::<glutin::RawContext<glutin::PossiblyCurrent>>()
    }

    pub fn create_context_with_backend<C: ContextBackend>(&self) -> Context<C> {
        Context::new(self)
    }

    #[cfg(feature = "winit")]
    pub fn update(&mut self, gl: &Gl) {
        self.should_stop = self.event_loop.process_event(&mut self.input, &self.window, gl);
    }

    pub fn should_stop(&self) -> bool {
        self.should_stop
    }

    #[cfg(feature = "winit")]
    pub fn get_winit_window(&self) -> &winit::window::Window {
        &self.window
    }

    #[cfg(feature = "winit")]
    pub fn get_winit_window_mut(&mut self) -> &mut winit::window::Window {
        &mut self.window
    }

    #[cfg(feature = "winit")]
    pub fn keydown(&mut self, keycode: &winit::event::VirtualKeyCode) -> bool {
        self.input.get_keydown(keycode)
    }

    #[cfg(feature = "winit")]
    pub fn keyup(&mut self, keycode: &winit::event::VirtualKeyCode) -> bool {
        self.input.get_keyup(keycode)
    }

    #[cfg(feature = "winit")]
    pub fn keypressed(&mut self, keycode: &winit::event::VirtualKeyCode) -> bool {
        self.input.get_key_pressed(keycode)
    }

    #[cfg(feature = "winit")]
    pub fn cursor_pos(&mut self) -> CursorPosition<DesktopOrigin> {
        self.input.get_cursor_pos()
    }

    #[cfg(feature = "winit")]
    pub fn cursor_delta(&mut self) -> (i32, i32) {
        self.input.get_cursor_delta()
    }

    #[cfg(feature = "winit")]
    pub fn mouse_down(&mut self, button: &winit::event::MouseButton) -> bool {
        if let Some(index) = input::mouse_button_index_3(button) {
            self.input.get_mouse_down(index)
        } else {
            false
        }
    }

    #[cfg(feature = "winit")]
    pub fn mouse_up(&mut self, button: &winit::event::MouseButton) -> bool {
        if let Some(index) = input::mouse_button_index_3(button) {
            self.input.get_mouse_up(index)
        } else {
            false
        }
    }

    #[cfg(feature = "winit")]
    pub fn mouse_pressed(&mut self, button: &winit::event::MouseButton) -> bool {
        if let Some(index) = input::mouse_button_index_3(button) {
            self.input.get_mouse_pressed(index)
        } else {
            false
        }
    }
}