feat(iced): implement shape cycling for overlapping vector shapes

This commit is contained in:
2026-07-16 11:21:32 +03:00
parent 634592e26d
commit 6196a80ed4
+56 -3
View File
@@ -346,6 +346,9 @@ pub struct IcedDocument {
pub marching_ants_edges: Option<Arc<Vec<(u32, u32, u32, u32)>>>,
/// Index of the currently selected vector shape (None = no selection).
pub selected_vector_shape: Option<usize>,
/// Index into the list of shapes at the current click position for cycling
/// through overlapping vector shapes. Resets to 0 when clicking a new position.
pub vector_cycle_index: usize,
/// Which vector edit handle is active (hovered or being dragged).
pub vector_edit_handle: hcie_engine_api::VectorEditHandle,
}
@@ -1017,6 +1020,7 @@ impl HcieIcedApp {
selection_edge_cache: Default::default(),
marching_ants_edges: None,
selected_vector_shape: None,
vector_cycle_index: 0,
vector_edit_handle: hcie_engine_api::VectorEditHandle::None,
};
@@ -3075,6 +3079,7 @@ impl HcieIcedApp {
selection_edge_cache: Default::default(),
marching_ants_edges: None,
selected_vector_shape: None,
vector_cycle_index: 0,
vector_edit_handle: hcie_engine_api::VectorEditHandle::None,
});
self.active_doc = self.documents.len() - 1;
@@ -3483,13 +3488,45 @@ impl HcieIcedApp {
}
}
// Hit-test vector shapes under cursor; select the topmost one.
if let Some(idx) = self.documents[self.active_doc].engine.find_vector_shape_at(x, y) {
// Find ALL shapes at the click position for cycling support.
// Collect indices of shapes whose bounding box contains (x, y),
// iterating forward so index 0 is the bottom-most shape.
if let Some(shapes) = self.documents[self.active_doc].engine.active_vector_shapes() {
let hits: Vec<usize> = shapes.iter().enumerate()
.filter(|(_, s)| {
let (l, t, r, b) = s.normalized_bounds();
x >= l && x <= r && y >= t && y <= b
})
.map(|(i, _)| i)
.collect();
if !hits.is_empty() {
// Save snapshot before editing (for undo).
let snapshot = self.documents[self.active_doc].engine.active_vector_shapes();
self.vector_shapes_snapshot = snapshot;
let doc = &mut self.documents[self.active_doc];
doc.selected_vector_shape = Some(idx);
let currently_selected = doc.selected_vector_shape;
// If clicking on a position where a shape is already selected,
// cycle through overlapping shapes.
if let Some(cur_idx) = currently_selected {
if hits.contains(&cur_idx) {
// The current selection is among the hits — cycle to the next.
let next_pos = (doc.vector_cycle_index + 1) % hits.len();
doc.vector_cycle_index = next_pos;
doc.selected_vector_shape = Some(hits[next_pos]);
} else {
// Clicked on a different region — select topmost and reset cycle.
doc.vector_cycle_index = 0;
doc.selected_vector_shape = Some(*hits.last().unwrap());
}
} else {
// No shape was selected — pick the topmost (last in hits) and reset cycle.
doc.vector_cycle_index = 0;
doc.selected_vector_shape = Some(*hits.last().unwrap());
}
doc.vector_draw = None; // clear any preview
doc.vector_edit_handle = VEH::Move;
self.vector_drag_last = Some((x, y));
@@ -3498,6 +3535,16 @@ impl HcieIcedApp {
// No shape under cursor — deselect.
let doc = &mut self.documents[self.active_doc];
doc.selected_vector_shape = None;
doc.vector_cycle_index = 0;
doc.vector_edit_handle = VEH::None;
self.vector_shapes_snapshot = None;
self.vector_drag_last = None;
}
} else {
// No vector shapes on this layer — deselect.
let doc = &mut self.documents[self.active_doc];
doc.selected_vector_shape = None;
doc.vector_cycle_index = 0;
doc.vector_edit_handle = VEH::None;
self.vector_shapes_snapshot = None;
self.vector_drag_last = None;
@@ -3555,6 +3602,7 @@ impl HcieIcedApp {
let layer_id = self.documents[self.active_doc].engine.active_layer_id();
self.documents[self.active_doc].engine.delete_vector_shape(layer_id, idx);
self.documents[self.active_doc].selected_vector_shape = None;
self.documents[self.active_doc].vector_cycle_index = 0;
self.vector_shapes_snapshot = None;
self.documents[self.active_doc].engine.mark_composite_dirty();
self.refresh_composite_if_needed();
@@ -3566,10 +3614,12 @@ impl HcieIcedApp {
Message::VectorShapeSelect(idx) => {
self.documents[self.active_doc].selected_vector_shape = idx;
if idx.is_some() {
self.documents[self.active_doc].vector_cycle_index = 0;
let snapshot = self.documents[self.active_doc].engine.active_vector_shapes();
self.vector_shapes_snapshot = snapshot;
crate::shape_sync::sync_tool_from_shape(self);
} else {
self.documents[self.active_doc].vector_cycle_index = 0;
self.vector_shapes_snapshot = None;
}
}
@@ -3578,6 +3628,7 @@ impl HcieIcedApp {
// Commit edits: clear snapshot and deselect.
let doc = &mut self.documents[self.active_doc];
doc.selected_vector_shape = None;
doc.vector_cycle_index = 0;
doc.vector_edit_handle = hcie_engine_api::VectorEditHandle::None;
self.vector_shapes_snapshot = None;
self.vector_drag_last = None;
@@ -3591,6 +3642,7 @@ impl HcieIcedApp {
*shapes = snapshot;
}
doc.selected_vector_shape = None;
doc.vector_cycle_index = 0;
doc.vector_edit_handle = hcie_engine_api::VectorEditHandle::None;
self.vector_drag_last = None;
doc.engine.mark_composite_dirty();
@@ -4545,6 +4597,7 @@ impl HcieIcedApp {
selection_edge_cache: Default::default(),
marching_ants_edges: None,
selected_vector_shape: None,
vector_cycle_index: 0,
vector_edit_handle: hcie_engine_api::VectorEditHandle::None,
});
}