-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
192 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use crate::app_state::*; | ||
use bevy::{ | ||
input::mouse::{MouseMotion, MouseScrollUnit, MouseWheel}, | ||
prelude::*, | ||
}; | ||
use iyes_loopless::prelude::{AppLooplessStateExt, IntoConditionalSystem}; | ||
use smooth_bevy_cameras::{ | ||
controllers::orbit::{ControlEvent, OrbitCameraController}, | ||
LookTransform, | ||
}; | ||
|
||
pub struct CameraControlPlugin; | ||
|
||
impl Plugin for CameraControlPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_enter_system(AppState::InGame, reset_camera) | ||
.add_exit_system(AppState::InGame, freeze_camera) | ||
.add_system(control_camera.run_in_state(AppState::InGame)); | ||
} | ||
} | ||
|
||
fn reset_camera(mut query: Query<(&mut OrbitCameraController, &mut LookTransform)>) { | ||
for (mut orbit, mut look) in query.iter_mut() { | ||
orbit.enabled = true; | ||
look.target = Vec3::default(); | ||
look.eye = Vec3::new(5., 10., 5.); | ||
} | ||
} | ||
|
||
fn freeze_camera(mut query: Query<(&mut OrbitCameraController, &mut LookTransform)>) { | ||
for (mut orbit, mut look) in query.iter_mut() { | ||
orbit.enabled = false; | ||
look.target = Vec3::default(); | ||
look.eye = Vec3::new(5., 10., 5.); | ||
} | ||
} | ||
|
||
fn control_camera( | ||
mut events: EventWriter<ControlEvent>, | ||
keys: Res<Input<KeyCode>>, | ||
mut mouse_wheel: EventReader<MouseWheel>, | ||
mouse_buttons: Res<Input<MouseButton>>, | ||
mut mouse_motion_events: EventReader<MouseMotion>, | ||
controllers: Query<&OrbitCameraController>, | ||
) { | ||
let controller = if let Some(controller) = controllers.iter().find(|c| c.enabled) { | ||
controller | ||
} else { | ||
return; | ||
}; | ||
|
||
let mut cursor_delta = Vec2::ZERO; | ||
for event in mouse_motion_events.iter() { | ||
cursor_delta += event.delta; | ||
} | ||
|
||
if keys.any_pressed([KeyCode::LControl, KeyCode::RControl]) { | ||
// Orbit Mode | ||
if keys.any_pressed([KeyCode::Left, KeyCode::A]) { | ||
events.send(ControlEvent::Orbit(Vec2::new(1., 0.))); | ||
} | ||
if keys.any_pressed([KeyCode::Right, KeyCode::D]) { | ||
events.send(ControlEvent::Orbit(Vec2::new(-1., 0.))); | ||
} | ||
if keys.any_pressed([KeyCode::Up, KeyCode::W]) { | ||
events.send(ControlEvent::Orbit(Vec2::new(0., 1.))); | ||
} | ||
if keys.any_pressed([KeyCode::Down, KeyCode::S]) { | ||
events.send(ControlEvent::Orbit(Vec2::new(0., -1.))); | ||
} | ||
} else { | ||
// Move Mode | ||
if keys.any_pressed([KeyCode::Left, KeyCode::A]) { | ||
events.send(ControlEvent::TranslateTarget(Vec2::new(1., 0.))); | ||
} | ||
if keys.any_pressed([KeyCode::Right, KeyCode::D]) { | ||
events.send(ControlEvent::TranslateTarget(Vec2::new(-1., 0.))); | ||
} | ||
if keys.any_pressed([KeyCode::Up, KeyCode::W]) { | ||
events.send(ControlEvent::TranslateTarget(Vec2::new(0., 1.))); | ||
} | ||
if keys.any_pressed([KeyCode::Down, KeyCode::S]) { | ||
events.send(ControlEvent::TranslateTarget(Vec2::new(0., -1.))); | ||
} | ||
} | ||
|
||
if keys.any_pressed([KeyCode::NumpadSubtract, KeyCode::Minus]) { | ||
events.send(ControlEvent::Zoom(1.01)); | ||
} | ||
if keys.any_pressed([KeyCode::NumpadAdd, KeyCode::Equals]) { | ||
events.send(ControlEvent::Zoom(0.99)); | ||
} | ||
|
||
if mouse_buttons.pressed(MouseButton::Middle) { | ||
events.send(ControlEvent::TranslateTarget( | ||
controller.mouse_translate_sensitivity * cursor_delta, | ||
)); | ||
} | ||
|
||
if mouse_buttons.pressed(MouseButton::Right) { | ||
events.send(ControlEvent::Orbit( | ||
controller.mouse_rotate_sensitivity * cursor_delta, | ||
)); | ||
} | ||
|
||
for ev in mouse_wheel.iter() { | ||
let mut scalar = ev.y * controller.mouse_wheel_zoom_sensitivity; | ||
if ev.unit == MouseScrollUnit::Line { | ||
scalar /= controller.pixels_per_line; | ||
} | ||
events.send(ControlEvent::Zoom(1. - scalar)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use bevy::{ecs::schedule::ShouldRun, prelude::*}; | ||
use bevy_mod_picking::{ | ||
mesh_events_system, mesh_focus, pause_for_picking_blockers, CustomHighlightPlugin, | ||
DefaultHighlighting, PausedForBlockers, PickingEvent, PickingPlugin, PickingPluginsState, | ||
PickingSystem, | ||
}; | ||
|
||
pub struct CustomPickingPlugin; | ||
|
||
fn simple_criteria(flag: bool) -> ShouldRun { | ||
if flag { | ||
ShouldRun::Yes | ||
} else { | ||
ShouldRun::No | ||
} | ||
} | ||
|
||
impl Plugin for CustomPickingPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_plugin(PickingPlugin) | ||
.add_plugin(CustomHighlightPlugin::<StandardMaterial> { | ||
highlighting_default: |mut assets| DefaultHighlighting { | ||
hovered: assets.add(Color::rgb(0.35, 0.35, 0.35).into()), | ||
pressed: assets.add(Color::rgb(0.35, 0.75, 0.35).into()), | ||
selected: assets.add(Color::rgb(0.35, 0.35, 0.75).into()), | ||
}, | ||
}) | ||
.add_event::<PickingEvent>() | ||
.init_resource::<PausedForBlockers>() | ||
.add_system_set_to_stage( | ||
CoreStage::First, | ||
SystemSet::new() | ||
.with_run_criteria(|state: Res<PickingPluginsState>| { | ||
simple_criteria(state.enable_interacting) | ||
}) | ||
.with_system( | ||
pause_for_picking_blockers | ||
.label(PickingSystem::PauseForBlockers) | ||
.after(PickingSystem::UpdateIntersections), | ||
) | ||
.with_system( | ||
mesh_focus | ||
.label(PickingSystem::Focus) | ||
.after(PickingSystem::PauseForBlockers), | ||
) | ||
.with_system( | ||
mesh_events_system | ||
.label(PickingSystem::Events) | ||
.after(PickingSystem::Selection), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters