pub struct Scene { /* private fields */ }Expand description
A 2D scene: the declarative description of what to draw.
Implementations§
Source§impl Scene
impl Scene
Sourcepub fn set_clip(&mut self, clip: Rect)
pub fn set_clip(&mut self, clip: Rect)
Sets the clip rectangle applied to subsequent primitives.
Sourcepub fn current_clip(&self) -> Rect
pub fn current_clip(&self) -> Rect
The current clip rectangle, for intersecting with local bounds.
Sourcepub fn set_owner(&mut self, owner: u64)
pub fn set_owner(&mut self, owner: u64)
Sets the emitting widget’s identity for subsequent primitives.
Sourcepub fn set_bounds(&mut self, bounds: Rect)
pub fn set_bounds(&mut self, bounds: Rect)
Declares the box the widget about to paint was laid out in. Text primitives
record it, which is how the renderer knows what a line of text covers — a
Primitive::Text otherwise says only where it begins. Set by the widget walk
before every paint; a scene built by hand leaves it UNBOUNDED, and the
renderer then keeps that text above everything, as it always did.
Sourcepub fn current_bounds(&self) -> Rect
pub fn current_bounds(&self) -> Rect
The box currently being painted into.
Sourcepub fn push_primitive(&mut self, primitive: Primitive)
pub fn push_primitive(&mut self, primitive: Primitive)
Appends an already-formed primitive, with its clip and owner already baked in. This replays a cached subtree (a repaint boundary) as-is, without repainting it.
Sourcepub fn split_off(&mut self, start: usize) -> Vec<Primitive>
pub fn split_off(&mut self, start: usize) -> Vec<Primitive>
Removes and returns the primitives from index start onwards, preserving
order. This wraps an already-painted subtree in a layer
(Primitive::Layer): paint the subtree, then move its range of primitives
into a layer, giving it group opacity.
Sourcepub fn push_faded(&mut self, primitive: &Primitive, opacity: f32)
pub fn push_faded(&mut self, primitive: &Primitive, opacity: f32)
Replays an existing primitive at reduced opacity — an exit fade.
Sourcepub fn fill_rect(&mut self, rect: Rect, color: Color)
pub fn fill_rect(&mut self, rect: Rect, color: Color)
Adds a solid rectangle, with square corners and no border.
Sourcepub fn draw_shape(&mut self, rect: Rect, shape: ShapeBorder, fill: Color)
pub fn draw_shape(&mut self, rect: Rect, shape: ShapeBorder, fill: Color)
Adds a rectangle with rounded corners (uniform through f32, or per corner
through BorderRadius) and/or a border.
Fills a shape, and draws its edge if it has one.
Three of the four shapes are rounded rectangles once the box is known, so they go
down the same path as Scene::draw_rect and cost nothing extra; a bevel becomes
a filled path plus a stroked one. A caller who does this by hand at each site gets
the fast path wrong somewhere, which is the whole reason this is one function.
Sourcepub fn draw_rect(
&mut self,
rect: Rect,
color: Color,
radius: impl Into<BorderRadius>,
border_width: f32,
border_color: Color,
)
pub fn draw_rect( &mut self, rect: Rect, color: Color, radius: impl Into<BorderRadius>, border_width: f32, border_color: Color, )
Adds a flat-coloured rectangle, with an optional border.
Sourcepub fn gradient_rect(
&mut self,
rect: Rect,
color: Color,
color2: Color,
dir: [f32; 2],
radius: impl Into<BorderRadius>,
border_width: f32,
border_color: Color,
)
pub fn gradient_rect( &mut self, rect: Rect, color: Color, color2: Color, dir: [f32; 2], radius: impl Into<BorderRadius>, border_width: f32, border_color: Color, )
Adds a linear-gradient rectangle (color → color2 along dir).
Sourcepub fn shadow(
&mut self,
rect: Rect,
color: Color,
radius: impl Into<BorderRadius>,
blur: f32,
)
pub fn shadow( &mut self, rect: Rect, color: Color, radius: impl Into<BorderRadius>, blur: f32, )
Adds a soft shadow — a rounded rectangle with a blurred edge, no border.
Sourcepub fn fill_path(&mut self, path: &Path, color: Color)
pub fn fill_path(&mut self, path: &Path, color: Color)
Fills a vector path with a flat colour, using the non-zero rule.
Sourcepub fn fill_path_gradient(
&mut self,
path: &Path,
from_color: Color,
to_color: Color,
from: Point,
to: Point,
)
pub fn fill_path_gradient( &mut self, path: &Path, from_color: Color, to_color: Color, from: Point, to: Point, )
Fills a path with a linear gradient: from_color at from, to_color at
to, clamped outside that span.
The two points are in the path’s own space rather than relative to its box, so the fade can be aimed at the thing it is about — the edge a glow springs from, the depth of a band — which is what an ellipse cap needs, its bounding box being mostly off screen.
Sourcepub fn fill_path_radial(
&mut self,
path: &Path,
from_color: Color,
to_color: Color,
center: Point,
radii: Size,
inner: f32,
)
pub fn fill_path_radial( &mut self, path: &Path, from_color: Color, to_color: Color, center: Point, radii: Size, inner: f32, )
Fills a path with a radial gradient: from_color on the ellipse of radii
inner × radii about center, to_color on the ellipse of radii radii.
This is what a shape with a curved boundary needs in order to fade to nothing all the way round: a straight fade reaches zero on a line, so it still leaves an edge wherever the shape turns away from that line.
Sourcepub fn stroke_path(&mut self, path: &Path, color: Color, width: f32)
pub fn stroke_path(&mut self, path: &Path, color: Color, width: f32)
Strokes a path’s outline (colour plus width), with no fill.
Sourcepub fn paint_path(
&mut self,
path: &Path,
fill: Option<Color>,
stroke: Option<Stroke>,
)
pub fn paint_path( &mut self, path: &Path, fill: Option<Color>, stroke: Option<Stroke>, )
Adds a path filled and/or stroked — both passes in one primitive.
Sourcepub fn draw_image(
&mut self,
image: &Arc<ImageData>,
rect: Rect,
uv: Rect,
tint: Color,
)
pub fn draw_image( &mut self, image: &Arc<ImageData>, rect: Rect, uv: Rect, tint: Color, )
Draws an image into rect, sampling the uv sub-region (in 0..1) and
tinting it by tint (white = unchanged). Low level: see Scene::image
for automatic fitting through crate::BoxFit.
Sourcepub fn image(&mut self, image: &Arc<ImageData>, rect: Rect, fit: BoxFit)
pub fn image(&mut self, image: &Arc<ImageData>, rect: Rect, fit: BoxFit)
Draws an image fitted into rect per fit (aspect preserved, letterboxed
or cropped), untinted.
Sourcepub fn layer(&mut self, opacity: f32, build: impl FnOnce(&mut Scene))
pub fn layer(&mut self, opacity: f32, build: impl FnOnce(&mut Scene))
Composes a layer: build fills a subgroup of primitives, which is then
composited as one at opacity (0..1). Unlike an opacity applied
primitive by primitive, the group alpha stays correct where the inner
primitives overlap. The layer inherits the current clip and owner.
Sourcepub fn transformed(
&mut self,
transform: LayerTransform,
build: impl FnOnce(&mut Scene),
)
pub fn transformed( &mut self, transform: LayerTransform, build: impl FnOnce(&mut Scene), )
Composes a transformed layer: build fills a subgroup of primitives, which is
rendered flat and then composited through transform.
The transform belongs to the group and not to the primitives inside it, which is the whole point: a rotation applies to a rectangle, a line of text and an image at once, and none of them has to know how to rotate itself.
Paint the group where it fits, not where it lands. The renderer draws the group flat into a window-sized texture and composites that through the transform, so anything laid out flat beyond the window is gone before the transform could have brought it back inside. A group that ends up near an edge should be built at the origin and carried there by the transform.
Sourcepub fn masked(&mut self, mask: ShaderMask, build: impl FnOnce(&mut Scene))
pub fn masked(&mut self, mask: ShaderMask, build: impl FnOnce(&mut Scene))
Adds a masked group: everything build emits is composited as one, with
mask multiplied into it, so the shader’s alpha becomes the group’s transparency.
A fade has to be a group. Applied primitive by primitive it would fade each of them against the background separately, and where two of them overlap the result is neither one nor the other.
Sourcepub fn rich_text(&mut self, position: Point, runs: Vec<TextRun>)
pub fn rich_text(&mut self, position: Point, runs: Vec<TextRun>)
Adds rich text: resolved runs, mixing styles and colours, laid out as one piece, anchored by its top-left corner.
Sourcepub fn rich_text_wrapped(
&mut self,
position: Point,
runs: Vec<TextRun>,
max_width: f32,
)
pub fn rich_text_wrapped( &mut self, position: Point, runs: Vec<TextRun>, max_width: f32, )
Adds a rich paragraph: the runs wrap beyond max_width, and the render’s
wrapping matches the layout’s.
Sourcepub fn text(
&mut self,
position: Point,
text: impl Into<String>,
style: &ResolvedTextStyle,
color: Color,
)
pub fn text( &mut self, position: Point, text: impl Into<String>, style: &ResolvedTextStyle, color: Color, )
Adds a line of text, anchored by its top-left corner — size, weight and italics
from the ResolvedTextStyle.
It takes a resolved style and not a TextStyle because
this is the bottom of the chain: a shaper needs a number and there is nobody left
below to ask one of. color is settled the same way, by the caller, usually
against the theme — which is the one answer this crate cannot work out for itself.
There is deliberately no form of this that takes a bare f32 size. A number
arriving here has been through TextStyle::resolved,
which is the single place the reader’s font size is applied; a second door taking a
raw constant is a door onto text that ignores the reader, and forty-seven call sites
walked through it (milestone 406).
Sourcepub fn text_wrapped(
&mut self,
position: Point,
text: impl Into<String>,
style: &ResolvedTextStyle,
color: Color,
max_width: f32,
)
pub fn text_wrapped( &mut self, position: Point, text: impl Into<String>, style: &ResolvedTextStyle, color: Color, max_width: f32, )
Adds a paragraph: styled text that wraps beyond max_width, the render’s
wrapping matching the layout’s.
Sourcepub fn text_block(
&mut self,
position: Point,
text: impl Into<String>,
style: &ResolvedTextStyle,
color: Color,
block: TextBlock,
)
pub fn text_block( &mut self, position: Point, text: impl Into<String>, style: &ResolvedTextStyle, color: Color, block: TextBlock, )
Adds a block of styled text: a box of a known width, which the text may be wrapped in and is aligned inside.
The three settings travel together because they are one decision. A width with no wrapping is a line that knows where its box ends and runs past it anyway — which is what a clipped or ellipsised line is — and an alignment with no width has nothing to align against.
Sourcepub fn rich_text_block(
&mut self,
position: Point,
runs: Vec<TextRun>,
block: TextBlock,
)
pub fn rich_text_block( &mut self, position: Point, runs: Vec<TextRun>, block: TextBlock, )
Adds a block of rich text: resolved runs in a box of a known width, which they
may be wrapped in and are aligned inside. The rich counterpart of
Scene::text_block.
Sourcepub fn primitives(&self) -> &[Primitive]
pub fn primitives(&self) -> &[Primitive]
The primitives, in insertion order, which is drawing order.