From c97b91744ef37baa76b4a91deb1a9f48aeb3fd6a Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 3 Apr 2021 14:35:31 +0530 Subject: implement brush size caching --- src/app.rs | 33 +++++++++++++++++++++++++++++++++ src/cache.rs | 3 +++ src/main.rs | 1 + 3 files changed, 37 insertions(+) create mode 100644 src/cache.rs (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 119fc38..4a4e2cc 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,6 +1,7 @@ use crate::{ bitmap::{positive_angle_with_x, MapPoint, Pixmap}, brush::{Brush, CircleBrush, LineBrush}, + cache::Cache, command::CommandBox, consts::{colors::*, FONT_PATH, RC_PATH, STDLIB_PATH}, dither, @@ -15,6 +16,7 @@ use crate::{ }; use std::{ + cell::RefCell, convert::From, fs::File, io::prelude::*, @@ -45,6 +47,7 @@ pub struct AppState<'ctx> { pub brush: Brush, pub canvas: Canvas, pub command_box: CommandBox, + pub cache: RefCell>, pub context: &'ctx Sdl, pub current_operation: Vec, pub dither_level: u8, @@ -90,6 +93,13 @@ impl<'ctx> AppState<'ctx> { self.pixmap.height } + pub fn cache(&self) { + let mut cache = self.cache.borrow_mut(); + *cache = Some(Cache { + last_brush_size: self.brush.size().unwrap_or(0), + }); + } + fn bounds(&self) -> (Point, Point) { let x_min = self.start.x(); let y_min = self.start.y(); @@ -593,6 +603,7 @@ impl<'ctx> AppState<'ctx> { canvas, command_box: CommandBox::new(), context, + cache: RefCell::new(None), current_operation: Vec::new(), dither_level: 16, file_name, @@ -711,9 +722,30 @@ impl<'ctx> AppState<'ctx> { eprintln!("writing to file"); buffer.write_all(&encoded[..]).unwrap(); } + _ if keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD => { + self.brush = Brush::line( + self.cache + .borrow() + .as_ref() + .map(|c| c.last_brush_size) + .unwrap_or(0), + true, + ); + } _ => (), } } + Event::KeyUp { + keycode: Some(k), .. + } if k == Keycode::LCtrl || k == Keycode::RCtrl => { + self.brush = Brush::new( + self.cache + .borrow() + .as_ref() + .map(|c| c.last_brush_size) + .unwrap_or(0), + ); + } // start of operation Event::MouseButtonDown { x, y, mouse_btn, .. @@ -869,6 +901,7 @@ impl<'ctx> AppState<'ctx> { } } } + self.cache(); self.redraw(); } } diff --git a/src/cache.rs b/src/cache.rs new file mode 100644 index 0000000..249eca9 --- /dev/null +++ b/src/cache.rs @@ -0,0 +1,3 @@ +pub struct Cache { + pub last_brush_size: u8, +} diff --git a/src/main.rs b/src/main.rs index 304f6fc..9370abf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod app; mod bitmap; mod brush; +mod cache; mod cli; mod command; mod consts; -- cgit v1.2.3