-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from NthTensor/main
Add basic template macro
- Loading branch information
Showing
5 changed files
with
450 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
//! Super Sheep-Counter 2000 | ||
//! | ||
//! An all-in-one numerical ruminant package. | ||
use i_cant_believe_its_not_bsn::*; | ||
|
||
use bevy::color::palettes::css; | ||
use bevy::prelude::*; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
.add_plugins(sheep_plugin) | ||
.run(); | ||
} | ||
|
||
fn sheep_plugin(app: &mut App) { | ||
app.add_systems(Startup, setup) | ||
.add_systems(Update, sheep_system) | ||
.add_observer(observe_buttons); | ||
} | ||
|
||
fn setup(mut commands: Commands) { | ||
commands.spawn(Camera2d); | ||
} | ||
|
||
#[derive(Component)] | ||
struct Sheep; | ||
|
||
#[derive(Component)] | ||
enum Button { | ||
Increment, | ||
Decrement, | ||
} | ||
|
||
// A query that pulls data from the ecs and then updates it using a template. | ||
fn sheep_system(mut commands: Commands, sheep: Query<&Sheep>) { | ||
let num_sheep = sheep.iter().len(); | ||
|
||
let template = template!( | ||
root: { | ||
Node { | ||
position_type: PositionType::Absolute, | ||
bottom: Val::Px(5.0), | ||
right: Val::Px(5.0), | ||
..default() | ||
} | ||
} [ | ||
{ counter(num_sheep, "sheep", Button::Increment, Button::Decrement) }; | ||
]; | ||
); | ||
|
||
commands.build(template); | ||
} | ||
|
||
// A function that returns an ecs template. | ||
fn counter<T: Component>(num: usize, name: &str, inc: T, dec: T) -> Template { | ||
template! { | ||
header: { Text::new("You have ") } [ | ||
number: { TextSpan::new(format!("{num}")) }; | ||
sheep: { TextSpan::new(format!(" {name}!")) }; | ||
]; | ||
increase: {( | ||
Button, Text::new("Increase"), TextColor(css::GREEN.into()), inc, visible_if(num < 100) | ||
)}; | ||
decrease: {( | ||
Button, Text::new("Decrease"), TextColor(css::RED.into()), dec, visible_if(num > 0) | ||
)}; | ||
} | ||
} | ||
|
||
// A component helper function for computing visibility. | ||
fn visible_if(condition: bool) -> Visibility { | ||
if condition { | ||
Visibility::Visible | ||
} else { | ||
Visibility::Hidden | ||
} | ||
} | ||
|
||
// A global observer which responds to button clicks. | ||
fn observe_buttons( | ||
mut trigger: Trigger<Pointer<Up>>, | ||
buttons: Query<&Button>, | ||
sheep: Query<Entity, With<Sheep>>, | ||
mut commands: Commands, | ||
) { | ||
match buttons.get(trigger.target).ok() { | ||
Some(Button::Increment) => { | ||
commands.spawn(Sheep); | ||
} | ||
Some(Button::Decrement) => { | ||
if let Some(sheep) = sheep.iter().next() { | ||
commands.entity(sheep).despawn_recursive(); | ||
} | ||
} | ||
_ => {} | ||
} | ||
trigger.propagate(false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ pub use hierarchy::*; | |
|
||
mod maybe; | ||
pub use maybe::*; | ||
|
||
mod template; | ||
pub use template::*; |
Oops, something went wrong.