Build production-ready editor authoring workflows
CI / Format, lint, test, build (push) Waiting to run

This commit is contained in:
Rbanh committed 2026-07-11 12:41:04 -04:00
1 parent cc1fa6ea3b
commit 0553a85220
163 files changed
+34547 -1565

No files matched your search

+232 -8
View File
@@ -2,8 +2,8 @@
use bevy::prelude::*;
use shared::{
ActorKind, AuthoringLightKind, EditorVisibility, HierarchySiblingIndex, LevelObject, LightDesc,
ModelRef, PhysicsBody, PlayerSpawn,
ActorKind, AuthoringLightKind, EditorVisibility, HierarchySiblingIndex, HydratedPrefabMember,
LevelObject, LightDesc, ModelRef, PhysicsBody, PlayerSpawn, PrefabInstance,
};
use super::helpers::{entity_name, is_level_object, HierarchyNodeKind};
@@ -44,6 +44,66 @@ pub fn would_create_cycle(world: &World, targets: &[Entity], new_parent: Option<
.any(|entity| is_ancestor(world, *entity, parent))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HierarchyDropViolation {
Cycle,
PrefabBoundary { root: Entity },
}
/// Returns the authored prefab-instance root containing `entity`, including the root itself.
pub fn prefab_instance_boundary(world: &World, entity: Entity) -> Option<Entity> {
let mut current = Some(entity);
while let Some(candidate) = current {
if world.get::<PrefabInstance>(candidate).is_some() {
return Some(candidate);
}
current = world
.get::<ChildOf>(candidate)
.map(|child_of| child_of.parent())
.filter(|parent| is_level_object(world, *parent));
}
None
}
/// Authored local children may be attached to instances; hydrated source members remain read-only.
pub fn hierarchy_drop_violation(
world: &World,
targets: &[Entity],
new_parent: Option<Entity>,
) -> Option<HierarchyDropViolation> {
if would_create_cycle(world, targets, new_parent) {
return Some(HierarchyDropViolation::Cycle);
}
let prefab_targets: Vec<_> = targets
.iter()
.filter_map(|target| {
crate::assets::prefab_overrides::prefab_member_override_target(world, *target)
})
.collect();
if !prefab_targets.is_empty() {
let (root, path) = &prefab_targets[0];
let compatible_targets = prefab_targets.len() == targets.len()
&& prefab_targets.iter().all(|(candidate_root, candidate)| {
candidate_root == root && candidate.instance_chain == path.instance_chain
});
let compatible_parent = new_parent.is_none_or(|parent| {
crate::assets::prefab_overrides::prefab_member_override_target(world, parent)
.is_some_and(|(parent_root, parent_path)| {
parent_root == *root && parent_path.instance_chain == path.instance_chain
})
});
if compatible_targets && compatible_parent {
return None;
}
return Some(HierarchyDropViolation::PrefabBoundary { root: *root });
}
new_parent
.and_then(|parent| world.get::<HydratedPrefabMember>(parent))
.map(|owner| HierarchyDropViolation::PrefabBoundary {
root: owner.instance_root,
})
}
pub fn level_object_roots(world: &mut World) -> Vec<Entity> {
let mut query = world.query_filtered::<(Entity, Option<&ChildOf>), With<LevelObject>>();
query
@@ -67,6 +127,30 @@ pub fn authored_children(world: &World, parent: Entity) -> Vec<Entity> {
.unwrap_or_default()
}
pub fn mutable_authored_children(world: &World, parent: Entity) -> Vec<Entity> {
authored_children(world, parent)
.into_iter()
.filter(|entity| {
world.get::<HydratedPrefabMember>(*entity).is_none()
&& world
.get::<crate::scene_io::ComposedSceneMember>(*entity)
.is_none()
})
.collect()
}
fn mutable_level_object_roots(world: &mut World) -> Vec<Entity> {
level_object_roots(world)
.into_iter()
.filter(|entity| {
world.get::<HydratedPrefabMember>(*entity).is_none()
&& world
.get::<crate::scene_io::ComposedSceneMember>(*entity)
.is_none()
})
.collect()
}
fn entity_tie_break(a: Entity, b: Entity) -> std::cmp::Ordering {
a.to_bits().cmp(&b.to_bits())
}
@@ -75,8 +159,9 @@ pub fn sort_siblings(world: &World, entities: &mut [Entity], sort_mode: Hierarch
match sort_mode {
HierarchySort::Manual => {
entities.sort_by(|a, b| {
sibling_index(world, *a)
.cmp(&sibling_index(world, *b))
linked_order_class(world, *a)
.cmp(&linked_order_class(world, *b))
.then_with(|| sibling_index(world, *a).cmp(&sibling_index(world, *b)))
.then_with(|| entity_tie_break(*a, *b))
});
}
@@ -100,6 +185,18 @@ pub fn sort_siblings(world: &World, entities: &mut [Entity], sort_mode: Hierarch
}
}
fn linked_order_class(world: &World, entity: Entity) -> u8 {
if world.get::<HydratedPrefabMember>(entity).is_some()
|| world
.get::<crate::scene_io::ComposedSceneMember>(entity)
.is_some()
{
0
} else {
1
}
}
fn actor_kind_sort_key(world: &World, entity: Entity) -> u8 {
world
.get::<ActorKind>(entity)
@@ -108,6 +205,7 @@ fn actor_kind_sort_key(world: &World, entity: Entity) -> u8 {
ActorKind::Empty => 0,
ActorKind::Brush | ActorKind::StaticMesh | ActorKind::ImportedModel => 1,
ActorKind::Light => 2,
ActorKind::AudioSource | ActorKind::AudioListener => 3,
ActorKind::PrefabAnchor => 3,
ActorKind::PlayerSpawn => 4,
ActorKind::WeaponSpawn => 5,
@@ -160,6 +258,7 @@ pub fn renumber_siblings(world: &mut World, parent: Option<Entity>, ordered: &[E
}
pub fn capture_sibling_state(world: &World, entity: Entity) -> SiblingChange {
let transform = world.get::<Transform>(entity).copied();
SiblingChange {
entity,
old_parent: world
@@ -170,6 +269,8 @@ pub fn capture_sibling_state(world: &World, entity: Entity) -> SiblingChange {
.map(|child_of| child_of.parent()),
old_index: sibling_index(world, entity),
new_index: sibling_index(world, entity),
old_transform: transform,
new_transform: transform,
}
}
@@ -181,6 +282,9 @@ pub fn apply_sibling_change(world: &mut World, change: &SiblingChange) {
entity_mut.remove::<ChildOf>();
}
entity_mut.insert(HierarchySiblingIndex(change.old_index));
if let Some(transform) = change.old_transform {
entity_mut.insert(transform);
}
}
}
@@ -192,6 +296,9 @@ pub fn apply_sibling_change_new(world: &mut World, change: &SiblingChange) {
entity_mut.remove::<ChildOf>();
}
entity_mut.insert(HierarchySiblingIndex(change.new_index));
if let Some(transform) = change.new_transform {
entity_mut.insert(transform);
}
}
}
@@ -235,8 +342,8 @@ fn renumber_inserted_siblings(
insert_index: i32,
) -> Vec<SiblingChange> {
let mut siblings = match new_parent {
Some(parent) => authored_children(world, parent),
None => level_object_roots(world),
Some(parent) => mutable_authored_children(world, parent),
None => mutable_level_object_roots(world),
};
siblings.retain(|entity| !moving.contains(entity));
let insert = insert_index.clamp(0, siblings.len() as i32) as usize;
@@ -252,8 +359,8 @@ fn renumber_existing_siblings(
exclude: &[Entity],
) -> Vec<SiblingChange> {
let mut siblings = match parent {
Some(parent) => authored_children(world, parent),
None => level_object_roots(world),
Some(parent) => mutable_authored_children(world, parent),
None => mutable_level_object_roots(world),
};
siblings.retain(|entity| !exclude.contains(entity));
sort_siblings(world, &mut siblings, HierarchySort::Manual);
@@ -272,6 +379,20 @@ fn apply_sibling_order(
.map(|child_of| child_of.parent());
let old_index = sibling_index(world, *entity);
let new_index = index as i32;
let old_transform = world.get::<Transform>(*entity).copied();
let new_transform = if old_parent != parent {
world
.get::<GlobalTransform>(*entity)
.map(|global| {
match parent.and_then(|parent| world.get::<GlobalTransform>(parent)) {
Some(parent_global) => global.reparented_to(parent_global),
None => global.compute_transform(),
}
})
.or(old_transform)
} else {
old_transform
};
if old_parent != parent || old_index != new_index {
changes.push(SiblingChange {
entity: *entity,
@@ -279,6 +400,8 @@ fn apply_sibling_order(
new_parent: parent,
old_index,
new_index,
old_transform,
new_transform,
});
}
if let Ok(mut entity_mut) = world.get_entity_mut(*entity) {
@@ -288,6 +411,9 @@ fn apply_sibling_order(
entity_mut.remove::<ChildOf>();
}
entity_mut.insert(HierarchySiblingIndex(new_index));
if let Some(transform) = new_transform {
entity_mut.insert(transform);
}
}
}
changes
@@ -340,6 +466,8 @@ pub fn actor_kind_icon(kind: ActorKind) -> &'static str {
ActorKind::StaticMesh => icons::CUBE.as_str(),
ActorKind::ImportedModel => icons::CUBE_TRANSPARENT.as_str(),
ActorKind::Light => icons::LIGHTBULB.as_str(),
ActorKind::AudioSource => icons::SPEAKER_HIGH.as_str(),
ActorKind::AudioListener => icons::HEADPHONES.as_str(),
ActorKind::PrefabAnchor => icons::PACKAGE.as_str(),
ActorKind::PlayerSpawn => icons::USER_CIRCLE.as_str(),
ActorKind::WeaponSpawn => icons::CROSSHAIR.as_str(),
@@ -434,6 +562,7 @@ pub fn is_entity_locked(locked: &std::collections::HashSet<Entity>, entity: Enti
#[cfg(test)]
mod tests {
use super::*;
use shared::ActorId;
#[test]
fn sort_by_name_is_stable_for_duplicate_names() {
@@ -448,4 +577,99 @@ mod tests {
sort_siblings(&world, &mut entities, HierarchySort::Name);
assert_eq!(entities, first, "duplicate-name sort must be deterministic");
}
#[test]
fn reparent_preserves_world_transform_and_round_trips_history() {
let mut world = World::new();
let parent = world
.spawn((
LevelObject,
Transform::from_xyz(10.0, 0.0, 0.0),
GlobalTransform::from_xyz(10.0, 0.0, 0.0),
HierarchySiblingIndex(0),
))
.id();
let child = world
.spawn((
LevelObject,
Transform::from_xyz(12.0, 1.0, -3.0),
GlobalTransform::from_xyz(12.0, 1.0, -3.0),
HierarchySiblingIndex(1),
))
.id();
let changes = reorder_entities_under_parent(&mut world, &[child], Some(parent), 0);
let child_change = changes
.iter()
.find(|change| change.entity == child)
.expect("reparent should record the moved actor");
assert_eq!(world.get::<ChildOf>(child).unwrap().parent(), parent);
assert_eq!(
world.get::<Transform>(child).unwrap().translation,
Vec3::new(2.0, 1.0, -3.0)
);
apply_sibling_change(&mut world, child_change);
assert!(world.get::<ChildOf>(child).is_none());
assert_eq!(
world.get::<Transform>(child).unwrap().translation,
Vec3::new(12.0, 1.0, -3.0)
);
apply_sibling_change_new(&mut world, child_change);
assert_eq!(world.get::<ChildOf>(child).unwrap().parent(), parent);
assert_eq!(
world.get::<Transform>(child).unwrap().translation,
Vec3::new(2.0, 1.0, -3.0)
);
}
#[test]
fn cycle_detection_rejects_descendant_parent() {
let mut world = World::new();
let parent = world.spawn(LevelObject).id();
let child = world.spawn((LevelObject, ChildOf(parent))).id();
assert!(would_create_cycle(&world, &[parent], Some(child)));
assert!(!would_create_cycle(&world, &[child], None));
}
#[test]
fn prefab_boundary_allows_local_and_same_layer_override_structure() {
let mut world = World::new();
let instance = world.spawn((LevelObject, PrefabInstance::default())).id();
let local_child = world.spawn((LevelObject, ChildOf(instance))).id();
let generated_child = world
.spawn((
LevelObject,
ActorId::new("generated-child"),
ChildOf(instance),
HydratedPrefabMember {
instance_root: instance,
},
))
.id();
let actor = world.spawn(LevelObject).id();
let nested_instance = world.spawn((LevelObject, PrefabInstance::default())).id();
assert_eq!(
hierarchy_drop_violation(&world, &[actor], Some(instance)),
None
);
assert_eq!(
hierarchy_drop_violation(&world, &[nested_instance], Some(local_child)),
None
);
assert_eq!(
hierarchy_drop_violation(&world, &[nested_instance], Some(generated_child)),
Some(HierarchyDropViolation::PrefabBoundary { root: instance })
);
assert_eq!(
hierarchy_drop_violation(&world, &[generated_child], None),
None
);
assert_eq!(hierarchy_drop_violation(&world, &[instance], None), None);
assert_eq!(hierarchy_drop_violation(&world, &[local_child], None), None);
}
}