From cf214ca397814fb9e4ab1c449be6e8b1a4306524 Mon Sep 17 00:00:00 2001 From: Robin Quint Date: Tue, 1 Mar 2022 11:26:36 +0100 Subject: [PATCH] Reverted integration changes --- imgui-examples/examples/hello_world.rs | 3 +- imgui-examples/examples/support/mod.rs | 16 +-- imgui-winit-support/src/lib.rs | 175 +------------------------ 3 files changed, 5 insertions(+), 189 deletions(-) diff --git a/imgui-examples/examples/hello_world.rs b/imgui-examples/examples/hello_world.rs index 4a70e86..8728558 100644 --- a/imgui-examples/examples/hello_world.rs +++ b/imgui-examples/examples/hello_world.rs @@ -3,8 +3,7 @@ use imgui::*; mod support; fn main() { - let mut system = support::init(file!()); - system.enable_viewports(); + let system = support::init(file!()); let mut value = 0; let choices = ["test test this is 1", "test test this is 2"]; diff --git a/imgui-examples/examples/support/mod.rs b/imgui-examples/examples/support/mod.rs index bbbe347..44e8825 100644 --- a/imgui-examples/examples/support/mod.rs +++ b/imgui-examples/examples/support/mod.rs @@ -3,7 +3,7 @@ use glium::glutin::event::{Event, WindowEvent}; use glium::glutin::event_loop::{ControlFlow, EventLoop}; use glium::glutin::window::WindowBuilder; use glium::{Display, Surface}; -use imgui::{Context, FontConfig, FontGlyphRanges, FontSource, Ui, ConfigFlags}; +use imgui::{Context, FontConfig, FontGlyphRanges, FontSource, Ui}; use imgui_glium_renderer::Renderer; use imgui_winit_support::{HiDpiMode, WinitPlatform}; use std::path::Path; @@ -112,11 +112,6 @@ pub fn init(title: &str) -> System { } impl System { - pub fn enable_viewports(&mut self) { - WinitPlatform::init_viewports(&mut self.imgui, &self.event_loop, self.display.gl_window().window()); - self.imgui.io_mut().config_flags.insert(ConfigFlags::VIEWPORTS_ENABLE); - } - pub fn main_loop(self, mut run_ui: F) { let System { event_loop, @@ -159,18 +154,11 @@ impl System { .render(&mut target, draw_data) .expect("Rendering failed"); target.finish().expect("Failed to swap buffers"); - - imgui.update_platform_windows(); - imgui.render_platform_windows_default(); } Event::WindowEvent { event: WindowEvent::CloseRequested, - window_id, .. - } if window_id == display.gl_window().window().id() => *control_flow = ControlFlow::Exit, - Event::WindowEvent { event, window_id, .. } if window_id != display.gl_window().window().id() => { - platform.handle_viewport_event(&mut imgui, window_id, &event); - }, + } => *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 3a009c5..3c2c65d 100644 --- a/imgui-winit-support/src/lib.rs +++ b/imgui-winit-support/src/lib.rs @@ -188,10 +188,8 @@ use winit_20 as winit; ))] use winit_19 as winit; -use imgui::{self, BackendFlags, ConfigFlags, Context, Io, Key, Ui, PlatformViewportBackend, ViewportFlags, PlatformMonitor}; -use std::collections::hash_map::DefaultHasher; -use std::hash::{Hash, Hasher}; -use std::{cell::Cell, ptr::null_mut}; +use imgui::{self, BackendFlags, ConfigFlags, Context, Io, Key, Ui}; +use std::cell::Cell; use std::cmp::Ordering; use winit::dpi::{LogicalPosition, LogicalSize}; @@ -446,115 +444,6 @@ impl HiDpiMode { } } -struct ViewportBackend { - event_loop: *const winit::event_loop::EventLoopWindowTarget<()>, -} - -enum PlatformHandle { - MainWindow(*const winit::window::Window), - SecondaryWindow(winit::window::Window), -} - -impl PlatformHandle { - fn get(&self) -> &winit::window::Window { - match self { - PlatformHandle::MainWindow(ptr) => unsafe { &**ptr }, - PlatformHandle::SecondaryWindow(wnd) => wnd, - } - } -} - -impl PlatformViewportBackend for ViewportBackend { - fn create_window(&mut self, viewport: &mut imgui::Viewport) { - let window = winit::window::WindowBuilder::new() - .with_always_on_top(viewport.flags.contains(ViewportFlags::TOP_MOST)) - // .with_decorations(!viewport.flags.contains(ViewportFlags::NO_DECORATION)) - .with_resizable(true) - .with_visible(false) - .build(unsafe{&*self.event_loop}) - .unwrap(); - - let mut hasher = DefaultHasher::new(); - viewport.platform_handle = window.id().hash(&hasher); - viewport.platform_handle = hasher.finish() as *mut c_void; - - viewport.platform_user_data = Box::into_raw(Box::new(PlatformHandle::SecondaryWindow(window))) as *mut _; - } - - fn destroy_window(&mut self, viewport: &mut imgui::Viewport) { - unsafe { - // drop window - Box::from_raw(viewport.platform_user_data as *mut PlatformHandle); - viewport.platform_user_data = null_mut(); // satisfy ImGui check - } - } - - fn show_window(&mut self, viewport: &mut imgui::Viewport) { - let window = unsafe { (*(viewport.platform_user_data as *const PlatformHandle)).get() }; - window.set_visible(true); - } - - fn set_window_pos(&mut self, viewport: &mut imgui::Viewport, pos: [f32; 2]) { - let window = unsafe { (*(viewport.platform_user_data as *const PlatformHandle)).get() }; - window.set_outer_position(winit::dpi::LogicalPosition::new(pos[0], pos[1])); - } - - fn get_window_pos(&mut self, viewport: &mut imgui::Viewport) -> [f32; 2] { - let window = unsafe { (*(viewport.platform_user_data as *const PlatformHandle)).get() }; - let pos = window.outer_position().unwrap(); - [pos.x as f32, pos.y as f32] - } - - fn set_window_size(&mut self, viewport: &mut imgui::Viewport, size: [f32; 2]) { - let window = unsafe { (*(viewport.platform_user_data as *const PlatformHandle)).get() }; - window.set_inner_size(winit::dpi::LogicalSize::new(size[0], size[1])); - } - - fn get_window_size(&mut self, viewport: &mut imgui::Viewport) -> [f32; 2] { - let window = unsafe { (*(viewport.platform_user_data as *const PlatformHandle)).get() }; - let size = window.inner_size(); - [size.width as f32, size.width as f32] - } - - fn set_window_focus(&mut self, viewport: &mut imgui::Viewport) { - let window = unsafe { (*(viewport.platform_user_data as *const PlatformHandle)).get() }; - window.focus_window(); - } - - fn get_window_focus(&mut self, _viewport: &mut imgui::Viewport) -> bool { - true - } - - fn get_window_minimized(&mut self, _viewport: &mut imgui::Viewport) -> bool { - false - } - - fn set_window_title(&mut self, viewport: &mut imgui::Viewport, title: &str) { - let window = unsafe { (*(viewport.platform_user_data as *const PlatformHandle)).get() }; - window.set_title(title); - } - - fn set_window_alpha(&mut self, _viewport: &mut imgui::Viewport, _alpha: f32) { - - } - - fn update_window(&mut self, _viewport: &mut imgui::Viewport) { - - } - - fn render_window(&mut self, _viewport: &mut imgui::Viewport) { - - } - - fn swap_buffers(&mut self, _viewport: &mut imgui::Viewport) { - - } - - fn create_vk_surface(&mut self, _viewport: &mut imgui::Viewport, _instance: u64, _out_surface: &mut u64) -> i32 { - 0 - } -} - impl WinitPlatform { /// Initializes a winit platform instance and configures imgui. /// @@ -602,35 +491,6 @@ impl WinitPlatform { mouse_buttons: [Button::INIT; 5], } } - - pub fn init_viewports(imgui: &mut Context, event_loop: &winit::event_loop::EventLoopWindowTarget<()>, main_window: &winit::window::Window) { - let io = imgui.io_mut(); - io.backend_flags.insert(BackendFlags::PLATFORM_HAS_VIEWPORTS); - - imgui.set_platform_backend(ViewportBackend{ - event_loop: event_loop as *const _, - }); - - let pio = imgui.platform_io_mut(); - let mut monitors = Vec::new(); - for monitor in main_window.available_monitors() { - monitors.push(PlatformMonitor { - main_pos: [monitor.position().x as f32, monitor.position().y as f32], - main_size: [monitor.size().width as f32, monitor.size().height as f32], - work_pos: [monitor.position().x as f32, monitor.position().y as f32], - work_size: [monitor.size().width as f32, monitor.size().height as f32], - dpi_scale: 1.0, - }); - } - pio.monitors.replace_from_slice(&monitors); - - let main_viewport = imgui.main_viewport_mut(); - let mut hasher = DefaultHasher::new(); - main_window.id().hash(&mut hasher); - main_viewport.platform_handle = hasher.finish() as *mut c_void; - main_viewport.platform_user_data = Box::into_raw(Box::new(PlatformHandle::MainWindow(main_window as *const _))) as *mut _; - } - /// Attaches the platform instance to a winit window. /// /// This function configures imgui-rs in the following ways: @@ -987,37 +847,6 @@ impl WinitPlatform { _ => (), } } - pub fn handle_viewport_event(&mut self, imgui: &mut imgui::Context, window_id: winit::window::WindowId, window_event: &winit::event::WindowEvent) { - let hasher = DefaultHasher::new(); - window_id.hash(&mut hasher); - let viewport_id = hasher.finish(); - - match *window_event { - WindowEvent::Resized(new_size) => { - - }, - WindowEvent::Moved(_) => todo!(), - WindowEvent::CloseRequested => todo!(), - WindowEvent::Destroyed => todo!(), - WindowEvent::DroppedFile(_) => todo!(), - WindowEvent::HoveredFile(_) => todo!(), - WindowEvent::HoveredFileCancelled => todo!(), - WindowEvent::ReceivedCharacter(_) => todo!(), - WindowEvent::Focused(_) => todo!(), - WindowEvent::KeyboardInput { device_id, input, is_synthetic } => todo!(), - WindowEvent::ModifiersChanged(_) => todo!(), - WindowEvent::CursorMoved { device_id, position, modifiers } => todo!(), - WindowEvent::CursorEntered { device_id } => todo!(), - WindowEvent::CursorLeft { device_id } => todo!(), - WindowEvent::MouseWheel { device_id, delta, phase, modifiers } => todo!(), - WindowEvent::MouseInput { device_id, state, button, modifiers } => todo!(), - WindowEvent::TouchpadPressure { device_id, pressure, stage } => todo!(), - WindowEvent::AxisMotion { device_id, axis, value } => todo!(), - WindowEvent::Touch(_) => todo!(), - WindowEvent::ScaleFactorChanged { scale_factor, new_inner_size } => todo!(), - WindowEvent::ThemeChanged(_) => todo!(), - } - } #[cfg(all( not(any( feature = "winit-26",