-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathsimple_ui.rs
95 lines (77 loc) · 2.29 KB
/
simple_ui.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
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
use nannou::prelude::*;
use nannou_egui::{self, egui, Egui};
fn main() {
nannou::app(model).update(update).run();
}
struct Settings {
resolution: u32,
scale: f32,
rotation: f32,
color: Srgb<u8>,
position: Vec2,
}
struct Model {
settings: Settings,
egui: Egui,
}
fn model(app: &App) -> Model {
// Create window
let window_id = app
.new_window()
.view(view)
.raw_event(raw_window_event)
.build()
.unwrap();
let window = app.window(window_id).unwrap();
let egui = Egui::from_window(&window);
Model {
egui,
settings: Settings {
resolution: 10,
scale: 200.0,
rotation: 0.0,
color: WHITE,
position: vec2(0.0, 0.0),
},
}
}
fn update(_app: &App, model: &mut Model, update: Update) {
let egui = &mut model.egui;
let settings = &mut model.settings;
egui.set_elapsed_time(update.since_start);
let ctx = egui.begin_frame();
egui::Window::new("Settings").show(&ctx, |ui| {
// Resolution slider
ui.label("Resolution:");
ui.add(egui::Slider::new(&mut settings.resolution, 1..=40));
// Scale slider
ui.label("Scale:");
ui.add(egui::Slider::new(&mut settings.scale, 0.0..=1000.0));
// Rotation slider
ui.label("Rotation:");
ui.add(egui::Slider::new(&mut settings.rotation, 0.0..=360.0));
// Random color button
let clicked = ui.button("Random color").clicked();
if clicked {
settings.color = rgb(random(), random(), random());
}
});
}
fn raw_window_event(_app: &App, model: &mut Model, event: &nannou::winit::event::WindowEvent) {
// Let egui handle things like keyboard and mouse input.
model.egui.handle_raw_event(event);
}
fn view(app: &App, model: &Model, frame: Frame) {
let settings = &model.settings;
let draw = app.draw();
draw.background().color(BLACK);
let rotation_radians = deg_to_rad(settings.rotation);
draw.ellipse()
.resolution(settings.resolution as f32)
.xy(settings.position)
.color(settings.color)
.rotate(-rotation_radians)
.radius(settings.scale);
draw.to_frame(app, &frame).unwrap();
model.egui.draw_to_frame(&frame).unwrap();
}