mod utils;
use utils::*;
use bevy::prelude::*;
use jonmo::prelude::*;
fn main() {
App::new()
.add_plugins(examples_plugin)
.add_systems(
Startup,
(
|world: &mut World| {
ui_root().spawn(world);
},
camera,
),
)
.run();
}
#[derive(Component, Clone, Deref, DerefMut)]
struct Counter(i32);
fn ui_root() -> jonmo::Builder {
let counter_holder = LazyEntity::new();
jonmo::Builder::from(Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
})
.child(
jonmo::Builder::from(Node {
flex_direction: FlexDirection::Row,
column_gap: Val::Px(15.0),
align_items: AlignItems::Center,
padding: UiRect::all(Val::Px(25.)),
..default()
})
.insert(Counter(0))
.lazy_entity(counter_holder.clone())
.child(counter_button(counter_holder.clone(), PINK, "-", -1))
.child(
jonmo::Builder::from((Node::default(), TextFont::from_font_size(25.)))
.component_signal(
signal::from_component_changed::<Counter>(counter_holder.clone())
.map_in(deref_copied)
.map_in_ref(ToString::to_string)
.map_in(Text)
.map_in(Some),
),
)
.child(counter_button(counter_holder, BLUE, "+", 1)),
)
}
fn counter_button(counter_holder: LazyEntity, color: Color, label: &'static str, step: i32) -> jonmo::Builder {
jonmo::Builder::from((
Node {
width: Val::Px(45.0),
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
border_radius: BorderRadius::MAX,
..default()
},
BackgroundColor(color),
))
.observe(move |_: On<Pointer<Click>>, mut counters: Query<&mut Counter>| {
**counters.get_mut(*counter_holder).unwrap() += step;
})
.child(jonmo::Builder::from((Text::from(label), TextFont::from_font_size(25.))))
}
fn camera(mut commands: Commands) {
commands.spawn(Camera2d);
}