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
impl View
Sourcepub fn new(pos: Point<f32>, rect: Rect<f32>) -> View
pub fn new(pos: Point<f32>, rect: Rect<f32>) -> View
Create a new View from a pos point and a Rect
Sourcepub fn reset(&mut self, rect: Rect<f32>)
pub fn reset(&mut self, rect: Rect<f32>)
Reset the rect if you don’t want to you can use (set_sizes)[]
Sourcepub fn set_center(&mut self, pos: Point<f32>)
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}
Sourcepub fn set_viewport(&mut self, viewport: Rect<f32>)
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.
Sourcepub fn translate<T: Scalar + Into<f32>>(&mut self, offset: Vector<T>)
pub fn translate<T: Scalar + Into<f32>>(&mut self, offset: Vector<T>)
Move the view from actual position with the offset offset
.
Sourcepub fn update(&mut self)
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}
Sourcepub fn zoom(&mut self, zoom: f32)
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}
Sourcepub fn projection(&self) -> &Matrix4<f32>
pub fn projection(&self) -> &Matrix4<f32>
Return projection Matrix4.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> SetParameter for T
impl<T> SetParameter for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.