Skip to content

Commit

Permalink
feat: add arrow in annotation options
Browse files Browse the repository at this point in the history
  • Loading branch information
decipher3114 committed Oct 19, 2024
1 parent 823bac5 commit fde980e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
Binary file modified assets/fonts/icons.ttf
Binary file not shown.
4 changes: 3 additions & 1 deletion src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub const ELLIPSE_STROKE: char = '\u{F104}';

pub const LINE: char = '\u{F105}';

pub const HIGHLIGHT: char = '\u{F106}';
pub const ARROW: char = '\u{F106}';

pub const HIGHLIGHT: char = '\u{F107}';

// pub const CANCEL: char = '\u{F201}';

Expand Down
38 changes: 27 additions & 11 deletions src/windows/capture_window/annotate.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::f32::consts::PI;

use iced::{
event::Status,
mouse::{Button, Cursor, Interaction},
widget::canvas::{
path::{arc::Elliptical, Builder},
Event, Fill, Frame, Geometry, LineCap, LineDash, LineJoin, Path, Program, Stroke, Style,
Event, Fill, Frame, Geometry, LineCap, LineDash, Path, Program, Stroke, Style,
},
Color, Point, Radians, Rectangle, Renderer, Size, Vector,
};
Expand Down Expand Up @@ -180,6 +182,9 @@ fn draw_shape(frame: &mut Frame, shape: &Shape) {
if let Some(endpoints) = shape.endpoints {
let shape_type = shape.shape_type;
let color = shape.color.into_iced_color(shape.is_solid);
let stroke = Stroke::default()
.with_width(shape.stroke_width.f32())
.with_color(color);
match shape_type {
ShapeType::Rectangle => {
let (top_left, bottom_right) = endpoints.normalize();
Expand All @@ -189,10 +194,6 @@ fn draw_shape(frame: &mut Frame, shape: &Shape) {
let fill = Fill::from(color);
frame.fill(&path, fill);
} else {
let stroke = Stroke::default()
.with_width(shape.stroke_width.f32())
.with_color(color)
.with_line_join(LineJoin::Round);
frame.stroke(&path, stroke);
}
}
Expand All @@ -215,17 +216,32 @@ fn draw_shape(frame: &mut Frame, shape: &Shape) {
let fill = Fill::from(color);
frame.fill(&path, fill);
} else {
let stroke = Stroke::default()
.with_width(shape.stroke_width.f32())
.with_color(color);
frame.stroke(&path, stroke);
};
}
ShapeType::Line => {
let path = Path::line(endpoints.initial_pt, endpoints.final_pt);
let stroke = Stroke::default()
.with_width(shape.stroke_width.f32())
.with_color(color);
frame.stroke(&path, stroke);
}
ShapeType::Arrow => {
let (i_pt, f_pt) = (endpoints.initial_pt, endpoints.final_pt);
let line = f_pt - i_pt;
let rad = line.y.atan2(line.x);
let mut path = Builder::new();
path.move_to(i_pt);
path.line_to(f_pt);
let right_pt = Point::new(
f_pt.x - 30.0 * (rad - PI / 5.0).cos(),
f_pt.y - 30.0 * (rad - PI / 5.0).sin(),
);
let left_pt = Point::new(
f_pt.x - 30.0 * (rad + PI / 5.0).cos(),
f_pt.y - 30.0 * (rad + PI / 5.0).sin(),
);
path.move_to(right_pt);
path.line_to(f_pt);
path.line_to(left_pt);
let path = path.build();
frame.stroke(&path, stroke);
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/windows/capture_window/capture.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::f32::consts::PI;

use arboard::{Clipboard, ImageData};
use iced::{widget::canvas::Cache, Point};
use indexmap::IndexMap;
Expand Down Expand Up @@ -124,6 +126,27 @@ pub fn draw_shapes(image: &RgbaImage, shapes: Vec<Shape>) -> RgbaImage {
let path = builder.finish().unwrap();
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
}
ShapeType::Arrow => {
let mut builder = PathBuilder::new();
let (initial_pt, final_pt) = (endpoints.initial_pt, endpoints.final_pt);
let line = final_pt - initial_pt;
let rad = line.y.atan2(line.x);
let (right_x, right_y) = (
final_pt.x - 30.0 * (rad - PI / 5.0).cos(),
final_pt.y - 30.0 * (rad - PI / 5.0).sin(),
);
let (left_x, left_y) = (
final_pt.x - 30.0 * (rad + PI / 5.0).cos(),
final_pt.y - 30.0 * (rad + PI / 5.0).sin(),
);
builder.move_to(initial_pt.x, initial_pt.y);
builder.line_to(final_pt.x, final_pt.y);
builder.move_to(right_x, right_y);
builder.line_to(final_pt.x, final_pt.y);
builder.line_to(left_x, left_y);
let path = builder.finish().unwrap();
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
}
}
}
RgbaImage::from_vec(width, height, pixmap.take()).unwrap()
Expand Down
3 changes: 2 additions & 1 deletion src/windows/capture_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use xcap::image::RgbaImage;
use crate::{
app::AppEvent,
consts::{
ELLIPSE_FILLED, ELLIPSE_STROKE, HIGHLIGHT, ICON, LINE, RECT_FILLED, RECT_STROKE,
ARROW, ELLIPSE_FILLED, ELLIPSE_STROKE, HIGHLIGHT, ICON, LINE, RECT_FILLED, RECT_STROKE,
STROKE_BROAD, STROKE_MEDIUM, STROKE_THIN,
},
theme::{button::ButtonClass, text::TextClass, Element},
Expand Down Expand Up @@ -222,6 +222,7 @@ impl CaptureWindow {
shapes_icon(ELLIPSE_FILLED, ShapeType::Ellipse, true, true),
shapes_icon(ELLIPSE_STROKE, ShapeType::Ellipse, false, true),
shapes_icon(LINE, ShapeType::Line, false, true),
shapes_icon(ARROW, ShapeType::Arrow, false, true),
shapes_icon(HIGHLIGHT, ShapeType::Rectangle, true, false)
];
let shapes = panel(row.spacing(ROW));
Expand Down
1 change: 1 addition & 0 deletions src/windows/capture_window/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub enum ShapeType {
Rectangle,
Ellipse,
Line,
Arrow,
}

#[derive(Debug, Default, Clone, Copy, PartialEq)]
Expand Down

0 comments on commit fde980e

Please sign in to comment.