Skip to main content

NineSliceSource

Struct NineSliceSource 

Source
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: NineSlice

The insets that stay fixed, in texels.

§frame: Rect

Where the frame sits in the sheet image, in texels.

§sheet_width: u32

The sheet image’s width in texels.

§sheet_height: u32

The sheet image’s height in texels.

§texels_per_unit: f32

How 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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for NineSliceSource

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for NineSliceSource

Source§

impl Debug for NineSliceSource

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for NineSliceSource

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for NineSliceSource

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> HalThreadSafe for T
where T: Send + Sync,

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.