2019-04-21 08:13:05 +00:00
|
|
|
#![deny(warnings)]
|
2020-04-25 08:50:51 +00:00
|
|
|
#[allow(clippy::single_match)]
|
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,
|
2020-04-18 23:05:49 +00:00
|
|
|
math::*,
|
2019-03-12 21:59:55 +00:00
|
|
|
widgets::{Button, Label},
|
2020-04-20 08:01:13 +00:00
|
|
|
Align, CursorIcon, Emigui, Window,
|
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();
|
|
|
|
|
2020-04-22 18:01:49 +00:00
|
|
|
display
|
|
|
|
.gl_window()
|
|
|
|
.set_inner_size(glutin::dpi::LogicalSize {
|
|
|
|
width: 1200.0,
|
|
|
|
height: 800.0,
|
|
|
|
});
|
|
|
|
display.gl_window().set_position((16, 32).into()); // Useful when debugging and constantly restarting it
|
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
|
2020-04-21 12:46:42 +00:00
|
|
|
// used to keep track of time for animations
|
|
|
|
let start_time = Instant::now();
|
|
|
|
|
2019-11-18 19:06:41 +00:00
|
|
|
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
|
|
|
|
2020-04-21 12:46:42 +00:00
|
|
|
raw_input.time = start_time.elapsed().as_nanos() as f64 * 1e-9;
|
2020-04-22 18:01:49 +00:00
|
|
|
raw_input.scroll_delta = vec2(0.0, 0.0);
|
|
|
|
|
|
|
|
events_loop.poll_events(|event| match event {
|
|
|
|
glutin::Event::WindowEvent { event, .. } => match event {
|
|
|
|
glutin::WindowEvent::CloseRequested => quit = true,
|
|
|
|
|
|
|
|
glutin::WindowEvent::Resized(glutin::dpi::LogicalSize { width, height }) => {
|
|
|
|
raw_input.screen_size = vec2(width as f32, height as f32);
|
|
|
|
}
|
|
|
|
glutin::WindowEvent::MouseInput { state, .. } => {
|
|
|
|
raw_input.mouse_down = state == glutin::ElementState::Pressed;
|
|
|
|
}
|
|
|
|
glutin::WindowEvent::CursorMoved { position, .. } => {
|
|
|
|
raw_input.mouse_pos = Some(pos2(position.x as f32, position.y as f32));
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
2020-04-22 18:01:49 +00:00
|
|
|
}
|
|
|
|
glutin::WindowEvent::MouseWheel { delta, .. } => {
|
|
|
|
match delta {
|
|
|
|
glutin::MouseScrollDelta::LineDelta(x, y) => {
|
|
|
|
raw_input.scroll_delta = vec2(x, y) * 24.0;
|
|
|
|
}
|
|
|
|
glutin::MouseScrollDelta::PixelDelta(delta) => {
|
|
|
|
// Actually point delta
|
|
|
|
raw_input.scroll_delta = vec2(delta.x as f32, delta.y as f32);
|
2019-11-18 19:06:41 +00:00
|
|
|
}
|
2019-03-12 21:59:55 +00:00
|
|
|
}
|
2020-04-22 18:01:49 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// dbg!(event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
2019-11-18 19:06:41 +00:00
|
|
|
});
|
|
|
|
|
2020-04-23 17:15:17 +00:00
|
|
|
emigui.begin_frame(raw_input);
|
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 {
|
|
|
|
quit = true;
|
2019-03-12 21:59:55 +00:00
|
|
|
}
|
2020-04-21 18:48:31 +00:00
|
|
|
|
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")
|
|
|
|
.default_pos(pos2(100.0, 100.0))
|
|
|
|
.default_size(vec2(300.0, 400.0))
|
2020-04-21 18:48:31 +00:00
|
|
|
.show(region.ctx(), |region| {
|
|
|
|
example_app.ui(region);
|
|
|
|
});
|
|
|
|
|
|
|
|
Window::new("Emigui settings")
|
2020-04-22 22:17:37 +00:00
|
|
|
.default_pos(pos2(500.0, 100.0))
|
|
|
|
.default_size(vec2(500.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();
|
|
|
|
painter.paint_batches(&display, paint_batches, emigui.texture());
|
2020-04-20 08:01:13 +00:00
|
|
|
|
2020-04-23 17:15:17 +00:00
|
|
|
let cursor = match output.cursor_icon {
|
2020-04-20 08:01:13 +00:00
|
|
|
CursorIcon::Default => glutin::MouseCursor::Default,
|
2020-04-23 17:15:17 +00:00
|
|
|
CursorIcon::PointingHand => glutin::MouseCursor::Hand,
|
|
|
|
CursorIcon::ResizeNwSe => glutin::MouseCursor::NwseResize,
|
2020-04-20 08:01:13 +00:00
|
|
|
};
|
2020-04-23 17:15:17 +00:00
|
|
|
|
|
|
|
if let Some(url) = output.open_url {
|
|
|
|
if let Err(err) = webbrowser::open(&url) {
|
|
|
|
eprintln!("Failed to open url: {}", err); // TODO show error in imgui
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 08:01:13 +00:00
|
|
|
display.gl_window().set_cursor(cursor);
|
2019-11-18 19:06:41 +00:00
|
|
|
}
|
2019-03-12 21:59:55 +00:00
|
|
|
}
|