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
mod bike_network;
mod explore;
mod labels;
mod layers;
//mod magnifying;
mod predict;
mod quick_sketch;
mod route;
mod share;

use map_gui::tools::{grey_out_map, open_browser, CityPicker};
use widgetry::{
    EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Panel, SimpleState, State, Text, TextExt,
    VerticalAlignment, Widget,
};

pub use self::explore::ExploreMap;
pub use self::layers::Layers;
use crate::app::{App, Transition};
pub use predict::ModeShiftData;
pub use share::PROPOSAL_HOST_URL;

// The 3 modes are very different States, so TabController doesn't seem like the best fit
#[derive(PartialEq)]
pub enum Tab {
    Explore,
    Create,
    Route,
    PredictImpact,
}

pub trait TakeLayers {
    fn take_layers(self) -> Layers;
}

impl Tab {
    pub fn make_left_panel(self, ctx: &mut EventCtx, app: &App, contents: Widget) -> Panel {
        // Ideally TabController could manage this, but the contents of each section are
        // substantial, controlled by entirely different States.

        let mut contents = Some(contents.section(ctx));

        let mut col = vec![Widget::row(vec![
            ctx.style()
                .btn_plain
                .btn()
                .image_path("system/assets/pregame/logo.svg")
                .image_dims(50.0)
                .build_widget(ctx, "about A/B Street"),
            map_gui::tools::change_map_btn(ctx, app)
                .centered_vert()
                .align_right(),
        ])];

        col.push(
            ctx.style()
                .btn_tab
                .icon_text("system/assets/tools/pan.svg", "Explore")
                .hotkey(Key::Num1)
                .disabled(self == Tab::Explore)
                .build_def(ctx),
        );
        if self == Tab::Explore {
            col.push(contents.take().unwrap());
        }

        col.push(
            ctx.style()
                .btn_tab
                .icon_text("system/assets/tools/pencil.svg", "Create new bike lanes")
                .hotkey(Key::Num2)
                .disabled(self == Tab::Create)
                .build_def(ctx),
        );
        if self == Tab::Create {
            col.push(contents.take().unwrap());
        }

        col.push(
            ctx.style()
                .btn_tab
                .icon_text("system/assets/tools/pin.svg", "Plan a route")
                .hotkey(Key::Num3)
                .disabled(self == Tab::Route)
                .build_def(ctx),
        );
        if self == Tab::Route {
            col.push(contents.take().unwrap());
        }

        col.push(
            ctx.style()
                .btn_tab
                .icon_text("system/assets/meters/trip_histogram.svg", "Predict impact")
                .hotkey(Key::Num4)
                .disabled(self == Tab::PredictImpact)
                .build_def(ctx),
        );
        if self == Tab::PredictImpact {
            col.push(contents.take().unwrap());
        }

        let mut panel = Panel::new_builder(Widget::col(col))
            .exact_height(ctx.canvas.window_height)
            .aligned(HorizontalAlignment::Left, VerticalAlignment::Top);
        if self == Tab::Route {
            // Hovering on a card
            panel = panel.ignore_initial_events();
        }
        panel.build(ctx)
    }

    pub fn handle_action<T: TakeLayers + State<App>>(
        self,
        ctx: &mut EventCtx,
        app: &mut App,
        action: &str,
    ) -> Option<Transition> {
        match action {
            "about A/B Street" => Some(Transition::Push(About::new_state(ctx))),
            "change map" => {
                Some(Transition::Push(CityPicker::new_state(
                    ctx,
                    app,
                    Box::new(move |ctx, app| {
                        // Since we're totally changing maps, don't reuse the Layers
                        let layers = Layers::new(ctx, app);
                        Transition::Multi(vec![
                            Transition::Pop,
                            Transition::Replace(match self {
                                Tab::Explore => ExploreMap::new_state(ctx, app, layers),
                                Tab::Create => {
                                    quick_sketch::QuickSketch::new_state(ctx, app, layers)
                                }
                                Tab::Route => route::RoutePlanner::new_state(ctx, app, layers),
                                Tab::PredictImpact => {
                                    predict::ShowGaps::new_state(ctx, app, layers)
                                }
                            }),
                        ])
                    }),
                )))
            }
            "Explore" => Some(Transition::ConsumeState(Box::new(|state, ctx, app| {
                let state = state.downcast::<T>().ok().unwrap();
                vec![ExploreMap::new_state(ctx, app, state.take_layers())]
            }))),
            "Create new bike lanes" => {
                // This is only necessary to do coming from ExploreMap, but eh
                app.primary.current_selection = None;
                Some(Transition::ConsumeState(Box::new(|state, ctx, app| {
                    let state = state.downcast::<T>().ok().unwrap();
                    vec![quick_sketch::QuickSketch::new_state(
                        ctx,
                        app,
                        state.take_layers(),
                    )]
                })))
            }
            "Plan a route" => Some(Transition::ConsumeState(Box::new(|state, ctx, app| {
                let state = state.downcast::<T>().ok().unwrap();
                vec![route::RoutePlanner::new_state(
                    ctx,
                    app,
                    state.take_layers(),
                )]
            }))),
            "Predict impact" => Some(Transition::ConsumeState(Box::new(|state, ctx, app| {
                let state = state.downcast::<T>().ok().unwrap();
                vec![predict::ShowGaps::new_state(ctx, app, state.take_layers())]
            }))),
            _ => None,
        }
    }
}

struct About;

impl About {
    fn new_state(ctx: &mut EventCtx) -> Box<dyn State<App>> {
        let panel = Panel::new_builder(Widget::col(vec![
            Widget::row(vec![
                Line("About A/B Street").small_heading().into_widget(ctx),
                ctx.style().btn_close_widget(ctx),
            ]),
            Text::from_multiline(vec![
                Line("Created by Dustin Carlino, Yuwen Li, & Michael Kirk").small(),
                Line("Data from OpenStreetMap, King County GIS, King County LIDAR").small(),
            ])
            .into_widget(ctx),
            "This is a simplified version. Check out the full version below.".text_widget(ctx),
            ctx.style().btn_outline.text("abstreet.org").build_def(ctx),
        ]))
        .build(ctx);
        <dyn SimpleState<_>>::new_state(panel, Box::new(About))
    }
}

impl SimpleState<App> for About {
    fn on_click(&mut self, _: &mut EventCtx, _: &mut App, x: &str, _: &Panel) -> Transition {
        if x == "close" {
            return Transition::Pop;
        } else if x == "abstreet.org" {
            open_browser("https://abstreet.org");
        }
        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        grey_out_map(g, app);
    }
}