From 591d2b6167af53ce07b060711a4074f1e19c5f3f Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 8 May 2021 21:25:47 +0530 Subject: add basic user-definable keybinds --- src/app.rs | 69 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 22 deletions(-) (limited to 'src/app.rs') diff --git a/src/app.rs b/src/app.rs index 70d68b8..e69794f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -8,7 +8,8 @@ use crate::{ error::AppError, grid::Grid, guide::Guide, - lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, + keybind::Keybind, + lisp::{eval, expr::LispExpr, lex::Lexer, parse::Parser, prelude, EnvList}, message::Message, rect, symmetry::Symmetry, @@ -59,6 +60,7 @@ pub struct AppState<'ctx> { pub grid: Grid, pub minimap: bool, pub lisp_env: EnvList, + pub keybinds: HashMap, pub message: Message, pub mode: Mode, pub mouse: (i32, i32), @@ -303,21 +305,23 @@ impl<'ctx> AppState<'ctx> { } } + pub fn eval_expr(&mut self, expr: &LispExpr) { + let mut evaluator = eval::Evaluator { + app: self, + context: Vec::new(), + }; + match evaluator.eval(expr) { + Ok(val) => self.message.set_info(format!("{}", val)), + Err(eval_err) => self.message.set_error(format!("{}", eval_err)), + } + } + pub fn eval_command(&mut self) { let lisp_expr = &self.command_box.text; let mut parser = Parser::new(Lexer::new(lisp_expr)); let res = parser.parse_single_expr(); match res { - Ok(expr) => { - let mut evaluator = eval::Evaluator { - app: self, - context: Vec::new(), - }; - match evaluator.eval(&expr) { - Ok(val) => self.message.set_info(format!("{}", val)), - Err(eval_err) => self.message.set_error(format!("{}", eval_err)), - } - } + Ok(expr) => self.eval_expr(&expr), Err(err) => self.message = handle_error(err, &lisp_expr, "repl"), } self.command_box.hist_append(); @@ -612,11 +616,11 @@ impl<'ctx> AppState<'ctx> { self.grid .draw(&mut self.canvas, self.zoom, &self.start, width, height); } + self.draw_guides(); + self.draw_symmetry(); if self.minimap { self.draw_minimap(); } - self.draw_guides(); - self.draw_symmetry(); self.draw_statusline(); self.draw_command_box(); self.draw_brush(); @@ -663,23 +667,24 @@ impl<'ctx> AppState<'ctx> { let mut app = Self { active_color: true, brush: Brush::new(0), + cache: RefCell::new(None), canvas, command_box: CommandBox::new(), context, - cache: RefCell::new(None), - guides: HashMap::new(), current_operation: Vec::new(), dither_level: 16, file_name, grid: Grid::new(), + guides: HashMap::new(), + keybinds: HashMap::new(), lisp_env: vec![prelude::new_env().map_err(AppError::Lisp)?], message: Message::new().text(" "), - mode: Mode::Draw, minimap: false, + mode: Mode::Draw, mouse: (0, 0), + pan_start: Point::new(0, 0), pixmap, start: Point::new(60, 60), - pan_start: Point::new(0, 0), symmetry: Default::default(), ttf_context, undo_stack: UndoStack::new(), @@ -732,7 +737,7 @@ impl<'ctx> AppState<'ctx> { match event { Event::KeyDown { keycode: Some(k), - keymod, + keymod: Mod::NOMOD, .. } => { match k { @@ -742,9 +747,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 if keymod == Mod::LSHIFTMOD => { + // self.center_grid(); + // } Keycode::C => { let cursor = (mouse.x(), mouse.y()); self.zoom_in(cursor); @@ -799,7 +804,7 @@ impl<'ctx> AppState<'ctx> { } continue; } - _ if keymod == Mod::LCTRLMOD || keymod == Mod::RCTRLMOD => { + Keycode::LCtrl | Keycode::RCtrl => { self.brush = Brush::line( self.cache .borrow() @@ -812,6 +817,26 @@ impl<'ctx> AppState<'ctx> { _ => (), } } + Event::KeyDown { + keycode: Some(k), + keymod, + .. + } if self.keybinds.contains_key(&Keybind::new(k, keymod)) => { + let body = self.keybinds.get(&Keybind::new(k, keymod)).unwrap(); + self.eval_expr(&body.clone()); + } + Event::KeyDown { + keycode: Some(k), .. + } if k == Keycode::LCtrl || k == Keycode::RCtrl => { + self.brush = Brush::line( + self.cache + .borrow() + .as_ref() + .map(|c| c.last_brush.size().unwrap_or(0)) + .unwrap_or(0), + true, + ); + } Event::KeyUp { keycode: Some(k), .. } if k == Keycode::LCtrl || k == Keycode::RCtrl => { -- cgit v1.2.3