From abcf2b32777ffb934788e3219cacc2bbc048b6a3 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 11 Apr 2021 15:31:32 +0530 Subject: add `selection-start` and `selection-end` primitives --- .direnv/flake-profile | 2 +- .direnv/flake-profile.rc | 4 +- flake.nix | 104 +++++++++++++++++++++++------------------------ src/app.rs | 89 +++++++++++++++++++++++----------------- src/lisp/eval.rs | 13 +++--- src/lisp/expr.rs | 6 +++ src/lisp/number.rs | 7 ++++ src/lisp/prelude.rs | 38 ++++++++++++++++- src/utils.rs | 5 ++- 9 files changed, 165 insertions(+), 103 deletions(-) diff --git a/.direnv/flake-profile b/.direnv/flake-profile index caea00d..5d2798d 120000 --- a/.direnv/flake-profile +++ b/.direnv/flake-profile @@ -1 +1 @@ -/nix/store/9dsqn6si93g5gs60rlka4xjcx36m5jyj-nix-shell-env \ No newline at end of file +/nix/store/w4hwpczf7xldw1qvgmwc63j4yzrr14nh-nix-shell-env \ No newline at end of file diff --git a/.direnv/flake-profile.rc b/.direnv/flake-profile.rc index 81165b8..40c99ee 100644 --- a/.direnv/flake-profile.rc +++ b/.direnv/flake-profile.rc @@ -32,7 +32,7 @@ NIX_CC=/nix/store/ca37d3qrydh0wpw40kswsx30j8dyzxh2-gcc-wrapper-10.2.0 export NIX_CC NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu=1 export NIX_CC_WRAPPER_TARGET_HOST_x86_64_unknown_linux_gnu -NIX_CFLAGS_COMPILE=' -frandom-seed=9dsqn6si93 -isystem /nix/store/gkqv9ia81dmmjqcn46ih5yirbzjy7s9p-SDL2-2.0.14-dev/include -isystem /nix/store/5sphs39wg8gyy5pcrcml5pyj1wrn9jil-libGL-1.3.2-dev/include -isystem /nix/store/skwk7hmzwpjvhgjf1y633l01p114xc6n-libglvnd-1.3.2-dev/include -isystem /nix/store/3y6j0k82fdp3ifnq655yv1ad1ih7r70j-libX11-1.7.0-dev/include -isystem /nix/store/vjimy2jlifszmmms664373jdw0yiz4ln-xorgproto-2020.1/include -isystem /nix/store/6xg7mhwh2k7i27r44lx679diwd7ahq6d-libxcb-1.14-dev/include -isystem /nix/store/qx325fjgyhy5cql0fkbkzzbi9dpd2cb8-SDL2_ttf-2.0.15/include -isystem /nix/store/gkqv9ia81dmmjqcn46ih5yirbzjy7s9p-SDL2-2.0.14-dev/include -isystem /nix/store/5sphs39wg8gyy5pcrcml5pyj1wrn9jil-libGL-1.3.2-dev/include -isystem /nix/store/skwk7hmzwpjvhgjf1y633l01p114xc6n-libglvnd-1.3.2-dev/include -isystem /nix/store/3y6j0k82fdp3ifnq655yv1ad1ih7r70j-libX11-1.7.0-dev/include -isystem /nix/store/vjimy2jlifszmmms664373jdw0yiz4ln-xorgproto-2020.1/include -isystem /nix/store/6xg7mhwh2k7i27r44lx679diwd7ahq6d-libxcb-1.14-dev/include -isystem /nix/store/qx325fjgyhy5cql0fkbkzzbi9dpd2cb8-SDL2_ttf-2.0.15/include' +NIX_CFLAGS_COMPILE=' -frandom-seed=w4hwpczf7x -isystem /nix/store/gkqv9ia81dmmjqcn46ih5yirbzjy7s9p-SDL2-2.0.14-dev/include -isystem /nix/store/5sphs39wg8gyy5pcrcml5pyj1wrn9jil-libGL-1.3.2-dev/include -isystem /nix/store/skwk7hmzwpjvhgjf1y633l01p114xc6n-libglvnd-1.3.2-dev/include -isystem /nix/store/3y6j0k82fdp3ifnq655yv1ad1ih7r70j-libX11-1.7.0-dev/include -isystem /nix/store/vjimy2jlifszmmms664373jdw0yiz4ln-xorgproto-2020.1/include -isystem /nix/store/6xg7mhwh2k7i27r44lx679diwd7ahq6d-libxcb-1.14-dev/include -isystem /nix/store/qx325fjgyhy5cql0fkbkzzbi9dpd2cb8-SDL2_ttf-2.0.15/include -isystem /nix/store/gkqv9ia81dmmjqcn46ih5yirbzjy7s9p-SDL2-2.0.14-dev/include -isystem /nix/store/5sphs39wg8gyy5pcrcml5pyj1wrn9jil-libGL-1.3.2-dev/include -isystem /nix/store/skwk7hmzwpjvhgjf1y633l01p114xc6n-libglvnd-1.3.2-dev/include -isystem /nix/store/3y6j0k82fdp3ifnq655yv1ad1ih7r70j-libX11-1.7.0-dev/include -isystem /nix/store/vjimy2jlifszmmms664373jdw0yiz4ln-xorgproto-2020.1/include -isystem /nix/store/6xg7mhwh2k7i27r44lx679diwd7ahq6d-libxcb-1.14-dev/include -isystem /nix/store/qx325fjgyhy5cql0fkbkzzbi9dpd2cb8-SDL2_ttf-2.0.15/include' export NIX_CFLAGS_COMPILE NIX_ENFORCE_NO_NATIVE=1 export NIX_ENFORCE_NO_NATIVE @@ -67,7 +67,7 @@ RUST_BACKTRACE=1 export RUST_BACKTRACE RUST_LOG=info export RUST_LOG -RUST_SRC_PATH=/nix/store/wfv522w143y3yxx8ap3fkpjk9cnh1w3a-rust-lib-src +RUST_SRC_PATH=/nix/store/znf8wv5xxzrbq7jq0bxmjxsimgnbxnn8-rust-src-1.53.0-nightly-2021-03-30-74874a690/lib/rustlib/src/rust/library export RUST_SRC_PATH SDL2_PATH='/nix/store/gkqv9ia81dmmjqcn46ih5yirbzjy7s9p-SDL2-2.0.14-dev/include/SDL2 /nix/store/qx325fjgyhy5cql0fkbkzzbi9dpd2cb8-SDL2_ttf-2.0.15/include/SDL2 /nix/store/gkqv9ia81dmmjqcn46ih5yirbzjy7s9p-SDL2-2.0.14-dev/include/SDL2 /nix/store/qx325fjgyhy5cql0fkbkzzbi9dpd2cb8-SDL2_ttf-2.0.15/include/SDL2 /nix/store/gkqv9ia81dmmjqcn46ih5yirbzjy7s9p-SDL2-2.0.14-dev/include/SDL2 /nix/store/qx325fjgyhy5cql0fkbkzzbi9dpd2cb8-SDL2_ttf-2.0.15/include/SDL2' export SDL2_PATH diff --git a/flake.nix b/flake.nix index 07a9a06..2acc1f3 100644 --- a/flake.nix +++ b/flake.nix @@ -6,9 +6,9 @@ url = "github:mozilla/nixpkgs-mozilla"; flake = false; }; - gitignore = { - url = "github:hercules-ci/gitignore"; - flake=false; + gitignore = { + url = "github:hercules-ci/gitignore"; + flake = false; }; flake-compat = { url = "github:edolstra/flake-compat"; @@ -17,59 +17,57 @@ }; outputs = { self, nixpkgs, utils, naersk, mozillapkgs, gitignore, ... }: - utils.lib.eachDefaultSystem (system: - let - pkgs = nixpkgs.legacyPackages."${system}"; - inherit (import gitignore { inherit (pkgs) lib; }) gitignoreSource; - - # Get a specific rust version - mozilla = pkgs.callPackage (mozillapkgs + "/package-set.nix") {}; + utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages."${system}"; + inherit (import gitignore { inherit (pkgs) lib; }) gitignoreSource; - rust = (mozilla.rustChannelOf { - date = "2021-03-31"; - channel = "nightly"; - sha256 = "oK5ebje09MRn988saJMT3Zze/tRE7u9zTeFPV1CEeLc="; # set zeros after modifying channel or date - }).rust; + # Get a specific rust version + mozilla = pkgs.callPackage (mozillapkgs + "/package-set.nix") { }; + chanspec = { + date = "2021-03-31"; + channel = "nightly"; + sha256 = "oK5ebje09MRn988saJMT3Zze/tRE7u9zTeFPV1CEeLc="; # set zeros after modifying channel or date + }; - rust-src = (mozilla.rustChannelOf { - date = "2021-03-31"; - channel = "nightly"; - sha256 = "oK5ebje09MRn988saJMT3Zze/tRE7u9zTeFPV1CEeLc="; # set zeros after modifying channel or date - }).rust-src; + rustChannel = mozilla.rustChannelOf chanspec; + rust = rustChannel.rust; + rust-src = rustChannel.rust-src; - naersk-lib = naersk.lib."${system}".override { - cargo = rust; - rustc = rust; - }; + naersk-lib = naersk.lib."${system}".override { + cargo = rust; + rustc = rust; + }; - nativeBuildInputs = with pkgs; [ - SDL2 - SDL2_ttf - ]; + nativeBuildInputs = with pkgs; [ + SDL2 + SDL2_ttf + ]; - in rec { - packages.my-project = naersk-lib.buildPackage { - pname = "sdl-tests"; - version = "0.1.0"; - root = gitignoreSource ./.; - inherit nativeBuildInputs; - }; - defaultPackage = packages.my-project; - apps.my-project = utils.lib.mkApp { - drv = packages.my-project; - }; - defaultApp = apps.my-project; - devShell = pkgs.mkShell { - nativeBuildInputs = nativeBuildInputs ++ (with pkgs; [ - rust - rust-src - rust-analyzer - rustfmt - cargo - ]); - RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; - RUST_LOG="info"; - RUST_BACKTRACE=1; - }; - }); + in + rec { + packages.my-project = naersk-lib.buildPackage { + pname = "sdl-tests"; + version = "0.1.0"; + root = gitignoreSource ./.; + inherit nativeBuildInputs; + }; + defaultPackage = packages.my-project; + apps.my-project = utils.lib.mkApp { + drv = packages.my-project; + }; + defaultApp = apps.my-project; + devShell = pkgs.mkShell { + nativeBuildInputs = nativeBuildInputs ++ [ + rust + rust-src + pkgs.rust-analyzer + pkgs.rustfmt + pkgs.cargo + ]; + RUST_SRC_PATH = "${rust-src}/lib/rustlib/src/rust/library"; + RUST_LOG = "info"; + RUST_BACKTRACE = 1; + }; + }); } diff --git a/src/app.rs b/src/app.rs index 83e30e8..2e14dbc 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,5 +1,5 @@ use crate::{ - bitmap::{positive_angle_with_x, abs_difference, Axis, MapPoint, Pixmap}, + bitmap::{abs_difference, positive_angle_with_x, Axis, MapPoint, Pixmap}, brush::{Brush, CircleBrush, LineBrush, RectSelectBrush}, cache::Cache, command::CommandBox, @@ -458,27 +458,34 @@ impl<'ctx> AppState<'ctx> { "{:.width$}°", angle, width = if (angle - ANGLE).abs() < 1e-3 { 3 } else { 0 } - ), - PINK, - (self.mouse.0 + size as i32, self.mouse.1 + size as i32), - ); + ), + PINK, + (self.mouse.0 + size as i32, self.mouse.1 + size as i32), + ); for MapPoint { x, y } in line.into_iter() { self.canvas.set_draw_color(PINK); self.canvas .fill_rect(Rect::new( - x as i32 * cs as i32 + self.start.x(), - y as i32 * cs as i32 + self.start.y(), - cs, - cs, - )) + x as i32 * cs as i32 + self.start.x(), + y as i32 * cs as i32 + self.start.y(), + cs, + cs, + )) .unwrap(); } } } - Brush::RectSelect(RectSelectBrush { start: Some(s), end: Some(e), active_end }) => { + Brush::RectSelect(RectSelectBrush { + start: Some(s), + end: Some(e), + .. + }) => { self.canvas.set_draw_color(PINK); let (width, height) = (abs_difference(s.x, e.x), abs_difference(s.y, e.y)); - let MapPoint{x: start_x, y: start_y} = utils::rect_coords(s, e).0; + let MapPoint { + x: start_x, + y: start_y, + } = utils::rect_coords(s, e).0; let start_loc_x = self.start.x() + (start_x * cs) as i32; let start_loc_y = self.start.y() + (start_y * cs) as i32; draw_text( @@ -488,11 +495,11 @@ impl<'ctx> AppState<'ctx> { PINK, (start_loc_x, start_loc_y - 20), ); - self.canvas.draw_rect( - rect!(start_loc_x, start_loc_y, width * cs, height * cs) - ).unwrap(); + self.canvas + .draw_rect(rect!(start_loc_x, start_loc_y, width * cs, height * cs)) + .unwrap(); } - _ => () + _ => (), } } @@ -748,7 +755,7 @@ impl<'ctx> AppState<'ctx> { Keycode::Escape => { match self.brush { Brush::RectSelect(_) => self.brush = Brush::rect(), - _ => () + _ => (), } continue; } @@ -879,42 +886,50 @@ impl<'ctx> AppState<'ctx> { } => { if mousestate.is_mouse_button_pressed(MouseButton::Left) { match self.brush { - Brush::RectSelect(RectSelectBrush{start, end, active_end}) => { + Brush::RectSelect(RectSelectBrush { + start, + end, + active_end, + }) => { if active_end { - self.brush = Brush::RectSelect(RectSelectBrush{ + self.brush = Brush::RectSelect(RectSelectBrush { start, - end: self.idx_at_coord((x, y)).map(MapPoint::from), - active_end + end: self + .idx_at_coord((x, y)) + .map(MapPoint::from), + active_end, }); } else { - self.brush = Brush::RectSelect(RectSelectBrush{ - start: self.idx_at_coord((x, y)).map(MapPoint::from), + self.brush = Brush::RectSelect(RectSelectBrush { + start: self + .idx_at_coord((x, y)) + .map(MapPoint::from), end, - active_end + active_end, }); } - }, - Brush::Circle(CircleBrush { size }) + } + Brush::Circle(CircleBrush { size }) | Brush::Line(LineBrush { size, .. }) => { let pt = (x, y); let val = self.active_color; if let Ok(o) = self.paint_point(pt, val, size) { self.current_operation.extend(o); } - }, - _ => () + } + _ => (), } } else if mousestate.is_mouse_button_pressed(MouseButton::Right) { match self.brush { - Brush::Circle(CircleBrush { size }) - | Brush::Line(LineBrush { size, .. }) => { - let pt = (x, y); - let val = !self.active_color; - if let Ok(o) = self.paint_point(pt, val, size) { - self.current_operation.extend(o); - } - }, - _ => () + Brush::Circle(CircleBrush { size }) + | Brush::Line(LineBrush { size, .. }) => { + let pt = (x, y); + let val = !self.active_color; + if let Ok(o) = self.paint_point(pt, val, size) { + self.current_operation.extend(o); + } + } + _ => (), } } } diff --git a/src/lisp/eval.rs b/src/lisp/eval.rs index 0cf267c..b404662 100644 --- a/src/lisp/eval.rs +++ b/src/lisp/eval.rs @@ -217,12 +217,13 @@ where pub fn eval_for(&mut self, args: &[LispExpr]) -> Result { let arity = Arity::Exact(2); - let valid_binding_stmt = |expr: &LispExpr| + let valid_binding_stmt = |expr: &LispExpr| { matches!( expr, - LispExpr::List(v) - if v.len() == 2 - && matches!(v[0], LispExpr::Ident(_))); + LispExpr::List(v) + if v.len() == 2 + && matches!(v[0], LispExpr::Ident(_))) + }; if !arity.check(args) { Err(arity.to_error()) @@ -258,8 +259,8 @@ where } _ => { error!("invalid for loop args"); - Err(EvalError::BadForm.into()) - }, + Err(EvalError::BadForm.into()) + } } } } diff --git a/src/lisp/expr.rs b/src/lisp/expr.rs index 692f951..d086ecf 100644 --- a/src/lisp/expr.rs +++ b/src/lisp/expr.rs @@ -310,6 +310,12 @@ impl TryFrom for LispNumber { } } +impl From for LispExpr { + fn from(num: i64) -> Self { + LispExpr::Number(num.into()) + } +} + impl<'a> TryFrom<&'a LispExpr> for &'a LispNumber { type Error = LispError; fn try_from(value: &'a LispExpr) -> Result { diff --git a/src/lisp/number.rs b/src/lisp/number.rs index 4824e21..06c6baa 100644 --- a/src/lisp/number.rs +++ b/src/lisp/number.rs @@ -1,5 +1,6 @@ use std::{ cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd}, + convert::From, fmt, ops::{Add, Mul, Sub}, }; @@ -113,3 +114,9 @@ impl fmt::Display for LispNumber { } } } + +impl From for LispNumber { + fn from(target: i64) -> Self { + LispNumber::Integer(target) + } +} diff --git a/src/lisp/prelude.rs b/src/lisp/prelude.rs index e24ade5..1d37a32 100644 --- a/src/lisp/prelude.rs +++ b/src/lisp/prelude.rs @@ -1,6 +1,6 @@ use crate::{ bitmap::MapPoint, - brush::Brush, + brush::{Brush, RectSelectBrush}, grid::GridKind, guide::Guide, lisp::{ @@ -11,7 +11,7 @@ use crate::{ }, primitive, undo::PaintRecord, - utils::load_script, + utils::{load_script, rect_coords}, }; use std::convert::TryInto; @@ -437,6 +437,40 @@ pub fn new_env() -> Result { } }); + primitive!(env, Arity::Exact(0), "selection-start", |_, app| { + if let Brush::RectSelect(RectSelectBrush { + start: Some(s), + end: Some(e), + .. + }) = app.brush + { + let pt = rect_coords(s, e).0; + Ok(LispExpr::DottedList(vec![ + (pt.x as i64).into(), + (pt.y as i64).into(), + ])) + } else { + Err(EvalError::CustomInternal("No active selection!").into()) + } + }); + + primitive!(env, Arity::Exact(0), "selection-end", |_, app| { + if let Brush::RectSelect(RectSelectBrush { + start: Some(s), + end: Some(e), + .. + }) = app.brush + { + let pt = rect_coords(s, e).1; + Ok(LispExpr::DottedList(vec![ + (pt.x as i64).into(), + (pt.y as i64).into(), + ])) + } else { + Err(EvalError::CustomInternal("No active selection!").into()) + } + }); + primitive!(env, Arity::Exact(2), "range", |args, _| { if type_match!( args, diff --git a/src/utils.rs b/src/utils.rs index 0825c8c..d5d4039 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,7 +1,7 @@ use crate::{ app::AppState, - consts::FONT_PATH, bitmap::{abs_difference, MapPoint}, + consts::FONT_PATH, lisp::{ error::{EvalError, LispError, ParseError}, eval::Evaluator, @@ -143,7 +143,8 @@ pub fn rect_coords(s: MapPoint, e: MapPoint) -> (MapPoint, MapPoint) { (e.x, s.y) } else { (e.x, e.y) - }.into(); + } + .into(); let end_loc = start_loc + (width, height).into(); (start_loc, end_loc) } -- cgit v1.2.3