egui/emigui/src/emigui.rs

132 lines
4.3 KiB
Rust
Raw Normal View History

2019-01-12 22:07:30 +00:00
use std::sync::Arc;
use crate::{
2019-01-21 07:48:32 +00:00
label, layout,
2020-04-12 10:07:51 +00:00
layout::Region,
mesher::Mesher,
2019-04-25 16:07:36 +00:00
types::{GuiInput, PaintCmd},
2019-01-12 22:07:30 +00:00
widgets::*,
2019-12-02 19:08:49 +00:00
FontDefinitions, Fonts, Mesh, RawInput, Texture,
2019-01-12 22:07:30 +00:00
};
#[derive(Clone, Copy, Default)]
struct Stats {
num_vertices: usize,
num_triangles: usize,
}
/// Encapsulates input, layout and painting for ease of use.
pub struct Emigui {
pub last_input: RawInput,
pub data: Arc<layout::Data>,
stats: Stats,
2020-04-17 12:25:27 +00:00
anti_alias: bool,
2019-01-12 22:07:30 +00:00
}
impl Emigui {
2019-01-19 16:10:28 +00:00
pub fn new(pixels_per_point: f32) -> Emigui {
2019-01-12 22:07:30 +00:00
Emigui {
last_input: Default::default(),
2019-01-19 16:10:28 +00:00
data: Arc::new(layout::Data::new(pixels_per_point)),
2019-01-12 22:07:30 +00:00
stats: Default::default(),
2020-04-17 12:25:27 +00:00
anti_alias: true,
2019-01-12 22:07:30 +00:00
}
}
2019-01-16 23:09:12 +00:00
pub fn texture(&self) -> &Texture {
2019-01-12 23:55:56 +00:00
self.data.fonts.texture()
}
2019-01-12 22:07:30 +00:00
pub fn new_frame(&mut self, new_input: RawInput) {
let gui_input = GuiInput::from_last_and_new(&self.last_input, &new_input);
self.last_input = new_input;
2019-01-12 23:55:56 +00:00
// TODO: avoid this clone
2019-01-12 22:07:30 +00:00
let mut new_data = (*self.data).clone();
new_data.new_frame(gui_input);
self.data = Arc::new(new_data);
}
pub fn whole_screen_region(&mut self) -> layout::Region {
let size = self.data.input.screen_size;
layout::Region {
data: self.data.clone(),
style: self.data.style(),
2019-01-12 22:07:30 +00:00
id: Default::default(),
dir: layout::Direction::Vertical,
align: layout::Align::Center,
2019-01-12 22:07:30 +00:00
cursor: Default::default(),
bounding_size: Default::default(),
available_space: size,
}
}
2019-03-10 20:00:28 +00:00
pub fn paint(&mut self) -> Mesh {
2019-03-11 14:59:49 +00:00
let paint_commands: Vec<PaintCmd> = self.data.graphics.lock().unwrap().drain().collect();
let mut mesher = Mesher::new(self.last_input.pixels_per_point);
2020-04-17 12:25:27 +00:00
mesher.anti_alias = self.anti_alias;
mesher.paint(&self.data.fonts, &paint_commands);
2019-03-10 20:00:28 +00:00
let mesh = mesher.mesh;
self.stats.num_vertices = mesh.vertices.len();
self.stats.num_triangles = mesh.indices.len() / 3;
mesh
2019-01-12 22:07:30 +00:00
}
2020-04-12 10:07:51 +00:00
pub fn ui(&mut self, region: &mut Region) {
region.foldable("Style", |region| {
2020-04-17 12:25:27 +00:00
region.add(Checkbox::new(&mut self.anti_alias, "Antialias"));
2020-04-12 10:07:51 +00:00
self.data.style_ui(region);
2019-01-12 22:07:30 +00:00
});
2020-04-12 10:07:51 +00:00
region.foldable("Fonts", |region| {
2019-12-02 19:08:49 +00:00
let old_font_definitions = self.data.fonts.definitions();
let mut new_font_definitions = old_font_definitions.clone();
2020-04-12 10:07:51 +00:00
font_definitions_ui(&mut new_font_definitions, region);
self.data.fonts.texture().ui(region);
2019-12-02 19:08:49 +00:00
if *old_font_definitions != new_font_definitions {
2019-01-17 17:03:39 +00:00
let mut new_data = (*self.data).clone();
2019-12-02 19:08:49 +00:00
let fonts =
Fonts::from_definitions(new_font_definitions, self.data.input.pixels_per_point);
2019-01-17 17:03:39 +00:00
new_data.fonts = Arc::new(fonts);
self.data = Arc::new(new_data);
}
});
2020-04-12 10:07:51 +00:00
region.foldable("Stats", |region| {
region.add(label!(
2019-01-21 07:48:32 +00:00
"Screen size: {} x {} points, pixels_per_point: {}",
2020-04-12 10:07:51 +00:00
region.input().screen_size.x,
region.input().screen_size.y,
region.input().pixels_per_point,
2019-01-21 07:48:32 +00:00
));
2020-04-12 10:07:51 +00:00
if let Some(mouse_pos) = region.input().mouse_pos {
region.add(label!("mouse_pos: {} x {}", mouse_pos.x, mouse_pos.y,));
} else {
2020-04-12 10:07:51 +00:00
region.add(label!("mouse_pos: None"));
}
2020-04-12 10:07:51 +00:00
region.add(label!(
"region cursor: {} x {}",
region.cursor().x,
region.cursor().y,
2019-01-21 07:48:32 +00:00
));
2020-04-12 10:07:51 +00:00
region.add(label!("num_vertices: {}", self.stats.num_vertices));
region.add(label!("num_triangles: {}", self.stats.num_triangles));
2019-01-12 22:07:30 +00:00
});
}
}
2020-04-12 10:07:51 +00:00
fn font_definitions_ui(font_definitions: &mut FontDefinitions, region: &mut Region) {
for (text_style, (_family, size)) in font_definitions.iter_mut() {
// TODO: radiobutton for family
region.add(
Slider::f32(size, 4.0, 40.0)
.precision(0)
.text(format!("{:?}", text_style)),
);
}
if region.add(Button::new("Reset fonts")).clicked {
*font_definitions = crate::fonts::default_font_definitions();
}
}