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
use crate::app::App;
use crate::common::CommonState;
use crate::game::{State, Transition};
use geom::{Circle, Distance, LonLat, Pt2D, Ring};
use std::fs::File;
use std::io::{Error, Write};
use widgetry::{
    hotkey, Btn, Color, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, Text,
    VerticalAlignment, Widget,
};

const POINT_RADIUS: Distance = Distance::const_meters(10.0);
// Localized and internal, so don't put in ColorScheme.
const POINT_COLOR: Color = Color::RED;
const POLYGON_COLOR: Color = Color::BLUE.alpha(0.6);
const POINT_TO_MOVE: Color = Color::CYAN;
const LAST_PLACED_POINT: Color = Color::GREEN;

pub struct PolygonEditor {
    panel: Panel,
    name: String,
    points: Vec<LonLat>,
    mouseover_pt: Option<usize>,
    moving_pt: bool,
}

impl PolygonEditor {
    pub fn new(ctx: &mut EventCtx, name: String, mut points: Vec<LonLat>) -> Box<dyn State> {
        points.pop();
        Box::new(PolygonEditor {
            panel: Panel::new(Widget::col(vec![
                Widget::row(vec![
                    Line("Polygon editor").small_heading().draw(ctx),
                    Btn::text_fg("X")
                        .build(ctx, "close", hotkey(Key::Escape))
                        .align_right(),
                ]),
                Btn::text_fg("export as an Osmosis polygon filter").build_def(ctx, hotkey(Key::X)),
            ]))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx),
            name,
            points,
            mouseover_pt: None,
            moving_pt: false,
        })
    }
}

impl State for PolygonEditor {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        let gps_bounds = app.primary.map.get_gps_bounds();

        ctx.canvas_movement();

        if self.moving_pt {
            if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
                self.points[self.mouseover_pt.unwrap()] = pt.to_gps(gps_bounds);
            }
            if ctx.input.key_released(Key::LeftControl) {
                self.moving_pt = false;
            }

            return Transition::Keep;
        }

        match self.panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                "export as an Osmosis polygon filter" => {
                    if self.points.len() >= 3 {
                        save_as_osmosis(&self.name, &self.points).unwrap();
                    }
                }
                _ => unreachable!(),
            },
            _ => {}
        }

        if let Some(cursor) = ctx.canvas.get_cursor_in_map_space() {
            self.mouseover_pt = self.points.iter().position(|pt| {
                Circle::new(
                    Pt2D::from_gps(*pt, gps_bounds),
                    POINT_RADIUS / ctx.canvas.cam_zoom,
                )
                .contains_pt(cursor)
            });
        } else {
            self.mouseover_pt = None;
        }
        // TODO maybe click-and-drag is more intuitive
        if self.mouseover_pt.is_some() {
            if ctx.input.key_pressed(Key::LeftControl) {
                self.moving_pt = true;
            }
        } else if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
            if app.per_obj.left_click(ctx, "add a new point") {
                self.points.push(pt.to_gps(gps_bounds));
            }
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        let pts: Vec<Pt2D> = app.primary.map.get_gps_bounds().convert(&self.points);

        if pts.len() == 2 {
            g.draw_polygon(
                POINT_COLOR,
                geom::Line::must_new(pts[0], pts[1]).make_polygons(POINT_RADIUS / 2.0),
            );
        }
        if pts.len() >= 3 {
            let mut pts = pts.clone();
            pts.push(pts[0]);
            g.draw_polygon(POLYGON_COLOR, Ring::must_new(pts).to_polygon());
        }
        for (idx, pt) in pts.iter().enumerate() {
            let color = if Some(idx) == self.mouseover_pt {
                POINT_TO_MOVE
            } else if idx == pts.len() - 1 {
                LAST_PLACED_POINT
            } else {
                POINT_COLOR
            };
            g.draw_polygon(
                color,
                Circle::new(*pt, POINT_RADIUS / g.canvas.cam_zoom).to_polygon(),
            );
        }

        self.panel.draw(g);
        if self.mouseover_pt.is_some() {
            CommonState::draw_custom_osd(
                g,
                app,
                Text::from(Line("hold left Control to move point")),
            );
        } else {
            CommonState::draw_osd(g, app);
        }
    }
}

// https://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format
fn save_as_osmosis(name: &str, pts: &Vec<LonLat>) -> Result<(), Error> {
    let path = format!("{}.poly", name);
    let mut f = File::create(&path)?;

    writeln!(f, "{}", name)?;
    writeln!(f, "1")?;
    for gps in pts {
        writeln!(f, "     {}    {}", gps.x(), gps.y())?;
    }
    // Have to repeat the first point
    {
        writeln!(f, "     {}    {}", pts[0].x(), pts[0].y())?;
    }
    writeln!(f, "END")?;
    writeln!(f, "END")?;

    println!("Exported {}", path);
    Ok(())
}