 ddfafab971
			
		
	
	
		ddfafab971
		
	
	
	
	
		
			
			# Objective Fix https://github.com/bevyengine/bevy/issues/4530 - Make it easier to open/close/modify windows by setting them up as `Entity`s with a `Window` component. - Make multiple windows very simple to set up. (just add a `Window` component to an entity and it should open) ## Solution - Move all properties of window descriptor to ~components~ a component. - Replace `WindowId` with `Entity`. - ~Use change detection for components to update backend rather than events/commands. (The `CursorMoved`/`WindowResized`/... events are kept for user convenience.~ Check each field individually to see what we need to update, events are still kept for user convenience. --- ## Changelog - `WindowDescriptor` renamed to `Window`. - Width/height consolidated into a `WindowResolution` component. - Requesting maximization/minimization is done on the [`Window::state`] field. - `WindowId` is now `Entity`. ## Migration Guide - Replace `WindowDescriptor` with `Window`. - Change `width` and `height` fields in a `WindowResolution`, either by doing ```rust WindowResolution::new(width, height) // Explicitly // or using From<_> for tuples for convenience (1920., 1080.).into() ``` - Replace any `WindowCommand` code to just modify the `Window`'s fields directly and creating/closing windows is now by spawning/despawning an entity with a `Window` component like so: ```rust let window = commands.spawn(Window { ... }).id(); // open window commands.entity(window).despawn(); // close window ``` ## Unresolved - ~How do we tell when a window is minimized by a user?~ ~Currently using the `Resize(0, 0)` as an indicator of minimization.~ No longer attempting to tell given how finnicky this was across platforms, now the user can only request that a window be maximized/minimized. ## Future work - Move `exit_on_close` functionality out from windowing and into app(?) - https://github.com/bevyengine/bevy/issues/5621 - https://github.com/bevyengine/bevy/issues/7099 - https://github.com/bevyengine/bevy/issues/7098 Co-authored-by: Carter Anderson <mcanders1@gmail.com>
		
			
				
	
	
		
			114 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			114 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| //! Renders two cameras to the same window to accomplish "split screen".
 | |
| 
 | |
| use std::f32::consts::PI;
 | |
| 
 | |
| use bevy::{
 | |
|     core_pipeline::clear_color::ClearColorConfig, prelude::*, render::camera::Viewport,
 | |
|     window::WindowResized,
 | |
| };
 | |
| 
 | |
| fn main() {
 | |
|     App::new()
 | |
|         .add_plugins(DefaultPlugins)
 | |
|         .add_startup_system(setup)
 | |
|         .add_system(set_camera_viewports)
 | |
|         .run();
 | |
| }
 | |
| 
 | |
| /// set up a simple 3D scene
 | |
| fn setup(
 | |
|     mut commands: Commands,
 | |
|     asset_server: Res<AssetServer>,
 | |
|     mut meshes: ResMut<Assets<Mesh>>,
 | |
|     mut materials: ResMut<Assets<StandardMaterial>>,
 | |
| ) {
 | |
|     // plane
 | |
|     commands.spawn(PbrBundle {
 | |
|         mesh: meshes.add(Mesh::from(shape::Plane { size: 100.0 })),
 | |
|         material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
 | |
|         ..default()
 | |
|     });
 | |
| 
 | |
|     commands.spawn(SceneBundle {
 | |
|         scene: asset_server.load("models/animated/Fox.glb#Scene0"),
 | |
|         ..default()
 | |
|     });
 | |
| 
 | |
|     // Light
 | |
|     commands.spawn(DirectionalLightBundle {
 | |
|         transform: Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
 | |
|         directional_light: DirectionalLight {
 | |
|             shadows_enabled: true,
 | |
|             ..default()
 | |
|         },
 | |
|         ..default()
 | |
|     });
 | |
| 
 | |
|     // Left Camera
 | |
|     commands.spawn((
 | |
|         Camera3dBundle {
 | |
|             transform: Transform::from_xyz(0.0, 200.0, -100.0).looking_at(Vec3::ZERO, Vec3::Y),
 | |
|             ..default()
 | |
|         },
 | |
|         LeftCamera,
 | |
|     ));
 | |
| 
 | |
|     // Right Camera
 | |
|     commands.spawn((
 | |
|         Camera3dBundle {
 | |
|             transform: Transform::from_xyz(100.0, 100., 150.0).looking_at(Vec3::ZERO, Vec3::Y),
 | |
|             camera: Camera {
 | |
|                 // Renders the right camera after the left camera, which has a default priority of 0
 | |
|                 order: 1,
 | |
|                 ..default()
 | |
|             },
 | |
|             camera_3d: Camera3d {
 | |
|                 // don't clear on the second camera because the first camera already cleared the window
 | |
|                 clear_color: ClearColorConfig::None,
 | |
|                 ..default()
 | |
|             },
 | |
|             ..default()
 | |
|         },
 | |
|         RightCamera,
 | |
|     ));
 | |
| }
 | |
| 
 | |
| #[derive(Component)]
 | |
| struct LeftCamera;
 | |
| 
 | |
| #[derive(Component)]
 | |
| struct RightCamera;
 | |
| 
 | |
| fn set_camera_viewports(
 | |
|     windows: Query<&Window>,
 | |
|     mut resize_events: EventReader<WindowResized>,
 | |
|     mut left_camera: Query<&mut Camera, (With<LeftCamera>, Without<RightCamera>)>,
 | |
|     mut right_camera: Query<&mut Camera, With<RightCamera>>,
 | |
| ) {
 | |
|     // We need to dynamically resize the camera's viewports whenever the window size changes
 | |
|     // so then each camera always takes up half the screen.
 | |
|     // A resize_event is sent when the window is first created, allowing us to reuse this system for initial setup.
 | |
|     for resize_event in resize_events.iter() {
 | |
|         let window = windows.get(resize_event.window).unwrap();
 | |
|         let mut left_camera = left_camera.single_mut();
 | |
|         left_camera.viewport = Some(Viewport {
 | |
|             physical_position: UVec2::new(0, 0),
 | |
|             physical_size: UVec2::new(
 | |
|                 window.resolution.physical_width() / 2,
 | |
|                 window.resolution.physical_height(),
 | |
|             ),
 | |
|             ..default()
 | |
|         });
 | |
| 
 | |
|         let mut right_camera = right_camera.single_mut();
 | |
|         right_camera.viewport = Some(Viewport {
 | |
|             physical_position: UVec2::new(window.resolution.physical_width() / 2, 0),
 | |
|             physical_size: UVec2::new(
 | |
|                 window.resolution.physical_width() / 2,
 | |
|                 window.resolution.physical_height(),
 | |
|             ),
 | |
|             ..default()
 | |
|         });
 | |
|     }
 | |
| }
 |