diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 27 | ||||
-rw-r--r-- | src/consts.rs | 1 |
2 files changed, 22 insertions, 6 deletions
@@ -2,7 +2,7 @@ use crate::{ | |||
2 | bitmap::{positive_angle_with_x, MapPoint, Pixmap}, | 2 | bitmap::{positive_angle_with_x, MapPoint, Pixmap}, |
3 | brush::{Brush, CircleBrush, LineBrush}, | 3 | brush::{Brush, CircleBrush, LineBrush}, |
4 | command::CommandBox, | 4 | command::CommandBox, |
5 | consts::{colors::*, FONT_PATH, STDLIB_PATH}, | 5 | consts::{colors::*, FONT_PATH, RC_PATH, STDLIB_PATH}, |
6 | dither, | 6 | dither, |
7 | error::AppError, | 7 | error::AppError, |
8 | lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, | 8 | lisp::{eval, lex::Lexer, parse::Parser, prelude, EnvList}, |
@@ -13,7 +13,12 @@ use crate::{ | |||
13 | utils::{draw_text, handle_error, is_copy_event, is_paste_event, load_script}, | 13 | utils::{draw_text, handle_error, is_copy_event, is_paste_event, load_script}, |
14 | }; | 14 | }; |
15 | 15 | ||
16 | use std::{convert::From, fs::File, io::prelude::*, path::Path}; | 16 | use std::{ |
17 | convert::From, | ||
18 | fs::File, | ||
19 | io::prelude::*, | ||
20 | path::{Path, PathBuf}, | ||
21 | }; | ||
17 | 22 | ||
18 | use obi::Image; | 23 | use obi::Image; |
19 | use sdl2::{ | 24 | use sdl2::{ |
@@ -34,7 +39,7 @@ pub enum Mode { | |||
34 | Command, | 39 | Command, |
35 | } | 40 | } |
36 | 41 | ||
37 | pub struct AppState<'ctx, 'file> { | 42 | pub struct AppState<'ctx> { |
38 | pub active_color: bool, | 43 | pub active_color: bool, |
39 | pub brush: Brush, | 44 | pub brush: Brush, |
40 | pub canvas: Canvas<Window>, | 45 | pub canvas: Canvas<Window>, |
@@ -42,7 +47,7 @@ pub struct AppState<'ctx, 'file> { | |||
42 | pub context: &'ctx Sdl, | 47 | pub context: &'ctx Sdl, |
43 | pub current_operation: Vec<PaintRecord>, | 48 | pub current_operation: Vec<PaintRecord>, |
44 | pub dither_level: u8, | 49 | pub dither_level: u8, |
45 | pub file_name: Option<&'file Path>, | 50 | pub file_name: Option<PathBuf>, |
46 | pub grid: Grid, | 51 | pub grid: Grid, |
47 | pub lisp_env: EnvList, | 52 | pub lisp_env: EnvList, |
48 | pub message: Message, | 53 | pub message: Message, |
@@ -71,7 +76,7 @@ impl Grid { | |||
71 | } | 76 | } |
72 | 77 | ||
73 | // private actions on appstate | 78 | // private actions on appstate |
74 | impl<'ctx, 'file> AppState<'ctx, 'file> { | 79 | impl<'ctx> AppState<'ctx> { |
75 | fn pan<P: Into<Point>>(&mut self, direction: P) { | 80 | fn pan<P: Into<Point>>(&mut self, direction: P) { |
76 | self.start += direction.into(); | 81 | self.start += direction.into(); |
77 | } | 82 | } |
@@ -516,7 +521,7 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { | |||
516 | context: &'ctx Sdl, | 521 | context: &'ctx Sdl, |
517 | ttf_context: &'ctx Sdl2TtfContext, | 522 | ttf_context: &'ctx Sdl2TtfContext, |
518 | start_data: Option<Vec<bool>>, | 523 | start_data: Option<Vec<bool>>, |
519 | file_name: Option<&'file Path>, | 524 | file_name: Option<PathBuf>, |
520 | ) -> Result<Self, AppError> { | 525 | ) -> Result<Self, AppError> { |
521 | let video_subsystem = context.video().map_err(AppError::Sdl)?; | 526 | let video_subsystem = context.video().map_err(AppError::Sdl)?; |
522 | 527 | ||
@@ -557,6 +562,9 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { | |||
557 | zoom: 5, | 562 | zoom: 5, |
558 | }; | 563 | }; |
559 | load_script(STDLIB_PATH, &mut app).map_err(AppError::Lisp)?; | 564 | load_script(STDLIB_PATH, &mut app).map_err(AppError::Lisp)?; |
565 | std::fs::create_dir_all(RC_PATH).map_err(AppError::File)?; | ||
566 | load_script([RC_PATH, "rc.lisp"].iter().collect::<PathBuf>(), &mut app) | ||
567 | .map_err(AppError::Lisp)?; | ||
560 | Ok(app) | 568 | Ok(app) |
561 | } | 569 | } |
562 | 570 | ||
@@ -566,6 +574,13 @@ impl<'ctx, 'file> AppState<'ctx, 'file> { | |||
566 | image | 574 | image |
567 | } | 575 | } |
568 | 576 | ||
577 | pub fn save_as<P: AsRef<Path>>(&self, file_name: P) -> Result<(), AppError> { | ||
578 | let image = self.export().encode().unwrap(); | ||
579 | let mut file = File::create(file_name).map_err(AppError::File)?; | ||
580 | file.write_all(&image[..]).unwrap(); | ||
581 | return Ok(()); | ||
582 | } | ||
583 | |||
569 | pub fn run(&mut self) { | 584 | pub fn run(&mut self) { |
570 | self.canvas.set_draw_color(BLACK); | 585 | self.canvas.set_draw_color(BLACK); |
571 | self.canvas.clear(); | 586 | self.canvas.clear(); |
diff --git a/src/consts.rs b/src/consts.rs index 52ab580..3f9ecb9 100644 --- a/src/consts.rs +++ b/src/consts.rs | |||
@@ -10,3 +10,4 @@ pub mod colors { | |||
10 | 10 | ||
11 | pub const FONT_PATH: &'static str = "./assets/NerdInput-Regular.ttf"; | 11 | pub const FONT_PATH: &'static str = "./assets/NerdInput-Regular.ttf"; |
12 | pub const STDLIB_PATH: &'static str = "./src/lisp/std.lisp"; | 12 | pub const STDLIB_PATH: &'static str = "./src/lisp/std.lisp"; |
13 | pub const RC_PATH: &'static str = "/home/np/.config/sdl-tests"; | ||