2020-04-04 21:59:49 +00:00
|
|
|
mod converters;
|
2020-03-30 21:53:32 +00:00
|
|
|
mod winit_windows;
|
|
|
|
pub use winit_windows::*;
|
|
|
|
|
2020-04-06 03:19:02 +00:00
|
|
|
use bevy_input::{
|
|
|
|
keyboard::KeyboardInput,
|
2020-06-05 06:34:21 +00:00
|
|
|
mouse::{MouseButtonInput, MouseMotion},
|
2020-04-05 06:42:39 +00:00
|
|
|
};
|
2020-03-28 00:43:03 +00:00
|
|
|
|
2020-06-04 06:53:00 +00:00
|
|
|
use bevy_app::{App, AppBuilder, AppExit, AppPlugin, EventReader, Events};
|
2020-04-19 19:13:04 +00:00
|
|
|
use bevy_window::{
|
2020-06-04 06:22:32 +00:00
|
|
|
CreateWindow, CursorMoved, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows,
|
2020-04-19 19:13:04 +00:00
|
|
|
};
|
2020-06-04 06:22:32 +00:00
|
|
|
use glam::Vec2;
|
2020-04-06 03:19:02 +00:00
|
|
|
use legion::prelude::*;
|
2020-03-28 00:43:03 +00:00
|
|
|
use winit::{
|
|
|
|
event,
|
2020-04-05 07:32:53 +00:00
|
|
|
event::{DeviceEvent, 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-04-06 03:19:02 +00:00
|
|
|
fn build(&self, app: &mut AppBuilder) {
|
2020-04-04 21:59:49 +00:00
|
|
|
app
|
|
|
|
// TODO: It would be great to provide a raw winit WindowEvent here, but the lifetime on it is
|
|
|
|
// stopping us. there are plans to remove the lifetime: https://github.com/rust-windowing/winit/pull/1456
|
|
|
|
// .add_event::<winit::event::WindowEvent>()
|
2020-05-13 23:35:38 +00:00
|
|
|
.init_resource::<WinitWindows>()
|
2020-04-06 03:19:02 +00:00
|
|
|
.set_runner(winit_runner);
|
2020-03-29 07:53:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 18:52:33 +00:00
|
|
|
pub fn winit_runner(mut app: App) {
|
|
|
|
let event_loop = EventLoop::new();
|
2020-06-04 06:53:00 +00:00
|
|
|
let mut create_window_event_reader = EventReader::<CreateWindow>::default();
|
|
|
|
let mut app_exit_event_reader = EventReader::<AppExit>::default();
|
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-04-05 04:02:31 +00:00
|
|
|
log::debug!("Entering winit event 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
|
|
|
|
};
|
2020-04-19 19:13:04 +00:00
|
|
|
|
|
|
|
if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() {
|
2020-04-28 17:59:42 +00:00
|
|
|
if app_exit_event_reader.latest(&app_exit_events).is_some() {
|
2020-04-19 19:13:04 +00:00
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 18:52:33 +00:00
|
|
|
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();
|
|
|
|
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-19 19:13:04 +00:00
|
|
|
let mut resize_events = app.resources.get_mut::<Events<WindowResized>>().unwrap();
|
|
|
|
resize_events.send(WindowResized {
|
2020-03-30 21:53:32 +00:00
|
|
|
id: window_id,
|
2020-05-30 00:25:14 +00:00
|
|
|
height: window.height as usize,
|
|
|
|
width: window.width as usize,
|
2020-04-19 19:13:04 +00:00
|
|
|
is_primary: windows.is_primary(window_id),
|
2020-03-30 18:52:33 +00:00
|
|
|
});
|
|
|
|
}
|
2020-04-19 19:13:04 +00:00
|
|
|
event::Event::WindowEvent {
|
|
|
|
event,
|
|
|
|
window_id: winit_window_id,
|
|
|
|
..
|
|
|
|
} => match event {
|
2020-04-04 21:59:49 +00:00
|
|
|
WindowEvent::CloseRequested => {
|
2020-04-19 19:13:04 +00:00
|
|
|
let mut window_close_requested_events = app
|
|
|
|
.resources
|
|
|
|
.get_mut::<Events<WindowCloseRequested>>()
|
|
|
|
.unwrap();
|
|
|
|
let windows = app.resources.get_mut::<Windows>().unwrap();
|
|
|
|
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
|
|
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
|
|
|
window_close_requested_events.send(WindowCloseRequested {
|
|
|
|
id: window_id,
|
|
|
|
is_primary: windows.is_primary(window_id),
|
|
|
|
});
|
2020-03-28 00:43:03 +00:00
|
|
|
}
|
2020-04-04 21:59:49 +00:00
|
|
|
WindowEvent::KeyboardInput { ref input, .. } => {
|
|
|
|
let mut keyboard_input_events =
|
|
|
|
app.resources.get_mut::<Events<KeyboardInput>>().unwrap();
|
2020-04-06 03:19:02 +00:00
|
|
|
keyboard_input_events.send(converters::convert_keyboard_input(input));
|
2020-04-04 21:59:49 +00:00
|
|
|
}
|
2020-06-04 06:22:32 +00:00
|
|
|
WindowEvent::CursorMoved { position, .. } => {
|
|
|
|
let mut cursor_moved_events =
|
|
|
|
app.resources.get_mut::<Events<CursorMoved>>().unwrap();
|
|
|
|
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
|
|
|
|
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
|
|
|
|
cursor_moved_events.send(CursorMoved {
|
|
|
|
id: window_id,
|
|
|
|
position: Vec2::new(position.x as f32, position.y as f32),
|
|
|
|
});
|
|
|
|
}
|
2020-04-05 06:42:39 +00:00
|
|
|
WindowEvent::MouseInput { state, button, .. } => {
|
2020-04-05 07:32:53 +00:00
|
|
|
let mut mouse_button_input_events =
|
|
|
|
app.resources.get_mut::<Events<MouseButtonInput>>().unwrap();
|
|
|
|
mouse_button_input_events.send(MouseButtonInput {
|
2020-04-06 03:19:02 +00:00
|
|
|
button: converters::convert_mouse_button(button.into()),
|
|
|
|
state: converters::convert_element_state(state),
|
2020-04-05 06:42:39 +00:00
|
|
|
});
|
|
|
|
}
|
2020-03-30 18:52:33 +00:00
|
|
|
_ => {}
|
|
|
|
},
|
2020-04-05 07:32:53 +00:00
|
|
|
event::Event::DeviceEvent { ref event, .. } => match event {
|
|
|
|
DeviceEvent::MouseMotion { delta } => {
|
|
|
|
let mut mouse_motion_events =
|
2020-06-05 06:34:21 +00:00
|
|
|
app.resources.get_mut::<Events<MouseMotion>>().unwrap();
|
|
|
|
mouse_motion_events.send(MouseMotion {
|
2020-06-04 06:22:32 +00:00
|
|
|
delta: Vec2::new(delta.0 as f32, delta.1 as f32),
|
|
|
|
});
|
2020-04-06 03:19:02 +00:00
|
|
|
}
|
2020-04-05 07:32:53 +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();
|
2020-04-28 17:59:42 +00:00
|
|
|
for create_window_event in create_window_event_reader.iter(&create_window_events) {
|
2020-06-25 23:02:21 +00:00
|
|
|
let window = Window::new(create_window_event.id, &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-04-04 19:43:16 +00:00
|
|
|
window_created_events.send(WindowCreated {
|
|
|
|
id: window_id,
|
2020-04-19 19:13:04 +00:00
|
|
|
is_primary: windows.is_primary(window_id),
|
2020-04-04 19:43:16 +00:00
|
|
|
});
|
2020-03-30 21:53:32 +00:00
|
|
|
}
|
|
|
|
}
|