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,
|
2020-04-17 13:33:52 +00:00
|
|
|
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(),
|
2020-04-17 13:33:52 +00:00
|
|
|
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 {
|
2020-04-17 13:33:52 +00:00
|
|
|
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
|
2020-04-17 13:33:52 +00:00
|
|
|
let mut new_data = (*self.ctx).clone();
|
2019-01-12 22:07:30 +00:00
|
|
|
new_data.new_frame(gui_input);
|
2020-04-17 13:33:52 +00:00
|
|
|
self.ctx = Arc::new(new_data);
|
2019-01-12 22:07:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 09:13:24 +00:00
|
|
|
/// A region for the entire screen, behind any windows.
|
|
|
|
pub fn background_region(&mut self) -> Region {
|
2020-04-17 12:26:36 +00:00
|
|
|
Region {
|
2020-04-17 13:33:52 +00:00
|
|
|
ctx: self.ctx.clone(),
|
2020-04-17 12:26:36 +00:00
|
|
|
layer: Layer::Background,
|
2020-04-17 13:33:52 +00:00
|
|
|
style: self.ctx.style(),
|
2020-04-19 09:13:24 +00:00
|
|
|
id: Id::background(),
|
2019-01-12 22:07:30 +00:00
|
|
|
dir: layout::Direction::Vertical,
|
2019-01-14 13:54:06 +00:00
|
|
|
align: layout::Align::Center,
|
2020-04-19 22:48:54 +00:00
|
|
|
rect: Rect::from_min_size(Default::default(), self.ctx.input.screen_size),
|
2019-01-12 22:07:30 +00:00
|
|
|
cursor: Default::default(),
|
|
|
|
bounding_size: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2019-02-24 16:18:30 +00:00
|
|
|
let mut mesher = Mesher::new(self.last_input.pixels_per_point);
|
2020-04-18 22:27:25 +00:00
|
|
|
mesher.options.anti_alias = self.anti_alias;
|
2020-04-17 12:25:27 +00:00
|
|
|
|
2020-04-17 13:33:52 +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"));
|
2020-04-17 13:33:52 +00:00
|
|
|
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| {
|
2020-04-17 13:33:52 +00:00
|
|
|
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);
|
2020-04-17 13:33:52 +00:00
|
|
|
self.ctx.fonts.texture().ui(region);
|
2019-12-02 19:08:49 +00:00
|
|
|
if *old_font_definitions != new_font_definitions {
|
2020-04-17 13:33:52 +00:00
|
|
|
let mut new_data = (*self.ctx).clone();
|
2019-12-02 19:08:49 +00:00
|
|
|
let fonts =
|
2020-04-17 13:33:52 +00:00
|
|
|
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);
|
2020-04-17 13:33:52 +00:00
|
|
|
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,));
|
2019-02-10 14:30:48 +00:00
|
|
|
} else {
|
2020-04-20 21:42:11 +00:00
|
|
|
region.add_label("mouse_pos: None");
|
2019-02-10 14:30:48 +00:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
}
|