You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
218 lines
6.7 KiB
Rust
218 lines
6.7 KiB
Rust
use bevy::{
|
|
color::palettes::css::MAGENTA,
|
|
pbr::wireframe::{WireframeConfig, WireframePlugin}, platform::collections::HashMap,
|
|
};
|
|
|
|
use super::*;
|
|
|
|
/// Debugging systems, resources, events, etc.
|
|
pub struct DebuggingPlugin;
|
|
|
|
impl Plugin for DebuggingPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.init_state::<DebuggingState>()
|
|
.init_resource::<ToolTip>()
|
|
.add_plugins(RapierDebugRenderPlugin::default().disabled())
|
|
.add_plugins(WireframePlugin::default())
|
|
.insert_resource(WireframeConfig {
|
|
global: false,
|
|
default_color: MAGENTA.into(),
|
|
})
|
|
.add_systems(Startup, init_debug_ui)
|
|
.add_systems(OnEnter(DebuggingState::On), enable_wireframe)
|
|
.add_systems(OnExit(DebuggingState::On), disable_wireframe)
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
(
|
|
toggle_state_visibility::<DebuggingState>,
|
|
toggle_rapier_debug_render,
|
|
)
|
|
.run_if(state_changed::<DebuggingState>),
|
|
toggle_debug_state.run_if(on_keyboard_press(KeyCode::F12)),
|
|
(
|
|
(hover_mesh, hover_ui).run_if(on_event::<Pointer<Over>>.or(on_event::<Pointer<Out>>)),
|
|
tooltip_follow.run_if(any_component_changed::<Window>),
|
|
sync_resource_to_ui::<ToolTip>.run_if(resource_changed::<ToolTip>),
|
|
)
|
|
.run_if(in_state(DebuggingState::On)),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Tracks if the debugger is on or off for other games systems to hook into
|
|
///
|
|
/// The Debugging state may add it's own global debugging information, but is mostly a shell
|
|
#[derive(States, Component, Default, Debug, Clone, Hash, Eq, PartialEq)]
|
|
pub enum DebuggingState {
|
|
#[default]
|
|
Off,
|
|
On,
|
|
}
|
|
|
|
/// Create the Debugging UI
|
|
fn init_debug_ui(mut commands: Commands) {
|
|
commands.spawn((
|
|
DebuggingState::On,
|
|
Text(" Debug: ON ".into()),
|
|
TextColor(WHITE.into()),
|
|
BackgroundColor(RED.into()),
|
|
BorderRadius::MAX,
|
|
Node {
|
|
align_self: AlignSelf::Center,
|
|
justify_self: JustifySelf::End,
|
|
..default()
|
|
},
|
|
));
|
|
|
|
commands.spawn((
|
|
DebuggingState::On,
|
|
Text("Tooltip Placeholder".into()),
|
|
TextColor(WHITE.into()),
|
|
SyncResource::<ToolTip>::default(),
|
|
BackgroundColor(BLACK.with_alpha(0.9).into()),
|
|
GlobalZIndex(i32::MAX),
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
margin: UiRect {
|
|
left: Val::Px(20.0),
|
|
..default()
|
|
},
|
|
align_content: AlignContent::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..default()
|
|
},
|
|
));
|
|
}
|
|
|
|
/// Toggles the debug state from off -> on // off -> on when triggered
|
|
fn toggle_debug_state(
|
|
mut next: ResMut<NextState<DebuggingState>>,
|
|
curr: Res<State<DebuggingState>>,
|
|
) {
|
|
use DebuggingState::*;
|
|
|
|
next.set(match curr.get() {
|
|
On => Off,
|
|
Off => On,
|
|
});
|
|
debug!("Toggling debug state: {:?} -> {:?}", curr, next);
|
|
}
|
|
|
|
/// Simple system that enables/disables rapier debug visuals when the debugging state changes
|
|
fn toggle_rapier_debug_render(
|
|
state: Res<State<DebuggingState>>,
|
|
mut context: ResMut<DebugRenderContext>,
|
|
) {
|
|
context.enabled = *state.get() == DebuggingState::On;
|
|
}
|
|
|
|
/// Add a generic Tooltip that follows the mouse in debug mode
|
|
#[derive(Default, Resource)]
|
|
pub struct ToolTip(HashMap<String, String>);
|
|
|
|
impl ToolTip {
|
|
pub fn insert(&mut self, k: &str, v: String) {
|
|
self.0.insert(k.into(), v);
|
|
}
|
|
|
|
pub fn remove(&mut self, k: &str) {
|
|
self.0.remove(k);
|
|
}
|
|
}
|
|
|
|
impl Display for ToolTip {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
for (k, v) in self.0.iter() {
|
|
write!(f, "{k}: {v}\n")?
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn tooltip_follow(
|
|
mut tooltip: Single<(&mut Node, &mut Visibility), With<SyncResource<ToolTip>>>,
|
|
window: Single<&Window>,
|
|
) {
|
|
if let Some(Vec2 { x, y }) = window.cursor_position() {
|
|
*tooltip.1 = Visibility::Inherited;
|
|
tooltip.0.left = Val::Px(x);
|
|
tooltip.0.top = Val::Px(y);
|
|
} else {
|
|
*tooltip.1 = Visibility::Hidden;
|
|
}
|
|
}
|
|
|
|
/// When you hover over a mesh, update the tooltip with some info
|
|
fn hover_mesh(
|
|
mut over_events: EventReader<Pointer<Over>>,
|
|
mut out_events: EventReader<Pointer<Out>>,
|
|
mut tooltip: ResMut<ToolTip>,
|
|
meshes: Query<(&Transform, Option<&Name>), With<Mesh3d>>,
|
|
) {
|
|
out_events
|
|
.read()
|
|
.filter_map(|Pointer { target, .. }| meshes.contains(*target).then_some(*target))
|
|
.for_each(|_| {
|
|
tooltip.remove("ID");
|
|
tooltip.remove("Pos");
|
|
tooltip.remove("Name");
|
|
});
|
|
over_events
|
|
.read()
|
|
.filter_map(|Pointer { target, .. }| meshes.contains(*target).then_some(*target))
|
|
.for_each(|e| {
|
|
if let Ok((t, n)) = meshes.get(e) {
|
|
let pos = (t.translation.x, t.translation.y, t.translation.z);
|
|
let name = match n {
|
|
Some(x) => x,
|
|
None => "???",
|
|
};
|
|
tooltip.insert("ID", format!("{e}"));
|
|
tooltip.insert("Pos", format!("{pos:.3?}"));
|
|
tooltip.insert("Name", name.into());
|
|
} else {
|
|
warn!("Failed to query data");
|
|
}
|
|
});
|
|
}
|
|
|
|
fn hover_ui(
|
|
mut over_events: EventReader<Pointer<Over>>,
|
|
mut out_events: EventReader<Pointer<Out>>,
|
|
mut tooltip: ResMut<ToolTip>,
|
|
nodes: Query<Option<&Name>, With<Node>>,
|
|
) {
|
|
out_events
|
|
.read()
|
|
.filter_map(|Pointer { target, .. }| nodes.contains(*target).then_some(*target))
|
|
.for_each(|_| {
|
|
tooltip.remove("ID");
|
|
tooltip.remove("Name");
|
|
});
|
|
over_events
|
|
.read()
|
|
.filter_map(|Pointer { target, .. }| nodes.contains(*target).then_some(*target))
|
|
.for_each(|e| {
|
|
if let Ok(n) = nodes.get(e) {
|
|
let name = match n {
|
|
Some(x) => x,
|
|
None => "???",
|
|
};
|
|
tooltip.insert("ID", format!("{e}"));
|
|
tooltip.insert("Name", name.into());
|
|
} else {
|
|
warn!("Failed to query data");
|
|
}
|
|
});
|
|
}
|
|
|
|
fn enable_wireframe(mut wireframe_config: ResMut<WireframeConfig>) {
|
|
wireframe_config.global = true;
|
|
}
|
|
|
|
fn disable_wireframe(mut wireframe_config: ResMut<WireframeConfig>) {
|
|
wireframe_config.global = false;
|
|
}
|