diff options
author | Akshay <[email protected]> | 2021-03-25 07:38:25 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-03-25 07:38:25 +0000 |
commit | 8171b30adbc4cddd2c51f043c3379d78428666b8 (patch) | |
tree | 85e34bef77d330a5df52eef3bb2f249453329934 /src/app.rs | |
parent | 5a232822a25604e669740a803eea8d9889772d74 (diff) |
use new error kinds; track Environment nesting with stack
Diffstat (limited to 'src/app.rs')
-rw-r--r-- | src/app.rs | 62 |
1 files changed, 35 insertions, 27 deletions
@@ -3,7 +3,7 @@ use crate::{ | |||
3 | command::CommandBox, | 3 | command::CommandBox, |
4 | consts::{colors::*, FONT_PATH}, | 4 | consts::{colors::*, FONT_PATH}, |
5 | dither, | 5 | dither, |
6 | lisp::{eval, lex::Lexer, parse::Parser, Environment}, | 6 | lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList, Environment}, |
7 | message::Message, | 7 | message::Message, |
8 | rect, | 8 | rect, |
9 | symmetry::Symmetry, | 9 | symmetry::Symmetry, |
@@ -11,7 +11,12 @@ use crate::{ | |||
11 | utils::{draw_text, is_copy_event, is_paste_event}, | 11 | utils::{draw_text, is_copy_event, is_paste_event}, |
12 | }; | 12 | }; |
13 | 13 | ||
14 | use std::{convert::From, fs::File, io::prelude::*}; | 14 | use std::{ |
15 | convert::From, | ||
16 | fs::File, | ||
17 | io::prelude::*, | ||
18 | path::{Path, PathBuf}, | ||
19 | }; | ||
15 | 20 | ||
16 | use obi::Image; | 21 | use obi::Image; |
17 | use sdl2::{ | 22 | use sdl2::{ |
@@ -32,7 +37,7 @@ pub enum Mode { | |||
32 | Command, | 37 | Command, |
33 | } | 38 | } |
34 | 39 | ||
35 | pub struct AppState<'ctx> { | 40 | pub struct AppState<'ctx, 'file> { |
36 | pub active_color: bool, | 41 | pub active_color: bool, |
37 | pub brush_size: u8, | 42 | pub brush_size: u8, |
38 | pub canvas: Canvas<Window>, | 43 | pub canvas: Canvas<Window>, |
@@ -40,9 +45,10 @@ pub struct AppState<'ctx> { | |||
40 | pub context: &'ctx Sdl, | 45 | pub context: &'ctx Sdl, |
41 | pub current_operation: Operation, | 46 | pub current_operation: Operation, |
42 | pub dither_level: u8, | 47 | pub dither_level: u8, |
48 | pub file_name: Option<&'file Path>, | ||
43 | pub grid: Grid, | 49 | pub grid: Grid, |
44 | pub last_point: Option<MapPoint>, | 50 | pub last_point: Option<MapPoint>, |
45 | pub lisp_env: Environment, | 51 | pub lisp_env: EnvList, |
46 | pub message: Message, | 52 | pub message: Message, |
47 | pub mode: Mode, | 53 | pub mode: Mode, |
48 | pub mouse: (i32, i32), | 54 | pub mouse: (i32, i32), |
@@ -69,7 +75,7 @@ impl Grid { | |||
69 | } | 75 | } |
70 | 76 | ||
71 | // private actions on appstate | 77 | // private actions on appstate |
72 | impl<'ctx> AppState<'ctx> { | 78 | impl<'ctx, 'file> AppState<'ctx, 'file> { |
73 | fn pan<P: Into<Point>>(&mut self, direction: P) { | 79 | fn pan<P: Into<Point>>(&mut self, direction: P) { |
74 | self.start += direction.into(); | 80 | self.start += direction.into(); |
75 | } | 81 | } |
@@ -280,22 +286,22 @@ impl<'ctx> AppState<'ctx> { | |||
280 | let lisp_expr = &self.command_box.text; | 286 | let lisp_expr = &self.command_box.text; |
281 | let mut parser = Parser::new(Lexer::new(lisp_expr, 0)); | 287 | let mut parser = Parser::new(Lexer::new(lisp_expr, 0)); |
282 | let res = parser.parse_single_expr(); | 288 | let res = parser.parse_single_expr(); |
283 | match eval::eval(&res.unwrap(), self, None) { | 289 | match res { |
284 | Ok(val) => { | 290 | Ok(expr) => match eval::eval(&expr, self) { |
285 | self.message.text = format!("{}", val); | 291 | Ok(val) => self.message.set_info(format!("{}", val)), |
286 | } | 292 | Err(eval_err) => self.message.set_error(format!("{}", eval_err)), |
287 | Err(_) => { | 293 | }, |
288 | self.message.text = format!("Lisp Error!"); | 294 | Err(parse_err) => self.message.set_error(format!("{}", parse_err)), |
289 | } | ||
290 | } | ||
291 | |||
292 | if let Some(path) = self.command_box.text.strip_prefix("(save ") { | ||
293 | let image = self.export(); | ||
294 | let encoded = image.encode().unwrap(); | ||
295 | let mut buffer = File::create(path).unwrap(); | ||
296 | buffer.write_all(&encoded[..]).unwrap(); | ||
297 | self.command_box.hist_append(); | ||
298 | } | 295 | } |
296 | self.command_box.hist_append(); | ||
297 | |||
298 | // if let Some(path) = self.command_box.text.strip_prefix("(save ") { | ||
299 | // let image = self.export(); | ||
300 | // let encoded = image.encode().unwrap(); | ||
301 | // let mut buffer = File::create(path).unwrap(); | ||
302 | // buffer.write_all(&encoded[..]).unwrap(); | ||
303 | // self.command_box.hist_append(); | ||
304 | // } | ||
299 | self.command_box.clear(); | 305 | self.command_box.clear(); |
300 | self.mode = Mode::Draw; | 306 | self.mode = Mode::Draw; |
301 | } | 307 | } |
@@ -371,7 +377,7 @@ impl<'ctx> AppState<'ctx> { | |||
371 | &mut self.canvas, | 377 | &mut self.canvas, |
372 | self.ttf_context, | 378 | self.ttf_context, |
373 | &self.message.text[..], | 379 | &self.message.text[..], |
374 | WHITE, | 380 | self.message.kind.into(), |
375 | (0, winsize_y - cmd_height), | 381 | (0, winsize_y - cmd_height), |
376 | ); | 382 | ); |
377 | return; | 383 | return; |
@@ -473,18 +479,19 @@ impl<'ctx> AppState<'ctx> { | |||
473 | 479 | ||
474 | pub fn quit(&mut self) { | 480 | pub fn quit(&mut self) { |
475 | let ev = self.context.event().unwrap(); | 481 | let ev = self.context.event().unwrap(); |
476 | ev.push_event(Event::Quit { timestamp: 0u32 }); | 482 | ev.push_event(Event::Quit { timestamp: 0u32 }) |
483 | .expect("ohno unable to quit"); | ||
477 | } | 484 | } |
478 | } | 485 | } |
479 | 486 | ||
480 | // publicly available functions on appstate | 487 | impl<'ctx, 'file> AppState<'ctx, 'file> { |
481 | impl<'ctx> AppState<'ctx> { | ||
482 | pub fn init( | 488 | pub fn init( |
483 | width: u32, | 489 | width: u32, |
484 | height: u32, | 490 | height: u32, |
485 | context: &'ctx Sdl, | 491 | context: &'ctx Sdl, |
486 | ttf_context: &'ctx Sdl2TtfContext, | 492 | ttf_context: &'ctx Sdl2TtfContext, |
487 | start_data: Option<Vec<bool>>, | 493 | start_data: Option<Vec<bool>>, |
494 | file_name: Option<&'file Path>, | ||
488 | ) -> Self { | 495 | ) -> Self { |
489 | let video_subsystem = context.video().unwrap(); | 496 | let video_subsystem = context.video().unwrap(); |
490 | 497 | ||
@@ -509,21 +516,22 @@ impl<'ctx> AppState<'ctx> { | |||
509 | active_color: true, | 516 | active_color: true, |
510 | brush_size: 0, | 517 | brush_size: 0, |
511 | canvas, | 518 | canvas, |
512 | context, | ||
513 | command_box: CommandBox::new(), | 519 | command_box: CommandBox::new(), |
520 | context, | ||
514 | current_operation: Vec::new(), | 521 | current_operation: Vec::new(), |
515 | dither_level: 16, | 522 | dither_level: 16, |
523 | file_name, | ||
516 | grid: Grid::new(), | 524 | grid: Grid::new(), |
517 | last_point: None, | 525 | last_point: None, |
518 | mode: Mode::Draw, | 526 | lisp_env: vec![prelude::new_env()], |
519 | message: Message::new().text(" "), | 527 | message: Message::new().text(" "), |
528 | mode: Mode::Draw, | ||
520 | mouse: (0, 0), | 529 | mouse: (0, 0), |
521 | pixmap, | 530 | pixmap, |
522 | start: Point::new(60, 60), | 531 | start: Point::new(60, 60), |
523 | symmetry: Default::default(), | 532 | symmetry: Default::default(), |
524 | ttf_context, | 533 | ttf_context, |
525 | undo_stack: UndoStack::new(), | 534 | undo_stack: UndoStack::new(), |
526 | lisp_env: eval::with_prelude(), | ||
527 | zoom: 5, | 535 | zoom: 5, |
528 | } | 536 | } |
529 | } | 537 | } |