From 7c311d2449cc81a20a9a3f5e26b71f36be263a0d Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 1 May 2021 13:28:42 +0530 Subject: add minimap/preview box, lisp function to toggle --- src/app.rs | 36 ++++++++++++++++++++++++++++++++++++ src/lisp/prelude.rs | 9 +++++++++ 2 files changed, 45 insertions(+) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 23458f4..fc7f736 100644 --- a/src/app.rs +++ b/src/app.rs @@ -57,6 +57,7 @@ pub struct AppState<'ctx> { pub file_name: Option, pub guides: HashMap, pub grid: Grid, + pub minimap: bool, pub lisp_env: EnvList, pub message: Message, pub mode: Mode, @@ -128,6 +129,10 @@ impl<'ctx> AppState<'ctx> { self.grid.enabled = !self.grid.enabled; } + pub fn toggle_minimap(&mut self) { + self.minimap = !self.minimap; + } + pub fn cycle_symmetry(&mut self) { let Symmetry { x, y } = self.symmetry; self.symmetry = match (x, y) { @@ -544,6 +549,33 @@ impl<'ctx> AppState<'ctx> { } } + fn draw_minimap(&mut self) { + let width = self.width(); + let container = Container::new(Offset::Right(self.width()), Offset::Top(0), &self.canvas) + .width(Size::Absolute(self.width()), &self.canvas) + .height(Size::Absolute(self.height()), &self.canvas); + + self.canvas.set_draw_color(BLACK); + self.canvas.fill_rect(container.area()).unwrap(); + self.canvas.set_draw_color(WHITE); + for (line_nr, scan) in self.pixmap.data[..].chunks(width as usize).enumerate() { + let mut pass = 0usize; + for (color, length) in utils::compress(scan) { + if color { + self.canvas + .fill_rect(rect!( + pass as i32 + container.start.0, + line_nr as i32 + container.start.1, + length as u32, + 1 + )) + .unwrap(); + } + pass += length; + } + } + } + fn draw(&mut self) { let cs = self.zoom as u32; let (width, height) = (self.width(), self.height()); @@ -579,6 +611,9 @@ impl<'ctx> AppState<'ctx> { self.grid .draw(&mut self.canvas, self.zoom, &self.start, width, height); } + if self.minimap { + self.draw_minimap(); + } self.draw_guides(); self.draw_symmetry(); self.draw_statusline(); @@ -639,6 +674,7 @@ impl<'ctx> AppState<'ctx> { lisp_env: vec![prelude::new_env().map_err(AppError::Lisp)?], message: Message::new().text(" "), mode: Mode::Draw, + minimap: false, mouse: (0, 0), pixmap, start: Point::new(60, 60), diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index 8088510..9330372 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -281,6 +281,15 @@ pub fn new_env() -> Result { Ok(LispExpr::Unit) }); + primitive!(env, Arity::Exact(0), "minimap-enabled?", |_, app| { + Ok(LispExpr::BoolLit(app.minimap)) + }); + + primitive!(env, Arity::Exact(1), "set-minimap!", |args, app| { + app.minimap = args[0].cast_bool(); + Ok(LispExpr::Unit) + }); + primitive!(env, Arity::Exact(0), "grid-rectangle", |_, app| { app.grid.kind = GridKind::Rectangle; Ok(LispExpr::Unit) -- cgit v1.2.3