From 316d20f84bd2736b1a663fe763aa82b8cdb8e5c9 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 3 Apr 2021 14:35:01 +0530 Subject: fix symmetry calculations and uneven grid drawing --- src/app.rs | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 725a45a..119fc38 100644 --- a/src/app.rs +++ b/src/app.rs @@ -132,15 +132,15 @@ impl<'ctx> AppState<'ctx> { let Symmetry { x, y } = self.symmetry; self.symmetry = match (x, y) { (None, None) => Symmetry { - x: Some(self.width() / 2), + x: Some(self.height() / 2), y: None, }, (_, None) => Symmetry { x: None, - y: Some(self.height() / 2), + y: Some(self.width() / 2), }, (None, y) => Symmetry { - x: Some(self.width() / 2), + x: Some(self.height() / 2), y, }, (Some(_), Some(_)) => Symmetry { x: None, y: None }, @@ -383,8 +383,14 @@ impl<'ctx> AppState<'ctx> { format!("---, ---") }; let status_text = format!( - "[DITHER {}][PT {}][KIND {}]", - self.dither_level, mouse_coords, self.brush + "{} [PT {}][KIND {}]", + if self.file_name.is_some() { + format!("{}", self.file_name.as_ref().unwrap().display()) + } else { + "No Name".into() + }, + mouse_coords, + self.brush ); draw_text( &mut self.canvas, @@ -513,21 +519,27 @@ impl<'ctx> AppState<'ctx> { let (width, height) = (self.width(), self.height()); let start = self.start; let grid_enabled = self.grid.enabled; - for (idx, val) in self.pixmap.data.iter().enumerate() { - if *val { - let idx = idx as i32; - let (x, y) = (idx % width as i32, idx / height as i32); - self.canvas.set_draw_color(WHITE); - self.canvas - .fill_rect(Rect::new( - x * cs as i32 + start.x(), - y * cs as i32 + start.y(), - cs, - cs, - )) - .unwrap(); + self.canvas.set_draw_color(WHITE); + for x in 0..width { + for y in 0..height { + if self.pixmap.get(MapPoint { x, y }) { + self.canvas + .fill_rect(Rect::new( + (x * cs) as i32 + start.x(), + (y * cs) as i32 + start.y(), + cs, + cs, + )) + .unwrap(); + } } } + // for (idx, val) in self.pixmap.data.iter().enumerate() { + // if *val { + // let idx = idx as i32; + // let (x, y) = (idx % width as i32, idx / height as i32); + // } + // } if grid_enabled { self.draw_grid(); } @@ -612,7 +624,7 @@ impl<'ctx> AppState<'ctx> { pub fn save_as>(&self, file_name: P) -> Result<(), AppError> { let image = self.export().encode().unwrap(); let mut file = File::create(file_name).map_err(AppError::File)?; - file.write_all(&image[..]).unwrap(); + file.write_all(&image[..]).map_err(AppError::File)?; return Ok(()); } @@ -742,11 +754,15 @@ impl<'ctx> AppState<'ctx> { } Brush::Fill => { if let Some(c) = contact { + // this `get` is unchecked because contact is checked + // to be within pixmap let target = self.pixmap.get(c); let replacement = self.active_color; let operation = self.pixmap.flood_fill(c, target, replacement); for o in operation.iter() { + // this `set` is unchecked because the returned + // value of flood_fill is checked to be within pixmap self.pixmap.set(o.clone(), replacement); } self.current_operation.extend( -- cgit v1.2.3