Update example_glium
#![deny(warnings)]
+use std::time::{Duration, Instant};
+
use {
emigui::{
label,
math::vec2,
widgets::{Button, Label},
- Emigui,
+ Align, Emigui,
},
emigui_glium::Painter,
glium::glutin,
@@ -31,48 +33,40 @@ fn main() {
..Default::default()
};
- let mut paint = |raw_input| {
- emigui.new_frame(raw_input);
- let mut region = emigui.whole_screen_region();
- let mut region = region.left_column(region.width().min(480.0));
- region.add(label!("Emigui!").text_style(emigui::TextStyle::Heading));
- let exit = region.add(Button::new("Quit")).clicked;
- emigui.example(&mut region);
- let mesh = emigui.paint();
- painter.paint(&display, mesh, emigui.texture());
- exit
- };
+ let mut quit = false;
+
+ let mut frame_start = Instant::now();
- paint(raw_input);
+ while !quit {
+ {
+ // 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();
+ }
- events_loop.run_forever(|event| {
+ events_loop.poll_events(|event| {
match event {
glutin::Event::WindowEvent { event, .. } => match event {
- glutin::WindowEvent::CloseRequested => return glutin::ControlFlow::Break,
+ glutin::WindowEvent::CloseRequested => quit = true,
glutin::WindowEvent::Resized(glutin::dpi::LogicalSize { width, height }) => {
- raw_input.screen_size = vec2(width as f32, height as f32) / pixels_per_point;
- if paint(raw_input) {
- return glutin::ControlFlow::Break;
- }
+ raw_input.screen_size =
+ vec2(width as f32, height as f32) / pixels_per_point;
}
glutin::WindowEvent::MouseInput { state, .. } => {
raw_input.mouse_down = state == glutin::ElementState::Pressed;
- if paint(raw_input) {
- return glutin::ControlFlow::Break;
- }
}
glutin::WindowEvent::CursorMoved { position, .. } => {
raw_input.mouse_pos = Some(vec2(position.x as f32, position.y as f32));
- if paint(raw_input) {
- return glutin::ControlFlow::Break;
- }
}
glutin::WindowEvent::KeyboardInput { input, .. } => {
if input.virtual_keycode == Some(glutin::VirtualKeyCode::Q)
&& input.modifiers.logo
{
- return glutin::ControlFlow::Break;
+ quit = true;
}
}
_ => {
@@ -81,6 +75,18 @@ fn main() {
},
_ => (),
}
- glutin::ControlFlow::Continue
});
+
+ emigui.new_frame(raw_input);
+ let mut region = emigui.whole_screen_region();
+ let mut region = region.left_column(region.width().min(480.0));
+ 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 {
+ quit = true;
+ }
+ emigui.example(&mut region);
+ let mesh = emigui.paint();
+ painter.paint(&display, mesh, emigui.texture());
+ }
}
