cotis_wgpu/drawable.rs
1//! Custom render command extension point for the wgpu backend.
2//!
3//! Implement [`WgpuDrawable`] on types not covered by [`crate::default_drawables`].
4//! Custom drawables receive a [`WgpuDrawContext`] with access to the frame's geometry batch,
5//! text state, scissor region, depth ordering, and DPI scale.
6//!
7//! # Examples
8//!
9//! ```rust,ignore
10//! use cotis_wgpu::drawable::{WgpuDrawable, WgpuDrawContext};
11//! use cotis_wgpu::pipeline::{UIColor, UIPosition, UICornerRadii};
12//!
13//! struct RedSquare;
14//!
15//! impl WgpuDrawable for RedSquare {
16//! fn draw(self: Box<Self>, ctx: &mut WgpuDrawContext<'_>) {
17//! ctx.geometry.filled_rectangle(
18//! UIPosition { x: 10.0, y: 10.0, z: *ctx.depth },
19//! UIPosition { x: 100.0, y: 100.0, z: *ctx.depth },
20//! UIColor { r: 1.0, g: 0.0, b: 0.0 },
21//! UICornerRadii {
22//! top_left: 0.0,
23//! top_right: 0.0,
24//! bottom_left: 0.0,
25//! bottom_right: 0.0,
26//! },
27//! );
28//! *ctx.depth -= 0.000_1;
29//! }
30//! }
31//! ```
32
33use crate::pipeline::GeometryBatch;
34use crate::pipeline::UIPosition;
35use crate::text::TextState;
36
37/// Per-frame drawing context passed to each [`WgpuDrawable`] command.
38pub struct WgpuDrawContext<'a> {
39 /// CPU-side geometry batch flushed to the UI render pipeline at end of frame.
40 pub geometry: &'a mut GeometryBatch,
41 /// Glyphon text state for queuing text during command execution.
42 pub text: &'a mut TextState,
43 /// Whether a clip region from [`cotis_defaults::render_commands::ClipStart`] is active.
44 pub scissor_active: bool,
45 /// Top-left corner of the active clip region in physical pixels.
46 pub scissor_position: UIPosition,
47 /// Width and height of the active clip region in physical pixels.
48 pub scissor_bounds: UIPosition,
49 /// Current draw depth; decremented by each command for painter's-order z-sorting.
50 pub depth: &'a mut f32,
51 /// Window scale factor from winit; multiply text font sizes by this value.
52 pub dpi_scale: f32,
53}
54
55/// A render command that can be executed against the wgpu backend.
56///
57/// Commands are consumed as `Box<Self>` and executed during frame presentation.
58/// See [`crate::default_drawables`] for built-in implementations of standard
59/// `cotis-defaults` command types.
60pub trait WgpuDrawable {
61 /// Executes this command against the given draw context.
62 ///
63 /// Decrement `ctx.depth` after drawing if z-ordering relative to other commands matters.
64 fn draw(self: Box<Self>, ctx: &mut WgpuDrawContext<'_>);
65}