Added WindowFocused event (#956)

This commit is contained in:
thebluefish 2020-12-07 13:24:25 -08:00 committed by GitHub
parent e1b995f0b0
commit 08b6aa59f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 6 deletions

View File

@ -57,3 +57,10 @@ pub struct ReceivedCharacter {
pub id: WindowId, pub id: WindowId,
pub char: char, pub char: char,
} }
/// An event that indicates a window has received or lost focus.
#[derive(Debug, Clone)]
pub struct WindowFocused {
pub id: WindowId,
pub focused: bool,
}

View File

@ -42,6 +42,7 @@ impl Plugin for WindowPlugin {
.add_event::<CursorEntered>() .add_event::<CursorEntered>()
.add_event::<CursorLeft>() .add_event::<CursorLeft>()
.add_event::<ReceivedCharacter>() .add_event::<ReceivedCharacter>()
.add_event::<WindowFocused>()
.init_resource::<Windows>(); .init_resource::<Windows>();
if self.add_primary_window { if self.add_primary_window {

View File

@ -15,7 +15,7 @@ use bevy_math::Vec2;
use bevy_utils::tracing::{error, trace}; use bevy_utils::tracing::{error, trace};
use bevy_window::{ use bevy_window::{
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window, CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window,
WindowCloseRequested, WindowCreated, WindowResized, Windows, WindowCloseRequested, WindowCreated, WindowFocused, WindowResized, Windows,
}; };
use winit::{ use winit::{
event::{self, DeviceEvent, Event, WindowEvent}, event::{self, DeviceEvent, Event, WindowEvent},
@ -27,11 +27,7 @@ pub struct WinitPlugin;
impl Plugin for WinitPlugin { impl Plugin for WinitPlugin {
fn build(&self, app: &mut AppBuilder) { fn build(&self, app: &mut AppBuilder) {
app app.init_resource::<WinitWindows>()
// 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>()
.init_resource::<WinitWindows>()
.set_runner(winit_runner) .set_runner(winit_runner)
.add_system(change_window); .add_system(change_window);
} }
@ -340,6 +336,16 @@ pub fn winit_runner(mut app: App) {
window.update_scale_factor_from_backend(scale_factor); window.update_scale_factor_from_backend(scale_factor);
window.update_resolution_from_backend(size.width, size.height); window.update_resolution_from_backend(size.width, size.height);
} }
WindowEvent::Focused(focused) => {
let mut focused_events =
app.resources.get_mut::<Events<WindowFocused>>().unwrap();
let winit_windows = app.resources.get_mut::<WinitWindows>().unwrap();
let window_id = winit_windows.get_window_id(winit_window_id).unwrap();
focused_events.send(WindowFocused {
id: window_id,
focused,
});
}
_ => {} _ => {}
}, },
event::Event::DeviceEvent { event::Event::DeviceEvent {