From 0ecfaf7b7cf762590d5ba92f57567901c02af15b Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 23 Mar 2021 13:09:41 +0530 Subject: implement message display and types --- src/app.rs | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'src/app.rs') diff --git a/src/app.rs b/src/app.rs index f6e2b96..9730cf2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,7 +2,10 @@ use crate::{ bitmap::{MapPoint, Pixmap}, command::CommandBox, consts::{colors::*, FONT_PATH}, - dither, rect, + dither, + lisp::{env, lex::Lexer, parse::Parser, Environment}, + message::Message, + rect, symmetry::Symmetry, undo::{ModifyRecord, OpKind, Operation, UndoStack}, utils::{draw_text, is_copy_event, is_paste_event}, @@ -274,6 +277,18 @@ impl<'ctx> AppState<'ctx> { } fn eval_command(&mut self) { + let lisp_expr = &self.command_box.text; + let mut parser = Parser::new(Lexer::new(lisp_expr, 0)); + let res = parser.parse_single_expr(); + match env::eval(&res.unwrap(), self) { + Ok(val) => { + self.message.text = format!("{}", val); + } + Err(_) => { + self.message.text = format!("Lisp Error!"); + } + } + if let Some(path) = self.command_box.text.strip_prefix("(save ") { let image = self.export(); let encoded = image.encode().unwrap(); @@ -345,22 +360,31 @@ impl<'ctx> AppState<'ctx> { } fn draw_command_box(&mut self) { - if self.command_box.is_empty() { - self.mode = Mode::Draw; - return; - } let (winsize_x, winsize_y) = self.canvas.window().size(); let cmd_height: u32 = 20; let cmd_width = winsize_x; - self.canvas.set_draw_color(WHITE); + self.canvas.set_draw_color(BLACK); self.canvas .fill_rect(rect!(0, winsize_y - cmd_height, cmd_width, cmd_height)) .unwrap(); + if self.command_box.is_empty() { + self.mode = Mode::Draw; + // show msg + draw_text( + &mut self.canvas, + self.ttf_context, + &self.message.text[..], + WHITE, + (0, winsize_y - cmd_height), + ); + return; + } + // show repl draw_text( &mut self.canvas, self.ttf_context, &self.command_box.text[..], - BLACK, + WHITE, (0, winsize_y - cmd_height), ); @@ -438,11 +462,11 @@ impl<'ctx> AppState<'ctx> { .draw_line((line_coord, 0), (line_coord, winsize_y as i32)) .unwrap(); } - if self.mode == Mode::Draw { - self.draw_statusline(); - } else { - self.draw_command_box(); - } + // if self.mode == Mode::Draw { + // self.draw_statusline(); + // } else { + self.draw_command_box(); + // } self.draw_mouse(); } @@ -493,12 +517,14 @@ impl<'ctx> AppState<'ctx> { grid: Grid::new(), last_point: None, mode: Mode::Draw, + message: Message::new().text(" "), mouse: (0, 0), pixmap, start: Point::new(60, 60), symmetry: Default::default(), ttf_context, undo_stack: UndoStack::new(), + lisp_env: env::with_prelude(), zoom: 5, } } @@ -700,6 +726,7 @@ impl<'ctx> AppState<'ctx> { Keycode::Return => self.eval_command(), Keycode::Escape => { self.command_box.clear(); + self.message.text = format!(" "); self.mode = Mode::Draw; } _ => (), -- cgit v1.2.3