Skip to main content

Transform2D

Struct Transform2D 

Source
pub struct Transform2D { /* private fields */ }
Expand description

A 2D transform with position, rotation, scale, layer, and aspect ratio.

The position is expressed in normalized coordinates (0–1 across the screen). The aspect ratio corrects the horizontal component so that a square sprite appears square regardless of the viewport dimensions.

Call calc_matrix after mutating to recompute the 4×4 matrix used for rendering.

§Coordinate system

  • Position — normalized space: (0, 0) is bottom-left, (1, 1) is top-right.
  • Rotation — degrees around the Z axis (counter-clockwise).
  • Layer — draw order: higher values are rendered on top.

§Operations

CategoryMethods
Position — getterpos
Position — absolute setterset_pos_all, set_pos_x, set_pos_y
Position — relative movemove_all, move_x, move_y
Rotation — getter/setterrot, set_rot, rotate
Scale — getterscale
Scale — absolute setterset_scale_all, set_scale_same, set_scale_x, set_scale_y
Scale — relative addscale_all, scale_same, scale_x, scale_y
Layerlayer, set_layer
Aspectaspect, set_aspect
Matrixmatrix, calc_matrix

Implementations§

Source§

impl Transform2D

Source

pub fn calc_matrix(&mut self)

Recomputes the transformation matrix from the current pos/rot/scale.

Source

pub fn aspect(&self) -> f32

Returns the aspect ratio (width / height).

Source

pub fn set_aspect(&mut self, aspect: f32)

Sets the aspect ratio.

Source

pub fn pos(&self) -> Vector2<f32>

Returns the position in normalized coordinates.

Source

pub fn rot(&self) -> f32

Returns the rotation in degrees.

Source

pub fn layer(&self) -> u8

Returns the layer (draw order).

Source

pub fn scale(&self) -> Vector2<f32>

Returns the scale factor.

Source

pub fn matrix(&self) -> Matrix4<f32>

Returns the cached 4×4 transformation matrix.

Source

pub fn move_all(&mut self, x: f32, y: f32)

Translates by (x, y) in normalized coordinates.

Source

pub fn move_x(&mut self, x: f32)

Translates along the X axis.

Source

pub fn move_y(&mut self, y: f32)

Translates along the Y axis.

Source

pub fn set_pos_all(&mut self, x: f32, y: f32)

Sets the position to (x, y).

Source

pub fn set_pos_x(&mut self, x: f32)

Sets the X coordinate.

Source

pub fn set_pos_y(&mut self, y: f32)

Sets the Y coordinate.

Source

pub fn rotate(&mut self, rot: f32)

Adds rot degrees to the current rotation.

Source

pub fn set_rot(&mut self, rot: f32)

Sets the rotation to rot degrees.

Source

pub fn set_layer(&mut self, layer: u8)

Sets the layer.

Source

pub fn scale_all(&mut self, x: f32, y: f32)

Adds (x, y) to the current scale.

Source

pub fn scale_same(&mut self, xy: f32)

Adds xy to both scale components.

Source

pub fn scale_x(&mut self, x: f32)

Adds x to the scale X component.

Source

pub fn scale_y(&mut self, y: f32)

Adds y to the scale Y component.

Source

pub fn set_scale_all(&mut self, x: f32, y: f32)

Sets the scale to (x, y).

Source

pub fn set_scale_same(&mut self, xy: f32)

Sets both scale components to xy.

Source

pub fn set_scale_x(&mut self, x: f32)

Sets the scale X component.

Source

pub fn set_scale_y(&mut self, y: f32)

Sets the scale Y component.

Trait Implementations§

Source§

impl Clone for Transform2D

Source§

fn clone(&self) -> Transform2D

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 Debug for Transform2D

Source§

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

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

impl Default for Transform2D

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>