Skip to content

Commit

Permalink
Add Spawn::on_spawn and rename other methods to fit the declarative s…
Browse files Browse the repository at this point in the history
…tyle
  • Loading branch information
matthunz committed Nov 26, 2024
1 parent 3dc39dd commit 2ecb1e2
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Compose for Counter {
flex_direction: FlexDirection::Column,
..default()
})
.with_content((
.content((
spawn(Text::new(format!("High five count: {}", count))),
spawn(Text::new("Up high")).observe(move |_trigger: In<Trigger<Pointer<Click>>>| {
Mut::update(count, |x| *x += 1)
Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Compose for Counter {
flex_direction: FlexDirection::Column,
..default()
})
.with_content((
.content((
spawn(Text::new(format!("High five count: {}", count))),
spawn(Text::new("Up high")).observe(move |_trigger: In<Trigger<Pointer<Click>>>| {
Mut::update(count, |x| *x += 1)
Expand Down
6 changes: 3 additions & 3 deletions examples/ecs/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Compose for Breed<'_> {
flex_direction: FlexDirection::Row,
..default()
})
.with_content((
.content((
spawn((
Text::new(cx.me().name),
Node {
Expand All @@ -32,7 +32,7 @@ impl Compose for Breed<'_> {
flex_direction: FlexDirection::Column,
..default()
})
.with_content(compose::from_iter(
.content(compose::from_iter(
Ref::map(cx.me(), |me| me.families),
|family| spawn(Text::from(family.to_string())),
)),
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Compose for BreedList {
overflow: Overflow::scroll_y(),
..default()
})
.with_content(compose::from_iter(breeds, |breed| Breed {
.content(compose::from_iter(breeds, |breed| Breed {
name: breed.0,
families: breed.1,
}))
Expand Down
6 changes: 2 additions & 4 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;
use std::collections::HashMap;

pub use actuate_macros::{Data, data};
pub use actuate_macros::{data, Data};

/// Composable data.
///
Expand All @@ -12,9 +12,7 @@ pub use actuate_macros::{Data, data};
///
/// For example, a `RefCell<&'a T>` is unsafe because the compiler will infer the lifetime of a child composable's lifetime (e.g. `'a`)
/// as this struct's lifetime (e.g. `'a`).
pub unsafe trait Data {

}
pub unsafe trait Data {}

macro_rules! impl_data_for_std {
($($t:ty),*) => {
Expand Down
34 changes: 18 additions & 16 deletions src/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ where
content: (),
target: None,
observer_fns: Vec::new(),
on_add: Cell::new(None),
}
}

Expand All @@ -499,41 +500,38 @@ pub struct Spawn<'a, C> {
content: C,
target: Option<Entity>,
observer_fns: Vec<ObserverFn<'a>>,
on_add: Cell<Option<Box<dyn FnOnce(EntityWorldMut) + 'a>>>,
}

impl<'a, C> Spawn<'a, C> {
/// Get the target entity to spawn the composition into.
///
/// If `None`, this will use the composition's parent (if any).
pub fn target(&self) -> Option<Entity> {
self.target
}

/// Set the target entity to spawn the composition into.
///
/// If `None`, this will use the composition's parent (if any).
pub fn set_target(&mut self, target: Option<Entity>) {
self.target = target;
}

/// Set the target entity to spawn the composition into.
///
/// If `None`, this will use the composition's parent (if any).
pub fn with_target(mut self, target: Entity) -> Self {
pub fn target(mut self, target: Entity) -> Self {
self.target = Some(target);
self
}

/// Set the child content.
pub fn with_content<C2>(self, content: C2) -> Spawn<'a, C2> {
pub fn content<C2>(self, content: C2) -> Spawn<'a, C2> {
Spawn {
spawn_fn: self.spawn_fn,
content,
target: self.target,
observer_fns: self.observer_fns,
on_add: self.on_add,
}
}

/// Set a function to be called when this entity is spawned.
pub fn on_spawn<F>(self, f: F) -> Self
where
F: FnOnce(EntityWorldMut) + 'a,
{
self.on_add.set(Some(Box::new(f)));
self
}

/// Add an observer to the spawned entity.
pub fn observe<F, E, B, Marker>(mut self, observer: F) -> Self
where
Expand Down Expand Up @@ -586,6 +584,10 @@ impl<C: Compose> Compose for Spawn<'_, C> {
f(&mut entity_mut);
}

if let Some(f) = cx.me().on_add.take() {
f(entity_mut);
}

is_initial.set(false);
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! flex_direction: FlexDirection::Column,
//! ..default()
//! })
//! .with_content((
//! .content((
//! spawn(Text::new(format!("High five count: {}", count))),
//! spawn(Text::new("Up high")).observe(move |_trigger: In<Trigger<Pointer<Click>>>| {
//! Mut::update(count, |x| *x += 1)
Expand Down

0 comments on commit 2ecb1e2

Please sign in to comment.