ltn/pages/
freehand_boundary.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
use std::collections::BTreeSet;

use geom::{Distance, Polygon, QuadTree};
use map_gui::tools::EditPolygon;
use map_model::RoadID;
use widgetry::tools::Lasso;
use widgetry::{
    Color, Drawable, EventCtx, GeomBatch, GfxCtx, Key, Line, Outcome, Panel, State, Text, TextExt,
    Widget,
};

use crate::components::{AppwidePanel, Mode};
use crate::logic::CustomBoundary;
use crate::{mut_partitioning, pages, App, NeighbourhoodID, Transition};

pub struct FreehandBoundary {
    appwide_panel: AppwidePanel,
    left_panel: Panel,

    name: String,
    id: Option<NeighbourhoodID>,
    custom: Option<CustomBoundary>,
    draw_custom: Drawable,
    edit: EditPolygon,
    lasso: Option<Lasso>,
    quadtree: QuadTree<RoadID>,

    // TODO Add WorldOutcome::DragEnd and plumb here if this is useful otherwise, or some kind of
    // rate-limiting
    queued_recalculate: bool,
}

impl FreehandBoundary {
    pub fn blank(ctx: &mut EventCtx, app: &mut App, name: String) -> Box<dyn State<App>> {
        let appwide_panel = AppwidePanel::new(ctx, app, Mode::FreehandBoundary);
        let left_panel = make_panel(ctx, &appwide_panel.top_panel);

        Box::new(Self {
            appwide_panel,
            left_panel,
            id: None,
            custom: None,
            draw_custom: Drawable::empty(ctx),
            edit: EditPolygon::new(ctx, Vec::new(), false),
            lasso: None,
            name,
            quadtree: make_quadtree(app),
            queued_recalculate: false,
        })
    }

    pub fn edit_existing(
        ctx: &mut EventCtx,
        app: &mut App,
        name: String,
        id: NeighbourhoodID,
        custom: CustomBoundary,
    ) -> Box<dyn State<App>> {
        let appwide_panel = AppwidePanel::new(ctx, app, Mode::FreehandBoundary);
        let left_panel = make_panel(ctx, &appwide_panel.top_panel);
        let mut state = Self {
            appwide_panel,
            left_panel,
            id: Some(id),
            custom: Some(custom),
            draw_custom: Drawable::empty(ctx),
            edit: EditPolygon::new(ctx, Vec::new(), false),
            lasso: None,
            name,
            quadtree: make_quadtree(app),
            queued_recalculate: false,
        };
        state.edit = EditPolygon::new(
            ctx,
            state
                .custom
                .as_ref()
                .unwrap()
                .boundary_polygon
                .clone()
                .into_outer_ring()
                .into_points(),
            false,
        );
        state.draw_custom = render(ctx, app, state.custom.as_ref().unwrap());
        Box::new(state)
    }

    pub fn new_from_polygon(
        ctx: &mut EventCtx,
        app: &mut App,
        name: String,
        polygon: Polygon,
    ) -> Box<dyn State<App>> {
        let appwide_panel = AppwidePanel::new(ctx, app, Mode::FreehandBoundary);
        let left_panel = make_panel(ctx, &appwide_panel.top_panel);
        let mut state = Self {
            appwide_panel,
            left_panel,
            id: None,
            custom: None,
            draw_custom: Drawable::empty(ctx),
            edit: EditPolygon::new(ctx, polygon.into_outer_ring().into_points(), false),
            lasso: None,
            name,
            quadtree: make_quadtree(app),
            queued_recalculate: false,
        };
        state.recalculate(ctx, app);
        Box::new(state)
    }

    fn recalculate(&mut self, ctx: &EventCtx, app: &App) {
        self.queued_recalculate = false;
        if let Ok(ring) = self.edit.get_ring() {
            self.custom = Some(polygon_to_custom_boundary(
                app,
                ring.into_polygon(),
                self.name.clone(),
                &self.quadtree,
            ));
            self.draw_custom = render(ctx, app, self.custom.as_ref().unwrap());
        }
    }
}

impl State<App> for FreehandBoundary {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        if let Some(ref mut lasso) = self.lasso {
            if let Some(polygon) = lasso.event(ctx) {
                let polygon = polygon.simplify(50.0);

                self.lasso = None;
                self.edit =
                    EditPolygon::new(ctx, polygon.clone().into_outer_ring().into_points(), false);

                self.recalculate(ctx, app);
                self.left_panel = make_panel(ctx, &self.appwide_panel.top_panel);
            }
            return Transition::Keep;
        }

        if self.edit.event(ctx) {
            self.queued_recalculate = true;
        }
        // TODO This doesn't recalculate when pressing the leafblower key
        if self.queued_recalculate && ctx.input.left_mouse_button_released() {
            self.recalculate(ctx, app);
        }

        // PreserveState doesn't matter, can't switch proposals in FreehandBoundary anyway
        if let Some(t) =
            self.appwide_panel
                .event(ctx, app, &crate::save::PreserveState::Route, help)
        {
            return t;
        }
        if let Some(t) = app
            .session
            .layers
            .event(ctx, &app.cs, Mode::FreehandBoundary, None)
        {
            return t;
        }
        if let Outcome::Clicked(x) = self.left_panel.event(ctx) {
            match x.as_ref() {
                "Cancel" => {
                    return Transition::Replace(pages::PickArea::new_state(ctx, app));
                }
                "Confirm" => {
                    if let Some(custom) = self.custom.take() {
                        let id = if let Some(id) = self.id {
                            // Overwrite the existing one
                            mut_partitioning!(app).custom_boundaries.insert(id, custom);
                            id
                        } else {
                            mut_partitioning!(app).add_custom_boundary(custom)
                        };
                        // TODO Clicking is weird, acts like we click load proposal
                        return Transition::Replace(pages::DesignLTN::new_state(ctx, app, id));
                    }
                }
                "Select freehand" => {
                    self.lasso = Some(Lasso::new(Distance::meters(1.0)));
                    self.left_panel = make_panel_for_lasso(ctx, &self.appwide_panel.top_panel);
                }
                _ => unreachable!(),
            }
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.appwide_panel.draw(g);
        self.left_panel.draw(g);
        app.session.layers.draw(g, app);
        if let Some(ref lasso) = self.lasso {
            lasso.draw(g);
        }
        self.edit.draw(g);
        g.redraw(&self.draw_custom);
    }
}

fn make_panel(ctx: &mut EventCtx, top_panel: &Panel) -> Panel {
    crate::components::LeftPanel::builder(
        ctx,
        top_panel,
        Widget::col(vec![
            Line("Draw custom neighbourhood boundary")
                .small_heading()
                .into_widget(ctx),
            ctx.style()
                .btn_outline
                .icon_text("system/assets/tools/select.svg", "Select freehand")
                .hotkey(Key::F)
                .build_def(ctx),
            Text::from_all(vec![
                Line("Press "),
                Line(Key::D.describe()).fg(ctx.style().text_hotkey_color),
                Line(" to displace points in some direction"),
            ])
            .into_widget(ctx),
            Widget::row(vec![
                ctx.style()
                    .btn_solid_primary
                    .text("Confirm")
                    .hotkey(Key::Enter)
                    .build_def(ctx),
                ctx.style()
                    .btn_solid_destructive
                    .text("Cancel")
                    .hotkey(Key::Escape)
                    .build_def(ctx),
            ]),
        ]),
    )
    .build(ctx)
}

fn make_panel_for_lasso(ctx: &mut EventCtx, top_panel: &Panel) -> Panel {
    crate::components::LeftPanel::builder(
        ctx,
        top_panel,
        Widget::col(vec![
            "Draw a custom boundary for a neighbourhood"
                .text_widget(ctx)
                .centered_vert(),
            Text::from_all(vec![
                Line("Click and drag").fg(ctx.style().text_hotkey_color),
                Line(" to sketch the boundary of this neighbourhood"),
            ])
            .into_widget(ctx),
        ]),
    )
    .build(ctx)
}

fn help() -> Vec<&'static str> {
    vec![
        "Draw neighbourhood boundaries here freeform.",
        "This is still experimental, but is useful when the regular Adjust Boundary tool fails.",
    ]
}

fn polygon_to_custom_boundary(
    app: &App,
    boundary_polygon: Polygon,
    name: String,
    quadtree: &QuadTree<RoadID>,
) -> CustomBoundary {
    let map = &app.per_map.map;

    // Find all roads inside the polygon at least partly
    let mut interior_roads = BTreeSet::new();
    for id in quadtree.query_bbox(boundary_polygon.get_bounds()) {
        let r = map.get_r(id);
        if boundary_polygon.intersects_polyline(&r.center_pts) && crate::is_driveable(r, map) {
            interior_roads.insert(r.id);
        }
    }

    // Border intersections are connected to these roads, but not inside the polygon
    let mut borders = BTreeSet::new();
    for r in &interior_roads {
        for i in map.get_r(*r).endpoints() {
            if !boundary_polygon.contains_pt(map.get_i(i).polygon.center()) {
                borders.insert(i);
            }
        }
    }

    CustomBoundary {
        name,
        boundary_polygon,
        borders,
        interior_roads,
    }
}

fn render(ctx: &EventCtx, app: &App, custom: &CustomBoundary) -> Drawable {
    let mut batch = GeomBatch::new();

    for i in &custom.borders {
        batch.push(Color::BLACK, app.per_map.map.get_i(*i).polygon.clone());
    }

    for r in &custom.interior_roads {
        batch.push(
            Color::GREEN.alpha(0.5),
            app.per_map.map.get_r(*r).get_thick_polygon(),
        );
    }

    ctx.upload(batch)
}

fn make_quadtree(app: &App) -> QuadTree<RoadID> {
    QuadTree::bulk_load(
        app.per_map
            .map
            .all_roads()
            .into_iter()
            .map(|r| r.center_pts.get_bounds().as_bbox(r.id))
            .collect(),
    )
}