@@ -92,25 +92,43 @@ 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, width : 1.0 } ) )
95+ . paint ( move |ctx| ctx. draw ( & Display { lh_frame, width : 1.0 , height : 1.0 } ) )
9696 . x_bounds ( [ 0.0 , 1.0 ] )
9797 . y_bounds ( [ 0.0 , 1.0 ] )
9898}
9999
100100struct Display {
101101 lh_frame : Frame ,
102102 width : f64 ,
103+ height : f64 ,
103104}
104105
105106impl Shape for Display {
106107 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 = ( grid_width / LIGHTHOUSE_COLS ) . min ( grid_height / LIGHTHOUSE_ROWS ) ;
108+ // Figure out the bounds in the terminal coordinate system.
109+ // Note that min_y and max_y are flipped since the canvas coordinate
110+ // system has an upward-facing y-axis (as opposed to the terminal grid)
111+
112+ let Some ( ( bounds_min_x, bounds_max_y) ) = painter. get_point ( 0.0 , 0.0 ) else { return } ;
113+ let Some ( ( bounds_max_x, bounds_min_y) ) = painter. get_point ( self . width , self . height ) else { return } ;
114+
115+ let bounds_width = bounds_max_x - bounds_min_x;
116+ let bounds_height = bounds_max_y - bounds_min_y;
117+
118+ // Compute the scale of each "pixel" in the lighthouse frame
119+
120+ let scale = ( bounds_width / LIGHTHOUSE_COLS ) . min ( bounds_height / LIGHTHOUSE_ROWS ) ;
121+
122+ // Compute the actual size within the terminal coordinate system.
123+ // We compute another `min_x` and `min_y` since we want to align
124+ // the lighthouse display to the bottom left corner.
125+
126+ let _width = scale * LIGHTHOUSE_COLS ;
127+ let height = scale * LIGHTHOUSE_ROWS ;
128+ let min_x = bounds_min_x;
129+ let min_y = bounds_min_y + bounds_height - height;
130+
131+ // Draw the lighthouse display
114132
115133 for y in 0 ..LIGHTHOUSE_ROWS {
116134 for x in 0 ..LIGHTHOUSE_COLS {
@@ -119,9 +137,13 @@ impl Shape for Display {
119137 ( ( lh_color. red as u32 ) << 16 ) | ( lh_color. green as u32 ) << 8 | lh_color. blue as u32
120138 ) ;
121139
122- for dy in 0 ..scale. min ( grid_height - 1 ) {
123- for dx in 0 ..scale. min ( grid_width - 1 ) {
124- painter. paint ( x * scale + dx, y * scale + dy, tui_color) ;
140+ for dy in 0 ..scale {
141+ for dx in 0 ..scale {
142+ painter. paint (
143+ min_x + x * scale + dx,
144+ min_y + y * scale + dy,
145+ tui_color
146+ ) ;
125147 }
126148 }
127149 }
0 commit comments