From bfe6a86bd5b1d02988dbd9a3bb5759854d76a050 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 24 May 2021 22:27:41 +0530 Subject: basic primitives to repeat last paint op --- src/app.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src/app.rs') diff --git a/src/app.rs b/src/app.rs index 3e7ac0f..d8a6132 100644 --- a/src/app.rs +++ b/src/app.rs @@ -257,6 +257,40 @@ impl<'ctx> AppState<'ctx> { } } + pub fn repeat_last_paint(&mut self, target: MapPoint) { + self.commit_operation(); + if let Some(ModifyRecord::Paint(records)) = self + .undo_stack + .operations + .iter() + .rev() + .find(|&o| matches!(o, ModifyRecord::Paint(_))) + { + dbg!(target); + // find center of paint cluster + let centroid: MapPoint = records + .iter() + .fold(MapPoint { x: 0, y: 0 }, |acc, x| acc + x.point) + .reduce(records.len() as u32); + dbg!(centroid); + // re-center cluster around target + let centered_op = records + .iter() + .map(|x| PaintRecord { + point: x.point + target - centroid, + ..*x + }) + .collect::>(); + let mut ud = Vec::new(); + for r in ¢ered_op { + let p = r.point; + let v = r.new; + ud.push(PaintRecord::new(p, self.pixmap.set(p, v), v)); + } + self.undo_stack.push(ModifyRecord::Paint(ud)); + } + } + pub fn zoom_in(&mut self, p: (i32, i32)) { // attempt to center around cursor if let Some(p) = self.idx_at_coord(p) { @@ -774,6 +808,11 @@ impl<'ctx> AppState<'ctx> { let cursor = (mouse.x(), mouse.y()); self.zoom_out(cursor); } + Keycode::Period => { + if let Some(contact) = self.idx_at_coord(self.mouse) { + self.repeat_last_paint(contact.into()); + } + } // brush ops Keycode::Q => self.brush.shrink(), Keycode::E => self.brush.grow(), -- cgit v1.2.3