From 305bf638f823a41f391936712eef302bc6733d00 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 23 Mar 2021 18:56:25 +0530 Subject: expose functions to lisp interface, add primitives with macros --- src/app.rs | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'src/app.rs') diff --git a/src/app.rs b/src/app.rs index 9730cf2..ef299cd 100644 --- a/src/app.rs +++ b/src/app.rs @@ -13,6 +13,7 @@ use crate::{ use std::{convert::From, fs::File, io::prelude::*}; +use log::{info, warn}; use obi::Image; use sdl2::{ event::Event, @@ -74,11 +75,11 @@ impl<'ctx> AppState<'ctx> { self.start += direction.into(); } - fn width(&self) -> u32 { + pub fn width(&self) -> u32 { self.pixmap.width } - fn height(&self) -> u32 { + pub fn height(&self) -> u32 { self.pixmap.height } @@ -93,11 +94,11 @@ impl<'ctx> AppState<'ctx> { ); } - fn change_active_color(&mut self) { + pub fn change_active_color(&mut self) { self.active_color = !self.active_color; } - fn idx_at_coord>(&self, p: P) -> Option<(u32, u32)> { + pub fn idx_at_coord>(&self, p: P) -> Option<(u32, u32)> { let p: Point = p.into(); if self.within_canvas(p) { // convert p relative to start of drawing area @@ -110,17 +111,17 @@ impl<'ctx> AppState<'ctx> { } } - fn within_canvas>(&self, p: P) -> bool { + pub fn within_canvas>(&self, p: P) -> bool { let p: Point = p.into(); let (mini, maxi) = self.bounds(); p.x() < maxi.x() && p.y() < maxi.y() && p.x() >= mini.x() && p.y() >= mini.y() } - fn toggle_grid(&mut self) { + pub fn toggle_grid(&mut self) { self.grid.enabled = !self.grid.enabled; } - fn cycle_symmetry(&mut self) { + pub fn cycle_symmetry(&mut self) { let Symmetry { x, y } = self.symmetry; self.symmetry = match (x, y) { (None, None) => Symmetry { @@ -139,7 +140,7 @@ impl<'ctx> AppState<'ctx> { } } - fn paint_point>( + pub fn paint_point>( &mut self, center: P, val: bool, @@ -164,7 +165,7 @@ impl<'ctx> AppState<'ctx> { Ok(modify_record) } - fn paint_line>( + pub fn paint_line>( &mut self, start: MapPoint, end: P, @@ -191,7 +192,7 @@ impl<'ctx> AppState<'ctx> { Ok(line_modify_record) } - fn apply_operation(&mut self, op: Operation, op_kind: OpKind) { + pub fn apply_operation(&mut self, op: Operation, op_kind: OpKind) { for ModifyRecord { point, old_val, @@ -207,7 +208,7 @@ impl<'ctx> AppState<'ctx> { } } - fn commit_operation(&mut self) { + pub fn commit_operation(&mut self) { if !self.current_operation.is_empty() { let op = self .current_operation @@ -218,7 +219,7 @@ impl<'ctx> AppState<'ctx> { } } - fn zoom_in(&mut self, p: (i32, i32)) { + pub fn zoom_in(&mut self, p: (i32, i32)) { // attempt to center around cursor if let Some(p) = self.idx_at_coord(p) { let (x1, y1) = (p.0 * (self.zoom as u32), p.1 * (self.zoom as u32)); @@ -230,7 +231,7 @@ impl<'ctx> AppState<'ctx> { self.zoom += 1; } - fn zoom_out(&mut self, p: (i32, i32)) { + pub fn zoom_out(&mut self, p: (i32, i32)) { if self.zoom > 1 { // attempt to center around cursor if let Some(p) = self.idx_at_coord(p) { @@ -244,7 +245,7 @@ impl<'ctx> AppState<'ctx> { } } - fn center_grid(&mut self) { + pub fn center_grid(&mut self) { let (winsize_x, winsize_y) = self.canvas.window().size(); let grid_width = self.width() * self.zoom as u32; let grid_height = self.height() * self.zoom as u32; @@ -254,29 +255,29 @@ impl<'ctx> AppState<'ctx> { ); } - fn increase_brush_size(&mut self) { + pub fn increase_brush_size(&mut self) { self.brush_size += 1; } - fn decrease_brush_size(&mut self) { + pub fn decrease_brush_size(&mut self) { if self.brush_size > 0 { self.brush_size -= 1; } } - fn reduce_intensity(&mut self) { + pub fn reduce_intensity(&mut self) { if self.dither_level > 0 { self.dither_level -= 1; } } - fn increase_intensity(&mut self) { + pub fn increase_intensity(&mut self) { if self.dither_level < 16 { self.dither_level += 1; } } - fn eval_command(&mut self) { + pub 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(); @@ -293,11 +294,8 @@ impl<'ctx> AppState<'ctx> { let image = self.export(); let encoded = image.encode().unwrap(); let mut buffer = File::create(path).unwrap(); - eprintln!("writing to file"); buffer.write_all(&encoded[..]).unwrap(); self.command_box.hist_append(); - } else { - eprintln!("cmd: {}", self.command_box.text); } self.command_box.clear(); self.mode = Mode::Draw; @@ -332,7 +330,7 @@ impl<'ctx> AppState<'ctx> { self.canvas .fill_rect(rect!( 0, - winsize_y - status_height, + winsize_y - status_height - 20, status_width, status_height )) @@ -355,7 +353,7 @@ impl<'ctx> AppState<'ctx> { self.ttf_context, status_text, BLACK, - (0, winsize_y - status_height), + (0, winsize_y - status_height - 20), ); } @@ -462,11 +460,8 @@ 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_statusline(); self.draw_command_box(); - // } self.draw_mouse(); } -- cgit v1.2.3