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
use geom::{Circle, Distance, FindClosest, Polygon};
use sim::TripEndpoint;
use widgetry::{
    Color, DragDrop, Drawable, EventCtx, GeomBatch, GfxCtx, Image, Line, Outcome, RewriteColor,
    StackAxis, Text, Widget,
};

use crate::app::App;

/// Click to add waypoints, drag them, see the list on a panel and delete them. The caller owns the
/// Panel, since there's probably more stuff there too.
pub struct InputWaypoints {
    waypoints: Vec<Waypoint>,
    draw_waypoints: Drawable,
    hovering_on_waypt: Option<usize>,
    draw_hover: Drawable,
    // TODO Invariant not captured by these separate fields: when dragging is true,
    // hovering_on_waypt is fixed.
    dragging: bool,
    snap_to_endpts: FindClosest<TripEndpoint>,
}

// TODO Maybe it's been a while and I've forgotten some UI patterns, but this is painfully manual.
// I think we need a draggable map-space thing.
struct Waypoint {
    // TODO Different colors would also be helpful
    order: char,
    at: TripEndpoint,
    label: String,
    geom: GeomBatch,
    hitbox: Polygon,
}

impl InputWaypoints {
    pub fn new(ctx: &mut EventCtx, app: &App) -> InputWaypoints {
        let map = &app.primary.map;
        let mut snap_to_endpts = FindClosest::new(map.get_bounds());
        for i in map.all_intersections() {
            if i.is_border() {
                snap_to_endpts.add(TripEndpoint::Border(i.id), i.polygon.points());
            }
        }
        for b in map.all_buildings() {
            snap_to_endpts.add(TripEndpoint::Bldg(b.id), b.polygon.points());
        }

        InputWaypoints {
            waypoints: Vec::new(),
            draw_waypoints: Drawable::empty(ctx),
            hovering_on_waypt: None,
            draw_hover: Drawable::empty(ctx),
            dragging: false,
            snap_to_endpts,
        }
    }

    pub fn overwrite(&mut self, ctx: &mut EventCtx, app: &App, waypoints: Vec<TripEndpoint>) {
        self.waypoints.clear();
        for at in waypoints {
            self.waypoints
                .push(Waypoint::new(ctx, app, at, self.waypoints.len()));
        }
        self.update_waypoints_drawable(ctx);
        self.update_hover(ctx);
    }

    pub fn get_panel_widget(&self, ctx: &mut EventCtx) -> Widget {
        let mut drag_drop = DragDrop::new(ctx, "waypoint cards", StackAxis::Vertical);
        let mut delete_buttons = Vec::new();

        for (idx, waypt) in self.waypoints.iter().enumerate() {
            let batch = Text::from(Line(format!("{}) {}", waypt.order, waypt.label))).render(ctx);
            let bounds = batch.get_bounds();
            let image = Image::from_batch(batch, bounds)
                .color(RewriteColor::NoOp)
                .padding(16);

            let (default_batch, bounds) = image.clone().build_batch(ctx).unwrap();
            let (hovering_batch, _) = image
                .clone()
                .bg_color(ctx.style().btn_tab.bg_disabled.dull(0.3))
                .build_batch(ctx)
                .unwrap();
            let (selected_batch, _) = image
                .bg_color(ctx.style().btn_solid_primary.bg)
                .build_batch(ctx)
                .unwrap();

            drag_drop.push_card(
                idx,
                bounds.into(),
                default_batch,
                hovering_batch,
                selected_batch,
            );

            delete_buttons.push(
                ctx.style()
                    .btn_plain_destructive
                    .text("X")
                    .build_widget(ctx, &format!("delete waypoint {}", idx)),
            );
        }
        drag_drop.set_initial_state(None, None);

        Widget::col(vec![
            Widget::row(vec![
                drag_drop.into_widget(ctx),
                // TODO The alignment doesn't match the cards, but it's... usable
                Widget::col(delete_buttons).evenly_spaced(),
            ]),
            Widget::row(vec![
                Image::from_path("system/assets/tools/mouse.svg").into_widget(ctx),
                Text::from_all(vec![
                    Line("Click").fg(ctx.style().text_hotkey_color),
                    Line(" to add a waypoint, "),
                    Line("drag").fg(ctx.style().text_hotkey_color),
                    Line(" a waypoint to move it"),
                ])
                .into_widget(ctx),
            ]),
        ])
    }

    pub fn get_waypoints(&self) -> Vec<TripEndpoint> {
        self.waypoints.iter().map(|w| w.at).collect()
    }

    /// If the outcome from the panel isn't used by the caller, pass it along here. This handles
    /// calling `ctx.canvas_movement` when appropriate. When this returns true, something has
    /// changed, so the caller may want to update their view of the route and call
    /// `get_panel_widget` again.
    pub fn event(&mut self, ctx: &mut EventCtx, app: &mut App, outcome: Outcome) -> bool {
        if self.dragging {
            if ctx.redo_mouseover() && self.update_dragging(ctx, app) == Some(true) {
                return true;
            }
            if ctx.input.left_mouse_button_released() {
                self.dragging = false;
                self.update_hover(ctx);
            }
        } else {
            if ctx.redo_mouseover() {
                self.update_hover(ctx);
            }

            if self.hovering_on_waypt.is_none() {
                ctx.canvas_movement();
            } else if let Some((_, dy)) = ctx.input.get_mouse_scroll() {
                // Zooming is OK, but can't start click and drag
                ctx.canvas.zoom(dy, ctx.canvas.get_cursor());
            }

            if self.hovering_on_waypt.is_some() && ctx.input.left_mouse_button_pressed() {
                self.dragging = true;
            }

            if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
                if self.hovering_on_waypt.is_none() && ctx.normal_left_click() {
                    if let Some((at, _)) =
                        self.snap_to_endpts.closest_pt(pt, Distance::meters(30.0))
                    {
                        self.waypoints
                            .push(Waypoint::new(ctx, app, at, self.waypoints.len()));
                        self.update_waypoints_drawable(ctx);
                        self.update_hover(ctx);
                        return true;
                    }
                }
            }
        }

        match outcome {
            Outcome::Clicked(x) => {
                if let Some(x) = x.strip_prefix("delete waypoint ") {
                    let idx = x.parse::<usize>().unwrap();
                    self.waypoints.remove(idx);
                    // Recalculate labels, in case we deleted in the middle
                    for (idx, waypt) in self.waypoints.iter_mut().enumerate() {
                        *waypt = Waypoint::new(ctx, app, waypt.at, idx);
                    }

                    self.update_waypoints_drawable(ctx);
                    return true;
                } else {
                    panic!("Unknown InputWaypoints click {}", x);
                }
            }
            Outcome::DragDropReleased(_, old_idx, new_idx) => {
                self.waypoints.swap(old_idx, new_idx);
                // The order field is baked in, so calculate everything again from scratch
                let waypoints = self.get_waypoints();
                self.overwrite(ctx, app, waypoints);
                return true;
            }
            _ => {}
        }

        false
    }

    pub fn draw(&self, g: &mut GfxCtx) {
        g.redraw(&self.draw_waypoints);
        g.redraw(&self.draw_hover);
    }

    fn update_waypoints_drawable(&mut self, ctx: &mut EventCtx) {
        let mut batch = GeomBatch::new();
        for waypt in &self.waypoints {
            batch.append(waypt.geom.clone());
        }
        self.draw_waypoints = ctx.upload(batch);
    }

    fn update_hover(&mut self, ctx: &EventCtx) {
        self.hovering_on_waypt = None;

        if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
            self.hovering_on_waypt = self
                .waypoints
                .iter()
                .position(|waypt| waypt.hitbox.contains_pt(pt));
        }

        let mut batch = GeomBatch::new();
        if let Some(idx) = self.hovering_on_waypt {
            batch.push(Color::BLUE.alpha(0.5), self.waypoints[idx].hitbox.clone());
        }
        self.draw_hover = ctx.upload(batch);
    }

    // `Some(true)` means to update.
    fn update_dragging(&mut self, ctx: &mut EventCtx, app: &App) -> Option<bool> {
        let pt = ctx.canvas.get_cursor_in_map_space()?;
        let (at, _) = self.snap_to_endpts.closest_pt(pt, Distance::meters(30.0))?;

        let mut changed = false;
        let idx = self.hovering_on_waypt.unwrap();
        if self.waypoints[idx].at != at {
            self.waypoints[idx] = Waypoint::new(ctx, app, at, idx);
            self.update_waypoints_drawable(ctx);
            changed = true;
        }

        let mut batch = GeomBatch::new();
        // Show where we're currently snapped
        batch.push(Color::BLUE.alpha(0.5), self.waypoints[idx].hitbox.clone());
        self.draw_hover = ctx.upload(batch);

        Some(changed)
    }
}

impl Waypoint {
    fn new(ctx: &mut EventCtx, app: &App, at: TripEndpoint, idx: usize) -> Waypoint {
        let order = char::from_u32('A' as u32 + idx as u32).unwrap();
        let map = &app.primary.map;
        let (center, label) = match at {
            TripEndpoint::Bldg(b) => {
                let b = map.get_b(b);
                (b.polygon.center(), b.address.clone())
            }
            TripEndpoint::Border(i) => {
                let i = map.get_i(i);
                (i.polygon.center(), i.name(app.opts.language.as_ref(), map))
            }
            TripEndpoint::SuddenlyAppear(pos) => (pos.pt(map), pos.to_string()),
        };
        let circle = Circle::new(center, Distance::meters(30.0)).to_polygon();
        let mut geom = GeomBatch::new();
        geom.push(Color::RED, circle.clone());
        geom.append(
            Text::from(Line(format!("{}", order)).fg(Color::WHITE))
                .render(ctx)
                .centered_on(center),
        );
        let hitbox = circle;

        Waypoint {
            order,
            at,
            label,
            geom,
            hitbox,
        }
    }
}