Skip to main content

cranpose_render_common/
dev_overlay.rs

1//! Renderer-drawn dev overlay (fps counter): one shared graph builder so
2//! every backend draws the identical overlay without composition impact.
3
4use cranpose_ui_graphics::{Brush, Color, CornerRadii, DrawPrimitive, Point, Rect, Size};
5
6use crate::{
7    graph::{DrawPrimitiveNode, PrimitiveNode, TextPrimitiveNode},
8    scene_store::OverlayLayer,
9};
10
11/// Cache key for the last-built overlay; rebuilding only on change keeps the
12/// overlay free when the text is stable.
13#[derive(Clone, PartialEq, Eq)]
14pub struct DevOverlayKey {
15    /// The overlay text, for example the fps line.
16    pub text: String,
17    /// Bit pattern of the viewport width in logical pixels, so the key can be compared exactly.
18    pub viewport_width_bits: u32,
19    /// Bit pattern of the viewport height in logical pixels, so the key can be compared exactly.
20    pub viewport_height_bits: u32,
21}
22
23impl DevOverlayKey {
24    /// Builds the key for `text` drawn in a viewport of the given size in logical pixels.
25    pub fn new(text: &str, viewport: Size) -> Self {
26        Self {
27            text: text.to_string(),
28            viewport_width_bits: viewport.width.to_bits(),
29            viewport_height_bits: viewport.height.to_bits(),
30        }
31    }
32}
33
34/// The frame counter's overlay: its layer, and the window it lies in.
35#[derive(Clone, Debug)]
36pub struct FpsOverlay {
37    /// The size of the window the layer lies in, in logical pixels.
38    pub window: Size,
39    /// The pill that holds the stats text.
40    pub layer: OverlayLayer,
41}
42
43/// Build the dev overlay: a translucent pill with the stats text, laid out
44/// in `overlay`, in the top-right corner of `viewport`. The pill draws for
45/// no layout node; `node_id` keys its text.
46pub fn build_fps_overlay(
47    overlay: &cranpose_ui::PreparedTextLayout,
48    viewport: Size,
49    node_id: cranpose_core::NodeId,
50) -> FpsOverlay {
51    let padding = 8.0;
52    let Size {
53        width: text_width,
54        height: text_height,
55    } = overlay.size();
56    let x = (viewport.width - text_width - padding * 2.0).max(padding);
57    let y = padding;
58
59    let layer = OverlayLayer {
60        size: Size::new(text_width + padding, text_height + padding / 2.0),
61        origin: Point::new(x, y),
62        primitives: vec![
63            PrimitiveNode::Draw(Box::new(DrawPrimitiveNode {
64                primitive: DrawPrimitive::RoundRect {
65                    rect: Rect {
66                        x: 0.0,
67                        y: 0.0,
68                        width: text_width + padding,
69                        height: text_height + padding / 2.0,
70                    },
71                    brush: Brush::Solid(Color(0.0, 0.0, 0.0, 0.7)),
72                    radii: CornerRadii::uniform(4.0),
73                    stroke: None,
74                },
75                clip: None,
76            })),
77            PrimitiveNode::Text(Box::new(TextPrimitiveNode {
78                node_id,
79                rect: Rect {
80                    x: padding / 2.0,
81                    y: padding / 4.0,
82                    width: text_width,
83                    height: text_height,
84                },
85                render_text: overlay.render_text(),
86                layout: overlay.layout.clone(),
87                text_style: std::sync::Arc::clone(&overlay.visual_style),
88                font_size: overlay
89                    .visual_style
90                    .resolve_font_size(cranpose_ui_graphics::DEFAULT_FONT_SIZE),
91                overflow: cranpose_ui::TextOverflow::Clip,
92                clip: None,
93                paint: Default::default(),
94            })),
95        ],
96    };
97    FpsOverlay {
98        window: viewport,
99        layer,
100    }
101}