This commit is contained in:
1 parent
d9e52d753b
commit
8abe47047d
2 files changed
+51
-6
No files matched your search
@@ -2,7 +2,10 @@
|
||||
|
||||
use bevy::math::Affine3A;
|
||||
use bevy::prelude::*;
|
||||
use shared::{BrushDesc, LevelObject};
|
||||
use shared::{
|
||||
brush_math::{validate_brush, BrushDiagnosticSeverity},
|
||||
BrushDesc, LevelObject,
|
||||
};
|
||||
|
||||
use crate::history::{
|
||||
delete_entities_with_history, set_brush_with_history, set_transform_with_history,
|
||||
@@ -86,6 +89,22 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
|
||||
set_status(world, "Select at least two brushes for CSG");
|
||||
return;
|
||||
}
|
||||
for entity in &selected {
|
||||
let Some(brush) = world.get::<BrushDesc>(*entity) else {
|
||||
continue;
|
||||
};
|
||||
let validation = validate_brush(brush);
|
||||
if !validation.is_valid() {
|
||||
set_status(
|
||||
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");
|
||||
@@ -129,7 +148,21 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
|
||||
}
|
||||
};
|
||||
|
||||
apply_bounds_result(world, selected[0], result);
|
||||
let size = result.size();
|
||||
let new_brush = BrushDesc::cuboid(size);
|
||||
let validation = validate_brush(&new_brush);
|
||||
if !validation.is_valid() {
|
||||
set_status(
|
||||
world,
|
||||
format!(
|
||||
"CSG failed: result is invalid: {}",
|
||||
first_brush_error(&validation)
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
apply_bounds_result(world, selected[0], result, new_brush);
|
||||
if matches!(op, BrushCsgOp::Merge | BrushCsgOp::Intersect) {
|
||||
delete_entities_with_history(world, &selected[1..]);
|
||||
}
|
||||
@@ -143,10 +176,22 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
|
||||
);
|
||||
}
|
||||
|
||||
fn apply_bounds_result(world: &mut World, entity: Entity, bounds: BrushBounds) {
|
||||
fn first_brush_error(report: &shared::brush_math::BrushValidationReport) -> &str {
|
||||
report
|
||||
.diagnostics
|
||||
.iter()
|
||||
.find(|diagnostic| diagnostic.severity == BrushDiagnosticSeverity::Error)
|
||||
.map(|diagnostic| diagnostic.message.as_str())
|
||||
.unwrap_or("Brush geometry is invalid.")
|
||||
}
|
||||
|
||||
fn apply_bounds_result(
|
||||
world: &mut World,
|
||||
entity: Entity,
|
||||
bounds: BrushBounds,
|
||||
new_brush: BrushDesc,
|
||||
) {
|
||||
let center = bounds.center();
|
||||
let size = bounds.size();
|
||||
let new_brush = BrushDesc::cuboid(size);
|
||||
let old_transform = world.get::<Transform>(entity).copied().unwrap_or_default();
|
||||
let mut new_transform = old_transform;
|
||||
new_transform.translation = center;
|
||||
|
||||
Reference in new issue
Block a user