egui/emigui/src/emigui.rs

126 lines
4.2 KiB
Rust
Raw Normal View History

2019-01-12 22:07:30 +00:00
use std::sync::Arc;
2020-04-17 13:29:48 +00:00
use crate::{layout, mesher::Mesher, widgets::*, *};
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 ctx: Arc<Context>,
2019-01-12 22:07:30 +00:00
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(),
ctx: Arc::new(Context::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 {
self.ctx.fonts.texture()
2019-01-12 23:55:56 +00:00
}
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
let mut new_data = (*self.ctx).clone();
2019-01-12 22:07:30 +00:00
new_data.new_frame(gui_input);
self.ctx = Arc::new(new_data);
2019-01-12 22:07:30 +00:00
}
/// A region for the entire screen, behind any windows.
pub fn background_region(&mut self) -> Region {
Region {
ctx: self.ctx.clone(),
layer: Layer::Background,
style: self.ctx.style(),
id: Id::background(),
2019-01-12 22:07:30 +00:00
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: self.ctx.input.screen_size,
2019-01-12 22:07:30 +00:00
}
}
2019-03-10 20:00:28 +00:00
pub fn paint(&mut self) -> Mesh {
2020-04-17 21:30:01 +00:00
let paint_commands = self.ctx.drain_paint_lists();
let mut mesher = Mesher::new(self.last_input.pixels_per_point);
mesher.options.anti_alias = self.anti_alias;
2020-04-17 12:25:27 +00:00
mesher.paint(&self.ctx.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"));
self.ctx.style_ui(region);
2019-01-12 22:07:30 +00:00
});
2020-04-12 10:07:51 +00:00
region.foldable("Fonts", |region| {
let old_font_definitions = self.ctx.fonts.definitions();
2019-12-02 19:08:49 +00:00
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.ctx.fonts.texture().ui(region);
2019-12-02 19:08:49 +00:00
if *old_font_definitions != new_font_definitions {
let mut new_data = (*self.ctx).clone();
2019-12-02 19:08:49 +00:00
let fonts =
Fonts::from_definitions(new_font_definitions, self.ctx.input.pixels_per_point);
2019-01-17 17:03:39 +00:00
new_data.fonts = Arc::new(fonts);
self.ctx = Arc::new(new_data);
2019-01-17 17:03:39 +00:00
}
});
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();
}
}