pub struct NineSliceSource {
pub nine: NineSlice,
pub frame: Rect,
pub sheet_width: u32,
pub sheet_height: u32,
pub texels_per_unit: f32,
}Expand description
The art a nine-slice is expanded from: the insets, the frame they inset, and the sheet the frame sits in.
The sheet’s size is here because it is the UV divisor — the one number a
Rect on its own does not know, for exactly the reason
Sheet::uv lives on the sheet rather than on the rect.
No Eq: texels_per_unit is an f32, and f32
is not Eq.
Fields§
§nine: NineSliceThe insets that stay fixed, in texels.
frame: RectWhere the frame sits in the sheet image, in texels.
sheet_width: u32The sheet image’s width in texels.
sheet_height: u32The sheet image’s height in texels.
texels_per_unit: f32How many texels of art one caller unit is.
The fixed bands expand emits and
minimum_size come back divided by this, so a
source whose caller’s world is not one unit per texel does not have to
scale its camera to compensate. from_sheet defaults
it to 1 — one caller unit per texel, which is the behaviour this type
always had — and with_texels_per_unit is
the way a caller changes it.
Implementations§
Source§impl NineSliceSource
impl NineSliceSource
Sourcepub fn from_sheet(sheet: &Sheet, index: usize) -> Option<Self>
pub fn from_sheet(sheet: &Sheet, index: usize) -> Option<Self>
The source for frame index of sheet.
None when the sheet declares no nine-slice, when it has no frame at
that index, or when it has no size at all — the last because the UV
divisor would be zero and every quad would carry a NaN into a vertex
buffer, which draws nothing and gets blamed on the shader.
Sheet::uv refuses the same case for the same reason.
Sourcepub fn with_texels_per_unit(self, texels_per_unit: f32) -> Self
pub fn with_texels_per_unit(self, texels_per_unit: f32) -> Self
The source, with its texels-per-unit scale set.
The fixed bands expand emits and
minimum_size come back divided by
texels_per_unit. Any value is accepted: expand draws
nothing when the scale is not a positive finite number, the same way it
draws nothing for a nonsense target.
Sourcepub fn insets(&self) -> NineSlice
pub fn insets(&self) -> NineSlice
The insets this source actually expands with, in texels.
NineSliceSource::nine as given, trimmed so it cannot exceed the frame
— the same trimming NineSliceSource::expand applies, exposed because a
caller laying out around the fixed bands (a button sizing itself to its
corners) has to agree with the geometry rather than with the request.
Sourcepub fn minimum_size(&self) -> (f32, f32)
pub fn minimum_size(&self) -> (f32, f32)
The smallest target this source draws at its natural corner size, in
world units — NineSlice::minimum_size as floats, divided by
texels_per_unit so it is in the same caller
units as the bands expand emits.
Sourcepub fn expand(&self, target: [f32; 4]) -> NineQuads
pub fn expand(&self, target: [f32; 4]) -> NineQuads
The quads that draw this frame stretched to target.
target is [x, y, w, h] in world units, minimum corner first and Y
up — Sprite::rect’s layout, because that is what the quads become.
§What comes back
Between zero and nine quads, in image order (top row left to right, then
middle, then bottom). Corners are left/right wide and top/bottom
tall in world units — inset / texels_per_unit each, so at the default
of one caller unit per texel a target the size of the frame reproduces
the single sprite exactly. Edges stretch on one axis and the centre on
both.
Empty bands are not emitted. A band whose extent is zero in texels or
zero in world units is skipped, so a three-slice (top == bottom == 0)
comes back as three quads and a frame with no insets as one. A zero-area
quad is invisible and still costs a whole instance, and a UV rectangle of
zero extent is a division waiting to happen in anything that later wants
a texel-per-world-unit ratio.
The seams cannot bleed. The four cut lines on each axis are computed
once, and every quad indexes into them — so two adjacent quads share an
edge as the same f32, in world space and in UV space, rather than as
two numbers that happen to have been rounded the same way. A one-texel
seam between bands is exactly what independent per-quad arithmetic
produces.
§A target smaller than the corners
The fixed bands shrink in proportion and the stretched band vanishes.
A target 6 units wide on a slice with left = 4, right = 8 (at the
default scale of one caller unit per texel) draws a 2-unit left cap and a
4-unit right cap and no centre at all.
The alternatives were both worse. Refusing — no quads, or a clamp of
the target up to the minimum — makes a pipe squeezed below its two caps
either vanish or spill outside the rectangle it was given; a sprite that
disappears at one size and not another is the failure
NineSlice::fits_in already exists to prevent. Letting the corners
overlap inverts the middle band, which with no backface culling
rasterises a mirrored quad rather than nothing, and double-blends the
overlap.
Shrinking keeps three properties that matter more than the corners
staying literally fixed at a size where they cannot: the quads still tile
target exactly, nothing is drawn outside it, and the picture is
continuous — at exactly the minimum size this and the ordinary path agree,
so a pipe closing to nothing does not jump. The corners are squashed,
which is visible and is the honest thing for a size where “corners at
their natural size” is arithmetically impossible.
Each axis decides independently: a target narrower than left + right
but taller than top + bottom shrinks only horizontally.