aboutsummaryrefslogtreecommitdiff
path: root/src/app.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-04-06 10:24:54 +0100
committerAkshay <[email protected]>2021-04-06 10:24:54 +0100
commitc84c38544bc6e81f0b0482e4e82b6c95848c1a0c (patch)
tree3ae2f3387ba5ef56a7f7c3304dc029ae7845f175 /src/app.rs
parent3f5b917c6ced370d940774b51ff89cec0d03c562 (diff)
apply clippy lints
Diffstat (limited to 'src/app.rs')
-rw-r--r--src/app.rs101
1 files changed, 48 insertions, 53 deletions
diff --git a/src/app.rs b/src/app.rs
index 33d4934..f42b48c 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -108,10 +108,10 @@ impl<'ctx> AppState<'ctx> {
108 let y_min = self.start.y(); 108 let y_min = self.start.y();
109 let x_max = self.start.x() + (self.width() * self.zoom as u32) as i32; 109 let x_max = self.start.x() + (self.width() * self.zoom as u32) as i32;
110 let y_max = self.start.y() + (self.height() * self.zoom as u32) as i32; 110 let y_max = self.start.y() + (self.height() * self.zoom as u32) as i32;
111 return ( 111 (
112 Point::new(x_min, y_min), 112 Point::new(x_min, y_min),
113 Point::new(x_max as i32, y_max as i32), 113 Point::new(x_max as i32, y_max as i32),
114 ); 114 )
115 } 115 }
116 116
117 pub fn change_active_color(&mut self) { 117 pub fn change_active_color(&mut self) {
@@ -125,7 +125,7 @@ impl<'ctx> AppState<'ctx> {
125 let rel_p = p - self.start; 125 let rel_p = p - self.start;
126 // reduce p based on zoom and cell size 126 // reduce p based on zoom and cell size
127 let (sx, sy) = (rel_p.x() / self.zoom as i32, rel_p.y() / self.zoom as i32); 127 let (sx, sy) = (rel_p.x() / self.zoom as i32, rel_p.y() / self.zoom as i32);
128 return Some((sx as u32, sy as u32)); 128 Some((sx as u32, sy as u32))
129 } else { 129 } else {
130 None 130 None
131 } 131 }
@@ -254,7 +254,7 @@ impl<'ctx> AppState<'ctx> {
254 let op = self 254 let op = self
255 .current_operation 255 .current_operation
256 .drain(..) 256 .drain(..)
257 .filter(|v| !v.old == v.new) 257 .filter(|v| v.old != v.new)
258 .collect::<Vec<_>>(); 258 .collect::<Vec<_>>();
259 self.undo_stack.push(ModifyRecord::Paint(op)); 259 self.undo_stack.push(ModifyRecord::Paint(op));
260 } 260 }
@@ -267,7 +267,7 @@ impl<'ctx> AppState<'ctx> {
267 let (x2, y2) = (p.0 * (1 + self.zoom as u32), p.1 * (1 + self.zoom as u32)); 267 let (x2, y2) = (p.0 * (1 + self.zoom as u32), p.1 * (1 + self.zoom as u32));
268 let diffx = x2 as i32 - x1 as i32; 268 let diffx = x2 as i32 - x1 as i32;
269 let diffy = y2 as i32 - y1 as i32; 269 let diffy = y2 as i32 - y1 as i32;
270 self.start = self.start - Point::from((diffx, diffy)); 270 self.start -= Point::from((diffx, diffy));
271 } 271 }
272 self.zoom += 1; 272 self.zoom += 1;
273 } 273 }
@@ -280,7 +280,7 @@ impl<'ctx> AppState<'ctx> {
280 let (x2, y2) = (p.0 * (self.zoom as u32 - 1), p.1 * (self.zoom as u32 - 1)); 280 let (x2, y2) = (p.0 * (self.zoom as u32 - 1), p.1 * (self.zoom as u32 - 1));
281 let diffx = x2 as i32 - x1 as i32; 281 let diffx = x2 as i32 - x1 as i32;
282 let diffy = y2 as i32 - y1 as i32; 282 let diffy = y2 as i32 - y1 as i32;
283 self.start = self.start - Point::from((diffx, diffy)); 283 self.start -= Point::from((diffx, diffy));
284 } 284 }
285 self.zoom -= 1; 285 self.zoom -= 1;
286 } 286 }
@@ -310,7 +310,7 @@ impl<'ctx> AppState<'ctx> {
310 310
311 pub fn eval_command(&mut self) { 311 pub fn eval_command(&mut self) {
312 let lisp_expr = &self.command_box.text; 312 let lisp_expr = &self.command_box.text;
313 let mut parser = Parser::new(Lexer::new(lisp_expr, 0)); 313 let mut parser = Parser::new(Lexer::new(lisp_expr));
314 let res = parser.parse_single_expr(); 314 let res = parser.parse_single_expr();
315 match res { 315 match res {
316 Ok(expr) => { 316 Ok(expr) => {
@@ -393,7 +393,7 @@ impl<'ctx> AppState<'ctx> {
393 let mouse_coords = if let Some((x, y)) = self.idx_at_coord(self.mouse) { 393 let mouse_coords = if let Some((x, y)) = self.idx_at_coord(self.mouse) {
394 format!("{:3}, {:3}", x + 1, y + 1) 394 format!("{:3}, {:3}", x + 1, y + 1)
395 } else { 395 } else {
396 format!("---, ---") 396 String::from("---, ---")
397 }; 397 };
398 let status_text = format!( 398 let status_text = format!(
399 "{} [PT {}][KIND {}]", 399 "{} [PT {}][KIND {}]",
@@ -478,32 +478,29 @@ impl<'ctx> AppState<'ctx> {
478 } 478 }
479 } 479 }
480 } 480 }
481 match self.brush { 481 if let Brush::Line(LineBrush { start, size, .. }) = self.brush {
482 Brush::Line(LineBrush { start, size, .. }) => { 482 let size = self.zoom as u32 * (size as u32 + 5);
483 let size = self.zoom as u32 * (size as u32 + 5); 483 if let (Some(from), Some(to)) = (start, pt) {
484 if let (Some(from), Some(to)) = (start, pt) { 484 let line = self.pixmap.get_line(from, to.into());
485 let line = self.pixmap.get_line(from, to.into()); 485 draw_text(
486 draw_text( 486 &mut self.canvas,
487 &mut self.canvas, 487 self.ttf_context,
488 self.ttf_context, 488 format!("{}°", positive_angle_with_x(from, to.into())),
489 format!("{}°", positive_angle_with_x(from, to.into())), 489 PINK,
490 PINK, 490 (self.mouse.0 + size as i32, self.mouse.1 + size as i32),
491 (self.mouse.0 + size as i32, self.mouse.1 + size as i32), 491 );
492 ); 492 for MapPoint { x, y } in line.into_iter() {
493 for MapPoint { x, y } in line.into_iter() { 493 self.canvas.set_draw_color(PINK);
494 self.canvas.set_draw_color(PINK); 494 self.canvas
495 self.canvas 495 .fill_rect(Rect::new(
496 .fill_rect(Rect::new( 496 x as i32 * cs as i32 + self.start.x(),
497 x as i32 * cs as i32 + self.start.x(), 497 y as i32 * cs as i32 + self.start.y(),
498 y as i32 * cs as i32 + self.start.y(), 498 cs,
499 cs, 499 cs,
500 cs, 500 ))
501 )) 501 .unwrap();
502 .unwrap();
503 }
504 } 502 }
505 } 503 }
506 _ => {}
507 } 504 }
508 } 505 }
509 506
@@ -530,14 +527,12 @@ impl<'ctx> AppState<'ctx> {
530 527
531 fn draw_symmetry(&mut self) { 528 fn draw_symmetry(&mut self) {
532 let Symmetry { x, y } = self.symmetry; 529 let Symmetry { x, y } = self.symmetry;
533 x.and_then(|line| { 530 if let Some(line) = x {
534 self.draw_line_to_grid(line, Axis::X, CYAN); 531 self.draw_line_to_grid(line, Axis::X, CYAN)
535 Some(()) 532 }
536 }); 533 if let Some(line) = y {
537 y.and_then(|line| { 534 self.draw_line_to_grid(line, Axis::Y, CYAN)
538 self.draw_line_to_grid(line, Axis::Y, CYAN); 535 }
539 Some(())
540 });
541 } 536 }
542 537
543 fn draw_guides(&mut self) { 538 fn draw_guides(&mut self) {
@@ -626,7 +621,7 @@ impl<'ctx> AppState<'ctx> {
626 .build() 621 .build()
627 .map_err(|e| AppError::Sdl(e.to_string()))?; 622 .map_err(|e| AppError::Sdl(e.to_string()))?;
628 623
629 let data = start_data.unwrap_or(vec![false; (width * height) as usize]); 624 let data = start_data.unwrap_or_else(|| vec![false; (width * height) as usize]);
630 let pixmap = Pixmap::new_with(width, height, data); 625 let pixmap = Pixmap::new_with(width, height, data);
631 let mut app = Self { 626 let mut app = Self {
632 active_color: true, 627 active_color: true,
@@ -668,7 +663,7 @@ impl<'ctx> AppState<'ctx> {
668 let image = self.export().encode().unwrap(); 663 let image = self.export().encode().unwrap();
669 let mut file = File::create(file_name).map_err(AppError::File)?; 664 let mut file = File::create(file_name).map_err(AppError::File)?;
670 file.write_all(&image[..]).map_err(AppError::File)?; 665 file.write_all(&image[..]).map_err(AppError::File)?;
671 return Ok(()); 666 Ok(())
672 } 667 }
673 668
674 pub fn run(&mut self) { 669 pub fn run(&mut self) {
@@ -799,21 +794,21 @@ impl<'ctx> AppState<'ctx> {
799 start, 794 start,
800 extend, 795 extend,
801 }) => { 796 }) => {
802 if start.is_none() { 797 if let Some(s) = start {
798 if let Ok(o) = self.paint_line(s, pt, val, size) {
799 self.current_operation.extend(o);
800 self.brush = Brush::Line(LineBrush {
801 size,
802 start: if extend { contact } else { None },
803 extend,
804 });
805 }
806 } else {
803 self.brush = Brush::Line(LineBrush { 807 self.brush = Brush::Line(LineBrush {
804 size, 808 size,
805 start: contact, 809 start: contact,
806 extend, 810 extend,
807 }); 811 });
808 } else if let Ok(o) =
809 self.paint_line(start.unwrap(), pt, val, size)
810 {
811 self.current_operation.extend(o);
812 self.brush = Brush::Line(LineBrush {
813 size,
814 start: if extend { contact } else { None },
815 extend,
816 });
817 } 812 }
818 } 813 }
819 Brush::Fill => { 814 Brush::Fill => {
@@ -827,7 +822,7 @@ impl<'ctx> AppState<'ctx> {
827 for o in operation.iter() { 822 for o in operation.iter() {
828 // this `set` is unchecked because the returned 823 // this `set` is unchecked because the returned
829 // value of flood_fill is checked to be within pixmap 824 // value of flood_fill is checked to be within pixmap
830 self.pixmap.set(o.clone(), replacement); 825 self.pixmap.set(*o, replacement);
831 } 826 }
832 self.current_operation.extend( 827 self.current_operation.extend(
833 operation 828 operation