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
use std::collections::{BTreeMap, BTreeSet};

use geom::Distance;
use map_model::{Block, Perimeter};
use widgetry::mapspace::{ObjectID, World, WorldOutcome};
use widgetry::{
    Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Outcome, Panel, State,
    TextExt, VerticalAlignment, Widget,
};

use crate::app::{App, Transition};
use crate::ltn::Neighborhood;

const UNSELECTED: Color = Color::BLUE;
const SELECTED: Color = Color::CYAN;

pub struct SelectBoundary {
    panel: Panel,
    id_counter: usize,
    blocks: BTreeMap<Obj, Block>,
    world: World<Obj>,
    selected: BTreeSet<Obj>,
    draw_outline: Drawable,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Obj(usize);
impl ObjectID for Obj {}

impl SelectBoundary {
    pub fn new_state(
        ctx: &mut EventCtx,
        app: &App,
        initial_boundary: Option<Perimeter>,
    ) -> Box<dyn State<App>> {
        let mut state = SelectBoundary {
            panel: make_panel(ctx, app, false),
            id_counter: 0,
            blocks: BTreeMap::new(),
            world: World::bounded(app.primary.map.get_bounds()),
            selected: BTreeSet::new(),
            draw_outline: Drawable::empty(ctx),
        };

        ctx.loading_screen("calculate all blocks", |ctx, timer| {
            timer.start("find single blocks");
            let perimeters = Perimeter::find_all_single_blocks(&app.primary.map);
            timer.stop("find single blocks");

            timer.start_iter("blockify", perimeters.len());
            for perimeter in perimeters {
                timer.next();
                match perimeter.to_block(&app.primary.map) {
                    Ok(block) => {
                        let id = state.new_id();
                        state.add_block(ctx, id, UNSELECTED, block);
                    }
                    Err(err) => {
                        warn!("Failed to make a block from a perimeter: {}", err);
                    }
                }
            }
        });

        if let Some(perimeter) = initial_boundary {
            let mut included = Vec::new();
            for (id, block) in &state.blocks {
                if perimeter.contains(&block.perimeter) {
                    included.push(*id);
                }
            }
            for id in included {
                state.selected.insert(id);
                state.block_changed(ctx, app, id);
            }
        }

        state.world.initialize_hover(ctx);
        Box::new(state)
    }

    fn new_id(&mut self) -> Obj {
        let id = Obj(self.id_counter);
        self.id_counter += 1;
        id
    }

    fn add_block(&mut self, ctx: &mut EventCtx, id: Obj, color: Color, block: Block) {
        let mut obj = self
            .world
            .add(id)
            .hitbox(block.polygon.clone())
            .draw_color(color.alpha(0.5))
            .hover_alpha(0.8)
            .clickable();
        if self.selected.contains(&id) {
            obj = obj.hotkey(Key::Space, "remove")
        } else {
            obj = obj.hotkey(Key::Space, "add")
        }
        obj.build(ctx);
        self.blocks.insert(id, block);
    }

    fn merge_selected(&self) -> Vec<Perimeter> {
        let mut perimeters = Vec::new();
        for id in &self.selected {
            perimeters.push(self.blocks[&id].perimeter.clone());
        }
        Perimeter::merge_all(perimeters, false)
    }

    fn block_changed(&mut self, ctx: &mut EventCtx, app: &App, id: Obj) {
        let block = self.blocks.remove(&id).unwrap();
        self.world.delete_before_replacement(id);
        self.add_block(
            ctx,
            id,
            if self.selected.contains(&id) {
                SELECTED
            } else {
                UNSELECTED
            },
            block,
        );

        // Draw the outline of the current blocks
        let mut valid_blocks = 0;
        let mut batch = GeomBatch::new();

        for perimeter in self.merge_selected() {
            if let Ok(block) = perimeter.to_block(&app.primary.map) {
                if let Ok(outline) = block.polygon.to_outline(Distance::meters(10.0)) {
                    // Alternate colors, to help people figure out where two disjoint boundaries
                    // exist
                    // TODO Ideally have more than 2 colors to cycle through
                    batch.push(
                        if valid_blocks % 2 == 0 {
                            Color::RED
                        } else {
                            Color::GREEN
                        },
                        outline,
                    );
                }
                valid_blocks += 1;
            }
        }
        self.draw_outline = batch.upload(ctx);
        self.panel = make_panel(ctx, app, valid_blocks == 1);
    }
}

impl State<App> for SelectBoundary {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "Cancel" => {
                    return Transition::Pop;
                }
                "Confirm" => {
                    let mut perimeters = self.merge_selected();
                    assert_eq!(perimeters.len(), 1);
                    return Transition::Replace(super::viewer::Viewer::new_state(
                        ctx,
                        app,
                        Neighborhood::new(ctx, app, perimeters.pop().unwrap()),
                    ));
                }
                _ => unreachable!(),
            }
        }

        match self.world.event(ctx) {
            WorldOutcome::Keypress("add", id) => {
                self.selected.insert(id);
                self.block_changed(ctx, app, id);
            }
            WorldOutcome::Keypress("remove", id) => {
                self.selected.remove(&id);
                self.block_changed(ctx, app, id);
            }
            WorldOutcome::ClickedObject(id) => {
                if self.selected.contains(&id) {
                    self.selected.remove(&id);
                } else {
                    self.selected.insert(id);
                }
                self.block_changed(ctx, app, id);
            }
            _ => {}
        }
        // TODO Bypasses World...
        if ctx.redo_mouseover() {
            if let Some(id) = self.world.get_hovering() {
                if ctx.is_key_down(Key::LeftControl) {
                    if !self.selected.contains(&id) {
                        self.selected.insert(id);
                        self.block_changed(ctx, app, id);
                    }
                } else if ctx.is_key_down(Key::LeftShift) {
                    if self.selected.contains(&id) {
                        self.selected.remove(&id);
                        self.block_changed(ctx, app, id);
                    }
                }
            }
        }

        Transition::Keep
    }

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

fn make_panel(ctx: &mut EventCtx, app: &App, boundary_ok: bool) -> Panel {
    Panel::new_builder(Widget::col(vec![
        map_gui::tools::app_header(ctx, app, "Low traffic neighborhoods"),
        "Draw a custom boundary for a neighborhood"
            .text_widget(ctx)
            .centered_vert(),
        "Click to add/remove a block".text_widget(ctx),
        "Hold LCtrl and paint blocks to add".text_widget(ctx),
        "Hold LShift and paint blocks to remove".text_widget(ctx),
        Widget::row(vec![
            ctx.style()
                .btn_solid_primary
                .text("Confirm")
                .disabled(!boundary_ok)
                .disabled_tooltip("You must select one contiguous boundary")
                .build_def(ctx),
            ctx.style()
                .btn_solid_destructive
                .text("Cancel")
                .build_def(ctx),
        ]),
    ]))
    .aligned(HorizontalAlignment::Left, VerticalAlignment::Top)
    .build(ctx)
}