mirror of
https://github.com/bevyengine/bevy
synced 2024-12-30 15:03:23 +00:00
25bfa80e60
# Objective Yet another PR for migrating stuff to required components. This time, cameras! ## Solution As per the [selected proposal](https://hackmd.io/tsYID4CGRiWxzsgawzxG_g#Combined-Proposal-1-Selected), deprecate `Camera2dBundle` and `Camera3dBundle` in favor of `Camera2d` and `Camera3d`. Adding a `Camera` without `Camera2d` or `Camera3d` now logs a warning, as suggested by Cart [on Discord](https://discord.com/channels/691052431525675048/1264881140007702558/1291506402832945273). I would personally like cameras to work a bit differently and be split into a few more components, to avoid some footguns and confusing semantics, but that is more controversial, and shouldn't block this core migration. ## Testing I ran a few 2D and 3D examples, and tried cameras with and without render graphs. --- ## Migration Guide `Camera2dBundle` and `Camera3dBundle` have been deprecated in favor of `Camera2d` and `Camera3d`. Inserting them will now also insert the other components required by them automatically.
104 lines
3.2 KiB
Rust
104 lines
3.2 KiB
Rust
//! Displays information about available monitors (displays).
|
|
|
|
use bevy::{
|
|
prelude::*,
|
|
render::camera::RenderTarget,
|
|
window::{ExitCondition, Monitor, WindowMode, WindowRef},
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
|
primary_window: None,
|
|
exit_condition: ExitCondition::DontExit,
|
|
..default()
|
|
}))
|
|
.add_systems(Update, (update, close_on_esc))
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct MonitorRef(Entity);
|
|
|
|
fn update(
|
|
mut commands: Commands,
|
|
monitors_added: Query<(Entity, &Monitor), Added<Monitor>>,
|
|
mut monitors_removed: RemovedComponents<Monitor>,
|
|
monitor_refs: Query<(Entity, &MonitorRef)>,
|
|
) {
|
|
for (entity, monitor) in monitors_added.iter() {
|
|
// Spawn a new window on each monitor
|
|
let name = monitor.name.clone().unwrap_or_else(|| "<no name>".into());
|
|
let size = format!("{}x{}px", monitor.physical_height, monitor.physical_width);
|
|
let hz = monitor
|
|
.refresh_rate_millihertz
|
|
.map(|x| format!("{}Hz", x as f32 / 1000.0))
|
|
.unwrap_or_else(|| "<unknown>".into());
|
|
let position = format!(
|
|
"x={} y={}",
|
|
monitor.physical_position.x, monitor.physical_position.y
|
|
);
|
|
let scale = format!("{:.2}", monitor.scale_factor);
|
|
|
|
let window = commands
|
|
.spawn((
|
|
Window {
|
|
title: name.clone(),
|
|
mode: WindowMode::Fullscreen(MonitorSelection::Entity(entity)),
|
|
position: WindowPosition::Centered(MonitorSelection::Entity(entity)),
|
|
..default()
|
|
},
|
|
MonitorRef(entity),
|
|
))
|
|
.id();
|
|
|
|
let camera = commands
|
|
.spawn((
|
|
Camera2d,
|
|
Camera {
|
|
target: RenderTarget::Window(WindowRef::Entity(window)),
|
|
..default()
|
|
},
|
|
))
|
|
.id();
|
|
|
|
let info_text = format!(
|
|
"Monitor: {name}\nSize: {size}\nRefresh rate: {hz}\nPosition: {position}\nScale: {scale}\n\n",
|
|
);
|
|
commands.spawn((
|
|
TextBundle::from_section(info_text, default()).with_style(Style {
|
|
position_type: PositionType::Relative,
|
|
height: Val::Percent(100.0),
|
|
width: Val::Percent(100.0),
|
|
..default()
|
|
}),
|
|
TargetCamera(camera),
|
|
MonitorRef(entity),
|
|
));
|
|
}
|
|
|
|
// Remove windows for removed monitors
|
|
for monitor_entity in monitors_removed.read() {
|
|
for (ref_entity, monitor_ref) in monitor_refs.iter() {
|
|
if monitor_ref.0 == monitor_entity {
|
|
commands.entity(ref_entity).despawn_recursive();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn close_on_esc(
|
|
mut commands: Commands,
|
|
focused_windows: Query<(Entity, &Window)>,
|
|
input: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
for (window, focus) in focused_windows.iter() {
|
|
if !focus.focused {
|
|
continue;
|
|
}
|
|
|
|
if input.just_pressed(KeyCode::Escape) {
|
|
commands.entity(window).despawn();
|
|
}
|
|
}
|
|
}
|