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.

74 lines
2.0 KiB
Rust

use bevy::prelude::*;
use crate::deck::Card;
/// Debugging systems
pub struct DebugPlugin;
impl Plugin for DebugPlugin {
fn build(&self, app: &mut App) {
app.add_observer(track_card_info)
.add_systems(Startup, init_ui);
}
}
#[derive(Component)]
pub(crate) struct DebugText;
fn init_ui(mut commands: Commands) {
commands
.spawn((
Sprite::from_color(Color::BLACK.with_alpha(0.9), [250.0, 150.0].into()),
DebugText,
Visibility::Hidden,
Transform::default().with_translation(Vec3::new(0.0, 0.0, 1.0)),
PickingBehavior::IGNORE,
))
.with_children(|parent| {
parent.spawn((
Text2d("...".to_string()),
DebugText,
Transform::default().with_translation(Vec3::new(0.0, 0.0, 2.0)),
PickingBehavior::IGNORE,
));
});
}
fn track_card_info(
trigger: Trigger<Pointer<Move>>,
mut transforms: Query<&mut Transform, (With<DebugText>, With<Sprite>)>,
window: Query<&Window>,
) {
let p = trigger.pointer_location.position;
transforms.iter_mut().for_each(|mut t| {
let offset = window.single().resolution.size() / 2.0;
let pos = p - offset + Vec2::new(-125.0, 75.0);
t.translation.x = pos.x;
t.translation.y = -pos.y;
});
}
pub(crate) fn set_debug_card(
trigger: Trigger<Pointer<Over>>,
cards: Query<&Card>,
mut vis: Query<&mut Visibility, With<DebugText>>,
mut debug_text: Query<&mut Text2d, With<DebugText>>,
) {
let card = cards.get(trigger.entity()).unwrap();
debug_text.iter_mut().for_each(|mut text| {
text.0 = format!("{:#?}", card);
});
vis.iter_mut().for_each(|mut v| {
*v = Visibility::Inherited;
});
}
pub(crate) fn hide_debug_card(
_trigger: Trigger<Pointer<Out>>,
mut vis: Query<&mut Visibility, With<DebugText>>,
) {
vis.iter_mut().for_each(|mut v| {
*v = Visibility::Hidden;
});
}