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
use geom::Ring;
use map_gui::tools::EditPolygon;
use widgetry::{
    EventCtx, GfxCtx, HorizontalAlignment, Line, Outcome, Panel, State, TextExt, VerticalAlignment,
    Widget,
};

use crate::{App, NeighborhoodID, Transition};

pub struct CustomizeBoundary {
    panel: Panel,
    edit: EditPolygon,
    id: NeighborhoodID,
}

impl CustomizeBoundary {
    pub fn new_state(ctx: &mut EventCtx, app: &App, id: NeighborhoodID) -> Box<dyn State<App>> {
        let points = app
            .session
            .partitioning
            .neighborhood_boundary_polygon(app, id)
            .into_points();
        Box::new(Self {
            id,
            panel: Panel::new_builder(Widget::col(vec![
                Widget::row(vec![
                    Line("Customize boundary").small_heading().into_widget(ctx),
                    ctx.style().btn_close_widget(ctx),
                ]),
                "For drawing only. You can't edit roads outside the normal boundary"
                    .text_widget(ctx),
                ctx.style().btn_solid_primary.text("Save").build_def(ctx),
            ]))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx),
            edit: EditPolygon::new(points),
        })
    }
}

impl State<App> for CustomizeBoundary {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        self.edit.event(ctx, app);

        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                "Save" => {
                    let mut pts = self.edit.get_points().to_vec();
                    pts.push(pts[0]);
                    if let Ok(ring) = Ring::new(pts) {
                        app.session
                            .partitioning
                            .override_neighborhood_boundary_polygon(self.id, ring.into_polygon());
                        return Transition::Multi(vec![Transition::Pop, Transition::Recreate]);
                    }
                    // Silently stay here so the user can try to fix
                }
                _ => unreachable!(),
            }
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, _: &App) {
        self.panel.draw(g);
        self.edit.draw(g);
    }
}