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
+65 -12
View File
@@ -346,6 +346,9 @@ pub struct IcedDocument {
pub marching_ants_edges: Option<Arc<Vec<(u32, u32, u32, u32)>>>, pub marching_ants_edges: Option<Arc<Vec<(u32, u32, u32, u32)>>>,
/// Index of the currently selected vector shape (None = no selection). /// Index of the currently selected vector shape (None = no selection).
pub selected_vector_shape: Option<usize>, 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). /// Which vector edit handle is active (hovered or being dragged).
pub vector_edit_handle: hcie_engine_api::VectorEditHandle, pub vector_edit_handle: hcie_engine_api::VectorEditHandle,
} }
@@ -1017,6 +1020,7 @@ impl HcieIcedApp {
selection_edge_cache: Default::default(), selection_edge_cache: Default::default(),
marching_ants_edges: None, marching_ants_edges: None,
selected_vector_shape: None, selected_vector_shape: None,
vector_cycle_index: 0,
vector_edit_handle: hcie_engine_api::VectorEditHandle::None, vector_edit_handle: hcie_engine_api::VectorEditHandle::None,
}; };
@@ -3075,6 +3079,7 @@ impl HcieIcedApp {
selection_edge_cache: Default::default(), selection_edge_cache: Default::default(),
marching_ants_edges: None, marching_ants_edges: None,
selected_vector_shape: None, selected_vector_shape: None,
vector_cycle_index: 0,
vector_edit_handle: hcie_engine_api::VectorEditHandle::None, vector_edit_handle: hcie_engine_api::VectorEditHandle::None,
}); });
self.active_doc = self.documents.len() - 1; self.active_doc = self.documents.len() - 1;
@@ -3483,21 +3488,63 @@ impl HcieIcedApp {
} }
} }
// Hit-test vector shapes under cursor; select the topmost one. // Find ALL shapes at the click position for cycling support.
if let Some(idx) = self.documents[self.active_doc].engine.find_vector_shape_at(x, y) { // Collect indices of shapes whose bounding box contains (x, y),
// Save snapshot before editing (for undo). // iterating forward so index 0 is the bottom-most shape.
let snapshot = self.documents[self.active_doc].engine.active_vector_shapes(); if let Some(shapes) = self.documents[self.active_doc].engine.active_vector_shapes() {
self.vector_shapes_snapshot = snapshot; let hits: Vec<usize> = shapes.iter().enumerate()
let doc = &mut self.documents[self.active_doc]; .filter(|(_, s)| {
doc.selected_vector_shape = Some(idx); let (l, t, r, b) = s.normalized_bounds();
doc.vector_draw = None; // clear any preview x >= l && x <= r && y >= t && y <= b
doc.vector_edit_handle = VEH::Move; })
self.vector_drag_last = Some((x, y)); .map(|(i, _)| i)
crate::shape_sync::sync_tool_from_shape(self); .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];
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));
crate::shape_sync::sync_tool_from_shape(self);
} else {
// 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 { } else {
// No shape under cursor — deselect. // No vector shapes on this layer — deselect.
let doc = &mut self.documents[self.active_doc]; let doc = &mut self.documents[self.active_doc];
doc.selected_vector_shape = None; doc.selected_vector_shape = None;
doc.vector_cycle_index = 0;
doc.vector_edit_handle = VEH::None; doc.vector_edit_handle = VEH::None;
self.vector_shapes_snapshot = None; self.vector_shapes_snapshot = None;
self.vector_drag_last = None; self.vector_drag_last = None;
@@ -3555,6 +3602,7 @@ impl HcieIcedApp {
let layer_id = self.documents[self.active_doc].engine.active_layer_id(); 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].engine.delete_vector_shape(layer_id, idx);
self.documents[self.active_doc].selected_vector_shape = None; self.documents[self.active_doc].selected_vector_shape = None;
self.documents[self.active_doc].vector_cycle_index = 0;
self.vector_shapes_snapshot = None; self.vector_shapes_snapshot = None;
self.documents[self.active_doc].engine.mark_composite_dirty(); self.documents[self.active_doc].engine.mark_composite_dirty();
self.refresh_composite_if_needed(); self.refresh_composite_if_needed();
@@ -3566,10 +3614,12 @@ impl HcieIcedApp {
Message::VectorShapeSelect(idx) => { Message::VectorShapeSelect(idx) => {
self.documents[self.active_doc].selected_vector_shape = idx; self.documents[self.active_doc].selected_vector_shape = idx;
if idx.is_some() { if idx.is_some() {
self.documents[self.active_doc].vector_cycle_index = 0;
let snapshot = self.documents[self.active_doc].engine.active_vector_shapes(); let snapshot = self.documents[self.active_doc].engine.active_vector_shapes();
self.vector_shapes_snapshot = snapshot; self.vector_shapes_snapshot = snapshot;
crate::shape_sync::sync_tool_from_shape(self); crate::shape_sync::sync_tool_from_shape(self);
} else { } else {
self.documents[self.active_doc].vector_cycle_index = 0;
self.vector_shapes_snapshot = None; self.vector_shapes_snapshot = None;
} }
} }
@@ -3578,6 +3628,7 @@ impl HcieIcedApp {
// Commit edits: clear snapshot and deselect. // Commit edits: clear snapshot and deselect.
let doc = &mut self.documents[self.active_doc]; let doc = &mut self.documents[self.active_doc];
doc.selected_vector_shape = None; doc.selected_vector_shape = None;
doc.vector_cycle_index = 0;
doc.vector_edit_handle = hcie_engine_api::VectorEditHandle::None; doc.vector_edit_handle = hcie_engine_api::VectorEditHandle::None;
self.vector_shapes_snapshot = None; self.vector_shapes_snapshot = None;
self.vector_drag_last = None; self.vector_drag_last = None;
@@ -3591,6 +3642,7 @@ impl HcieIcedApp {
*shapes = snapshot; *shapes = snapshot;
} }
doc.selected_vector_shape = None; doc.selected_vector_shape = None;
doc.vector_cycle_index = 0;
doc.vector_edit_handle = hcie_engine_api::VectorEditHandle::None; doc.vector_edit_handle = hcie_engine_api::VectorEditHandle::None;
self.vector_drag_last = None; self.vector_drag_last = None;
doc.engine.mark_composite_dirty(); doc.engine.mark_composite_dirty();
@@ -4545,6 +4597,7 @@ impl HcieIcedApp {
selection_edge_cache: Default::default(), selection_edge_cache: Default::default(),
marching_ants_edges: None, marching_ants_edges: None,
selected_vector_shape: None, selected_vector_shape: None,
vector_cycle_index: 0,
vector_edit_handle: hcie_engine_api::VectorEditHandle::None, vector_edit_handle: hcie_engine_api::VectorEditHandle::None,
}); });
} }