ltn/pages/
customize_boundary.rs

1use map_gui::tools::EditPolygon;
2use widgetry::{
3    EventCtx, GfxCtx, HorizontalAlignment, Line, Outcome, Panel, State, TextExt, VerticalAlignment,
4    Widget,
5};
6
7use crate::{mut_partitioning, App, Neighbourhood, NeighbourhoodID, Transition};
8
9pub struct CustomizeBoundary {
10    panel: Panel,
11    edit: EditPolygon,
12    id: NeighbourhoodID,
13}
14
15impl CustomizeBoundary {
16    pub fn new_state(ctx: &mut EventCtx, app: &App, id: NeighbourhoodID) -> Box<dyn State<App>> {
17        let points = Neighbourhood::new(app, id)
18            .boundary_polygon
19            .into_outer_ring()
20            .into_points();
21        Box::new(Self {
22            id,
23            panel: Panel::new_builder(Widget::col(vec![
24                Widget::row(vec![
25                    Line("Customize boundary").small_heading().into_widget(ctx),
26                    ctx.style().btn_close_widget(ctx),
27                ]),
28                "For drawing only. You can't edit roads outside the normal boundary"
29                    .text_widget(ctx),
30                ctx.style().btn_solid_primary.text("Save").build_def(ctx),
31            ]))
32            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
33            .build(ctx),
34            edit: EditPolygon::new(ctx, points, false),
35        })
36    }
37}
38
39impl State<App> for CustomizeBoundary {
40    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
41        self.edit.event(ctx);
42
43        if let Outcome::Clicked(x) = self.panel.event(ctx) {
44            match x.as_ref() {
45                "close" => {
46                    return Transition::Pop;
47                }
48                "Save" => {
49                    if let Ok(ring) = self.edit.get_ring() {
50                        mut_partitioning!(app)
51                            .override_neighbourhood_boundary_polygon(self.id, ring.into_polygon());
52                        return Transition::Multi(vec![Transition::Pop, Transition::Recreate]);
53                    }
54                    // Silently stay here so the user can try to fix
55                }
56                _ => unreachable!(),
57            }
58        }
59
60        Transition::Keep
61    }
62
63    fn draw(&self, g: &mut GfxCtx, _: &App) {
64        self.panel.draw(g);
65        self.edit.draw(g);
66    }
67}