From 64dc19db93dfc2e4ff06aa7386ffa97d3c3b38c5 Mon Sep 17 00:00:00 2001 From: tomara_x <86204514+tomara-x@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:20:33 +0200 Subject: [PATCH 1/5] 15.0-rc.3 --- Cargo.toml | 10 +++++----- README.md | 20 ++++++++++++++------ src/lib.rs | 2 +- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 245d5ba..9e9d06e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,11 +13,11 @@ version = "0.15.0" bevy_egui = ["dep:bevy_egui"] [dependencies] -bevy = {version = "0.14", features = ["bevy_render"], default-features = false} -bevy_egui = {version = "0.30", optional = true, default-features = false} +bevy = {version = "0.15.0-rc.3", features = ["bevy_render"], default-features = false} +bevy_egui = {git = "https://github.com/mvlabat/bevy_egui", rev = "refs/pull/309/head", optional = true, default-features = false} [dev-dependencies] -bevy = {version = "0.14", default-features = false, features = [ +bevy = {version = "0.15.0-rc.3", default-features = false, features = [ "bevy_render", "bevy_asset", "bevy_sprite", @@ -25,8 +25,8 @@ bevy = {version = "0.14", default-features = false, features = [ "bevy_core_pipeline", "x11", # github actions runners don't have libxkbcommon installed, so can't use wayland ]} -bevy-inspector-egui = { version = "0.27", default-features = false, features = ["bevy_render"]} -bevy_egui = {version = "0.30", default-features = false, features = ["default_fonts"]} +bevy-inspector-egui = { git = "https://github.com/jakobhellermann/bevy-inspector-egui", rev = "refs/pull/221/head", default-features = false, features = ["bevy_render"]} +bevy_egui = {git = "https://github.com/mvlabat/bevy_egui", rev = "refs/pull/309/head", default-features = false} rand = "0.8" [[example]] diff --git a/README.md b/README.md index ad7daf0..eb3f1bb 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,10 @@ App::new() Add the component to an orthographic camera: ```rust ignore -commands.spawn(Camera2dBundle::default()) - .insert(PanCam::default()); +commands.spawn(( + Camera2d, + PanCam::default(), +)) ``` This is enough to get going with sensible defaults. @@ -39,8 +41,9 @@ This is enough to get going with sensible defaults. Alternatively, set the fields of the `PanCam` component to customize behavior: ```rust ignore -commands.spawn(Camera2dBundle::default()) - .insert(PanCam { +commands.spawn(( + Camera2d, + PanCam { grab_buttons: vec![MouseButton::Left, MouseButton::Middle], // which buttons should drag the camera move_keys: DirectionKeys { // the keyboard buttons used to move the camera up: vec![KeyCode::KeyQ], // initalize the struct like this or use the provided methods for @@ -52,8 +55,13 @@ commands.spawn(Camera2dBundle::default()) enabled: true, // when false, controls are disabled. See toggle example. zoom_to_cursor: true, // whether to zoom towards the mouse or the center of the screen min_scale: 1., // prevent the camera from zooming too far in - max_scale: Some(40.), // prevent the camera from zooming too far out - }); + max_scale: 40., // prevent the camera from zooming too far out + min_x: f32::NEG_INFINITY, // minimum x position of the camera window + max_x: f32::INFINITY, // maximum x position of the camera window + min_y: f32::NEG_INFINITY, // minimum y position of the camera window + max_y: f32::INFINITY, // maximum y position of the camera window + }, +)); ``` See the [`simple`](./examples/simple.rs) and [`toggle`](./examples/toggle.rs) examples. diff --git a/src/lib.rs b/src/lib.rs index 6fd4af4..3c6ed3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -310,7 +310,7 @@ fn do_camera_movement( let direction = pan_cam.move_keys.direction(&keyboard_buttons); let keyboard_delta = - time.delta_seconds() * direction.normalize_or_zero() * pan_cam.speed * projection.scale; + time.delta_secs() * direction.normalize_or_zero() * pan_cam.speed * projection.scale; let delta = mouse_delta - keyboard_delta; if delta == Vec2::ZERO { From 84272003cff65825826e9cef620d8d142711e10a Mon Sep 17 00:00:00 2001 From: tomara_x <86204514+tomara-x@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:15:59 +0200 Subject: [PATCH 2/5] migrate examples egui example doesn't show the egui window for some reason. not sure if that's due to ongoing work on the pr at bevy_egui also inspector doesn't compile because of the work being done there. will check back on this when the dust settles a bit --- examples/camera_scaling_mode.rs | 18 ++++++++-------- examples/clamped_borders.rs | 13 ++++++------ examples/egui.rs | 13 ++++++------ examples/inspector.rs | 15 +++++++------ examples/limits.rs | 13 ++++++------ examples/simple.rs | 13 ++++++------ examples/toggle.rs | 13 ++++++------ examples/viewport.rs | 37 ++++++++++++++------------------- 8 files changed, 62 insertions(+), 73 deletions(-) diff --git a/examples/camera_scaling_mode.rs b/examples/camera_scaling_mode.rs index f6a65d7..4e49d75 100644 --- a/examples/camera_scaling_mode.rs +++ b/examples/camera_scaling_mode.rs @@ -4,17 +4,18 @@ use rand::prelude::random; fn main() { App::new() - .add_plugins((DefaultPlugins, PanCamPlugin::default())) + .add_plugins((DefaultPlugins, PanCamPlugin)) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands) { - let mut cam = Camera2dBundle::default(); - cam.projection.scaling_mode = ScalingMode::FixedVertical(10.0); + let mut ortho = OrthographicProjection::default_2d(); + ortho.scaling_mode = ScalingMode::FixedVertical { viewport_height: 10.0 }; commands.spawn(( - cam, + Camera2d, + ortho, PanCam { min_x: -10., max_x: 10., @@ -33,15 +34,14 @@ fn setup(mut commands: Commands) { let x = x as f32 * spacing + offset; let y = y as f32 * spacing + offset; let color = Color::hsl(240., random::() * 0.3, random::() * 0.3); - commands.spawn(SpriteBundle { - sprite: Sprite { + commands.spawn(( + Sprite { color, custom_size, ..default() }, - transform: Transform::from_xyz(x, y, 0.), - ..default() - }); + Transform::from_xyz(x, y, 0.), + )); } } } diff --git a/examples/clamped_borders.rs b/examples/clamped_borders.rs index c837959..c471a39 100644 --- a/examples/clamped_borders.rs +++ b/examples/clamped_borders.rs @@ -4,14 +4,14 @@ use rand::prelude::random; fn main() { App::new() - .add_plugins((DefaultPlugins, PanCamPlugin::default())) + .add_plugins((DefaultPlugins, PanCamPlugin)) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands) { commands.spawn(( - Camera2dBundle::default(), + Camera2d, PanCam { // prevent the camera from zooming too far out max_scale: 40., @@ -40,15 +40,14 @@ fn setup(mut commands: Commands) { let x = x as f32 * spacing - offset; let y = y as f32 * spacing - offset; let color = Color::hsl(240., random::() * 0.3, random::() * 0.3); - commands.spawn(SpriteBundle { - sprite: Sprite { + commands.spawn(( + Sprite { color, custom_size, ..default() }, - transform: Transform::from_xyz(x, y, 0.), - ..default() - }); + Transform::from_xyz(x, y, 0.), + )); } } } diff --git a/examples/egui.rs b/examples/egui.rs index 1123ca9..e5288f4 100644 --- a/examples/egui.rs +++ b/examples/egui.rs @@ -8,7 +8,7 @@ use rand::random; fn main() { App::new() - .add_plugins((DefaultPlugins, PanCamPlugin::default(), EguiPlugin)) + .add_plugins((DefaultPlugins, PanCamPlugin, EguiPlugin)) .add_systems(Update, egui_ui) .add_systems(Startup, setup) .run(); @@ -33,7 +33,7 @@ fn egui_ui(mut contexts: EguiContexts) { } fn setup(mut commands: Commands) { - commands.spawn((Camera2dBundle::default(), PanCam::default())); + commands.spawn((Camera2d, PanCam::default())); let n = 20; let spacing = 50.; @@ -44,15 +44,14 @@ fn setup(mut commands: Commands) { let x = x as f32 * spacing - offset; let y = y as f32 * spacing - offset; let color = Color::hsl(240., random::() * 0.3, random::() * 0.3); - commands.spawn(SpriteBundle { - sprite: Sprite { + commands.spawn(( + Sprite { color, custom_size, ..default() }, - transform: Transform::from_xyz(x, y, 0.), - ..default() - }); + Transform::from_xyz(x, y, 0.), + )); } } } diff --git a/examples/inspector.rs b/examples/inspector.rs index 99a0b4e..df495a4 100644 --- a/examples/inspector.rs +++ b/examples/inspector.rs @@ -7,15 +7,15 @@ fn main() { App::new() .add_plugins(( DefaultPlugins, - PanCamPlugin::default(), - WorldInspectorPlugin::default(), + PanCamPlugin, + WorldInspectorPlugin::new(), )) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands) { - commands.spawn((Camera2dBundle::default(), PanCam::default())); + commands.spawn((Camera2d, PanCam::default())); let n = 20; let spacing = 50.; @@ -26,15 +26,14 @@ fn setup(mut commands: Commands) { let x = x as f32 * spacing - offset; let y = y as f32 * spacing - offset; let color = Color::hsl(240., random::() * 0.3, random::() * 0.3); - commands.spawn(SpriteBundle { - sprite: Sprite { + commands.spawn(( + Sprite { color, custom_size, ..default() }, - transform: Transform::from_xyz(x, y, 0.), - ..default() - }); + Transform::from_xyz(x, y, 0.), + )); } } } diff --git a/examples/limits.rs b/examples/limits.rs index dab0bbe..c8c82b9 100644 --- a/examples/limits.rs +++ b/examples/limits.rs @@ -4,14 +4,14 @@ use rand::prelude::random; fn main() { App::new() - .add_plugins((DefaultPlugins, PanCamPlugin::default())) + .add_plugins((DefaultPlugins, PanCamPlugin)) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands) { commands.spawn(( - Camera2dBundle::default(), + Camera2d, PanCam { // Set max scale in order to prevent the camera from zooming too far out max_scale: 40., @@ -30,15 +30,14 @@ fn setup(mut commands: Commands) { let x = x as f32 * spacing - offset; let y = y as f32 * spacing - offset; let color = Color::hsl(240., random::() * 0.3, random::() * 0.3); - commands.spawn(SpriteBundle { - sprite: Sprite { + commands.spawn(( + Sprite { color, custom_size, ..default() }, - transform: Transform::from_xyz(x, y, 0.), - ..default() - }); + Transform::from_xyz(x, y, 0.), + )); } } } diff --git a/examples/simple.rs b/examples/simple.rs index 9e8f428..4c906a1 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -4,13 +4,13 @@ use rand::prelude::random; fn main() { App::new() - .add_plugins((DefaultPlugins, PanCamPlugin::default())) + .add_plugins((DefaultPlugins, PanCamPlugin)) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands) { - commands.spawn((Camera2dBundle::default(), PanCam::default())); + commands.spawn((Camera2d, PanCam::default())); let n = 20; let spacing = 50.; @@ -21,15 +21,14 @@ fn setup(mut commands: Commands) { let x = x as f32 * spacing - offset; let y = y as f32 * spacing - offset; let color = Color::hsl(240., random::() * 0.3, random::() * 0.3); - commands.spawn(SpriteBundle { - sprite: Sprite { + commands.spawn(( + Sprite { color, custom_size, ..default() }, - transform: Transform::from_xyz(x, y, 0.), - ..default() - }); + Transform::from_xyz(x, y, 0.), + )); } } } diff --git a/examples/toggle.rs b/examples/toggle.rs index 1535c38..e6fe594 100644 --- a/examples/toggle.rs +++ b/examples/toggle.rs @@ -4,14 +4,14 @@ use rand::prelude::random; fn main() { App::new() - .add_plugins((DefaultPlugins, PanCamPlugin::default())) + .add_plugins((DefaultPlugins, PanCamPlugin)) .add_systems(Startup, setup) .add_systems(Update, toggle_key) .run(); } fn setup(mut commands: Commands) { - commands.spawn((Camera2dBundle::default(), PanCam::default())); + commands.spawn((Camera2d, PanCam::default())); let n = 20; let spacing = 50.; @@ -22,15 +22,14 @@ fn setup(mut commands: Commands) { let x = x as f32 * spacing - offset; let y = y as f32 * spacing - offset; let color = Color::hsl(240., random::() * 0.3, random::() * 0.3); - commands.spawn(SpriteBundle { - sprite: Sprite { + commands.spawn(( + Sprite { color, custom_size, ..default() }, - transform: Transform::from_xyz(x, y, 0.), - ..default() - }); + Transform::from_xyz(x, y, 0.), + )); } } } diff --git a/examples/viewport.rs b/examples/viewport.rs index 67bc206..8237317 100644 --- a/examples/viewport.rs +++ b/examples/viewport.rs @@ -1,25 +1,22 @@ use bevy::{prelude::*, render::camera::Viewport}; use bevy_pancam::{PanCam, PanCamPlugin}; -use rand::prelude::random; fn main() { App::new() - .add_plugins((DefaultPlugins, PanCamPlugin::default())) + .add_plugins((DefaultPlugins, PanCamPlugin)) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands) { commands.spawn(( - Camera2dBundle { - camera: Camera { - viewport: Some(Viewport { - physical_position: UVec2::new(100, 200), - physical_size: UVec2::new(600, 400), - depth: 0.0..1.0, - }), - ..Camera2dBundle::default().camera - }, + Camera2d, + Camera { + viewport: Some(Viewport { + physical_position: UVec2::new(100, 200), + physical_size: UVec2::new(600, 400), + depth: 0.0..1.0, + }), ..default() }, PanCam { @@ -32,24 +29,22 @@ fn setup(mut commands: Commands) { )); // background - commands.spawn(SpriteBundle { - sprite: Sprite { + commands.spawn(( + Sprite { color: Color::srgb(0.3, 0.3, 0.3), custom_size: Some(Vec2::new(1000., 1000.)), ..default() }, - transform: Transform::from_xyz(0., 0., 0.), - ..default() - }); + Transform::from_xyz(0., 0., 0.), + )); // red square - commands.spawn(SpriteBundle { - sprite: Sprite { + commands.spawn(( + Sprite { color: Color::srgb(0.8, 0.3, 0.3), custom_size: Some(Vec2::new(100., 100.)), ..default() }, - transform: Transform::from_xyz(0., 0., 1.), - ..default() - }); + Transform::from_xyz(0., 0., 1.), + )); } From db5938a05ba09b3e5380601655d4887ad4ea4df8 Mon Sep 17 00:00:00 2001 From: tomara_x <86204514+tomara-x@users.noreply.github.com> Date: Sat, 30 Nov 2024 17:57:38 +0200 Subject: [PATCH 3/5] i think that's everything merged the edits from #69 (ortho, requiring camera2d, and bevy_window feature) --- Cargo.toml | 10 +++++----- examples/camera_scaling_mode.rs | 4 +++- examples/inspector.rs | 6 +----- src/lib.rs | 3 ++- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9e9d06e..9d11a46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,11 +13,11 @@ version = "0.15.0" bevy_egui = ["dep:bevy_egui"] [dependencies] -bevy = {version = "0.15.0-rc.3", features = ["bevy_render"], default-features = false} -bevy_egui = {git = "https://github.com/mvlabat/bevy_egui", rev = "refs/pull/309/head", optional = true, default-features = false} +bevy = {version = "0.15", features = ["bevy_render", "bevy_window"], default-features = false} +bevy_egui = {version = "0.31", optional = true, default-features = false} [dev-dependencies] -bevy = {version = "0.15.0-rc.3", default-features = false, features = [ +bevy = {version = "0.15", default-features = false, features = [ "bevy_render", "bevy_asset", "bevy_sprite", @@ -25,8 +25,8 @@ bevy = {version = "0.15.0-rc.3", default-features = false, features = [ "bevy_core_pipeline", "x11", # github actions runners don't have libxkbcommon installed, so can't use wayland ]} -bevy-inspector-egui = { git = "https://github.com/jakobhellermann/bevy-inspector-egui", rev = "refs/pull/221/head", default-features = false, features = ["bevy_render"]} -bevy_egui = {git = "https://github.com/mvlabat/bevy_egui", rev = "refs/pull/309/head", default-features = false} +bevy-inspector-egui = {version = "0.28", default-features = false, features = ["bevy_render"]} +bevy_egui = {version = "0.31", default-features = false} rand = "0.8" [[example]] diff --git a/examples/camera_scaling_mode.rs b/examples/camera_scaling_mode.rs index 4e49d75..0e9b72b 100644 --- a/examples/camera_scaling_mode.rs +++ b/examples/camera_scaling_mode.rs @@ -11,7 +11,9 @@ fn main() { fn setup(mut commands: Commands) { let mut ortho = OrthographicProjection::default_2d(); - ortho.scaling_mode = ScalingMode::FixedVertical { viewport_height: 10.0 }; + ortho.scaling_mode = ScalingMode::FixedVertical { + viewport_height: 10.0, + }; commands.spawn(( Camera2d, diff --git a/examples/inspector.rs b/examples/inspector.rs index df495a4..5a9011a 100644 --- a/examples/inspector.rs +++ b/examples/inspector.rs @@ -5,11 +5,7 @@ use rand::random; fn main() { App::new() - .add_plugins(( - DefaultPlugins, - PanCamPlugin, - WorldInspectorPlugin::new(), - )) + .add_plugins((DefaultPlugins, PanCamPlugin, WorldInspectorPlugin::new())) .add_systems(Startup, setup) .run(); } diff --git a/src/lib.rs b/src/lib.rs index 3c6ed3b..f36f906 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -330,6 +330,7 @@ fn do_camera_movement( /// A component that adds panning camera controls to an orthographic camera #[derive(Component, Reflect)] #[reflect(Component)] +#[require(Camera2d)] pub struct PanCam { /// The mouse buttons that will be used to drag and pan the camera pub grab_buttons: Vec, @@ -437,7 +438,7 @@ mod tests { /// Simple mock function to construct a square projection from a window size fn mock_proj(window_size: Vec2) -> OrthographicProjection { - let mut proj = Camera2dBundle::default().projection; + let mut proj = OrthographicProjection::default_2d(); proj.update(window_size.x, window_size.y); proj } From 263b798893f0bfa9cd9dd20c28feab8ddf305bb5 Mon Sep 17 00:00:00 2001 From: tomara_x <86204514+tomara-x@users.noreply.github.com> Date: Sat, 30 Nov 2024 18:09:09 +0200 Subject: [PATCH 4/5] we need core_pipeline for Camera2d --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9d11a46..c09feed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ version = "0.15.0" bevy_egui = ["dep:bevy_egui"] [dependencies] -bevy = {version = "0.15", features = ["bevy_render", "bevy_window"], default-features = false} +bevy = {version = "0.15", features = ["bevy_window", "bevy_core_pipeline"], default-features = false} bevy_egui = {version = "0.31", optional = true, default-features = false} [dev-dependencies] From 7f4130b4b1d5c9c8ad6e85695d3aac6ccb98eb66 Mon Sep 17 00:00:00 2001 From: tomara_x <86204514+tomara-x@users.noreply.github.com> Date: Sat, 30 Nov 2024 18:17:38 +0200 Subject: [PATCH 5/5] idk why i removed default fonts there --- Cargo.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c09feed..a41a0b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,8 +13,8 @@ version = "0.15.0" bevy_egui = ["dep:bevy_egui"] [dependencies] -bevy = {version = "0.15", features = ["bevy_window", "bevy_core_pipeline"], default-features = false} -bevy_egui = {version = "0.31", optional = true, default-features = false} +bevy = { version = "0.15", features = ["bevy_window", "bevy_core_pipeline"], default-features = false } +bevy_egui = { version = "0.31", optional = true, default-features = false } [dev-dependencies] bevy = {version = "0.15", default-features = false, features = [ @@ -25,8 +25,8 @@ bevy = {version = "0.15", default-features = false, features = [ "bevy_core_pipeline", "x11", # github actions runners don't have libxkbcommon installed, so can't use wayland ]} -bevy-inspector-egui = {version = "0.28", default-features = false, features = ["bevy_render"]} -bevy_egui = {version = "0.31", default-features = false} +bevy-inspector-egui = { version = "0.28", default-features = false, features = ["bevy_render"] } +bevy_egui = { version = "0.31", default-features = false, features = ["default_fonts"] } rand = "0.8" [[example]]