Upgrade to Bevy 0.19 and harden editor workflows
CI / Format, lint, test, build (push) Has been cancelled

This commit is contained in:
Rbanh committed 2026-07-09 23:43:47 -04:00
1 parent 3f85d25cc7
commit b5e561904c
204 files changed
+6972 -29907

No files matched your search

+252 -131
View File
@@ -9,12 +9,14 @@ use shared::{
use crate::camera::EditorCamera;
use crate::history::{push_command, set_brush_transform_with_history, EditorCommand};
use crate::infra::EditorOnly;
use crate::operators::{ActiveOperator, OperatorPhase, OperatorStatus};
use crate::scene_io::SceneIo;
use crate::selection::ViewportClick;
use crate::state::scene_tools_active;
use crate::ui::{viewport_keyboard_shortcuts_active, UiState};
use crate::viewport::{scene_view_ray, ViewportDisplayMode};
use transform_gizmo_bevy::prelude::GizmoTarget;
#[derive(Resource, Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum BrushEditMode {
@@ -98,27 +100,33 @@ impl Plugin for BrushEditPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<BrushEditMode>()
.init_resource::<BrushElementSelection>()
.init_resource::<BrushElementDrag>()
.init_resource::<BrushElementGizmoState>()
.add_systems(
Update,
(
brush_edit_hotkeys,
brush_element_pick,
brush_element_drag,
sync_brush_element_gizmo,
brush_clip_commit,
draw_brush_edit_overlays,
)
.chain()
.run_if(scene_tools_active),
);
)
.add_systems(Last, apply_brush_element_gizmo.run_if(scene_tools_active));
}
}
#[derive(Component, Debug, Clone, Copy)]
pub struct BrushElementGizmo;
#[derive(Resource, Default, Debug, Clone)]
struct BrushElementDrag {
struct BrushElementGizmoState {
entity: Option<Entity>,
brush: Option<Entity>,
old: Option<BrushDesc>,
last_floor: Option<Vec3>,
old_brush: Option<BrushDesc>,
old_gizmo: Option<Transform>,
last_gizmo: Option<Transform>,
changed: bool,
}
@@ -139,7 +147,9 @@ fn brush_edit_hotkeys(
return Ok(());
}
let ctx = contexts.ctx_mut()?;
if !viewport_keyboard_shortcuts_active(&ui_state, ctx, &buttons) || ctx.wants_keyboard_input() {
if !viewport_keyboard_shortcuts_active(&ui_state, ctx, &buttons)
|| ctx.egui_wants_keyboard_input()
{
return Ok(());
}
if keys.just_pressed(KeyCode::Escape) && mode.is_element_mode() {
@@ -233,121 +243,272 @@ fn brush_element_pick(
}
}
#[allow(clippy::too_many_arguments)]
fn brush_element_drag(
fn sync_brush_element_gizmo(
mut commands: Commands,
mode: Res<BrushEditMode>,
selection: Res<BrushElementSelection>,
mut drag: ResMut<BrushElementDrag>,
mut active_operator: ResMut<ActiveOperator>,
buttons: Res<ButtonInput<MouseButton>>,
ui_state: Res<UiState>,
cameras: Query<(&Camera, &GlobalTransform), With<EditorCamera>>,
mut brushes: Query<(&mut BrushDesc, &GlobalTransform)>,
mut state: ResMut<BrushElementGizmoState>,
brushes: Query<(&BrushDesc, &GlobalTransform)>,
mut gizmos: Query<&mut Transform, With<BrushElementGizmo>>,
) {
if !mode.is_element_mode() || matches!(*mode, BrushEditMode::Clip) {
clear_drag(&mut drag);
clear_brush_element_gizmo(&mut commands, &mut state);
return;
}
let Some(brush_entity) = selection.brush else {
clear_drag(&mut drag);
clear_brush_element_gizmo(&mut commands, &mut state);
return;
};
if selection.elements.is_empty() {
clear_drag(&mut drag);
clear_brush_element_gizmo(&mut commands, &mut state);
return;
}
let Some(pointer_pos) = ui_state.viewport_pointer_pos else {
let Ok((brush, brush_transform)) = brushes.get(brush_entity) else {
clear_brush_element_gizmo(&mut commands, &mut state);
return;
};
let Some(current_floor) = pointer_floor_position(&cameras, pointer_pos, ui_state.viewport_rect)
let Some(pivot) = selected_element_world_pivot(brush, brush_transform, &selection.elements)
else {
clear_brush_element_gizmo(&mut commands, &mut state);
return;
};
if buttons.just_pressed(MouseButton::Left) && drag.brush.is_none() {
if let Ok((brush, _)) = brushes.get(brush_entity) {
drag.brush = Some(brush_entity);
drag.old = Some(brush.clone());
drag.last_floor = Some(current_floor);
drag.changed = false;
}
let rotation = brush_transform.to_scale_rotation_translation().1;
let desired = Transform::from_translation(pivot).with_rotation(rotation);
let needs_new_entity = state.brush != Some(brush_entity)
|| state
.entity
.is_none_or(|entity| gizmos.get(entity).is_err());
if needs_new_entity {
clear_brush_element_gizmo(&mut commands, &mut state);
let entity = commands
.spawn((
BrushElementGizmo,
EditorOnly,
GizmoTarget::default(),
desired,
GlobalTransform::default(),
Visibility::Hidden,
InheritedVisibility::default(),
))
.id();
state.entity = Some(entity);
state.brush = Some(brush_entity);
state.old_brush = None;
state.old_gizmo = Some(desired);
state.last_gizmo = Some(desired);
state.changed = false;
return;
}
if buttons.pressed(MouseButton::Left) && drag.brush == Some(brush_entity) {
let Some(last_floor) = drag.last_floor else {
drag.last_floor = Some(current_floor);
let Some(entity) = state.entity else {
return;
};
let Ok(mut transform) = gizmos.get_mut(entity) else {
clear_brush_element_gizmo(&mut commands, &mut state);
return;
};
if state.old_brush.is_none() {
let current = *transform;
if !gizmo_transform_is_active(&state, current) {
*transform = desired;
state.old_gizmo = Some(desired);
state.last_gizmo = Some(desired);
}
}
}
#[allow(clippy::too_many_arguments)]
fn apply_brush_element_gizmo(
mut commands: Commands,
mode: Res<BrushEditMode>,
selection: Res<BrushElementSelection>,
mut state: ResMut<BrushElementGizmoState>,
mut active_operator: ResMut<ActiveOperator>,
mut brushes: Query<(&mut BrushDesc, &GlobalTransform)>,
gizmos: Query<(&Transform, &GizmoTarget), With<BrushElementGizmo>>,
) {
if !mode.is_element_mode() || matches!(*mode, BrushEditMode::Clip) {
clear_brush_element_gizmo(&mut commands, &mut state);
return;
}
let (Some(gizmo_entity), Some(brush_entity)) = (state.entity, selection.brush) else {
return;
};
if selection.elements.is_empty() || state.brush != Some(brush_entity) {
clear_brush_element_gizmo(&mut commands, &mut state);
return;
}
let Ok((gizmo_transform, target)) = gizmos.get(gizmo_entity) else {
clear_brush_element_gizmo(&mut commands, &mut state);
return;
};
if target.is_active() {
let Ok((mut brush, brush_transform)) = brushes.get_mut(brush_entity) else {
clear_brush_element_gizmo(&mut commands, &mut state);
return;
};
let world_delta = current_floor - last_floor;
if world_delta.length_squared() <= 0.000001 {
if state.old_brush.is_none() {
state.old_brush = Some(brush.clone());
state.old_gizmo = Some(*gizmo_transform);
state.last_gizmo = Some(*gizmo_transform);
state.changed = false;
return;
}
if let Ok((mut brush, transform)) = brushes.get_mut(brush_entity) {
let local_delta = transform.affine().inverse().transform_vector3(world_delta);
if move_selected_elements(&mut brush, &selection.elements, local_delta) {
drag.changed = true;
drag.last_floor = Some(current_floor);
let Some(last_gizmo) = state.last_gizmo else {
state.last_gizmo = Some(*gizmo_transform);
return;
};
if !transforms_nearly_equal(last_gizmo, *gizmo_transform)
&& transform_selected_elements_by_gizmo(
&mut brush,
&selection.elements,
brush_transform,
last_gizmo,
*gizmo_transform,
)
{
state.changed = true;
state.last_gizmo = Some(*gizmo_transform);
set_brush_edit_status(
&mut active_operator,
OperatorPhase::Preview,
format!(
"Gizmo editing {} brush element(s)",
selection.elements.len()
),
);
}
return;
}
if let (Some(old), Some(_)) = (state.old_brush.take(), state.old_gizmo.take()) {
if state.changed {
let Ok((mut brush, _)) = brushes.get_mut(brush_entity) else {
clear_brush_element_gizmo(&mut commands, &mut state);
return;
};
let new = brush.clone();
let validation = validate_brush(&new);
if validation.is_valid() {
commands.queue(move |world: &mut World| {
push_command(
world,
EditorCommand::SetBrush {
entity: brush_entity,
old: Some(old),
new,
},
);
});
set_brush_edit_status(
&mut active_operator,
OperatorPhase::Preview,
format!("Dragging {} element(s)", selection.elements.len()),
OperatorPhase::Committed,
"Committed brush element gizmo edit",
);
} else {
*brush = old;
let message = validation
.diagnostics
.iter()
.find(|diagnostic| diagnostic.severity == BrushDiagnosticSeverity::Error)
.map(|diagnostic| diagnostic.message.as_str())
.unwrap_or("Brush edit produced invalid geometry");
set_brush_edit_status(
&mut active_operator,
OperatorPhase::Blocked,
format!("Rejected brush edit: {message}"),
);
}
}
}
state.last_gizmo = Some(*gizmo_transform);
state.changed = false;
}
if buttons.just_released(MouseButton::Left) {
if let (Some(brush), Some(old)) = (drag.brush, drag.old.take()) {
if drag.changed {
if let Ok((mut brush_desc, _)) = brushes.get_mut(brush) {
let new = brush_desc.clone();
let validation = validate_brush(&new);
if validation.is_valid() {
commands.queue(move |world: &mut World| {
push_command(
world,
EditorCommand::SetBrush {
entity: brush,
old: Some(old),
new,
},
);
});
set_brush_edit_status(
&mut active_operator,
OperatorPhase::Committed,
"Committed brush element edit",
);
} else {
*brush_desc = old;
let message = validation
.diagnostics
.iter()
.find(|diagnostic| {
diagnostic.severity == BrushDiagnosticSeverity::Error
})
.map(|diagnostic| diagnostic.message.as_str())
.unwrap_or("Brush edit produced invalid geometry");
set_brush_edit_status(
&mut active_operator,
OperatorPhase::Blocked,
format!("Rejected brush edit: {message}"),
);
}
fn clear_brush_element_gizmo(commands: &mut Commands, state: &mut BrushElementGizmoState) {
if let Some(entity) = state.entity.take() {
if let Ok(mut entity_mut) = commands.get_entity(entity) {
entity_mut.despawn();
}
}
state.brush = None;
state.old_brush = None;
state.old_gizmo = None;
state.last_gizmo = None;
state.changed = false;
}
fn selected_element_world_pivot(
brush: &BrushDesc,
transform: &GlobalTransform,
selected: &[BrushElementKey],
) -> Option<Vec3> {
let anchors = selected_anchor_positions(brush, selected);
if anchors.is_empty() {
return None;
}
let affine = transform.affine();
Some(
anchors
.iter()
.map(|anchor| affine.transform_point3(*anchor))
.sum::<Vec3>()
/ anchors.len() as f32,
)
}
fn gizmo_transform_is_active(state: &BrushElementGizmoState, transform: Transform) -> bool {
state
.last_gizmo
.is_some_and(|last| !transforms_nearly_equal(last, transform))
|| state.old_brush.is_some()
}
fn transform_selected_elements_by_gizmo(
brush: &mut BrushDesc,
selected: &[BrushElementKey],
brush_transform: &GlobalTransform,
old_gizmo: Transform,
new_gizmo: Transform,
) -> bool {
let anchors = selected_anchor_positions(brush, selected);
if anchors.is_empty() {
return false;
}
let old_world_from_gizmo = old_gizmo.compute_affine();
let new_world_from_gizmo = new_gizmo.compute_affine();
let world_delta = new_world_from_gizmo * old_world_from_gizmo.inverse();
let brush_from_world = brush_transform.affine().inverse();
let world_from_brush = brush_transform.affine();
let mut changed = false;
for face in &mut brush.faces {
for vertex in &mut face.vertices {
if anchors
.iter()
.any(|anchor| vertex.distance_squared(*anchor) <= 0.0001)
{
let world = world_from_brush.transform_point3(*vertex);
let transformed = world_delta.transform_point3(world);
let local = brush_from_world.transform_point3(transformed);
if vertex.distance_squared(local) > 0.000001 {
*vertex = local;
changed = true;
}
}
}
clear_drag(&mut drag);
}
if changed {
recompute_face_planes(brush);
}
changed
}
fn clear_drag(drag: &mut BrushElementDrag) {
drag.brush = None;
drag.old = None;
drag.last_floor = None;
drag.changed = false;
fn transforms_nearly_equal(a: Transform, b: Transform) -> bool {
a.translation.distance_squared(b.translation) <= 0.000001
&& a.rotation.dot(b.rotation).abs() >= 0.99999
&& a.scale.distance_squared(b.scale) <= 0.000001
}
fn brush_clip_commit(
@@ -677,50 +838,6 @@ fn pick_element(
}
}
fn pointer_floor_position(
cameras: &Query<(&Camera, &GlobalTransform), With<EditorCamera>>,
pointer_pos: egui::Pos2,
scene_rect: egui::Rect,
) -> Option<Vec3> {
let ray = viewport_ray(cameras, pointer_pos, scene_rect)?;
let direction = ray.direction.as_vec3();
if direction.y.abs() < 0.0001 {
return Some(ray.origin);
}
let t = -ray.origin.y / direction.y;
Some(ray.origin + direction * t.max(0.0))
}
fn move_selected_elements(
brush: &mut BrushDesc,
selected: &[BrushElementKey],
delta: Vec3,
) -> bool {
if delta.length_squared() <= 0.000001 {
return false;
}
let anchors = selected_anchor_positions(brush, selected);
if anchors.is_empty() {
return false;
}
let mut changed = false;
for face in &mut brush.faces {
for vertex in &mut face.vertices {
if anchors
.iter()
.any(|anchor| vertex.distance_squared(*anchor) <= 0.0001)
{
*vertex += delta;
changed = true;
}
}
}
if changed {
recompute_face_planes(brush);
}
changed
}
fn selected_anchor_positions(brush: &BrushDesc, selected: &[BrushElementKey]) -> Vec<Vec3> {
let mut anchors = Vec::new();
for element in selected {
@@ -986,10 +1103,12 @@ mod tests {
.count();
let face = brush.faces[0].id.clone();
let changed = move_selected_elements(
let changed = transform_selected_elements_by_gizmo(
&mut brush,
&[BrushElementKey::Vertex { face, index: 0 }],
Vec3::new(0.5, 0.0, 0.0),
&GlobalTransform::default(),
Transform::default(),
Transform::from_translation(Vec3::new(0.5, 0.0, 0.0)),
);
let after = brush
@@ -1006,10 +1125,12 @@ mod tests {
fn collapsed_face_move_produces_invalid_brush() {
let mut brush = BrushDesc::default();
let face = brush.faces[0].id.clone();
assert!(move_selected_elements(
assert!(transform_selected_elements_by_gizmo(
&mut brush,
&[BrushElementKey::Face { face }],
Vec3::new(-1.0, 0.0, 0.0),
&GlobalTransform::default(),
Transform::default(),
Transform::from_translation(Vec3::new(-1.0, 0.0, 0.0)),
));
assert!(!validate_brush(&brush).is_valid());
}