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 bb1477b commit 7cd8e8c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ fn grid_overlay_isometric_dot(document: &DocumentMessageHandler, overlay_context
document_to_viewport.transform_point2(end),
Some(&("#".to_string() + &grid_color.rgba_hex())),
Some((spacing_x / cos_a) * document_to_viewport.matrix2.x_axis.length()),
None,
);
}
}
Expand Down
33 changes: 4 additions & 29 deletions editor/src/messages/portfolio/document/overlays/utility_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ impl OverlayContext {
}

pub fn line(&mut self, start: DVec2, end: DVec2, color: Option<&str>) {
self.dashed_line(start, end, color, None)
self.dashed_line(start, end, color, None, None)
}

pub fn dashed_line(&mut self, start: DVec2, end: DVec2, color: Option<&str>, dash_width: Option<f64>) {
pub fn dashed_line(&mut self, start: DVec2, end: DVec2, color: Option<&str>, dash_width: Option<f64>, gap_width: Option<f64>) {
let start = start.round() - DVec2::splat(0.5);
let end = end.round() - DVec2::splat(0.5);
if let Some(dash_width) = dash_width {
let gap_width = gap_width.unwrap_or(1.0);
let array = js_sys::Array::new();
array.push(&JsValue::from(1));
array.push(&JsValue::from(dash_width - 1.));
array.push(&JsValue::from(gap_width));
self.render_context
.set_line_dash(&JsValue::from(array))
.map_err(|error| log::warn!("Error drawing dashed line: {:?}", error))
Expand All @@ -80,32 +81,6 @@ impl OverlayContext {
.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) {
let position = position.round() - DVec2::splat(0.5);

Expand Down
8 changes: 4 additions & 4 deletions editor/src/messages/tool/common_functionality/measure.rs
Original file line number Diff line number Diff line change
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_with_pattern(min_viewport, max_viewport, None, 2.0, 2.0);
overlay_context.dashed_line(min_viewport, max_viewport, None, Some(2.0), Some(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_with_pattern(min_viewport_y_hovered, max_viewport_y_hovered, None, 2.0, 2.0);
overlay_context.dashed_line(min_viewport_y_hovered, max_viewport_y_hovered, None, Some(2.0), Some(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_with_pattern(min_viewport, max_viewport, None, 2.0, 2.0);
overlay_context.dashed_line(min_viewport, max_viewport, None, Some(2.0), Some(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_with_pattern(min_viewport, max_viewport, None, 2.0, 2.0);
overlay_context.dashed_line(min_viewport, max_viewport, None, Some(2.0), Some(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 7cd8e8c

Please sign in to comment.