feat(grip): support multi-grip editing

Refs #606
This commit is contained in:
Hakan Seven 2026-07-31 22:05:30 +03:00
commit a66940a3f0
13 changed files with 368 additions and 170 deletions

View file

@ -20,12 +20,42 @@ pub struct GripEdit {
pub handle: Handle,
/// Index into the entity's grip list.
pub grip_id: usize,
/// `true` → midpoint / translate grip; `false` → endpoint / absolute grip.
pub is_translate: bool,
/// World-space position of the grip when the drag started (ortho/polar base).
pub origin_world: DVec3,
/// Last world-space cursor position (needed for incremental delta on translate drags).
pub last_world: DVec3,
/// Every hot grip moved by this edit. A normal grip edit contains one target.
pub targets: Vec<GripTarget>,
}
#[derive(Clone, Debug)]
pub struct GripTarget {
pub handle: Handle,
pub grip_id: usize,
pub is_translate: bool,
pub last_world: DVec3,
}
impl GripEdit {
pub fn single(
handle: Handle,
grip_id: usize,
is_translate: bool,
world: DVec3,
) -> Self {
Self {
handle,
grip_id,
origin_world: world,
last_world: world,
targets: vec![GripTarget {
handle,
grip_id,
is_translate,
last_world: world,
}],
}
}
}
// ── Screen-space helpers ───────────────────────────────────────────────────
@ -83,11 +113,11 @@ pub fn find_hit_grip_paper(
half_w: f32,
half_h: f32,
bounds: Rectangle,
) -> Option<(usize, bool, DVec3)> {
) -> Option<(usize, usize, bool, DVec3)> {
let mut best_dist = GRIP_THRESHOLD_PX;
let mut best: Option<(usize, bool, DVec3)> = None;
let mut best: Option<(usize, usize, bool, DVec3)> = None;
for g in grips {
for (index, g) in grips.iter().enumerate() {
let screen = Point::new(
(g.world.x as f32 - tx + half_w) / (2.0 * half_w) * bounds.width,
(ty + half_h - g.world.y as f32) / (2.0 * half_h) * bounds.height,
@ -97,7 +127,7 @@ pub fn find_hit_grip_paper(
let d = (dx * dx + dy * dy).sqrt();
if d < best_dist {
best_dist = d;
best = Some((g.id, g.is_midpoint, g.world));
best = Some((index, g.id, g.is_midpoint, g.world));
}
}
best
@ -110,11 +140,11 @@ pub fn find_hit_grip(
grips: &[GripDef],
camera: &crate::scene::view::camera::Camera,
bounds: Rectangle,
) -> Option<(usize, bool, DVec3)> {
) -> Option<(usize, usize, bool, DVec3)> {
let mut best_dist = GRIP_THRESHOLD_PX;
let mut best: Option<(usize, bool, DVec3)> = None;
let mut best: Option<(usize, usize, bool, DVec3)> = None;
for g in grips {
for (index, g) in grips.iter().enumerate() {
let Some(screen) = camera.project(g.world, bounds) else {
continue;
};
@ -124,7 +154,7 @@ pub fn find_hit_grip(
let d = (dx * dx + dy * dy).sqrt();
if d < best_dist {
best_dist = d;
best = Some((g.id, g.is_midpoint, g.world));
best = Some((index, g.id, g.is_midpoint, g.world));
}
}
best
@ -176,11 +206,11 @@ pub fn find_hit_grip_rte(
view_rot: Mat4,
eye: DVec3,
bounds: Rectangle,
) -> Option<(usize, bool, DVec3)> {
) -> Option<(usize, usize, bool, DVec3)> {
let mut best_dist = GRIP_THRESHOLD_PX;
let mut best: Option<(usize, bool, DVec3)> = None;
let mut best: Option<(usize, usize, bool, DVec3)> = None;
for g in grips {
for (index, g) in grips.iter().enumerate() {
let Some(screen) = project_rte(g.world, view_rot, eye, bounds) else {
continue;
};
@ -189,7 +219,7 @@ pub fn find_hit_grip_rte(
let d = (dx * dx + dy * dy).sqrt();
if d < best_dist {
best_dist = d;
best = Some((g.id, g.is_midpoint, g.world));
best = Some((index, g.id, g.is_midpoint, g.world));
}
}
best

View file

@ -14,16 +14,20 @@ impl Scene {
self.preview_wires = wires;
}
/// Publish the current cached hatch as a one-entity live fill overlay.
/// The edited hatch is hidden from the resident set during a grip drag;
/// this keeps its pattern visible without rebuilding the full hatch batch.
pub fn set_preview_hatch(&mut self, handle: Handle) {
/// Publish all edited hatches as one live fill overlay.
pub fn set_preview_hatches(&mut self, handles: &[Handle]) {
let mut models = Vec::new();
for &handle in handles {
self.append_preview_hatch(handle, &mut models);
}
self.preview_hatches = std::sync::Arc::new(models);
}
fn append_preview_hatch(&self, handle: Handle, models: &mut Vec<HatchModel>) {
let Some(mut model) = self.hatches.get(&handle).cloned() else {
self.preview_hatches = std::sync::Arc::new(Vec::new());
return;
};
let Some(entity) = self.document.get_entity(handle) else {
self.preview_hatches = std::sync::Arc::new(Vec::new());
return;
};
@ -41,7 +45,6 @@ impl Scene {
.get(&handle.value())
.map_or(0.0, |depth| depth[0]);
let mut models = Vec::with_capacity(2);
if let EntityType::Hatch(hatch) = entity {
if let Some(background) = crate::entities::hatch::background_color(hatch) {
let mut backdrop = model.clone();
@ -63,7 +66,6 @@ impl Scene {
}
}
models.push(model);
self.preview_hatches = std::sync::Arc::new(models);
}
pub fn set_preview_text(&mut self, verts: Vec<crate::scene::pipeline::text_gpu::TextVertex>) {