From fb535106cae403ec72b94b46f029b52f7cfe6a08 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Fri, 10 Oct 2025 20:12:27 -0700 Subject: [PATCH] Display shape and orientation for debugging --- src/bin/tetris/main.rs | 52 +++++++++++++++++++++++++++++++++++++++--- src/ui.rs | 17 ++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/bin/tetris/main.rs b/src/bin/tetris/main.rs index 42694de..5121c86 100644 --- a/src/bin/tetris/main.rs +++ b/src/bin/tetris/main.rs @@ -16,7 +16,7 @@ fn main() { ..default() }) .init_state::() - .add_systems(Startup, init_pieces) + .add_systems(Startup, (init_pieces, init_debug_ui)) .add_systems( Update, ( @@ -35,6 +35,8 @@ fn main() { any_component_added:: .or(any_component_changed::), ), + sync_singleton_to_ui::.run_if(any_component_changed::), + sync_singleton_to_ui::.run_if(any_component_changed::), ), ) .add_systems(Update, draw_grid) @@ -44,8 +46,9 @@ fn main() { const SCALE: f32 = 30.0; /// A shape, e.g., the long piece -#[derive(Component, Debug)] +#[derive(Component, Debug, Default)] enum Shape { + #[default] O, T, L, @@ -55,6 +58,20 @@ enum Shape { I, } +impl Display for Shape { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Shape::O => write!(f, "O"), + Shape::T => write!(f, "T"), + Shape::L => write!(f, "L"), + Shape::J => write!(f, "J"), + Shape::S => write!(f, "S"), + Shape::Z => write!(f, "Z"), + Shape::I => write!(f, "I"), + } + } +} + /// A part of a piece, i.e., a single square of a piece #[derive(Component, Debug)] struct ShapePiece; @@ -189,6 +206,17 @@ impl Orientation { } } +impl Display for Orientation { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Orientation::Up => write!(f, "up"), + Orientation::Down => write!(f, "down"), + Orientation::Left => write!(f, "<-"), + Orientation::Right => write!(f, "->"), + } + } +} + #[derive(States, Clone, Eq, PartialEq, Debug, Hash, Default, Component)] enum Falling { #[default] @@ -218,13 +246,31 @@ fn init_pieces( commands.spawn((Orientation::default(), GridPosition::default(), Shape::T)); } +fn init_debug_ui( + mut commands: Commands, +) { + commands.spawn((Node { + top: Val::Px(0.0), + left: Val::Px(0.0), + ..default() + }, DebuggingState::On)).with_children(|parent| { + parent.spawn(( + Node::default(), + children![ + (Text::new("SHAPE"), SyncSingleton::::default()), + (Text::new("ORIENTATION"), SyncSingleton::::default()), + ] + )); + }); +} + fn set_piece( query: Query<(Entity, &Shape, &Orientation), Or<(Added, Changed, Added, Changed)>>, mut commands: Commands, visuals: Res, ) { query.iter().for_each(|(e, s, o)| { - info!("{e:?} {s:?} {o:?}"); + debug!("{e:?} {s:?} {o:?}"); commands .entity(e) .despawn_related::() diff --git a/src/ui.rs b/src/ui.rs index ba1ce67..65dc07a 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -175,6 +175,23 @@ pub fn sync_resource_to_ui( }); } +/// Marker component for handling Resource -> Ui Sync +#[derive(Component, Default, Debug)] +pub struct SyncSingleton(C); + +/// Sync a singleton entity's component to the UI +/// +/// Mostly useful for quick n' dirty getting data to the user +pub fn sync_singleton_to_ui( + mut q: Query<(&mut Text, &mut Visibility), With>>, + c: Single<&C>, +) { + q.iter_mut().for_each(|(mut t, mut v)| { + t.0 = format!("{}", *c); + *v = Visibility::Inherited; + }); +} + /// Updates the scroll position of scrollable nodes in response to mouse input pub fn scroll(trigger: Trigger>, mut scrollers: Query<&mut ScrollPosition>) { let Pointer {