This commit is contained in:
1 parent
9a67a25a15
commit
1ab3886028
24 files changed
+1836
-311
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user