2021-12-19 05:44:28 +00:00
|
|
|
use bevy::{
|
|
|
|
input::mouse::{MouseScrollUnit, MouseWheel},
|
|
|
|
prelude::*,
|
2022-03-09 21:59:57 +00:00
|
|
|
winit::WinitSettings,
|
2021-12-19 05:44:28 +00:00
|
|
|
};
|
2020-01-11 09:46:51 +00:00
|
|
|
|
2020-07-28 20:43:07 +00:00
|
|
|
/// This example illustrates the various features of Bevy UI.
|
2020-01-11 09:46:51 +00:00
|
|
|
fn main() {
|
2021-07-27 20:21:06 +00:00
|
|
|
App::new()
|
2020-11-03 03:01:17 +00:00
|
|
|
.add_plugins(DefaultPlugins)
|
2022-03-09 21:59:57 +00:00
|
|
|
// Only run the app when there is user input. This will significantly reduce CPU/GPU use.
|
|
|
|
.insert_resource(WinitSettings::desktop_app())
|
2021-07-27 23:42:36 +00:00
|
|
|
.add_startup_system(setup)
|
2021-12-19 05:44:28 +00:00
|
|
|
.add_system(mouse_scroll)
|
2020-04-16 02:40:24 +00:00
|
|
|
.run();
|
2020-01-11 09:46:51 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 03:58:23 +00:00
|
|
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
2021-03-23 00:23:40 +00:00
|
|
|
// ui camera
|
|
|
|
commands.spawn_bundle(UiCameraBundle::default());
|
2021-12-14 03:58:23 +00:00
|
|
|
|
2021-03-23 00:23:40 +00:00
|
|
|
// root node
|
2020-07-10 04:18:35 +00:00
|
|
|
commands
|
2021-03-23 00:23:40 +00:00
|
|
|
.spawn_bundle(NodeBundle {
|
2020-07-26 19:27:09 +00:00
|
|
|
style: Style {
|
2020-07-28 02:12:48 +00:00
|
|
|
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
|
2020-07-25 06:04:45 +00:00
|
|
|
justify_content: JustifyContent::SpaceBetween,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-25 06:04:45 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::NONE.into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-03-09 09:00:59 +00:00
|
|
|
})
|
2020-07-10 04:18:35 +00:00
|
|
|
.with_children(|parent| {
|
2021-03-23 00:23:40 +00:00
|
|
|
// left vertical fill (border)
|
2020-07-10 04:18:35 +00:00
|
|
|
parent
|
2021-03-23 00:23:40 +00:00
|
|
|
.spawn_bundle(NodeBundle {
|
2020-07-26 19:27:09 +00:00
|
|
|
style: Style {
|
2020-07-28 02:12:48 +00:00
|
|
|
size: Size::new(Val::Px(200.0), Val::Percent(100.0)),
|
2022-04-25 19:20:38 +00:00
|
|
|
border: UiRect::all(Val::Px(2.0)),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-25 06:04:45 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::rgb(0.65, 0.65, 0.65).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-06-25 20:19:17 +00:00
|
|
|
})
|
2020-07-10 04:18:35 +00:00
|
|
|
.with_children(|parent| {
|
2021-03-23 00:23:40 +00:00
|
|
|
// left vertical fill (content)
|
2020-07-25 06:04:45 +00:00
|
|
|
parent
|
2021-03-23 00:23:40 +00:00
|
|
|
.spawn_bundle(NodeBundle {
|
2020-07-26 19:27:09 +00:00
|
|
|
style: Style {
|
2020-07-28 02:12:48 +00:00
|
|
|
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
|
2020-07-25 06:04:45 +00:00
|
|
|
align_items: AlignItems::FlexEnd,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-06-25 20:19:17 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::rgb(0.15, 0.15, 0.15).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-25 06:04:45 +00:00
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
2020-07-28 02:12:48 +00:00
|
|
|
// text
|
2021-03-23 00:23:40 +00:00
|
|
|
parent.spawn_bundle(TextBundle {
|
2020-07-26 19:27:09 +00:00
|
|
|
style: Style {
|
2022-04-25 19:20:38 +00:00
|
|
|
margin: UiRect::all(Val::Px(5.0)),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-25 06:04:45 +00:00
|
|
|
},
|
2021-01-25 01:07:43 +00:00
|
|
|
text: Text::with_section(
|
|
|
|
"Text Example",
|
|
|
|
TextStyle {
|
|
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
2020-07-25 06:04:45 +00:00
|
|
|
font_size: 30.0,
|
|
|
|
color: Color::WHITE,
|
|
|
|
},
|
2021-01-25 01:07:43 +00:00
|
|
|
Default::default(),
|
|
|
|
),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-25 06:04:45 +00:00
|
|
|
});
|
|
|
|
});
|
2021-03-23 00:23:40 +00:00
|
|
|
});
|
|
|
|
// right vertical fill
|
2021-12-19 05:44:28 +00:00
|
|
|
parent
|
|
|
|
.spawn_bundle(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
flex_direction: FlexDirection::ColumnReverse,
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
size: Size::new(Val::Px(200.0), Val::Percent(100.0)),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
},
|
|
|
|
color: Color::rgb(0.15, 0.15, 0.15).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
|
|
|
// Title
|
|
|
|
parent.spawn_bundle(TextBundle {
|
|
|
|
style: Style {
|
|
|
|
size: Size::new(Val::Undefined, Val::Px(25.)),
|
2022-04-25 19:20:38 +00:00
|
|
|
margin: UiRect {
|
2021-12-19 05:44:28 +00:00
|
|
|
left: Val::Auto,
|
|
|
|
right: Val::Auto,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
},
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
},
|
|
|
|
text: Text::with_section(
|
|
|
|
"Scrolling list",
|
|
|
|
TextStyle {
|
|
|
|
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
font_size: 25.,
|
|
|
|
color: Color::WHITE,
|
|
|
|
},
|
|
|
|
Default::default(),
|
|
|
|
),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
});
|
|
|
|
// List with hidden overflow
|
|
|
|
parent
|
|
|
|
.spawn_bundle(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
flex_direction: FlexDirection::ColumnReverse,
|
|
|
|
align_self: AlignSelf::Center,
|
|
|
|
size: Size::new(Val::Percent(100.0), Val::Percent(50.0)),
|
|
|
|
overflow: Overflow::Hidden,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
},
|
|
|
|
color: Color::rgb(0.10, 0.10, 0.10).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
|
|
|
// Moving panel
|
|
|
|
parent
|
|
|
|
.spawn_bundle(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
flex_direction: FlexDirection::ColumnReverse,
|
|
|
|
flex_grow: 1.0,
|
|
|
|
max_size: Size::new(Val::Undefined, Val::Undefined),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
},
|
|
|
|
color: Color::NONE.into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
})
|
|
|
|
.insert(ScrollingList::default())
|
|
|
|
.with_children(|parent| {
|
|
|
|
// List items
|
|
|
|
for i in 0..30 {
|
|
|
|
parent.spawn_bundle(TextBundle {
|
|
|
|
style: Style {
|
|
|
|
flex_shrink: 0.,
|
|
|
|
size: Size::new(Val::Undefined, Val::Px(20.)),
|
2022-04-25 19:20:38 +00:00
|
|
|
margin: UiRect {
|
2021-12-19 05:44:28 +00:00
|
|
|
left: Val::Auto,
|
|
|
|
right: Val::Auto,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
},
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
},
|
|
|
|
text: Text::with_section(
|
|
|
|
format!("Item {}", i),
|
|
|
|
TextStyle {
|
|
|
|
font: asset_server
|
|
|
|
.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
font_size: 20.,
|
|
|
|
color: Color::WHITE,
|
|
|
|
},
|
|
|
|
Default::default(),
|
|
|
|
),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-12-19 05:44:28 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-03-23 00:23:40 +00:00
|
|
|
// absolute positioning
|
|
|
|
parent
|
|
|
|
.spawn_bundle(NodeBundle {
|
2020-07-26 19:27:09 +00:00
|
|
|
style: Style {
|
2020-07-28 02:12:48 +00:00
|
|
|
size: Size::new(Val::Px(200.0), Val::Px(200.0)),
|
|
|
|
position_type: PositionType::Absolute,
|
2022-04-25 19:20:38 +00:00
|
|
|
position: UiRect {
|
2020-07-28 02:12:48 +00:00
|
|
|
left: Val::Px(210.0),
|
2020-07-26 19:27:09 +00:00
|
|
|
bottom: Val::Px(10.0),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-26 19:27:09 +00:00
|
|
|
},
|
2022-04-25 19:20:38 +00:00
|
|
|
border: UiRect::all(Val::Px(20.0)),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-26 19:27:09 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::rgb(0.4, 0.4, 1.0).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
2021-03-23 00:23:40 +00:00
|
|
|
parent.spawn_bundle(NodeBundle {
|
2020-07-28 02:12:48 +00:00
|
|
|
style: Style {
|
|
|
|
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::rgb(0.8, 0.8, 1.0).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
});
|
2021-03-23 00:23:40 +00:00
|
|
|
});
|
|
|
|
// render order test: reddest in the back, whitest in the front (flex center)
|
|
|
|
parent
|
|
|
|
.spawn_bundle(NodeBundle {
|
2020-07-28 02:12:48 +00:00
|
|
|
style: Style {
|
|
|
|
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
align_items: AlignItems::Center,
|
|
|
|
justify_content: JustifyContent::Center,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::NONE.into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
|
|
|
parent
|
2021-03-23 00:23:40 +00:00
|
|
|
.spawn_bundle(NodeBundle {
|
2020-07-28 02:12:48 +00:00
|
|
|
style: Style {
|
|
|
|
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::rgb(1.0, 0.0, 0.0).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
2021-03-23 00:23:40 +00:00
|
|
|
parent.spawn_bundle(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
|
|
|
|
position_type: PositionType::Absolute,
|
2022-04-25 19:20:38 +00:00
|
|
|
position: UiRect {
|
2021-03-23 00:23:40 +00:00
|
|
|
left: Val::Px(20.0),
|
|
|
|
bottom: Val::Px(20.0),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-03-23 00:23:40 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::rgb(1.0, 0.3, 0.3).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-03-23 00:23:40 +00:00
|
|
|
});
|
|
|
|
parent.spawn_bundle(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
|
|
|
|
position_type: PositionType::Absolute,
|
2022-04-25 19:20:38 +00:00
|
|
|
position: UiRect {
|
2021-03-23 00:23:40 +00:00
|
|
|
left: Val::Px(40.0),
|
|
|
|
bottom: Val::Px(40.0),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-03-23 00:23:40 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::rgb(1.0, 0.5, 0.5).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-03-23 00:23:40 +00:00
|
|
|
});
|
|
|
|
parent.spawn_bundle(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
|
|
|
|
position_type: PositionType::Absolute,
|
2022-04-25 19:20:38 +00:00
|
|
|
position: UiRect {
|
2021-03-23 00:23:40 +00:00
|
|
|
left: Val::Px(60.0),
|
|
|
|
bottom: Val::Px(60.0),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-03-23 00:23:40 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::rgb(1.0, 0.7, 0.7).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-03-23 00:23:40 +00:00
|
|
|
});
|
|
|
|
// alpha test
|
|
|
|
parent.spawn_bundle(NodeBundle {
|
|
|
|
style: Style {
|
|
|
|
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
|
|
|
|
position_type: PositionType::Absolute,
|
2022-04-25 19:20:38 +00:00
|
|
|
position: UiRect {
|
2021-03-23 00:23:40 +00:00
|
|
|
left: Val::Px(80.0),
|
|
|
|
bottom: Val::Px(80.0),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-03-23 00:23:40 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::rgba(1.0, 0.9, 0.9, 0.4).into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2021-03-23 00:23:40 +00:00
|
|
|
});
|
2020-07-28 02:12:48 +00:00
|
|
|
});
|
2021-03-23 00:23:40 +00:00
|
|
|
});
|
|
|
|
// bevy logo (flex center)
|
|
|
|
parent
|
|
|
|
.spawn_bundle(NodeBundle {
|
2020-07-28 02:12:48 +00:00
|
|
|
style: Style {
|
|
|
|
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
justify_content: JustifyContent::Center,
|
|
|
|
align_items: AlignItems::FlexEnd,
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Color::NONE.into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
})
|
|
|
|
.with_children(|parent| {
|
|
|
|
// bevy logo (image)
|
2021-03-23 00:23:40 +00:00
|
|
|
parent.spawn_bundle(ImageBundle {
|
2020-07-28 02:12:48 +00:00
|
|
|
style: Style {
|
2020-07-28 07:37:25 +00:00
|
|
|
size: Size::new(Val::Px(500.0), Val::Auto),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
},
|
2021-12-14 03:58:23 +00:00
|
|
|
image: asset_server.load("branding/bevy_logo_dark_big.png").into(),
|
2022-03-01 20:52:09 +00:00
|
|
|
..default()
|
2020-07-28 02:12:48 +00:00
|
|
|
});
|
2020-07-10 04:18:35 +00:00
|
|
|
});
|
2020-07-26 19:10:18 +00:00
|
|
|
});
|
2020-01-11 09:59:39 +00:00
|
|
|
}
|
2021-12-19 05:44:28 +00:00
|
|
|
|
|
|
|
#[derive(Component, Default)]
|
|
|
|
struct ScrollingList {
|
|
|
|
position: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mouse_scroll(
|
|
|
|
mut mouse_wheel_events: EventReader<MouseWheel>,
|
|
|
|
mut query_list: Query<(&mut ScrollingList, &mut Style, &Children, &Node)>,
|
|
|
|
query_item: Query<&Node>,
|
|
|
|
) {
|
|
|
|
for mouse_wheel_event in mouse_wheel_events.iter() {
|
|
|
|
for (mut scrolling_list, mut style, children, uinode) in query_list.iter_mut() {
|
|
|
|
let items_height: f32 = children
|
|
|
|
.iter()
|
|
|
|
.map(|entity| query_item.get(*entity).unwrap().size.y)
|
|
|
|
.sum();
|
|
|
|
let panel_height = uinode.size.y;
|
|
|
|
let max_scroll = (items_height - panel_height).max(0.);
|
|
|
|
let dy = match mouse_wheel_event.unit {
|
|
|
|
MouseScrollUnit::Line => mouse_wheel_event.y * 20.,
|
|
|
|
MouseScrollUnit::Pixel => mouse_wheel_event.y,
|
|
|
|
};
|
|
|
|
scrolling_list.position += dy;
|
|
|
|
scrolling_list.position = scrolling_list.position.clamp(-max_scroll, 0.);
|
|
|
|
style.position.top = Val::Px(scrolling_list.position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|