Skip to content

Commit

Permalink
visualization: Replace custom event conversion with imgui-winit-suppo…
Browse files Browse the repository at this point in the history
…rt (#29)
  • Loading branch information
MarijnS95 authored Oct 3, 2021
1 parent 502489b commit 4498abf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 121 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"]
Expand Down
114 changes: 0 additions & 114 deletions examples/vulkan-visualization/src/imgui_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
24 changes: 21 additions & 3 deletions examples/vulkan-visualization/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -290,11 +298,12 @@ fn main() -> ash::prelude::VkResult<()> {
.collect::<Vec<_>>();

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 {
Expand All @@ -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;
}
_ => {}
}

Expand All @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 4498abf

Please sign in to comment.