Refactor ui to full bsn!

main
Elijah Voigt 1 month ago
parent e3f1bf83e2
commit 774e8f11f9

@ -1,3 +1,4 @@
use bevy::asset::{AssetPath, HandleTemplate};
use engine::*; use engine::*;
const TILE: &str = "tileGrey_01.png"; const TILE: &str = "tileGrey_01.png";
@ -72,7 +73,6 @@ fn main() {
Update, Update,
toggle_state_entities::<AudioPlayback>.run_if(state_changed::<AudioPlayback>), toggle_state_entities::<AudioPlayback>.run_if(state_changed::<AudioPlayback>),
) )
.add_observer(on_add_ui_button)
.add_observer(on_add_coordinates) .add_observer(on_add_coordinates)
.run(); .run();
} }
@ -86,7 +86,7 @@ enum AudioPlayback {
Play, Play,
} }
#[derive(Debug, Component, PartialEq, Default, FromTemplate)] #[derive(Debug, Component, PartialEq, Default)]
#[require(Visibility, Transform)] #[require(Visibility, Transform)]
enum UiButton { enum UiButton {
Power, Power,
@ -117,51 +117,41 @@ impl UiButton {
} }
} }
fn on_add_ui_button( fn ui_button(
trigger: On<Add, UiButton>, button: UiButton,
mut commands: Commands, ) -> impl Scene {
query: Query<&UiButton>, let color: Color = WHITE.with_alpha(0.5).into();
server: Res<AssetServer>, bsn! {
) { Button
let button = query.get(trigger.entity).unwrap(); Hovered
commands Outline {
.entity(trigger.entity) width: Val::Px(5.0),
.insert(( color: Color::NONE,
Button, }
Hovered::default(), Node {
Outline { top: Val::Px(2.5),
width: Val::Px(5.0), left: Val::Px(2.5),
color: Color::NONE, margin: UiRect::all(Val::Px(2.5)),
..default() }
}, BackgroundColor(GRAY)
ImageNode {
image: HandleTemplate::Path(TILE),
color,
}
Children [
Node { Node {
top: Val::Px(2.5), align_self: AlignSelf::Center,
left: Val::Px(2.5), justify_self: JustifySelf::Center,
margin: UiRect::all(Val::Px(2.5)), width: Val::Percent(100.0),
..default() height: Val::Percent(100.0),
}, }
BackgroundColor(GRAY.into()),
ImageNode { ImageNode {
image: server.load(TILE), image: HandleTemplate::Path(AssetPath::parse(button.icon())),
color: WHITE.with_alpha(0.5).into(), }
..default() ]
}, on(responsive_button_over)
children![( on(responsive_button_out)
Node { }
align_self: AlignSelf::Center,
justify_self: JustifySelf::Center,
width: Val::Percent(100.0),
height: Val::Percent(100.0),
..default()
},
ImageNode {
image: server.load(button.icon()),
..default()
},
)],
))
.observe(responsive_button_over)
.observe(responsive_button_out);
} }
fn the_camera() -> impl Scene { fn the_camera() -> impl Scene {
@ -180,27 +170,27 @@ fn the_ui() -> impl Scene {
} }
Transform Transform
Children [ Children [
UiButton::Power ui_button(UiButton::Power)
on(|_: On<Pointer<Press>>, mut writer: MessageWriter<AppExit>| { on(|_: On<Pointer<Press>>, mut writer: MessageWriter<AppExit>| {
writer.write(AppExit::Success); writer.write(AppExit::Success);
}), }),
UiButton::Pause ui_button(UiButton::Pause)
SimulationPlayback::Run SimulationPlayback::Run
on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<SimulationPlayback>>| { on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<SimulationPlayback>>| {
// Set simulation state // Set simulation state
next.set(SimulationPlayback::Pause); next.set(SimulationPlayback::Pause);
}), }),
UiButton::Play ui_button(UiButton::Play)
SimulationPlayback::Pause SimulationPlayback::Pause
on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<SimulationPlayback>>| { on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<SimulationPlayback>>| {
// Set simulation state // Set simulation state
next.set(SimulationPlayback::Run); next.set(SimulationPlayback::Run);
}), }),
UiButton::Step ui_button(UiButton::Step)
on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<SimulationPlayback>>| { on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<SimulationPlayback>>| {
next.set(SimulationPlayback::Step); next.set(SimulationPlayback::Step);
}), }),
UiButton::ZoomOut ui_button(UiButton::ZoomOut)
on(|_: On<Pointer<Press>>, mut projection: Query<&mut Projection>| { on(|_: On<Pointer<Press>>, mut projection: Query<&mut Projection>| {
projection.iter_mut().for_each(|mut p| match *p { projection.iter_mut().for_each(|mut p| match *p {
Projection::Orthographic(ref mut o) => { Projection::Orthographic(ref mut o) => {
@ -209,7 +199,7 @@ fn the_ui() -> impl Scene {
_ => todo!(), _ => todo!(),
}); });
}), }),
UiButton::ZoomIn ui_button(UiButton::ZoomIn)
on(|_: On<Pointer<Press>>, mut projection: Query<&mut Projection>| { on(|_: On<Pointer<Press>>, mut projection: Query<&mut Projection>| {
projection.iter_mut().for_each(|mut p| match *p { projection.iter_mut().for_each(|mut p| match *p {
Projection::Orthographic(ref mut o) => { Projection::Orthographic(ref mut o) => {
@ -218,15 +208,15 @@ fn the_ui() -> impl Scene {
_ => todo!(), _ => todo!(),
}); });
}), }),
UiButton::MuteAudio ui_button(UiButton::MuteAudio)
AudioPlayback::Play AudioPlayback::Play
on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<AudioPlayback>>| { on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<AudioPlayback>>| {
next.set(AudioPlayback::Play); next.set(AudioPlayback::Mute);
}), }),
UiButton::PlayAudio ui_button(UiButton::PlayAudio)
AudioPlayback::Mute AudioPlayback::Mute
on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<AudioPlayback>>| { on(|_: On<Pointer<Press>>, mut next: ResMut<NextState<AudioPlayback>>| {
next.set(AudioPlayback::Mute); next.set(AudioPlayback::Play);
}), }),
] ]
} }
@ -235,7 +225,7 @@ fn the_ui() -> impl Scene {
#[derive(Default, Debug, Resource)] #[derive(Default, Debug, Resource)]
struct NextCoordinates(Coordinates); struct NextCoordinates(Coordinates);
#[derive(Debug, Default, Clone, PartialEq, Hash, Eq, Component)] #[derive(Debug, Default, Clone, PartialEq, Hash, Eq, Component, FromTemplate)]
#[require(Visibility, Transform)] #[require(Visibility, Transform)]
struct Coordinates { struct Coordinates {
x: isize, x: isize,
@ -383,7 +373,7 @@ fn grid_gizmo(mut gizmos: Gizmos, coordinates: Res<NextCoordinates>) {
gizmos.rect_2d(isometry, Vec2::ONE * SCALE, PURPLE) gizmos.rect_2d(isometry, Vec2::ONE * SCALE, PURPLE)
} }
#[derive(Component, Debug, PartialEq, Hash, Eq)] #[derive(Component, Debug, PartialEq, Hash, Eq, FromTemplate)]
struct Cell; struct Cell;
// When left mouse clicked, spawn a cell at GridPosition // When left mouse clicked, spawn a cell at GridPosition
@ -522,12 +512,19 @@ enum Lifecycle {
Dead(Entity), Dead(Entity),
} }
fn new_cell(Coordinates { x, y }: Coordinates) -> impl Scene {
bsn! {
Coordinates { x, y }
Cell
}
}
fn cell_lifecycle(mut reader: MessageReader<Lifecycle>, mut commands: Commands) { fn cell_lifecycle(mut reader: MessageReader<Lifecycle>, mut commands: Commands) {
reader.read().for_each(|msg| match msg { reader.read().for_each(|msg| match msg {
Lifecycle::Alive(c) => { Lifecycle::Alive(c) => {
debug!("creating {:?}", c); debug!("creating {:?}", c);
// TODO: Safeguard: ensure no other cell there? // TODO: Safeguard: ensure no other cell there?
commands.spawn((c.clone(), Cell)); commands.spawn_scene(new_cell(c.clone()));
} }
Lifecycle::Dead(e) => { Lifecycle::Dead(e) => {
commands.entity(*e).despawn(); commands.entity(*e).despawn();

Loading…
Cancel
Save