Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions crates/bevy_input_focus/src/directional_navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ use bevy_ecs::{
prelude::*,
system::SystemParam,
};
use bevy_math::{CompassOctant, Dir2, Rect, Vec2};
use bevy_math::{ops, CompassOctant, Dir2, Rect, Vec2};
use bevy_ui::{ComputedNode, ComputedUiTargetCamera, UiGlobalTransform};
use thiserror::Error;

Expand Down Expand Up @@ -402,7 +402,17 @@ impl DirectionalNavigationMap {
self.neighbors.get(&entity)
}
}

fn get_rotated_bounds(size: Vec2, rotation: f32) -> Vec2 {
Copy link
Member

@alice-i-cecile alice-i-cecile Jan 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be better as a public helper method on UiTransform TBH :)

if rotation == 0.0 {
return size;
}
let cos_r = ops::cos(rotation).abs();
let sin_r = ops::sin(rotation).abs();
Vec2::new(
size.x * cos_r + size.y * sin_r,
size.x * sin_r + size.y * cos_r,
)
}
/// A system parameter for navigating between focusable entities in a directional way.
#[derive(SystemParam, Debug)]
pub struct DirectionalNavigation<'w, 's> {
Expand Down Expand Up @@ -492,12 +502,13 @@ impl<'w, 's> DirectionalNavigation<'w, 's> {
if let Some(tc) = computed_target_camera.get()
&& tc == target_camera
{
let (_scale, _rotation, translation) =
transform.to_scale_angle_translation();
let (scale, rotation, translation) = transform.to_scale_angle_translation();
let scaled_size = computed.size() * computed.inverse_scale_factor() * scale;
let rotated_size = get_rotated_bounds(scaled_size, rotation);
Some(FocusableArea {
entity,
position: translation * computed.inverse_scale_factor(),
size: computed.size() * computed.inverse_scale_factor(),
size: rotated_size,
})
} else {
// The node either does not have a target camera or it is not the same as the desired one.
Expand All @@ -520,13 +531,13 @@ impl<'w, 's> DirectionalNavigation<'w, 's> {
None,
|(entity, computed_target_camera, computed, transform)| {
if let Some(target_camera) = computed_target_camera.get() {
let (_scale, _rotation, translation) = transform.to_scale_angle_translation();
let (scale, _rotation, translation) = transform.to_scale_angle_translation();
Some((
target_camera,
FocusableArea {
entity,
position: translation * computed.inverse_scale_factor(),
size: computed.size() * computed.inverse_scale_factor(),
size: computed.size() * computed.inverse_scale_factor() * scale,
},
))
} else {
Expand Down
2 changes: 2 additions & 0 deletions examples/ui/auto_directional_navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ fn setup_scattered_ui(mut commands: Commands, mut input_focus: ResMut<InputFocus
border_radius: BorderRadius::all(px(12)),
..default()
},
// Apply a scale transform to demonstrate that navigation handles transforms
Transform::from_scale(Vec3::splat(2.0)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be using UiTransform here

// This is the key: just add this component for automatic navigation!
AutoDirectionalNavigation::default(),
ResetTimer::default(),
Expand Down