View

Struct View 

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

A View is a 2D camera. It’s the screen viewport. You can make multiple views and use them as Minimap, 2nd player screen etc… The view is attached to a gust::Window.

Implementations§

Source§

impl View

Source

pub fn new(pos: Point<f32>, rect: Rect<f32>) -> View

Create a new View from a pos point and a Rect

Source

pub fn reset(&mut self, rect: Rect<f32>)

Reset the rect if you don’t want to you can use (set_sizes)[]

Source

pub fn set_center(&mut self, pos: Point<f32>)

Set pos of the view (usefull for game like 2D Zelda-Like)

Examples found in repository?
examples/view.rs (line 54)
41fn event_process(event: Event, window: &mut Window) {
42    match event.1 {
43        Events::Key(Key::Escape, _, _, _) => {
44            window.close();
45        }
46        Events::Key(Key::Up, _, _, _) => {
47            window.view_mut().zoom(2.0);
48        }
49        Events::Key(Key::Down, _, _, _) => {
50            window.view_mut().zoom(0.5);
51        }
52        Events::CursorPos(x, y) => {
53            let center = Vector::new(x as f32, y as f32);
54            window.view_mut().set_center(center);
55            window.set_mouse_pos(center)
56        }
57        _ => println!("Another event !"),
58    }
59}
Source

pub fn set_viewport(&mut self, viewport: Rect<f32>)

Set the viewport of the view (the viewport is given as a float factor 0.5 / 1.0 / 0.2 etc) That way people can simply handle screen part.

Source

pub fn set_sizes(&mut self, sizes: Vector<f32>)

Set the size of the rect

Source

pub fn translate<T: Scalar + Into<f32>>(&mut self, offset: Vector<T>)

Move the view from actual position with the offset offset.

Source

pub fn update(&mut self)

Update the view: Apply all transformations.

Examples found in repository?
examples/view.rs (line 28)
7fn main() {
8    let mut window = Window::new(gust::WIDTH, gust::HEIGHT, "Hello");
9    let tex_leave = Rc::new(Texture::from_path("examples/texture/Z.png").unwrap());
10    let tex_dirt = Rc::new(Texture::from_path("examples/texture/Dirt.png").unwrap());
11    let event_handler = EventHandler::new(&window);
12    let mut sprite = Sprite::from(&tex_dirt);
13    let mut leave = Sprite::from(&tex_leave);
14
15    leave.set_position(Point::new(300.0, 300.0));
16    window.set_clear_color(Color::new(0.0, 0.0, 1.0));
17    window.enable_cursor();
18    window.poll(None);
19    leave.set_scale(Vector::new(0.5, 0.5));
20    leave
21        .set_origin_to_center()
22        .unwrap_or_else(|e| println!("{}", e));
23    while window.is_open() {
24        window.poll_events();
25        leave.rotate(1.0);
26        leave.update();
27        sprite.update();
28        window.view_mut().update();
29
30        for event in event_handler.fetch() {
31            event_process(event, &mut window);
32        }
33
34        window.clear();
35        window.draw(&mut sprite);
36        window.draw(&mut leave);
37        window.display();
38    }
39}
Source

pub fn set_zoom(&mut self, zoom: f32)

Set the view zoom.

Source

pub fn zoom(&mut self, zoom: f32)

Multiply the current zoom by this one.

Examples found in repository?
examples/view.rs (line 47)
41fn event_process(event: Event, window: &mut Window) {
42    match event.1 {
43        Events::Key(Key::Escape, _, _, _) => {
44            window.close();
45        }
46        Events::Key(Key::Up, _, _, _) => {
47            window.view_mut().zoom(2.0);
48        }
49        Events::Key(Key::Down, _, _, _) => {
50            window.view_mut().zoom(0.5);
51        }
52        Events::CursorPos(x, y) => {
53            let center = Vector::new(x as f32, y as f32);
54            window.view_mut().set_center(center);
55            window.set_mouse_pos(center)
56        }
57        _ => println!("Another event !"),
58    }
59}
Source

pub fn get_zoom(&self) -> f32

Return zoom.

Source

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

Return projection Matrix4.

Source

pub fn sizes(&self) -> Vector<f32>

Return sizes in a Vector.

Source

pub fn postition(&self) -> Vector<f32>

Return position of the view in a Vector.

Trait Implementations§

Source§

impl Clone for View

Source§

fn clone(&self) -> View

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 View

Source§

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

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

impl From<Rect<f32>> for View

Source§

fn from(rect: Rect<f32>) -> View

Converts to this type from the input type.
Source§

impl PartialEq for View

Source§

fn eq(&self, other: &View) -> 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 StructuralPartialEq for View

Auto Trait Implementations§

§

impl Freeze for View

§

impl RefUnwindSafe for View

§

impl Send for View

§

impl Sync for View

§

impl Unpin for View

§

impl UnwindSafe for View

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

Source§

fn set<T>(&mut self, value: T) -> <T as Parameter<Self>>::Result
where T: Parameter<Self>,

Sets value as a parameter of 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§

unsafe 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.