Skip to content

Commit

Permalink
Setup quick measure functionality for intersecting objects and handle…
Browse files Browse the repository at this point in the history
… centerline cases for non intersecting objects. Add a dashed_line_with_pattern function that gives more granular control over dash_width and gap_width.
  • Loading branch information
singhutsav5502 committed Dec 21, 2024
1 parent e4b854b commit d1adbe5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
30 changes: 30 additions & 0 deletions editor/src/messages/portfolio/document/overlays/utility_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,36 @@ impl OverlayContext {
self.render_context.line_to(end.x, end.y);
self.render_context.set_stroke_style_str(color.unwrap_or(COLOR_OVERLAY_BLUE));
self.render_context.stroke();
self.render_context
.set_line_dash(&JsValue::from(js_sys::Array::new()))
.map_err(|error| log::warn!("Error drawing dashed line: {:?}", error))
.ok();
}

pub fn dashed_line_with_pattern(&mut self, start: DVec2, end: DVec2, color: Option<&str>, dash_width: f64, gap_width: f64) {
let start = start.round() - DVec2::splat(0.5);
let end = end.round() - DVec2::splat(0.5);

// Set the dash and gap pattern
let array = js_sys::Array::new();
array.push(&JsValue::from(dash_width));
array.push(&JsValue::from(gap_width));
self.render_context
.set_line_dash(&JsValue::from(array))
.map_err(|error| log::warn!("Error setting line dash: {:?}", error))
.ok();

self.render_context.begin_path();
self.render_context.move_to(start.x, start.y);
self.render_context.line_to(end.x, end.y);
self.render_context.set_stroke_style_str(color.unwrap_or(COLOR_OVERLAY_BLUE));
self.render_context.stroke();

// Reset the dash pattern after drawing
self.render_context
.set_line_dash(&JsValue::from(js_sys::Array::new()))
.map_err(|error| log::warn!("Error resetting line dash: {:?}", error))
.ok();
}

pub fn manipulator_handle(&mut self, position: DVec2, selected: bool) {
Expand Down
10 changes: 5 additions & 5 deletions editor/src/messages/tool/common_functionality/measure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::consts::{COLOR_OVERLAY_BLUE, COLOR_OVERLAY_GREEN};
use crate::consts::COLOR_OVERLAY_BLUE;
use crate::messages::portfolio::document::overlays::utility_types::{OverlayContext, Pivot};
use crate::messages::tool::tool_messages::tool_prelude::*;

Expand Down Expand Up @@ -39,7 +39,7 @@ fn draw_zero_axis_crossings(selected_bounds: Rect, hovered_bounds: Rect, transfo
let min_viewport = transform.transform_point2(line_start);
let max_viewport = transform.transform_point2(line_end);

overlay_context.dashed_line(min_viewport, max_viewport, None, Some(8.0));
overlay_context.dashed_line_with_pattern(min_viewport, max_viewport, None, 2.0, 2.0);

let min_viewport_y_selected = transform.transform_point2(DVec2::new(selected_x, selected_y));
let max_viewport_y_selected = transform.transform_point2(DVec2::new(selected_x, hovered_y));
Expand All @@ -56,7 +56,7 @@ fn draw_zero_axis_crossings(selected_bounds: Rect, hovered_bounds: Rect, transfo

let min_viewport_y_hovered = transform.transform_point2(DVec2::new(hovered_x, selected_y));
let max_viewport_y_hovered = transform.transform_point2(DVec2::new(hovered_x, hovered_y));
overlay_context.dashed_line(min_viewport_y_hovered, max_viewport_y_hovered, None, Some(8.0));
overlay_context.dashed_line_with_pattern(min_viewport_y_hovered, max_viewport_y_hovered, None, 2.0, 2.0);
}
fn draw_single_axis_zero_crossings(selected_bounds: Rect, hovered_bounds: Rect, transform: DAffine2, document_to_viewport: DAffine2, overlay_context: &mut OverlayContext) {
let transform_to_document = document_to_viewport.inverse() * transform;
Expand Down Expand Up @@ -123,7 +123,7 @@ fn draw_single_axis_zero_crossings(selected_bounds: Rect, hovered_bounds: Rect,
let min_viewport = transform.transform_point2(dashed_line_start);
let max_viewport = transform.transform_point2(dashed_line_end);

overlay_context.dashed_line(min_viewport, max_viewport, None, Some(8.0));
overlay_context.dashed_line_with_pattern(min_viewport, max_viewport, None, 2.0, 2.0);
} else if overlap_x {
let selected_facing_edge = if hovered_bounds.max().x < selected_bounds.min().x {
selected_bounds.min().x
Expand Down Expand Up @@ -182,7 +182,7 @@ fn draw_single_axis_zero_crossings(selected_bounds: Rect, hovered_bounds: Rect,
let min_viewport = transform.transform_point2(dashed_line_start);
let max_viewport = transform.transform_point2(dashed_line_end);

overlay_context.dashed_line(min_viewport, max_viewport, None, Some(8.0));
overlay_context.dashed_line_with_pattern(min_viewport, max_viewport, None, 2.0, 2.0);
}
}
fn draw_single_axis_one_crossings(selected_bounds: Rect, hovered_bounds: Rect, transform: DAffine2, document_to_viewport: DAffine2, overlay_context: &mut OverlayContext) {
Expand Down

0 comments on commit d1adbe5

Please sign in to comment.