From e0e341214c35347a30c3e76265d216396abc2cfb Mon Sep 17 00:00:00 2001 From: Crozet Sébastien Date: Wed, 2 Jun 2021 16:00:23 +0200 Subject: Update dependencies --- src_testbed/camera2d.rs | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src_testbed/camera2d.rs (limited to 'src_testbed/camera2d.rs') diff --git a/src_testbed/camera2d.rs b/src_testbed/camera2d.rs new file mode 100644 index 0000000..81b3263 --- /dev/null +++ b/src_testbed/camera2d.rs @@ -0,0 +1,95 @@ +// NOTE: this is inspired from the `bevy-orbit-controls` projects but +// with some modifications like Panning, and 2D support. +// Most of these modifications have been contributed upstream. +use bevy::input::mouse::MouseMotion; +use bevy::input::mouse::MouseScrollUnit::{Line, Pixel}; +use bevy::input::mouse::MouseWheel; +use bevy::prelude::*; +use bevy::render::camera::Camera; + +const LINE_TO_PIXEL_RATIO: f32 = 0.1; + +pub struct OrbitCamera { + pub zoom: f32, + pub center: Vec3, + pub pan_sensitivity: f32, + pub zoom_sensitivity: f32, + pub pan_button: MouseButton, + pub enabled: bool, +} + +impl Default for OrbitCamera { + fn default() -> Self { + OrbitCamera { + zoom: 100.0, + center: Vec3::ZERO, + pan_sensitivity: 1.0, + zoom_sensitivity: 0.8, + pan_button: MouseButton::Right, + enabled: true, + } + } +} + +// Adapted from the 3D orbit camera from bevy-orbit-controls +pub struct OrbitCameraPlugin; +impl OrbitCameraPlugin { + fn update_transform_system( + mut query: Query<(&OrbitCamera, &mut Transform), (Changed, With)>, + ) { + for (camera, mut transform) in query.iter_mut() { + if camera.enabled { + transform.translation = camera.center; + transform.scale = Vec3::new(1.0 / camera.zoom, 1.0 / camera.zoom, 1.0); + } + } + } + + fn mouse_motion_system( + _time: Res