Skip to content

Commit

Permalink
Added SchematicContext
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGVSV committed Apr 30, 2023
1 parent 190ed69 commit faca07d
Show file tree
Hide file tree
Showing 23 changed files with 340 additions and 357 deletions.
10 changes: 4 additions & 6 deletions bevy_proto_backend/src/impls/bevy_impls/asset.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use bevy::app::App;
use bevy::asset::{Asset, AssetServer, Handle};
use bevy::ecs::world::EntityMut;

use crate::impls::macros::register_schematic;
use bevy_proto_derive::impl_external_schematic;

use crate::proto::ProtoAsset;
use crate::schematics::FromSchematicInput;
use crate::tree::EntityTree;
use crate::schematics::{FromSchematicInput, SchematicContext};

#[allow(unused_variables)]
pub(super) fn register(app: &mut App) {
Expand Down Expand Up @@ -70,10 +68,10 @@ impl_external_schematic! {
struct Handle<T: Asset> {}
// ---
impl<T: Asset> FromSchematicInput<ProtoAsset> for Handle<T> {
fn from_input(input: ProtoAsset, entity: &mut EntityMut, _: &EntityTree) -> Self {
fn from_input(input: ProtoAsset, context: &mut SchematicContext) -> Self {
match input {
ProtoAsset::AssetPath(path) => entity.world().resource::<AssetServer>().load(path),
ProtoAsset::HandleId(handle_id) => entity.world().resource::<AssetServer>().get_handle(handle_id),
ProtoAsset::AssetPath(path) => context.world().resource::<AssetServer>().load(path),
ProtoAsset::HandleId(handle_id) => context.world().resource::<AssetServer>().get_handle(handle_id),
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions bevy_proto_backend/src/impls/bevy_impls/text.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use bevy::app::App;
use bevy::ecs::world::EntityMut;
use bevy::math::Vec2;
use bevy::reflect::{std_traits::ReflectDefault, FromReflect, Reflect};
use bevy::text::{BreakLineOn, Text, Text2dBounds, TextAlignment, TextSection, TextStyle};

use crate::impls::macros::{from_to, from_to_default, from_to_input, register_schematic};
use crate::proto::{ProtoAsset, ProtoColor};
use crate::schematics::FromSchematicInput;
use crate::schematics::{FromSchematicInput, SchematicContext};
use bevy_proto_derive::impl_external_schematic;

pub(super) fn register(app: &mut App) {
Expand Down Expand Up @@ -34,10 +33,10 @@ impl_external_schematic! {
from_to_input! {
Text,
TextInput,
move |input: Input, entity: &mut EntityMut, tree| {
move |input: Input, context: &mut SchematicContext| {
let mut sections = Vec::with_capacity(input.sections.len());
for section in input.sections {
sections.push(FromSchematicInput::from_input(section, &mut *entity, tree));
sections.push(FromSchematicInput::from_input(section, &mut *context));
}

Self {
Expand Down Expand Up @@ -66,9 +65,9 @@ impl_external_schematic! {
from_to_input! {
TextSection,
TextSectionInput,
|input: Input, entity, tree| Self {
|input: Input, context| Self {
value: input.value,
style: FromSchematicInput::from_input(input.style, entity, tree)
style: FromSchematicInput::from_input(input.style, context)
}
}

Expand All @@ -81,8 +80,8 @@ impl_external_schematic! {
from_to_input! {
TextStyle,
TextStyleInput,
|input: Input, entity, tree| Self {
font: FromSchematicInput::from_input(input.font, entity, tree),
|input: Input, context| Self {
font: FromSchematicInput::from_input(input.font, context),
font_size: input.font_size,
color: input.color.into(),
}
Expand Down
10 changes: 4 additions & 6 deletions bevy_proto_backend/src/impls/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ macro_rules! from_to_input {
impl $crate::schematics::FromSchematicInput<Input> for $mock {
fn from_input(
input: Input,
entity: &mut bevy::ecs::world::EntityMut,
tree: &$crate::tree::EntityTree,
context: &mut $crate::schematics::SchematicContext,
) -> Self {
$body(input, entity, tree)
$body(input, context)
}
}
};
Expand All @@ -92,10 +91,9 @@ macro_rules! from_to_input {
impl $crate::schematics::FromSchematicInput<Input> for $real {
fn from_input(
input: Input,
entity: &mut bevy::ecs::world::EntityMut,
tree: &$crate::tree::EntityTree,
context: &mut $crate::schematics::SchematicContext,
) -> Self {
$body(input, entity, tree)
$body(input, context)
}
}
};
Expand Down
107 changes: 52 additions & 55 deletions bevy_proto_backend/src/proto/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use std::marker::PhantomData;

use bevy::asset::Assets;
use bevy::ecs::system::{Command, EntityCommands, SystemParam};
use bevy::ecs::world::EntityMut;
use bevy::prelude::{Commands, Entity, Mut, World};

use crate::proto::{Config, Prototypical};
use crate::registration::ProtoRegistry;
use crate::schematics::DynamicSchematic;
use crate::tree::{EntityTree, EntityTreeNode};
use crate::schematics::{DynamicSchematic, SchematicContext};
use crate::tree::EntityTreeNode;

/// A system parameter similar to [`Commands`], but catered towards [prototypes].
///
Expand Down Expand Up @@ -164,8 +163,8 @@ impl<T: Prototypical> Command for ProtoInsertCommand<T> {
self.data.assert_is_registered(world);

self.data
.for_each_schematic(world, true, |schematic, entity, tree| {
schematic.apply(entity, tree).unwrap();
.for_each_schematic(world, true, |schematic, context| {
schematic.apply(context).unwrap();
});
}
}
Expand All @@ -191,8 +190,8 @@ impl<T: Prototypical> Command for ProtoRemoveCommand<T> {
self.data.assert_is_registered(world);

self.data
.for_each_schematic(world, false, |schematic, entity, tree| {
schematic.remove(entity, tree).unwrap();
.for_each_schematic(world, false, |schematic, context| {
schematic.remove(context).unwrap();
});
}
}
Expand Down Expand Up @@ -225,7 +224,7 @@ impl<T: Prototypical> ProtoCommandData<T> {

fn for_each_entity<F>(&self, world: &mut World, is_apply: bool, callback: F)
where
F: Fn(&EntityTreeNode, &mut EntityMut, &EntityTree, &Assets<T>, &mut T::Config),
F: Fn(&EntityTreeNode, &mut SchematicContext, &Assets<T>, &mut T::Config),
{
world.resource_scope(|world: &mut World, registry: Mut<ProtoRegistry<T>>| {
world.resource_scope(|world: &mut World, mut config: Mut<T::Config>| {
Expand All @@ -238,17 +237,19 @@ impl<T: Prototypical> ProtoCommandData<T> {
for node in entity_tree.iter() {
entity_tree.set_current(node);

let mut entity = world.entity_mut(node.entity());
let mut context = SchematicContext::new(world, &entity_tree);

#[cfg(feature = "auto_name")]
if is_apply && !entity.contains::<bevy::core::Name>() {
entity.insert(bevy::core::Name::new(format!(
"{} (Prototype)",
node.id()
)));
if let Some(mut entity) = context.entity_mut() {
if is_apply && !entity.contains::<bevy::core::Name>() {
entity.insert(bevy::core::Name::new(format!(
"{} (Prototype)",
node.id()
)));
}
}

callback(node, &mut entity, &entity_tree, &prototypes, &mut config);
callback(node, &mut context, &prototypes, &mut config);
}
})
})
Expand All @@ -261,48 +262,44 @@ impl<T: Prototypical> ProtoCommandData<T> {
/// [prototype]: Prototypical
fn for_each_schematic<F>(&self, world: &mut World, is_apply: bool, callback: F)
where
F: Fn(&DynamicSchematic, &mut EntityMut, &EntityTree),
F: Fn(&DynamicSchematic, &mut SchematicContext),
{
self.for_each_entity(
world,
is_apply,
|node, entity, entity_tree, prototypes, config| {
let on_before_prototype = if is_apply {
Config::<T>::on_before_apply_prototype
} else {
Config::<T>::on_before_remove_prototype
};
let on_after_prototype = if is_apply {
Config::<T>::on_after_apply_prototype
} else {
Config::<T>::on_after_remove_prototype
};
let on_before_schematic = if is_apply {
Config::<T>::on_before_apply_schematic
} else {
Config::<T>::on_before_remove_schematic
};
let on_after_schematic = if is_apply {
Config::<T>::on_after_apply_schematic
} else {
Config::<T>::on_after_remove_schematic
};

for handle_id in node.prototypes() {
let handle = prototypes.get_handle(*handle_id);
let proto = prototypes.get(&handle).unwrap();

on_before_prototype(config, proto, entity, entity_tree);

for (_, schematic) in proto.schematics().iter() {
on_before_schematic(config, schematic, entity, entity_tree);
callback(schematic, entity, entity_tree);
on_after_schematic(config, schematic, entity, entity_tree);
}
self.for_each_entity(world, is_apply, |node, context, prototypes, config| {
let on_before_prototype = if is_apply {
Config::<T>::on_before_apply_prototype
} else {
Config::<T>::on_before_remove_prototype
};
let on_after_prototype = if is_apply {
Config::<T>::on_after_apply_prototype
} else {
Config::<T>::on_after_remove_prototype
};
let on_before_schematic = if is_apply {
Config::<T>::on_before_apply_schematic
} else {
Config::<T>::on_before_remove_schematic
};
let on_after_schematic = if is_apply {
Config::<T>::on_after_apply_schematic
} else {
Config::<T>::on_after_remove_schematic
};

on_after_prototype(config, proto, entity, entity_tree);
for handle_id in node.prototypes() {
let handle = prototypes.get_handle(*handle_id);
let proto = prototypes.get(&handle).unwrap();

on_before_prototype(config, proto, context);

for (_, schematic) in proto.schematics().iter() {
on_before_schematic(config, schematic, context);
callback(schematic, context);
on_after_schematic(config, schematic, context);
}
},
);

on_after_prototype(config, proto, context);
}
});
}
}
48 changes: 9 additions & 39 deletions bevy_proto_backend/src/proto/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use bevy::asset::Handle;
use bevy::ecs::world::EntityMut;
use bevy::prelude::{FromWorld, Resource};

use crate::cycles::{Cycle, CycleResponse};
use crate::proto::Prototypical;
use crate::schematics::DynamicSchematic;
use crate::tree::EntityTree;
use crate::schematics::{DynamicSchematic, SchematicContext};

/// Configuration for a [prototype].
///
Expand Down Expand Up @@ -79,13 +77,7 @@ pub trait Config<T: Prototypical>: Resource + FromWorld {
///
/// [prototype]: Prototypical
/// [`ProtoCommands`]: crate::proto::ProtoCommands
fn on_before_apply_prototype(
&mut self,
prototype: &T,
entity: &mut EntityMut,
tree: &EntityTree,
) {
}
fn on_before_apply_prototype(&mut self, prototype: &T, context: &mut SchematicContext) {}

/// Callback method that's triggered _after_ a [prototype] is applied to an entity.
///
Expand All @@ -94,13 +86,7 @@ pub trait Config<T: Prototypical>: Resource + FromWorld {
///
/// [prototype]: Prototypical
/// [`ProtoCommands`]: crate::proto::ProtoCommands
fn on_after_apply_prototype(
&mut self,
prototype: &T,
entity: &mut EntityMut,
tree: &EntityTree,
) {
}
fn on_after_apply_prototype(&mut self, prototype: &T, context: &mut SchematicContext) {}

/// Callback method that's triggered _before_ a [prototype] is removed from an entity.
///
Expand All @@ -109,13 +95,7 @@ pub trait Config<T: Prototypical>: Resource + FromWorld {
///
/// [prototype]: Prototypical
/// [`ProtoCommands`]: crate::proto::ProtoCommands
fn on_before_remove_prototype(
&mut self,
prototype: &T,
entity: &mut EntityMut,
tree: &EntityTree,
) {
}
fn on_before_remove_prototype(&mut self, prototype: &T, context: &mut SchematicContext) {}

/// Callback method that's triggered _after_ a [prototype] is removed from an entity.
///
Expand All @@ -124,13 +104,7 @@ pub trait Config<T: Prototypical>: Resource + FromWorld {
///
/// [prototype]: Prototypical
/// [`ProtoCommands`]: crate::proto::ProtoCommands
fn on_after_remove_prototype(
&mut self,
prototype: &T,
entity: &mut EntityMut,
tree: &EntityTree,
) {
}
fn on_after_remove_prototype(&mut self, prototype: &T, context: &mut SchematicContext) {}

/// Callback method that's triggered _before_ a [schematic] is applied to an entity.
///
Expand All @@ -142,8 +116,7 @@ pub trait Config<T: Prototypical>: Resource + FromWorld {
fn on_before_apply_schematic(
&mut self,
schematic: &DynamicSchematic,
entity: &mut EntityMut,
tree: &EntityTree,
context: &mut SchematicContext,
) {
}

Expand All @@ -157,8 +130,7 @@ pub trait Config<T: Prototypical>: Resource + FromWorld {
fn on_after_apply_schematic(
&mut self,
schematic: &DynamicSchematic,
entity: &mut EntityMut,
tree: &EntityTree,
context: &mut SchematicContext,
) {
}

Expand All @@ -172,8 +144,7 @@ pub trait Config<T: Prototypical>: Resource + FromWorld {
fn on_before_remove_schematic(
&mut self,
schematic: &DynamicSchematic,
entity: &mut EntityMut,
tree: &EntityTree,
context: &mut SchematicContext,
) {
}

Expand All @@ -187,8 +158,7 @@ pub trait Config<T: Prototypical>: Resource + FromWorld {
fn on_after_remove_schematic(
&mut self,
schematic: &DynamicSchematic,
entity: &mut EntityMut,
tree: &EntityTree,
context: &mut SchematicContext,
) {
}

Expand Down
Loading

0 comments on commit faca07d

Please sign in to comment.