-
Notifications
You must be signed in to change notification settings - Fork 169
/
app.rs
188 lines (159 loc) · 6.1 KB
/
app.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use eframe::{egui, epi};
/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
pub struct TemplateApp {
// Example stuff:
label: String,
value: f32,
painting: Painting,
}
impl Default for TemplateApp {
fn default() -> Self {
Self {
// Example stuff:
label: "Hello World!".to_owned(),
value: 2.7,
painting: Default::default(),
}
}
}
impl epi::App for TemplateApp {
fn name(&self) -> &str {
"egui template"
}
/// Called by the framework to load old app state (if any).
#[cfg(feature = "persistence")]
fn load(&mut self, storage: &dyn epi::Storage) {
*self = epi::get_value(storage, epi::APP_KEY).unwrap_or_default()
}
/// Called by the frame work to save state before shutdown.
#[cfg(feature = "persistence")]
fn save(&mut self, storage: &mut dyn epi::Storage) {
epi::set_value(storage, epi::APP_KEY, self);
}
/// Called each time the UI needs repainting, which may be many times per second.
/// Put your widgets into a `SidePanel`, `TopPanel`, `CentralPanel`, `Window` or `Area`.
fn update(&mut self, ctx: &egui::CtxRef, frame: &mut epi::Frame<'_>) {
let TemplateApp {
label,
value,
painting,
} = self;
// Examples of how to create different panels and windows.
// Pick whichever suits you.
// Tip: a good default choice is to just keep the `CentralPanel`.
// For inspiration and more examples, go to https://emilk.github.io/egui
egui::SidePanel::left("side_panel", 200.0).show(ctx, |ui| {
ui.heading("Side Panel");
ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(label);
});
ui.add(egui::Slider::new(value, 0.0..=10.0).text("value"));
if ui.button("Increment").clicked() {
*value += 1.0;
}
ui.with_layout(egui::Layout::bottom_up(egui::Align::Center), |ui| {
ui.add(
egui::Hyperlink::new("https://github.com/emilk/egui/").text("powered by egui"),
);
});
});
egui::TopPanel::top("top_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::menu::bar(ui, |ui| {
egui::menu::menu(ui, "File", |ui| {
if ui.button("Quit").clicked() {
frame.quit();
}
});
});
});
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("egui template");
ui.hyperlink("https://github.com/emilk/egui_template");
ui.add(egui::github_link_file_line!(
"https://github.com/emilk/egui_template/blob/master/",
"Direct link to source code."
));
egui::warn_if_debug_build(ui);
ui.separator();
ui.heading("Central Panel");
ui.label("The central panel the region left after adding TopPanel's and SidePanel's");
ui.label("It is often a great place for big things, like drawings:");
ui.heading("Draw with your mouse to paint:");
painting.ui_control(ui);
egui::Frame::dark_canvas(ui.style()).show(ui, |ui| {
painting.ui_content(ui);
});
});
if false {
egui::Window::new("Window").show(ctx, |ui| {
ui.label("Windows can be moved by dragging them.");
ui.label("They are automatically sized based on contents.");
ui.label("You can turn on resizing and scrolling if you like.");
ui.label("You would normally chose either panels OR windows.");
});
}
}
}
// ----------------------------------------------------------------------------
/// Example code for painting on a canvas with your mouse
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
struct Painting {
lines: Vec<Vec<egui::Pos2>>,
stroke: egui::Stroke,
}
impl Default for Painting {
fn default() -> Self {
Self {
lines: Default::default(),
stroke: egui::Stroke::new(1.0, egui::Color32::LIGHT_BLUE),
}
}
}
impl Painting {
pub fn ui_control(&mut self, ui: &mut egui::Ui) -> egui::Response {
ui.horizontal(|ui| {
egui::stroke_ui(ui, &mut self.stroke, "Stroke");
ui.separator();
if ui.button("Clear Painting").clicked() {
self.lines.clear();
}
})
.response
}
pub fn ui_content(&mut self, ui: &mut egui::Ui) -> egui::Response {
use egui::emath::{Pos2, Rect, RectTransform};
let (mut response, painter) =
ui.allocate_painter(ui.available_size_before_wrap_finite(), egui::Sense::drag());
let to_screen = RectTransform::from_to(
Rect::from_min_size(Pos2::ZERO, response.rect.square_proportions()),
response.rect,
);
let from_screen = to_screen.inverse();
if self.lines.is_empty() {
self.lines.push(vec![]);
}
let current_line = self.lines.last_mut().unwrap();
if let Some(pointer_pos) = response.interact_pointer_pos() {
let canvas_pos = from_screen * pointer_pos;
if current_line.last() != Some(&canvas_pos) {
current_line.push(canvas_pos);
response.mark_changed();
}
} else if !current_line.is_empty() {
self.lines.push(vec![]);
response.mark_changed();
}
let mut shapes = vec![];
for line in &self.lines {
if line.len() >= 2 {
let points: Vec<Pos2> = line.iter().map(|p| to_screen * *p).collect();
shapes.push(egui::Shape::line(points, self.stroke));
}
}
painter.extend(shapes);
response
}
}