From 733a7df549aa7bb7a7bb727a25235f25db875ecd Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 31 Mar 2021 20:22:04 +0530 Subject: introduce basic canvas primitive functions --- src/lisp/prelude.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'src/lisp/prelude.rs') diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index 54c831f..4a6a4ed 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -1,4 +1,5 @@ use crate::{ + bitmap::MapPoint, brush::Brush, lisp::{ error::{EvalError, LispError}, @@ -7,6 +8,7 @@ use crate::{ Environment, }, primitive, + undo::PaintRecord, utils::load_script, }; @@ -39,7 +41,8 @@ macro_rules! type_match { { let mut temp_vec = vec![]; $( - for arg in &$args[$range] { + let arg_range: &[LispExpr] = &$args[$range]; + for arg in arg_range { temp_vec.push(matches!(arg, $kind)); } )+ @@ -334,5 +337,44 @@ pub fn new_env() -> Result { } }); + primitive!(env, Arity::Exact(0), "canvas-width", |_, app| { + return Ok(LispExpr::Number(LispNumber::Integer(app.width() as i64))); + }); + + primitive!(env, Arity::Exact(0), "canvas-height", |_, app| { + return Ok(LispExpr::Number(LispNumber::Integer(app.height() as i64))); + }); + + primitive!(env, Arity::Exact(3), "set-pixel!", |args, app| { + if type_match!( + args, 0 => LispExpr::Number(LispNumber::Integer(_)), + 1 => LispExpr::Number(LispNumber::Integer(_)), + 2 => LispExpr::BoolLit(_) + ) { + let x = args[0].unwrap_number(); + let y = args[1].unwrap_number(); + let val = args[2].cast_bool(); + + let set_loc: MapPoint = (x.unwrap_integer(), y.unwrap_integer()) + .try_into() + .map_err(|_| -> LispError { EvalError::InvalidCoordinates((x, y)).into() })?; + if !app.pixmap.contains(set_loc) { + return Err(EvalError::InvalidCoordinates((x, y)).into()); + } else { + let old_val = app.pixmap.set(set_loc, val); + app.current_operation + .push(PaintRecord::new(set_loc, old_val, val)); + return Ok(LispExpr::Unit); + } + } else { + return Err(EvalError::TypeMismatch.into()); + } + }); + + primitive!(env, Arity::Exact(0), "commit", |_, app| { + app.commit_operation(); + return Ok(LispExpr::Unit); + }); + Ok(env) } -- cgit v1.2.3