UI
This commit is contained in:
parent
5b4ad9a47a
commit
86b9134156
5 changed files with 172 additions and 8 deletions
143
src/view/ui.rs
143
src/view/ui.rs
|
|
@ -0,0 +1,143 @@
|
|||
use bevy::feathers::{FeathersPlugins, containers::*, controls::*, dark_theme::create_dark_theme, display::*, palette, rounded_corners::RoundedCorners, theme::UiTheme};
|
||||
use bevy::ui_widgets::{Activate};
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::grid::{Grid, GridElement};
|
||||
use crate::simulation::SimulationStatus;
|
||||
|
||||
pub struct UIPlugin;
|
||||
|
||||
impl Plugin for UIPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_plugins(FeathersPlugins)
|
||||
.insert_resource(UiTheme(create_dark_theme()))
|
||||
.add_systems(Startup, ui.spawn())
|
||||
.add_systems(Update, count_water_amount);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component, Clone, Default)]
|
||||
struct WaterLabel;
|
||||
|
||||
#[derive(Component, Clone, Default)]
|
||||
struct SpeedLabel;
|
||||
|
||||
fn count_water_amount(grid: Res<Grid>, text: Single<&mut Text, With<WaterLabel>>) {
|
||||
let mut sum: u32 = 0;
|
||||
for i in 0..grid.cell_amount() {
|
||||
let GridElement::Water { amount: water_amount } = grid.data[i as usize] else {
|
||||
continue;
|
||||
};
|
||||
|
||||
sum += water_amount;
|
||||
}
|
||||
|
||||
let mut label = text.into_inner();
|
||||
label.0 = format!("Water amount: {} liters",sum);
|
||||
}
|
||||
|
||||
fn ui() -> impl Scene {
|
||||
bsn! {
|
||||
Node {
|
||||
width: percent(100),
|
||||
height: percent(100),
|
||||
align_items: AlignItems::Start,
|
||||
justify_content: JustifyContent::Start,
|
||||
display: Display::Flex,
|
||||
flex_direction: FlexDirection::Row,
|
||||
column_gap: px(8)
|
||||
}
|
||||
Children [
|
||||
debug_column()
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn debug_column() -> impl Scene {
|
||||
bsn! {
|
||||
Node {
|
||||
display: Display::Flex,
|
||||
flex_direction: FlexDirection::Column,
|
||||
align_items: AlignItems::Stretch,
|
||||
justify_content: JustifyContent::Start,
|
||||
padding: px(8),
|
||||
row_gap: px(8),
|
||||
width: px(256),
|
||||
min_width: px(256),
|
||||
}
|
||||
pane()
|
||||
pane_header()
|
||||
pane_body()
|
||||
Children [
|
||||
water_amount_label(),
|
||||
pause_button(),
|
||||
simulation_speed(),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn water_amount_label() -> impl Scene {
|
||||
bsn! {
|
||||
Node {
|
||||
width: percent(100),
|
||||
height: px(32),
|
||||
|
||||
}
|
||||
Children [
|
||||
label("")
|
||||
WaterLabel
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
fn pause_button() -> impl Scene {
|
||||
bsn!{
|
||||
@FeathersButton {
|
||||
@caption: bsn! { label("Toggle simulation") }
|
||||
}
|
||||
on(|_activate: On<Activate>, current_state: Res<State<SimulationStatus>>, mut next_state: ResMut<NextState<SimulationStatus>>| {
|
||||
next_state.set(match current_state.get() {
|
||||
SimulationStatus::Paused => SimulationStatus::Continued,
|
||||
SimulationStatus::Continued => SimulationStatus::Paused,
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn simulation_speed() -> impl Scene {
|
||||
bsn! {
|
||||
Node {
|
||||
display: Display::Flex,
|
||||
align_items: AlignItems::Stretch,
|
||||
justify_content: JustifyContent::Start,
|
||||
width: percent(100),
|
||||
height: px(32),
|
||||
}
|
||||
Children [
|
||||
@FeathersButton {
|
||||
@caption: bsn! { label("<<") }
|
||||
}
|
||||
on(|_activate: On<Activate>, mut time: ResMut<Time<Fixed>>, mut label: Single<&mut Text, With<SpeedLabel>>|{
|
||||
let mut current_frequency = (1./time.timestep().as_secs_f64()) as u32;
|
||||
current_frequency = if current_frequency == 1 {1} else {current_frequency / 2};
|
||||
time.set_timestep_hz(current_frequency as f64);
|
||||
label.0 = format!("{} hz", current_frequency);
|
||||
})
|
||||
,
|
||||
label("")
|
||||
SpeedLabel
|
||||
Node {
|
||||
justify_self: JustifySelf::Stretch
|
||||
},
|
||||
@FeathersButton {
|
||||
@caption: bsn! { label(">>") }
|
||||
}
|
||||
on(|_activate: On<Activate>, mut time: ResMut<Time<Fixed>>, mut label: Single<&mut Text, With<SpeedLabel>>|{
|
||||
let mut current_frequency = (1./time.timestep().as_secs_f64()) as u32;
|
||||
current_frequency = if current_frequency >= 128 {128} else {current_frequency * 2};
|
||||
time.set_timestep_hz(current_frequency as f64);
|
||||
label.0 = format!("{} hz", current_frequency);
|
||||
}),
|
||||
]
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue