mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-17 00:13:17 +00:00
Split UI module
This commit is contained in:
@@ -1,65 +0,0 @@
|
||||
use bevy::{
|
||||
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
pub struct UiLayerPlugin;
|
||||
|
||||
impl Plugin for UiLayerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(FrameTimeDiagnosticsPlugin)
|
||||
.add_systems(Startup, layout)
|
||||
.add_systems(Update, update);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
pub struct FramerateCounter;
|
||||
|
||||
fn layout(mut cmd: Commands) {
|
||||
cmd.spawn(NodeBundle {
|
||||
style: Style {
|
||||
width: Val::Percent(100.),
|
||||
height: Val::Percent(100.),
|
||||
padding: UiRect::all(Val::Px(16.)),
|
||||
..default()
|
||||
},
|
||||
..default()
|
||||
})
|
||||
.with_children(|parent| {
|
||||
parent.spawn((
|
||||
FramerateCounter,
|
||||
TextBundle::from_section(
|
||||
"0 fps",
|
||||
TextStyle {
|
||||
color: Color::GREEN,
|
||||
font_size: 14.,
|
||||
..default()
|
||||
},
|
||||
)
|
||||
.with_text_alignment(TextAlignment::Right)
|
||||
.with_style(Style {
|
||||
// TODO: This doesn't seem to have any effect
|
||||
width: Val::Percent(100.),
|
||||
..default()
|
||||
}),
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
fn update(
|
||||
time: Res<Time>,
|
||||
diag: Res<DiagnosticsStore>,
|
||||
mut counter: Query<&mut Text, With<FramerateCounter>>,
|
||||
) {
|
||||
if let Some(fps_diag) = diag.get(FrameTimeDiagnosticsPlugin::FPS) {
|
||||
let fps = if let Some(fps_smoothed) = fps_diag.smoothed() {
|
||||
fps_smoothed
|
||||
} else {
|
||||
let ms = time.delta().as_micros() as f64 / 1000.;
|
||||
1000. / ms
|
||||
};
|
||||
|
||||
counter.single_mut().sections[0].value = format!("{fps:.2} fps");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
use bevy::{
|
||||
diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
pub(super) struct FpsCounterPlugin;
|
||||
|
||||
impl Plugin for FpsCounterPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(FrameTimeDiagnosticsPlugin)
|
||||
.add_systems(Startup, layout)
|
||||
.add_systems(Update, update);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct FramerateCounter;
|
||||
|
||||
fn layout(mut cmd: Commands) {
|
||||
cmd.spawn((
|
||||
FramerateCounter,
|
||||
TextBundle::from_section(
|
||||
"0 fps",
|
||||
TextStyle {
|
||||
color: Color::GREEN,
|
||||
font_size: 14.,
|
||||
..default()
|
||||
},
|
||||
)
|
||||
.with_text_alignment(TextAlignment::Right)
|
||||
.with_style(Style {
|
||||
position_type: PositionType::Absolute,
|
||||
top: Val::Px(16.),
|
||||
right: Val::Px(16.),
|
||||
..default()
|
||||
}),
|
||||
));
|
||||
}
|
||||
|
||||
fn update(
|
||||
time: Res<Time>,
|
||||
diag: Res<DiagnosticsStore>,
|
||||
mut counter: Query<&mut Text, With<FramerateCounter>>,
|
||||
) {
|
||||
if let Some(fps_diag) = diag.get(FrameTimeDiagnosticsPlugin::FPS) {
|
||||
let fps = if let Some(fps_smoothed) = fps_diag.smoothed() {
|
||||
fps_smoothed
|
||||
} else {
|
||||
1. / time.delta_seconds_f64()
|
||||
};
|
||||
|
||||
counter.single_mut().sections[0].value = format!("{fps:.2} fps");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
use self::fps::FpsCounterPlugin;
|
||||
|
||||
mod fps;
|
||||
|
||||
pub struct UiLayerPlugin;
|
||||
|
||||
impl Plugin for UiLayerPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(FpsCounterPlugin);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user