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

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

pub struct PolygonDebugger {
    panel: Panel,
    noun: String,
    items: Vec<Item>,
    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> {
        Box::new(PolygonDebugger {
            panel: Panel::new(Widget::col(vec![
                Widget::row(vec![
                    Line("Geometry debugger").small_heading().draw(ctx),
                    Btn::text_fg("X")
                        .build(ctx, "close", Key::Escape)
                        .align_right(),
                ]),
                Widget::row(vec![
                    // TODO inactive
                    Btn::text_fg("<").build(ctx, "previous", Key::LeftArrow),
                    "noun X/Y".draw_text(ctx).named("pointer"),
                    Btn::text_fg(">").build(ctx, "next", Key::RightArrow),
                ])
                .evenly_spaced(),
                Slider::horizontal(ctx, 100.0, 25.0, 0.0)
                    .named("slider")
                    .centered_horiz(),
            ]))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx),
            noun: noun.to_string(),
            items,
            center,
        })
    }
}

impl State 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" => {
                    let idx = (self.panel.slider("slider").get_percent()
                        * (self.items.len() - 1) as f64) as usize;
                    if idx != 0 {
                        self.panel
                            .slider_mut("slider")
                            .set_percent(ctx, (idx - 1) as f64 / (self.items.len() - 1) as f64);
                    }
                }
                "next" => {
                    let idx = (self.panel.slider("slider").get_percent()
                        * (self.items.len() - 1) as f64) as usize;
                    if idx != self.items.len() - 1 {
                        self.panel
                            .slider_mut("slider")
                            .set_percent(ctx, (idx + 1) as f64 / (self.items.len() - 1) as f64);
                    }
                }
                _ => unreachable!(),
            },
            _ => {}
        }
        // TODO Could be more efficient here
        let idx =
            (self.panel.slider("slider").get_percent() * (self.items.len() - 1) as f64) as usize;
        self.panel.replace(
            ctx,
            "pointer",
            format!("{} {}/{}", self.noun, idx + 1, self.items.len())
                .draw_text(ctx)
                .named("pointer"),
        );

        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();

        let idx =
            (self.panel.slider("slider").get_percent() * (self.items.len() - 1) as f64) as usize;
        match &self.items[idx] {
            Item::Point(pt) => {
                batch.append(
                    Text::from(Line(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(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(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);
    }
}