egui/example_glium/src/main.rs

97 lines
3.3 KiB
Rust
Raw Normal View History

2019-04-21 08:13:05 +00:00
#![deny(warnings)]
2019-03-12 21:59:55 +00:00
2019-11-18 19:06:41 +00:00
use std::time::{Duration, Instant};
2019-03-12 21:59:55 +00:00
use {
emigui::{
2020-04-12 10:07:51 +00:00
example_app::ExampleApp,
2019-03-12 21:59:55 +00:00
label,
math::vec2,
widgets::{Button, Label},
2019-11-18 19:06:41 +00:00
Align, Emigui,
2019-03-12 21:59:55 +00:00
},
2019-04-21 08:13:05 +00:00
emigui_glium::Painter,
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();
let pixels_per_point = display.gl_window().get_hidpi_factor() as f32;
let mut emigui = Emigui::new(pixels_per_point);
let mut painter = Painter::new(&display);
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
};
2019-11-18 19:06:41 +00:00
let mut quit = false;
let mut frame_start = Instant::now();
2019-03-12 21:59:55 +00:00
2020-04-12 10:07:51 +00:00
let mut example_app = ExampleApp::default();
2019-11-18 19:06:41 +00:00
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();
}
2019-03-12 21:59:55 +00:00
2019-11-18 19:06:41 +00:00
events_loop.poll_events(|event| {
match event {
glutin::Event::WindowEvent { event, .. } => match event {
glutin::WindowEvent::CloseRequested => quit = true,
2019-03-12 21:59:55 +00:00
2019-11-18 19:06:41 +00:00
glutin::WindowEvent::Resized(glutin::dpi::LogicalSize { width, height }) => {
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;
2019-03-12 21:59:55 +00:00
}
2019-11-18 19:06:41 +00:00
glutin::WindowEvent::CursorMoved { position, .. } => {
raw_input.mouse_pos = Some(vec2(position.x as f32, position.y as f32));
2019-03-12 21:59:55 +00:00
}
2019-11-18 19:06:41 +00:00
glutin::WindowEvent::KeyboardInput { input, .. } => {
if input.virtual_keycode == Some(glutin::VirtualKeyCode::Q)
&& input.modifiers.logo
{
quit = true;
}
2019-03-12 21:59:55 +00:00
}
2019-11-18 19:06:41 +00:00
_ => {
// dbg!(event);
2019-04-25 16:07:36 +00:00
}
2019-11-18 19:06:41 +00:00
},
_ => (),
}
});
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;
2019-03-12 21:59:55 +00:00
}
2020-04-12 10:07:51 +00:00
example_app.ui(&mut region);
emigui.ui(&mut region);
2019-11-18 19:06:41 +00:00
let mesh = emigui.paint();
painter.paint(&display, mesh, emigui.texture());
}
2019-03-12 21:59:55 +00:00
}