Replace bevy_pancam with custom Plugin

This commit is contained in:
Tobias Berger 2022-09-22 10:32:09 +02:00
parent 162850e263
commit ca230f6a9b
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
8 changed files with 77 additions and 36 deletions

10
Cargo.lock generated
View file

@ -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",
]

View file

@ -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

View file

@ -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;

View file

@ -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 }
}
}

View file

@ -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<Camera2d>, Without<Camera3d>),
>,
#[cfg(feature = "globe_view")] mut materials: ResMut<'_, Assets<StandardMaterial>>,
@ -529,20 +529,17 @@ fn generate_graphics(
});
}
_ = commands.spawn_bundle(Camera2dBundle::default());
_ = commands
.spawn_bundle(Camera2dBundle { ..default() })
.insert(PanCam {
max_scale: Some(80.0),
..default()
});
_ = commands.spawn_bundle(SpriteBundle {
.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 {

View file

@ -1,2 +1,4 @@
#[cfg(feature = "render")]
pub(crate) mod panning_plugin;
pub(crate) mod world_plugins;
pub(crate) use world_plugins::WorldPlugins;

View file

@ -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<Sprite>>,
mut mouse_motion_events: EventReader<'_, '_, MouseMotion>,
input_mouse: Res<'_, Input<MouseButton>>,
) {
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;
}
});
}

View file

@ -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;