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 +++++++++++++++++++++++++++++++++++++++------------ src/main.rs | 4 +++- src/message.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 src/message.rs (limited to 'src') 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; } _ => (), diff --git a/src/main.rs b/src/main.rs index ebdf793..197b200 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ mod bitmap; mod command; mod consts; mod dither; +mod lisp; +mod message; mod symmetry; mod undo; mod utils; @@ -22,7 +24,7 @@ pub fn main() { let ttf_context = sdl2::ttf::init().unwrap(); let args: Vec<_> = env::args().collect(); if args.len() < 2 { - AppState::init(200, 200, &sdl_context, &ttf_context, None).run(); + AppState::init(160, 160, &sdl_context, &ttf_context, None).run(); return; } else { let path = args.get(1).unwrap(); diff --git a/src/message.rs b/src/message.rs new file mode 100644 index 0000000..d1032cf --- /dev/null +++ b/src/message.rs @@ -0,0 +1,42 @@ +#[derive(Debug)] +pub struct Message { + pub text: String, + pub kind: MessageKind, +} + +impl Message { + pub fn new() -> Self { + Self { + text: String::new(), + kind: MessageKind::Info, + } + } + pub fn kind(mut self, kind: MessageKind) -> Self { + self.kind = kind; + self + } + pub fn text>(mut self, text: S) -> Self { + self.text = text.as_ref().into(); + self + } +} + +#[derive(Debug, Copy, Clone)] +pub enum MessageKind { + Error, + Info, + Hint, + LispResult, +} + +impl From for Message +where + T: AsRef, +{ + fn from(item: T) -> Self { + return Message { + text: item.as_ref().into(), + kind: MessageKind::Info, + }; + } +} -- cgit v1.2.3