From 03dab2bb2367236609ed3adf214932753187543c Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 18 Mar 2024 01:02:17 +0100 Subject: [PATCH] auto-hide log messages --- src/hud.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/hud.rs b/src/hud.rs index 35fb291..4bd5a4f 100644 --- a/src/hud.rs +++ b/src/hud.rs @@ -2,10 +2,12 @@ use crate::{settings, actor}; use bevy::prelude::*; use bevy::diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin}; use std::collections::VecDeque; +use std::time::SystemTime; const HUD_REFRESH_TIME: f32 = 0.5; const FONT: &str = "tmp/fonts/NotoSansSC-Thin.ttf"; const LOG_MAX: usize = 5; +const LOG_MAX_TIME_S: u64 = 3; pub struct HudPlugin; impl Plugin for HudPlugin { @@ -24,9 +26,14 @@ struct GaugesText; #[derive(Resource)] struct FPSUpdateTimer(Timer); +struct Message { + text: String, + time: u64, +} + #[derive(Resource)] struct Log { - logs: VecDeque, + logs: VecDeque, } impl Log { @@ -34,7 +41,22 @@ impl Log { if self.logs.len() == LOG_MAX { self.logs.pop_front(); } - self.logs.push_back(message); + if let Ok(epoch) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { + self.logs.push_back(Message { + text: message, + time: epoch.as_secs(), + }); + } + } + + pub fn remove_old(&mut self) { + if let Some(message) = self.logs.front() { + if let Ok(epoch) = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { + if epoch.as_secs() - message.time > LOG_MAX_TIME_S { + self.logs.pop_front(); + } + } + } } } @@ -156,7 +178,7 @@ fn setup( fn update( diagnostics: Res, time: Res