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
mod filters;

use map_model::{IntersectionID, RoadID};
use widgetry::mapspace::{ObjectID, World};
use widgetry::{EventCtx, Key, Line, Panel, PanelBuilder, Widget, DEFAULT_CORNER_RADIUS};

use crate::shortcuts::Shortcuts;
use crate::{after_edit, App, BrowseNeighborhoods, Neighborhood, Transition};

// TODO This is only used for styling now
#[derive(PartialEq)]
pub enum Tab {
    Connectivity,
    Shortcuts,
}

impl Tab {
    fn make_buttons(self, ctx: &mut EventCtx, app: &App) -> Widget {
        let mut row = Vec::new();
        for (tab, label, key) in [
            (Tab::Connectivity, "Connectivity", Key::F1),
            (Tab::Shortcuts, "Shortcuts", Key::F2),
        ] {
            // TODO Match the TabController styling
            row.push(
                ctx.style()
                    .btn_tab
                    .text(label)
                    .corner_rounding(geom::CornerRadii {
                        top_left: DEFAULT_CORNER_RADIUS,
                        top_right: DEFAULT_CORNER_RADIUS,
                        bottom_left: 0.0,
                        bottom_right: 0.0,
                    })
                    .hotkey(key)
                    // We abuse "disabled" to denote "currently selected"
                    .disabled(self == tab)
                    .build_def(ctx),
            );
        }
        if app.session.consultation.is_none() {
            // TODO The 3rd doesn't really act like a tab
            row.push(
                ctx.style()
                    .btn_tab
                    .text("Adjust boundary")
                    .corner_rounding(geom::CornerRadii {
                        top_left: DEFAULT_CORNER_RADIUS,
                        top_right: DEFAULT_CORNER_RADIUS,
                        bottom_left: 0.0,
                        bottom_right: 0.0,
                    })
                    .hotkey(Key::B)
                    .build_def(ctx),
            );
        }

        Widget::row(row)
    }
}

pub struct EditNeighborhood {
    // Only pub for drawing
    pub world: World<Obj>,
    // True if we're editing filters, false if we're editing one-ways. (An enum is overkill)
    filters: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Obj {
    InteriorRoad(RoadID),
    InteriorIntersection(IntersectionID),
}
impl ObjectID for Obj {}

impl EditNeighborhood {
    pub fn temporary() -> Self {
        Self {
            world: World::unbounded(),
            filters: true,
        }
    }

    // TODO filters!
    pub fn new(
        ctx: &mut EventCtx,
        app: &App,
        neighborhood: &Neighborhood,
        shortcuts: &Shortcuts,
    ) -> Self {
        Self {
            world: filters::make_world(ctx, app, neighborhood, shortcuts),
            filters: true,
        }
    }

    pub fn panel_builder(
        &self,
        ctx: &mut EventCtx,
        app: &App,
        tab: Tab,
        top_panel: &Panel,
        per_tab_contents: Widget,
    ) -> PanelBuilder {
        let contents = Widget::col(vec![
            app.session.alt_proposals.to_widget(ctx, app),
            BrowseNeighborhoods::button(ctx, app),
            Line("Editing neighborhood")
                .small_heading()
                .into_widget(ctx),
            if self.filters {
                filters::widget(ctx, app)
            } else {
                todo!()
            }
            .section(ctx),
            tab.make_buttons(ctx, app),
            per_tab_contents,
            crate::route_planner::RoutePlanner::button(ctx),
        ]);
        crate::components::LeftPanel::builder(ctx, top_panel, contents)
    }

    /// If true, the neighborhood has changed and the caller should recalculate stuff, including
    /// the panel
    pub fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> bool {
        let outcome = self.world.event(ctx);
        if self.filters {
            filters::handle_world_outcome(ctx, app, outcome)
        } else {
            todo!()
        }
    }

    pub fn handle_panel_action(
        &self,
        ctx: &mut EventCtx,
        app: &mut App,
        action: &str,
        neighborhood: &Neighborhood,
        panel: &Panel,
    ) -> Option<Transition> {
        let id = neighborhood.id;
        match action {
            "Browse neighborhoods" => {
                // Recalculate the state to redraw any changed filters
                Some(Transition::Replace(BrowseNeighborhoods::new_state(
                    ctx, app,
                )))
            }
            "Adjust boundary" => Some(Transition::Replace(
                crate::select_boundary::SelectBoundary::new_state(ctx, app, id),
            )),
            "Connectivity" => Some(Transition::Replace(crate::connectivity::Viewer::new_state(
                ctx, app, id,
            ))),
            "Shortcuts" => Some(Transition::Replace(
                crate::shortcut_viewer::BrowseShortcuts::new_state(ctx, app, id, None),
            )),
            // Overkill to force all mode-specific code into the module
            "Create filters along a shape" => Some(Transition::Push(
                crate::components::FreehandFilters::new_state(
                    ctx,
                    neighborhood,
                    panel.center_of("Create filters along a shape"),
                ),
            )),
            "undo" => {
                let prev = app.session.modal_filters.previous_version.take().unwrap();
                app.session.modal_filters = prev;
                after_edit(ctx, app);
                // TODO Ideally, preserve panel state (checkboxes and dropdowns)
                Some(Transition::Recreate)
            }
            "Plan a route" => Some(Transition::Push(
                crate::route_planner::RoutePlanner::new_state(ctx, app),
            )),
            _ => None,
        }
    }
}