diff --git a/Cargo.toml b/Cargo.toml index 1e8159f..3be8878 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,5 +23,10 @@ path = "bin/animation-wtf.rs" name = "text-inspect" path = "bin/text-inspect.rs" +[[bin]] +name = "debug-info" +path = "bin/debug-info.rs" + + [dependencies] bevy = "0.10" diff --git a/src/main.rs b/bin/debug-info.rs similarity index 69% rename from src/main.rs rename to bin/debug-info.rs index c6b23f1..82362d7 100644 --- a/src/main.rs +++ b/bin/debug-info.rs @@ -1,14 +1,17 @@ use bevy::prelude::*; +use monologue_trees::*; + fn main() { App::new() .add_plugins(DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { - title: "Monologue Trees".into(), + title: "Debug Info".into(), resolution: (640., 480.).into(), ..default() }), ..default() })) - .run(); + .add_plugin(debug::DebugInfo) + .run() } diff --git a/bin/text-inspect.rs b/bin/text-inspect.rs index afb50a9..60ca952 100644 --- a/bin/text-inspect.rs +++ b/bin/text-inspect.rs @@ -2,6 +2,8 @@ use std::time::Duration; use bevy::prelude::*; +use monologue_trees::*; + 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", "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() })) + .add_plugin(debug::DebugInfo) .add_startup_system(load_fonts) .add_startup_system(init_ui) .add_system(manage_buttons) @@ -231,7 +234,7 @@ fn play( if *duration > Duration::ZERO { *duration = duration.saturating_sub(time.delta()); - info!("Delta: {:?}", time.delta()); + // info!("Delta: {:?}", time.delta()); 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 // Set this text block to the full section - } else if len_total >= desired.len() { - info!("len_total >= desired.len()"); + } else if len_total >= desirted.len() { + // info!("len_total >= desired.len()"); len_total -= desired.len(); if section.value.len() != desired.len() { - info!("value != desired"); + // info!("value != desired"); section.value = desired.clone(); } // Total remaining text is less than this section // Set to a sub-string of text } else if len_total < desired.len() { - info!("len total < desired len"); + // info!("len total < desired len"); // Difference between current value and desired length let diff = desired .split_at(section.value.len()) @@ -266,7 +269,7 @@ fn play( .split_at(len_total - section.value.len()) .0 .into(); - info!("adding value {}", diff); + // info!("adding value {}", diff); section.value.push_str(diff); len_total = 0; diff --git a/src/debug.rs b/src/debug.rs new file mode 100644 index 0000000..4dccbe6 --- /dev/null +++ b/src/debug.rs @@ -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) { + // 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 = 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, + active: Option>, + mut visibility: Query<&mut Visibility, With>, +) { + 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::(); + visibility + .par_iter_mut() + .for_each_mut(|mut vis| *vis = Visibility::Hidden); + } + }, + _ => (), + } + } +} + +fn update( + active: Option>, + mut fps: Query<&mut Text, With>, + time: Res