Skip to content

Commit

Permalink
refactor: replace Endpoints with Vec<Point>
Browse files Browse the repository at this point in the history
  • Loading branch information
decipher3114 committed Dec 21, 2024
1 parent 3c76eed commit 88b3a23
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 50 deletions.
27 changes: 15 additions & 12 deletions src/windows/capture_window/annotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ impl Program<CaptureEvent, Theme> for CaptureWindow {
_cursor: Cursor,
) -> Vec<Geometry<Renderer>> {
let shapes_frame = self.cache.draw(renderer, bounds.size(), |frame| {
for shape in self.shapes.iter() {
draw_shape(frame, shape);
}
self.shapes.iter().for_each(
|shape| {
draw_shape(frame, shape)
}
)
});

let mut frame = Frame::new(renderer, bounds.size());
Expand Down Expand Up @@ -64,7 +66,7 @@ impl Program<CaptureEvent, Theme> for CaptureWindow {
SelectionMode::InProgress(initial_pt) => {
normalize(initial_pt, self.cursor_position)
}
SelectionMode::Area(endpoints) => (endpoints.initial_pt, endpoints.final_pt),
SelectionMode::Area(endpoints) => (endpoints[0], endpoints[1]),
};

let selection = Path::rectangle(top_left, (bottom_right - top_left).into());
Expand Down Expand Up @@ -196,15 +198,16 @@ impl Program<CaptureEvent, Theme> for CaptureWindow {
}

fn draw_shape(frame: &mut Frame, shape: &Shape) {
if let Some(endpoints) = shape.endpoints {
if shape.endpoints.len() > 0 {
let endpoints = shape.endpoints.as_slice();
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) = normalize(endpoints.initial_pt, endpoints.final_pt);
let (top_left, bottom_right) = normalize(endpoints[0], endpoints[1]);
let size = (bottom_right - top_left).into();
let path = Path::rectangle(top_left, size);
if shape.is_filled {
Expand All @@ -215,7 +218,7 @@ fn draw_shape(frame: &mut Frame, shape: &Shape) {
}
}
ShapeType::Ellipse => {
let (top_left, bottom_right) = normalize(endpoints.initial_pt, endpoints.final_pt);
let (top_left, bottom_right) = normalize(endpoints[0], endpoints[1]);
let size = bottom_right - top_left;
let radii = Vector::new(size.x / 2.0, size.y / 2.0);
let center = Point::new(top_left.x + radii.x, top_left.y + radii.y);
Expand All @@ -237,17 +240,17 @@ fn draw_shape(frame: &mut Frame, shape: &Shape) {
};
}
ShapeType::Line => {
let path = Path::line(endpoints.initial_pt, endpoints.final_pt);
let path = Path::line(endpoints[0], endpoints[1]);
frame.stroke(&path, stroke);
}
ShapeType::Arrow => {
let (right_pt, left_pt) =
resolve_arrow_points(endpoints.initial_pt, endpoints.final_pt);
resolve_arrow_points(endpoints[0], endpoints[1]);
let mut builder = Builder::new();
builder.move_to(endpoints.initial_pt);
builder.line_to(endpoints.final_pt);
builder.move_to(endpoints[0]);
builder.line_to(endpoints[1]);
builder.move_to(right_pt);
builder.line_to(endpoints.final_pt);
builder.line_to(endpoints[1]);
builder.line_to(left_pt);
let path = builder.build();
frame.stroke(&path, stroke);
Expand Down
19 changes: 10 additions & 9 deletions src/windows/capture_window/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl CaptureWindow {
.into_rgba8()
}
SelectionMode::Area(endpoints) => {
let (top_left, bottom_right) = (endpoints.initial_pt, endpoints.final_pt);
let (top_left, bottom_right) = (endpoints[0], endpoints[1]);
let x = top_left.x;
let y = top_left.y;
let size = bottom_right - top_left;
Expand Down Expand Up @@ -104,7 +104,7 @@ pub fn draw_shapes(image: &RgbaImage, shapes: Vec<Shape>) -> RgbaImage {
let mut pixmap = Pixmap::new(width, height).unwrap();
let transform = Transform::identity();
for shape in shapes.iter() {
let endpoints = shape.endpoints.unwrap();
let (initial_pt, final_pt) = (shape.endpoints[0], shape.endpoints[1]);
let mut paint = Paint::default();
let color = shape.color.into_paint(shape.is_solid);
paint.set_color(color);
Expand All @@ -114,7 +114,7 @@ pub fn draw_shapes(image: &RgbaImage, shapes: Vec<Shape>) -> RgbaImage {
}
match shape.shape_type {
ShapeType::Rectangle | ShapeType::Ellipse => {
let (top_left, bottom_right) = normalize(endpoints.initial_pt, endpoints.final_pt);
let (top_left, bottom_right) = normalize(initial_pt, final_pt);
let (x, y) = (top_left.x, top_left.y);
let size = bottom_right - top_left;
let w = size.x;
Expand All @@ -137,20 +137,21 @@ pub fn draw_shapes(image: &RgbaImage, shapes: Vec<Shape>) -> RgbaImage {
}
}
ShapeType::Line => {
let (initial_pt, final_pt) = (shape.endpoints[0], shape.endpoints[1]);
let mut builder = PathBuilder::new();
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(initial_pt.x, initial_pt.y);
builder.line_to(final_pt.x, final_pt.y);
let path = builder.finish().unwrap();
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
}
ShapeType::Arrow => {
let (right_pt, left_pt) =
resolve_arrow_points(endpoints.initial_pt, endpoints.final_pt);
resolve_arrow_points(initial_pt, final_pt);
let mut builder = PathBuilder::new();
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(initial_pt.x, initial_pt.y);
builder.line_to(final_pt.x, 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(final_pt.x, 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
35 changes: 15 additions & 20 deletions src/windows/capture_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use iced::{
};
use indexmap::IndexMap;
use models::{
CapturedWindow, Endpoints, Mode, SelectionMode, Shape, ShapeColor, ShapeStroke, ShapeType,
CapturedWindow, Mode, SelectionMode, Shape, ShapeColor, ShapeStroke, ShapeType,
};
use utils::normalize;
use xcap::image::RgbaImage;
Expand Down Expand Up @@ -80,7 +80,7 @@ impl CaptureWindow {
}
}
CaptureEvent::ChooseShapeType(shape_type, is_filled, is_solid) => {
self.shape.endpoints = None;
self.shape.endpoints = Vec::new();
self.mode = Mode::Draw;
self.shape.shape_type = shape_type;
self.shape.is_filled = is_filled;
Expand All @@ -94,10 +94,8 @@ impl CaptureWindow {
}
CaptureEvent::SetInitialPoint => match self.mode {
Mode::Draw => {
self.shape.endpoints = Some(Endpoints {
initial_pt: self.cursor_position,
final_pt: self.cursor_position,
})
self.shape.endpoints.push(self.cursor_position);
self.shape.endpoints.push(self.cursor_position);
}
Mode::Crop => {
self.selection_mode = SelectionMode::InProgress(self.cursor_position);
Expand All @@ -106,37 +104,34 @@ impl CaptureWindow {
CaptureEvent::UpdateCurrentPosition(final_pt) => {
self.cursor_position = final_pt;
match self.mode {
Mode::Draw => {
if let Some(ref mut endpoints) = self.shape.endpoints {
endpoints.final_pt = final_pt;
}
}
Mode::Crop => match self.selection_mode {
SelectionMode::FullScreen | SelectionMode::Window(_) => {
self.auto_detect_area();
}
_ => (),
},
Mode::Draw => {
if self.shape.endpoints.len() >= 2 {
self.shape.endpoints[1] = self.cursor_position;
}
}
}
}
CaptureEvent::SetFinalPoint => {
match self.mode {
Mode::Draw => {
if self.shape.endpoints.is_some() {
self.shapes.push(self.shape);
if ! self.shape.endpoints.is_empty() {
self.shapes.push(self.shape.clone());
self.cache.clear();
}
self.shape.endpoints = None
self.shape.endpoints.clear();
}
Mode::Crop => {
if let SelectionMode::InProgress(initial_pt) = self.selection_mode {
if self.cursor_position != initial_pt {
let (top_left, bottom_right) =
normalize(initial_pt, self.cursor_position);
self.selection_mode = SelectionMode::Area(Endpoints {
initial_pt: top_left,
final_pt: bottom_right,
})
self.selection_mode = SelectionMode::Area([top_left, bottom_right])
} else {
self.auto_detect_area();
}
Expand Down Expand Up @@ -269,7 +264,7 @@ impl CaptureWindow {

let mut overlay = column![vertical_space().height(5)];

if self.shape.endpoints.is_none() {
if self.shape.endpoints.is_empty() {
overlay = overlay.push(toolbar);
};

Expand All @@ -282,7 +277,7 @@ impl CaptureWindow {
format!("Area: {} x {}", area.x as u32, area.y as u32)
}
SelectionMode::Area(endpoints) => {
let area = endpoints.final_pt - endpoints.initial_pt;
let area = endpoints[1] - endpoints[0];
format!("Area: {} x {}", area.x as u32, area.y as u32)
}
};
Expand Down
12 changes: 3 additions & 9 deletions src/windows/capture_window/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum SelectionMode {
FullScreen,
Window(u32),
InProgress(Point),
Area(Endpoints),
Area([Point; 2]),
}

pub struct CapturedWindow {
Expand All @@ -26,16 +26,10 @@ pub enum Mode {
Crop,
}

#[derive(Debug, Default, Clone, Copy)]
pub struct Endpoints {
pub initial_pt: Point,
pub final_pt: Point,
}

#[derive(Debug, Default, Clone, Copy)]
#[derive(Debug, Default, Clone)]
pub struct Shape {
pub shape_type: ShapeType,
pub endpoints: Option<Endpoints>,
pub endpoints: Vec<Point>,
pub color: ShapeColor,
pub is_filled: bool,
pub is_solid: bool,
Expand Down

0 comments on commit 88b3a23

Please sign in to comment.