This commit is contained in:
1 parent
b2305932d9
commit
4b6617f7f2
4 files changed
+138
-7
No files matched your search
@@ -13,6 +13,18 @@ use crate::history::{
|
||||
use crate::scene_io::SceneIo;
|
||||
use crate::ui::UiState;
|
||||
|
||||
pub struct BrushCsgPlugin;
|
||||
|
||||
impl Plugin for BrushCsgPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_resource::<BrushCsgPreview>().add_systems(
|
||||
Update,
|
||||
(brush_csg_preview_input, draw_brush_csg_preview)
|
||||
.run_if(crate::state::scene_tools_active),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
struct BrushBounds {
|
||||
min: Vec3,
|
||||
@@ -69,7 +81,19 @@ pub fn subtract_selected_brushes(world: &mut World) {
|
||||
run_bounds_csg(world, BrushCsgOp::Subtract);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Resource, Default, Debug, Clone)]
|
||||
struct BrushCsgPreview {
|
||||
pending: Option<PendingBrushCsg>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct PendingBrushCsg {
|
||||
op: BrushCsgOp,
|
||||
selected: Vec<Entity>,
|
||||
result: BrushBounds,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum BrushCsgOp {
|
||||
Intersect,
|
||||
Merge,
|
||||
@@ -162,13 +186,75 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
|
||||
return;
|
||||
}
|
||||
|
||||
apply_bounds_result(world, selected[0], result, new_brush);
|
||||
if matches!(op, BrushCsgOp::Merge | BrushCsgOp::Intersect) {
|
||||
delete_entities_with_history(world, &selected[1..]);
|
||||
}
|
||||
world.resource_mut::<BrushCsgPreview>().pending = Some(PendingBrushCsg {
|
||||
op,
|
||||
selected,
|
||||
result,
|
||||
});
|
||||
set_status(
|
||||
world,
|
||||
match op {
|
||||
BrushCsgOp::Intersect => "Brush intersect preview: Enter commits, Esc cancels",
|
||||
BrushCsgOp::Merge => "Brush convex merge preview: Enter commits, Esc cancels",
|
||||
BrushCsgOp::Subtract => "Brush subtract preview: Enter commits, Esc cancels",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn brush_csg_preview_input(world: &mut World) {
|
||||
let enter = world
|
||||
.get_resource::<ButtonInput<KeyCode>>()
|
||||
.is_some_and(|keys| keys.just_pressed(KeyCode::Enter));
|
||||
let escape = world
|
||||
.get_resource::<ButtonInput<KeyCode>>()
|
||||
.is_some_and(|keys| keys.just_pressed(KeyCode::Escape));
|
||||
if !enter && !escape {
|
||||
return;
|
||||
}
|
||||
if escape {
|
||||
if world
|
||||
.resource_mut::<BrushCsgPreview>()
|
||||
.pending
|
||||
.take()
|
||||
.is_some()
|
||||
{
|
||||
set_status(world, "Brush CSG preview canceled");
|
||||
}
|
||||
return;
|
||||
}
|
||||
let Some(pending) = world.resource_mut::<BrushCsgPreview>().pending.take() else {
|
||||
return;
|
||||
};
|
||||
commit_csg_preview(world, pending);
|
||||
}
|
||||
|
||||
fn commit_csg_preview(world: &mut World, pending: PendingBrushCsg) {
|
||||
let Some(primary) = pending.selected.first().copied() else {
|
||||
set_status(world, "Brush CSG commit failed: missing primary brush");
|
||||
return;
|
||||
};
|
||||
if world.get::<BrushDesc>(primary).is_none() {
|
||||
set_status(
|
||||
world,
|
||||
"Brush CSG commit failed: primary brush no longer exists",
|
||||
);
|
||||
return;
|
||||
}
|
||||
let new_brush = BrushDesc::cuboid(pending.result.size());
|
||||
apply_bounds_result(world, primary, pending.result, new_brush);
|
||||
if matches!(pending.op, BrushCsgOp::Merge | BrushCsgOp::Intersect) {
|
||||
let to_delete: Vec<_> = pending
|
||||
.selected
|
||||
.iter()
|
||||
.skip(1)
|
||||
.copied()
|
||||
.filter(|entity| world.get::<BrushDesc>(*entity).is_some())
|
||||
.collect();
|
||||
delete_entities_with_history(world, &to_delete);
|
||||
}
|
||||
set_status(
|
||||
world,
|
||||
match pending.op {
|
||||
BrushCsgOp::Intersect => "Brush intersect committed",
|
||||
BrushCsgOp::Merge => "Brush convex merge committed",
|
||||
BrushCsgOp::Subtract => "Brush subtract committed",
|
||||
@@ -176,6 +262,48 @@ fn run_bounds_csg(world: &mut World, op: BrushCsgOp) {
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_brush_csg_preview(preview: Res<BrushCsgPreview>, mut gizmos: Gizmos) {
|
||||
let Some(pending) = preview.pending.as_ref() else {
|
||||
return;
|
||||
};
|
||||
draw_bounds_box(
|
||||
&mut gizmos,
|
||||
pending.result,
|
||||
Color::srgba(0.25, 0.85, 1.0, 0.95),
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_bounds_box(gizmos: &mut Gizmos, bounds: BrushBounds, color: Color) {
|
||||
let min = bounds.min;
|
||||
let max = bounds.max;
|
||||
let corners = [
|
||||
Vec3::new(min.x, min.y, min.z),
|
||||
Vec3::new(max.x, min.y, min.z),
|
||||
Vec3::new(max.x, max.y, min.z),
|
||||
Vec3::new(min.x, max.y, min.z),
|
||||
Vec3::new(min.x, min.y, max.z),
|
||||
Vec3::new(max.x, min.y, max.z),
|
||||
Vec3::new(max.x, max.y, max.z),
|
||||
Vec3::new(min.x, max.y, max.z),
|
||||
];
|
||||
for (a, b) in [
|
||||
(0, 1),
|
||||
(1, 2),
|
||||
(2, 3),
|
||||
(3, 0),
|
||||
(4, 5),
|
||||
(5, 6),
|
||||
(6, 7),
|
||||
(7, 4),
|
||||
(0, 4),
|
||||
(1, 5),
|
||||
(2, 6),
|
||||
(3, 7),
|
||||
] {
|
||||
gizmos.line(corners[a], corners[b], color);
|
||||
}
|
||||
}
|
||||
|
||||
fn first_brush_error(report: &shared::brush_math::BrushValidationReport) -> &str {
|
||||
report
|
||||
.diagnostics
|
||||
|
||||
Reference in new issue
Block a user