[frame_stats] Build stats layer layout based on viewport dimensions

This commit is contained in:
Arman Uguray 2023-03-05 11:26:58 -08:00 committed by Arman Uguray
parent 89fb1b89da
commit d7bacbcc3f
2 changed files with 31 additions and 13 deletions

View file

@ -305,7 +305,12 @@ fn run(
} }
builder.append(&fragment, Some(transform)); builder.append(&fragment, Some(transform));
if stats_toggle { if stats_toggle {
snapshot.draw_layer(&mut builder, &mut scene_params.text, width as f64); snapshot.draw_layer(
&mut builder,
&mut scene_params.text,
width as f64,
height as f64,
);
} }
let surface_texture = render_state let surface_texture = render_state
.surface .surface

View file

@ -33,45 +33,58 @@ pub struct Snapshot {
} }
impl Snapshot { impl Snapshot {
pub fn draw_layer(&self, sb: &mut SceneBuilder, text: &mut SimpleText, width: f64) { pub fn draw_layer(
let x_offset = width - 450.; &self,
sb: &mut SceneBuilder,
text: &mut SimpleText,
viewport_width: f64,
viewport_height: f64,
) {
let width = (viewport_width * 0.4).max(200.).min(400.);
let height = width * 0.3;
let x_offset = viewport_width - width;
let y_offset = viewport_height - height;
let offset = Affine::translate((x_offset, y_offset));
let text_height = height * 0.2;
let left_margin = width * 0.03;
let text_size = (text_height * 0.9) as f32;
sb.fill( sb.fill(
Fill::NonZero, Fill::NonZero,
Affine::IDENTITY, offset,
&Brush::Solid(Color::rgba8(0, 0, 0, 200)), &Brush::Solid(Color::rgba8(0, 0, 0, 200)),
None, None,
&Rect::new(x_offset, 0., width, 115.), &Rect::new(0., 0., width, height),
); );
text.add( text.add(
sb, sb,
None, None,
25., text_size,
Some(&Brush::Solid(Color::WHITE)), Some(&Brush::Solid(Color::WHITE)),
Affine::translate((x_offset + 15., 30.)), offset * Affine::translate((left_margin, text_height)),
&format!("Frame Time: {:.2} ms", self.frame_time_ms), &format!("Frame Time: {:.2} ms", self.frame_time_ms),
); );
text.add( text.add(
sb, sb,
None, None,
25., text_size,
Some(&Brush::Solid(Color::WHITE)), Some(&Brush::Solid(Color::WHITE)),
Affine::translate((x_offset + 15., 60.)), offset * Affine::translate((left_margin, 2. * text_height)),
&format!("Frame Time (min): {:.2} ms", self.frame_time_min_ms), &format!("Frame Time (min): {:.2} ms", self.frame_time_min_ms),
); );
text.add( text.add(
sb, sb,
None, None,
25., text_size,
Some(&Brush::Solid(Color::WHITE)), Some(&Brush::Solid(Color::WHITE)),
Affine::translate((x_offset + 15., 90.)), offset * Affine::translate((left_margin, 3. * text_height)),
&format!("Frame Time (max): {:.2} ms", self.frame_time_max_ms), &format!("Frame Time (max): {:.2} ms", self.frame_time_max_ms),
); );
text.add( text.add(
sb, sb,
None, None,
25., text_size,
Some(&Brush::Solid(Color::WHITE)), Some(&Brush::Solid(Color::WHITE)),
Affine::translate((x_offset + 300., 30.)), offset * Affine::translate((width * 0.67, text_height)),
&format!("FPS: {:.2}", self.fps), &format!("FPS: {:.2}", self.fps),
); );
} }