Skip to content

Commit 4498abf

Browse files
authored
visualization: Replace custom event conversion with imgui-winit-support (#29)
1 parent 502489b commit 4498abf

File tree

3 files changed

+26
-121
lines changed

3 files changed

+26
-121
lines changed

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ ash = { version = "0.33", optional = true }
2626
winapi = { version = "0.3.9", features = ["d3d12", "winerror", "impl-default", "impl-debug"], optional = true }
2727
# Only needed for visualizer.
2828
imgui = { version = "0.7", optional = true }
29+
imgui-winit-support = { version = "0.7", optional = true }
2930

3031
[dev-dependencies]
3132
ash-window = "0.7"
32-
winit = "0.25"
33-
winapi = { version = "0.3.9", features = ["d3d12", "d3d12sdklayers", "dxgi1_6", "winerror", "impl-default", "impl-debug", "winuser", "windowsx", "libloaderapi"] }
34-
widestring = "0.4.3"
3533
hassle-rs = "0.5.2"
3634
raw-window-handle = "0.3"
35+
widestring = "0.4.3"
36+
winapi = { version = "0.3.9", features = ["d3d12", "d3d12sdklayers", "dxgi1_6", "winerror", "impl-default", "impl-debug", "winuser", "windowsx", "libloaderapi"] }
37+
winit = "0.24"
3738

3839
[[example]]
3940
name = "vulkan-buffer"
@@ -57,7 +58,7 @@ required-features = ["d3d12", "public-winapi", "visualizer"]
5758

5859

5960
[features]
60-
visualizer = ["imgui"]
61+
visualizer = ["imgui", "imgui-winit-support"]
6162
vulkan = ["ash"]
6263
d3d12 = ["winapi"]
6364
public-winapi = ["winapi"]

examples/vulkan-visualization/src/imgui_renderer.rs

Lines changed: 0 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -827,117 +827,3 @@ impl ImGuiRenderer {
827827
}
828828
}
829829
}
830-
831-
pub(crate) fn handle_imgui_event(
832-
io: &mut imgui::Io,
833-
window: &winit::window::Window,
834-
event: &winit::event::Event<()>,
835-
) -> bool {
836-
use winit::event::{
837-
DeviceEvent, ElementState, Event, KeyboardInput, MouseButton, MouseScrollDelta, TouchPhase,
838-
VirtualKeyCode, WindowEvent,
839-
};
840-
841-
match event {
842-
Event::WindowEvent { event, window_id } if *window_id == window.id() => match *event {
843-
WindowEvent::Resized(physical_size) => {
844-
io.display_size = [physical_size.width as f32, physical_size.height as f32];
845-
false
846-
}
847-
WindowEvent::KeyboardInput {
848-
input:
849-
KeyboardInput {
850-
virtual_keycode: Some(key),
851-
state,
852-
..
853-
},
854-
..
855-
} => {
856-
let pressed = state == ElementState::Pressed;
857-
io.keys_down[key as usize] = pressed;
858-
match key {
859-
VirtualKeyCode::LShift | VirtualKeyCode::RShift => io.key_shift = pressed,
860-
VirtualKeyCode::LControl | VirtualKeyCode::RControl => io.key_ctrl = pressed,
861-
VirtualKeyCode::LAlt | VirtualKeyCode::RAlt => io.key_alt = pressed,
862-
VirtualKeyCode::LWin | VirtualKeyCode::RWin => io.key_super = pressed,
863-
_ => (),
864-
}
865-
866-
io.want_capture_keyboard
867-
}
868-
WindowEvent::ReceivedCharacter(ch) => {
869-
io.add_input_character(ch);
870-
871-
io.want_capture_keyboard
872-
}
873-
874-
WindowEvent::CursorMoved { position, .. } => {
875-
io.mouse_pos = [position.x as f32, position.y as f32];
876-
877-
io.want_capture_mouse
878-
}
879-
WindowEvent::MouseWheel {
880-
delta,
881-
phase: TouchPhase::Moved,
882-
..
883-
} => {
884-
match delta {
885-
MouseScrollDelta::LineDelta(h, v) => {
886-
io.mouse_wheel_h = h;
887-
io.mouse_wheel = v;
888-
}
889-
MouseScrollDelta::PixelDelta(pos) => {
890-
match pos.x.partial_cmp(&0.0) {
891-
Some(std::cmp::Ordering::Greater) => io.mouse_wheel_h += 1.0,
892-
Some(std::cmp::Ordering::Less) => io.mouse_wheel_h -= 1.0,
893-
_ => (),
894-
}
895-
match pos.y.partial_cmp(&0.0) {
896-
Some(std::cmp::Ordering::Greater) => io.mouse_wheel += 1.0,
897-
Some(std::cmp::Ordering::Less) => io.mouse_wheel -= 1.0,
898-
_ => (),
899-
}
900-
}
901-
}
902-
903-
io.want_capture_mouse
904-
}
905-
WindowEvent::MouseInput { state, button, .. } => {
906-
let pressed = state == ElementState::Pressed;
907-
match button {
908-
MouseButton::Left => io.mouse_down[0] = pressed,
909-
MouseButton::Right => io.mouse_down[1] = pressed,
910-
MouseButton::Middle => io.mouse_down[2] = pressed,
911-
MouseButton::Other(idx @ 0..=4) => io.mouse_down[idx as usize] = pressed,
912-
_ => (),
913-
}
914-
915-
io.want_capture_mouse
916-
}
917-
_ => false,
918-
},
919-
// Track key release events outside our window. If we don't do this,
920-
// we might never see the release event if some other window gets focus.
921-
Event::DeviceEvent {
922-
event:
923-
DeviceEvent::Key(KeyboardInput {
924-
state: ElementState::Released,
925-
virtual_keycode: Some(key),
926-
..
927-
}),
928-
..
929-
} => {
930-
io.keys_down[*key as usize] = false;
931-
match *key {
932-
VirtualKeyCode::LShift | VirtualKeyCode::RShift => io.key_shift = false,
933-
VirtualKeyCode::LControl | VirtualKeyCode::RControl => io.key_ctrl = false,
934-
VirtualKeyCode::LAlt | VirtualKeyCode::RAlt => io.key_alt = false,
935-
VirtualKeyCode::LWin | VirtualKeyCode::RWin => io.key_super = false,
936-
_ => (),
937-
}
938-
939-
io.want_capture_keyboard
940-
}
941-
_ => false,
942-
}
943-
}

examples/vulkan-visualization/src/main.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ mod helper;
99
use helper::record_and_submit_command_buffer;
1010

1111
mod imgui_renderer;
12-
use imgui_renderer::{handle_imgui_event, ImGuiRenderer};
12+
use imgui_renderer::ImGuiRenderer;
13+
14+
use imgui_winit_support::{HiDpiMode, WinitPlatform};
1315

1416
fn main() -> ash::prelude::VkResult<()> {
1517
let entry = unsafe { ash::Entry::new() }.unwrap();
@@ -241,7 +243,13 @@ fn main() -> ash::prelude::VkResult<()> {
241243
unsafe { device.create_semaphore(&semaphore_create_info, None) }.unwrap();
242244

243245
let mut imgui = imgui::Context::create();
244-
imgui.io_mut().display_size = [window_width as f32, window_height as f32];
246+
let mut platform = WinitPlatform::init(&mut imgui);
247+
// imgui.io_mut().display_size = [window_width as f32, window_height as f32];
248+
platform.attach_window(
249+
imgui.io_mut(),
250+
&window,
251+
HiDpiMode::Rounded, /* Default is blurry! */
252+
);
245253

246254
let descriptor_pool = {
247255
let pool_sizes = [
@@ -290,11 +298,12 @@ fn main() -> ash::prelude::VkResult<()> {
290298
.collect::<Vec<_>>();
291299

292300
let mut visualizer = Some(gpu_allocator::vulkan::AllocatorVisualizer::new());
301+
let mut last_frame = std::time::Instant::now();
293302

294303
event_loop.run(move |event, _, control_flow| {
295304
*control_flow = winit::event_loop::ControlFlow::Wait;
296305

297-
handle_imgui_event(imgui.io_mut(), &window, &event);
306+
platform.handle_event(imgui.io_mut(), &window, &event);
298307

299308
let mut ready_for_rendering = false;
300309
match event {
@@ -313,6 +322,11 @@ fn main() -> ash::prelude::VkResult<()> {
313322
_ => {}
314323
},
315324
winit::event::Event::MainEventsCleared => ready_for_rendering = true,
325+
winit::event::Event::NewEvents(_) => {
326+
let now = std::time::Instant::now();
327+
imgui.io_mut().update_delta_time(now - last_frame);
328+
last_frame = now;
329+
}
316330
_ => {}
317331
}
318332

@@ -328,6 +342,9 @@ fn main() -> ash::prelude::VkResult<()> {
328342
.unwrap();
329343

330344
// Start ImGui frame
345+
platform
346+
.prepare_frame(imgui.io_mut(), &window)
347+
.expect("Failed to prepare frame");
331348
let ui = imgui.frame();
332349

333350
// Submit visualizer ImGui commands
@@ -337,6 +354,7 @@ fn main() -> ash::prelude::VkResult<()> {
337354
.render(allocator.as_ref().unwrap(), &ui);
338355

339356
// Finish ImGui Frame
357+
platform.prepare_render(&ui, &window);
340358
let imgui_draw_data = ui.render();
341359

342360
record_and_submit_command_buffer(

0 commit comments

Comments
 (0)