Skip to main content

View2

Struct View2 

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

Object providing a world-to-model transform in 2D

Rendering and meshing happen in the ±1 square or cube; these are referred to as world coordinates. A View generates a homogeneous transform matrix that maps from positions in world coordinates to model coordinates, which can be whatever you want.

For example, the world-to-model transform could map the ±1 region onto the ±0.5 region, which would be a zoom transform.

Here’s an example of using a View2 to focus on the region [4, 6]:

let view = View2::from_center_and_scale(Vector2::new(5.0, 5.0), 1.0);

//   -------d-------
//   |             |
//   |             |
//   c      a      b
//   |             |
//   |             |
//   -------e-------
let a = view.transform_point(&Point2::new(0.0, 0.0));
assert_eq!(a, Point2::new(5.0, 5.0));

let b = view.transform_point(&Point2::new(1.0, 0.0));
assert_eq!(b, Point2::new(6.0, 5.0));

let c = view.transform_point(&Point2::new(-1.0, 0.0));
assert_eq!(c, Point2::new(4.0, 5.0));

let d = view.transform_point(&Point2::new(0.0, 1.0));
assert_eq!(d, Point2::new(5.0, 6.0));

let e = view.transform_point(&Point2::new(0.0, -1.0));
assert_eq!(e, Point2::new(5.0, 4.0));

See also RegionSize::screen_to_world, which converts from screen to world coordinates.

Implementations§

Source§

impl View2

Source

pub fn from_center_and_scale(center: Vector2<f32>, scale: f32) -> Self

Builds a camera from a center (in world coordinates) and a scale

The resulting camera will point at the center, and the viewport will be ± scale in size.

Source

pub fn components(&self) -> (Vector2<f32>, f32)

Returns a (center, scale) tuple

Source

pub fn from_components(center: Vector2<f32>, scale: f32) -> Self

Builds a view from its components

This function is identical to from_center_and_scale

Source

pub fn world_to_model(&self) -> Matrix3<f32>

Returns the world-to-model transform matrix

Source

pub fn transform_point(&self, p: &Point2<f32>) -> Point2<f32>

Transform a point from world to model space

Source

pub fn begin_translate(&self, start: Point2<f32>) -> TranslateHandle<2>

Begins a translation operation, given a point in world space

Source

pub fn translate(&mut self, h: &TranslateHandle<2>, pos: Point2<f32>) -> bool

Applies a translation (in world units) to the current camera position

Source

pub fn zoom(&mut self, amount: f32, pos: Option<Point2<f32>>) -> bool

Zooms the camera about a particular position (in world space)

Returns true if the view has changed, false otherwise

Trait Implementations§

Source§

impl Clone for View2

Source§

fn clone(&self) -> View2

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for View2

Source§

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

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

impl Default for View2

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for View2

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for View2

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for View2

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for View2

Source§

impl StructuralPartialEq for View2

Auto Trait Implementations§

§

impl Freeze for View2

§

impl RefUnwindSafe for View2

§

impl Send for View2

§

impl Sync for View2

§

impl Unpin for View2

§

impl UnsafeUnpin for View2

§

impl UnwindSafe for View2

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, 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,