bevy/crates/bevy_winit/src/lib.rs

168 lines
6.7 KiB
Rust
Raw Normal View History

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,
mouse::{MouseButtonInput, MouseMotion},
2020-04-05 06:42:39 +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::{
CreateWindow, CursorMoved, Window, WindowCloseRequested, WindowCreated, WindowResized, Windows,
2020-04-19 19:13:04 +00:00
};
use glam::Vec2;
2020-04-06 03:19:02 +00:00
use legion::prelude::*;
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-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-30 21:53:32 +00:00
handle_create_window_events(
&mut app.resources,
&event_loop,
&mut create_window_event_reader,
2020-03-30 21:53:32 +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>>() {
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-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,
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-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
}
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 =
app.resources.get_mut::<Events<MouseMotion>>().unwrap();
mouse_motion_events.send(MouseMotion {
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,
&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-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<()>,
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();
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_event_reader.iter(&create_window_events) {
2020-03-30 21:53:32 +00:00
let window = Window::new(&create_window_event.descriptor);
winit_windows.create_window(event_loop, &window);
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
}
}