aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-04-03 10:05:31 +0100
committerAkshay <[email protected]>2021-04-03 10:06:41 +0100
commitc97b91744ef37baa76b4a91deb1a9f48aeb3fd6a (patch)
treebf5c062d1219d2584613182b619a15c8e9beb0f4
parent316d20f84bd2736b1a663fe763aa82b8cdb8e5c9 (diff)
implement brush size caching
-rw-r--r--src/app.rs33
-rw-r--r--src/cache.rs3
-rw-r--r--src/main.rs1
3 files changed, 37 insertions, 0 deletions
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 @@
1use crate::{ 1use crate::{
2 bitmap::{positive_angle_with_x, MapPoint, Pixmap}, 2 bitmap::{positive_angle_with_x, MapPoint, Pixmap},
3 brush::{Brush, CircleBrush, LineBrush}, 3 brush::{Brush, CircleBrush, LineBrush},
4 cache::Cache,
4 command::CommandBox, 5 command::CommandBox,
5 consts::{colors::*, FONT_PATH, RC_PATH, STDLIB_PATH}, 6 consts::{colors::*, FONT_PATH, RC_PATH, STDLIB_PATH},
6 dither, 7 dither,
@@ -15,6 +16,7 @@ use crate::{
15}; 16};
16 17
17use std::{ 18use std::{
19 cell::RefCell,
18 convert::From, 20 convert::From,
19 fs::File, 21 fs::File,
20 io::prelude::*, 22 io::prelude::*,
@@ -45,6 +47,7 @@ pub struct AppState<'ctx> {
45 pub brush: Brush, 47 pub brush: Brush,
46 pub canvas: Canvas<Window>, 48 pub canvas: Canvas<Window>,
47 pub command_box: CommandBox, 49 pub command_box: CommandBox,
50 pub cache: RefCell<Option<Cache>>,
48 pub context: &'ctx Sdl, 51 pub context: &'ctx Sdl,
49 pub current_operation: Vec<PaintRecord>, 52 pub current_operation: Vec<PaintRecord>,
50 pub dither_level: u8, 53 pub dither_level: u8,
@@ -90,6 +93,13 @@ impl<'ctx> AppState<'ctx> {
90 self.pixmap.height 93 self.pixmap.height
91 } 94 }
92 95
96 pub fn cache(&self) {
97 let mut cache = self.cache.borrow_mut();
98 *cache = Some(Cache {
99 last_brush_size: self.brush.size().unwrap_or(0),
100 });
101 }
102
93 fn bounds(&self) -> (Point, Point) { 103 fn bounds(&self) -> (Point, Point) {
94 let x_min = self.start.x(); 104 let x_min = self.start.x();
95 let y_min = self.start.y(); 105 let y_min = self.start.y();
@@ -593,6 +603,7 @@ impl<'ctx> AppState<'ctx> {
593 canvas, 603 canvas,
594 command_box: CommandBox::new(), 604 command_box: CommandBox::new(),
595 context, 605 context,
606 cache: RefCell::new(None),
596 current_operation: Vec::new(), 607 current_operation: Vec::new(),
597 dither_level: 16, 608 dither_level: 16,
598 file_name, 609 file_name,
@@ -711,9 +722,30 @@ impl<'ctx> AppState<'ctx> {
711 eprintln!("writing to file"); 722 eprintln!("writing to file");
712 buffer.write_all(&encoded[..]).unwrap(); 723 buffer.write_all(&encoded[..]).unwrap();
713 } 724 }
725 _ if keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD => {
726 self.brush = Brush::line(
727 self.cache
728 .borrow()
729 .as_ref()
730 .map(|c| c.last_brush_size)
731 .unwrap_or(0),
732 true,
733 );
734 }
714 _ => (), 735 _ => (),
715 } 736 }
716 } 737 }
738 Event::KeyUp {
739 keycode: Some(k), ..
740 } if k == Keycode::LCtrl || k == Keycode::RCtrl => {
741 self.brush = Brush::new(
742 self.cache
743 .borrow()
744 .as_ref()
745 .map(|c| c.last_brush_size)
746 .unwrap_or(0),
747 );
748 }
717 // start of operation 749 // start of operation
718 Event::MouseButtonDown { 750 Event::MouseButtonDown {
719 x, y, mouse_btn, .. 751 x, y, mouse_btn, ..
@@ -869,6 +901,7 @@ impl<'ctx> AppState<'ctx> {
869 } 901 }
870 } 902 }
871 } 903 }
904 self.cache();
872 self.redraw(); 905 self.redraw();
873 } 906 }
874 } 907 }
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 @@
1pub struct Cache {
2 pub last_brush_size: u8,
3}
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 @@
1mod app; 1mod app;
2mod bitmap; 2mod bitmap;
3mod brush; 3mod brush;
4mod cache;
4mod cli; 5mod cli;
5mod command; 6mod command;
6mod consts; 7mod consts;