mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
bca057d7ec
# Objective Problems: * The clipped, non-visible regions of UI nodes are interactive. * `RelativeCursorPostion` is set relative to the visible part of the node. It should be relative to the whole node. * The `RelativeCursorPostion::mouse_over` method returns `true` when the mouse is over a clipped part of a node. fixes #10470 ## Solution Intersect a node's bounding rect with its clipping rect before checking if it contains the cursor. Added the field `normalized_visible_node_rect` to `RelativeCursorPosition`. This is set to the bounds of the unclipped area of the node rect by `ui_focus_system` expressed in normalized coordinates relative to the entire node. Instead of checking if the normalized cursor position lies within a unit square, it instead checks if it is contained by `normalized_visible_node_rect`. Added outlines to the `overflow` example that appear when the cursor is over the visible part of the images, but not the clipped area. --- ## Changelog * `ui_focus_system` intersects a node's bounding rect with its clipping rect before checking if mouse over. * Added the field `normalized_visible_node_rect` to `RelativeCursorPosition`. This is set to the bounds of the unclipped area of the node rect by `ui_focus_system` expressed in normalized coordinates relative to the entire node. * `RelativeCursorPostion` is calculated relative to the whole node's position and size, not only the visible part. * `RelativeCursorPosition::mouse_over` only returns true when the mouse is over an unclipped region of the UI node. * Removed the `Deref` and `DerefMut` derives from `RelativeCursorPosition` as it is no longer a single field struct. * Added some outlines to the `overflow` example that respond to `Interaction` changes. ## Migration Guide The clipped areas of UI nodes are no longer interactive. `RelativeCursorPostion` is now calculated relative to the whole node's position and size, not only the visible part. Its `mouse_over` method only returns true when the cursor is over an unclipped part of the node. `RelativeCursorPosition` no longer implements `Deref` and `DerefMut`.
125 lines
5 KiB
Rust
125 lines
5 KiB
Rust
//! Simple example demonstrating overflow behavior.
|
|
|
|
use bevy::{prelude::*, winit::WinitSettings};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins)
|
|
// Only run the app when there is user input. This will significantly reduce CPU/GPU use.
|
|
.insert_resource(WinitSettings::desktop_app())
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, update_outlines)
|
|
.run();
|
|
}
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
let text_style = TextStyle {
|
|
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
|
|
font_size: 20.0,
|
|
..default()
|
|
};
|
|
|
|
let image = asset_server.load("branding/icon.png");
|
|
|
|
commands
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
align_items: AlignItems::Center,
|
|
justify_content: JustifyContent::Center,
|
|
..Default::default()
|
|
},
|
|
background_color: Color::ANTIQUE_WHITE.into(),
|
|
..Default::default()
|
|
})
|
|
.with_children(|parent| {
|
|
for overflow in [
|
|
Overflow::visible(),
|
|
Overflow::clip_x(),
|
|
Overflow::clip_y(),
|
|
Overflow::clip(),
|
|
] {
|
|
parent
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
flex_direction: FlexDirection::Column,
|
|
align_items: AlignItems::Center,
|
|
margin: UiRect::horizontal(Val::Px(25.)),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
})
|
|
.with_children(|parent| {
|
|
let label = format!("{overflow:#?}");
|
|
parent
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
padding: UiRect::all(Val::Px(10.)),
|
|
margin: UiRect::bottom(Val::Px(25.)),
|
|
..Default::default()
|
|
},
|
|
background_color: Color::DARK_GRAY.into(),
|
|
..Default::default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent.spawn(TextBundle {
|
|
text: Text::from_section(label, text_style.clone()),
|
|
..Default::default()
|
|
});
|
|
});
|
|
parent
|
|
.spawn(NodeBundle {
|
|
style: Style {
|
|
width: Val::Px(100.),
|
|
height: Val::Px(100.),
|
|
padding: UiRect {
|
|
left: Val::Px(25.),
|
|
top: Val::Px(25.),
|
|
..Default::default()
|
|
},
|
|
overflow,
|
|
..Default::default()
|
|
},
|
|
background_color: Color::GRAY.into(),
|
|
..Default::default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent.spawn((
|
|
ImageBundle {
|
|
image: UiImage::new(image.clone()),
|
|
style: Style {
|
|
min_width: Val::Px(100.),
|
|
min_height: Val::Px(100.),
|
|
..Default::default()
|
|
},
|
|
background_color: Color::WHITE.into(),
|
|
|
|
..Default::default()
|
|
},
|
|
Interaction::default(),
|
|
Outline {
|
|
width: Val::Px(2.),
|
|
offset: Val::Px(2.),
|
|
color: Color::NONE,
|
|
},
|
|
));
|
|
});
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
fn update_outlines(mut outlines_query: Query<(&mut Outline, Ref<Interaction>)>) {
|
|
for (mut outline, interaction) in outlines_query.iter_mut() {
|
|
if interaction.is_changed() {
|
|
outline.color = match *interaction {
|
|
Interaction::Pressed => Color::RED,
|
|
Interaction::Hovered => Color::WHITE,
|
|
Interaction::None => Color::NONE,
|
|
};
|
|
}
|
|
}
|
|
}
|