1#![cfg_attr(
16 feature = "items",
17 doc = "| [`PickupNotificationPlugin`] | Timed pop-up text on item pickup, top-right by default |"
18)]
19#![cfg_attr(
20 not(feature = "items"),
21 doc = "| `PickupNotificationPlugin` *(feature `items`)* | Timed pop-up text on item pickup, top-right by default |"
22)]
23#![cfg_attr(
28 feature = "items",
29 doc = "Use [`UiPlugin`] when your game uses [`DefaultItem`] and wants all four HUD elements with zero configuration:"
30)]
31#![cfg_attr(
32 not(feature = "items"),
33 doc = "Use [`UiPlugin`] with the default `items` feature when your game uses `DefaultItem` and wants all four HUD elements with zero configuration:"
34)]
35use bevy::prelude::*;
78
79#[cfg(feature = "items")]
80use crate::items::inventory::ItemPickedUp;
81#[cfg(feature = "items")]
82use crate::items::inventory::ItemType;
83use crate::world::loading::{LoadingProgress, WorldLoadingState};
84
85pub mod hud;
87pub use hud::{
88 HudAnchor, HudBar, HudBindingKind, HudDefinitionError, HudDiagnostic, HudDiagnosticKind,
89 HudElementDefinition, HudElementId, HudElementSnapshot, HudIcon, HudIconSource, HudPlacement,
90 HudPresentation, HudRegistry, HudRegistryError, HudScalar, HudScalarField, HudSegments,
91 HudStackDirection, HudStyleOverride, HudSystemSet, HudText, HudTheme, HudValue,
92 HudValueChanged, HudValueError, HudValueId, HudValueKind, HudValueSet, HudValueSnapshot,
93 MAX_HUD_SEGMENTS, VoxelHudPlugin, remove_hud_value, set_hud_value,
94};
95
96#[derive(Resource, Debug, Clone)]
115pub struct CrosshairConfig {
116 pub color: [f32; 4],
119 pub size: f32,
122 pub gap: f32,
125 pub thickness: f32,
128 pub visible: bool,
132}
133
134impl Default for CrosshairConfig {
135 fn default() -> Self {
136 Self {
137 color: [1.0, 1.0, 1.0, 1.0],
138 size: 20.0,
139 gap: 4.0,
140 thickness: 2.0,
141 visible: true,
142 }
143 }
144}
145
146#[derive(Component)]
148struct CrosshairNode;
149
150fn spawn_crosshair(mut commands: Commands, config: Res<CrosshairConfig>) {
151 if !config.visible {
152 return;
153 }
154
155 let c = &*config;
156 let color = Color::srgba(c.color[0], c.color[1], c.color[2], c.color[3]);
157
158 commands
160 .spawn((
161 CrosshairNode,
162 Node {
163 width: Val::Percent(100.0),
164 height: Val::Percent(100.0),
165 position_type: PositionType::Absolute,
166 justify_content: JustifyContent::Center,
167 align_items: AlignItems::Center,
168 ..default()
169 },
170 BackgroundColor(Color::NONE),
171 ))
172 .with_children(|root| {
173 root.spawn((
175 CrosshairNode,
176 Node {
177 position_type: PositionType::Absolute,
178 width: Val::Px(c.size),
179 height: Val::Px(c.thickness),
180 right: Val::Px(c.gap + c.size / 2.0 + c.size / 2.0),
181 top: Val::Px(-c.thickness / 2.0),
182 ..default()
183 },
184 BackgroundColor(color),
185 ));
186 root.spawn((
188 CrosshairNode,
189 Node {
190 position_type: PositionType::Absolute,
191 width: Val::Px(c.size),
192 height: Val::Px(c.thickness),
193 left: Val::Px(c.gap + c.size / 2.0 - c.size / 2.0),
194 top: Val::Px(-c.thickness / 2.0),
195 ..default()
196 },
197 BackgroundColor(color),
198 ));
199 root.spawn((
201 CrosshairNode,
202 Node {
203 position_type: PositionType::Absolute,
204 width: Val::Px(c.thickness),
205 height: Val::Px(c.size),
206 left: Val::Px(-c.thickness / 2.0),
207 bottom: Val::Px(c.gap + c.size / 2.0 - c.size / 2.0),
208 ..default()
209 },
210 BackgroundColor(color),
211 ));
212 root.spawn((
214 CrosshairNode,
215 Node {
216 position_type: PositionType::Absolute,
217 width: Val::Px(c.thickness),
218 height: Val::Px(c.size),
219 left: Val::Px(-c.thickness / 2.0),
220 top: Val::Px(c.gap + c.size / 2.0 - c.size / 2.0),
221 ..default()
222 },
223 BackgroundColor(color),
224 ));
225 });
226}
227
228pub struct CrosshairPlugin;
252
253impl Plugin for CrosshairPlugin {
254 fn build(&self, app: &mut App) {
255 app.init_resource::<CrosshairConfig>();
256 app.add_systems(Startup, spawn_crosshair);
257 }
258}
259
260#[derive(Resource, Debug, Clone)]
277pub struct LoadingScreenConfig {
278 pub background_color: [f32; 4],
282 pub text_color: [f32; 4],
284 pub title: String,
287 pub show_progress_bar: bool,
291}
292
293impl Default for LoadingScreenConfig {
294 fn default() -> Self {
295 Self {
296 background_color: [0.05, 0.05, 0.05, 0.95],
297 text_color: [1.0, 1.0, 1.0, 1.0],
298 title: "Loading...".to_string(),
299 show_progress_bar: true,
300 }
301 }
302}
303
304#[derive(Resource, Debug, Clone, PartialEq)]
336pub struct LoadingScreenTheme {
337 pub bar_color: Color,
339 pub bar_background: Color,
341}
342
343impl Default for LoadingScreenTheme {
344 fn default() -> Self {
345 Self {
346 bar_color: Color::srgba(0.2, 0.7, 0.3, 1.0),
347 bar_background: Color::srgba(0.2, 0.2, 0.2, 1.0),
348 }
349 }
350}
351
352#[derive(Component)]
354struct LoadingOverlay;
355
356#[derive(Component)]
358struct LoadingText;
359
360#[derive(Component)]
362struct LoadingProgressBarTrack;
363
364#[derive(Component)]
366struct LoadingProgressBar;
367
368fn spawn_loading_overlay(
369 mut commands: Commands,
370 config: Res<LoadingScreenConfig>,
371 theme: Option<Res<LoadingScreenTheme>>,
372) {
373 let theme = theme.as_deref().cloned().unwrap_or_default();
375 let bg = Color::srgba(
376 config.background_color[0],
377 config.background_color[1],
378 config.background_color[2],
379 config.background_color[3],
380 );
381 let tc = Color::srgba(
382 config.text_color[0],
383 config.text_color[1],
384 config.text_color[2],
385 config.text_color[3],
386 );
387
388 commands
389 .spawn((
390 LoadingOverlay,
391 Node {
392 width: Val::Percent(100.0),
393 height: Val::Percent(100.0),
394 position_type: PositionType::Absolute,
395 flex_direction: FlexDirection::Column,
396 justify_content: JustifyContent::Center,
397 align_items: AlignItems::Center,
398 ..default()
399 },
400 BackgroundColor(bg),
401 ZIndex(100),
402 ))
403 .with_children(|parent| {
404 parent.spawn((
406 LoadingText,
407 Text::new(config.title.clone()),
408 TextFont {
409 font_size: 32.0,
410 ..default()
411 },
412 TextColor(tc),
413 ));
414
415 if config.show_progress_bar {
416 parent
418 .spawn((
419 LoadingProgressBarTrack,
420 Node {
421 width: Val::Px(400.0),
422 height: Val::Px(20.0),
423 margin: UiRect::top(Val::Px(20.0)),
424 ..default()
425 },
426 BackgroundColor(theme.bar_background),
427 ))
428 .with_children(|bar| {
429 bar.spawn((
431 LoadingProgressBar,
432 Node {
433 width: Val::Percent(0.0),
434 height: Val::Percent(100.0),
435 ..default()
436 },
437 BackgroundColor(theme.bar_color),
438 ));
439 });
440 }
441 });
442}
443
444fn update_loading_overlay(
445 loading_state: Res<WorldLoadingState>,
446 mut progress_reader: bevy::ecs::message::MessageReader<LoadingProgress>,
447 mut overlay_query: Query<&mut Visibility, With<LoadingOverlay>>,
448 mut text_query: Query<&mut Text, With<LoadingText>>,
449 mut bar_query: Query<&mut Node, With<LoadingProgressBar>>,
450) {
451 if loading_state.ready {
453 if let Ok(mut vis) = overlay_query.single_mut() {
454 *vis = Visibility::Hidden;
455 }
456 return;
457 }
458
459 let mut latest: Option<LoadingProgress> = None;
461 for msg in progress_reader.read() {
462 latest = Some(msg.clone());
463 }
464
465 if let Some(progress) = latest {
466 if let Ok(mut text) = text_query.single_mut() {
468 *text = Text::new(format!(
469 "Loading... {}/{} chunks",
470 progress.loaded, progress.total
471 ));
472 }
473
474 if let Ok(mut node) = bar_query.single_mut() {
476 node.width = Val::Percent((progress.progress * 100.0).clamp(0.0, 100.0));
477 }
478 }
479}
480
481fn despawn_loading_overlay_on_ready(
482 mut commands: Commands,
483 loading_state: Res<WorldLoadingState>,
484 overlay_query: Query<Entity, With<LoadingOverlay>>,
485) {
486 if loading_state.ready {
487 for entity in overlay_query.iter() {
488 commands.entity(entity).despawn();
489 }
490 }
491}
492
493pub struct LoadingScreenPlugin;
518
519impl Plugin for LoadingScreenPlugin {
520 fn build(&self, app: &mut App) {
521 app.init_resource::<LoadingScreenConfig>();
522 app.init_resource::<LoadingScreenTheme>();
523 app.add_systems(Startup, spawn_loading_overlay);
524 app.add_systems(
525 Update,
526 (update_loading_overlay, despawn_loading_overlay_on_ready),
527 );
528 }
529}
530
531#[cfg(feature = "items")]
533pub mod hotbar;
534#[cfg(feature = "items")]
535pub use hotbar::{HotbarContentPlugin, HotbarUiConfig, HotbarUiPlugin, ItemIconPalette};
536
537#[cfg(feature = "items")]
558#[derive(Resource, Debug, Clone)]
559pub struct PickupNotificationConfig {
560 pub visible: bool,
563 pub duration_secs: f32,
567 pub text_color: [f32; 4],
569 pub max_notifications: usize,
573}
574
575#[cfg(feature = "items")]
576impl Default for PickupNotificationConfig {
577 fn default() -> Self {
578 Self {
579 visible: true,
580 duration_secs: 3.0,
581 text_color: [1.0, 1.0, 1.0, 1.0],
582 max_notifications: 5,
583 }
584 }
585}
586
587#[cfg(feature = "items")]
651#[derive(Resource, Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
652pub enum PickupNotificationFilter {
653 #[default]
655 Every,
656 FirstPerItemType,
663 OnRequest,
666}
667
668#[cfg(feature = "items")]
678#[derive(Resource, Clone, Default)]
679pub struct PickupNotificationSeen<I: ItemType> {
680 seen: std::collections::HashSet<I>,
681}
682
683#[cfg(feature = "items")]
684impl<I: ItemType> PickupNotificationSeen<I> {
685 pub fn contains(&self, item: I) -> bool {
687 self.seen.contains(&item)
688 }
689
690 pub fn insert(&mut self, item: I) -> bool {
693 self.seen.insert(item)
694 }
695
696 pub fn clear(&mut self) {
698 self.seen.clear();
699 }
700
701 pub fn len(&self) -> usize {
703 self.seen.len()
704 }
705
706 pub fn is_empty(&self) -> bool {
708 self.seen.is_empty()
709 }
710}
711
712#[cfg(feature = "items")]
724#[derive(Message, Debug, Clone)]
725pub struct ShowPickupNotification<I: ItemType> {
726 pub item: I,
728 pub count: u32,
730}
731
732#[cfg(feature = "items")]
733impl<I: ItemType> ShowPickupNotification<I> {
734 pub fn new(item: I, count: u32) -> Self {
736 Self { item, count }
737 }
738}
739
740#[cfg(feature = "items")]
744#[derive(Debug, Clone, Copy, PartialEq)]
745pub struct PickupNotificationAccentBar {
746 pub color: [f32; 4],
749 pub width: f32,
752}
753
754#[cfg(feature = "items")]
759#[derive(Debug, Clone, PartialEq)]
760pub struct PickupNotificationLabel {
761 pub text: String,
763 pub color: [f32; 4],
766}
767
768#[cfg(feature = "items")]
789#[derive(Resource, Debug, Clone, PartialEq)]
790pub struct PickupNotificationLayout {
791 pub anchor: HudAnchor,
795 pub offset: [f32; 2],
799 pub background: Option<[f32; 4]>,
802 pub padding: [f32; 2],
806 pub accent_bar: Option<PickupNotificationAccentBar>,
808 pub label: Option<PickupNotificationLabel>,
810}
811
812#[cfg(feature = "items")]
813impl Default for PickupNotificationLayout {
814 fn default() -> Self {
815 Self {
816 anchor: HudAnchor::TopRight,
817 offset: [16.0, 16.0],
818 background: None,
819 padding: [0.0, 0.0],
820 accent_bar: None,
821 label: None,
822 }
823 }
824}
825
826#[cfg(feature = "items")]
828const NOTIFICATION_FONT_SIZE: f32 = 18.0;
829
830#[cfg(feature = "items")]
832#[derive(Component)]
833struct NotificationTimer(Timer);
834
835#[cfg(feature = "items")]
837#[derive(Component)]
838struct NotificationContainer;
839
840#[cfg(feature = "items")]
842#[derive(Component)]
843struct NotificationAccentBar;
844
845#[cfg(feature = "items")]
847#[derive(Component)]
848struct NotificationLabel;
849
850#[cfg(feature = "items")]
852#[derive(Clone, Copy)]
853enum NotificationAxis {
854 Start,
855 Center,
856 End,
857}
858
859#[cfg(feature = "items")]
861fn notification_axes(anchor: HudAnchor) -> (NotificationAxis, NotificationAxis) {
862 use NotificationAxis::{Center, End, Start};
863 match anchor {
864 HudAnchor::TopLeft => (Start, Start),
865 HudAnchor::TopCenter => (Center, Start),
866 HudAnchor::TopRight => (End, Start),
867 HudAnchor::CenterLeft => (Start, Center),
868 HudAnchor::Center => (Center, Center),
869 HudAnchor::CenterRight => (End, Center),
870 HudAnchor::BottomLeft => (Start, End),
871 HudAnchor::BottomCenter => (Center, End),
872 HudAnchor::BottomRight => (End, End),
873 }
874}
875
876#[cfg(feature = "items")]
877fn notification_align(axis: NotificationAxis) -> AlignItems {
878 match axis {
879 NotificationAxis::Start => AlignItems::FlexStart,
880 NotificationAxis::Center => AlignItems::Center,
881 NotificationAxis::End => AlignItems::FlexEnd,
882 }
883}
884
885#[cfg(feature = "items")]
886fn finite_or_zero(value: f32) -> f32 {
887 if value.is_finite() { value } else { 0.0 }
888}
889
890#[cfg(feature = "items")]
891fn positive_or_zero(value: f32) -> f32 {
892 if value.is_finite() && value > 0.0 {
893 value
894 } else {
895 0.0
896 }
897}
898
899#[cfg(feature = "items")]
900fn notification_color(rgba: [f32; 4]) -> Color {
901 Color::srgba(rgba[0], rgba[1], rgba[2], rgba[3])
902}
903
904#[cfg(feature = "items")]
909fn notification_container_node(layout: &PickupNotificationLayout) -> Node {
910 let [x, y] = layout.offset.map(finite_or_zero);
911 let (horizontal, vertical) = notification_axes(layout.anchor);
912 let mut node = Node {
913 position_type: PositionType::Absolute,
914 flex_direction: FlexDirection::Column,
915 row_gap: Val::Px(4.0),
916 align_items: notification_align(horizontal),
917 ..default()
918 };
919 match horizontal {
920 NotificationAxis::Start => node.left = Val::Px(x),
921 NotificationAxis::Center => {
922 node.left = Val::Px(x);
923 node.right = Val::Px(-x);
924 }
925 NotificationAxis::End => node.right = Val::Px(x),
926 }
927 match vertical {
928 NotificationAxis::Start => node.top = Val::Px(y),
929 NotificationAxis::Center => {
930 node.top = Val::Px(y);
931 node.bottom = Val::Px(-y);
932 node.justify_content = JustifyContent::Center;
933 }
934 NotificationAxis::End => node.bottom = Val::Px(y),
935 }
936 node
937}
938
939#[cfg(feature = "items")]
940fn spawn_notification_container(mut commands: Commands, layout: Res<PickupNotificationLayout>) {
941 commands.spawn((
942 NotificationContainer,
943 notification_container_node(&layout),
944 BackgroundColor(Color::NONE),
945 ));
946}
947
948#[cfg(feature = "items")]
950fn apply_notification_layout(
951 layout: Res<PickupNotificationLayout>,
952 mut container_query: Query<&mut Node, With<NotificationContainer>>,
953) {
954 if !layout.is_changed() {
955 return;
956 }
957 let desired = notification_container_node(&layout);
958 for mut node in container_query.iter_mut() {
959 if *node != desired {
960 *node = desired.clone();
961 }
962 }
963}
964
965#[cfg(feature = "items")]
971fn spawn_notification(
972 commands: &mut Commands,
973 container: Entity,
974 config: &PickupNotificationConfig,
975 layout: &PickupNotificationLayout,
976 text: String,
977) {
978 let timer = NotificationTimer(Timer::from_seconds(config.duration_secs, TimerMode::Once));
979 let text_font = TextFont {
980 font_size: NOTIFICATION_FONT_SIZE,
981 ..default()
982 };
983 let item_line = (
984 Text::new(text),
985 text_font.clone(),
986 TextColor(notification_color(config.text_color)),
987 Node { ..default() },
988 );
989
990 let [pad_x, pad_y] = layout.padding.map(positive_or_zero);
991 let accent_bar = layout
992 .accent_bar
993 .filter(|bar| positive_or_zero(bar.width) > 0.0);
994 let decorated = layout.background.is_some()
995 || accent_bar.is_some()
996 || layout.label.is_some()
997 || pad_x > 0.0
998 || pad_y > 0.0;
999
1000 if !decorated {
1001 let notification = commands.spawn((timer, item_line)).id();
1002 commands.entity(container).add_child(notification);
1003 return;
1004 }
1005
1006 let notification = commands
1007 .spawn((
1008 timer,
1009 Node {
1010 flex_direction: FlexDirection::Row,
1011 align_items: AlignItems::Stretch,
1012 ..default()
1013 },
1014 BackgroundColor(
1015 layout
1016 .background
1017 .map(notification_color)
1018 .unwrap_or(Color::NONE),
1019 ),
1020 ))
1021 .id();
1022
1023 if let Some(bar) = accent_bar {
1024 let bar_entity = commands
1025 .spawn((
1026 NotificationAccentBar,
1027 Node {
1028 width: Val::Px(bar.width),
1029 ..default()
1030 },
1031 BackgroundColor(notification_color(bar.color)),
1032 ))
1033 .id();
1034 commands.entity(notification).add_child(bar_entity);
1035 }
1036
1037 let (horizontal, _) = notification_axes(layout.anchor);
1038 let body = commands
1039 .spawn(Node {
1040 flex_direction: FlexDirection::Column,
1041 align_items: notification_align(horizontal),
1042 padding: UiRect::axes(Val::Px(pad_x), Val::Px(pad_y)),
1043 ..default()
1044 })
1045 .id();
1046 commands.entity(notification).add_child(body);
1047
1048 if let Some(label) = &layout.label {
1049 let label_entity = commands
1050 .spawn((
1051 NotificationLabel,
1052 Text::new(label.text.clone()),
1053 text_font,
1054 TextColor(notification_color(label.color)),
1055 Node { ..default() },
1056 ))
1057 .id();
1058 commands.entity(body).add_child(label_entity);
1059 }
1060
1061 let item_entity = commands.spawn(item_line).id();
1062 commands.entity(body).add_child(item_entity);
1063 commands.entity(container).add_child(notification);
1064}
1065
1066#[cfg(feature = "items")]
1067#[allow(clippy::too_many_arguments)]
1068fn on_item_picked_up<I: ItemType>(
1069 mut commands: Commands,
1070 mut pickup_reader: bevy::ecs::message::MessageReader<ItemPickedUp<I>>,
1071 mut request_reader: bevy::ecs::message::MessageReader<ShowPickupNotification<I>>,
1072 config: Res<PickupNotificationConfig>,
1073 filter: Res<PickupNotificationFilter>,
1074 layout: Res<PickupNotificationLayout>,
1075 mut seen: ResMut<PickupNotificationSeen<I>>,
1076 container_query: Query<Entity, With<NotificationContainer>>,
1077 existing_query: Query<Entity, With<NotificationTimer>>,
1078) {
1079 if !config.visible {
1080 return;
1081 }
1082
1083 let mut pending: Vec<(I, u32, bool)> = Vec::new();
1087 for pickup in pickup_reader.read() {
1088 match *filter {
1089 PickupNotificationFilter::Every => pending.push((pickup.item, pickup.count, false)),
1090 PickupNotificationFilter::FirstPerItemType => {
1091 let queued = pending
1092 .iter()
1093 .any(|(item, _, first_of_type)| *first_of_type && *item == pickup.item);
1094 if !queued && !seen.contains(pickup.item) {
1095 pending.push((pickup.item, pickup.count, true));
1096 }
1097 }
1098 PickupNotificationFilter::OnRequest => {}
1099 }
1100 }
1101 for request in request_reader.read() {
1102 pending.push((request.item, request.count, false));
1103 }
1104
1105 if pending.is_empty() {
1106 return;
1107 }
1108
1109 let Ok(container) = container_query.single() else {
1110 return;
1111 };
1112
1113 let existing: Vec<Entity> = existing_query.iter().collect();
1115 let available = config.max_notifications.saturating_sub(existing.len());
1116 let spawn_count = pending.len().min(available);
1117
1118 if existing.len() + pending.len() > config.max_notifications {
1119 let overflow = (existing.len() + pending.len()).saturating_sub(config.max_notifications);
1120 for entity in existing.iter().take(overflow) {
1121 commands.entity(*entity).despawn();
1122 }
1123 }
1124
1125 for (item, count, first_of_type) in pending.into_iter().take(spawn_count) {
1126 if first_of_type {
1127 seen.insert(item);
1128 }
1129 let text = format!("+{count} {}", item.name());
1130 spawn_notification(&mut commands, container, &config, &layout, text);
1131 }
1132}
1133
1134#[cfg(feature = "items")]
1135fn tick_notifications(
1136 mut commands: Commands,
1137 time: Res<Time>,
1138 mut query: Query<(Entity, &mut NotificationTimer)>,
1139) {
1140 for (entity, mut timer) in query.iter_mut() {
1141 timer.0.tick(time.delta());
1142 if timer.0.just_finished() {
1143 commands.entity(entity).despawn();
1144 }
1145 }
1146}
1147
1148#[cfg(feature = "items")]
1186pub struct PickupNotificationPlugin<I: ItemType> {
1187 _marker: std::marker::PhantomData<I>,
1188}
1189
1190#[cfg(feature = "items")]
1191impl<I: ItemType> PickupNotificationPlugin<I> {
1192 pub fn new() -> Self {
1193 Self {
1194 _marker: std::marker::PhantomData,
1195 }
1196 }
1197}
1198
1199#[cfg(feature = "items")]
1200impl<I: ItemType> Default for PickupNotificationPlugin<I> {
1201 fn default() -> Self {
1202 Self::new()
1203 }
1204}
1205
1206#[cfg(feature = "items")]
1207impl<I: ItemType> Plugin for PickupNotificationPlugin<I> {
1208 fn build(&self, app: &mut App) {
1209 app.init_resource::<PickupNotificationConfig>();
1210 app.init_resource::<PickupNotificationFilter>();
1211 app.init_resource::<PickupNotificationLayout>();
1212 app.init_resource::<PickupNotificationSeen<I>>();
1213 app.add_message::<ItemPickedUp<I>>();
1215 app.add_message::<ShowPickupNotification<I>>();
1216 app.add_systems(Startup, spawn_notification_container);
1217 app.add_systems(
1218 Update,
1219 (
1220 apply_notification_layout,
1221 on_item_picked_up::<I>,
1222 tick_notifications,
1223 ),
1224 );
1225 }
1226}
1227
1228mod pause_menu;
1229pub use pause_menu::{
1230 ButtonColors, PauseMenuConfig, PauseMenuPlugin, PauseMenuState, PauseMenuTheme,
1231 SaveSettingsRequest,
1232};
1233
1234use crate::chunk::world_to_chunk_coord;
1237use crate::player::Player;
1238use crate::world::ChunkManager;
1239use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin};
1240
1241#[derive(Resource, Default)]
1243pub struct DebugOverlayState {
1244 pub visible: bool,
1246}
1247
1248#[derive(Component)]
1250struct DebugOverlay;
1251
1252#[derive(Component)]
1254struct DebugLine(usize);
1255
1256fn spawn_debug_overlay(mut commands: Commands) {
1257 commands
1258 .spawn((
1259 DebugOverlay,
1260 Node {
1261 position_type: PositionType::Absolute,
1262 top: Val::Px(8.0),
1263 left: Val::Px(8.0),
1264 flex_direction: FlexDirection::Column,
1265 row_gap: Val::Px(2.0),
1266 padding: UiRect::all(Val::Px(6.0)),
1267 ..default()
1268 },
1269 BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.6)),
1270 Visibility::Hidden,
1271 ZIndex(200),
1272 ))
1273 .with_children(|root| {
1274 let style = TextFont {
1275 font_size: 14.0,
1276 ..default()
1277 };
1278 let color = TextColor(Color::WHITE);
1279 for i in 0..4 {
1280 root.spawn((DebugLine(i), Text::new(""), style.clone(), color));
1281 }
1282 });
1283}
1284
1285fn update_debug_overlay(
1286 keys: Res<ButtonInput<KeyCode>>,
1287 mut state: ResMut<DebugOverlayState>,
1288 mut overlay_q: Query<&mut Visibility, With<DebugOverlay>>,
1289 mut line_q: Query<(&DebugLine, &mut Text)>,
1290 player_q: Query<&Transform, With<Player>>,
1291 manager: Res<ChunkManager>,
1292 diagnostics: Res<DiagnosticsStore>,
1293) {
1294 if keys.just_pressed(KeyCode::F3) {
1296 state.visible = !state.visible;
1297 if let Ok(mut vis) = overlay_q.single_mut() {
1298 *vis = if state.visible {
1299 Visibility::Visible
1300 } else {
1301 Visibility::Hidden
1302 };
1303 }
1304 }
1305
1306 if !state.visible {
1307 return;
1308 }
1309
1310 let pos = player_q
1311 .single()
1312 .map(|t| t.translation)
1313 .unwrap_or(Vec3::ZERO);
1314
1315 let chunk = world_to_chunk_coord(pos);
1316 let fps = diagnostics
1317 .get(&FrameTimeDiagnosticsPlugin::FPS)
1318 .and_then(|d| d.smoothed())
1319 .unwrap_or(0.0);
1320 let chunk_count = manager.loaded_count();
1321
1322 let lines = [
1323 format!("XYZ: {:.1} / {:.1} / {:.1}", pos.x, pos.y, pos.z),
1324 format!("Chunk: {} {} {}", chunk.x, chunk.y, chunk.z),
1325 format!("FPS: {fps:.0}"),
1326 format!("Chunks: {chunk_count}"),
1327 ];
1328
1329 for (line_marker, mut text) in line_q.iter_mut() {
1330 if let Some(content) = lines.get(line_marker.0) {
1331 *text = Text::new(content.clone());
1332 }
1333 }
1334}
1335
1336pub struct DebugOverlayPlugin;
1352
1353impl Plugin for DebugOverlayPlugin {
1354 fn build(&self, app: &mut App) {
1355 app.add_plugins(FrameTimeDiagnosticsPlugin::default());
1356 app.init_resource::<DebugOverlayState>();
1357 app.add_systems(Startup, spawn_debug_overlay);
1358 app.add_systems(Update, update_debug_overlay);
1359 }
1360}
1361
1362#[cfg(feature = "items")]
1367use crate::items::inventory::DefaultItem;
1368
1369#[cfg_attr(
1370 feature = "items",
1371 doc = "Umbrella plugin that adds all four HUD sub-plugins with [`DefaultItem`]."
1372)]
1373#[cfg_attr(
1374 not(feature = "items"),
1375 doc = "Umbrella plugin for the built-in HUD sub-plugins; item-specific overlays require the `items` feature."
1376)]
1377#[cfg_attr(
1379 feature = "items",
1380 doc = "Equivalent to adding [`CrosshairPlugin`], [`LoadingScreenPlugin`], [`HotbarUiPlugin`], and [`PickupNotificationPlugin::<DefaultItem>`] in one call. Configuration resources for each sub-plugin can still be inserted individually before `UiPlugin` is added to override their defaults."
1381)]
1382#[cfg_attr(
1383 not(feature = "items"),
1384 doc = "Without the `items` feature, this adds [`CrosshairPlugin`] and [`LoadingScreenPlugin`]. Configuration resources for each sub-plugin can still be inserted individually before `UiPlugin` is added to override their defaults."
1385)]
1386#[cfg_attr(
1388 feature = "items",
1389 doc = "To use a custom item type for pickup notifications, skip `UiPlugin` and add [`PickupNotificationPlugin::<YourItem>::new()`] alongside the other plugins individually."
1390)]
1391#[cfg_attr(
1392 not(feature = "items"),
1393 doc = "Enable the `items` feature before configuring item-specific pickup notifications or hotbar UI."
1394)]
1395pub struct UiPlugin;
1407
1408impl Plugin for UiPlugin {
1409 fn build(&self, app: &mut App) {
1410 app.add_plugins(CrosshairPlugin);
1411 app.add_plugins(LoadingScreenPlugin);
1412 #[cfg(feature = "items")]
1413 app.add_plugins(HotbarUiPlugin);
1414 #[cfg(feature = "items")]
1415 app.add_plugins(PickupNotificationPlugin::<DefaultItem>::new());
1416 }
1417}
1418
1419#[cfg(test)]
1420mod loading_screen_tests {
1421 use super::*;
1422 use bevy::ecs::system::RunSystemOnce;
1423
1424 fn spawn_overlay(world: &mut World) {
1425 world
1426 .run_system_once(spawn_loading_overlay)
1427 .expect("spawn_loading_overlay should run");
1428 }
1429
1430 fn bar_fill(world: &mut World) -> (Color, Entity) {
1432 let mut query =
1433 world.query_filtered::<(&BackgroundColor, &ChildOf), With<LoadingProgressBar>>();
1434 let (bg, child_of) = query.single(world).expect("one progress bar fill");
1435 (bg.0, child_of.parent())
1436 }
1437
1438 fn bar_track(world: &mut World) -> (Color, Entity) {
1440 let mut query =
1441 world.query_filtered::<(&BackgroundColor, Entity), With<LoadingProgressBarTrack>>();
1442 let (bg, entity) = query.single(world).expect("one progress bar track");
1443 (bg.0, entity)
1444 }
1445
1446 #[test]
1447 fn default_loading_theme_equals_the_previous_constants() {
1448 let theme = LoadingScreenTheme::default();
1449 assert_eq!(theme.bar_color, Color::srgba(0.2, 0.7, 0.3, 1.0));
1450 assert_eq!(theme.bar_background, Color::srgba(0.2, 0.2, 0.2, 1.0));
1451 }
1452
1453 #[test]
1454 fn loading_bar_without_a_theme_resource_keeps_the_previous_colors() {
1455 let mut world = World::new();
1456 world.insert_resource(LoadingScreenConfig::default());
1457
1458 spawn_overlay(&mut world);
1459
1460 let (track_color, track) = bar_track(&mut world);
1461 let (fill_color, fill_parent) = bar_fill(&mut world);
1462 assert_eq!(track_color, Color::srgba(0.2, 0.2, 0.2, 1.0));
1463 assert_eq!(fill_color, Color::srgba(0.2, 0.7, 0.3, 1.0));
1464 assert_eq!(fill_parent, track);
1465 }
1466
1467 #[test]
1468 fn custom_loading_theme_colors_the_bar_and_its_track() {
1469 let theme = LoadingScreenTheme {
1470 bar_color: Color::srgb(0.9, 0.1, 0.2),
1471 bar_background: Color::srgb(0.1, 0.2, 0.9),
1472 };
1473 let mut world = World::new();
1474 world.insert_resource(LoadingScreenConfig::default());
1475 world.insert_resource(theme.clone());
1476
1477 spawn_overlay(&mut world);
1478
1479 let (track_color, track) = bar_track(&mut world);
1480 let (fill_color, fill_parent) = bar_fill(&mut world);
1481 assert_eq!(track_color, theme.bar_background);
1482 assert_eq!(fill_color, theme.bar_color);
1483 assert_eq!(fill_parent, track);
1484
1485 let mut overlay = world.query_filtered::<&BackgroundColor, With<LoadingOverlay>>();
1487 assert_eq!(
1488 overlay.single(&world).expect("one overlay").0,
1489 Color::srgba(0.05, 0.05, 0.05, 0.95)
1490 );
1491 }
1492
1493 #[test]
1494 fn loading_theme_is_unused_when_the_bar_is_hidden() {
1495 let mut world = World::new();
1496 world.insert_resource(LoadingScreenConfig {
1497 show_progress_bar: false,
1498 ..Default::default()
1499 });
1500 world.insert_resource(LoadingScreenTheme::default());
1501
1502 spawn_overlay(&mut world);
1503
1504 let mut tracks = world.query_filtered::<Entity, With<LoadingProgressBarTrack>>();
1505 assert_eq!(tracks.iter(&world).count(), 0);
1506 let mut fills = world.query_filtered::<Entity, With<LoadingProgressBar>>();
1507 assert_eq!(fills.iter(&world).count(), 0);
1508 }
1509}
1510
1511#[cfg(all(test, feature = "items"))]
1512mod pickup_notification_tests {
1513 use super::*;
1514 use crate::items::inventory::DefaultItem;
1515
1516 const BACKGROUND: [f32; 4] = [0.0, 0.0, 0.0, 0.6];
1517 const ACCENT: [f32; 4] = [1.0, 0.8, 0.2, 1.0];
1518 const LABEL: [f32; 4] = [0.4, 0.9, 1.0, 1.0];
1519
1520 fn app() -> App {
1521 let mut app = App::new();
1522 app.add_plugins((
1523 MinimalPlugins,
1524 PickupNotificationPlugin::<DefaultItem>::new(),
1525 ));
1526 app
1527 }
1528
1529 fn pick_up(app: &mut App, item: DefaultItem, count: u32) {
1530 app.world_mut().write_message(ItemPickedUp { item, count });
1531 app.update();
1532 }
1533
1534 fn line(item: DefaultItem, count: u32) -> String {
1535 format!("+{count} {}", item.name())
1536 }
1537
1538 fn notifications(app: &mut App) -> Vec<Entity> {
1540 let world = app.world_mut();
1541 let mut query = world.query_filtered::<Entity, With<NotificationTimer>>();
1542 query.iter(world).collect()
1543 }
1544
1545 fn item_lines(app: &mut App) -> Vec<String> {
1547 let world = app.world_mut();
1548 let mut query = world.query_filtered::<&Text, Without<NotificationLabel>>();
1549 let mut lines: Vec<String> = query.iter(world).map(|text| text.0.clone()).collect();
1550 lines.sort();
1551 lines
1552 }
1553
1554 fn container(app: &mut App) -> (Entity, Node) {
1555 let world = app.world_mut();
1556 let mut query = world.query_filtered::<(Entity, &Node), With<NotificationContainer>>();
1557 let (entity, node) = query.single(world).unwrap();
1558 (entity, node.clone())
1559 }
1560
1561 fn children(app: &App, entity: Entity) -> Vec<Entity> {
1562 app.world()
1563 .get::<Children>(entity)
1564 .map(|children| children.to_vec())
1565 .unwrap_or_default()
1566 }
1567
1568 fn text(app: &App, entity: Entity) -> String {
1569 app.world().get::<Text>(entity).unwrap().0.clone()
1570 }
1571
1572 #[test]
1573 fn defaults_keep_the_top_right_plain_text() {
1574 let mut app = app();
1575 app.update();
1576 assert_eq!(
1577 *app.world().resource::<PickupNotificationFilter>(),
1578 PickupNotificationFilter::Every
1579 );
1580 assert_eq!(
1581 *app.world().resource::<PickupNotificationLayout>(),
1582 PickupNotificationLayout {
1583 anchor: HudAnchor::TopRight,
1584 offset: [16.0, 16.0],
1585 background: None,
1586 padding: [0.0, 0.0],
1587 accent_bar: None,
1588 label: None,
1589 }
1590 );
1591 let (container_entity, container_node) = container(&mut app);
1592 assert_eq!(
1593 container_node,
1594 Node {
1595 position_type: PositionType::Absolute,
1596 top: Val::Px(16.0),
1597 right: Val::Px(16.0),
1598 flex_direction: FlexDirection::Column,
1599 row_gap: Val::Px(4.0),
1600 align_items: AlignItems::FlexEnd,
1601 ..default()
1602 }
1603 );
1604
1605 pick_up(&mut app, DefaultItem::Dirt, 2);
1606 let entities = notifications(&mut app);
1607 assert_eq!(entities.len(), 1);
1608 let entity = entities[0];
1609 assert!(children(&app, entity).is_empty());
1610 assert_eq!(text(&app, entity), line(DefaultItem::Dirt, 2));
1611 let world = app.world();
1612 assert_eq!(world.get::<TextFont>(entity).unwrap().font_size, 18.0);
1613 assert_eq!(
1614 world.get::<TextColor>(entity).unwrap().0,
1615 Color::srgba(1.0, 1.0, 1.0, 1.0)
1616 );
1617 assert_eq!(*world.get::<Node>(entity).unwrap(), Node::default());
1618 assert_eq!(world.get::<BackgroundColor>(entity).unwrap().0, Color::NONE);
1619 assert_eq!(
1620 world.get::<ChildOf>(entity).unwrap().parent(),
1621 container_entity
1622 );
1623 }
1624
1625 #[test]
1626 fn plugin_keeps_resources_inserted_by_the_game() {
1627 let layout = PickupNotificationLayout {
1628 anchor: HudAnchor::BottomRight,
1629 ..default()
1630 };
1631 let mut app = App::new();
1632 app.insert_resource(PickupNotificationFilter::OnRequest)
1633 .insert_resource(layout.clone())
1634 .add_plugins((
1635 MinimalPlugins,
1636 PickupNotificationPlugin::<DefaultItem>::new(),
1637 ));
1638 app.update();
1639 assert_eq!(
1640 *app.world().resource::<PickupNotificationFilter>(),
1641 PickupNotificationFilter::OnRequest
1642 );
1643 assert_eq!(*app.world().resource::<PickupNotificationLayout>(), layout);
1644 }
1645
1646 #[test]
1647 fn every_filter_notifies_each_pickup() {
1648 let mut app = app();
1649 pick_up(&mut app, DefaultItem::Dirt, 1);
1650 pick_up(&mut app, DefaultItem::Dirt, 3);
1651 assert_eq!(
1652 item_lines(&mut app),
1653 [line(DefaultItem::Dirt, 1), line(DefaultItem::Dirt, 3)]
1654 );
1655 assert!(
1656 app.world()
1657 .resource::<PickupNotificationSeen<DefaultItem>>()
1658 .is_empty()
1659 );
1660 }
1661
1662 #[test]
1663 fn first_per_item_type_notifies_once_per_type() {
1664 let mut app = app();
1665 app.insert_resource(PickupNotificationFilter::FirstPerItemType);
1666 pick_up(&mut app, DefaultItem::Dirt, 1);
1667 pick_up(&mut app, DefaultItem::Dirt, 5);
1668 assert_eq!(item_lines(&mut app), [line(DefaultItem::Dirt, 1)]);
1669
1670 pick_up(&mut app, DefaultItem::Sand, 2);
1671 assert_eq!(
1672 item_lines(&mut app),
1673 [line(DefaultItem::Dirt, 1), line(DefaultItem::Sand, 2)]
1674 );
1675 let seen = app
1676 .world()
1677 .resource::<PickupNotificationSeen<DefaultItem>>();
1678 assert!(seen.contains(DefaultItem::Dirt));
1679 assert!(seen.contains(DefaultItem::Sand));
1680 assert_eq!(seen.len(), 2);
1681 }
1682
1683 #[test]
1684 fn first_per_item_type_merges_pickups_of_one_frame() {
1685 let mut app = app();
1686 app.insert_resource(PickupNotificationFilter::FirstPerItemType);
1687 app.world_mut().write_message(ItemPickedUp {
1688 item: DefaultItem::Wood,
1689 count: 1,
1690 });
1691 app.world_mut().write_message(ItemPickedUp {
1692 item: DefaultItem::Wood,
1693 count: 4,
1694 });
1695 app.update();
1696 assert_eq!(item_lines(&mut app), [line(DefaultItem::Wood, 1)]);
1697 }
1698
1699 #[test]
1700 fn seen_record_can_be_restored_and_cleared_by_the_game() {
1701 let mut app = app();
1702 app.insert_resource(PickupNotificationFilter::FirstPerItemType);
1703 app.world_mut()
1704 .resource_mut::<PickupNotificationSeen<DefaultItem>>()
1705 .insert(DefaultItem::Dirt);
1706 pick_up(&mut app, DefaultItem::Dirt, 1);
1707 assert!(notifications(&mut app).is_empty());
1708
1709 app.world_mut()
1710 .resource_mut::<PickupNotificationSeen<DefaultItem>>()
1711 .clear();
1712 pick_up(&mut app, DefaultItem::Dirt, 2);
1713 assert_eq!(item_lines(&mut app), [line(DefaultItem::Dirt, 2)]);
1714 }
1715
1716 #[test]
1717 fn pickup_that_cannot_be_shown_is_not_recorded_as_seen() {
1718 let mut app = app();
1719 app.insert_resource(PickupNotificationFilter::FirstPerItemType)
1720 .insert_resource(PickupNotificationConfig {
1721 max_notifications: 0,
1722 ..default()
1723 });
1724 pick_up(&mut app, DefaultItem::Dirt, 1);
1725 assert!(notifications(&mut app).is_empty());
1726 assert!(
1727 app.world()
1728 .resource::<PickupNotificationSeen<DefaultItem>>()
1729 .is_empty()
1730 );
1731
1732 app.world_mut()
1733 .resource_mut::<PickupNotificationConfig>()
1734 .max_notifications = 5;
1735 pick_up(&mut app, DefaultItem::Dirt, 3);
1736 assert_eq!(item_lines(&mut app), [line(DefaultItem::Dirt, 3)]);
1737 }
1738
1739 #[test]
1740 fn on_request_shows_only_what_the_game_asks_for() {
1741 let mut app = app();
1742 app.insert_resource(PickupNotificationFilter::OnRequest);
1743 pick_up(&mut app, DefaultItem::Dirt, 1);
1744 assert!(notifications(&mut app).is_empty());
1745
1746 app.world_mut()
1747 .write_message(ShowPickupNotification::new(DefaultItem::Sand, 7));
1748 app.update();
1749 assert_eq!(item_lines(&mut app), [line(DefaultItem::Sand, 7)]);
1750 }
1751
1752 #[test]
1753 fn request_is_shown_under_other_filters_without_touching_seen() {
1754 let mut app = app();
1755 app.insert_resource(PickupNotificationFilter::FirstPerItemType);
1756 app.world_mut()
1757 .resource_mut::<PickupNotificationSeen<DefaultItem>>()
1758 .insert(DefaultItem::Dirt);
1759 app.world_mut()
1760 .write_message(ShowPickupNotification::new(DefaultItem::Dirt, 2));
1761 app.world_mut()
1762 .write_message(ShowPickupNotification::new(DefaultItem::Sand, 1));
1763 app.update();
1764 assert_eq!(
1765 item_lines(&mut app),
1766 [line(DefaultItem::Sand, 1), line(DefaultItem::Dirt, 2)]
1767 );
1768 let seen = app
1769 .world()
1770 .resource::<PickupNotificationSeen<DefaultItem>>();
1771 assert!(!seen.contains(DefaultItem::Sand));
1772 assert_eq!(seen.len(), 1);
1773 }
1774
1775 #[test]
1776 fn anchor_and_offset_place_the_container() {
1777 let mut app = app();
1778 app.insert_resource(PickupNotificationLayout {
1779 anchor: HudAnchor::BottomLeft,
1780 offset: [24.0, 40.0],
1781 ..default()
1782 });
1783 app.update();
1784 assert_eq!(
1785 container(&mut app).1,
1786 Node {
1787 position_type: PositionType::Absolute,
1788 left: Val::Px(24.0),
1789 bottom: Val::Px(40.0),
1790 flex_direction: FlexDirection::Column,
1791 row_gap: Val::Px(4.0),
1792 align_items: AlignItems::FlexStart,
1793 ..default()
1794 }
1795 );
1796 }
1797
1798 #[test]
1799 fn centered_anchor_spans_the_screen_shifted_by_the_offset() {
1800 let mut app = app();
1801 app.insert_resource(PickupNotificationLayout {
1802 anchor: HudAnchor::Center,
1803 offset: [10.0, -20.0],
1804 ..default()
1805 });
1806 app.update();
1807 assert_eq!(
1808 container(&mut app).1,
1809 Node {
1810 position_type: PositionType::Absolute,
1811 left: Val::Px(10.0),
1812 right: Val::Px(-10.0),
1813 top: Val::Px(-20.0),
1814 bottom: Val::Px(20.0),
1815 flex_direction: FlexDirection::Column,
1816 row_gap: Val::Px(4.0),
1817 align_items: AlignItems::Center,
1818 justify_content: JustifyContent::Center,
1819 ..default()
1820 }
1821 );
1822 }
1823
1824 #[test]
1825 fn layout_change_at_runtime_moves_the_container() {
1826 let mut app = app();
1827 app.update();
1828 assert_eq!(container(&mut app).1.right, Val::Px(16.0));
1829
1830 app.insert_resource(PickupNotificationLayout {
1831 anchor: HudAnchor::TopLeft,
1832 offset: [8.0, f32::NAN],
1833 ..default()
1834 });
1835 app.update();
1836 let node = container(&mut app).1;
1837 assert_eq!(node.left, Val::Px(8.0));
1838 assert_eq!(node.top, Val::Px(0.0));
1839 assert_eq!(node.right, Val::Auto);
1840 assert_eq!(node.align_items, AlignItems::FlexStart);
1841 }
1842
1843 #[test]
1844 fn background_accent_bar_and_label_are_spawned_on_the_notification() {
1845 let mut app = app();
1846 app.insert_resource(PickupNotificationLayout {
1847 background: Some(BACKGROUND),
1848 padding: [10.0, 6.0],
1849 accent_bar: Some(PickupNotificationAccentBar {
1850 color: ACCENT,
1851 width: 4.0,
1852 }),
1853 label: Some(PickupNotificationLabel {
1854 text: "New item".to_string(),
1855 color: LABEL,
1856 }),
1857 ..default()
1858 });
1859 pick_up(&mut app, DefaultItem::Dirt, 1);
1860
1861 let entities = notifications(&mut app);
1862 assert_eq!(entities.len(), 1);
1863 let root = entities[0];
1864 let root_children = children(&app, root);
1865 assert_eq!(root_children.len(), 2);
1866 let (bar, body) = (root_children[0], root_children[1]);
1867 let body_children = children(&app, body);
1868 assert_eq!(body_children.len(), 2);
1869 let (label, item) = (body_children[0], body_children[1]);
1870 assert_eq!(text(&app, label), "New item");
1871 assert_eq!(text(&app, item), line(DefaultItem::Dirt, 1));
1872
1873 let world = app.world();
1874 assert!(world.get::<Text>(root).is_none());
1875 assert_eq!(
1876 world.get::<BackgroundColor>(root).unwrap().0,
1877 notification_color(BACKGROUND)
1878 );
1879 assert_eq!(
1880 world.get::<Node>(root).unwrap().flex_direction,
1881 FlexDirection::Row
1882 );
1883
1884 assert!(world.get::<NotificationAccentBar>(bar).is_some());
1885 assert_eq!(world.get::<Node>(bar).unwrap().width, Val::Px(4.0));
1886 assert_eq!(
1887 world.get::<BackgroundColor>(bar).unwrap().0,
1888 notification_color(ACCENT)
1889 );
1890
1891 let body_node = world.get::<Node>(body).unwrap();
1892 assert_eq!(body_node.padding, UiRect::axes(Val::Px(10.0), Val::Px(6.0)));
1893 assert_eq!(body_node.flex_direction, FlexDirection::Column);
1894 assert_eq!(body_node.align_items, AlignItems::FlexEnd);
1895
1896 assert!(world.get::<NotificationLabel>(label).is_some());
1897 assert_eq!(
1898 world.get::<TextColor>(label).unwrap().0,
1899 notification_color(LABEL)
1900 );
1901 assert_eq!(world.get::<TextFont>(label).unwrap().font_size, 18.0);
1902 assert_eq!(
1903 world.get::<TextColor>(item).unwrap().0,
1904 Color::srgba(1.0, 1.0, 1.0, 1.0)
1905 );
1906 }
1907
1908 #[test]
1909 fn background_alone_adds_no_bar_and_no_label() {
1910 let mut app = app();
1911 app.insert_resource(PickupNotificationLayout {
1912 background: Some(BACKGROUND),
1913 accent_bar: Some(PickupNotificationAccentBar {
1914 color: ACCENT,
1915 width: 0.0,
1916 }),
1917 ..default()
1918 });
1919 pick_up(&mut app, DefaultItem::Sand, 3);
1920
1921 let entities = notifications(&mut app);
1922 assert_eq!(entities.len(), 1);
1923 let root_children = children(&app, entities[0]);
1924 assert_eq!(root_children.len(), 1);
1925 let body_children = children(&app, root_children[0]);
1926 assert_eq!(body_children.len(), 1);
1927 assert_eq!(text(&app, body_children[0]), line(DefaultItem::Sand, 3));
1928
1929 let world = app.world_mut();
1930 let mut bars = world.query_filtered::<Entity, With<NotificationAccentBar>>();
1931 assert_eq!(bars.iter(world).count(), 0);
1932 let mut labels = world.query_filtered::<Entity, With<NotificationLabel>>();
1933 assert_eq!(labels.iter(world).count(), 0);
1934 }
1935}