From f33cf3e1cc42ff72e7d2dc3d66c47064f60bd0eb Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 16 Mar 2021 12:38:40 +0530 Subject: add dither_level to app state, integrate with brushes --- src/app.rs | 33 +++++++++++++++++++++++++++++---- src/main.rs | 3 ++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/app.rs b/src/app.rs index 059635a..9a1155e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,7 +1,7 @@ use crate::{ bitmap::{MapPoint, Pixmap}, consts::colors::*, - rect, + dither, rect, symmetry::Symmetry, undo::{ModifyRecord, OpKind, Operation, UndoStack}, utils::draw_text, @@ -24,6 +24,7 @@ use sdl2::{ pub struct AppState<'ctx> { active_color: bool, brush_size: u8, + dither_level: u8, canvas: Canvas, context: &'ctx Sdl, ttf_context: &'ctx Sdl2TtfContext, @@ -130,13 +131,18 @@ impl<'ctx> AppState<'ctx> { ) -> Result, ()> { let radius = self.brush_size as u32; let center = self.idx_at_coord(center).ok_or(())?; + let dither_level = self.dither_level; let circle = self.pixmap.get_circle(center, radius, true); let sym_circle = self.symmetry.apply(&circle); let mut modify_record = vec![]; - for point in circle.into_iter().chain(sym_circle) { + for point in circle + .into_iter() + .chain(sym_circle) + .filter(|&pt| dither::bayer(dither_level, pt)) + { let old_val = self.pixmap.set(point, val); modify_record.push(ModifyRecord::new(point, old_val, val)); } @@ -151,6 +157,7 @@ impl<'ctx> AppState<'ctx> { ) -> Result, ()> { let start = self.idx_at_coord(start).ok_or(())?; let end = self.idx_at_coord(end).ok_or(())?; + let dither_level = self.dither_level; let line = self.pixmap.get_line(start, end); let sym_line = self.symmetry.apply(&line); @@ -158,7 +165,10 @@ impl<'ctx> AppState<'ctx> { let mut line_modify_record = vec![]; for point in line.into_iter().chain(sym_line) { let circle_around_point = self.pixmap.get_circle(point, self.brush_size as u32, true); - for c in circle_around_point { + for c in circle_around_point + .into_iter() + .filter(|&pt| dither::bayer(dither_level, pt)) + { let old_val = self.pixmap.set(c, val); line_modify_record.push(ModifyRecord::new(c, old_val, val)); } @@ -215,6 +225,18 @@ impl<'ctx> AppState<'ctx> { } } + fn reduce_intensity(&mut self) { + if self.dither_level > 0 { + self.dither_level -= 1; + } + } + + fn increase_intensity(&mut self) { + if self.dither_level < 16 { + self.dither_level += 1; + } + } + fn zoom_out(&mut self, p: (i32, i32)) { if self.zoom > 1 { // attempt to center around cursor @@ -290,7 +312,7 @@ impl<'ctx> AppState<'ctx> { if let Some(center) = pt { let circle = self.pixmap.get_circle(center, brush_size as u32, false); for MapPoint { x, y } in circle.into_iter() { - self.canvas.set_draw_color(GREY); + self.canvas.set_draw_color(PINK); self.canvas .fill_rect(Rect::new( x as i32 * cs as i32 + self.start.x(), @@ -383,6 +405,7 @@ impl<'ctx> AppState<'ctx> { Self { active_color: true, brush_size: 0, + dither_level: 16, canvas, context, ttf_context, @@ -431,6 +454,8 @@ impl<'ctx> AppState<'ctx> { // brush ops Keycode::Q => self.decrease_brush_size(), Keycode::E => self.increase_brush_size(), + Keycode::Num1 => self.reduce_intensity(), + Keycode::Num3 => self.increase_intensity(), // flip color Keycode::X => self.change_active_color(), // toggle grid diff --git a/src/main.rs b/src/main.rs index c9c1bdd..14f4cf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod app; mod bitmap; mod consts; +mod dither; mod symmetry; mod undo; mod utils; @@ -10,5 +11,5 @@ use app::AppState; pub fn main() { let sdl_context = sdl2::init().unwrap(); let ttf_context = sdl2::ttf::init().unwrap(); - AppState::init(160, 160, &sdl_context, &ttf_context).run(); + AppState::init(200, 200, &sdl_context, &ttf_context).run(); } -- cgit v1.2.3