2020-03-30 21:53:32 +00:00
|
|
|
mod winit_windows;
|
|
|
|
pub use winit_windows::*;
|
|
|
|
|
|
|
|
use crate::prelude::*;
|
2020-03-28 00:43:03 +00:00
|
|
|
|
2020-03-31 02:21:12 +00:00
|
|
|
use super::{CreateWindow, Window, WindowCreated, WindowResized, Windows};
|
2020-03-28 00:43:03 +00:00
|
|
|
use winit::{
|
|
|
|
event,
|
|
|
|
event::WindowEvent,
|
2020-03-30 21:53:32 +00:00
|
|
|
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
|
2020-03-28 00:43:03 +00:00
|
|
|
};
|
|
|
|
|
2020-03-29 07:53:47 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct WinitPlugin;
|
|
|
|
|
|
|
|
impl AppPlugin for WinitPlugin {
|
2020-03-30 18:52:33 +00:00
|
|
|
fn build(&self, app: AppBuilder) -> AppBuilder {
|
2020-03-31 02:21:12 +00:00
|
|
|
app.add_resource(WinitWindows::default())
|
|
|
|
.set_runner(winit_runner)
|
2020-03-29 07:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"Winit"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 18:52:33 +00:00
|
|
|
pub fn winit_runner(mut app: App) {
|
|
|
|
env_logger::init();
|
|
|
|
let event_loop = EventLoop::new();
|
2020-04-01 01:04:54 +00:00
|
|
|
let mut create_window_event_reader = app.resources.get_event_reader::<CreateWindow>();
|
2020-03-28 00:43:03 +00:00
|
|
|
|
2020-03-30 21:53:32 +00:00
|
|
|
handle_create_window_events(
|
|
|
|
&mut app.resources,
|
|
|
|
&event_loop,
|
2020-04-01 01:04:54 +00:00
|
|
|
&mut create_window_event_reader,
|
2020-03-30 21:53:32 +00:00
|
|
|
);
|
2020-03-28 00:43:03 +00:00
|
|
|
|
2020-03-30 18:52:33 +00:00
|
|
|
log::debug!("Entering render loop");
|
2020-03-30 21:53:32 +00:00
|
|
|
event_loop.run(move |event, event_loop, control_flow| {
|
2020-03-30 18:52:33 +00:00
|
|
|
*control_flow = if cfg!(feature = "metal-auto-capture") {
|
|
|
|
ControlFlow::Exit
|
|
|
|
} else {
|
|
|
|
ControlFlow::Poll
|
|
|
|
};
|
|
|
|
match event {
|
|
|
|
event::Event::WindowEvent {
|
|
|
|
event: WindowEvent::Resized(size),
|
2020-03-30 21:53:32 +00:00
|
|
|
window_id: winit_window_id,
|
2020-03-30 18:52:33 +00:00
|
|
|
..
|
|
|
|
} => {
|
2020-03-30 21:53:32 +00:00
|
|
|
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
|
|
let mut windows = app.resources.get_mut::<Windows>().unwrap();
|
|
|
|
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
2020-03-31 02:21:12 +00:00
|
|
|
let is_primary = windows
|
|
|
|
.get_primary()
|
|
|
|
.map(|primary_window| primary_window.id == window_id)
|
|
|
|
.unwrap_or(false);
|
2020-03-30 21:53:32 +00:00
|
|
|
let mut window = windows.get_mut(window_id).unwrap();
|
2020-03-30 18:52:33 +00:00
|
|
|
window.width = size.width;
|
|
|
|
window.height = size.height;
|
2020-03-28 00:43:03 +00:00
|
|
|
|
2020-04-01 01:04:54 +00:00
|
|
|
let mut resize_event = app.resources.get_mut::<Events<WindowResized>>().unwrap();
|
2020-03-31 02:21:12 +00:00
|
|
|
resize_event.send(WindowResized {
|
2020-03-30 21:53:32 +00:00
|
|
|
id: window_id,
|
2020-03-30 18:52:33 +00:00
|
|
|
height: window.height,
|
|
|
|
width: window.width,
|
2020-03-31 02:21:12 +00:00
|
|
|
is_primary,
|
2020-03-30 18:52:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
event::Event::WindowEvent { event, .. } => match event {
|
|
|
|
WindowEvent::KeyboardInput {
|
|
|
|
input:
|
|
|
|
event::KeyboardInput {
|
|
|
|
virtual_keycode: Some(event::VirtualKeyCode::Escape),
|
|
|
|
state: event::ElementState::Pressed,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
2020-03-28 00:43:03 +00:00
|
|
|
}
|
2020-03-30 18:52:33 +00:00
|
|
|
| WindowEvent::CloseRequested => {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
2020-03-28 00:43:03 +00:00
|
|
|
}
|
2020-03-30 18:52:33 +00:00
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
event::Event::MainEventsCleared => {
|
2020-03-30 21:53:32 +00:00
|
|
|
handle_create_window_events(
|
|
|
|
&mut app.resources,
|
|
|
|
event_loop,
|
2020-04-01 01:04:54 +00:00
|
|
|
&mut create_window_event_reader,
|
2020-03-30 21:53:32 +00:00
|
|
|
);
|
2020-03-30 18:52:33 +00:00
|
|
|
app.update();
|
2020-03-28 00:43:03 +00:00
|
|
|
}
|
2020-03-30 18:52:33 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
});
|
2020-03-29 07:53:47 +00:00
|
|
|
}
|
2020-03-30 21:53:32 +00:00
|
|
|
|
|
|
|
fn handle_create_window_events(
|
|
|
|
resources: &mut Resources,
|
|
|
|
event_loop: &EventLoopWindowTarget<()>,
|
2020-04-01 01:04:54 +00:00
|
|
|
create_window_event_reader: &mut EventReader<CreateWindow>,
|
2020-03-30 21:53:32 +00:00
|
|
|
) {
|
|
|
|
let mut winit_windows = resources.get_mut::<WinitWindows>().unwrap();
|
|
|
|
let mut windows = resources.get_mut::<Windows>().unwrap();
|
2020-04-01 01:04:54 +00:00
|
|
|
let create_window_events = resources.get::<Events<CreateWindow>>().unwrap();
|
|
|
|
let mut window_created_events = resources.get_mut::<Events<WindowCreated>>().unwrap();
|
|
|
|
for create_window_event in create_window_events.iter(create_window_event_reader) {
|
2020-03-30 21:53:32 +00:00
|
|
|
let window = Window::new(&create_window_event.descriptor);
|
2020-03-31 02:21:12 +00:00
|
|
|
winit_windows.create_window(event_loop, &window);
|
2020-03-31 05:23:48 +00:00
|
|
|
let window_id = window.id;
|
2020-03-30 21:53:32 +00:00
|
|
|
windows.add(window);
|
2020-03-31 05:23:48 +00:00
|
|
|
let is_primary = windows.get_primary().map(|primary| primary.id == window_id).unwrap_or(false);
|
|
|
|
window_created_events.send(WindowCreated { id: window_id, is_primary });
|
2020-03-30 21:53:32 +00:00
|
|
|
}
|
|
|
|
}
|