-
-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathmain.rs
More file actions
105 lines (100 loc) · 3.22 KB
/
main.rs
File metadata and controls
105 lines (100 loc) · 3.22 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use three_d::*;
fn main() {
let args: Vec<String> = std::env::args().collect();
let window = Window::new(WindowSettings {
title: "Shapes 2D!".to_string(),
max_size: Some((1280, 720)),
..Default::default()
})
.unwrap();
let context = window.gl().unwrap();
let mut rectangle = Rectangle::new_with_material(
&context,
vec2(200.0, 200.0),
degrees(45.0),
100.0,
200.0,
ColorMaterial {
color: Color::RED,
..Default::default()
},
)
.unwrap();
let mut circle = Circle::new_with_material(
&context,
vec2(500.0, 500.0),
200.0,
ColorMaterial {
color: Color::BLUE,
..Default::default()
},
)
.unwrap();
let mut line = Line::new_with_material(
&context,
vec2(0.0, 0.0),
vec2(
window.viewport().unwrap().width as f32,
window.viewport().unwrap().height as f32,
),
5.0,
ColorMaterial {
color: Color::GREEN,
..Default::default()
},
)
.unwrap();
window
.render_loop(move |frame_input: FrameInput| {
for event in frame_input.events.iter() {
match event {
Event::MousePress {
button,
position,
modifiers,
..
} => {
let pos = vec2(
(frame_input.device_pixel_ratio * position.0) as f32,
(frame_input.device_pixel_ratio * position.1) as f32,
);
if *button == MouseButton::Left && !modifiers.ctrl {
rectangle.set_center(pos);
}
if *button == MouseButton::Right && !modifiers.ctrl {
circle.set_center(pos);
}
if *button == MouseButton::Left && modifiers.ctrl {
line.set_endpoints(pos, line.end_point1());
}
if *button == MouseButton::Right && modifiers.ctrl {
line.set_endpoints(line.end_point0(), pos);
}
}
_ => {}
}
}
Screen::write(
&context,
ClearState::color_and_depth(0.8, 0.8, 0.8, 1.0, 1.0),
|| {
line.render(frame_input.viewport)?;
rectangle.render(frame_input.viewport)?;
circle.render(frame_input.viewport)?;
Ok(())
},
)
.unwrap();
if args.len() > 1 {
// To automatically generate screenshots of the examples, can safely be ignored.
FrameOutput {
screenshot: Some(args[1].clone().into()),
exit: true,
..Default::default()
}
} else {
FrameOutput::default()
}
})
.unwrap();
}