Skip to content

Commit

Permalink
cleanup leafwing usage and add zoom action
Browse files Browse the repository at this point in the history
  • Loading branch information
luan committed Feb 9, 2024
1 parent 9606ebc commit 1f8e76b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/johanhelsing/bevy_pancam"
version = "0.10.0"

[features]
default = ["leafwing-input-manager"]
default = []
bevy_egui = ["dep:bevy_egui"]
leafwing-input-manager = ["dep:leafwing-input-manager"]

Expand Down
9 changes: 7 additions & 2 deletions examples/leafwing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use bevy::prelude::*;
use bevy_pancam::{PanCam, PanCamPlugin};
use leafwing_input_manager::{
action_state::ActionState, input_map::InputMap, user_input::InputKind, InputManagerBundle,
action_state::ActionState,
axislike::SingleAxis,
input_map::InputMap,
user_input::{InputKind, Modifier},
InputManagerBundle,
};
use rand::prelude::random;

Expand All @@ -16,11 +20,12 @@ fn setup(mut commands: Commands) {
let mut input_map = InputMap::default();
input_map.insert_chord(
[
InputKind::Modifier(Modifier::Alt),
InputKind::Mouse(MouseButton::Left),
InputKind::Keyboard(KeyCode::AltLeft),
],
bevy_pancam::Action::Grab,
);
input_map.insert(SingleAxis::mouse_wheel_y(), bevy_pancam::Action::Zoom);
commands.spawn((
Camera2dBundle::default(),
PanCam::default(),
Expand Down
42 changes: 26 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]

use bevy::{
input::mouse::{MouseScrollUnit, MouseWheel},
math::vec2,
prelude::*,
render::camera::CameraProjection,
window::PrimaryWindow,
};
use bevy::{math::vec2, prelude::*, render::camera::CameraProjection, window::PrimaryWindow};

#[cfg(not(feature = "leafwing-input-manager"))]
use bevy::input::mouse::{MouseScrollUnit, MouseWheel};

#[cfg(feature = "leafwing-input-manager")]
use leafwing_input_manager::{action_state::ActionState, plugin::InputManagerPlugin, Actionlike};
Expand Down Expand Up @@ -67,17 +64,28 @@ fn check_egui_wants_focus(

fn camera_zoom(
mut query: Query<(&PanCam, &mut OrthographicProjection, &mut Transform)>,
mut scroll_events: EventReader<MouseWheel>,
#[cfg(not(feature = "leafwing-input-manager"))] mut scroll_events: EventReader<MouseWheel>,
#[cfg(feature = "leafwing-input-manager")] action_query: Query<&ActionState<Action>>,
primary_window: Query<&Window, With<PrimaryWindow>>,
) {
let pixels_per_line = 100.; // Maybe make configurable?
let scroll = scroll_events
.read()
.map(|ev| match ev.unit {
MouseScrollUnit::Pixel => ev.y,
MouseScrollUnit::Line => ev.y * pixels_per_line,
})
.sum::<f32>();
#[cfg(not(feature = "leafwing-input-manager"))]
let scroll = {
let pixels_per_line = 100.; // Maybe make configurable?
scroll_events
.read()
.map(|ev| match ev.unit {
MouseScrollUnit::Pixel => ev.y,
MouseScrollUnit::Line => ev.y * pixels_per_line,
})
.sum::<f32>()
};

#[cfg(feature = "leafwing-input-manager")]
let scroll = if let Ok(action_state) = action_query.get_single() {
action_state.value(Action::Zoom)
} else {
0.
};

if scroll == 0. {
return;
Expand Down Expand Up @@ -266,6 +274,8 @@ fn camera_movement(
pub enum Action {
/// Action to grab the camera
Grab,
/// Action to zoom in and out
Zoom,
}

/// A component that adds panning camera controls to an orthographic camera
Expand Down

0 comments on commit 1f8e76b

Please sign in to comment.