Speed up polygon lasso tool. I'm not sure why I ever thought simplifying

constantly was a good idea
This commit is contained in:
Dustin Carlino 2022-11-09 12:03:05 +00:00
parent 42457b5efe
commit c162cf438e
3 changed files with 9 additions and 8 deletions

View File

@ -240,7 +240,7 @@ impl State<App> for StoryMapEditor {
}
"Draw freehand" => {
return Transition::Push(Box::new(DrawFreehand {
lasso: Lasso::new(),
lasso: Lasso::new(Distance::meters(1.0)),
new_idx: self.story.markers.len(),
}));
}

View File

@ -2,7 +2,7 @@ use std::collections::BTreeSet;
use anyhow::Result;
use geom::Polygon;
use geom::{Distance, Polygon};
use map_gui::tools::DrawSimpleRoadLabels;
use widgetry::mapspace::{World, WorldOutcome};
use widgetry::tools::{Lasso, PopupMsg};
@ -298,7 +298,7 @@ impl State<App> for SelectBoundary {
));
}
"Select freehand" => {
self.lasso = Some(Lasso::new());
self.lasso = Some(Lasso::new(Distance::meters(1.0)));
self.left_panel = make_panel_for_lasso(ctx, &self.appwide_panel.top_panel);
}
_ => unreachable!(),

View File

@ -8,13 +8,16 @@ use crate::{Color, EventCtx, GfxCtx};
pub struct Lasso {
points: Vec<Pt2D>,
polygon: Option<Polygon>,
threshold: Distance,
}
impl Lasso {
pub fn new() -> Self {
/// How far do points need to be spaced apart to add?
pub fn new(threshold: Distance) -> Self {
Self {
points: Vec::new(),
polygon: None,
threshold,
}
}
@ -35,16 +38,14 @@ impl Lasso {
if ctx.redo_mouseover() {
if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
if self.points.last().as_ref().unwrap().dist_to(pt) > Distance::meters(0.1) {
if self.points.last().as_ref().unwrap().dist_to(pt) > self.threshold {
self.points.push(pt);
// TODO It's better if the user doesn't close the polygon themselves. When they
// try to, usually the result is the smaller polygon chunk
let mut copy = self.points.clone();
copy.push(copy[0]);
self.polygon = Ring::new(copy)
.ok()
.map(|ring| ring.into_polygon().simplify(1.0));
self.polygon = Ring::new(copy).ok().map(|ring| ring.into_polygon());
}
}
}