Skip to content

Commit

Permalink
fix: use variable arrow size
Browse files Browse the repository at this point in the history
  • Loading branch information
decipher3114 committed Oct 23, 2024
1 parent 3e18eb4 commit 2df3a23
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 42 deletions.
31 changes: 9 additions & 22 deletions src/windows/capture_window/annotate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::f32::consts::PI;

use iced::{
event::Status,
mouse::{Button, Cursor, Interaction},
Expand All @@ -16,8 +14,7 @@ use crate::{
};

use super::{
models::{Shape, ShapeType},
CaptureEvent, CaptureWindow,
models::{Shape, ShapeType}, utils::resolve_arrow_points, CaptureEvent, CaptureWindow
};

impl Program<CaptureEvent, Theme> for CaptureWindow {
Expand Down Expand Up @@ -224,24 +221,14 @@ fn draw_shape(frame: &mut Frame, shape: &Shape) {
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();
let (right_pt, left_pt) = resolve_arrow_points(endpoints.initial_pt, endpoints.final_pt);
let mut builder = Builder::new();
builder.move_to(endpoints.initial_pt);
builder.line_to(endpoints.final_pt);
builder.move_to(right_pt);
builder.line_to(endpoints.final_pt);
builder.line_to(left_pt);
let path = builder.build();
frame.stroke(&path, stroke);
}
}
Expand Down
27 changes: 7 additions & 20 deletions src/windows/capture_window/capture.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::f32::consts::PI;

use arboard::{Clipboard, ImageData};
use iced::{widget::canvas::Cache, Point};
use indexmap::IndexMap;
Expand All @@ -10,8 +8,7 @@ use xcap::{
};

use super::{
models::{CapturedWindow, CropMode, Endpoints, Mode, Shape, ShapeType},
CaptureWindow,
models::{CapturedWindow, CropMode, Endpoints, Mode, Shape, ShapeType}, utils::resolve_arrow_points, CaptureWindow
};

impl CaptureWindow {
Expand Down Expand Up @@ -127,23 +124,13 @@ pub fn draw_shapes(image: &RgbaImage, shapes: Vec<Shape>) -> RgbaImage {
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
}
ShapeType::Arrow => {
let (right_pt, left_pt) = resolve_arrow_points(endpoints.initial_pt, endpoints.final_pt);
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);
builder.move_to(endpoints.initial_pt.x, endpoints.initial_pt.y);
builder.line_to(endpoints.final_pt.x, endpoints.final_pt.y);
builder.move_to(right_pt.x, right_pt.y);
builder.line_to(endpoints.final_pt.x, endpoints.final_pt.y);
builder.line_to(left_pt.x, left_pt.y);
let path = builder.finish().unwrap();
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
}
Expand Down
1 change: 1 addition & 0 deletions src/windows/capture_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{
pub mod annotate;
pub mod capture;
pub mod models;
pub mod utils;

pub struct CaptureWindow {
pub scale_factor: f32,
Expand Down
19 changes: 19 additions & 0 deletions src/windows/capture_window/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::f32::consts::PI;

use iced::Point;

pub fn resolve_arrow_points(initial_pt: Point, final_pt: Point) -> (Point, Point) {
let line = final_pt - initial_pt;
let length = f32::sqrt(line.x.powf(2.0) + line.y.powf(2.0));
let size = if length < 60.0 { length / 2.0 } else { 30.0 };
let rad = line.y.atan2(line.x);
let right_pt = Point::new(
final_pt.x - size * (rad - PI / 5.0).cos(),
final_pt.y - size * (rad - PI / 5.0).sin(),
);
let left_pt = Point::new(
final_pt.x - size * (rad + PI / 5.0).cos(),
final_pt.y - size * (rad + PI / 5.0).sin(),
);
(right_pt , left_pt)
}

0 comments on commit 2df3a23

Please sign in to comment.