From 4498abf28bdbc8dfc11ef1001c0748b5ec3a459c Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sun, 3 Oct 2021 11:24:12 +0200 Subject: [PATCH] visualization: Replace custom event conversion with imgui-winit-support (#29) --- Cargo.toml | 9 +- .../src/imgui_renderer.rs | 114 ------------------ examples/vulkan-visualization/src/main.rs | 24 +++- 3 files changed, 26 insertions(+), 121 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7f85d76b..0e54634b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,14 +26,15 @@ ash = { version = "0.33", optional = true } winapi = { version = "0.3.9", features = ["d3d12", "winerror", "impl-default", "impl-debug"], optional = true } # Only needed for visualizer. imgui = { version = "0.7", optional = true } +imgui-winit-support = { version = "0.7", optional = true } [dev-dependencies] ash-window = "0.7" -winit = "0.25" -winapi = { version = "0.3.9", features = ["d3d12", "d3d12sdklayers", "dxgi1_6", "winerror", "impl-default", "impl-debug", "winuser", "windowsx", "libloaderapi"] } -widestring = "0.4.3" hassle-rs = "0.5.2" raw-window-handle = "0.3" +widestring = "0.4.3" +winapi = { version = "0.3.9", features = ["d3d12", "d3d12sdklayers", "dxgi1_6", "winerror", "impl-default", "impl-debug", "winuser", "windowsx", "libloaderapi"] } +winit = "0.24" [[example]] name = "vulkan-buffer" @@ -57,7 +58,7 @@ required-features = ["d3d12", "public-winapi", "visualizer"] [features] -visualizer = ["imgui"] +visualizer = ["imgui", "imgui-winit-support"] vulkan = ["ash"] d3d12 = ["winapi"] public-winapi = ["winapi"] diff --git a/examples/vulkan-visualization/src/imgui_renderer.rs b/examples/vulkan-visualization/src/imgui_renderer.rs index de6f037b..175cd6d9 100644 --- a/examples/vulkan-visualization/src/imgui_renderer.rs +++ b/examples/vulkan-visualization/src/imgui_renderer.rs @@ -827,117 +827,3 @@ impl ImGuiRenderer { } } } - -pub(crate) fn handle_imgui_event( - io: &mut imgui::Io, - window: &winit::window::Window, - event: &winit::event::Event<()>, -) -> bool { - use winit::event::{ - DeviceEvent, ElementState, Event, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase, - VirtualKeyCode, WindowEvent, - }; - - match event { - Event::WindowEvent { event, window_id } if *window_id == window.id() => match *event { - WindowEvent::Resized(physical_size) => { - io.display_size = [physical_size.width as f32, physical_size.height as f32]; - false - } - WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(key), - state, - .. - }, - .. - } => { - let pressed = state == ElementState::Pressed; - io.keys_down[key as usize] = pressed; - match key { - VirtualKeyCode::LShift | VirtualKeyCode::RShift => io.key_shift = pressed, - VirtualKeyCode::LControl | VirtualKeyCode::RControl => io.key_ctrl = pressed, - VirtualKeyCode::LAlt | VirtualKeyCode::RAlt => io.key_alt = pressed, - VirtualKeyCode::LWin | VirtualKeyCode::RWin => io.key_super = pressed, - _ => (), - } - - io.want_capture_keyboard - } - WindowEvent::ReceivedCharacter(ch) => { - io.add_input_character(ch); - - io.want_capture_keyboard - } - - WindowEvent::CursorMoved { position, .. } => { - io.mouse_pos = [position.x as f32, position.y as f32]; - - io.want_capture_mouse - } - WindowEvent::MouseWheel { - delta, - phase: TouchPhase::Moved, - .. - } => { - match delta { - MouseScrollDelta::LineDelta(h, v) => { - io.mouse_wheel_h = h; - io.mouse_wheel = v; - } - MouseScrollDelta::PixelDelta(pos) => { - match pos.x.partial_cmp(&0.0) { - Some(std::cmp::Ordering::Greater) => io.mouse_wheel_h += 1.0, - Some(std::cmp::Ordering::Less) => io.mouse_wheel_h -= 1.0, - _ => (), - } - match pos.y.partial_cmp(&0.0) { - Some(std::cmp::Ordering::Greater) => io.mouse_wheel += 1.0, - Some(std::cmp::Ordering::Less) => io.mouse_wheel -= 1.0, - _ => (), - } - } - } - - io.want_capture_mouse - } - WindowEvent::MouseInput { state, button, .. } => { - let pressed = state == ElementState::Pressed; - match button { - MouseButton::Left => io.mouse_down[0] = pressed, - MouseButton::Right => io.mouse_down[1] = pressed, - MouseButton::Middle => io.mouse_down[2] = pressed, - MouseButton::Other(idx @ 0..=4) => io.mouse_down[idx as usize] = pressed, - _ => (), - } - - io.want_capture_mouse - } - _ => false, - }, - // Track key release events outside our window. If we don't do this, - // we might never see the release event if some other window gets focus. - Event::DeviceEvent { - event: - DeviceEvent::Key(KeyboardInput { - state: ElementState::Released, - virtual_keycode: Some(key), - .. - }), - .. - } => { - io.keys_down[*key as usize] = false; - match *key { - VirtualKeyCode::LShift | VirtualKeyCode::RShift => io.key_shift = false, - VirtualKeyCode::LControl | VirtualKeyCode::RControl => io.key_ctrl = false, - VirtualKeyCode::LAlt | VirtualKeyCode::RAlt => io.key_alt = false, - VirtualKeyCode::LWin | VirtualKeyCode::RWin => io.key_super = false, - _ => (), - } - - io.want_capture_keyboard - } - _ => false, - } -} diff --git a/examples/vulkan-visualization/src/main.rs b/examples/vulkan-visualization/src/main.rs index 7437831b..ea3bbdf2 100644 --- a/examples/vulkan-visualization/src/main.rs +++ b/examples/vulkan-visualization/src/main.rs @@ -9,7 +9,9 @@ mod helper; use helper::record_and_submit_command_buffer; mod imgui_renderer; -use imgui_renderer::{handle_imgui_event, ImGuiRenderer}; +use imgui_renderer::ImGuiRenderer; + +use imgui_winit_support::{HiDpiMode, WinitPlatform}; fn main() -> ash::prelude::VkResult<()> { let entry = unsafe { ash::Entry::new() }.unwrap(); @@ -241,7 +243,13 @@ fn main() -> ash::prelude::VkResult<()> { unsafe { device.create_semaphore(&semaphore_create_info, None) }.unwrap(); let mut imgui = imgui::Context::create(); - imgui.io_mut().display_size = [window_width as f32, window_height as f32]; + let mut platform = WinitPlatform::init(&mut imgui); + // imgui.io_mut().display_size = [window_width as f32, window_height as f32]; + platform.attach_window( + imgui.io_mut(), + &window, + HiDpiMode::Rounded, /* Default is blurry! */ + ); let descriptor_pool = { let pool_sizes = [ @@ -290,11 +298,12 @@ fn main() -> ash::prelude::VkResult<()> { .collect::>(); let mut visualizer = Some(gpu_allocator::vulkan::AllocatorVisualizer::new()); + let mut last_frame = std::time::Instant::now(); event_loop.run(move |event, _, control_flow| { *control_flow = winit::event_loop::ControlFlow::Wait; - handle_imgui_event(imgui.io_mut(), &window, &event); + platform.handle_event(imgui.io_mut(), &window, &event); let mut ready_for_rendering = false; match event { @@ -313,6 +322,11 @@ fn main() -> ash::prelude::VkResult<()> { _ => {} }, winit::event::Event::MainEventsCleared => ready_for_rendering = true, + winit::event::Event::NewEvents(_) => { + let now = std::time::Instant::now(); + imgui.io_mut().update_delta_time(now - last_frame); + last_frame = now; + } _ => {} } @@ -328,6 +342,9 @@ fn main() -> ash::prelude::VkResult<()> { .unwrap(); // Start ImGui frame + platform + .prepare_frame(imgui.io_mut(), &window) + .expect("Failed to prepare frame"); let ui = imgui.frame(); // Submit visualizer ImGui commands @@ -337,6 +354,7 @@ fn main() -> ash::prelude::VkResult<()> { .render(allocator.as_ref().unwrap(), &ui); // Finish ImGui Frame + platform.prepare_render(&ui, &window); let imgui_draw_data = ui.render(); record_and_submit_command_buffer(