-
Notifications
You must be signed in to change notification settings - Fork 77
/
10_resize.rs
70 lines (67 loc) · 2.31 KB
/
10_resize.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Example 10: Resize Handling
// Show the different ways of resizing the window
use quicksilver::{
geom::{Rectangle, Vector},
graphics::{Color, Element, Graphics, Mesh, ResizeHandler, Vertex},
run, Input, Result, Settings, Window,
};
const SIZE: Vector = Vector { x: 800.0, y: 600.0 };
fn main() {
run(
Settings {
size: SIZE,
title: "Resizing example",
// By default, resizing is disabled: Here we need to enable it!
resizable: true,
..Settings::default()
},
app,
);
}
async fn app(window: Window, mut gfx: Graphics, mut input: Input) -> Result<()> {
// We'll use the triangle from the rgb_triangle example
let vertices = {
let top = Vertex {
pos: Vector::new(400.0, 200.0),
uv: None,
color: Color::RED,
};
let left = Vertex {
pos: Vector::new(200.0, 400.0),
uv: None,
color: Color::GREEN,
};
let right = Vertex {
pos: Vector::new(600.0, 400.0),
uv: None,
color: Color::BLUE,
};
vec![top, left, right]
};
let elements = vec![Element::Triangle([0, 1, 2])];
let mesh = Mesh {
vertices,
elements,
image: None,
};
// Create a ResizeHandler that will Fit the content to the screen, leaving off area if we need
// to. Here, we provide an aspect ratio of 4:3.
// By default, a ResizeHandler::Fit is applied with the same aspect ratio as the initial size
let resize_handler = ResizeHandler::Fit {
aspect_width: 4.0,
aspect_height: 3.0,
};
gfx.set_resize_handler(resize_handler);
loop {
while let Some(_) = input.next_event().await {}
gfx.clear(Color::BLACK);
// Fill the relevant part of the screen with white
// This helps us determine what part of the screen is the black bars, and what is the
// background. If we wanted white bars and a black background, we could simply clear to
// Color::WHITE and fill a rectangle of Color::BLACK
gfx.fill_rect(&Rectangle::new_sized(SIZE), Color::WHITE);
// Draw the RGB triangle, which lets us see the squash and stress caused by resizing
gfx.draw_mesh(&mesh);
gfx.present(&window)?;
}
}