reverie_engine_opengl/
window.rs1use self::builder::WindowConfig;
2use crate::gl::Gl;
3use crate::{Context, ContextBackend};
4use input::Input;
5
6#[cfg(feature = "glutin")]
7pub use glutin;
8#[cfg(feature = "winit")]
9pub use winit;
10
11mod builder;
12mod event_loop;
13pub(crate) mod input;
14
15pub use builder::WindowBuilder;
16pub use event_loop::EventLoop;
17pub use input::cursor::{CursorPosition, DesktopOrigin, WindowCenter, WindowOrigin};
18
19#[derive(Debug)]
20pub struct Window {
21 #[cfg(feature = "winit")]
22 event_loop: EventLoop,
23 #[cfg(feature = "winit")]
24 pub(crate) window: winit::window::Window,
25 #[cfg(feature = "winit")]
26 input: Input,
27 should_stop: bool,
28}
29
30impl Window {
31 #[cfg(feature = "winit")]
32 pub(crate) fn new(config: WindowConfig) -> Self {
33 let event_loop = EventLoop::new();
34 let window = winit::window::WindowBuilder::new()
35 .with_title(config.title.unwrap_or_else(|| "ReverieEngine".to_owned()))
36 .with_inner_size(winit::dpi::LogicalSize::new(
37 config.width as f32,
38 config.height as f32,
39 ))
40 .with_maximized(config.maximize)
41 .build(&event_loop.event_loop)
42 .unwrap();
43 let input = Input::new();
44
45 Self {
46 event_loop,
47 window,
48 input,
49 should_stop: false,
50 }
51 }
52
53 #[cfg(not(feature = "winit"))]
54 pub(crate) fn new() -> Self {
55 Self { should_stop: false }
56 }
57
58 #[cfg(feature = "raw_gl_context")]
59 pub fn create_context_raw_gl_context(&self) -> Context<raw_gl_context::GlContext> {
60 self.create_context_with_backend::<raw_gl_context::GlContext>()
61 }
62
63 #[cfg(feature = "glutin")]
64 pub fn create_context_glutin(&self) -> Context<glutin::RawContext<glutin::PossiblyCurrent>> {
65 self.create_context_with_backend::<glutin::RawContext<glutin::PossiblyCurrent>>()
66 }
67
68 pub fn create_context_with_backend<C: ContextBackend>(&self) -> Context<C> {
69 Context::new(self)
70 }
71
72 #[cfg(feature = "winit")]
73 pub fn update(&mut self, gl: &Gl) {
74 self.should_stop = self
75 .event_loop
76 .process_event(&mut self.input, &self.window, gl);
77 }
78
79 pub const fn should_stop(&self) -> bool {
80 self.should_stop
81 }
82
83 #[cfg(feature = "winit")]
84 pub const fn get_winit_window(&self) -> &winit::window::Window {
85 &self.window
86 }
87
88 #[cfg(feature = "winit")]
89 pub fn get_winit_window_mut(&mut self) -> &mut winit::window::Window {
90 &mut self.window
91 }
92
93 #[cfg(feature = "winit")]
94 pub fn keydown(&mut self, keycode: &winit::event::VirtualKeyCode) -> bool {
95 self.input.get_keydown(keycode)
96 }
97
98 #[cfg(feature = "winit")]
99 pub fn keyup(&mut self, keycode: &winit::event::VirtualKeyCode) -> bool {
100 self.input.get_keyup(keycode)
101 }
102
103 #[cfg(feature = "winit")]
104 pub fn keypressed(&self, keycode: &winit::event::VirtualKeyCode) -> bool {
105 self.input.get_key_pressed(keycode)
106 }
107
108 #[cfg(feature = "winit")]
109 pub const fn cursor_pos(&self) -> CursorPosition<DesktopOrigin> {
110 self.input.get_cursor_pos()
111 }
112
113 #[cfg(feature = "winit")]
114 pub const fn cursor_delta(&self) -> (i32, i32) {
115 self.input.get_cursor_delta()
116 }
117
118 #[cfg(feature = "winit")]
119 pub fn mouse_down(&mut self, button: &winit::event::MouseButton) -> bool {
120 input::mouse_button_index_3(button).map_or(false, |index| self.input.get_mouse_down(index))
121 }
122
123 #[cfg(feature = "winit")]
124 pub fn mouse_up(&mut self, button: &winit::event::MouseButton) -> bool {
125 input::mouse_button_index_3(button).map_or(false, |index| self.input.get_mouse_up(index))
126 }
127
128 #[cfg(feature = "winit")]
129 pub fn mouse_pressed(&self, button: &winit::event::MouseButton) -> bool {
130 input::mouse_button_index_3(button)
131 .map_or(false, |index| self.input.get_mouse_pressed(index))
132 }
133}