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

+135
View File
@@ -469,6 +469,86 @@ mod tests {
);
}
#[test]
fn multi_actor_material_assignment_is_one_undo_group() {
let mut world = World::new();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
let material_path = std::env::temp_dir().join(format!(
"blacksite-material-operator-{}.material.ron",
uuid::Uuid::new_v4()
));
let fixture = shared::MaterialAsset {
schema_version: shared::MATERIAL_ASSET_SCHEMA_VERSION,
label: "Concrete".into(),
shader: None,
shader_ref: None,
render_state: Default::default(),
material: MaterialDesc {
roughness: 0.85,
..Default::default()
},
};
std::fs::write(
&material_path,
ron::ser::to_string_pretty(&fixture, ron::ser::PrettyConfig::default()).unwrap(),
)
.unwrap();
let material_path = material_path.to_string_lossy().into_owned();
world.insert_resource(AssetRegistry {
records: vec![AssetRecord {
id: AssetId::new(),
path: material_path.clone(),
label: "Concrete".into(),
kind_tag: "Material".into(),
import_settings: Default::default(),
dependencies: Vec::new(),
}],
index_dirty: false,
});
let first = world.spawn((LevelObject, MaterialDesc::default())).id();
let second = world.spawn((LevelObject, MaterialDesc::default())).id();
let mut selected = SelectedEntities::default();
selected.select_replace(first);
selected.select_maybe_add(second, true);
let asset = EditorAsset {
label: "Concrete".to_string(),
path: Some(material_path.clone()),
folder_path: "assets/materials".to_string(),
kind: super::super::EditorAssetKind::Material,
};
super::super::materials::material_desc_from_asset(&material_path)
.expect("committed material fixture should resolve");
let harness = OperatorInvariantHarness::capture(&mut world);
let read_materials = move |world: &mut World| {
[first, second]
.into_iter()
.map(|entity| {
world
.get::<MaterialDesc>(entity)
.and_then(|material| material.material_asset_path.clone())
})
.collect::<Vec<_>>()
};
let applied = apply_material_operator(&mut world, asset, &selected);
assert!(
applied,
"material operator failed: {:?}",
world.resource::<ActiveOperator>().status
);
harness.assert_committed(&mut world, 1, 0);
assert_undo_redo_round_trip(
&mut world,
vec![None, None],
vec![Some(material_path.clone()), Some(material_path.clone())],
read_materials,
);
let _ = std::fs::remove_file(material_path);
}
#[test]
fn missing_subasset_commit_cancels_without_side_effects() {
let mut world = World::new();
@@ -533,4 +613,59 @@ mod tests {
harness.assert_blocked(&mut world);
}
#[test]
fn material_assignment_without_authored_selection_is_blocked() {
let mut world = World::new();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
let harness = OperatorInvariantHarness::capture(&mut world);
let asset = EditorAsset {
label: "Concrete".to_string(),
path: Some("assets/materials/concrete.ron".to_string()),
folder_path: "assets/materials".to_string(),
kind: super::super::EditorAssetKind::Material,
};
assert!(!apply_material_operator(
&mut world,
asset,
&SelectedEntities::default()
));
harness.assert_blocked(&mut world);
}
#[test]
fn audio_and_animation_assignment_reject_incompatible_actors() {
let mut world = World::new();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
let entity = world.spawn(LevelObject).id();
let audio = EditorAsset {
label: "Impact".into(),
path: Some("assets/audio/impact.ogg".into()),
folder_path: "assets/audio".into(),
kind: super::super::EditorAssetKind::AudioClip,
};
let audio_harness = OperatorInvariantHarness::capture(&mut world);
assert!(!assign_audio_clip_operator(&mut world, audio, entity));
audio_harness.assert_blocked(&mut world);
let animation_harness = OperatorInvariantHarness::capture(&mut world);
let selection = AssetSelection::SubAsset {
parent_path: "assets/models/missing.glb".into(),
sub_asset_id: "animation:missing".into(),
label: "Missing".into(),
kind: super::super::AssetSubAssetKind::AnimationClip,
source_path: None,
};
assert!(!assign_animation_clip_operator(
&mut world, selection, entity
));
animation_harness.assert_blocked(&mut world);
}
}
+305 -33
View File
@@ -4,7 +4,7 @@ use bevy::prelude::*;
use bevy_egui::{egui, EguiContexts, EguiPrimaryContextPass};
use crate::history::group_selection_with_history;
use crate::operators::{run_immediate_operator, OperatorAvailability};
use crate::operators::{run_operator_action, OperatorAction, OperatorAvailability};
use crate::state::{EditorMode, PlayPossession};
use crate::ui::helpers::{
reset_scene_lighting_to_project_defaults, toggle_play_mode, toggle_play_paused,
@@ -27,7 +27,7 @@ pub trait EditorCommand: Send + Sync {
fn disabled_reason(&self, _world: &World) -> Option<String> {
None
}
fn execute(&self, world: &mut World);
fn execute(&self, world: &mut World) -> Result<OperatorAction, String>;
}
#[derive(Resource, Default)]
@@ -70,13 +70,11 @@ impl EditorCommandRegistry {
.map(|cmd| (cmd.label().to_string(), cmd.disabled_reason(world)))
}
pub fn run(&self, world: &mut World, name: &str) -> bool {
if let Some(command) = self.commands.iter().find(|cmd| cmd.name() == name) {
command.execute(world);
true
} else {
false
}
pub fn run(&self, world: &mut World, name: &str) -> Option<Result<OperatorAction, String>> {
self.commands
.iter()
.find(|command| command.name() == name)
.map(|command| command.execute(world))
}
}
@@ -191,6 +189,10 @@ pub fn register_editor_plugin(app: &mut App, plugin: Box<dyn EditorPlugin>) {
}
fn register_builtin_commands(mut registry: ResMut<EditorCommandRegistry>) {
populate_builtin_commands(&mut registry);
}
fn populate_builtin_commands(registry: &mut EditorCommandRegistry) {
registry.register(Box::new(TogglePlayCommand));
registry.register(Box::new(TogglePlayPausedCommand));
registry.register(Box::new(TogglePossessionCommand));
@@ -235,7 +237,7 @@ fn dispatch_editor_command(world: &mut World, name: &str) {
let name_for_commit = name.to_string();
let name_for_status = name.to_string();
let label_for_commit = label.clone();
run_immediate_operator(
run_operator_action(
world,
&name_for_status,
&label,
@@ -244,14 +246,10 @@ fn dispatch_editor_command(world: &mut World, name: &str) {
None => OperatorAvailability::Ready,
},
move |world| {
let ran = world.resource_scope(|world, registry: Mut<EditorCommandRegistry>| {
let result = world.resource_scope(|world, registry: Mut<EditorCommandRegistry>| {
registry.run(world, &name_for_commit)
});
if ran {
Ok(())
} else {
Err(format!("Unknown editor command: {name_for_commit}"))
}
result.unwrap_or_else(|| Err(format!("Unknown editor command: {name_for_commit}")))
},
);
@@ -269,8 +267,9 @@ impl EditorCommand for TogglePlayCommand {
"Toggle Play / Edit"
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
toggle_play_mode(world);
Ok(OperatorAction::Commit)
}
}
@@ -290,8 +289,9 @@ impl EditorCommand for TogglePlayPausedCommand {
.then(|| "Enter Play mode before pausing the simulation".to_string())
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
toggle_play_paused(world);
Ok(OperatorAction::Commit)
}
}
@@ -311,12 +311,13 @@ impl EditorCommand for TogglePossessionCommand {
.then(|| "Enter Play mode before toggling possession".to_string())
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
let next = match *world.resource::<PlayPossession>() {
PlayPossession::Possessed => PlayPossession::Ejected,
PlayPossession::Ejected => PlayPossession::Possessed,
};
*world.resource_mut::<PlayPossession>() = next;
Ok(OperatorAction::Commit)
}
}
@@ -331,8 +332,9 @@ impl EditorCommand for ResetLightingCommand {
"Reset Scene Lighting"
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
reset_scene_lighting_to_project_defaults(world);
Ok(OperatorAction::Commit)
}
}
@@ -347,13 +349,23 @@ impl EditorCommand for GroupSelectionCommand {
"Group Selection"
}
fn execute(&self, world: &mut World) {
fn disabled_reason(&self, world: &World) -> Option<String> {
world
.resource::<UiState>()
.selected_entities
.as_slice()
.is_empty()
.then(|| "Select at least one actor to group".to_string())
}
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
let selected: Vec<Entity> = world
.resource::<UiState>()
.selected_entities
.iter()
.collect();
group_selection_with_history(world, &selected);
Ok(OperatorAction::Commit)
}
}
@@ -368,8 +380,9 @@ impl EditorCommand for FocusSelectionCommand {
"Focus Selection"
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
focus_editor_camera_on_selection(world);
Ok(OperatorAction::Commit)
}
}
@@ -384,8 +397,9 @@ impl EditorCommand for ResetSelectionTransformCommand {
"Reset Selection Transform"
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
reset_selected_transforms(world);
Ok(OperatorAction::Commit)
}
}
@@ -405,8 +419,9 @@ impl EditorCommand for DrawBrushCommand {
.then(|| "Enter Edit mode before drawing brushes".to_string())
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
start_draw_brush_tool(world);
Ok(OperatorAction::ContinuePreview)
}
}
@@ -425,8 +440,9 @@ impl EditorCommand for IntersectBrushesCommand {
(selected_brush_count(world) < 2).then(|| "Select at least two brushes".to_string())
}
fn execute(&self, world: &mut World) {
intersect_selected_brushes(world);
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
intersect_selected_brushes(world)?;
Ok(OperatorAction::ContinuePreview)
}
}
@@ -445,8 +461,9 @@ impl EditorCommand for MergeBrushesCommand {
(selected_brush_count(world) < 2).then(|| "Select at least two brushes".to_string())
}
fn execute(&self, world: &mut World) {
merge_selected_brushes(world);
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
merge_selected_brushes(world)?;
Ok(OperatorAction::ContinuePreview)
}
}
@@ -465,8 +482,9 @@ impl EditorCommand for SubtractBrushesCommand {
(selected_brush_count(world) < 2).then(|| "Select at least two brushes".to_string())
}
fn execute(&self, world: &mut World) {
subtract_selected_brushes(world);
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
subtract_selected_brushes(world)?;
Ok(OperatorAction::ContinuePreview)
}
}
@@ -481,8 +499,9 @@ impl EditorCommand for CreatePostProcessVolumeCommand {
"Create Post-process Volume"
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
crate::rendering_diagnostics::spawn_post_process_volume_at_camera(world);
Ok(OperatorAction::Commit)
}
}
@@ -497,9 +516,10 @@ impl EditorCommand for FocusActiveVolumesCommand {
"Focus Active Post-process Volumes"
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
crate::rendering_diagnostics::select_volumes_at_camera(world);
focus_editor_camera_on_selection(world);
Ok(OperatorAction::Commit)
}
}
@@ -514,8 +534,9 @@ impl EditorCommand for SelectVolumesAtCameraCommand {
"Select Volumes at Camera"
}
fn execute(&self, world: &mut World) {
fn execute(&self, world: &mut World) -> Result<OperatorAction, String> {
crate::rendering_diagnostics::select_volumes_at_camera(world);
Ok(OperatorAction::Commit)
}
}
@@ -671,6 +692,53 @@ fn command_palette_row(entry: &EditorCommandEntry) -> egui::WidgetText {
#[cfg(test)]
mod tests {
use super::*;
use crate::history::{apply_command_redo, apply_command_undo, EditorHistory};
use crate::operators::test_harness::OperatorInvariantHarness;
use crate::operators::{ActiveOperator, OperatorPhase};
use crate::scene_io::SceneIo;
use crate::selection::{SelectedEntity, ViewportClick};
use crate::viewport::brush_tool::BrushToolState;
use shared::{
ActorKind, AuthoringLightKind, BrushDesc, EditorVisibility, HierarchySiblingIndex,
LevelObject, LightDesc,
};
fn command_world() -> World {
let mut world = World::new();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
world.init_resource::<SelectedEntity>();
world.init_resource::<ViewportClick>();
world.init_resource::<BrushToolState>();
world.init_resource::<crate::viewport::brush_csg::BrushCsgPreview>();
world.insert_resource(State::new(EditorMode::Editing));
world.insert_resource(UiState::default_layout());
let mut registry = EditorCommandRegistry::default();
populate_builtin_commands(&mut registry);
world.insert_resource(registry);
world
}
fn hierarchy_projection(world: &mut World) -> Vec<(String, Option<String>, i32)> {
let mut query = world.query_filtered::<(Entity, &Name), With<LevelObject>>();
let mut result = query
.iter(world)
.map(|(entity, name)| {
let parent = world
.get::<ChildOf>(entity)
.and_then(|child| world.get::<Name>(child.parent()))
.map(|name| name.as_str().to_string());
let sibling_index = world
.get::<HierarchySiblingIndex>(entity)
.map(|index| index.0)
.unwrap_or_default();
(name.as_str().to_string(), parent, sibling_index)
})
.collect::<Vec<_>>();
result.sort();
result
}
#[test]
fn command_filter_matches_human_label_and_stable_id() {
@@ -697,4 +765,208 @@ mod tests {
assert_eq!(entries[0].name, "play.toggle");
assert_eq!(entries[1].name, "scene.reset_lighting");
}
#[test]
fn grouping_dispatch_is_one_history_transaction_and_round_trips() {
let mut world = command_world();
let first = world
.spawn((
Name::new("First"),
LevelObject,
ActorKind::Empty,
Transform::from_xyz(1.0, 0.0, 0.0),
GlobalTransform::default(),
HierarchySiblingIndex(0),
EditorVisibility::default(),
))
.id();
let second = world
.spawn((
Name::new("Second"),
LevelObject,
ActorKind::Empty,
Transform::from_xyz(2.0, 0.0, 0.0),
GlobalTransform::default(),
HierarchySiblingIndex(1),
EditorVisibility::default(),
))
.id();
let unselected = world
.spawn((
Name::new("Unselected"),
LevelObject,
ActorKind::Empty,
Transform::from_xyz(3.0, 0.0, 0.0),
GlobalTransform::default(),
HierarchySiblingIndex(2),
EditorVisibility::default(),
))
.id();
world
.resource_mut::<UiState>()
.selected_entities
.select_replace(first);
world
.resource_mut::<UiState>()
.selected_entities
.select_maybe_add(second, true);
let initial = hierarchy_projection(&mut world);
let harness = OperatorInvariantHarness::capture(&mut world);
dispatch_editor_command(&mut world, "selection.group");
harness.assert_committed(&mut world, 1, 1);
harness.assert_status(&world, "selection.group", OperatorPhase::Committed);
let committed = hierarchy_projection(&mut world);
assert!(committed
.iter()
.any(|(name, parent, _)| name == "First" && parent.as_deref() == Some("Group")));
assert!(committed
.iter()
.any(|(name, parent, _)| name == "Second" && parent.as_deref() == Some("Group")));
apply_command_undo(&mut world);
assert_eq!(hierarchy_projection(&mut world), initial);
apply_command_redo(&mut world);
assert_eq!(hierarchy_projection(&mut world), committed);
assert!(world.get::<ChildOf>(unselected).is_none());
apply_command_undo(&mut world);
assert_eq!(hierarchy_projection(&mut world), initial);
apply_command_redo(&mut world);
assert_eq!(hierarchy_projection(&mut world), committed);
assert!(world.get::<ChildOf>(unselected).is_none());
}
#[test]
fn reset_lighting_dispatch_is_one_undoable_group() {
let mut world = command_world();
let point = world
.spawn((
LevelObject,
ActorKind::Light,
LightDesc {
kind: AuthoringLightKind::Point,
..Default::default()
},
))
.id();
let directional = world
.spawn((
LevelObject,
ActorKind::Light,
LightDesc {
kind: AuthoringLightKind::Directional,
..Default::default()
},
))
.id();
let harness = OperatorInvariantHarness::capture(&mut world);
dispatch_editor_command(&mut world, "scene.reset_lighting");
harness.assert_committed(&mut world, 1, 0);
harness.assert_status(&world, "scene.reset_lighting", OperatorPhase::Committed);
assert!(world.get::<LightDesc>(point).is_none());
assert!(world.get::<LightDesc>(directional).is_none());
apply_command_undo(&mut world);
assert!(world.get::<LightDesc>(point).is_some());
assert!(world.get::<LightDesc>(directional).is_some());
apply_command_redo(&mut world);
assert!(world.get::<LightDesc>(point).is_none());
assert!(world.get::<LightDesc>(directional).is_none());
}
#[test]
fn modal_command_dispatch_retains_preview_ownership() {
let mut world = command_world();
let harness = OperatorInvariantHarness::capture(&mut world);
dispatch_editor_command(&mut world, "brush.draw");
harness.assert_status(&world, "brush.draw", OperatorPhase::Preview);
harness.assert_projection_unchanged(&mut world, 0, |world| {
world.resource::<EditorHistory>().undo_depth()
});
assert!(!world.resource::<SceneIo>().dirty);
assert!(world.resource::<BrushToolState>().active);
}
#[test]
fn failed_csg_dispatch_terminates_without_preview_or_history() {
let mut world = command_world();
let first = world
.spawn((
LevelObject,
BrushDesc::cuboid(Vec3::splat(1.0)),
Transform::IDENTITY,
))
.id();
let mut invalid = BrushDesc::cuboid(Vec3::splat(1.0));
invalid.faces.clear();
let second = world
.spawn((LevelObject, invalid, Transform::IDENTITY))
.id();
world
.resource_mut::<UiState>()
.selected_entities
.select_replace(first);
world
.resource_mut::<UiState>()
.selected_entities
.select_maybe_add(second, true);
let harness = OperatorInvariantHarness::capture(&mut world);
dispatch_editor_command(&mut world, "brush.intersect");
harness.assert_canceled(&mut world);
harness.assert_status(&world, "brush.intersect", OperatorPhase::Canceled);
assert!(!world.resource::<SceneIo>().dirty);
}
#[test]
fn csg_dispatch_blocks_read_only_selection_without_history() {
let mut world = command_world();
let first = world
.spawn((
LevelObject,
BrushDesc::cuboid(Vec3::splat(1.0)),
Transform::IDENTITY,
))
.id();
let second = world
.spawn((
LevelObject,
BrushDesc::cuboid(Vec3::splat(1.0)),
Transform::from_xyz(0.5, 0.0, 0.0),
))
.id();
let mut hierarchy = crate::ui::hierarchy_state::HierarchyPanelState::default();
hierarchy.locked.insert(second);
world.insert_resource(hierarchy);
world
.resource_mut::<UiState>()
.selected_entities
.select_replace(first);
world
.resource_mut::<UiState>()
.selected_entities
.select_maybe_add(second, true);
let harness = OperatorInvariantHarness::capture(&mut world);
dispatch_editor_command(&mut world, "brush.merge_convex");
harness.assert_blocked(&mut world);
harness.assert_status(&world, "brush.merge_convex", OperatorPhase::Blocked);
assert!(!world.resource::<SceneIo>().dirty);
}
#[test]
fn disabled_command_dispatch_is_a_blocked_no_op() {
let mut world = command_world();
let harness = OperatorInvariantHarness::capture(&mut world);
dispatch_editor_command(&mut world, "selection.group");
harness.assert_blocked(&mut world);
harness.assert_status(&world, "selection.group", OperatorPhase::Blocked);
}
}
+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();
+97
View File
@@ -59,6 +59,12 @@ pub enum OperatorAvailability {
Disabled(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OperatorAction {
Commit,
ContinuePreview,
}
pub trait EditorOperator {
fn id(&self) -> &str;
fn label(&self) -> &str;
@@ -221,6 +227,51 @@ pub fn run_immediate_operator(
run_operator(world, &mut operator)
}
/// Runs an editor command that may either complete immediately or hand ownership to a modal tool.
/// Modal starters retain the preview status installed by their production entry point.
pub fn run_operator_action(
world: &mut World,
id: &str,
label: &str,
availability: OperatorAvailability,
action: impl FnOnce(&mut World) -> Result<OperatorAction, String>,
) -> bool {
if let OperatorAvailability::Disabled(reason) = availability {
set_operator_status(
world,
OperatorStatus::new(id, label, OperatorPhase::Blocked, reason),
);
return false;
}
set_operator_status(
world,
OperatorStatus::new(id, label, OperatorPhase::Preview, "Starting"),
);
match action(world) {
Ok(OperatorAction::Commit) => {
set_operator_status(
world,
OperatorStatus::new(
id,
label,
OperatorPhase::Committed,
format!("Completed {label}"),
),
);
true
}
Ok(OperatorAction::ContinuePreview) => true,
Err(error) => {
set_operator_status(
world,
OperatorStatus::new(id, label, OperatorPhase::Canceled, error),
);
false
}
}
}
fn set_operator_status(world: &mut World, status: OperatorStatus) {
if let Some(mut active) = world.get_resource_mut::<ActiveOperator>() {
active.status = Some(status);
@@ -355,6 +406,52 @@ mod tests {
harness.assert_no_helpers::<PreviewHelper>(&mut world);
}
#[test]
fn preview_failure_runs_cancel_and_cleans_helpers() {
struct PreviewFailure(CounterOperator);
impl EditorOperator for PreviewFailure {
fn id(&self) -> &str {
self.0.id()
}
fn label(&self) -> &str {
self.0.label()
}
fn begin(&mut self, world: &mut World) -> Result<(), String> {
self.0.begin(world)
}
fn preview(&mut self, world: &mut World) -> Result<(), String> {
self.0.preview(world)?;
Err("preview failed".to_string())
}
fn commit(&mut self, _world: &mut World) -> Result<(), String> {
panic!("commit must not run after preview failure")
}
fn cancel(&mut self, world: &mut World) {
self.0.cancel(world);
}
}
let mut world = test_world();
let harness = OperatorInvariantHarness::capture(&mut world);
let mut operator = PreviewFailure(CounterOperator {
before: 0,
helper: None,
});
assert!(!run_operator(&mut world, &mut operator));
harness.assert_canceled(&mut world);
harness.assert_status(&world, "test.counter", OperatorPhase::Canceled);
harness.assert_no_helpers::<PreviewHelper>(&mut world);
assert_eq!(world.resource::<Counter>().0, 0);
}
#[test]
fn blocked_operator_never_begins_or_mutates_world() {
let mut world = test_world();
@@ -47,6 +47,12 @@ impl OperatorInvariantHarness {
world.resource::<SceneIo>().dirty,
"a committed authored mutation must mark the scene dirty"
);
} else {
assert_eq!(
world.resource::<SceneIo>().dirty,
self.dirty,
"a committed non-authoring operator must preserve the prior dirty state"
);
}
assert_eq!(
authored_count(world) as isize,
@@ -93,6 +99,39 @@ impl OperatorInvariantHarness {
);
}
pub(crate) fn assert_status(
&self,
world: &World,
expected_id: &str,
expected_phase: OperatorPhase,
) {
let status = world
.resource::<ActiveOperator>()
.status
.as_ref()
.expect("operator must retain an inspectable lifecycle status");
assert_eq!(status.id, expected_id, "operator reported the wrong id");
assert_eq!(
status.phase, expected_phase,
"operator reported the wrong phase"
);
}
pub(crate) fn assert_projection_unchanged<T>(
&self,
world: &mut World,
expected: T,
mut read: impl FnMut(&mut World) -> T,
) where
T: Debug + PartialEq,
{
assert_eq!(
read(world),
expected,
"operator changed authored state without a committed history transaction"
);
}
fn assert_phase(&self, world: &World, expected: OperatorPhase) {
let phase = world
.resource::<ActiveOperator>()
+13 -24
View File
@@ -11,7 +11,10 @@ use sim::Player;
use crate::assets::{snapshot_for_asset, EditorAsset, EditorAssetKind};
use crate::camera::EditorCamera;
use crate::gizmos::{EditorGizmoMode, EditorGizmoSpace};
use crate::history::{set_material_group_with_history, spawn_with_history, EditorEntitySnapshot};
use crate::history::{
set_light_group_with_history, set_material_group_with_history, spawn_with_history,
EditorEntitySnapshot,
};
use crate::play::default_player_spawn;
use crate::scene_io::SceneIo;
use crate::viewport::{recall_camera_bookmark, save_camera_bookmark, CameraBookmarks};
@@ -191,37 +194,23 @@ pub fn ensure_player_spawn_for_edit(world: &mut World) -> Entity {
/// Removes all authored [`LightDesc`] so project sun/ambient drive outdoor lighting.
pub fn reset_scene_lighting_to_project_defaults(world: &mut World) {
let mut query = world.query_filtered::<Entity, With<LevelObject>>();
let with_lights: Vec<Entity> = query
let mut query = world.query_filtered::<(Entity, &LightDesc), With<LevelObject>>();
let changes = query
.iter(world)
.filter(|entity| world.get::<LightDesc>(*entity).is_some())
.collect();
for entity in with_lights {
if let Ok(mut entity_mut) = world.get_entity_mut(entity) {
entity_mut.remove::<LightDesc>();
}
}
world
.resource_mut::<crate::scene_io::SceneIo>()
.mark_dirty();
.map(|(entity, _)| (entity, None))
.collect::<Vec<_>>();
set_light_group_with_history(world, changes, "Reset Scene Lighting");
}
/// Removes authored directional lights so [`ProjectSun`] drives outdoor lighting.
pub fn use_project_sun(world: &mut World) {
let mut query = world.query_filtered::<(Entity, &LightDesc), With<LevelObject>>();
let directionals: Vec<Entity> = query
let changes = query
.iter(world)
.filter(|(_, light)| matches!(light.kind, AuthoringLightKind::Directional))
.map(|(e, _)| e)
.collect();
for entity in directionals {
if let Ok(mut entity_mut) = world.get_entity_mut(entity) {
entity_mut.remove::<LightDesc>();
}
}
world
.resource_mut::<crate::scene_io::SceneIo>()
.mark_dirty();
.map(|(entity, _)| (entity, None))
.collect::<Vec<_>>();
set_light_group_with_history(world, changes, "Use Project Sun");
}
pub fn create_scene_sun_override_from_project_settings(world: &mut World) -> Entity {
+54 -30
View File
@@ -4,7 +4,7 @@ use bevy::math::Affine3A;
use bevy::prelude::*;
use shared::{
brush_math::{validate_brush, BrushDiagnosticSeverity},
BrushDesc, LevelObject,
BrushDesc,
};
use crate::history::apply_brush_csg_with_history;
@@ -64,24 +64,27 @@ pub fn selected_brush_count(world: &World) -> usize {
.resource::<UiState>()
.selected_entities
.iter()
.filter(|entity| world.get::<BrushDesc>(*entity).is_some())
.filter(|entity| {
world.get::<BrushDesc>(*entity).is_some()
&& crate::ui::selection_ops::is_mutable_level_object(world, *entity)
})
.count()
}
pub fn intersect_selected_brushes(world: &mut World) {
run_bounds_csg(world, BrushCsgOp::Intersect);
pub fn intersect_selected_brushes(world: &mut World) -> Result<(), String> {
run_bounds_csg(world, BrushCsgOp::Intersect)
}
pub fn merge_selected_brushes(world: &mut World) {
run_bounds_csg(world, BrushCsgOp::Merge);
pub fn merge_selected_brushes(world: &mut World) -> Result<(), String> {
run_bounds_csg(world, BrushCsgOp::Merge)
}
pub fn subtract_selected_brushes(world: &mut World) {
run_bounds_csg(world, BrushCsgOp::Subtract);
pub fn subtract_selected_brushes(world: &mut World) -> Result<(), String> {
run_bounds_csg(world, BrushCsgOp::Subtract)
}
#[derive(Resource, Default, Debug, Clone)]
struct BrushCsgPreview {
pub(crate) struct BrushCsgPreview {
pending: Option<PendingBrushCsg>,
}
@@ -99,18 +102,18 @@ enum BrushCsgOp {
Subtract,
}
fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
fn run_bounds_csg(world: &mut World, op: BrushCsgOp) -> Result<(), String> {
let selected: Vec<Entity> = world
.resource::<UiState>()
.selected_entities
.iter()
.filter(|entity| {
world.get::<LevelObject>(*entity).is_some() && world.get::<BrushDesc>(*entity).is_some()
world.get::<BrushDesc>(*entity).is_some()
&& crate::ui::selection_ops::is_mutable_level_object(world, *entity)
})
.collect();
if selected.len() < 2 {
set_status(world, "Select at least two brushes for CSG");
return;
return csg_failure(world, "Select at least two editable brushes for CSG");
}
for entity in &selected {
let Some(brush) = world.get::<BrushDesc>(*entity) else {
@@ -118,20 +121,18 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
};
let validation = validate_brush(brush);
if !validation.is_valid() {
set_status(
return csg_failure(
world,
format!(
"CSG failed: selected brush is invalid: {}",
first_brush_error(&validation)
),
);
return;
}
}
let Some(primary_bounds) = brush_world_bounds(world, selected[0]) else {
set_status(world, "Primary brush has no valid bounds");
return;
return csg_failure(world, "Primary brush has no valid bounds");
};
let result = match op {
@@ -147,8 +148,7 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
continue;
};
let Some(intersection) = result.intersection(bounds) else {
set_status(world, "Brush intersect failed: no overlap");
return;
return csg_failure(world, "Brush intersect failed: no overlap");
};
result = intersection;
}
@@ -156,16 +156,13 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
}
BrushCsgOp::Subtract => {
let Some(cutter_bounds) = brush_world_bounds(world, selected[1]) else {
set_status(world, "Subtract failed: cutter brush has no valid bounds");
return;
return csg_failure(world, "Subtract failed: cutter brush has no valid bounds");
};
let Some(overlap) = primary_bounds.intersection(cutter_bounds) else {
set_status(world, "Subtract failed: brushes do not overlap");
return;
return csg_failure(world, "Subtract failed: brushes do not overlap");
};
let Some(result) = largest_remaining_slab(primary_bounds, overlap) else {
set_status(world, "Subtract failed: result would be empty");
return;
return csg_failure(world, "Subtract failed: result would be empty");
};
result
}
@@ -175,14 +172,13 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
let new_brush = BrushDesc::cuboid(size);
let validation = validate_brush(&new_brush);
if !validation.is_valid() {
set_status(
return csg_failure(
world,
format!(
"CSG failed: result is invalid: {}",
first_brush_error(&validation)
),
);
return;
}
world.resource_mut::<BrushCsgPreview>().pending = Some(PendingBrushCsg {
@@ -197,6 +193,13 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
};
set_status(world, hint);
set_csg_operator_status(world, OperatorPhase::Preview, hint);
Ok(())
}
fn csg_failure(world: &mut World, message: impl Into<String>) -> Result<(), String> {
let message = message.into();
set_status(world, message.clone());
Err(message)
}
fn brush_csg_preview_input(world: &mut World) {
@@ -253,6 +256,22 @@ fn commit_csg_preview(world: &mut World, pending: PendingBrushCsg) {
);
return;
}
if pending
.selected
.iter()
.any(|entity| !crate::ui::selection_ops::is_mutable_level_object(world, *entity))
{
set_status(
world,
"Brush CSG commit blocked: selected content is no longer editable",
);
set_csg_operator_status(
world,
OperatorPhase::Blocked,
"Commit blocked: selected content is no longer editable",
);
return;
}
let new_brush = BrushDesc::cuboid(pending.result.size());
let to_delete: Vec<_> = if matches!(pending.op, BrushCsgOp::Merge | BrushCsgOp::Intersect) {
pending
@@ -265,7 +284,11 @@ fn commit_csg_preview(world: &mut World, pending: PendingBrushCsg) {
} else {
Vec::new()
};
apply_bounds_result(world, primary, pending.result, new_brush, &to_delete);
if !apply_bounds_result(world, primary, pending.result, new_brush, &to_delete) {
set_status(world, "Brush CSG commit made no changes");
set_csg_operator_status(world, OperatorPhase::Canceled, "No changes");
return;
}
let hint = match pending.op {
BrushCsgOp::Intersect => "Brush intersect committed",
BrushCsgOp::Merge => "Brush convex merge committed",
@@ -332,14 +355,14 @@ fn apply_bounds_result(
bounds: BrushBounds,
new_brush: BrushDesc,
delete_entities: &[Entity],
) {
) -> bool {
let center = bounds.center();
let old_transform = world.get::<Transform>(entity).copied().unwrap_or_default();
let mut new_transform = old_transform;
new_transform.translation = center;
new_transform.rotation = Quat::IDENTITY;
new_transform.scale = Vec3::ONE;
apply_brush_csg_with_history(world, entity, new_transform, new_brush, delete_entities);
apply_brush_csg_with_history(world, entity, new_transform, new_brush, delete_entities)
}
fn brush_world_bounds(world: &World, entity: Entity) -> Option<BrushBounds> {
@@ -426,6 +449,7 @@ mod tests {
use crate::history::{EditorCommand, EditorHistory};
use crate::operators::test_harness::{assert_undo_redo_round_trip, OperatorInvariantHarness};
use crate::selection::SelectedEntity;
use shared::LevelObject;
fn bounds(min: Vec3, max: Vec3) -> BrushBounds {
BrushBounds { min, max }
+51
View File
@@ -1308,4 +1308,55 @@ mod tests {
assert!(state.old_brush.is_none());
assert!(!state.changed);
}
#[test]
fn brush_element_release_uses_production_finalizer_and_round_trips() {
let mut app = App::new();
app.init_resource::<ActiveOperator>()
.init_resource::<EditorHistory>()
.init_resource::<SceneIo>()
.init_resource::<BrushEditMode>()
.init_resource::<BrushElementSelection>()
.init_resource::<BrushElementGizmoState>()
.add_systems(Update, apply_brush_element_gizmo);
let original = BrushDesc::cuboid(Vec3::ONE);
let preview = BrushDesc::cuboid(Vec3::new(1.5, 1.0, 1.0));
let brush_entity = app
.world_mut()
.spawn((LevelObject, preview.clone(), GlobalTransform::default()))
.id();
let helper = app
.world_mut()
.spawn((
BrushElementGizmo,
Transform::default(),
GizmoTarget::default(),
))
.id();
*app.world_mut().resource_mut::<BrushEditMode>() = BrushEditMode::Vertex;
*app.world_mut().resource_mut::<BrushElementSelection>() = BrushElementSelection {
brush: Some(brush_entity),
elements: vec![BrushElementKey::Vertex {
face: original.faces[0].id.clone(),
index: 0,
}],
};
*app.world_mut().resource_mut::<BrushElementGizmoState>() = BrushElementGizmoState {
entity: Some(helper),
brush: Some(brush_entity),
old_brush: Some(original.clone()),
old_gizmo: Some(Transform::default()),
last_gizmo: Some(Transform::from_translation(Vec3::X * 0.25)),
changed: true,
};
let harness = OperatorInvariantHarness::capture(app.world_mut());
app.update();
harness.assert_committed(app.world_mut(), 1, 0);
harness.assert_status(app.world(), "brush.edit_mode", OperatorPhase::Committed);
assert_undo_redo_round_trip(app.world_mut(), original, preview, |world| {
world.get::<BrushDesc>(brush_entity).unwrap().clone()
});
}
}
+91 -52
View File
@@ -779,31 +779,17 @@ fn update_material_drop_session(world: &mut World) {
.is_some_and(|buttons| buttons.just_pressed(MouseButton::Right));
if cancel_requested && (selection.is_some() || state.preview.is_some()) {
restore_material_drop_preview(world, state.preview.take());
cancel_material_drop_session(world, &mut state, true);
clear_drag_and_viewport_click(world);
state.feedback = None;
state.captures_viewport_input = false;
state.cancel_consumed = true;
set_drop_operator_status(
world,
OperatorPhase::Canceled,
"Surface assignment canceled",
);
world.insert_resource(state);
return;
}
let Some(selection) = selection else {
let had_preview = state.preview.is_some();
restore_material_drop_preview(world, state.preview.take());
state.feedback = None;
state.captures_viewport_input = false;
if had_preview {
set_drop_operator_status(
world,
OperatorPhase::Canceled,
"Surface assignment canceled",
);
if state.preview.is_some() {
cancel_material_drop_session(world, &mut state, false);
} else {
reset_material_drop_session(&mut state, false);
}
world.insert_resource(state);
return;
@@ -877,19 +863,44 @@ fn update_material_drop_session(world: &mut World) {
);
if primary_released {
let preview = state.preview.take();
if let Some(preview) = preview.as_ref() {
preview.snapshot.restore(world);
}
if let Some(preview) = preview {
commit_material_drop_preview(world, preview);
}
commit_material_drop_session(world, &mut state);
clear_drag_and_viewport_click(world);
state.captures_viewport_input = false;
}
world.insert_resource(state);
}
fn cancel_material_drop_session(
world: &mut World,
state: &mut MaterialDropState,
cancel_consumed: bool,
) {
restore_material_drop_preview(world, state.preview.take());
reset_material_drop_session(state, cancel_consumed);
set_drop_operator_status(
world,
OperatorPhase::Canceled,
"Surface assignment canceled",
);
}
fn commit_material_drop_session(world: &mut World, state: &mut MaterialDropState) -> bool {
let Some(preview) = state.preview.take() else {
reset_material_drop_session(state, false);
return false;
};
preview.snapshot.restore(world);
let committed = commit_material_drop_preview(world, preview);
reset_material_drop_session(state, false);
committed
}
fn reset_material_drop_session(state: &mut MaterialDropState, cancel_consumed: bool) {
state.resolved = None;
state.feedback = None;
state.captures_viewport_input = false;
state.cancel_consumed = cancel_consumed;
}
fn restore_material_drop_preview(world: &mut World, preview: Option<MaterialDropPreview>) {
if let Some(preview) = preview {
preview.snapshot.restore(world);
@@ -1029,7 +1040,8 @@ fn draw_material_drop_target(
#[cfg(test)]
mod tests {
use super::*;
use crate::history::{apply_command_undo, EditorHistory};
use crate::history::EditorHistory;
use crate::operators::test_harness::{assert_undo_redo_round_trip, OperatorInvariantHarness};
use shared::{ActorKind, RendererMaterialSet, RendererMaterialSlot, SharedTypesPlugin};
fn test_app() -> App {
@@ -1098,6 +1110,7 @@ mod tests {
hit_point: Vec3::ZERO,
hit_normal: Vec3::Y,
};
let harness = OperatorInvariantHarness::capture(world);
let preview = begin_material_drop_preview(
world,
AssetSelection::File("assets/materials/steel.ron".into()),
@@ -1123,18 +1136,25 @@ mod tests {
assert!(!world.resource::<SceneIo>().dirty);
assert_eq!(world.resource::<EditorHistory>().undo_depth(), 0);
preview.snapshot.restore(world);
assert!(world
.get::<StaticMeshRenderer>(entity)
.unwrap()
.materials
.slot(&slot_b)
.unwrap()
.material
.is_none());
assert!(commit_material_drop_preview(world, preview));
assert_eq!(world.resource::<EditorHistory>().undo_depth(), 1);
assert!(world.resource::<SceneIo>().dirty);
let mut state = MaterialDropState {
resolved: Some(target),
preview: Some(preview),
feedback: Some(MaterialDropFeedback {
valid: true,
action: "Assign material".into(),
target: "Crate / B".into(),
}),
captures_viewport_input: true,
cancel_consumed: false,
};
assert!(commit_material_drop_session(world, &mut state));
harness.assert_committed(world, 1, 0);
harness.assert_status(world, "assets.assign_surface", OperatorPhase::Committed);
assert!(state.preview.is_none());
assert!(state.resolved.is_none());
assert!(state.feedback.is_none());
assert!(!state.captures_viewport_input);
assert!(!state.cancel_consumed);
assert!(world
.get::<StaticMeshRenderer>(entity)
.unwrap()
@@ -1143,16 +1163,17 @@ mod tests {
.unwrap()
.material
.is_none());
apply_command_undo(world);
assert!(world
.get::<StaticMeshRenderer>(entity)
.unwrap()
.materials
.slot(&slot_b)
.unwrap()
.material
.is_none());
assert_undo_redo_round_trip(world, None, Some("steel".to_string()), |world| {
world
.get::<StaticMeshRenderer>(entity)
.unwrap()
.materials
.slot(&slot_b)
.unwrap()
.material
.as_ref()
.map(|material| material.0.asset_id.clone())
});
}
#[test]
@@ -1172,6 +1193,7 @@ mod tests {
hit_point: Vec3::ZERO,
hit_normal: Vec3::Y,
};
let harness = OperatorInvariantHarness::capture(world);
let preview = begin_material_drop_preview(
world,
AssetSelection::File("assets/materials/steel.ron".into()),
@@ -1196,9 +1218,26 @@ mod tests {
.material
.is_none());
preview.snapshot.restore(world);
let mut state = MaterialDropState {
resolved: Some(target),
preview: Some(preview),
feedback: Some(MaterialDropFeedback {
valid: true,
action: "Assign material".into(),
target: "Blockout / face".into(),
}),
captures_viewport_input: true,
cancel_consumed: false,
};
cancel_material_drop_session(world, &mut state, true);
harness.assert_canceled(world);
harness.assert_status(world, "assets.assign_surface", OperatorPhase::Canceled);
assert_eq!(world.get::<BrushDesc>(entity), Some(&brush));
assert!(!world.resource::<SceneIo>().dirty);
assert!(state.preview.is_none());
assert!(state.resolved.is_none());
assert!(state.feedback.is_none());
assert!(!state.captures_viewport_input);
assert!(state.cancel_consumed);
}
#[test]
+88 -18
View File
@@ -6,7 +6,7 @@ use std::time::Duration;
use avian3d::prelude::*;
use bevy::prelude::*;
use crate::history::set_transform_group_with_history;
use crate::history::{set_transform_group_with_history, EditorHistory};
use crate::operators::{ActiveOperator, OperatorPhase, OperatorStatus};
use crate::state::EditorMode;
use crate::ui::selection_ops::{entity_name, is_mutable_level_object};
@@ -256,6 +256,7 @@ fn finish_physics_placement(world: &mut World, commit: bool) {
}
}
let undo_depth = world.resource::<EditorHistory>().undo_depth();
if commit {
let changes = session.selected.iter().filter_map(|snapshot| {
final_transforms
@@ -265,6 +266,7 @@ fn finish_physics_placement(world: &mut World, commit: bool) {
});
set_transform_group_with_history(world, changes);
}
let committed = commit && world.resource::<EditorHistory>().undo_depth() > undo_depth;
state.phase = PhysicsPlacementPhase::Inactive;
state.request = PhysicsPlacementRequest::None;
@@ -272,7 +274,14 @@ fn finish_physics_placement(world: &mut World, commit: bool) {
state.simulated_steps = 0;
state.last_error = None;
world.insert_resource(state);
clear_operator_status(world);
let (phase, hint) = if committed {
(OperatorPhase::Committed, "Physics placement committed")
} else if commit {
(OperatorPhase::Canceled, "Physics placement made no changes")
} else {
(OperatorPhase::Canceled, "Physics placement canceled")
};
set_operator_status(world, phase, hint.to_string(), Vec::new());
}
fn tick_physics_placement(world: &mut World) {
@@ -437,22 +446,12 @@ fn set_operator_status(
}
}
fn clear_operator_status(world: &mut World) {
if let Some(mut operator) = world.get_resource_mut::<ActiveOperator>() {
if operator
.status
.as_ref()
.is_some_and(|status| status.id == "physics.placement")
{
operator.status = None;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::history::{apply_command_undo, EditorHistory};
use crate::history::EditorHistory;
use crate::operators::test_harness::{assert_undo_redo_round_trip, OperatorInvariantHarness};
use crate::scene_io::SceneIo;
use bevy::asset::{AssetApp, AssetPlugin};
use bevy::state::app::StatesPlugin;
use shared::{ColliderDesc, LevelObject, RigidBodyDesc};
@@ -469,7 +468,9 @@ mod tests {
.init_asset::<Mesh>()
.init_state::<EditorMode>()
.init_resource::<PhysicsPlacementState>()
.init_resource::<EditorHistory>();
.init_resource::<EditorHistory>()
.init_resource::<ActiveOperator>()
.init_resource::<SceneIo>();
app.finish();
app.cleanup();
app.update();
@@ -498,12 +499,16 @@ mod tests {
.world_mut()
.spawn((Name::new("No Physics"), LevelObject, Transform::default()))
.id();
let harness = OperatorInvariantHarness::capture(app.world_mut());
let error = begin_physics_placement(app.world_mut(), [entity]).unwrap_err();
assert!(error.contains("add and enable a Rigid Body"));
assert!(error.contains("add and enable a non-trigger Collider"));
assert!(!app.world().resource::<PhysicsPlacementState>().active());
harness.assert_blocked(app.world_mut());
harness.assert_status(app.world(), "physics.placement", OperatorPhase::Blocked);
harness.assert_no_helpers::<PhysicsPlacementPreview>(app.world_mut());
}
#[test]
@@ -531,6 +536,7 @@ mod tests {
))
.id();
app.update();
let harness = OperatorInvariantHarness::capture(app.world_mut());
begin_physics_placement(app.world_mut(), [selected]).unwrap();
assert_eq!(
@@ -581,6 +587,9 @@ mod tests {
Some(&LinearVelocity(Vec3::X * 3.0))
);
assert_eq!(app.world().resource::<EditorHistory>().undo_depth(), 0);
harness.assert_canceled(app.world_mut());
harness.assert_status(app.world(), "physics.placement", OperatorPhase::Canceled);
harness.assert_no_helpers::<PhysicsPlacementPreview>(app.world_mut());
}
#[test]
@@ -594,6 +603,7 @@ mod tests {
let start = Transform::from_xyz(0.0, 4.0, 0.0);
let prop = spawn_placeable(app.world_mut(), "Drop Prop", start);
app.update();
let harness = OperatorInvariantHarness::capture(app.world_mut());
begin_physics_placement(app.world_mut(), [prop]).unwrap();
for _ in 0..400 {
@@ -613,15 +623,75 @@ mod tests {
commit_physics_placement(app.world_mut());
assert_eq!(app.world().resource::<EditorHistory>().undo_depth(), 1);
harness.assert_committed(app.world_mut(), 1, 0);
harness.assert_status(app.world(), "physics.placement", OperatorPhase::Committed);
harness.assert_no_helpers::<PhysicsPlacementPreview>(app.world_mut());
assert_eq!(
app.world().resource::<EditorHistory>().status,
"Undo: Move Selection"
);
assert_eq!(app.world().get::<RigidBody>(prop), Some(&RigidBody::Static));
assert_eq!(*app.world().get::<Transform>(prop).unwrap(), settled);
assert_undo_redo_round_trip(app.world_mut(), start, settled, |world| {
*world.get::<Transform>(prop).unwrap()
});
}
apply_command_undo(app.world_mut());
#[test]
fn no_op_commit_terminates_without_dirtying_or_history() {
let mut app = app_with_physics();
let start = Transform::from_xyz(0.0, 4.0, 0.0);
let prop = spawn_placeable(app.world_mut(), "Static Prop", start);
app.update();
let harness = OperatorInvariantHarness::capture(app.world_mut());
begin_physics_placement(app.world_mut(), [prop]).unwrap();
commit_physics_placement(app.world_mut());
harness.assert_canceled(app.world_mut());
harness.assert_status(app.world(), "physics.placement", OperatorPhase::Canceled);
harness.assert_no_helpers::<PhysicsPlacementPreview>(app.world_mut());
assert_eq!(*app.world().get::<Transform>(prop).unwrap(), start);
assert_eq!(app.world().get::<RigidBody>(prop), Some(&RigidBody::Static));
}
#[test]
fn multi_select_commit_is_one_transaction_and_cleans_every_preview_helper() {
let mut app = app_with_physics();
let starts = [
Transform::from_xyz(-1.0, 4.0, 0.0),
Transform::from_xyz(1.0, 6.0, 0.0),
];
let first = spawn_placeable(app.world_mut(), "First", starts[0]);
let second = spawn_placeable(app.world_mut(), "Second", starts[1]);
app.update();
let harness = OperatorInvariantHarness::capture(app.world_mut());
begin_physics_placement(app.world_mut(), [first, second, first]).unwrap();
assert_eq!(
app.world()
.resource::<PhysicsPlacementState>()
.selected_count,
2,
"duplicate selections must collapse to one placement snapshot"
);
let committed = [
Transform::from_xyz(-1.0, 0.5, 0.0),
Transform::from_xyz(1.0, 0.5, 0.0),
];
app.world_mut().entity_mut(first).insert(committed[0]);
app.world_mut().entity_mut(second).insert(committed[1]);
commit_physics_placement(app.world_mut());
harness.assert_committed(app.world_mut(), 1, 0);
harness.assert_status(app.world(), "physics.placement", OperatorPhase::Committed);
harness.assert_no_helpers::<PhysicsPlacementPreview>(app.world_mut());
assert_undo_redo_round_trip(app.world_mut(), starts, committed, |world| {
[
*world.get::<Transform>(first).unwrap(),
*world.get::<Transform>(second).unwrap(),
]
});
}
}
+302 -38
View File
@@ -6,7 +6,7 @@ use shared::{TerrainDesc, AUTHORING_COMPONENT_TERRAIN, COMPONENT_TERRAIN_DESC};
use super::terrain_sculpt::{active_scene_ray, smooth_falloff, terrain_ray_hit, TerrainHit};
use crate::camera::EditorCamera;
use crate::history::reflected_component_transaction;
use crate::history::{reflected_component_transaction, EditorHistory};
use crate::operators::{ActiveOperator, OperatorPhase, OperatorStatus};
use crate::scene_io::SceneIo;
use crate::selection::{SelectedEntity, ViewportClick};
@@ -125,16 +125,30 @@ fn terrain_paint_input(
viewport_click.0 = None;
state.clamp_settings();
let Some(entity) = state.target.or(selected.0) else {
cancel_stroke(&mut state, &mut terrains);
state.stop();
stop_tool_with_rollback(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Blocked,
"Terrain paint stopped: target is unavailable",
"Target unavailable",
);
return Ok(());
};
if selected.0 != Some(entity) {
selected.0 = Some(entity);
}
if display.clean_game_view {
cancel_stroke(&mut state, &mut terrains);
state.stop();
stop_tool_with_rollback(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Canceled,
"Terrain paint stopped for clean game view",
"Clean game view enabled; weights restored",
);
return Ok(());
}
let ctx = contexts.ctx_mut()?;
@@ -142,12 +156,11 @@ fn terrain_paint_input(
keys.just_pressed(KeyCode::Escape) || buttons.just_pressed(MouseButton::Right);
if cancel_requested {
if state.is_stroking() {
cancel_stroke(&mut state, &mut terrains);
scene_io.status = "Terrain paint stroke canceled".into();
set_status(
cancel_active_stroke(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Canceled,
"Weights restored",
);
} else {
state.stop();
@@ -157,14 +170,29 @@ fn terrain_paint_input(
return Ok(());
}
let Ok((global, mut terrain)) = terrains.get_mut(entity) else {
state.stroke = None;
state.stop();
stop_tool_with_rollback(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Blocked,
"Terrain paint stopped: target no longer exists",
"Target no longer exists",
);
return Ok(());
};
let layer_count = terrain.material_layers.len();
if layer_count == 0 {
if let Some(stroke) = state.stroke.take() {
*terrain = stroke.original;
}
state.stop();
scene_io.status = "Add a terrain material layer before painting".into();
set_status(
&mut active_operator,
OperatorPhase::Blocked,
"Add a material layer before painting",
);
return Ok(());
}
state.active_layer = state.active_layer.min(layer_count - 1);
@@ -227,17 +255,8 @@ fn terrain_paint_input(
let final_terrain = terrain.clone();
let mode = state.mode;
commands.queue(move |world: &mut World| {
if let Err(error) = commit_paint_stroke(world, stroke, final_terrain) {
world.resource_mut::<SceneIo>().status =
format!("Terrain paint commit failed: {error}");
}
finish_paint_stroke(world, stroke, final_terrain, mode);
});
scene_io.status = format!("{} terrain material weights committed", mode.label());
set_status(
&mut active_operator,
OperatorPhase::Committed,
format!("{} weights; Ctrl+Z to undo", mode.label()),
);
}
}
Ok(())
@@ -248,10 +267,11 @@ fn commit_paint_stroke(
stroke: TerrainPaintStroke,
final_terrain: TerrainDesc,
) -> Result<(), String> {
let original = stroke.original;
if let Ok(mut actor) = world.get_entity_mut(stroke.entity) {
actor.insert(stroke.original);
actor.insert(original.clone());
}
reflected_component_transaction(
let result = reflected_component_transaction(
world,
stroke.entity,
"Paint Terrain Material",
@@ -261,7 +281,77 @@ fn commit_paint_stroke(
world.entity_mut(entity).insert(final_terrain);
Ok(())
},
)
);
if result.is_err() {
if let Ok(mut actor) = world.get_entity_mut(stroke.entity) {
actor.insert(original);
}
}
result
}
fn finish_paint_stroke(
world: &mut World,
stroke: TerrainPaintStroke,
final_terrain: TerrainDesc,
mode: TerrainPaintMode,
) {
let undo_depth = world.resource::<EditorHistory>().undo_depth();
match commit_paint_stroke(world, stroke, final_terrain) {
Ok(()) if world.resource::<EditorHistory>().undo_depth() > undo_depth => {
world.resource_mut::<SceneIo>().status =
format!("{} terrain material weights committed", mode.label());
set_status(
&mut world.resource_mut::<ActiveOperator>(),
OperatorPhase::Committed,
format!("{} weights; Ctrl+Z to undo", mode.label()),
);
}
Ok(()) => {
world.resource_mut::<SceneIo>().status =
format!("{} terrain stroke made no changes", mode.label());
set_status(
&mut world.resource_mut::<ActiveOperator>(),
OperatorPhase::Canceled,
format!("{} stroke made no changes", mode.label()),
);
}
Err(error) => {
world.resource_mut::<SceneIo>().status =
format!("Terrain paint commit failed: {error}");
set_status(
&mut world.resource_mut::<ActiveOperator>(),
OperatorPhase::Blocked,
format!("Commit failed: {error}"),
);
}
}
}
fn cancel_active_stroke(
state: &mut TerrainPaintState,
terrains: &mut Query<(&GlobalTransform, &mut TerrainDesc)>,
scene_io: &mut SceneIo,
active_operator: &mut ActiveOperator,
) {
cancel_stroke(state, terrains);
scene_io.status = "Terrain paint stroke canceled".into();
set_status(active_operator, OperatorPhase::Canceled, "Weights restored");
}
fn stop_tool_with_rollback(
state: &mut TerrainPaintState,
terrains: &mut Query<(&GlobalTransform, &mut TerrainDesc)>,
scene_io: &mut SceneIo,
active_operator: &mut ActiveOperator,
phase: OperatorPhase,
message: &str,
hint: &str,
) {
cancel_stroke(state, terrains);
state.stop();
scene_io.status = message.to_string();
set_status(active_operator, phase, hint);
}
fn cancel_stroke(
@@ -415,7 +505,8 @@ fn draw_terrain_paint_preview(state: Res<TerrainPaintState>, mut gizmos: Gizmos)
#[cfg(test)]
mod tests {
use super::*;
use crate::history::{apply_command_redo, apply_command_undo, EditorHistory};
use crate::history::EditorHistory;
use crate::operators::test_harness::{assert_undo_redo_round_trip, OperatorInvariantHarness};
use shared::{ActorKind, LevelObject};
#[test]
@@ -452,8 +543,10 @@ mod tests {
.register_type::<shared::TerrainMaterialLayer>()
.register_type::<shared::EditorAssetRef>();
let world = app.world_mut();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
world.init_resource::<TerrainPaintState>();
let mut original = TerrainDesc::flat(9);
original.material_layers = vec![Default::default(), Default::default()];
let entity = world
@@ -471,23 +564,121 @@ mod tests {
);
}
world.entity_mut(entity).insert(final_terrain.clone());
commit_paint_stroke(
let stroke = TerrainPaintStroke {
entity,
original: original.clone(),
last_local: Vec3::ZERO,
};
{
let mut state = world.resource_mut::<TerrainPaintState>();
state.start(entity, 2);
state.stroke = Some(stroke);
}
let stroke = world
.resource_mut::<TerrainPaintState>()
.stroke
.take()
.unwrap();
let harness = OperatorInvariantHarness::capture(world);
finish_paint_stroke(
world,
stroke,
final_terrain.clone(),
TerrainPaintMode::Paint,
);
harness.assert_committed(world, 1, 0);
harness.assert_status(world, "terrain.paint", OperatorPhase::Committed);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&final_terrain));
let state = world.resource::<TerrainPaintState>();
assert!(state.active);
assert_eq!(state.target, Some(entity));
assert!(!state.is_stroking());
assert_undo_redo_round_trip(world, original, final_terrain, |world| {
world.get::<TerrainDesc>(entity).unwrap().clone()
});
}
#[test]
fn no_op_paint_release_terminates_without_dirtying_or_history() {
let mut app = App::new();
app.register_type::<ActorKind>()
.register_type::<TerrainDesc>()
.register_type::<shared::TerrainMaterialLayer>()
.register_type::<shared::EditorAssetRef>();
let world = app.world_mut();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
let mut original = TerrainDesc::flat(5);
original.material_layers = vec![Default::default(), Default::default()];
original.ensure_material_weights();
let entity = world
.spawn((LevelObject, ActorKind::Terrain, original.clone()))
.id();
let harness = OperatorInvariantHarness::capture(world);
finish_paint_stroke(
world,
TerrainPaintStroke {
entity,
original: original.clone(),
last_local: Vec3::ZERO,
},
final_terrain.clone(),
)
.unwrap();
original.clone(),
TerrainPaintMode::Erase,
);
assert_eq!(world.resource::<EditorHistory>().undo_depth(), 1);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&final_terrain));
apply_command_undo(world);
harness.assert_canceled(world);
harness.assert_status(world, "terrain.paint", OperatorPhase::Canceled);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&original));
apply_command_redo(world);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&final_terrain));
}
#[test]
fn commit_failure_restores_preview_and_reports_blocked_without_history() {
let mut app = App::new();
let world = app.world_mut();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
let mut original = TerrainDesc::flat(5);
original.material_layers = vec![Default::default(), Default::default()];
let mut preview = original.clone();
apply_weight_dab(
&mut preview,
Vec3::ZERO,
3.0,
0.8,
1,
TerrainPaintMode::Paint,
);
let entity = world
.spawn((LevelObject, ActorKind::Terrain, preview.clone()))
.id();
let mut hierarchy = crate::ui::hierarchy_state::HierarchyPanelState::default();
hierarchy.locked.insert(entity);
world.insert_resource(hierarchy);
let harness = OperatorInvariantHarness::capture(world);
finish_paint_stroke(
world,
TerrainPaintStroke {
entity,
original: original.clone(),
last_local: Vec3::ZERO,
},
preview,
TerrainPaintMode::Paint,
);
harness.assert_blocked(world);
harness.assert_status(world, "terrain.paint", OperatorPhase::Blocked);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&original));
assert!(world
.resource::<SceneIo>()
.status
.starts_with("Terrain paint commit failed:"));
}
#[test]
@@ -495,11 +686,21 @@ mod tests {
fn cancel_once(
mut state: ResMut<TerrainPaintState>,
mut terrains: Query<(&GlobalTransform, &mut TerrainDesc)>,
mut scene_io: ResMut<SceneIo>,
mut active_operator: ResMut<ActiveOperator>,
) {
cancel_stroke(&mut state, &mut terrains);
cancel_active_stroke(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
);
}
let mut app = App::new();
app.init_resource::<ActiveOperator>()
.init_resource::<EditorHistory>()
.init_resource::<SceneIo>();
let mut original = TerrainDesc::flat(5);
original.material_layers = vec![Default::default(), Default::default()];
let mut preview = original.clone();
@@ -513,7 +714,7 @@ mod tests {
);
let entity = app
.world_mut()
.spawn((GlobalTransform::default(), preview))
.spawn((LevelObject, GlobalTransform::default(), preview))
.id();
let mut state = TerrainPaintState::default();
state.start(entity, 2);
@@ -523,9 +724,72 @@ mod tests {
last_local: Vec3::ZERO,
});
app.insert_resource(state).add_systems(Update, cancel_once);
let harness = OperatorInvariantHarness::capture(app.world_mut());
app.update();
harness.assert_canceled(app.world_mut());
harness.assert_status(app.world(), "terrain.paint", OperatorPhase::Canceled);
assert_eq!(app.world().get::<TerrainDesc>(entity), Some(&original));
assert!(!app.world().resource::<TerrainPaintState>().is_stroking());
let state = app.world().resource::<TerrainPaintState>();
assert!(state.active);
assert_eq!(state.target, Some(entity));
assert!(!state.is_stroking());
}
#[test]
fn clean_view_interruption_restores_weights_and_terminates_tool() {
fn interrupt_once(
mut state: ResMut<TerrainPaintState>,
mut terrains: Query<(&GlobalTransform, &mut TerrainDesc)>,
mut scene_io: ResMut<SceneIo>,
mut active_operator: ResMut<ActiveOperator>,
) {
stop_tool_with_rollback(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Canceled,
"Terrain paint stopped for clean game view",
"Clean game view enabled; weights restored",
);
}
let mut app = App::new();
app.init_resource::<ActiveOperator>()
.init_resource::<EditorHistory>()
.init_resource::<SceneIo>();
let mut original = TerrainDesc::flat(5);
original.material_layers = vec![Default::default(), Default::default()];
let mut preview = original.clone();
apply_weight_dab(
&mut preview,
Vec3::ZERO,
3.0,
0.8,
1,
TerrainPaintMode::Paint,
);
let entity = app
.world_mut()
.spawn((LevelObject, GlobalTransform::default(), preview))
.id();
let mut state = TerrainPaintState::default();
state.start(entity, 2);
state.stroke = Some(TerrainPaintStroke {
entity,
original: original.clone(),
last_local: Vec3::ZERO,
});
app.insert_resource(state)
.add_systems(Update, interrupt_once);
let harness = OperatorInvariantHarness::capture(app.world_mut());
app.update();
harness.assert_canceled(app.world_mut());
harness.assert_status(app.world(), "terrain.paint", OperatorPhase::Canceled);
assert_eq!(app.world().get::<TerrainDesc>(entity), Some(&original));
assert!(!app.world().resource::<TerrainPaintState>().active);
}
}
+305 -41
View File
@@ -5,7 +5,7 @@ use bevy_egui::EguiContexts;
use shared::{TerrainDesc, AUTHORING_COMPONENT_TERRAIN, COMPONENT_TERRAIN_DESC};
use crate::camera::EditorCamera;
use crate::history::reflected_component_transaction;
use crate::history::{reflected_component_transaction, EditorHistory};
use crate::operators::{ActiveOperator, OperatorPhase, OperatorStatus};
use crate::scene_io::SceneIo;
use crate::selection::{SelectedEntity, ViewportClick};
@@ -143,8 +143,15 @@ fn terrain_sculpt_input(
state.clamp_settings();
let Some(entity) = state.target.or(selected.0) else {
cancel_stroke(&mut state, &mut terrains);
state.stop();
stop_tool_with_rollback(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Blocked,
"Terrain sculpt stopped: target is unavailable",
"Target unavailable",
);
return Ok(());
};
if selected.0 != Some(entity) {
@@ -152,8 +159,15 @@ fn terrain_sculpt_input(
}
if display.clean_game_view {
cancel_stroke(&mut state, &mut terrains);
state.stop();
stop_tool_with_rollback(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Canceled,
"Terrain sculpt stopped for clean game view",
"Clean game view enabled; terrain restored",
);
return Ok(());
}
@@ -163,12 +177,11 @@ fn terrain_sculpt_input(
keys.just_pressed(KeyCode::Escape) || buttons.just_pressed(MouseButton::Right);
if cancel_requested {
if state.is_stroking() {
cancel_stroke(&mut state, &mut terrains);
scene_io.status = "Terrain sculpt stroke canceled".to_string();
set_status(
cancel_active_stroke(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Canceled,
"Stroke canceled; terrain restored",
);
} else {
state.stop();
@@ -179,8 +192,15 @@ fn terrain_sculpt_input(
}
let Ok((global, mut terrain)) = terrains.get_mut(entity) else {
state.stroke = None;
state.stop();
stop_tool_with_rollback(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Blocked,
"Terrain sculpt stopped: target no longer exists",
"Target no longer exists",
);
return Ok(());
};
@@ -253,18 +273,8 @@ fn terrain_sculpt_input(
let label = sculpt_history_label(state.mode);
let mode_label = state.mode.label();
commands.queue(move |world: &mut World| {
let result = commit_terrain_stroke(world, stroke, final_terrain, label);
if let Err(error) = result {
world.resource_mut::<SceneIo>().status =
format!("Terrain sculpt commit failed: {error}");
}
finish_terrain_stroke(world, stroke, final_terrain, label, mode_label);
});
scene_io.status = format!("{mode_label} terrain stroke committed");
set_status(
&mut active_operator,
OperatorPhase::Committed,
format!("{mode_label} stroke; Ctrl+Z to undo"),
);
}
}
Ok(())
@@ -276,10 +286,11 @@ fn commit_terrain_stroke(
final_terrain: TerrainDesc,
label: &'static str,
) -> Result<(), String> {
let original = stroke.original;
if let Ok(mut actor) = world.get_entity_mut(stroke.entity) {
actor.insert(stroke.original);
actor.insert(original.clone());
}
reflected_component_transaction(
let result = reflected_component_transaction(
world,
stroke.entity,
label,
@@ -289,7 +300,82 @@ fn commit_terrain_stroke(
world.entity_mut(entity).insert(final_terrain);
Ok(())
},
)
);
if result.is_err() {
if let Ok(mut actor) = world.get_entity_mut(stroke.entity) {
actor.insert(original);
}
}
result
}
fn finish_terrain_stroke(
world: &mut World,
stroke: TerrainStroke,
final_terrain: TerrainDesc,
label: &'static str,
mode_label: &'static str,
) {
let undo_depth = world.resource::<EditorHistory>().undo_depth();
match commit_terrain_stroke(world, stroke, final_terrain, label) {
Ok(()) if world.resource::<EditorHistory>().undo_depth() > undo_depth => {
world.resource_mut::<SceneIo>().status =
format!("{mode_label} terrain stroke committed");
set_status(
&mut world.resource_mut::<ActiveOperator>(),
OperatorPhase::Committed,
format!("{mode_label} stroke; Ctrl+Z to undo"),
);
}
Ok(()) => {
world.resource_mut::<SceneIo>().status =
format!("{mode_label} terrain stroke made no changes");
set_status(
&mut world.resource_mut::<ActiveOperator>(),
OperatorPhase::Canceled,
format!("{mode_label} stroke made no changes"),
);
}
Err(error) => {
world.resource_mut::<SceneIo>().status =
format!("Terrain sculpt commit failed: {error}");
set_status(
&mut world.resource_mut::<ActiveOperator>(),
OperatorPhase::Blocked,
format!("Commit failed: {error}"),
);
}
}
}
fn cancel_active_stroke(
state: &mut TerrainSculptState,
terrains: &mut Query<(&GlobalTransform, &mut TerrainDesc)>,
scene_io: &mut SceneIo,
active_operator: &mut ActiveOperator,
) {
cancel_stroke(state, terrains);
scene_io.status = "Terrain sculpt stroke canceled".to_string();
set_status(
active_operator,
OperatorPhase::Canceled,
"Stroke canceled; terrain restored",
);
}
fn stop_tool_with_rollback(
state: &mut TerrainSculptState,
terrains: &mut Query<(&GlobalTransform, &mut TerrainDesc)>,
scene_io: &mut SceneIo,
active_operator: &mut ActiveOperator,
phase: OperatorPhase,
message: &str,
hint: &str,
) {
cancel_stroke(state, terrains);
state.stop();
scene_io.status = message.to_string();
set_status(active_operator, phase, hint);
}
fn cancel_stroke(
@@ -561,7 +647,8 @@ fn draw_terrain_sculpt_preview(state: Res<TerrainSculptState>, mut gizmos: Gizmo
#[cfg(test)]
mod tests {
use super::*;
use crate::history::{apply_command_redo, apply_command_undo, EditorHistory};
use crate::history::EditorHistory;
use crate::operators::test_harness::{assert_undo_redo_round_trip, OperatorInvariantHarness};
use shared::{ActorKind, LevelObject};
fn sample(terrain: &TerrainDesc, x: u32, z: u32) -> f32 {
@@ -657,8 +744,10 @@ mod tests {
app.register_type::<ActorKind>()
.register_type::<TerrainDesc>();
let world = app.world_mut();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
world.init_resource::<TerrainSculptState>();
let original = TerrainDesc::flat(9);
let entity = world
.spawn((LevelObject, ActorKind::Terrain, original.clone()))
@@ -676,26 +765,126 @@ mod tests {
);
}
world.entity_mut(entity).insert(final_terrain.clone());
commit_terrain_stroke(
let stroke = TerrainStroke {
entity,
original: original.clone(),
last_local: Vec3::ZERO,
flatten_height: 0.0,
seed: 7,
};
{
let mut state = world.resource_mut::<TerrainSculptState>();
state.start(entity);
state.stroke = Some(stroke);
}
let stroke = world
.resource_mut::<TerrainSculptState>()
.stroke
.take()
.unwrap();
let harness = OperatorInvariantHarness::capture(world);
finish_terrain_stroke(
world,
stroke,
final_terrain.clone(),
"Raise Terrain",
"Raise",
);
harness.assert_committed(world, 1, 0);
harness.assert_status(world, "terrain.sculpt", OperatorPhase::Committed);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&final_terrain));
let state = world.resource::<TerrainSculptState>();
assert!(state.active);
assert_eq!(state.target, Some(entity));
assert!(!state.is_stroking());
assert_undo_redo_round_trip(world, original, final_terrain, |world| {
world.get::<TerrainDesc>(entity).unwrap().clone()
});
}
#[test]
fn no_op_stroke_terminates_without_dirtying_or_history() {
let mut app = App::new();
app.register_type::<ActorKind>()
.register_type::<TerrainDesc>();
let world = app.world_mut();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
let original = TerrainDesc::flat(5);
let entity = world
.spawn((LevelObject, ActorKind::Terrain, original.clone()))
.id();
let harness = OperatorInvariantHarness::capture(world);
finish_terrain_stroke(
world,
TerrainStroke {
entity,
original: original.clone(),
last_local: Vec3::ZERO,
flatten_height: 0.0,
seed: 7,
seed: 1,
},
final_terrain.clone(),
"Raise Terrain",
)
.unwrap();
original.clone(),
"Smooth Terrain",
"Smooth",
);
assert_eq!(world.resource::<EditorHistory>().undo_depth(), 1);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&final_terrain));
apply_command_undo(world);
harness.assert_canceled(world);
harness.assert_status(world, "terrain.sculpt", OperatorPhase::Canceled);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&original));
apply_command_redo(world);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&final_terrain));
}
#[test]
fn commit_failure_restores_preview_and_reports_blocked_without_history() {
let mut app = App::new();
let world = app.world_mut();
world.init_resource::<ActiveOperator>();
world.init_resource::<EditorHistory>();
world.init_resource::<SceneIo>();
let original = TerrainDesc::flat(5);
let mut preview = original.clone();
apply_dab(
&mut preview,
Vec3::ZERO,
3.0,
0.8,
TerrainSculptMode::Raise,
0.0,
11,
);
let entity = world
.spawn((LevelObject, ActorKind::Terrain, preview.clone()))
.id();
let mut hierarchy = crate::ui::hierarchy_state::HierarchyPanelState::default();
hierarchy.locked.insert(entity);
world.insert_resource(hierarchy);
let harness = OperatorInvariantHarness::capture(world);
finish_terrain_stroke(
world,
TerrainStroke {
entity,
original: original.clone(),
last_local: Vec3::ZERO,
flatten_height: 0.0,
seed: 11,
},
preview,
"Raise Terrain",
"Raise",
);
harness.assert_blocked(world);
harness.assert_status(world, "terrain.sculpt", OperatorPhase::Blocked);
assert_eq!(world.get::<TerrainDesc>(entity), Some(&original));
assert!(world
.resource::<SceneIo>()
.status
.starts_with("Terrain sculpt commit failed:"));
}
#[test]
@@ -703,11 +892,21 @@ mod tests {
fn cancel_once(
mut state: ResMut<TerrainSculptState>,
mut terrains: Query<(&GlobalTransform, &mut TerrainDesc)>,
mut scene_io: ResMut<SceneIo>,
mut active_operator: ResMut<ActiveOperator>,
) {
cancel_stroke(&mut state, &mut terrains);
cancel_active_stroke(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
);
}
let mut app = App::new();
app.init_resource::<ActiveOperator>()
.init_resource::<EditorHistory>()
.init_resource::<SceneIo>();
let original = TerrainDesc::flat(5);
let mut preview = original.clone();
apply_dab(
@@ -721,7 +920,7 @@ mod tests {
);
let entity = app
.world_mut()
.spawn((GlobalTransform::default(), preview))
.spawn((LevelObject, GlobalTransform::default(), preview))
.id();
let mut state = TerrainSculptState::default();
state.start(entity);
@@ -733,9 +932,74 @@ mod tests {
seed: 11,
});
app.insert_resource(state).add_systems(Update, cancel_once);
let harness = OperatorInvariantHarness::capture(app.world_mut());
app.update();
harness.assert_canceled(app.world_mut());
harness.assert_status(app.world(), "terrain.sculpt", OperatorPhase::Canceled);
assert_eq!(app.world().get::<TerrainDesc>(entity), Some(&original));
assert!(!app.world().resource::<TerrainSculptState>().is_stroking());
let state = app.world().resource::<TerrainSculptState>();
assert!(state.active);
assert_eq!(state.target, Some(entity));
assert!(!state.is_stroking());
}
#[test]
fn clean_view_interruption_restores_stroke_and_terminates_tool() {
fn interrupt_once(
mut state: ResMut<TerrainSculptState>,
mut terrains: Query<(&GlobalTransform, &mut TerrainDesc)>,
mut scene_io: ResMut<SceneIo>,
mut active_operator: ResMut<ActiveOperator>,
) {
stop_tool_with_rollback(
&mut state,
&mut terrains,
&mut scene_io,
&mut active_operator,
OperatorPhase::Canceled,
"Terrain sculpt stopped for clean game view",
"Clean game view enabled; terrain restored",
);
}
let mut app = App::new();
app.init_resource::<ActiveOperator>()
.init_resource::<EditorHistory>()
.init_resource::<SceneIo>();
let original = TerrainDesc::flat(5);
let mut preview = original.clone();
apply_dab(
&mut preview,
Vec3::ZERO,
3.0,
0.8,
TerrainSculptMode::Raise,
0.0,
11,
);
let entity = app
.world_mut()
.spawn((LevelObject, GlobalTransform::default(), preview))
.id();
let mut state = TerrainSculptState::default();
state.start(entity);
state.stroke = Some(TerrainStroke {
entity,
original: original.clone(),
last_local: Vec3::ZERO,
flatten_height: 0.0,
seed: 11,
});
app.insert_resource(state)
.add_systems(Update, interrupt_once);
let harness = OperatorInvariantHarness::capture(app.world_mut());
app.update();
harness.assert_canceled(app.world_mut());
harness.assert_status(app.world(), "terrain.sculpt", OperatorPhase::Canceled);
assert_eq!(app.world().get::<TerrainDesc>(entity), Some(&original));
assert!(!app.world().resource::<TerrainSculptState>().active);
}
}