Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
27 changes: 21 additions & 6 deletions crates/bevy_ui/src/auto_directional_navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::{ComputedNode, ComputedUiTargetCamera, UiGlobalTransform};
use bevy_camera::visibility::InheritedVisibility;
use bevy_ecs::{prelude::*, system::SystemParam};
use bevy_math::CompassOctant;
use bevy_math::{ops, CompassOctant, Vec2};

use bevy_input_focus::{
directional_navigation::{
Expand Down Expand Up @@ -184,12 +184,13 @@ impl<'w, 's> AutoDirectionalNavigator<'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 @@ -212,13 +213,15 @@ impl<'w, 's> AutoDirectionalNavigator<'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();
let scaled_size = computed.size() * computed.inverse_scale_factor() * scale;
let rotated_size = get_rotated_bounds(scaled_size, rotation);
Some((
target_camera,
FocusableArea {
entity,
position: translation * computed.inverse_scale_factor(),
size: computed.size() * computed.inverse_scale_factor(),
size: rotated_size,
},
))
} else {
Expand All @@ -228,3 +231,15 @@ impl<'w, 's> AutoDirectionalNavigator<'w, 's> {
)
}
}

fn get_rotated_bounds(size: Vec2, rotation: f32) -> Vec2 {
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,
)
}
12 changes: 11 additions & 1 deletion examples/ui/auto_directional_navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use bevy::{
directional_navigation::{AutoNavigationConfig, DirectionalNavigationPlugin},
InputDispatchPlugin, InputFocus, InputFocusVisible,
},
math::{CompassOctant, Dir2},
math::{CompassOctant, Dir2, Rot2},
picking::{
backend::HitData,
pointer::{Location, PointerId},
Expand Down Expand Up @@ -210,6 +210,15 @@ fn setup_scattered_ui(mut commands: Commands, mut input_focus: ResMut<InputFocus

let mut first_button = None;
for (i, (x, y)) in button_positions.iter().enumerate() {
let transform = if i == 4 {
UiTransform {
scale: Vec2::splat(1.2),
rotation: Rot2::FRAC_PI_2,
..default()
}
} else {
UiTransform::IDENTITY
};
let button_entity = commands
.spawn((
Button,
Expand All @@ -225,6 +234,7 @@ fn setup_scattered_ui(mut commands: Commands, mut input_focus: ResMut<InputFocus
border_radius: BorderRadius::all(px(12)),
..default()
},
transform,
// This is the key: just add this component for automatic navigation!
AutoDirectionalNavigation::default(),
ResetTimer::default(),
Expand Down