From 149bc3627b886c306a877ac802c62089714326eb Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 19 Mar 2021 09:40:31 +0530 Subject: add grid centering keybind --- src/app.rs | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 3571436..8543c85 100644 --- a/src/app.rs +++ b/src/app.rs @@ -225,6 +225,30 @@ impl<'ctx> AppState<'ctx> { self.zoom += 1; } + fn zoom_out(&mut self, p: (i32, i32)) { + if self.zoom > 1 { + // attempt to center around cursor + if let Some(p) = self.idx_at_coord(p) { + let (x1, y1) = (p.0 * (self.zoom as u32), p.1 * (self.zoom as u32)); + let (x2, y2) = (p.0 * (self.zoom as u32 - 1), p.1 * (self.zoom as u32 - 1)); + let diffx = x2 as i32 - x1 as i32; + let diffy = y2 as i32 - y1 as i32; + self.start = self.start - Point::from((diffx, diffy)); + } + self.zoom -= 1; + } + } + + fn center_grid(&mut self) { + let (winsize_x, winsize_y) = self.canvas.window().size(); + let grid_width = self.width() * self.zoom as u32; + let grid_height = self.height() * self.zoom as u32; + self.start = Point::new( + (winsize_x as i32 - grid_width as i32) / 2, + (winsize_y as i32 - grid_height as i32) / 2, + ); + } + fn increase_brush_size(&mut self) { self.brush_size += 1; } @@ -263,20 +287,6 @@ impl<'ctx> AppState<'ctx> { self.mode = Mode::Draw; } - fn zoom_out(&mut self, p: (i32, i32)) { - if self.zoom > 1 { - // attempt to center around cursor - if let Some(p) = self.idx_at_coord(p) { - let (x1, y1) = (p.0 * (self.zoom as u32), p.1 * (self.zoom as u32)); - let (x2, y2) = (p.0 * (self.zoom as u32 - 1), p.1 * (self.zoom as u32 - 1)); - let diffx = x2 as i32 - x1 as i32; - let diffy = y2 as i32 - y1 as i32; - self.start = self.start - Point::from((diffx, diffy)); - } - self.zoom -= 1; - } - } - fn draw_grid(&mut self) { let cs = self.zoom as u32; let (width, height) = (self.width(), self.height()); @@ -523,7 +533,9 @@ impl<'ctx> AppState<'ctx> { Mode::Draw => { match event { Event::KeyDown { - keycode: Some(k), .. + keycode: Some(k), + keymod, + .. } => { match k { // pan @@ -532,6 +544,9 @@ impl<'ctx> AppState<'ctx> { Keycode::S => self.pan((0, -10)), Keycode::D => self.pan((-10, 0)), // zoom + Keycode::C if keymod == Mod::LSHIFTMOD => { + self.center_grid(); + } Keycode::C => { let cursor = (mouse.x(), mouse.y()); self.zoom_in(cursor); @@ -647,11 +662,14 @@ impl<'ctx> AppState<'ctx> { let video = self.context.video().unwrap(); let clipboard = video.clipboard(); if is_copy_event(keycode, keymod) { - clipboard.set_clipboard_text(&self.command_box.text); + clipboard + .set_clipboard_text(&self.command_box.text) + .unwrap(); } else if is_paste_event(keycode, keymod) && clipboard.has_clipboard_text() { - self.command_box.text = clipboard.clipboard_text().unwrap(); + self.command_box + .push_str(&clipboard.clipboard_text().unwrap()); } } if let Event::KeyDown { -- cgit v1.2.3