mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 06:30:19 +00:00
015f2c69ca
# Objective Continue improving the user experience of our UI Node API in the direction specified by [Bevy's Next Generation Scene / UI System](https://github.com/bevyengine/bevy/discussions/14437) ## Solution As specified in the document above, merge `Style` fields into `Node`, and move "computed Node fields" into `ComputedNode` (I chose this name over something like `ComputedNodeLayout` because it currently contains more than just layout info. If we want to break this up / rename these concepts, lets do that in a separate PR). `Style` has been removed. This accomplishes a number of goals: ## Ergonomics wins Specifying both `Node` and `Style` is now no longer required for non-default styles Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` ## Conceptual clarity `Style` was never a comprehensive "style sheet". It only defined "core" style properties that all `Nodes` shared. Any "styled property" that couldn't fit that mold had to be in a separate component. A "real" style system would style properties _across_ components (`Node`, `Button`, etc). We have plans to build a true style system (see the doc linked above). By moving the `Style` fields to `Node`, we fully embrace `Node` as the driving concept and remove the "style system" confusion. ## Next Steps * Consider identifying and splitting out "style properties that aren't core to Node". This should not happen for Bevy 0.15. --- ## Migration Guide Move any fields set on `Style` into `Node` and replace all `Style` component usage with `Node`. Before: ```rust commands.spawn(( Node::default(), Style { width: Val::Px(100.), ..default() }, )); ``` After: ```rust commands.spawn(Node { width: Val::Px(100.), ..default() }); ``` For any usage of the "computed node properties" that used to live on `Node`, use `ComputedNode` instead: Before: ```rust fn system(nodes: Query<&Node>) { for node in &nodes { let computed_size = node.size(); } } ``` After: ```rust fn system(computed_nodes: Query<&ComputedNode>) { for computed_node in &computed_nodes { let computed_size = computed_node.size(); } } ```
361 lines
18 KiB
Rust
361 lines
18 KiB
Rust
//! This example illustrates scrolling in Bevy UI.
|
|
|
|
use bevy::{
|
|
a11y::{
|
|
accesskit::{NodeBuilder, Role},
|
|
AccessibilityNode,
|
|
},
|
|
input::mouse::{MouseScrollUnit, MouseWheel},
|
|
picking::focus::HoverMap,
|
|
prelude::*,
|
|
winit::WinitSettings,
|
|
};
|
|
|
|
fn main() {
|
|
let mut app = App::new();
|
|
app.add_plugins(DefaultPlugins)
|
|
.insert_resource(WinitSettings::desktop_app())
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, update_scroll_position);
|
|
|
|
app.run();
|
|
}
|
|
|
|
const FONT_SIZE: f32 = 20.;
|
|
const LINE_HEIGHT: f32 = 21.;
|
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
|
// Camera
|
|
commands.spawn((Camera2d, IsDefaultUiCamera));
|
|
|
|
// root node
|
|
commands
|
|
.spawn(Node {
|
|
width: Val::Percent(100.0),
|
|
height: Val::Percent(100.0),
|
|
justify_content: JustifyContent::SpaceBetween,
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
})
|
|
.insert(PickingBehavior::IGNORE)
|
|
.with_children(|parent| {
|
|
// horizontal scroll example
|
|
parent
|
|
.spawn(Node {
|
|
width: Val::Percent(100.),
|
|
flex_direction: FlexDirection::Column,
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
// header
|
|
parent.spawn((
|
|
Text::new("Horizontally Scrolling list (Ctrl + Mousewheel)"),
|
|
TextFont {
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
font_size: FONT_SIZE,
|
|
..default()
|
|
},
|
|
Label,
|
|
));
|
|
|
|
// horizontal scroll container
|
|
parent
|
|
.spawn((
|
|
Node {
|
|
width: Val::Percent(80.),
|
|
margin: UiRect::all(Val::Px(10.)),
|
|
flex_direction: FlexDirection::Row,
|
|
overflow: Overflow::scroll_x(), // n.b.
|
|
..default()
|
|
},
|
|
BackgroundColor(Color::srgb(0.10, 0.10, 0.10)),
|
|
))
|
|
.with_children(|parent| {
|
|
for i in 0..100 {
|
|
parent.spawn((Text(format!("Item {i}")),
|
|
TextFont {
|
|
font: asset_server
|
|
.load("fonts/FiraSans-Bold.ttf"),
|
|
..default()
|
|
},
|
|
Label,
|
|
AccessibilityNode(NodeBuilder::new(Role::ListItem)),
|
|
))
|
|
.insert(Node {
|
|
min_width: Val::Px(200.),
|
|
align_content: AlignContent::Center,
|
|
..default()
|
|
})
|
|
.insert(PickingBehavior {
|
|
should_block_lower: false,
|
|
..default()
|
|
})
|
|
.observe(|
|
|
trigger: Trigger<Pointer<Down>>,
|
|
mut commands: Commands
|
|
| {
|
|
if trigger.event().button == PointerButton::Primary {
|
|
commands.entity(trigger.entity()).despawn_recursive();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
// container for all other examples
|
|
parent
|
|
.spawn(Node {
|
|
width: Val::Percent(100.),
|
|
height: Val::Percent(100.),
|
|
flex_direction: FlexDirection::Row,
|
|
justify_content: JustifyContent::SpaceBetween,
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
// vertical scroll example
|
|
parent
|
|
.spawn(Node {
|
|
flex_direction: FlexDirection::Column,
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
width: Val::Px(200.),
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
// Title
|
|
parent.spawn((
|
|
Text::new("Vertically Scrolling List"),
|
|
TextFont {
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
font_size: FONT_SIZE,
|
|
..default()
|
|
},
|
|
Label,
|
|
));
|
|
// Scrolling list
|
|
parent
|
|
.spawn((
|
|
Node {
|
|
flex_direction: FlexDirection::Column,
|
|
align_self: AlignSelf::Stretch,
|
|
height: Val::Percent(50.),
|
|
overflow: Overflow::scroll_y(), // n.b.
|
|
..default()
|
|
},
|
|
BackgroundColor(Color::srgb(0.10, 0.10, 0.10)),
|
|
))
|
|
.with_children(|parent| {
|
|
// List items
|
|
for i in 0..25 {
|
|
parent
|
|
.spawn(Node {
|
|
min_height: Val::Px(LINE_HEIGHT),
|
|
max_height: Val::Px(LINE_HEIGHT),
|
|
..default()
|
|
})
|
|
.insert(PickingBehavior {
|
|
should_block_lower: false,
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
parent
|
|
.spawn((
|
|
Text(format!("Item {i}")),
|
|
TextFont {
|
|
font: asset_server
|
|
.load("fonts/FiraSans-Bold.ttf"),
|
|
..default()
|
|
},
|
|
Label,
|
|
AccessibilityNode(NodeBuilder::new(
|
|
Role::ListItem,
|
|
)),
|
|
))
|
|
.insert(PickingBehavior {
|
|
should_block_lower: false,
|
|
..default()
|
|
});
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
// Bidirectional scroll example
|
|
parent
|
|
.spawn(Node {
|
|
flex_direction: FlexDirection::Column,
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
width: Val::Px(200.),
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
// Title
|
|
parent.spawn((
|
|
Text::new("Bidirectionally Scrolling List"),
|
|
TextFont {
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
font_size: FONT_SIZE,
|
|
..default()
|
|
},
|
|
Label,
|
|
));
|
|
// Scrolling list
|
|
parent
|
|
.spawn((
|
|
Node {
|
|
flex_direction: FlexDirection::Column,
|
|
align_self: AlignSelf::Stretch,
|
|
height: Val::Percent(50.),
|
|
overflow: Overflow::scroll(), // n.b.
|
|
..default()
|
|
},
|
|
BackgroundColor(Color::srgb(0.10, 0.10, 0.10)),
|
|
))
|
|
.with_children(|parent| {
|
|
// Rows in each column
|
|
for oi in 0..10 {
|
|
parent
|
|
.spawn(Node {
|
|
flex_direction: FlexDirection::Row,
|
|
..default()
|
|
})
|
|
.insert(PickingBehavior::IGNORE)
|
|
.with_children(|parent| {
|
|
// Elements in each row
|
|
for i in 0..25 {
|
|
parent
|
|
.spawn((
|
|
Text(format!("Item {}", (oi * 25) + i)),
|
|
TextFont {
|
|
font: asset_server.load(
|
|
"fonts/FiraSans-Bold.ttf",
|
|
),
|
|
..default()
|
|
},
|
|
Label,
|
|
AccessibilityNode(NodeBuilder::new(
|
|
Role::ListItem,
|
|
)),
|
|
))
|
|
.insert(PickingBehavior {
|
|
should_block_lower: false,
|
|
..default()
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
// Nested scrolls example
|
|
parent
|
|
.spawn(Node {
|
|
flex_direction: FlexDirection::Column,
|
|
justify_content: JustifyContent::Center,
|
|
align_items: AlignItems::Center,
|
|
width: Val::Px(200.),
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
// Title
|
|
parent.spawn((
|
|
Text::new("Nested Scrolling Lists"),
|
|
TextFont {
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
font_size: FONT_SIZE,
|
|
..default()
|
|
},
|
|
Label,
|
|
));
|
|
// Outer, horizontal scrolling container
|
|
parent
|
|
.spawn((
|
|
Node {
|
|
column_gap: Val::Px(20.),
|
|
flex_direction: FlexDirection::Row,
|
|
align_self: AlignSelf::Stretch,
|
|
height: Val::Percent(50.),
|
|
overflow: Overflow::scroll_x(), // n.b.
|
|
..default()
|
|
},
|
|
BackgroundColor(Color::srgb(0.10, 0.10, 0.10)),
|
|
))
|
|
.with_children(|parent| {
|
|
// Inner, scrolling columns
|
|
for oi in 0..30 {
|
|
parent
|
|
.spawn((
|
|
Node {
|
|
flex_direction: FlexDirection::Column,
|
|
align_self: AlignSelf::Stretch,
|
|
overflow: Overflow::scroll_y(),
|
|
..default()
|
|
},
|
|
BackgroundColor(Color::srgb(0.05, 0.05, 0.05)),
|
|
))
|
|
.insert(PickingBehavior {
|
|
should_block_lower: false,
|
|
..default()
|
|
})
|
|
.with_children(|parent| {
|
|
for i in 0..25 {
|
|
parent
|
|
.spawn((
|
|
Text(format!("Item {}", (oi * 25) + i)),
|
|
TextFont {
|
|
font: asset_server.load(
|
|
"fonts/FiraSans-Bold.ttf",
|
|
),
|
|
..default()
|
|
},
|
|
Label,
|
|
AccessibilityNode(NodeBuilder::new(
|
|
Role::ListItem,
|
|
)),
|
|
))
|
|
.insert(PickingBehavior {
|
|
should_block_lower: false,
|
|
..default()
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
/// Updates the scroll position of scrollable nodes in response to mouse input
|
|
pub fn update_scroll_position(
|
|
mut mouse_wheel_events: EventReader<MouseWheel>,
|
|
hover_map: Res<HoverMap>,
|
|
mut scrolled_node_query: Query<&mut ScrollPosition>,
|
|
keyboard_input: Res<ButtonInput<KeyCode>>,
|
|
) {
|
|
for mouse_wheel_event in mouse_wheel_events.read() {
|
|
let (mut dx, mut dy) = match mouse_wheel_event.unit {
|
|
MouseScrollUnit::Line => (
|
|
mouse_wheel_event.x * LINE_HEIGHT,
|
|
mouse_wheel_event.y * LINE_HEIGHT,
|
|
),
|
|
MouseScrollUnit::Pixel => (mouse_wheel_event.x, mouse_wheel_event.y),
|
|
};
|
|
|
|
if keyboard_input.pressed(KeyCode::ControlLeft)
|
|
|| keyboard_input.pressed(KeyCode::ControlRight)
|
|
{
|
|
std::mem::swap(&mut dx, &mut dy);
|
|
}
|
|
|
|
for (_pointer, pointer_map) in hover_map.iter() {
|
|
for (entity, _hit) in pointer_map.iter() {
|
|
if let Ok(mut scroll_position) = scrolled_node_query.get_mut(*entity) {
|
|
scroll_position.offset_x -= dx;
|
|
scroll_position.offset_y -= dy;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|