Debug system, working on text animation

main
Elijah Voigt 2 years ago
parent b381f0b7b7
commit 8a5a8d39d4

@ -23,5 +23,10 @@ path = "bin/animation-wtf.rs"
name = "text-inspect" name = "text-inspect"
path = "bin/text-inspect.rs" path = "bin/text-inspect.rs"
[[bin]]
name = "debug-info"
path = "bin/debug-info.rs"
[dependencies] [dependencies]
bevy = "0.10" bevy = "0.10"

@ -1,14 +1,17 @@
use bevy::prelude::*; use bevy::prelude::*;
use monologue_trees::*;
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin { .add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window { primary_window: Some(Window {
title: "Monologue Trees".into(), title: "Debug Info".into(),
resolution: (640., 480.).into(), resolution: (640., 480.).into(),
..default() ..default()
}), }),
..default() ..default()
})) }))
.run(); .add_plugin(debug::DebugInfo)
.run()
} }

@ -2,6 +2,8 @@ use std::time::Duration;
use bevy::prelude::*; use bevy::prelude::*;
use monologue_trees::*;
const LOREM: [&str; 5] = [ const LOREM: [&str; 5] = [
"Ullam nostrum aut amet adipisci consequuntur quisquam nemo consequatur. Vel eum et ullam ullam aperiam earum voluptas consequuntur. Blanditiis earum voluptatem voluptas animi dolorum fuga aliquam ea.\n", "Ullam nostrum aut amet adipisci consequuntur quisquam nemo consequatur. Vel eum et ullam ullam aperiam earum voluptas consequuntur. Blanditiis earum voluptatem voluptas animi dolorum fuga aliquam ea.\n",
"Velit ratione consequatur modi. Dolores quo quisquam occaecati veniam maxime totam minus et. Laudantium unde optio vel. Et cumque voluptatum dolorem. Odit tempore dolores quibusdam aspernatur vitae labore occaecati. Omnis quia tempora tenetur repellat in.\n", "Velit ratione consequatur modi. Dolores quo quisquam occaecati veniam maxime totam minus et. Laudantium unde optio vel. Et cumque voluptatum dolorem. Odit tempore dolores quibusdam aspernatur vitae labore occaecati. Omnis quia tempora tenetur repellat in.\n",
@ -20,6 +22,7 @@ fn main() {
}), }),
..default() ..default()
})) }))
.add_plugin(debug::DebugInfo)
.add_startup_system(load_fonts) .add_startup_system(load_fonts)
.add_startup_system(init_ui) .add_startup_system(init_ui)
.add_system(manage_buttons) .add_system(manage_buttons)
@ -231,7 +234,7 @@ fn play(
if *duration > Duration::ZERO { if *duration > Duration::ZERO {
*duration = duration.saturating_sub(time.delta()); *duration = duration.saturating_sub(time.delta());
info!("Delta: {:?}", time.delta()); // info!("Delta: {:?}", time.delta());
let percentage = 1.0 - (duration.as_secs_f32() / 30.0); let percentage = 1.0 - (duration.as_secs_f32() / 30.0);
@ -247,18 +250,18 @@ fn play(
// Total amount of remaining text is greater than this section // Total amount of remaining text is greater than this section
// Set this text block to the full section // Set this text block to the full section
} else if len_total >= desired.len() { } else if len_total >= desirted.len() {
info!("len_total >= desired.len()"); // info!("len_total >= desired.len()");
len_total -= desired.len(); len_total -= desired.len();
if section.value.len() != desired.len() { if section.value.len() != desired.len() {
info!("value != desired"); // info!("value != desired");
section.value = desired.clone(); section.value = desired.clone();
} }
// Total remaining text is less than this section // Total remaining text is less than this section
// Set to a sub-string of text // Set to a sub-string of text
} else if len_total < desired.len() { } else if len_total < desired.len() {
info!("len total < desired len"); // info!("len total < desired len");
// Difference between current value and desired length // Difference between current value and desired length
let diff = desired let diff = desired
.split_at(section.value.len()) .split_at(section.value.len())
@ -266,7 +269,7 @@ fn play(
.split_at(len_total - section.value.len()) .split_at(len_total - section.value.len())
.0 .0
.into(); .into();
info!("adding value {}", diff); // info!("adding value {}", diff);
section.value.push_str(diff); section.value.push_str(diff);
len_total = 0; len_total = 0;

@ -0,0 +1,120 @@
use std::collections::VecDeque;
use bevy::{
input::{keyboard::KeyboardInput, ButtonState},
prelude::*,
};
/// Debug info plugin
pub struct DebugInfo;
impl Plugin for DebugInfo {
fn build(&self, app: &mut App) {
app.add_startup_system(init)
.add_system(toggle)
.add_system(update);
}
}
/// Debug UI component
#[derive(Component)]
struct DebugUi;
#[derive(Component)]
struct DebugFps;
/// Debug info enabled marker
#[derive(Resource)]
struct DebugActive;
fn init(mut commands: Commands, server: Res<AssetServer>) {
// commands.spawn((Camera2dBundle { ..default() }, DebugUi));
commands
.spawn((
NodeBundle {
visibility: Visibility::Hidden,
style: Style {
position_type: PositionType::Absolute,
position: UiRect {
top: Val::Px(0.0),
left: Val::Px(0.0),
..default()
},
..default()
},
background_color: BackgroundColor(Color::GRAY),
z_index: ZIndex::Global(999),
..default()
},
DebugUi,
))
.with_children(|parent| {
let font: Handle<Font> = server.load("fonts/JMH Typewriter-Bold.otf");
let style = TextStyle {
font,
font_size: 16.0,
color: Color::ORANGE,
};
parent.spawn((TextBundle::from_section("FPS", style), DebugUi, DebugFps));
});
}
fn toggle(
mut commands: Commands,
mut key_evr: EventReader<KeyboardInput>,
active: Option<Res<DebugActive>>,
mut visibility: Query<&mut Visibility, With<DebugUi>>,
) {
for event in key_evr.iter() {
match event {
KeyboardInput {
key_code: Some(KeyCode::F12),
state: ButtonState::Pressed,
..
} => match active {
None => {
commands.insert_resource(DebugActive);
visibility
.par_iter_mut()
.for_each_mut(|mut vis| *vis = Visibility::Visible);
}
Some(_) => {
commands.remove_resource::<DebugActive>();
visibility
.par_iter_mut()
.for_each_mut(|mut vis| *vis = Visibility::Hidden);
}
},
_ => (),
}
}
}
fn update(
active: Option<Res<DebugActive>>,
mut fps: Query<&mut Text, With<DebugFps>>,
time: Res<Time>,
mut buffer: Local<VecDeque<f32>>,
) {
if active.is_some() {
// Moving average window size
let buffer_size = 60;
// Calculate instantaneous FPS
let last_fps = 1.0 / time.delta().as_secs_f32();
// Add current FPS to front
(*buffer).push_back(last_fps);
if (*buffer).len() > buffer_size {
// Remove extra elements from back
let _ = (*buffer).pop_front();
}
// Calculate moving average FPS
let average: f32 = (*buffer).iter().sum::<f32>() / ((*buffer).len() as f32);
// Display FPS value
fps.single_mut().sections[0].value = format!("FPS: {:.3}", average);
}
}

@ -0,0 +1 @@
pub mod debug;
Loading…
Cancel
Save