Guard stored paths and fix History redo jumps

This commit is contained in:
chasse20 committed 2026-11-10 22:23:51 -05:00
1 parent 6a976004ee
commit 76b9a30f63
5 files changed
+178 -19

No files matched your search

+42 -17
View File
@@ -532,6 +532,11 @@ impl Session {
return None;
}
// A click in the other view must not replace a committed path's coordinate space
if self.tool.path.editing.is_some() && self.path_draft.is_none() && self.tool.path.uv != uv {
return None;
}
self.path_checkpoint();
self.tool.path.push(point, uv);
self.tool.path.selected = self.tool.path.points.len().checked_sub(1);
@@ -602,26 +607,46 @@ impl Session {
return 0;
}
let mut steps = document_steps;
let mut current = &self.tool.path;
if self.tool.tool == Tool::Path {
for state in self.path_steps.0.iter().rev() {
if state.same_curve(current) {
continue;
}
let (point_steps, clears_path) = self.path_steps_before_document(true);
let cancel_draft = self.path_draft.is_some() && !clears_path;
document_steps.saturating_add(point_steps).saturating_add(usize::from(cancel_draft))
}
steps = steps.saturating_add(1);
// Restoring an empty curve cancels the draft and clears point history in the same undo
if state.points.is_empty() {
return steps;
}
current = state;
}
/// Redo calls needed to move forward through document history, including pending point edits.
/// Unlike undo, document redo cancels any remaining draft in the same call.
pub fn history_redo_steps(&self, document_steps: usize) -> usize {
if document_steps == 0 {
return 0;
}
steps.saturating_add(usize::from(self.path_draft.is_some()))
document_steps.saturating_add(self.path_steps_before_document(false).0)
}
/// Count pending point steps and whether their last step clears the path
fn path_steps_before_document(&self, back: bool) -> (usize, bool) {
if self.tool.tool != Tool::Path {
return (0, false);
}
let states = if back { &self.path_steps.0 } else { &self.path_steps.1 };
let mut steps = 0usize;
let mut current = &self.tool.path;
for state in states.iter().rev() {
if state.same_curve(current) {
continue;
}
steps = steps.saturating_add(1);
// Restoring an empty curve cancels the draft and clears point history in the same step
if state.points.is_empty() {
return (steps, true);
}
current = state;
}
(steps, false)
}
/// Undo (`back`) or redo a point change and repaint before returning; failure preserves the curve and history
+60
View File
@@ -517,6 +517,66 @@ fn preview_uses_the_commands_explicit_viewport() {
assert_eq!(preview, path_pixels(&direct));
}
/// An opposite-view placement must preserve the stored curve, painted pixels and point redo
#[test]
fn stored_paths_ignore_point_placement_from_the_other_view() {
for uv in [false, true] {
let mut session = session(wall());
let params =
if uv { json!({"uvPoints": [[0.2, 0.5], [0.8, 0.5]], "commit": true}) } else { json!({"points": [[140, 210], [260, 210]], "commit": true}) };
let report = execute(&mut session, "tool.path", &params).unwrap();
execute(&mut session, "path.edit", &json!({"effect": report["committed"]["effect"], "index": 0})).unwrap();
let (effect, index) = session.tool.path.editing.unwrap();
let uv_point = Vec3::new(0.5, 0.6, 0.0);
let world_point = session.pick(Vec2::new(200.0, 240.0), session.viewport).unwrap().pos;
assert_eq!(session.append_path_point(if uv { uv_point } else { world_point }, uv), Some(2));
session.path_live().unwrap();
let extended = session.tool.path.points.clone();
session.undo().unwrap();
let before = session.doc.clone();
let path = session.tool.path.clone();
let history = session.history_labels();
let placed = session.append_path_point(if uv { world_point } else { uv_point }, !uv);
session.path_live().unwrap();
assert_eq!(session.paths(effect).unwrap()[index].uv, uv, "the stored path must keep its coordinate space");
assert_eq!(placed, None);
assert_eq!(session.tool.path, path);
assert_eq!(session.doc.project, before.project);
assert_eq!(session.doc.pixels.layers, before.pixels.layers);
assert_eq!(session.doc.pixels.masks, before.pixels.masks);
assert_eq!(session.history_labels(), history);
assert!(session.can_redo(), "ignored placement must preserve point redo");
session.redo().unwrap();
assert_eq!(session.tool.path.points, extended);
}
}
/// A new, uncommitted curve can still switch views and cancel without changing the document
#[test]
fn draft_paths_can_switch_to_the_other_view() {
for uv in [false, true] {
let mut session = session(wall());
let before = session.doc.clone();
let history = session.history_labels();
let params = if uv { json!({"uvPoints": [[0.2, 0.5], [0.8, 0.5]]}) } else { json!({"points": [[140, 210], [260, 210]]}) };
execute(&mut session, "tool.path", &params).unwrap();
let point = if uv { session.pick(Vec2::new(200.0, 240.0), session.viewport).unwrap().pos } else { Vec3::new(0.5, 0.6, 0.0) };
assert_eq!(session.append_path_point(point, !uv), Some(0));
session.path_live().unwrap();
assert_eq!(session.tool.path.points, vec![point]);
assert_eq!(session.tool.path.uv, !uv);
assert_eq!(session.history_labels(), history);
session.cancel_path();
assert_eq!(session.doc.project, before.project);
assert_eq!(session.doc.pixels.layers, before.pixels.layers);
}
}
/// Headless history commands must restore geometry and pixels before any frame or subsequent edit
#[test]
fn stored_point_edits_undo_and_redo_in_order_without_selection_steps() {
+6 -2
View File
@@ -367,8 +367,12 @@ pub(crate) fn history(app: &mut TextureCraftApp, ui: &mut Ui) {
// Each row is the state after that many steps
let steps = done_count - row as i64;
if ui.selectable_label(steps == 0, text).on_hover_text(tr("Return to this step")).clicked() {
// Undo consumes point edits and drafts before the document rows shown here
jump = if steps >= 0 { app.session.history_undo_steps(steps as usize) as i64 } else { steps };
// Point history is consumed before the document rows shown here
jump = if steps >= 0 {
app.session.history_undo_steps(steps as usize) as i64
} else {
-(app.session.history_redo_steps((-steps) as usize) as i64)
};
}
}
});
+44
View File
@@ -97,6 +97,50 @@ fn history_jump_accounts_for_stored_point_edits_without_changing_single_steps()
assert!(harness.state().session.tool.path.points.is_empty());
}
/// Incomplete filled curves keep point redo separate from the document redo rows
#[test]
fn history_redo_jump_accounts_for_point_steps_without_changing_single_steps() {
let mut session = texturecraft_engine::sample_session(256, 64).unwrap();
let layer = session.selected_layer().unwrap();
let report =
execute(&mut session, "tool.path", &json!({"uvPoints": [[0.2, 0.5], [0.5, 0.3], [0.8, 0.5]], "closed": true, "fill": true, "commit": true})).unwrap();
for name in ["Step A", "Step B", "Step C"] {
execute(&mut session, "layer.rename", &json!({"name": name})).unwrap();
}
let done = session.history_labels().len();
execute(&mut session, "edit.undo", &json!({"steps": 3})).unwrap();
execute(&mut session, "path.edit", &json!({"effect": report["committed"]["effect"], "index": 0})).unwrap();
let original = session.tool.path.points.clone();
// Partial filled shapes do not repaint; their undo/redo leaves document redo available
for points in [json!([[0.2, 0.5], [0.8, 0.5]]), json!([[0.2, 0.5]]), json!([[0.2, 0.5], [0.8, 0.5]])] {
session.path_checkpoint();
execute(&mut session, "tool.path", &json!({"uvPoints": points})).unwrap();
}
for _ in 0..3 {
session.undo().unwrap();
}
assert_eq!(session.redo_labels().len(), 3);
let mut harness = history_harness(session);
harness.get_by_label(&format!("{} Redo", egui_phosphor::regular::ARROW_CLOCKWISE)).click();
harness.run_steps(3);
assert_eq!(harness.state().session.tool.path.points.len(), 2);
assert_eq!(harness.state().session.history_labels().len(), done - 3);
harness.get_by_label(&format!("{} Undo", egui_phosphor::regular::ARROW_COUNTER_CLOCKWISE)).click();
harness.run_steps(3);
assert_eq!(harness.state().session.tool.path.points, original);
assert_eq!(harness.state().session.history_labels().len(), done - 3);
harness.get_all_by_label("Rename layer (undone)").last().unwrap().click();
harness.run_steps(3);
assert_eq!(harness.state().session.doc.project.layer(layer).unwrap().name, "Step C");
assert_eq!(harness.state().session.history_labels().len(), done);
assert!(harness.state().session.redo_labels().is_empty());
assert!(harness.state().session.tool.path.points.is_empty());
}
/// Returning to the current document row cancels a draft without undoing a document step
#[test]
fn clicking_the_current_history_row_cancels_a_draft() {
+26
View File
@@ -144,6 +144,32 @@ fn clicking_an_applied_path_edits_it_and_dragging_a_point_changes_it() {
assert_eq!(h.state().session.history_labels().len(), steps + 1, "the edit is one step");
}
#[test]
fn clicking_the_2d_view_preserves_a_stored_3d_path_being_edited() {
let mut session =
texturecraft_engine::Session::new_project("t", texturecraft_engine::geom::primitives::sphere(1.0, 24, 12), "builtin:test", None, 128).unwrap();
session.camera.yaw = 0.0;
session.camera.pitch = 0.0;
session.update();
let report = execute(&mut session, "tool.path", &json!({"points": [[140, 210], [260, 210]], "viewport": [400, 400], "commit": true})).unwrap();
execute(&mut session, "path.edit", &json!({"effect": report["committed"]["effect"], "index": 0})).unwrap();
let before = session.doc.clone();
let path = session.tool.path.clone();
let history = session.history_labels();
let mut harness = Harness::builder().with_size(egui::vec2(1600.0, 960.0)).build_eframe(|context| TextureCraftApp::new(context, session));
harness.state_mut().ui.layout = ViewLayout::Split;
harness.run_steps(4);
let view_3d = harness.state().viewport_rect;
let view_2d_center = pos2(view_3d.right() + view_3d.width() * 0.5, view_3d.center().y);
click(&mut harness, view_2d_center);
assert_eq!(harness.state().session.tool.path, path);
assert_eq!(harness.state().session.doc.project, before.project);
assert_eq!(harness.state().session.doc.pixels.layers, before.pixels.layers);
assert_eq!(harness.state().session.history_labels(), history);
}
#[test]
fn clicking_the_curve_inserts_a_point_and_backspace_removes_the_selected_one() {
let mut h = harness();