Complete editor operator invariants
CI / Format, lint, test, build (push) Waiting to run

This commit is contained in:
Rbanh committed 2026-07-12 23:53:46 -04:00
1 parent 9a67a25a15
commit 1ab3886028
24 files changed
+1836 -311

No files matched your search

+13
View File
@@ -106,6 +106,11 @@ pub struct PrefabEditState {
#[derive(Debug, Clone)]
pub enum EditorCommand {
GroupSelection {
snapshot: EditorEntitySnapshot,
group: Option<Entity>,
changes: Vec<SiblingChange>,
},
Spawn {
snapshot: EditorEntitySnapshot,
entity: Option<Entity>,
@@ -152,6 +157,12 @@ pub enum EditorCommand {
old: Option<LightDesc>,
new: LightDesc,
},
SetLightGroup {
entities: Vec<Entity>,
olds: Vec<Option<LightDesc>>,
news: Vec<Option<LightDesc>>,
label: &'static str,
},
SetAnimationController {
entity: Entity,
old: Option<AnimationControllerDesc>,
@@ -293,6 +304,7 @@ pub enum EditorCommand {
impl EditorCommand {
pub fn label(&self) -> &'static str {
match self {
EditorCommand::GroupSelection { .. } => "Group Selection",
EditorCommand::Spawn { .. } => "Spawn",
EditorCommand::SpawnMany { .. } => "Spawn Brushes",
EditorCommand::Despawn { .. } => "Delete",
@@ -303,6 +315,7 @@ impl EditorCommand {
EditorCommand::SetMaterialGroup { .. } => "Apply Material to Selection",
EditorCommand::SetMaterialOverride { .. } => "Set Material Override",
EditorCommand::SetLight { .. } => "Set Light",
EditorCommand::SetLightGroup { label, .. } => label,
EditorCommand::SetAnimationController { .. } => "Set Animation Controller",
EditorCommand::SetAudioSource { .. } => "Set Audio Source",
EditorCommand::SetAudioListener { .. } => "Set Audio Listener",
+217 -40
View File
@@ -17,6 +17,7 @@ use shared::{
TriggerVolume, WeaponSpawn,
};
use crate::operators::{ActiveOperator, OperatorPhase, OperatorStatus};
use crate::scene_io::{SceneIo, SceneIoRequest};
use crate::selection::SelectedEntity;
use crate::state::scene_tools_active;
@@ -560,6 +561,40 @@ pub fn set_light_with_history(world: &mut World, entity: Entity, new: LightDesc)
push_history(world, EditorCommand::SetLight { entity, old, new });
}
pub fn set_light_group_with_history(
world: &mut World,
changes: impl IntoIterator<Item = (Entity, Option<LightDesc>)>,
label: &'static str,
) {
let mut entities = Vec::new();
let mut olds = Vec::new();
let mut news = Vec::new();
for (entity, new) in changes {
if !is_mutable_level_object(world, entity) {
continue;
}
let old = world.get::<LightDesc>(entity).cloned();
if old.is_none() && new.is_none() {
continue;
}
apply_light(world, entity, &new);
entities.push(entity);
olds.push(old);
news.push(new);
}
if !entities.is_empty() {
push_history(
world,
EditorCommand::SetLightGroup {
entities,
olds,
news,
label,
},
);
}
}
pub fn set_audio_source_with_history(world: &mut World, entity: Entity, new: AudioSourceDesc) {
if !is_mutable_level_object(world, entity) {
return;
@@ -752,9 +787,13 @@ pub fn apply_brush_csg_with_history(
new_transform: Transform,
new_brush: BrushDesc,
delete_entities: &[Entity],
) {
if !is_level_object(world, primary) {
return;
) -> bool {
if !is_mutable_level_object(world, primary)
|| delete_entities
.iter()
.any(|entity| !is_mutable_level_object(world, *entity))
{
return false;
}
let old_transform = world.get::<Transform>(primary).copied().unwrap_or_default();
let old_brush = world.get::<BrushDesc>(primary).cloned();
@@ -769,7 +808,7 @@ pub fn apply_brush_csg_with_history(
&& old_brush.as_ref() == Some(&new_brush)
&& deleted.is_empty()
{
return;
return false;
}
if let Ok(mut entity_mut) = world.get_entity_mut(primary) {
entity_mut.insert(new_transform);
@@ -790,6 +829,7 @@ pub fn apply_brush_csg_with_history(
},
);
select_one(world, primary);
true
}
pub fn set_static_mesh_renderer_with_history(
@@ -943,14 +983,16 @@ pub fn group_selection_with_history(world: &mut World, entities: &[Entity]) {
children: Vec::new(),
};
let group = spawn_snapshot(world, &snapshot);
let changes = reorder_entities_under_parent(world, &entities, Some(group), i32::MAX);
push_history(
world,
EditorCommand::Spawn {
EditorCommand::GroupSelection {
snapshot,
entity: Some(group),
group: Some(group),
changes,
},
);
reorder_siblings_with_history(world, &entities, Some(group), i32::MAX);
select_one(world, group);
}
pub fn create_empty_child_with_history(world: &mut World, parent: Entity) {
@@ -1637,6 +1679,13 @@ fn prepare_prefab_source_history(
fn undo_command(world: &mut World, command: &mut EditorCommand) {
match command {
EditorCommand::GroupSelection { group, changes, .. } => {
for change in changes.iter().rev() {
apply_sibling_change(world, change);
}
despawn_entity(world, *group);
clear_selection(world);
}
EditorCommand::Spawn { entity, .. } => {
despawn_entity(world, entity.take());
clear_selection(world);
@@ -1682,6 +1731,11 @@ fn undo_command(world: &mut World, command: &mut EditorCommand) {
EditorCommand::SetLight { entity, old, .. } => {
apply_light(world, *entity, old);
}
EditorCommand::SetLightGroup { entities, olds, .. } => {
for (entity, old) in entities.iter().zip(olds.iter()) {
apply_light(world, *entity, old);
}
}
EditorCommand::SetAnimationController { entity, old, .. } => {
apply_animation_controller(world, *entity, old);
}
@@ -1818,6 +1872,25 @@ fn undo_command(world: &mut World, command: &mut EditorCommand) {
fn redo_command(world: &mut World, command: &mut EditorCommand) {
match command {
EditorCommand::GroupSelection {
snapshot,
group,
changes,
} => {
let previous_group = *group;
let spawned = spawn_snapshot(world, snapshot);
*group = Some(spawned);
for change in changes.iter_mut() {
if Some(change.entity) == previous_group {
change.entity = spawned;
}
if change.new_parent == previous_group {
change.new_parent = Some(spawned);
}
apply_sibling_change_new(world, change);
}
select_one(world, spawned);
}
EditorCommand::Spawn { snapshot, entity } => {
let spawned = spawn_snapshot(world, snapshot);
*entity = Some(spawned);
@@ -1881,6 +1954,11 @@ fn redo_command(world: &mut World, command: &mut EditorCommand) {
entity_mut.insert(new.clone());
}
}
EditorCommand::SetLightGroup { entities, news, .. } => {
for (entity, new) in entities.iter().zip(news.iter()) {
apply_light(world, *entity, new);
}
}
EditorCommand::SetAnimationController { entity, new, .. } => {
if let Ok(mut entity_mut) = world.get_entity_mut(*entity) {
entity_mut.insert(new.clone());
@@ -2614,25 +2692,41 @@ fn capture_gizmo_transform_edits(world: &mut World) {
let start_group =
active.map(|(entity, transform)| selected_transforms(world, &selection, entity, transform));
let mut started_edit = false;
let (finished_edit, live_edit) = {
let mut tracker = world.resource_mut::<TransformEditTracker>();
if tracker.active.is_none() {
if let (Some((entity, transform)), Some(group)) = (active, start_group) {
tracker.active = Some((entity, transform, group));
return;
started_edit = true;
}
}
match tracker.active.take() {
Some((primary, old, group)) if active.is_none() => (Some((primary, old, group)), None),
Some(state) if active.is_some() => {
let live = Some(state.clone());
tracker.active = Some(state);
(None, live)
if started_edit {
(None, None)
} else {
match tracker.active.take() {
Some((primary, old, group)) if active.is_none() => {
(Some((primary, old, group)), None)
}
Some(state) if active.is_some() => {
let live = Some(state.clone());
tracker.active = Some(state);
(None, live)
}
_ => (None, None),
}
_ => (None, None),
}
};
if started_edit {
set_transform_operator_status(
world,
OperatorPhase::Preview,
"Transform gizmo drag in progress",
);
return;
}
if let Some((primary, old_primary, group)) = live_edit {
let Some(new_primary) = world.get::<Transform>(primary).copied() else {
return;
@@ -2645,14 +2739,50 @@ fn capture_gizmo_transform_edits(world: &mut World) {
if let Some((primary, old_primary, group)) = finished_edit {
let Some(new_primary) = world.get::<Transform>(primary).copied() else {
for (entity, old) in group {
if let Some(mut transform) = world.get_mut::<Transform>(entity) {
*transform = old;
}
}
set_transform_operator_status(
world,
OperatorPhase::Canceled,
"Transform target disappeared; surviving previews restored",
);
return;
};
let undo_depth = world.resource::<EditorHistory>().undo_depth();
if group.len() <= 1 {
set_transform_with_history(world, primary, old_primary, new_primary);
return;
} else {
let changes = transform_group_changes(old_primary, new_primary, group);
set_transform_group_with_history(world, changes);
}
let changes = transform_group_changes(old_primary, new_primary, group);
set_transform_group_with_history(world, changes);
if world.resource::<EditorHistory>().undo_depth() > undo_depth {
set_transform_operator_status(
world,
OperatorPhase::Committed,
"Transform gizmo edit committed",
);
} else {
set_transform_operator_status(
world,
OperatorPhase::Canceled,
"Transform gizmo edit made no changes",
);
}
}
}
fn set_transform_operator_status(world: &mut World, phase: OperatorPhase, hint: impl Into<String>) {
if let Some(mut active) = world.get_resource_mut::<ActiveOperator>() {
active.status = Some(OperatorStatus {
id: "transform.gizmo".to_string(),
label: "Transform Gizmo".to_string(),
phase,
hint: hint.into(),
warnings: Vec::new(),
});
}
}
@@ -2796,7 +2926,7 @@ pub(crate) fn material_eq(a: &MaterialDesc, b: &MaterialDesc) -> bool {
mod tests {
use super::*;
use crate::operators::test_harness::{assert_undo_redo_round_trip, OperatorInvariantHarness};
use crate::operators::{ActiveOperator, OperatorPhase, OperatorStatus};
use crate::operators::{ActiveOperator, OperatorPhase};
#[derive(Component, Reflect, Default, Debug, Clone, PartialEq)]
#[reflect(Component, Default)]
@@ -3074,6 +3204,7 @@ mod tests {
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
world.init_resource::<TransformEditTracker>();
let first = world.spawn((LevelObject, Transform::default())).id();
let second = world
.spawn((
@@ -3085,30 +3216,19 @@ mod tests {
let committed = vec![Vec3::Z, Vec3::new(2.0, 0.0, 1.0)];
let harness = OperatorInvariantHarness::capture(&mut world);
set_transform_group_with_history(
&mut world,
[
(
first,
Transform::default(),
Transform::from_translation(committed[0]),
),
(
second,
Transform::from_translation(initial[1]),
Transform::from_translation(committed[1]),
),
world.resource_mut::<TransformEditTracker>().active = Some((
first,
Transform::default(),
vec![
(first, Transform::default()),
(second, Transform::from_translation(initial[1])),
],
);
world.resource_mut::<ActiveOperator>().status = Some(OperatorStatus {
id: "transform.gizmo".to_string(),
label: "Transform Gizmo".to_string(),
phase: OperatorPhase::Committed,
hint: "Committed multi-selection drag".to_string(),
warnings: Vec::new(),
});
));
*world.get_mut::<Transform>(first).unwrap() = Transform::from_translation(committed[0]);
capture_gizmo_transform_edits(&mut world);
harness.assert_committed(&mut world, 1, 0);
harness.assert_status(&world, "transform.gizmo", OperatorPhase::Committed);
assert_undo_redo_round_trip(&mut world, initial, committed, |world| {
[first, second]
.into_iter()
@@ -3117,6 +3237,63 @@ mod tests {
});
}
#[test]
fn no_op_transform_release_terminates_preview_without_history() {
let mut world = World::new();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
world.init_resource::<TransformEditTracker>();
let transform = Transform::from_xyz(1.0, 2.0, 3.0);
let entity = world.spawn((LevelObject, transform)).id();
world.resource_mut::<TransformEditTracker>().active =
Some((entity, transform, vec![(entity, transform)]));
set_transform_operator_status(
&mut world,
OperatorPhase::Preview,
"Transform gizmo drag in progress",
);
let harness = OperatorInvariantHarness::capture(&mut world);
capture_gizmo_transform_edits(&mut world);
harness.assert_canceled(&mut world);
harness.assert_status(&world, "transform.gizmo", OperatorPhase::Canceled);
assert_eq!(world.get::<Transform>(entity), Some(&transform));
}
#[test]
fn missing_primary_transform_rolls_back_surviving_preview_targets() {
let mut world = World::new();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
world.init_resource::<TransformEditTracker>();
let primary_old = Transform::from_xyz(1.0, 0.0, 0.0);
let survivor_old = Transform::from_xyz(2.0, 0.0, 0.0);
let primary = world.spawn((LevelObject, primary_old)).id();
let survivor = world.spawn((LevelObject, survivor_old)).id();
world.resource_mut::<TransformEditTracker>().active = Some((
primary,
primary_old,
vec![(primary, primary_old), (survivor, survivor_old)],
));
*world.get_mut::<Transform>(survivor).unwrap() = Transform::from_xyz(8.0, 0.0, 0.0);
world.despawn(primary);
set_transform_operator_status(
&mut world,
OperatorPhase::Preview,
"Transform gizmo drag in progress",
);
let harness = OperatorInvariantHarness::capture(&mut world);
capture_gizmo_transform_edits(&mut world);
harness.assert_canceled(&mut world);
harness.assert_status(&world, "transform.gizmo", OperatorPhase::Canceled);
assert_eq!(world.get::<Transform>(survivor), Some(&survivor_old));
}
#[test]
fn unpack_prefab_is_one_undoable_history_command() {
let mut world = World::new();