Skip to content

Commit

Permalink
Populate info bar
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrankel committed Dec 4, 2023
1 parent 9d3a609 commit 36dbf2b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 96 deletions.
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod config;
#[cfg(feature = "dev")]
mod debug;
mod physics;
mod simulation;
mod state;
mod ui;

Expand Down Expand Up @@ -34,7 +35,12 @@ impl Plugin for AppPlugin {
));

// Other plugins
app.add_plugins((camera::CameraPlugin, physics::PhysicsPlugin, ui::UiPlugin));
app.add_plugins((
camera::CameraPlugin,
physics::PhysicsPlugin,
simulation::SimulationPlugin,
ui::UiPlugin,
));

#[cfg(feature = "dev")]
app.add_plugins(debug::DebugPlugin {
Expand Down
18 changes: 18 additions & 0 deletions src/simulation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use bevy::prelude::*;

pub struct SimulationPlugin;

impl Plugin for SimulationPlugin {
fn build(&self, app: &mut App) {
app.register_type::<Simulation>()
.init_resource::<Simulation>();
}
}

#[derive(Resource, Default, Reflect)]
#[reflect(Resource)]
pub struct Simulation {
pub plugins: f64,
pub lines: f64,
pub entities: f64,
}
27 changes: 26 additions & 1 deletion src/state/editor_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::Deserialize;
use serde::Serialize;

use crate::config::Config;
use crate::simulation::Simulation;
use crate::state::AppState::*;
use crate::ui::CodeTyper;
use crate::ui::FontSize;
Expand All @@ -26,7 +27,8 @@ impl Plugin for EditorScreenStatePlugin {
app.register_type::<EditorScreenAssets>()
.init_collection::<EditorScreenAssets>()
.add_systems(OnEnter(EditorScreen), enter_editor_screen)
.add_systems(OnExit(EditorScreen), exit_editor_screen);
.add_systems(OnExit(EditorScreen), exit_editor_screen)
.add_systems(Update, update_info_bar_text);
}
}

Expand Down Expand Up @@ -100,6 +102,8 @@ fn enter_editor_screen(mut commands: Commands, root: Res<AppRoot>, config: Res<C
style: Style {
width: Val::Percent(100.0),
height: config.info_bar_height,
padding: UiRect::horizontal(Val::Px(16.0)),
align_items: AlignItems::Center,
..default()
},
background_color: config.info_bar_background_color.into(),
Expand All @@ -121,6 +125,7 @@ fn enter_editor_screen(mut commands: Commands, root: Res<AppRoot>, config: Res<C
},
),
FontSize::new(config.info_bar_font_size),
InfoBarText,
))
.set_parent(info_bar);

Expand Down Expand Up @@ -298,3 +303,23 @@ fn exit_editor_screen(
};
transform.translation = Vec2::ZERO.extend(transform.translation.z);
}

#[derive(Component, Reflect)]
pub struct InfoBarText;

fn update_info_bar_text(
simulation: Res<Simulation>,
mut info_bar_query: Query<&mut Text, With<InfoBarText>>,
) {
// TODO: E.g. Format large numbers like 2,346,834 and then 8.435e22
let plugins = simulation.plugins;
let lines = simulation.lines;
let entities = simulation.entities;

// TODO: Remove "s" if number is equal to 1
let info = format!("{plugins} plugins, {lines} lines, {entities} entities");

for mut text in &mut info_bar_query {
text.sections[0].value = info.clone();
}
}
88 changes: 0 additions & 88 deletions src/state/editor_screen/entity_view.rs

This file was deleted.

12 changes: 6 additions & 6 deletions src/ui/code_typer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::str::Chars;

use bevy::prelude::*;

use crate::simulation::Simulation;

pub struct CodeTyperPlugin;

impl Plugin for CodeTyperPlugin {
Expand Down Expand Up @@ -31,8 +33,6 @@ pub struct CodeTyper {
pub lines_count: usize,
/// The maximum number of lines to display before old lines start getting deleted.
pub lines_max: usize,
/// The total number of \n typed over the full lifetime.
pub lines_typed: usize,
/// An infinite iterator that yields the next character that will be added.
#[reflect(ignore)]
pub code: CodeGenerator,
Expand All @@ -44,7 +44,6 @@ impl Default for CodeTyper {
chars_per_key: 1,
lines_count: 1,
lines_max: 1,
lines_typed: 0,
code: default(),
}
}
Expand All @@ -53,7 +52,8 @@ impl Default for CodeTyper {
pub fn type_code(
mut char_events: EventReader<ReceivedCharacter>,
keyboard_input: Res<Input<ScanCode>>,
mut query: Query<(&mut CodeTyper, &mut Text)>,
mut simulation: ResMut<Simulation>,
mut typer_query: Query<(&mut CodeTyper, &mut Text)>,
) {
let count = char_events
.read()
Expand All @@ -63,7 +63,7 @@ pub fn type_code(
return;
}

for (mut typer, mut text) in &mut query {
for (mut typer, mut text) in &mut typer_query {
let text = &mut text.sections[0].value;
for _ in 0..count * typer.chars_per_key {
loop {
Expand All @@ -73,7 +73,7 @@ pub fn type_code(

// If it was a newline, update typer's lines
if c == '\n' {
typer.lines_typed += 1;
simulation.lines += 1.0;
typer.lines_count += 1;
if typer.lines_count > typer.lines_max {
typer.lines_count -= 1;
Expand Down

0 comments on commit 36dbf2b

Please sign in to comment.