mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
Added WindowFocused event (#956)
This commit is contained in:
parent
e1b995f0b0
commit
08b6aa59f8
3 changed files with 20 additions and 6 deletions
|
@ -57,3 +57,10 @@ pub struct ReceivedCharacter {
|
|||
pub id: WindowId,
|
||||
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,
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ impl Plugin for WindowPlugin {
|
|||
.add_event::<CursorEntered>()
|
||||
.add_event::<CursorLeft>()
|
||||
.add_event::<ReceivedCharacter>()
|
||||
.add_event::<WindowFocused>()
|
||||
.init_resource::<Windows>();
|
||||
|
||||
if self.add_primary_window {
|
||||
|
|
|
@ -15,7 +15,7 @@ use bevy_math::Vec2;
|
|||
use bevy_utils::tracing::{error, trace};
|
||||
use bevy_window::{
|
||||
CreateWindow, CursorEntered, CursorLeft, CursorMoved, ReceivedCharacter, Window,
|
||||
WindowCloseRequested, WindowCreated, WindowResized, Windows,
|
||||
WindowCloseRequested, WindowCreated, WindowFocused, WindowResized, Windows,
|
||||
};
|
||||
use winit::{
|
||||
event::{self, DeviceEvent, Event, WindowEvent},
|
||||
|
@ -27,11 +27,7 @@ pub struct WinitPlugin;
|
|||
|
||||
impl Plugin for WinitPlugin {
|
||||
fn build(&self, app: &mut AppBuilder) {
|
||||
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>()
|
||||
.init_resource::<WinitWindows>()
|
||||
app.init_resource::<WinitWindows>()
|
||||
.set_runner(winit_runner)
|
||||
.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_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 {
|
||||
|
|
Loading…
Reference in a new issue