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::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 }
|
||||
|
||||
Reference in new issue
Block a user