From 0b8cd719a436220a80955d7e40caaf5f00f1292d Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 14 Mar 2021 12:35:19 +0530 Subject: simplify mvc model --- src/app.rs | 147 +++++++++++++++++++++++++++++-------------------------------- 1 file changed, 69 insertions(+), 78 deletions(-) diff --git a/src/app.rs b/src/app.rs index 3c57f27..bcc9c3a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,9 +1,9 @@ use crate::{ - bitmap::{MapPoint, Pixmap}, + bitmap::{Axis, MapPoint, Pixmap}, undo::{ModifyRecord, OpKind, Operation, UndoStack}, }; -use std::convert::{From, TryFrom}; +use std::convert::From; use sdl2::{ event::Event, @@ -19,17 +19,18 @@ use sdl2::{ use crate::consts::{BLACK, GRID_COLOR, WHITE}; pub struct AppState<'ctx> { - start: Point, - pixmap: Pixmap, - zoom: u8, + active_color: bool, brush_size: u8, - grid: Grid, - context: &'ctx Sdl, canvas: Canvas, + context: &'ctx Sdl, + current_operation: Operation, + grid: Grid, last_point: Option, - active_color: bool, + pixmap: Pixmap, + start: Point, + symmetry: Symmetry, undo_stack: UndoStack, - current_operation: Operation, + zoom: u8, } struct Grid { @@ -240,14 +241,9 @@ impl<'ctx> AppState<'ctx> { } } - fn modify(&mut self, func: F) - where - F: FnOnce(&mut Self), - { - func(self); - self.canvas.set_draw_color(Color::RGB(0, 0, 0)); + fn redraw(&mut self) { + self.canvas.set_draw_color(BLACK); self.canvas.clear(); - self.canvas.set_draw_color(Color::RGB(64, 64, 64)); self.draw(); self.canvas.present(); } @@ -274,17 +270,18 @@ impl<'ctx> AppState<'ctx> { let pixmap = Pixmap::new_with(width, height, false); Self { - start: Point::new(60, 60), - zoom: 5, + active_color: true, brush_size: 0, - pixmap, - grid: Grid::new(), canvas, context, + current_operation: Vec::new(), + grid: Grid::new(), last_point: None, - active_color: true, + pixmap, + start: Point::new(60, 60), + symmetry: Default::default(), undo_stack: UndoStack::new(), - current_operation: Vec::new(), + zoom: 5, } } @@ -305,52 +302,53 @@ impl<'ctx> AppState<'ctx> { } => { match k { // pan - Keycode::W => self.modify(|e| e.pan((0, 10))), - Keycode::A => self.modify(|e| e.pan((10, 0))), - Keycode::S => self.modify(|e| e.pan((0, -10))), - Keycode::D => self.modify(|e| e.pan((-10, 0))), + Keycode::W => self.pan((0, 10)), + Keycode::A => self.pan((10, 0)), + Keycode::S => self.pan((0, -10)), + Keycode::D => self.pan((-10, 0)), // zoom Keycode::C => { let cursor = (mouse.x(), mouse.y()); - self.modify(|e| e.zoom_in(cursor)); + self.zoom_in(cursor); } Keycode::Z => { let cursor = (mouse.x(), mouse.y()); - self.modify(|e| e.zoom_out(cursor)); + self.zoom_out(cursor); } // brush ops Keycode::Q => self.decrease_brush_size(), Keycode::E => self.increase_brush_size(), // flip color - Keycode::X => self.modify(|e| e.change_active_color()), + Keycode::X => self.change_active_color(), // toggle grid - Keycode::Tab => self.modify(|e| e.toggle_grid()), + Keycode::Tab => self.toggle_grid(), // line drawing - Keycode::F => self.modify(|e| { + Keycode::F => { let end = (mouse.x(), mouse.y()).into(); - if let Some(start) = e.last_point { - if let Ok(o) = e.paint_line(start, end) { - e.commit_operation(); - e.current_operation = + if let Some(start) = self.last_point { + if let Ok(o) = self.paint_line(start, end) { + self.commit_operation(); + self.current_operation = o.into_iter().filter(|v| !v.old_val == v.val).collect(); - e.commit_operation(); - e.last_point = Some(end); + self.commit_operation(); + self.last_point = Some(end); } } - }), + } + Keycode::V => self.cycle_symmetry(), // exit Keycode::Escape => break 'running, // undo & redo - Keycode::U => self.modify(|e| { - if let Some(op) = e.undo_stack.undo() { - e.apply_operation(op, OpKind::Undo); + Keycode::U => { + if let Some(op) = self.undo_stack.undo() { + self.apply_operation(op, OpKind::Undo); } - }), - Keycode::R => self.modify(|e| { - if let Some(op) = e.undo_stack.redo() { - e.apply_operation(op, OpKind::Redo); + } + Keycode::R => { + if let Some(op) = self.undo_stack.redo() { + self.apply_operation(op, OpKind::Redo); } - }), + } _ => (), } } @@ -358,57 +356,50 @@ impl<'ctx> AppState<'ctx> { Event::MouseButtonDown { x, y, mouse_btn, .. } => { - self.modify(|e| { - let pt = (x, y); - e.last_point = Some(pt.into()); - let val = match mouse_btn { - MouseButton::Right => !e.active_color, - _ => e.active_color, - }; - if let Ok(o) = e.paint_point(pt, val) { - e.current_operation.extend(o); - } - }); + let pt = (x, y); + self.last_point = Some(pt.into()); + let val = match mouse_btn { + MouseButton::Right => !self.active_color, + _ => self.active_color, + }; + if let Ok(o) = self.paint_point(pt, val) { + self.current_operation.extend(o); + } } // click and drag Event::MouseMotion { x, y, mousestate, .. } => { if mousestate.is_mouse_button_pressed(MouseButton::Left) { - self.modify(|e| { - let pt = (x, y); - let val = e.active_color; - if let Ok(o) = e.paint_point(pt, val) { - e.current_operation.extend(o); - } - }); + let pt = (x, y); + let val = self.active_color; + if let Ok(o) = self.paint_point(pt, val) { + self.current_operation.extend(o); + } } else if mousestate.is_mouse_button_pressed(MouseButton::Right) { - self.modify(|e| { - let pt = (x, y); - let val = !e.active_color; - if let Ok(o) = e.paint_point(pt, val) { - e.current_operation.extend(o); - } - }); + let pt = (x, y); + let val = !self.active_color; + if let Ok(o) = self.paint_point(pt, val) { + self.current_operation.extend(o); + } } } // end of operation - Event::MouseButtonUp { .. } => self.modify(|e| { - let op = e + Event::MouseButtonUp { .. } => { + let op = self .current_operation .drain(..) .filter(|v| !v.old_val == v.val) .collect::>(); - e.undo_stack.push(op); - }), + self.undo_stack.push(op); + } Event::Quit { .. } => { break 'running; } - _ => { - self.modify(|_| ()); - } + _ => {} } } + self.redraw(); } } } -- cgit v1.2.3