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
use geom::{Polygon, Pt2D, Triangle};
use map_gui::tools::PopupMsg;
use widgetry::{
    EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, State, Text,
    TextExt, VerticalAlignment, Widget,
};

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

pub struct PolygonDebugger {
    panel: Panel,
    noun: String,
    items: Vec<Item>,
    idx: usize,
    center: Option<Pt2D>,
}

pub enum Item {
    Point(Pt2D),
    Triangle(Triangle),
    Polygon(Polygon),
}

impl PolygonDebugger {
    pub fn new(
        ctx: &mut EventCtx,
        noun: &str,
        items: Vec<Item>,
        center: Option<Pt2D>,
    ) -> Box<dyn State<App>> {
        if items.is_empty() {
            return PopupMsg::new(ctx, "Woops", vec![format!("No {}, never mind", noun)]);
        }

        Box::new(PolygonDebugger {
            panel: Panel::new(Widget::col(vec![
                Widget::row(vec![
                    Line("Geometry debugger").small_heading().into_widget(ctx),
                    ctx.style().btn_close_widget(ctx),
                ]),
                Widget::row(vec![
                    // TODO Make these inactive when on the first/last. More generally, refactor
                    // some kind of pagination helper.
                    ctx.style()
                        .btn_prev()
                        .hotkey(Key::LeftArrow)
                        .build_widget(ctx, "previous"),
                    "noun X/Y".text_widget(ctx).named("pointer"),
                    ctx.style()
                        .btn_next()
                        .hotkey(Key::RightArrow)
                        .build_widget(ctx, "next"),
                ])
                .evenly_spaced(),
            ]))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx),
            noun: noun.to_string(),
            items,
            idx: 0,
            center,
        })
    }
}

impl State<App> for PolygonDebugger {
    fn event(&mut self, ctx: &mut EventCtx, _: &mut App) -> Transition {
        ctx.canvas_movement();

        match self.panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                "previous" => {
                    if self.idx != 0 {
                        self.idx -= 1;
                    }
                }
                "next" => {
                    if self.idx != self.items.len() - 1 {
                        self.idx += 1;
                    }
                }
                _ => unreachable!(),
            },
            _ => {}
        }
        self.panel.replace(
            ctx,
            "pointer",
            format!("{} {}/{}", self.noun, self.idx + 1, self.items.len()).text_widget(ctx),
        );

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        // This is drawn in screen-space, so zooming doesn't affect the text size
        let mut batch = GeomBatch::new();

        match &self.items[self.idx] {
            Item::Point(pt) => {
                batch.append(
                    Text::from(Line(self.idx.to_string()))
                        .bg(app.cs.panel_bg)
                        .render(g)
                        .centered_on(g.canvas.map_to_screen(*pt).to_pt()),
                );
            }
            Item::Triangle(ref tri) => {
                for pt in &[tri.pt1, tri.pt2, tri.pt3] {
                    batch.append(
                        Text::from(Line(self.idx.to_string()))
                            .bg(app.cs.panel_bg)
                            .render(g)
                            .centered_on(g.canvas.map_to_screen(*pt).to_pt()),
                    );
                }
                g.draw_polygon(app.cs.selected, Polygon::from_triangle(tri));
            }
            Item::Polygon(ref poly) => {
                g.draw_polygon(app.cs.selected, poly.clone());
                batch.append(
                    Text::from(Line(self.idx.to_string()))
                        .bg(app.cs.panel_bg)
                        .render(g)
                        .centered_on(g.canvas.map_to_screen(poly.center()).to_pt()),
                );
            }
        }
        if let Some(pt) = self.center {
            batch.append(
                Text::from(Line("c"))
                    .bg(app.cs.panel_bg)
                    .render(g)
                    .centered_on(g.canvas.map_to_screen(pt).to_pt()),
            );
        }

        let draw = g.upload(batch);
        g.fork_screenspace();
        g.redraw(&draw);
        g.unfork();

        self.panel.draw(g);
    }
}