mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-14 23:53:14 +00:00
Debug toggling (#9)
#8 introduced some additional debug information to the view, which was already rendering some information and visualizations which were not necessary relevant to the end-user experience. This PR adds an input action (bound to Ctrl + `) to toggle the display of those visualizations, and disables them by default.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
use bevy_inspector_egui::quick::WorldInspectorPlugin;
|
||||
use bevy_mod_picking::debug::DebugPickingMode;
|
||||
|
||||
use crate::{
|
||||
camera_controller::{CameraController, CameraMotionSystem},
|
||||
input::{Action, InputAction, Lifecycle},
|
||||
};
|
||||
|
||||
#[derive(Resource, Deref, DerefMut)]
|
||||
pub struct DebugMode(bool);
|
||||
|
||||
pub struct DebugPlugin;
|
||||
impl Plugin for DebugPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.insert_resource(DebugMode(false))
|
||||
.add_plugins(WorldInspectorPlugin::new().run_if(is_debug_mode))
|
||||
.insert_resource(State::new(DebugPickingMode::Disabled))
|
||||
.insert_resource(GizmoConfig {
|
||||
enabled: true,
|
||||
depth_bias: -0.1,
|
||||
line_perspective: false,
|
||||
line_width: 2.,
|
||||
..default()
|
||||
})
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
toggle_debug_mode,
|
||||
update_debug_picking_mode.run_if(resource_changed::<DebugMode>()),
|
||||
update_gizmo_line_thickness,
|
||||
draw_world_axes
|
||||
.after(CameraMotionSystem)
|
||||
.run_if(is_debug_mode),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn toggle_debug_mode(mut input: EventReader<InputAction>, mut r_debug: ResMut<DebugMode>) {
|
||||
if input
|
||||
.iter()
|
||||
.any(|action| matches!(action, InputAction(Lifecycle::Stop, Action::ToggleDebug)))
|
||||
{
|
||||
**r_debug = !**r_debug;
|
||||
}
|
||||
}
|
||||
|
||||
fn update_debug_picking_mode(
|
||||
r_debug: Res<DebugMode>,
|
||||
mut s_debug_picking: ResMut<NextState<DebugPickingMode>>,
|
||||
) {
|
||||
s_debug_picking.set(match **r_debug {
|
||||
true => DebugPickingMode::Normal,
|
||||
false => DebugPickingMode::Disabled,
|
||||
});
|
||||
}
|
||||
|
||||
pub fn is_debug_mode(r_debug: Res<DebugMode>) -> bool {
|
||||
**r_debug
|
||||
}
|
||||
|
||||
fn update_gizmo_line_thickness(
|
||||
mut config: ResMut<GizmoConfig>,
|
||||
q_window: Query<&Window, (Changed<Window>, With<PrimaryWindow>)>,
|
||||
) {
|
||||
if let Ok(scale_factor) = q_window.get_single().map(|win| win.scale_factor() as f32) {
|
||||
config.line_width = 2. * scale_factor;
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_world_axes(mut gizmos: Gizmos, q: Query<&CameraController>) {
|
||||
let &CameraController { focus, radius, .. } = q.single();
|
||||
|
||||
let mut draw_axis = |direction: Vec3, color: Color| {
|
||||
let end = focus + direction * radius / 15.;
|
||||
gizmos.line(focus, end, color);
|
||||
};
|
||||
|
||||
draw_axis(Vec3::X, Color::RED);
|
||||
draw_axis(Vec3::Y, Color::GREEN);
|
||||
draw_axis(Vec3::Z, Color::BLUE);
|
||||
}
|
||||
@@ -15,6 +15,7 @@ impl Plugin for StudioInputPlugin {
|
||||
PreUpdate,
|
||||
(
|
||||
translate_selection_events,
|
||||
translate_debug_toggle,
|
||||
translate_dcc_camera_events,
|
||||
translate_fps_camera_events,
|
||||
)
|
||||
@@ -38,6 +39,7 @@ pub enum Lifecycle {
|
||||
pub enum Action {
|
||||
Camera(CameraMotion),
|
||||
Selection(Selection),
|
||||
ToggleDebug,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
@@ -83,6 +85,16 @@ fn translate_selection_events(keyboard: Res<Input<KeyCode>>, mut input: EventWri
|
||||
}
|
||||
}
|
||||
|
||||
fn translate_debug_toggle(keyboard: Res<Input<KeyCode>>, mut input: EventWriter<InputAction>) {
|
||||
if keyboard.pressed(KeyCode::ControlLeft) || keyboard.pressed(KeyCode::ControlRight) {
|
||||
if keyboard.just_pressed(KeyCode::Grave) {
|
||||
input.send(InputAction(Lifecycle::Start, Action::ToggleDebug));
|
||||
} else if keyboard.just_released(KeyCode::Grave) {
|
||||
input.send(InputAction(Lifecycle::Stop, Action::ToggleDebug));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn translate_dcc_camera_events(
|
||||
mouse_motion: EventReader<MouseMotion>,
|
||||
mouse_buttons: Res<Input<MouseButton>>,
|
||||
|
||||
@@ -6,6 +6,7 @@ use bevy_mod_picking::{prelude::Pointer, selection::Select};
|
||||
|
||||
use crate::{
|
||||
camera_controller::CameraMotionSystem,
|
||||
debug,
|
||||
input::{self, Action, InputAction, Lifecycle},
|
||||
};
|
||||
|
||||
@@ -29,6 +30,7 @@ impl Plugin for InteractionPlugin {
|
||||
draw_selection_outline.run_if(resource_changed_or_removed::<Selection>()),
|
||||
draw_selection_axes
|
||||
.run_if(resource_exists::<Selection>())
|
||||
.run_if(debug::is_debug_mode)
|
||||
.after(CameraMotionSystem),
|
||||
),
|
||||
);
|
||||
|
||||
+5
-40
@@ -1,9 +1,9 @@
|
||||
#![allow(clippy::type_complexity, clippy::too_many_arguments)]
|
||||
|
||||
use bevy::{prelude::*, window::PrimaryWindow};
|
||||
use bevy_inspector_egui::quick::WorldInspectorPlugin;
|
||||
use bevy::prelude::*;
|
||||
use bevy_mod_picking::DefaultPickingPlugins;
|
||||
use camera_controller::{CameraController, CameraControllerPlugin, CameraMotionSystem};
|
||||
use camera_controller::CameraControllerPlugin;
|
||||
use debug::DebugPlugin;
|
||||
use input::StudioInputPlugin;
|
||||
use interaction::InteractionPlugin;
|
||||
use math::InterpolationPlugin;
|
||||
@@ -14,6 +14,7 @@ use viewport::ViewportSize;
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
mod camera_controller;
|
||||
mod debug;
|
||||
mod input;
|
||||
mod interaction;
|
||||
mod math;
|
||||
@@ -42,7 +43,7 @@ pub fn init(viewport_size: ViewportSize) {
|
||||
..default()
|
||||
}),
|
||||
DefaultPickingPlugins,
|
||||
WorldInspectorPlugin::new(),
|
||||
DebugPlugin,
|
||||
InteractionPlugin,
|
||||
StudioInputPlugin,
|
||||
InterpolationPlugin,
|
||||
@@ -51,41 +52,5 @@ pub fn init(viewport_size: ViewportSize) {
|
||||
SkyboxPlugin,
|
||||
UiLayerPlugin,
|
||||
))
|
||||
.insert_resource(GizmoConfig {
|
||||
enabled: true,
|
||||
depth_bias: -0.1,
|
||||
line_perspective: false,
|
||||
line_width: 2.,
|
||||
..default()
|
||||
})
|
||||
.add_systems(
|
||||
Update,
|
||||
(
|
||||
update_gizmo_line_thickness,
|
||||
draw_world_axes.after(CameraMotionSystem),
|
||||
),
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
fn update_gizmo_line_thickness(
|
||||
mut config: ResMut<GizmoConfig>,
|
||||
q_window: Query<&Window, (Changed<Window>, With<PrimaryWindow>)>,
|
||||
) {
|
||||
if let Ok(scale_factor) = q_window.get_single().map(|win| win.scale_factor() as f32) {
|
||||
config.line_width = 2. * scale_factor;
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_world_axes(mut gizmos: Gizmos, q: Query<&CameraController>) {
|
||||
let &CameraController { focus, radius, .. } = q.single();
|
||||
|
||||
let mut draw_axis = |direction: Vec3, color: Color| {
|
||||
let end = focus + direction * radius / 15.;
|
||||
gizmos.line(focus, end, color);
|
||||
};
|
||||
|
||||
draw_axis(Vec3::X, Color::RED);
|
||||
draw_axis(Vec3::Y, Color::GREEN);
|
||||
draw_axis(Vec3::Z, Color::BLUE);
|
||||
}
|
||||
|
||||
+38
-16
@@ -3,13 +3,19 @@ use bevy::{
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
use crate::debug::DebugMode;
|
||||
|
||||
pub(super) struct FpsCounterPlugin;
|
||||
|
||||
impl Plugin for FpsCounterPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(FrameTimeDiagnosticsPlugin)
|
||||
.add_systems(Startup, layout)
|
||||
.add_systems(Update, update);
|
||||
.add_systems(Update, update)
|
||||
.add_systems(
|
||||
Update,
|
||||
update_visibility.run_if(resource_exists_and_changed::<DebugMode>()),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,24 +23,28 @@ impl Plugin for FpsCounterPlugin {
|
||||
struct FramerateCounter;
|
||||
|
||||
fn layout(mut cmd: Commands) {
|
||||
let mut text_bundle = TextBundle::from_section(
|
||||
"0 fps",
|
||||
TextStyle {
|
||||
color: Color::GREEN,
|
||||
font_size: 14.,
|
||||
..default()
|
||||
},
|
||||
)
|
||||
.with_text_alignment(TextAlignment::Right)
|
||||
.with_style(Style {
|
||||
position_type: PositionType::Absolute,
|
||||
right: Val::Px(16.),
|
||||
bottom: Val::Px(16.),
|
||||
..default()
|
||||
});
|
||||
|
||||
text_bundle.visibility = Visibility::Hidden;
|
||||
|
||||
cmd.spawn((
|
||||
FramerateCounter,
|
||||
Name::new("Framerate Counter"),
|
||||
TextBundle::from_section(
|
||||
"0 fps",
|
||||
TextStyle {
|
||||
color: Color::GREEN,
|
||||
font_size: 14.,
|
||||
..default()
|
||||
},
|
||||
)
|
||||
.with_text_alignment(TextAlignment::Right)
|
||||
.with_style(Style {
|
||||
position_type: PositionType::Absolute,
|
||||
right: Val::Px(16.),
|
||||
bottom: Val::Px(16.),
|
||||
..default()
|
||||
}),
|
||||
text_bundle,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -53,3 +63,15 @@ fn update(
|
||||
counter.single_mut().sections[0].value = format!("{fps:.2} fps");
|
||||
}
|
||||
}
|
||||
|
||||
fn update_visibility(
|
||||
r_debug: Res<DebugMode>,
|
||||
mut q_visibility: Query<&mut Visibility, With<FramerateCounter>>,
|
||||
) {
|
||||
for mut visibility in q_visibility.iter_mut() {
|
||||
*visibility = match **r_debug {
|
||||
true => Visibility::Inherited,
|
||||
false => Visibility::Hidden,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user