Skip to content

Commit

Permalink
feat(drawnItems): add filled rectangle, toggle fill state
Browse files Browse the repository at this point in the history
  • Loading branch information
kakoc committed Sep 8, 2023
1 parent 2e9dd1b commit 7ce5305
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 57 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ Hotkeys:
Enter - take a screenshot of selected area, save to a clipboard and exit
f - take a screenshot where selected area is focused, save to a clipboard and exit
l - draw a line. after that hotkey you can press left button and start drawing a line
r - draw a rectangular border. after that hotkey you can press left button and start drawing a rectangular border
l - draw a line
r - draw a rectangular border
p - draw a filled rectangle
t - toggle latest drawn shape between filled/not filled states
Esc - exit
```
13 changes: 1 addition & 12 deletions src/circle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::blend::blend;

#[allow(dead_code)]
pub fn draw_circle(
pub fn draw_circle_filled(
canvas: &mut [u8],
x: usize,
y: usize,
Expand All @@ -25,15 +23,6 @@ pub fn draw_circle(
let dx = (x as isize - ww as isize).unsigned_abs();

if dx * dx + dy * dy <= r * r {
let (red, green, blue, alpha) = blend(
(
canvas[hh * (width * 4) + (ww * 4)],
canvas[hh * (width * 4) + (ww * 4) + 1],
canvas[hh * (width * 4) + (ww * 4) + 2],
canvas[hh * (width * 4) + (ww * 4) + 3],
),
(red, green, blue, alpha),
);
canvas[hh * (width * 4) + (ww * 4)] = red;
canvas[hh * (width * 4) + (ww * 4) + 1] = green;
canvas[hh * (width * 4) + (ww * 4) + 2] = blue;
Expand Down
125 changes: 82 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use line::draw_line;
use log::error;
use pixels::{Error, Pixels, SurfaceTexture};
use rectangle::draw_rect_borders;
use rectangle::draw_rect_filled;
use screenshots::Screen;
use serde::{Deserialize, Serialize};
use winit::dpi::PhysicalPosition;
Expand Down Expand Up @@ -55,8 +56,10 @@ Hotkeys:
Enter - take a screenshot of selected area, save to a clipboard and exit
f - take a screenshot where selected area is focused, save to a clipboard and exit
l - draw a line. after that hotkey you can press left button and start drawing a line
r - draw a rectangular border. after that hotkey you can press left button and start drawing a rectangular border
l - draw a line
r - draw a rectangular border
p - draw a filled rectangle
t - toggle latest drawn shape between filled/not filled states
Esc - exit
"#
Expand Down Expand Up @@ -178,6 +181,12 @@ Hotkeys:
if let Some(VirtualKeyCode::R) = virtual_keycode {
screenshot.draw_mode = Some(DrawMode::RectBorder);
}
if let Some(VirtualKeyCode::P) = virtual_keycode {
screenshot.draw_mode = Some(DrawMode::RectFilled);
}
if let Some(VirtualKeyCode::T) = virtual_keycode {
screenshot.toggle_filling_latest();
}

window.request_redraw();
}
Expand Down Expand Up @@ -338,60 +347,54 @@ impl Screenshot {
self.draw_boundaries();
self.darken_not_selected_area();

for draw_item in &self.drawn_items {
match draw_item {
DrawnItem::Line((x0, y0), (x1, y1)) => {
draw_line(
&mut self.modified_screenshot,
*x0,
*y0,
*x1,
*y1,
self.width,
BORDER_COLOR,
);
}
DrawnItem::RectBorder((x0, y0), (x1, y1)) => {
draw_rect_borders(
&mut self.modified_screenshot,
*x0,
*y0,
*x1,
*y1,
self.width,
BORDER_COLOR,
);
}
}
for draw_item in self.drawn_items.clone() {
self.draw_draw_item(&draw_item);
}

if let Some(drawing_item) = self.drawing_item {
self.draw_draw_item(&drawing_item);
}

if pixels.len() == self.modified_screenshot.len() {
pixels.copy_from_slice(&self.modified_screenshot);
}
match self.drawing_item {
Some(DrawnItem::Line((x0, y0), (x1, y1))) => {
}

fn draw_draw_item(&mut self, draw_item: &DrawnItem) {
match draw_item {
DrawnItem::Line((x0, y0), (x1, y1)) => {
draw_line(
&mut self.modified_screenshot,
x0,
y0,
x1,
y1,
*x0,
*y0,
*x1,
*y1,
self.width,
BORDER_COLOR,
);
}
Some(DrawnItem::RectBorder((x0, y0), (x1, y1))) => {
DrawnItem::RectBorder((x0, y0), (x1, y1)) => {
draw_rect_borders(
&mut self.modified_screenshot,
x0,
y0,
x1,
y1,
*x0,
*y0,
*x1,
*y1,
self.width,
BORDER_COLOR,
);
}
DrawnItem::RectFilled((x0, y0), (x1, y1)) => {
draw_rect_filled(
&mut self.modified_screenshot,
*x0,
*y0,
*x1,
*y1,
self.width,
BORDER_COLOR,
);
}
None => {}
}

if pixels.len() == self.modified_screenshot.len() {
pixels.copy_from_slice(&self.modified_screenshot);
}
}

Expand All @@ -417,6 +420,21 @@ impl Screenshot {
}
}

pub fn toggle_filling_latest(&mut self) {
if let Some(item) = self.drawn_items.pop() {
let filled_item = self.toggle_item_filling(&item);
self.drawn_items.push(filled_item);
}
}

pub fn toggle_item_filling(&mut self, draw_item: &DrawnItem) -> DrawnItem {
match draw_item {
DrawnItem::Line(..) => *draw_item,
DrawnItem::RectBorder(p0, p1) => DrawnItem::RectFilled(*p0, *p1),
DrawnItem::RectFilled(p0, p1) => DrawnItem::RectBorder(*p0, *p1),
}
}

pub fn on_mouse_move(&mut self, coordinates: PhysicalPosition<f64>) {
self.mouse_coordinates = Some(coordinates);

Expand Down Expand Up @@ -444,6 +462,13 @@ impl Screenshot {
*p1 = (x as usize, y as usize);
}
}
Some(DrawMode::RectFilled) => {
if let (Some(DrawnItem::RectFilled(_, p1)), Some(PhysicalPosition { x, y })) =
(&mut self.drawing_item, self.mouse_coordinates)
{
*p1 = (x as usize, y as usize);
}
}
None => {}
}
}
Expand Down Expand Up @@ -496,6 +521,9 @@ impl Screenshot {
Some(DrawMode::RectBorder) => {
self.drawing_item = Some(DrawnItem::RectBorder((x, y), (x, y)));
}
Some(DrawMode::RectFilled) => {
self.drawing_item = Some(DrawnItem::RectFilled((x, y), (x, y)));
}
None => {}
}
}
Expand Down Expand Up @@ -528,6 +556,15 @@ impl Screenshot {
self.drawing_item = None;
}
}
Some(DrawMode::RectFilled) => {
if let (Some(DrawnItem::RectFilled(p0, _)), Some(PhysicalPosition { x, y })) =
(self.drawing_item, self.mouse_coordinates)
{
self.drawn_items
.push(DrawnItem::RectFilled(p0, (x as usize, y as usize)));
self.drawing_item = None;
}
}
None => {}
}

Expand All @@ -538,10 +575,12 @@ impl Screenshot {
enum DrawMode {
Line,
RectBorder,
RectFilled,
}

#[derive(Clone, Copy)]
enum DrawnItem {
Line((usize, usize), (usize, usize)),
RectBorder((usize, usize), (usize, usize)),
RectFilled((usize, usize), (usize, usize)),
}
29 changes: 29 additions & 0 deletions src/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,32 @@ pub fn draw_rect_borders(
// left
draw_line(canvas, x0, y0, x0, y1, width, color);
}

pub fn draw_rect_filled(
canvas: &mut [u8],
x0: usize,
y0: usize,
x1: usize,
y1: usize,
width: usize,
color: (u8, u8, u8, u8),
) {
let red = color.0;
let green = color.1;
let blue = color.2;
let alpha = color.3;

let (x0, x1) = if x0 > x1 { (x1, x0) } else { (x0, x1) };
let (y0, y1) = if y0 > y1 { (y1, y0) } else { (y0, y1) };

for hh in y0..y1 {
for ww in x0..x1 {
if (hh >= y0 && hh <= y1) && (ww >= x0 && ww <= x1) {
canvas[hh * (width * 4) + (ww * 4)] = red;
canvas[hh * (width * 4) + (ww * 4) + 1] = green;
canvas[hh * (width * 4) + (ww * 4) + 2] = blue;
canvas[hh * (width * 4) + (ww * 4) + 3] = alpha;
}
}
}
}

0 comments on commit 7ce5305

Please sign in to comment.