diff options
author | Akshay <[email protected]> | 2021-03-16 07:08:40 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2021-03-16 07:08:40 +0000 |
commit | f33cf3e1cc42ff72e7d2dc3d66c47064f60bd0eb (patch) | |
tree | 43de2053437de0ef5838ef9ab661941a6008bab2 | |
parent | 85b851a28beca0a2f5ef48587a5e978c9f72d7f8 (diff) |
add dither_level to app state, integrate with brushes
-rw-r--r-- | src/app.rs | 33 | ||||
-rw-r--r-- | src/main.rs | 3 |
2 files changed, 31 insertions, 5 deletions
@@ -1,7 +1,7 @@ | |||
1 | use crate::{ | 1 | use crate::{ |
2 | bitmap::{MapPoint, Pixmap}, | 2 | bitmap::{MapPoint, Pixmap}, |
3 | consts::colors::*, | 3 | consts::colors::*, |
4 | rect, | 4 | dither, rect, |
5 | symmetry::Symmetry, | 5 | symmetry::Symmetry, |
6 | undo::{ModifyRecord, OpKind, Operation, UndoStack}, | 6 | undo::{ModifyRecord, OpKind, Operation, UndoStack}, |
7 | utils::draw_text, | 7 | utils::draw_text, |
@@ -24,6 +24,7 @@ use sdl2::{ | |||
24 | pub struct AppState<'ctx> { | 24 | pub struct AppState<'ctx> { |
25 | active_color: bool, | 25 | active_color: bool, |
26 | brush_size: u8, | 26 | brush_size: u8, |
27 | dither_level: u8, | ||
27 | canvas: Canvas<Window>, | 28 | canvas: Canvas<Window>, |
28 | context: &'ctx Sdl, | 29 | context: &'ctx Sdl, |
29 | ttf_context: &'ctx Sdl2TtfContext, | 30 | ttf_context: &'ctx Sdl2TtfContext, |
@@ -130,13 +131,18 @@ impl<'ctx> AppState<'ctx> { | |||
130 | ) -> Result<Vec<ModifyRecord>, ()> { | 131 | ) -> Result<Vec<ModifyRecord>, ()> { |
131 | let radius = self.brush_size as u32; | 132 | let radius = self.brush_size as u32; |
132 | let center = self.idx_at_coord(center).ok_or(())?; | 133 | let center = self.idx_at_coord(center).ok_or(())?; |
134 | let dither_level = self.dither_level; | ||
133 | 135 | ||
134 | let circle = self.pixmap.get_circle(center, radius, true); | 136 | let circle = self.pixmap.get_circle(center, radius, true); |
135 | let sym_circle = self.symmetry.apply(&circle); | 137 | let sym_circle = self.symmetry.apply(&circle); |
136 | 138 | ||
137 | let mut modify_record = vec![]; | 139 | let mut modify_record = vec![]; |
138 | 140 | ||
139 | for point in circle.into_iter().chain(sym_circle) { | 141 | for point in circle |
142 | .into_iter() | ||
143 | .chain(sym_circle) | ||
144 | .filter(|&pt| dither::bayer(dither_level, pt)) | ||
145 | { | ||
140 | let old_val = self.pixmap.set(point, val); | 146 | let old_val = self.pixmap.set(point, val); |
141 | modify_record.push(ModifyRecord::new(point, old_val, val)); | 147 | modify_record.push(ModifyRecord::new(point, old_val, val)); |
142 | } | 148 | } |
@@ -151,6 +157,7 @@ impl<'ctx> AppState<'ctx> { | |||
151 | ) -> Result<Vec<ModifyRecord>, ()> { | 157 | ) -> Result<Vec<ModifyRecord>, ()> { |
152 | let start = self.idx_at_coord(start).ok_or(())?; | 158 | let start = self.idx_at_coord(start).ok_or(())?; |
153 | let end = self.idx_at_coord(end).ok_or(())?; | 159 | let end = self.idx_at_coord(end).ok_or(())?; |
160 | let dither_level = self.dither_level; | ||
154 | 161 | ||
155 | let line = self.pixmap.get_line(start, end); | 162 | let line = self.pixmap.get_line(start, end); |
156 | let sym_line = self.symmetry.apply(&line); | 163 | let sym_line = self.symmetry.apply(&line); |
@@ -158,7 +165,10 @@ impl<'ctx> AppState<'ctx> { | |||
158 | let mut line_modify_record = vec![]; | 165 | let mut line_modify_record = vec![]; |
159 | for point in line.into_iter().chain(sym_line) { | 166 | for point in line.into_iter().chain(sym_line) { |
160 | let circle_around_point = self.pixmap.get_circle(point, self.brush_size as u32, true); | 167 | let circle_around_point = self.pixmap.get_circle(point, self.brush_size as u32, true); |
161 | for c in circle_around_point { | 168 | for c in circle_around_point |
169 | .into_iter() | ||
170 | .filter(|&pt| dither::bayer(dither_level, pt)) | ||
171 | { | ||
162 | let old_val = self.pixmap.set(c, val); | 172 | let old_val = self.pixmap.set(c, val); |
163 | line_modify_record.push(ModifyRecord::new(c, old_val, val)); | 173 | line_modify_record.push(ModifyRecord::new(c, old_val, val)); |
164 | } | 174 | } |
@@ -215,6 +225,18 @@ impl<'ctx> AppState<'ctx> { | |||
215 | } | 225 | } |
216 | } | 226 | } |
217 | 227 | ||
228 | fn reduce_intensity(&mut self) { | ||
229 | if self.dither_level > 0 { | ||
230 | self.dither_level -= 1; | ||
231 | } | ||
232 | } | ||
233 | |||
234 | fn increase_intensity(&mut self) { | ||
235 | if self.dither_level < 16 { | ||
236 | self.dither_level += 1; | ||
237 | } | ||
238 | } | ||
239 | |||
218 | fn zoom_out(&mut self, p: (i32, i32)) { | 240 | fn zoom_out(&mut self, p: (i32, i32)) { |
219 | if self.zoom > 1 { | 241 | if self.zoom > 1 { |
220 | // attempt to center around cursor | 242 | // attempt to center around cursor |
@@ -290,7 +312,7 @@ impl<'ctx> AppState<'ctx> { | |||
290 | if let Some(center) = pt { | 312 | if let Some(center) = pt { |
291 | let circle = self.pixmap.get_circle(center, brush_size as u32, false); | 313 | let circle = self.pixmap.get_circle(center, brush_size as u32, false); |
292 | for MapPoint { x, y } in circle.into_iter() { | 314 | for MapPoint { x, y } in circle.into_iter() { |
293 | self.canvas.set_draw_color(GREY); | 315 | self.canvas.set_draw_color(PINK); |
294 | self.canvas | 316 | self.canvas |
295 | .fill_rect(Rect::new( | 317 | .fill_rect(Rect::new( |
296 | x as i32 * cs as i32 + self.start.x(), | 318 | x as i32 * cs as i32 + self.start.x(), |
@@ -383,6 +405,7 @@ impl<'ctx> AppState<'ctx> { | |||
383 | Self { | 405 | Self { |
384 | active_color: true, | 406 | active_color: true, |
385 | brush_size: 0, | 407 | brush_size: 0, |
408 | dither_level: 16, | ||
386 | canvas, | 409 | canvas, |
387 | context, | 410 | context, |
388 | ttf_context, | 411 | ttf_context, |
@@ -431,6 +454,8 @@ impl<'ctx> AppState<'ctx> { | |||
431 | // brush ops | 454 | // brush ops |
432 | Keycode::Q => self.decrease_brush_size(), | 455 | Keycode::Q => self.decrease_brush_size(), |
433 | Keycode::E => self.increase_brush_size(), | 456 | Keycode::E => self.increase_brush_size(), |
457 | Keycode::Num1 => self.reduce_intensity(), | ||
458 | Keycode::Num3 => self.increase_intensity(), | ||
434 | // flip color | 459 | // flip color |
435 | Keycode::X => self.change_active_color(), | 460 | Keycode::X => self.change_active_color(), |
436 | // toggle grid | 461 | // 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 @@ | |||
1 | mod app; | 1 | mod app; |
2 | mod bitmap; | 2 | mod bitmap; |
3 | mod consts; | 3 | mod consts; |
4 | mod dither; | ||
4 | mod symmetry; | 5 | mod symmetry; |
5 | mod undo; | 6 | mod undo; |
6 | mod utils; | 7 | mod utils; |
@@ -10,5 +11,5 @@ use app::AppState; | |||
10 | pub fn main() { | 11 | pub fn main() { |
11 | let sdl_context = sdl2::init().unwrap(); | 12 | let sdl_context = sdl2::init().unwrap(); |
12 | let ttf_context = sdl2::ttf::init().unwrap(); | 13 | let ttf_context = sdl2::ttf::init().unwrap(); |
13 | AppState::init(160, 160, &sdl_context, &ttf_context).run(); | 14 | AppState::init(200, 200, &sdl_context, &ttf_context).run(); |
14 | } | 15 | } |