Skip to content

Commit

Permalink
transform: TransformPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Jul 16, 2020
1 parent f546aad commit d9adea1
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 48 deletions.
32 changes: 32 additions & 0 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl AppBuilder {
self.add_system_to_stage(stage::UPDATE, system)
}

pub fn add_systems(&mut self, systems: Vec<Box<dyn System>>) -> &mut Self {
self.add_systems_to_stage(stage::UPDATE, systems)
}

pub fn init_system(
&mut self,
build: impl FnMut(&mut Resources) -> Box<dyn System>,
Expand Down Expand Up @@ -101,13 +105,30 @@ impl AppBuilder {
self
}

pub fn add_startup_systems_to_stage(
&mut self,
stage_name: &'static str,
systems: Vec<Box<dyn System>>,
) -> &mut Self {
for system in systems {
self.app
.startup_schedule
.add_system_to_stage(stage_name, system);
}
self
}

pub fn add_startup_system(&mut self, system: Box<dyn System>) -> &mut Self {
self.app
.startup_schedule
.add_system_to_stage(startup_stage::STARTUP, system);
self
}

pub fn add_startup_systems(&mut self, systems: Vec<Box<dyn System>>) -> &mut Self {
self.add_startup_systems_to_stage(startup_stage::STARTUP, systems)
}

pub fn init_startup_system(
&mut self,
build: impl FnMut(&mut Resources) -> Box<dyn System>,
Expand Down Expand Up @@ -144,6 +165,17 @@ impl AppBuilder {
self
}

pub fn add_systems_to_stage(
&mut self,
stage_name: &'static str,
systems: Vec<Box<dyn System>>,
) -> &mut Self {
for system in systems {
self.app.schedule.add_system_to_stage(stage_name, system);
}
self
}

pub fn add_event<T>(&mut self) -> &mut Self
where
T: Send + Sync + 'static,
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ bevy_derive = { path = "../bevy_derive" }
bevy_ecs = { path = "../bevy_ecs" }
bevy_property = { path = "../bevy_property" }
bevy_type_registry = { path = "../bevy_type_registry" }
bevy_transform = { path = "../bevy_transform" }
glam = "0.8.7"
25 changes: 2 additions & 23 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
pub mod bytes;
pub mod float_ord;
pub mod time;
pub mod transform;
pub mod math;

use bevy_app::{stage, startup_stage, AppBuilder, AppPlugin};
use bevy_app::{stage, AppBuilder, AppPlugin};
use bevy_ecs::IntoQuerySystem;
use bevy_transform::{
build_systems,
components::{
Children, LocalTransform, NonUniformScale, Rotation, Scale, Transform, Translation,
},
};
use bevy_type_registry::RegisterType;
use glam::{Mat3, Mat4, Quat, Vec2, Vec3};
use time::{time_system, timer_system, Time, Timer};
Expand All @@ -20,22 +14,7 @@ pub struct CorePlugin;

impl AppPlugin for CorePlugin {
fn build(&self, app: &mut AppBuilder) {
// we also add a copy of transform systems to startup to ensure we begin with correct transform/parent state
for transform_system in build_systems() {
app.add_startup_system_to_stage(startup_stage::POST_STARTUP, transform_system);
}
for transform_system in build_systems() {
app.add_system_to_stage(stage::POST_UPDATE, transform_system);
}

app.init_resource::<Time>()
.register_component::<Children>()
.register_component::<LocalTransform>()
.register_component::<Transform>()
.register_component::<Translation>()
.register_component::<Rotation>()
.register_component::<Scale>()
.register_component::<NonUniformScale>()
.register_component::<Timer>()
.register_property_type::<Vec2>()
.register_property_type::<Vec3>()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod face_toward;
mod hierarchy;

pub use face_toward::*;
pub use hierarchy::*;
2 changes: 2 additions & 0 deletions crates/bevy_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ edition = "2018"
license = "MIT"

[dependencies]
bevy_app = { path = "../bevy_app"}
bevy_ecs = { path = "../bevy_ecs"}
bevy_property = { path = "../bevy_property" }
bevy_type_registry = { path = "../bevy_type_registry" }
glam = "0.8.7"
log = "0.4"
smallvec = { version = "1.4", features = ["serde"] }
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::components::Children;
use bevy_ecs::{Entity, Query};
use bevy_transform::prelude::Children;

pub fn run_on_hierarchy<T, S>(
children_query: &Query<&Children>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ pub fn hierarchy_maintenance_systems() -> Vec<Box<dyn System>> {
#[cfg(test)]
mod test {
use super::*;
use crate::build_systems;
use crate::transform_systems;
use bevy_ecs::{Resources, Schedule, World};

#[test]
Expand All @@ -119,7 +119,7 @@ mod test {

let mut schedule = Schedule::default();
schedule.add_stage("update");
for system in build_systems() {
for system in transform_systems() {
schedule.add_system_to_stage("update", system);
}

Expand Down
9 changes: 9 additions & 0 deletions crates/bevy_transform/src/hierarchy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod child_builder;
mod hierarchy;
mod hierarchy_maintenance_system;
mod world_child_builder;

pub use child_builder::*;
pub use hierarchy::*;
pub use hierarchy_maintenance_system::*;
pub use world_child_builder::*;
42 changes: 30 additions & 12 deletions crates/bevy_transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
pub use glam as math;

pub mod child_builder;
pub mod hierarchy;
pub mod components;
pub mod hierarchy_maintenance_system;
pub mod local_transform_systems;
pub mod transform_propagate_system;
pub mod transform_systems;
pub mod world_child_builder;

pub mod prelude {
pub use crate::{build_systems, child_builder::*, components::*, world_child_builder::*};
pub use crate::{components::*, hierarchy::*, TransformPlugin};
}

use bevy_app::{AppBuilder, AppPlugin};
use bevy_ecs::{IntoQuerySystem, System};
use bevy_type_registry::RegisterType;
use prelude::{Children, LocalTransform, NonUniformScale, Rotation, Scale, Transform, Translation};

// TODO: make this a plugin
pub fn build_systems() -> Vec<Box<dyn System>> {
let mut all_systems = Vec::with_capacity(5);
pub(crate) fn transform_systems() -> Vec<Box<dyn System>> {
let mut systems = Vec::with_capacity(5);

all_systems.append(&mut hierarchy_maintenance_system::hierarchy_maintenance_systems());
all_systems.append(&mut local_transform_systems::local_transform_systems());
all_systems.append(&mut transform_systems::transform_systems());
all_systems.push(transform_propagate_system::transform_propagate_system.system());
systems.append(&mut hierarchy::hierarchy_maintenance_systems());
systems.append(&mut local_transform_systems::local_transform_systems());
systems.append(&mut transform_systems::transform_systems());
systems.push(transform_propagate_system::transform_propagate_system.system());

all_systems
systems
}

#[derive(Default)]
pub struct TransformPlugin;

impl AppPlugin for TransformPlugin {
fn build(&self, app: &mut AppBuilder) {
app.register_component::<Children>()
.register_component::<LocalTransform>()
.register_component::<Transform>()
.register_component::<Translation>()
.register_component::<Rotation>()
.register_component::<Scale>()
.register_component::<NonUniformScale>()
// add transform systems to startup so the first update is "correct"
.add_startup_systems(transform_systems())
.add_systems(transform_systems());
}
}
4 changes: 2 additions & 2 deletions crates/bevy_transform/src/transform_propagate_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn propagate_recursive(
#[cfg(test)]
mod test {
use super::*;
use crate::build_systems;
use crate::transform_systems;
use bevy_ecs::{Resources, Schedule, World};
use glam::{Mat4, Vec3};

Expand All @@ -81,7 +81,7 @@ mod test {

let mut schedule = Schedule::default();
schedule.add_stage("update");
for system in build_systems() {
for system in transform_systems() {
schedule.add_system_to_stage("update", system);
}

Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = "2018"
bevy_app = { path = "../bevy_app" }
bevy_asset = { path = "../bevy_asset" }
bevy_type_registry = { path = "../bevy_type_registry" }
bevy_core = { path = "../bevy_core" }
bevy_derive = { path = "../bevy_derive" }
bevy_ecs = { path = "../bevy_ecs" }
bevy_sprite = { path = "../bevy_sprite" }
Expand Down
8 changes: 5 additions & 3 deletions crates/bevy_ui/src/ui_update_system.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::Node;
use bevy_core::transform::run_on_hierarchy;
use bevy_ecs::{Entity, Query, Res, Without};
use bevy_transform::prelude::{Children, Parent, Translation};
use bevy_transform::{
hierarchy,
prelude::{Children, Parent, Translation},
};
use bevy_window::Windows;
use glam::Vec2;

Expand Down Expand Up @@ -39,7 +41,7 @@ pub fn ui_update_system(
size: window_size,
});
for entity in orphan_nodes {
previous_sibling_result = run_on_hierarchy(
previous_sibling_result = hierarchy::run_on_hierarchy(
&children_query,
&mut node_query,
entity,
Expand Down
2 changes: 1 addition & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use crate::{
audio::{AudioOutput, AudioSource},
core::{
time::{Time, Timer},
transform::FaceToward,
math::FaceToward,
},
diagnostic::DiagnosticsPlugin,
ecs::{
Expand Down

0 comments on commit d9adea1

Please sign in to comment.