Skip to content

Commit

Permalink
Improved styling of snap point system
Browse files Browse the repository at this point in the history
  • Loading branch information
Liby99 committed Oct 9, 2019
1 parent b63d9da commit 2dbd14b
Showing 1 changed file with 57 additions and 72 deletions.
129 changes: 57 additions & 72 deletions src/systems/interactions/create/point/snap_point_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,85 +168,33 @@ impl<'a> System<'a> for SnapPointSystem {
let mut has_circle_line_itsct = false;

for ((line_ent, line), (circle_ent, circle)) in closest_lines.iter().cartesian_product(&closest_circles) {
match line.intersect(*circle) {
CircleIntersect::TwoPoints(p1, p2) => {
let (dist_1, dist_2) = ((p1 - virtual_mouse_pos).magnitude(), (p2 - virtual_mouse_pos).magnitude());
let (ty, p) = if dist_1 < dist_2 {
(CircleIntersectionType::First, p1)
} else {
(CircleIntersectionType::Second, p2)
};
let actual = p.to_actual(&*vp);
let norm_dist = (mouse_pos - actual).magnitude() / SNAP_TO_INTERSECTION_THRES;
if norm_dist < 1.0 {
if maybe_smallest_dist.is_none() || norm_dist < maybe_smallest_dist.unwrap() {
maybe_smallest_dist = Some(norm_dist);
maybe_snap_point.set(SnapPoint {
position: p,
symbo: SnapPointType::SnapOnCircleLineIntersection(*circle_ent, *line_ent, ty),
});
has_circle_line_itsct = true;
}
}
let ci = line.intersect(*circle);
check_circle_intersection(&mouse_pos, &virtual_mouse_pos, &*vp, ci, maybe_smallest_dist.clone(), &mut |m| match m {
Some((p, norm_dist, ty)) => {
maybe_smallest_dist = Some(norm_dist);
maybe_snap_point.set(SnapPoint {
position: p,
symbo: SnapPointType::SnapOnCircleLineIntersection(*circle_ent, *line_ent, ty),
});
has_circle_line_itsct = true;
},
CircleIntersect::OnePoint(p) => {
let actual = p.to_actual(&*vp);
let norm_dist = (mouse_pos - actual).magnitude() / SNAP_TO_INTERSECTION_THRES;
if norm_dist < 1.0 {
if maybe_smallest_dist.is_none() || norm_dist < maybe_smallest_dist.unwrap() {
maybe_smallest_dist = Some(norm_dist);

maybe_snap_point.set(SnapPoint {
position: p,
symbo: SnapPointType::SnapOnCircleLineIntersection(*circle_ent, *line_ent, CircleIntersectionType::First),
});
has_circle_line_itsct = true;
}
}
},
CircleIntersect::None => ()
}
None => (),
});
}

if !has_circle_line_itsct {
for comb in closest_circles.iter().combinations(2) {
if let &[(c1_ent, c1), (c2_ent, c2)] = &*comb {
match c1.intersect(*c2) {
CircleIntersect::TwoPoints(p1, p2) => {
let (dist_1, dist_2) = ((p1 - virtual_mouse_pos).magnitude(), (p2 - virtual_mouse_pos).magnitude());
let (ty, p) = if dist_1 < dist_2 {
(CircleIntersectionType::First, p1)
} else {
(CircleIntersectionType::Second, p2)
};
let actual = p.to_actual(&*vp);
let norm_dist = (mouse_pos - actual).magnitude() / SNAP_TO_INTERSECTION_THRES;
if norm_dist < 1.0 {
if maybe_smallest_dist.is_none() || norm_dist < maybe_smallest_dist.unwrap() {
maybe_smallest_dist = Some(norm_dist);
maybe_snap_point.set(SnapPoint {
position: p,
symbo: SnapPointType::SnapOnCircleCircleIntersection(*c1_ent, *c2_ent, ty),
});
}
}
},
CircleIntersect::OnePoint(p) => {
let actual = p.to_actual(&*vp);
let norm_dist = (mouse_pos - actual).magnitude() / SNAP_TO_INTERSECTION_THRES;
if norm_dist < 1.0 {
if maybe_smallest_dist.is_none() || norm_dist < maybe_smallest_dist.unwrap() {
maybe_smallest_dist = Some(norm_dist);

maybe_snap_point.set(SnapPoint {
position: p,
symbo: SnapPointType::SnapOnCircleCircleIntersection(*c1_ent, *c2_ent, CircleIntersectionType::First),
});
}
}
check_circle_intersection(&mouse_pos, &virtual_mouse_pos, &*vp, c1.intersect(*c2), maybe_smallest_dist.clone(), &mut |m| match m {
Some((p, norm_dist, ty)) => {
maybe_smallest_dist = Some(norm_dist);
maybe_snap_point.set(SnapPoint {
position: p,
symbo: SnapPointType::SnapOnCircleCircleIntersection(*c1_ent, *c2_ent, ty),
});
},
CircleIntersect::None => ()
}
None => (),
});
}
}
}
Expand All @@ -257,4 +205,41 @@ impl<'a> System<'a> for SnapPointSystem {
maybe_snap_point.clear();
}
}
}

fn check_circle_intersection<F>(
mouse_pos: &Vector2,
virtual_mouse_pos: &Vector2,
viewport: &Viewport,
ci: CircleIntersect,
maybe_smallest_dist: Option<f64>,
callback: &mut F,
) where F : FnMut(Option<(Vector2, f64, CircleIntersectionType)>) -> () {
match ci {
CircleIntersect::TwoPoints(p1, p2) => {
let (dist_1, dist_2) = ((p1 - *virtual_mouse_pos).magnitude(), (p2 - *virtual_mouse_pos).magnitude());
let (ty, p) = if dist_1 < dist_2 {
(CircleIntersectionType::First, p1)
} else {
(CircleIntersectionType::Second, p2)
};
let actual = p.to_actual(viewport);
let norm_dist = (*mouse_pos - actual).magnitude() / SNAP_TO_INTERSECTION_THRES;
if norm_dist < 1.0 {
if maybe_smallest_dist.is_none() || norm_dist < maybe_smallest_dist.unwrap() {
callback(Some((p, norm_dist, ty)));
}
}
},
CircleIntersect::OnePoint(p) => {
let actual = p.to_actual(viewport);
let norm_dist = (*mouse_pos - actual).magnitude() / SNAP_TO_INTERSECTION_THRES;
if norm_dist < 1.0 {
if maybe_smallest_dist.is_none() || norm_dist < maybe_smallest_dist.unwrap() {
callback(Some((p, norm_dist, CircleIntersectionType::First)));
}
}
},
CircleIntersect::None => ()
}
}

0 comments on commit 2dbd14b

Please sign in to comment.