Skip to content

Commit 3d18efb

Browse files
committed
Scale canvas in integer steps
1 parent 0948bd0 commit 3d18efb

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/cmd/display.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,39 @@ fn display_canvas(lh_frame: Frame, title: String) -> impl Widget {
9292
.padding(Padding::new(1, 1, 0, 0)),
9393
)
9494
.marker(Marker::Block)
95-
.paint(move |ctx| ctx.draw(&Display { lh_frame }))
95+
.paint(move |ctx| ctx.draw(&Display { lh_frame, width: 1.0 }))
96+
.x_bounds([0.0, 1.0])
97+
.y_bounds([0.0, 1.0])
9698
}
9799

98100
struct Display {
99-
lh_frame: Frame
101+
lh_frame: Frame,
102+
width: f64,
100103
}
101104

102105
impl Shape for Display {
103106
fn draw(&self, painter: &mut Painter) {
107+
// Height is not needed since the canvas coordinate system starts from
108+
// the bottom, so y = 0 corresponds to the height in the terminal/grid
109+
// coordinate system.
110+
let Some((grid_width, grid_height)) = painter.get_point(self.width, 0.0) else {
111+
return
112+
};
113+
let scale_x = grid_width / LIGHTHOUSE_COLS;
114+
let scale_y = grid_height / LIGHTHOUSE_ROWS;
115+
104116
for y in 0..LIGHTHOUSE_ROWS {
105117
for x in 0..LIGHTHOUSE_COLS {
106-
let c = self.lh_frame.get(x, y);
107-
painter.paint(x, y, Color::from_u32(
108-
((c.red as u32) << 16) | (c.green as u32) << 8 | c.blue as u32,
109-
));
118+
let lh_color = self.lh_frame.get(x, y);
119+
let tui_color = Color::from_u32(
120+
((lh_color.red as u32) << 16) | (lh_color.green as u32) << 8 | lh_color.blue as u32
121+
);
122+
123+
for dy in 0..scale_y.min(grid_height - 1) {
124+
for dx in 0..scale_x.min(grid_width - 1) {
125+
painter.paint(x * scale_x + dx, y * scale_y + dy, tui_color);
126+
}
127+
}
110128
}
111129
}
112130
}

0 commit comments

Comments
 (0)