mirror of
https://github.com/bevyengine/bevy
synced 2025-02-16 22:18:33 +00:00
camera: update cameras when windows are created
This commit is contained in:
parent
f72c4beadf
commit
06b2b06e9d
2 changed files with 18 additions and 1 deletions
|
@ -1,7 +1,7 @@
|
|||
use crate::CameraProjection;
|
||||
use bevy_app::{EventReader, Events};
|
||||
use bevy_property::Properties;
|
||||
use bevy_window::WindowResized;
|
||||
use bevy_window::{WindowCreated, WindowResized, Windows};
|
||||
use glam::Mat4;
|
||||
use legion::{prelude::*, storage::Component};
|
||||
|
||||
|
@ -15,11 +15,27 @@ pub fn camera_system<T: CameraProjection + Component>(
|
|||
_resources: &mut Resources,
|
||||
) -> Box<dyn Schedulable> {
|
||||
let mut window_resized_event_reader = EventReader::<WindowResized>::default();
|
||||
let mut window_created_event_reader = EventReader::<WindowCreated>::default();
|
||||
(move |world: &mut SubWorld,
|
||||
window_resized_events: Res<Events<WindowResized>>,
|
||||
window_created_events: Res<Events<WindowCreated>>,
|
||||
windows: Res<Windows>,
|
||||
query: &mut Query<(Write<Camera>, Write<T>)>| {
|
||||
let primary_window_resized_event = window_resized_event_reader
|
||||
.find_latest(&window_resized_events, |event| event.is_primary);
|
||||
|
||||
for event in window_created_event_reader.iter(&window_created_events) {
|
||||
if !event.is_primary {
|
||||
continue;
|
||||
}
|
||||
if let Some(window) = windows.get(event.id) {
|
||||
for (mut camera, mut camera_projection) in query.iter_mut(world) {
|
||||
camera_projection.update(window.width as usize, window.height as usize);
|
||||
camera.view_matrix = camera_projection.get_view_matrix();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(primary_window_resized_event) = primary_window_resized_event {
|
||||
for (mut camera, mut camera_projection) in query.iter_mut(world) {
|
||||
camera_projection.update(
|
||||
|
|
|
@ -14,6 +14,7 @@ impl WindowId {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Window {
|
||||
pub id: WindowId,
|
||||
pub width: u32,
|
||||
|
|
Loading…
Add table
Reference in a new issue