From 0e9f39c942b2555ba4a0dfd2440dae7515c79798 Mon Sep 17 00:00:00 2001 From: Robin Quint Date: Tue, 1 Mar 2022 14:24:31 +0100 Subject: [PATCH] More WIP --- imgui-examples/examples/hello_world.rs | 3 + imgui-examples/examples/support/mod.rs | 6 +- imgui-winit-support/src/lib.rs | 76 +++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/imgui-examples/examples/hello_world.rs b/imgui-examples/examples/hello_world.rs index 0b21816..ddb85f8 100644 --- a/imgui-examples/examples/hello_world.rs +++ b/imgui-examples/examples/hello_world.rs @@ -8,8 +8,11 @@ fn main() { let mut value = 0; let choices = ["test test this is 1", "test test this is 2"]; + let mut open = true; + system.main_loop(move |_, ui| { ui.window("Hello world") + .opened(&mut open) .size([300.0, 110.0], Condition::FirstUseEver) .build(|| { ui.text_wrapped("Hello world!"); diff --git a/imgui-examples/examples/support/mod.rs b/imgui-examples/examples/support/mod.rs index 0924593..aacd200 100644 --- a/imgui-examples/examples/support/mod.rs +++ b/imgui-examples/examples/support/mod.rs @@ -136,6 +136,8 @@ impl System { .prepare_frame(imgui.io_mut(), gl_window.window()) .expect("Failed to prepare frame"); gl_window.window().request_redraw(); + + platform.update_viewports(&mut imgui, window_target); } Event::RedrawRequested(_) => { let ui = imgui.frame(); @@ -157,12 +159,12 @@ impl System { target.finish().expect("Failed to swap buffers"); imgui.update_platform_windows(); - platform.update_viewports(&mut imgui, window_target); } Event::WindowEvent { event: WindowEvent::CloseRequested, + window_id, .. - } => *control_flow = ControlFlow::Exit, + } if window_id == display.gl_window().window().id() => *control_flow = ControlFlow::Exit, event => { let gl_window = display.gl_window(); platform.handle_event(imgui.io_mut(), gl_window.window(), &event); diff --git a/imgui-winit-support/src/lib.rs b/imgui-winit-support/src/lib.rs index 7f311ca..4239fac 100644 --- a/imgui-winit-support/src/lib.rs +++ b/imgui-winit-support/src/lib.rs @@ -1053,7 +1053,7 @@ impl WinitPlatform { if window_id == main_window.id() { imgui.main_viewport_mut() } else { - let imgui_id = self.windows.iter().find(|(id, wnd)| wnd.id() == window_id).map(|(id, wnd)| *id).unwrap(); + let imgui_id = self.windows.iter().find(|(_, wnd)| wnd.id() == window_id).map(|(id, _)| *id).unwrap(); imgui.viewport_by_id_mut(imgui_id).unwrap() } }; @@ -1078,12 +1078,86 @@ impl WinitPlatform { pos[1] += position.y as f32; imgui.io_mut().mouse_pos = pos; }, + WindowEvent::KeyboardInput { + input: + KeyboardInput { + virtual_keycode: Some(key), + state, + .. + }, + .. + } if window_id != main_window.id() => { + let io = imgui.io_mut(); + + let pressed = state == ElementState::Pressed; + io.keys_down[key as usize] = pressed; + + // This is a bit redundant here, but we'll leave it in. The OS occasionally + // fails to send modifiers keys, but it doesn't seem to send false-positives, + // so double checking isn't terrible in case some system *doesn't* send + // device events sometimes. + 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, + _ => (), + } + }, + WindowEvent::ReceivedCharacter(ch) if window_id != main_window.id() => { + let io = imgui.io_mut(); + + // Exclude the backspace key ('\u{7f}'). Otherwise we will insert this char and then + // delete it. + if ch != '\u{7f}' { + io.add_input_character(ch) + } + }, + WindowEvent::MouseWheel { + delta, + phase: TouchPhase::Moved, + .. + } if window_id != main_window.id() => match delta { + MouseScrollDelta::LineDelta(h, v) => { + let io = imgui.io_mut(); + io.mouse_wheel_h = h; + io.mouse_wheel = v; + } + MouseScrollDelta::PixelDelta(pos) => { + let io = imgui.io_mut(); + let pos = pos.to_logical::(self.hidpi_factor); + match pos.x.partial_cmp(&0.0) { + Some(Ordering::Greater) => io.mouse_wheel_h += 1.0, + Some(Ordering::Less) => io.mouse_wheel_h -= 1.0, + _ => (), + } + match pos.y.partial_cmp(&0.0) { + Some(Ordering::Greater) => io.mouse_wheel += 1.0, + Some(Ordering::Less) => io.mouse_wheel -= 1.0, + _ => (), + } + } + }, + WindowEvent::MouseInput { state, button, .. } if window_id != main_window.id() => { + let pressed = state == ElementState::Pressed; + match button { + MouseButton::Left => self.mouse_buttons[0].set(pressed), + MouseButton::Right => self.mouse_buttons[1].set(pressed), + MouseButton::Middle => self.mouse_buttons[2].set(pressed), + MouseButton::Other(idx @ 0..=4) => { + self.mouse_buttons[idx as usize].set(pressed) + } + _ => (), + } + }, _ => {}, } }, _ => {}, } } + + #[cfg(all( not(any( feature = "winit-26",