diff --git a/crates/bevy_input_focus/src/directional_navigation.rs b/crates/bevy_input_focus/src/directional_navigation.rs index a1f7abca1eb2f..88413e00a6dcf 100644 --- a/crates/bevy_input_focus/src/directional_navigation.rs +++ b/crates/bevy_input_focus/src/directional_navigation.rs @@ -562,4 +562,93 @@ mod tests { world.run_system_once(navigate_east).unwrap(); assert_eq!(world.resource::().get(), Some(a)); } + + #[test] + fn non_looping_grid_edges() { + let mut world = World::new(); + let a = world.spawn_empty().id(); + let b = world.spawn_empty().id(); + let c = world.spawn_empty().id(); + let d = world.spawn_empty().id(); + + let mut nav_map = DirectionalNavigationMap::default(); + // a b + // c d + let mut grid = HashMap::default(); + grid.insert(IVec2::new(0, 0), a); + grid.insert(IVec2::new(1, 0), b); + grid.insert(IVec2::new(0, 1), c); + grid.insert(IVec2::new(1, 1), d); + + nav_map.add_grid(grid, false); + + // Cardinal directions + assert_eq!(nav_map.get_neighbor(a, CompassOctant::East), Some(b)); + assert_eq!(nav_map.get_neighbor(b, CompassOctant::West), Some(a)); + assert_eq!(nav_map.get_neighbor(a, CompassOctant::South), Some(c)); + assert_eq!(nav_map.get_neighbor(c, CompassOctant::North), Some(a)); + assert_eq!(nav_map.get_neighbor(b, CompassOctant::South), Some(d)); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::North), Some(b)); + assert_eq!(nav_map.get_neighbor(c, CompassOctant::East), Some(d)); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::West), Some(c)); + + // Diagonal directions + assert_eq!(nav_map.get_neighbor(a, CompassOctant::SouthEast), Some(d)); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::NorthWest), Some(a)); + assert_eq!(nav_map.get_neighbor(b, CompassOctant::SouthWest), Some(c)); + assert_eq!(nav_map.get_neighbor(c, CompassOctant::NorthEast), Some(b)); + + // Out of bounds + assert_eq!(nav_map.get_neighbor(a, CompassOctant::North), None); + assert_eq!(nav_map.get_neighbor(a, CompassOctant::West), None); + assert_eq!(nav_map.get_neighbor(a, CompassOctant::NorthEast), None); + assert_eq!(nav_map.get_neighbor(a, CompassOctant::NorthWest), None); + assert_eq!(nav_map.get_neighbor(a, CompassOctant::SouthWest), None); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::East), None); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::South), None); + } + + #[test] + fn looping_grid_edges() { + let mut world = World::new(); + let a = world.spawn_empty().id(); + let b = world.spawn_empty().id(); + let c = world.spawn_empty().id(); + let d = world.spawn_empty().id(); + + let mut nav_map = DirectionalNavigationMap::default(); + // a b + // c d + let mut grid = HashMap::default(); + grid.insert(IVec2::new(0, 0), a); + grid.insert(IVec2::new(1, 0), b); + grid.insert(IVec2::new(0, 1), c); + grid.insert(IVec2::new(1, 1), d); + + nav_map.add_grid(grid, false); + + // Cardinal directions + assert_eq!(nav_map.get_neighbor(a, CompassOctant::East), Some(b)); + assert_eq!(nav_map.get_neighbor(b, CompassOctant::West), Some(a)); + assert_eq!(nav_map.get_neighbor(a, CompassOctant::South), Some(c)); + assert_eq!(nav_map.get_neighbor(c, CompassOctant::North), Some(a)); + assert_eq!(nav_map.get_neighbor(b, CompassOctant::South), Some(d)); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::North), Some(b)); + assert_eq!(nav_map.get_neighbor(c, CompassOctant::East), Some(d)); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::West), Some(c)); + + // Diagonal directions + assert_eq!(nav_map.get_neighbor(a, CompassOctant::SouthEast), Some(d)); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::NorthWest), Some(a)); + assert_eq!(nav_map.get_neighbor(b, CompassOctant::SouthWest), Some(c)); + assert_eq!(nav_map.get_neighbor(c, CompassOctant::NorthEast), Some(b)); + + // Out of bounds + assert_eq!(nav_map.get_neighbor(a, CompassOctant::North), Some(c)); + assert_eq!(nav_map.get_neighbor(a, CompassOctant::West), Some(b)); + assert_eq!(nav_map.get_neighbor(a, CompassOctant::NorthWest), Some(d)); + assert_eq!(nav_map.get_neighbor(a, CompassOctant::SouthWest), None); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::East), Some(c)); + assert_eq!(nav_map.get_neighbor(d, CompassOctant::South), Some(b)); + } }