From ca230f6a9b924818e78053ac5f745aa1d6b4d499 Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Thu, 22 Sep 2022 10:32:09 +0200 Subject: [PATCH] Replace bevy_pancam with custom Plugin --- Cargo.lock | 10 -------- Cargo.toml | 6 +---- src/components/mod.rs | 7 ++---- src/components/panning/mod.rs | 12 +++++++++ src/main.rs | 25 ++++++++----------- src/plugins/mod.rs | 2 ++ src/plugins/panning_plugin.rs | 47 +++++++++++++++++++++++++++++++++++ src/plugins/world_plugins.rs | 4 +-- 8 files changed, 77 insertions(+), 36 deletions(-) create mode 100644 src/components/panning/mod.rs create mode 100644 src/plugins/panning_plugin.rs diff --git a/Cargo.lock b/Cargo.lock index 3c71dd0..2061620 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,15 +427,6 @@ dependencies = [ "glam", ] -[[package]] -name = "bevy_pancam" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ebc698de3f4e824a67f517fe9cca35f08d5fdaab99ed8e141102c582768fc62" -dependencies = [ - "bevy", -] - [[package]] name = "bevy_pbr" version = "0.8.1" @@ -2771,7 +2762,6 @@ name = "worlds-sim-rust" version = "0.1.0" dependencies = [ "bevy", - "bevy_pancam", "planet", ] diff --git a/Cargo.toml b/Cargo.toml index 7db6bdf..5309f83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ resolver = "2" [features] logging = ["planet/logging"] globe_view = ["planet/globe_view", "render"] -render = ["bevy/bevy_asset", "bevy/bevy_winit", "bevy/x11", "bevy/wayland", "bevy/render", "planet/render", "dep:bevy_pancam"] +render = ["bevy/bevy_asset", "bevy/bevy_winit", "bevy/x11", "bevy/wayland", "bevy/render", "planet/render"] default = ["render", "logging"] [dependencies.planet] @@ -17,7 +17,3 @@ default-features = false [dependencies.bevy] version = "0.8" default-features = false - -[dependencies.bevy_pancam] -version = "0.6.1" -optional = true diff --git a/src/components/mod.rs b/src/components/mod.rs index 8c5fc1d..e44bb60 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -1,6 +1,3 @@ pub(crate) mod markers; - -pub(crate) mod third_party { - #[cfg(feature = "render")] - pub(crate) use bevy_pancam::PanCam; -} +#[cfg(feature = "render")] +pub(crate) mod panning; diff --git a/src/components/panning/mod.rs b/src/components/panning/mod.rs new file mode 100644 index 0000000..128c1b9 --- /dev/null +++ b/src/components/panning/mod.rs @@ -0,0 +1,12 @@ +use bevy::ecs::component::Component; + +#[derive(Component)] +pub(crate) struct Pan2d { + pub(crate) enabled: bool, +} +impl Pan2d { + #[must_use] + pub(crate) const fn new() -> Pan2d { + Pan2d { enabled: true } + } +} diff --git a/src/main.rs b/src/main.rs index 05a2eb6..ec0d105 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,7 +89,7 @@ use { }, components::{ markers::{InfoPanel, ToolbarButton}, - third_party::PanCam, + panning::Pan2d, }, planet::BiomeStats, resources::CursorMapPosition, @@ -198,7 +198,7 @@ fn handle_toolbar_button( #[cfg(feature = "globe_view")] mut camera_2d_query: Query< '_, '_, - (&mut Camera, &mut PanCam), + (&mut Camera, &mut Pan2d), (With, Without), >, #[cfg(feature = "globe_view")] mut materials: ResMut<'_, Assets>, @@ -529,20 +529,17 @@ fn generate_graphics( }); } + _ = commands.spawn_bundle(Camera2dBundle::default()); _ = commands - .spawn_bundle(Camera2dBundle { ..default() }) - .insert(PanCam { - max_scale: Some(80.0), + .spawn_bundle(SpriteBundle { + texture: images.get_handle(world_manager.map_image_handle_id.unwrap()), + sprite: Sprite { + custom_size: Some(custom_sprite_size), + ..default() + }, ..default() - }); - _ = commands.spawn_bundle(SpriteBundle { - texture: images.get_handle(world_manager.map_image_handle_id.unwrap()), - sprite: Sprite { - custom_size: Some(custom_sprite_size), - ..default() - }, - ..default() - }); + }) + .insert(Pan2d::new()); _ = commands .spawn_bundle(NodeBundle { diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index 6895c63..99aa3ce 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -1,2 +1,4 @@ +#[cfg(feature = "render")] +pub(crate) mod panning_plugin; pub(crate) mod world_plugins; pub(crate) use world_plugins::WorldPlugins; diff --git a/src/plugins/panning_plugin.rs b/src/plugins/panning_plugin.rs new file mode 100644 index 0000000..5f447bc --- /dev/null +++ b/src/plugins/panning_plugin.rs @@ -0,0 +1,47 @@ +use { + crate::components::panning::Pan2d, + bevy::{ + app::{App, Plugin}, + ecs::{ + event::EventReader, + query::With, + system::{Query, Res}, + }, + input::{ + mouse::{MouseButton, MouseMotion}, + Input, + }, + sprite::Sprite, + transform::components::Transform, + }, +}; + +#[derive(Default)] +pub(crate) struct PanningPlugin; + +impl Plugin for PanningPlugin { + fn build(&self, app: &mut App) { + _ = app.add_system(panning_system_2d); + } +} + +fn panning_system_2d( + mut query: Query<'_, '_, (&mut Transform, &Pan2d), With>, + mut mouse_motion_events: EventReader<'_, '_, MouseMotion>, + input_mouse: Res<'_, Input>, +) { + if !input_mouse.pressed(MouseButton::Left) { + return; + } + + let mut horizontal = 0.0; + for movement in mouse_motion_events.iter() { + horizontal += movement.delta.x; + } + + query.for_each_mut(|(mut transform, pan)| { + if pan.enabled { + transform.translation.x += horizontal; + } + }); +} diff --git a/src/plugins/world_plugins.rs b/src/plugins/world_plugins.rs index 7a1fe08..5926f1b 100644 --- a/src/plugins/world_plugins.rs +++ b/src/plugins/world_plugins.rs @@ -18,6 +18,7 @@ impl PluginGroup for WorldPlugins { #[cfg(feature = "render")] { use { + crate::plugins::panning_plugin::PanningPlugin, bevy::{ asset::AssetPlugin, core_pipeline::CorePipelinePlugin, @@ -31,7 +32,6 @@ impl PluginGroup for WorldPlugins { window::WindowPlugin, winit::WinitPlugin, }, - bevy_pancam::PanCamPlugin, }; _ = group @@ -47,7 +47,7 @@ impl PluginGroup for WorldPlugins { .add(SpritePlugin::default()) .add(TextPlugin::default()) .add(UiPlugin::default()) - .add(PanCamPlugin::default()); + .add(PanningPlugin::default()); #[cfg(feature = "globe_view")] { use bevy::pbr::PbrPlugin;