2019-04-21 08:13:05 +00:00
|
|
|
#![deny(warnings)]
|
2020-04-25 08:50:51 +00:00
|
|
|
#[allow(clippy::single_match)]
|
2020-05-01 00:08:01 +00:00
|
|
|
use std::{
|
|
|
|
collections::VecDeque,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2019-11-18 19:06:41 +00:00
|
|
|
|
2019-03-12 21:59:55 +00:00
|
|
|
use {
|
2020-05-01 00:08:01 +00:00
|
|
|
emigui::{containers::*, example_app::ExampleWindow, widgets::*, *},
|
2020-04-29 19:58:14 +00:00
|
|
|
glium::glutin,
|
2019-03-12 21:59:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut events_loop = glutin::EventsLoop::new();
|
|
|
|
let window = glutin::WindowBuilder::new().with_title("Emigui example");
|
|
|
|
let context = glutin::ContextBuilder::new();
|
|
|
|
let display = glium::Display::new(window, context, &events_loop).unwrap();
|
|
|
|
|
2020-04-22 18:01:49 +00:00
|
|
|
display
|
|
|
|
.gl_window()
|
|
|
|
.set_inner_size(glutin::dpi::LogicalSize {
|
2020-04-25 09:12:00 +00:00
|
|
|
width: 1024.0,
|
2020-04-22 18:01:49 +00:00
|
|
|
height: 800.0,
|
|
|
|
});
|
2020-04-25 09:12:00 +00:00
|
|
|
display.gl_window().set_position((0, 24).into()); // Useful when ddeveloping and constantly restarting it
|
2020-04-22 18:01:49 +00:00
|
|
|
|
2019-03-12 21:59:55 +00:00
|
|
|
let pixels_per_point = display.gl_window().get_hidpi_factor() as f32;
|
|
|
|
|
|
|
|
let mut emigui = Emigui::new(pixels_per_point);
|
2020-04-29 19:58:14 +00:00
|
|
|
let mut painter = emigui_glium::Painter::new(&display);
|
2019-03-12 21:59:55 +00:00
|
|
|
|
|
|
|
let mut raw_input = emigui::RawInput {
|
|
|
|
screen_size: {
|
|
|
|
let (width, height) = display.get_framebuffer_dimensions();
|
2019-03-16 11:57:44 +00:00
|
|
|
vec2(width as f32, height as f32) / pixels_per_point
|
2019-03-12 21:59:55 +00:00
|
|
|
},
|
|
|
|
pixels_per_point,
|
2019-04-21 08:13:05 +00:00
|
|
|
..Default::default()
|
2019-03-12 21:59:55 +00:00
|
|
|
};
|
|
|
|
|
2020-04-21 12:46:42 +00:00
|
|
|
// used to keep track of time for animations
|
|
|
|
let start_time = Instant::now();
|
2020-04-29 19:58:14 +00:00
|
|
|
let mut running = true;
|
2019-11-18 19:06:41 +00:00
|
|
|
let mut frame_start = Instant::now();
|
2020-05-01 00:08:01 +00:00
|
|
|
let mut frame_times = VecDeque::new();
|
|
|
|
let mut example_app = ExampleWindow::default();
|
2020-04-29 19:58:14 +00:00
|
|
|
let mut clipboard = emigui_glium::init_clipboard();
|
2020-04-29 19:25:49 +00:00
|
|
|
|
2020-05-02 09:37:12 +00:00
|
|
|
let memory_path = "emigui.json";
|
|
|
|
emigui_glium::read_memory(&emigui.ctx(), memory_path);
|
|
|
|
|
2020-04-28 21:05:22 +00:00
|
|
|
while running {
|
2019-11-18 19:06:41 +00:00
|
|
|
{
|
|
|
|
// Keep smooth frame rate. TODO: proper vsync
|
|
|
|
let frame_duration = frame_start.elapsed();
|
|
|
|
if frame_duration < Duration::from_millis(16) {
|
|
|
|
std::thread::sleep(Duration::from_millis(16) - frame_duration);
|
|
|
|
}
|
|
|
|
frame_start = Instant::now();
|
|
|
|
}
|
2019-03-12 21:59:55 +00:00
|
|
|
|
2020-04-28 21:05:22 +00:00
|
|
|
{
|
|
|
|
raw_input.time = start_time.elapsed().as_nanos() as f64 * 1e-9;
|
|
|
|
raw_input.scroll_delta = vec2(0.0, 0.0);
|
|
|
|
raw_input.dropped_files.clear();
|
|
|
|
raw_input.hovered_files.clear();
|
2020-04-29 19:25:49 +00:00
|
|
|
raw_input.events.clear();
|
|
|
|
events_loop.poll_events(|event| {
|
2020-04-29 19:58:14 +00:00
|
|
|
emigui_glium::input_event(event, clipboard.as_mut(), &mut raw_input, &mut running)
|
2020-04-29 19:25:49 +00:00
|
|
|
});
|
2020-04-28 21:05:22 +00:00
|
|
|
}
|
2019-11-18 19:06:41 +00:00
|
|
|
|
2020-04-29 19:25:49 +00:00
|
|
|
let emigui_start = Instant::now();
|
2020-04-28 21:05:22 +00:00
|
|
|
emigui.begin_frame(raw_input.clone()); // TODO: avoid clone
|
2020-04-19 09:13:24 +00:00
|
|
|
let mut region = emigui.background_region();
|
2020-04-21 14:50:56 +00:00
|
|
|
let mut region = region.centered_column(region.available_width().min(480.0));
|
2019-11-18 19:06:41 +00:00
|
|
|
region.set_align(Align::Min);
|
|
|
|
region.add(label!("Emigui running inside of Glium").text_style(emigui::TextStyle::Heading));
|
|
|
|
if region.add(Button::new("Quit")).clicked {
|
2020-04-28 21:05:22 +00:00
|
|
|
running = false;
|
2019-03-12 21:59:55 +00:00
|
|
|
}
|
2020-04-21 18:48:31 +00:00
|
|
|
|
2020-04-29 05:20:27 +00:00
|
|
|
region.add(
|
|
|
|
label!(
|
|
|
|
"Frame time: {:.1} ms (excludes painting)",
|
2020-05-01 00:08:01 +00:00
|
|
|
1e3 * mean_frame_time(&frame_times)
|
2020-04-29 05:20:27 +00:00
|
|
|
)
|
|
|
|
.text_style(TextStyle::Monospace),
|
|
|
|
);
|
|
|
|
|
2020-04-22 22:17:37 +00:00
|
|
|
// TODO: Make it even simpler to show a window
|
|
|
|
|
2020-04-22 18:01:49 +00:00
|
|
|
Window::new("Examples")
|
2020-04-25 09:12:00 +00:00
|
|
|
.default_pos(pos2(50.0, 100.0))
|
2020-04-25 12:37:39 +00:00
|
|
|
.default_size(vec2(300.0, 600.0))
|
2020-05-01 00:08:01 +00:00
|
|
|
// .mutate(|w| w.resize = w.resize.auto_expand_width(true))
|
|
|
|
// .resize(|r| r.auto_expand_width(true))
|
2020-04-21 18:48:31 +00:00
|
|
|
.show(region.ctx(), |region| {
|
|
|
|
example_app.ui(region);
|
|
|
|
});
|
|
|
|
|
|
|
|
Window::new("Emigui settings")
|
2020-04-25 09:12:00 +00:00
|
|
|
.default_pos(pos2(450.0, 100.0))
|
|
|
|
.default_size(vec2(450.0, 500.0))
|
2020-04-21 18:48:31 +00:00
|
|
|
.show(region.ctx(), |region| {
|
|
|
|
emigui.ui(region);
|
|
|
|
});
|
2020-04-17 12:26:36 +00:00
|
|
|
|
2020-04-23 17:15:17 +00:00
|
|
|
let (output, paint_batches) = emigui.end_frame();
|
2020-04-29 05:20:27 +00:00
|
|
|
|
|
|
|
frame_times.push_back((Instant::now() - emigui_start).as_secs_f64());
|
|
|
|
while frame_times.len() > 30 {
|
|
|
|
frame_times.pop_front();
|
|
|
|
}
|
|
|
|
|
2020-04-23 17:15:17 +00:00
|
|
|
painter.paint_batches(&display, paint_batches, emigui.texture());
|
2020-04-29 19:58:14 +00:00
|
|
|
emigui_glium::handle_output(output, &display, clipboard.as_mut());
|
2020-04-28 21:05:22 +00:00
|
|
|
}
|
2020-05-02 09:37:12 +00:00
|
|
|
|
|
|
|
if let Err(err) = emigui_glium::write_memory(&emigui.ctx(), memory_path) {
|
|
|
|
eprintln!("ERROR: Failed to save emigui state: {}", err);
|
|
|
|
}
|
2020-04-28 21:05:22 +00:00
|
|
|
}
|
2020-05-01 00:08:01 +00:00
|
|
|
|
|
|
|
pub fn mean_frame_time(frame_times: &VecDeque<f64>) -> f64 {
|
|
|
|
if frame_times.is_empty() {
|
|
|
|
0.0
|
|
|
|
} else {
|
|
|
|
frame_times.iter().sum::<f64>() / (frame_times.len() as f64)
|
|
|
|
}
|
|
|
|
}
|