bevy_debug_camera/lib.rs
1//! This library provides a simple, lightweight way of having a "flycam" style camera for
2//! debugging. It's confirgurable, letting you easily enable the system locally for a camera or
3//! globally for all cameras with a component and resource.
4//!
5//! # Bevy compatibility
6//!
7//! | Bevy Version | bevy-debug-camera version |
8//! |--------------|---------------------------|
9//! | 0.9.1 | ^0.1.0 |
10//! | ^0.10.0 | ^0.2.0 |
11//! | ^0.11.0 | ^0.3.0 |
12//!
13//! # Examples
14//!
15//! You can look at the examples folder for
16//! practical uses of this crate, but to get started you can simply do the following when setting
17//! up your app:
18//!
19//! ```no_run
20//! use bevy::prelude::*;
21//! use bevy_debug_camera::{DebugCamera, DebugCameraPlugin};
22//!
23//! fn main() {
24//! App::new()
25//! .add_plugins(DefaultPlugins)
26//! .add_plugin(DebugCameraPlugin::default())
27//! .add_startup_system(setup)
28//! .run();
29//! }
30//!
31//! fn setup(mut commands: Commands) {
32//! // ... other setup code
33//! commands
34//! .spawn(Camera3dBundle::default())
35//! .insert(DebugCamera {
36//! position: Vec3::new(-5., 1., 0.),
37//! ..default()
38//! });
39//! }
40//! ```
41//!
42//! # Bindings
43//!
44//! The default bindings are as follows:
45//!
46//! ## Mouse + Keyboard
47//!
48//! | Action | Binding |
49//! |---------------|----------|
50//! | Move forward | `W` |
51//! | Move backward | `S` |
52//! | Move left | `A` |
53//! | Move right | `D` |
54//! | Move up | `Lshift` |
55//! | Move down | `Space` |
56//! | Yaw | Mouse X |
57//! | pitch | Mouse Y |
58//! | Roll left | `Q` |
59//! | Roll right | `E` |
60//!
61//! ## Controller
62//!
63//! | Action | Binding |
64//! |-----------------|------------|
65//! | Move fwd/bwd | Lstick Y |
66//! | Move left/right | Lstick X |
67//! | Move up | `RTrigger` |
68//! | Move down | `LTrigger` |
69//! | Yaw | Rstick X |
70//! | pitch | Lstick Y |
71//! | Roll left | `LBumper` |
72//! | Roll right | `RBumper` |
73//!
74//! # Configuring Plugin
75//!
76//! The plugin comes with some configuration options you can set on startup that use to customise
77//! behaviour of the cameras in use. You can configure:
78//!
79//! * Keyboard bindings
80//! * Gamepad bindings
81//! * Accepted input
82//!
83//! All these customisation are exposed as resources, which are constantly read and can be modified
84//! during runtime as well An example using all configuration options can be seen below and in the
85//! `configuration` example:
86//!
87//! ```no_run
88//! use bevy::prelude::*;
89//! use bevy_debug_camera::{
90//! DebugCamera, DebugCameraPlugin, GamepadBindings, KeyboardBindings, DebugCameraActive,
91//! };
92//!
93//! fn main() {
94//! App::new()
95//! .add_plugins(DefaultPlugins)
96//! // Each field in `DebugCameraPlugin` can be set directly or picked up from
97//! // default.
98//! .add_plugin(DebugCameraPlugin {
99//! gamepad_bindings: GamepadBindings {
100//! // Overrides only the roll buttons
101//! roll_left: GamepadButtonType::West,
102//! roll_right: GamepadButtonType::East,
103//! ..default()
104//! },
105//! keyboard_bindings: KeyboardBindings {
106//! // Override WASD with arrows
107//! fwd: KeyCode::Up,
108//! bwd: KeyCode::Down,
109//! left: KeyCode::Left,
110//! right: KeyCode::Right,
111//! ..default()
112//! },
113//! debug_camera_active: DebugCameraActive {
114//! // Disable keyboard + mouse only
115//! keymouse: false,
116//! ..default()
117//! },
118//! })
119//! .add_startup_system(setup)
120//! .run();
121//! }
122//!
123//! fn setup() {
124//! // Setup logic here...
125//! }
126//! ```
127
128mod components;
129mod resources;
130mod systems;
131
132pub use components::DebugCamera;
133pub use resources::{ActiveGamepad, DebugCameraActive, GamepadBindings, KeyboardBindings};
134
135use bevy::prelude::*;
136
137#[derive(Debug, Default)]
138pub struct DebugCameraPlugin {
139 pub gamepad_bindings: resources::GamepadBindings,
140 pub keyboard_bindings: resources::KeyboardBindings,
141 pub debug_camera_active: resources::DebugCameraActive,
142}
143
144impl Plugin for DebugCameraPlugin {
145 fn build(&self, app: &mut App) {
146 app.insert_resource(resources::ActiveGamepad::default())
147 .insert_resource(self.debug_camera_active.clone())
148 .insert_resource(self.gamepad_bindings.clone())
149 .insert_resource(self.keyboard_bindings.clone())
150 .add_systems(Update, systems::camera_movement_system)
151 .add_systems(Update, systems::camera_update_system)
152 .add_systems(Update, systems::cursor_grab_system)
153 .add_systems(Update, systems::gamepad_connections);
154 }
155}