|
|
@ -1,4 +1,4 @@
|
|
|
|
//! This crate provides a winit-based backend platform for imgui-rs.
|
|
|
|
//! This crate provides a winit-based backend platform for imgui_supp-rs.
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! A backend platform handles window/input device events and manages their
|
|
|
|
//! A backend platform handles window/input device events and manages their
|
|
|
|
//! state.
|
|
|
|
//! state.
|
|
|
@ -17,7 +17,7 @@
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! ```rust,no_run,ignore
|
|
|
|
//! ```rust,no_run,ignore
|
|
|
|
//! # // TODO: Remove ignore when only one winit version is used
|
|
|
|
//! # // TODO: Remove ignore when only one winit version is used
|
|
|
|
//! use imgui::Context;
|
|
|
|
//! use imgui_supp::Context;
|
|
|
|
//! use imgui_winit_support::{HiDpiMode, WinitPlatform};
|
|
|
|
//! use imgui_winit_support::{HiDpiMode, WinitPlatform};
|
|
|
|
//! use std::time::Instant;
|
|
|
|
//! use std::time::Instant;
|
|
|
|
//! use winit::event::{Event, WindowEvent};
|
|
|
|
//! use winit::event::{Event, WindowEvent};
|
|
|
@ -27,11 +27,11 @@
|
|
|
|
//! let mut event_loop = EventLoop::new();
|
|
|
|
//! let mut event_loop = EventLoop::new();
|
|
|
|
//! let mut window = Window::new(&event_loop).unwrap();
|
|
|
|
//! let mut window = Window::new(&event_loop).unwrap();
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! let mut imgui = Context::create();
|
|
|
|
//! let mut imgui_supp = Context::create();
|
|
|
|
//! // configure imgui-rs Context if necessary
|
|
|
|
//! // configure imgui_supp-rs Context if necessary
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! let mut platform = WinitPlatform::init(&mut imgui); // step 1
|
|
|
|
//! let mut platform = WinitPlatform::init(&mut imgui_supp); // step 1
|
|
|
|
//! platform.attach_window(imgui.io_mut(), &window, HiDpiMode::Default); // step 2
|
|
|
|
//! platform.attach_window(imgui_supp.io_mut(), &window, HiDpiMode::Default); // step 2
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! let mut last_frame = Instant::now();
|
|
|
|
//! let mut last_frame = Instant::now();
|
|
|
|
//! let mut run = true;
|
|
|
|
//! let mut run = true;
|
|
|
@ -39,16 +39,16 @@
|
|
|
|
//! match event {
|
|
|
|
//! match event {
|
|
|
|
//! Event::NewEvents(_) => {
|
|
|
|
//! Event::NewEvents(_) => {
|
|
|
|
//! // other application-specific logic
|
|
|
|
//! // other application-specific logic
|
|
|
|
//! last_frame = imgui.io_mut().update_delta_time(last_frame);
|
|
|
|
//! last_frame = imgui_supp.io_mut().update_delta_time(last_frame);
|
|
|
|
//! },
|
|
|
|
//! },
|
|
|
|
//! Event::MainEventsCleared => {
|
|
|
|
//! Event::MainEventsCleared => {
|
|
|
|
//! // other application-specific logic
|
|
|
|
//! // other application-specific logic
|
|
|
|
//! platform.prepare_frame(imgui.io_mut(), &window) // step 4
|
|
|
|
//! platform.prepare_frame(imgui_supp.io_mut(), &window) // step 4
|
|
|
|
//! .expect("Failed to prepare frame");
|
|
|
|
//! .expect("Failed to prepare frame");
|
|
|
|
//! window.request_redraw();
|
|
|
|
//! window.request_redraw();
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! Event::RedrawRequested(_) => {
|
|
|
|
//! Event::RedrawRequested(_) => {
|
|
|
|
//! let ui = imgui.frame();
|
|
|
|
//! let ui = imgui_supp.frame();
|
|
|
|
//! // application-specific rendering *under the UI*
|
|
|
|
//! // application-specific rendering *under the UI*
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! // construct the UI
|
|
|
|
//! // construct the UI
|
|
|
@ -65,7 +65,7 @@
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! // other application-specific event handling
|
|
|
|
//! // other application-specific event handling
|
|
|
|
//! event => {
|
|
|
|
//! event => {
|
|
|
|
//! platform.handle_event(imgui.io_mut(), &window, &event); // step 3
|
|
|
|
//! platform.handle_event(imgui_supp.io_mut(), &window, &event); // step 3
|
|
|
|
//! // other application-specific event handling
|
|
|
|
//! // other application-specific event handling
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
@ -107,7 +107,7 @@
|
|
|
|
//! your `Cargo.toml`:
|
|
|
|
//! your `Cargo.toml`:
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! ```toml
|
|
|
|
//! [dependencies.imgui-winit-support]
|
|
|
|
//! [dependencies.imgui_supp-winit-support]
|
|
|
|
//! version = "0.6"
|
|
|
|
//! version = "0.6"
|
|
|
|
//! features = ["winit-$YOUR_VERSION_HERE"]
|
|
|
|
//! features = ["winit-$YOUR_VERSION_HERE"]
|
|
|
|
//! default-features = false
|
|
|
|
//! default-features = false
|
|
|
@ -238,7 +238,7 @@ fn check_multiple_winits() {
|
|
|
|
// ensure atomicity.
|
|
|
|
// ensure atomicity.
|
|
|
|
let _ = writeln!(
|
|
|
|
let _ = writeln!(
|
|
|
|
err,
|
|
|
|
err,
|
|
|
|
"Warning (imgui-winit-support): More than one `winit-*` version feature is enabled \
|
|
|
|
"Warning (imgui_supp-winit-support): More than one `winit-*` version feature is enabled \
|
|
|
|
(this likely indicates misconfiguration, see documentation for details)."
|
|
|
|
(this likely indicates misconfiguration, see documentation for details)."
|
|
|
|
);
|
|
|
|
);
|
|
|
|
let feats = [
|
|
|
|
let feats = [
|
|
|
@ -373,10 +373,10 @@ enum ActiveHiDpiMode {
|
|
|
|
|
|
|
|
|
|
|
|
/// DPI factor handling mode.
|
|
|
|
/// DPI factor handling mode.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Applications that use imgui-rs might want to customize the used DPI factor and not use
|
|
|
|
/// Applications that use imgui_supp-rs might want to customize the used DPI factor and not use
|
|
|
|
/// directly the value coming from winit.
|
|
|
|
/// directly the value coming from winit.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// **Note: if you use a mode other than default and the DPI factor is adjusted, winit and imgui-rs
|
|
|
|
/// **Note: if you use a mode other than default and the DPI factor is adjusted, winit and imgui_supp-rs
|
|
|
|
/// will use different logical coordinates, so be careful if you pass around logical size or
|
|
|
|
/// will use different logical coordinates, so be careful if you pass around logical size or
|
|
|
|
/// position values.**
|
|
|
|
/// position values.**
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
@ -405,9 +405,9 @@ impl HiDpiMode {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl WinitPlatform {
|
|
|
|
impl WinitPlatform {
|
|
|
|
/// Initializes a winit platform instance and configures imgui.
|
|
|
|
/// Initializes a winit platform instance and configures imgui_supp.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This function configures imgui-rs in the following ways:
|
|
|
|
/// This function configures imgui_supp-rs in the following ways:
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// * backend flags are updated
|
|
|
|
/// * backend flags are updated
|
|
|
|
/// * keys are configured
|
|
|
|
/// * keys are configured
|
|
|
@ -441,7 +441,7 @@ impl WinitPlatform {
|
|
|
|
io[Key::Y] = VirtualKeyCode::Y as _;
|
|
|
|
io[Key::Y] = VirtualKeyCode::Y as _;
|
|
|
|
io[Key::Z] = VirtualKeyCode::Z as _;
|
|
|
|
io[Key::Z] = VirtualKeyCode::Z as _;
|
|
|
|
imgui.set_platform_name(Some(ImString::from(format!(
|
|
|
|
imgui.set_platform_name(Some(ImString::from(format!(
|
|
|
|
"imgui-winit-support {}",
|
|
|
|
"imgui_supp-winit-support {}",
|
|
|
|
env!("CARGO_PKG_VERSION")
|
|
|
|
env!("CARGO_PKG_VERSION")
|
|
|
|
))));
|
|
|
|
))));
|
|
|
|
WinitPlatform {
|
|
|
|
WinitPlatform {
|
|
|
@ -453,7 +453,7 @@ impl WinitPlatform {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Attaches the platform instance to a winit window.
|
|
|
|
/// Attaches the platform instance to a winit window.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This function configures imgui-rs in the following ways:
|
|
|
|
/// This function configures imgui_supp-rs in the following ways:
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// * framebuffer scale (= DPI factor) is set
|
|
|
|
/// * framebuffer scale (= DPI factor) is set
|
|
|
|
/// * display size is set
|
|
|
|
/// * display size is set
|
|
|
@ -478,7 +478,7 @@ impl WinitPlatform {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Attaches the platform instance to a winit window.
|
|
|
|
/// Attaches the platform instance to a winit window.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This function configures imgui-rs in the following ways:
|
|
|
|
/// This function configures imgui_supp-rs in the following ways:
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// * framebuffer scale (= DPI factor) is set
|
|
|
|
/// * framebuffer scale (= DPI factor) is set
|
|
|
|
/// * display size is set
|
|
|
|
/// * display size is set
|
|
|
@ -506,7 +506,7 @@ impl WinitPlatform {
|
|
|
|
/// Scales a logical size coming from winit using the current DPI mode.
|
|
|
|
/// Scales a logical size coming from winit using the current DPI mode.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// your application to use the same logical coordinates as imgui-rs.
|
|
|
|
/// your application to use the same logical coordinates as imgui_supp-rs.
|
|
|
|
#[cfg(all(
|
|
|
|
#[cfg(all(
|
|
|
|
not(any(
|
|
|
|
not(any(
|
|
|
|
feature = "winit-24",
|
|
|
|
feature = "winit-24",
|
|
|
@ -527,7 +527,7 @@ impl WinitPlatform {
|
|
|
|
/// Scales a logical size coming from winit using the current DPI mode.
|
|
|
|
/// Scales a logical size coming from winit using the current DPI mode.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// your application to use the same logical coordinates as imgui-rs.
|
|
|
|
/// your application to use the same logical coordinates as imgui_supp-rs.
|
|
|
|
#[cfg(any(
|
|
|
|
#[cfg(any(
|
|
|
|
feature = "winit-20",
|
|
|
|
feature = "winit-20",
|
|
|
|
feature = "winit-22",
|
|
|
|
feature = "winit-22",
|
|
|
@ -549,7 +549,7 @@ impl WinitPlatform {
|
|
|
|
/// Scales a logical position coming from winit using the current DPI mode.
|
|
|
|
/// Scales a logical position coming from winit using the current DPI mode.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// your application to use the same logical coordinates as imgui-rs.
|
|
|
|
/// your application to use the same logical coordinates as imgui_supp-rs.
|
|
|
|
#[cfg(all(
|
|
|
|
#[cfg(all(
|
|
|
|
not(any(
|
|
|
|
not(any(
|
|
|
|
feature = "winit-24",
|
|
|
|
feature = "winit-24",
|
|
|
@ -574,7 +574,7 @@ impl WinitPlatform {
|
|
|
|
/// Scales a logical position coming from winit using the current DPI mode.
|
|
|
|
/// Scales a logical position coming from winit using the current DPI mode.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// your application to use the same logical coordinates as imgui-rs.
|
|
|
|
/// your application to use the same logical coordinates as imgui_supp-rs.
|
|
|
|
#[cfg(any(
|
|
|
|
#[cfg(any(
|
|
|
|
feature = "winit-20",
|
|
|
|
feature = "winit-20",
|
|
|
|
feature = "winit-22",
|
|
|
|
feature = "winit-22",
|
|
|
@ -596,7 +596,7 @@ impl WinitPlatform {
|
|
|
|
/// Scales a logical position for winit using the current DPI mode.
|
|
|
|
/// Scales a logical position for winit using the current DPI mode.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// your application to use the same logical coordinates as imgui-rs.
|
|
|
|
/// your application to use the same logical coordinates as imgui_supp-rs.
|
|
|
|
#[cfg(all(
|
|
|
|
#[cfg(all(
|
|
|
|
not(any(
|
|
|
|
not(any(
|
|
|
|
feature = "winit-24",
|
|
|
|
feature = "winit-24",
|
|
|
@ -621,7 +621,7 @@ impl WinitPlatform {
|
|
|
|
/// Scales a logical position for winit using the current DPI mode.
|
|
|
|
/// Scales a logical position for winit using the current DPI mode.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// This utility function is useful if you are using a DPI mode other than default, and want
|
|
|
|
/// your application to use the same logical coordinates as imgui-rs.
|
|
|
|
/// your application to use the same logical coordinates as imgui_supp-rs.
|
|
|
|
#[cfg(any(
|
|
|
|
#[cfg(any(
|
|
|
|
feature = "winit-20",
|
|
|
|
feature = "winit-20",
|
|
|
|
feature = "winit-22",
|
|
|
|
feature = "winit-22",
|
|
|
@ -722,7 +722,7 @@ impl WinitPlatform {
|
|
|
|
|
|
|
|
|
|
|
|
// We need to track modifiers separately because some system like macOS, will
|
|
|
|
// We need to track modifiers separately because some system like macOS, will
|
|
|
|
// not reliably send modifier states during certain events like ScreenCapture.
|
|
|
|
// not reliably send modifier states during certain events like ScreenCapture.
|
|
|
|
// Gotta let the people show off their pretty imgui widgets!
|
|
|
|
// Gotta let the people show off their pretty imgui_supp widgets!
|
|
|
|
Event::DeviceEvent {
|
|
|
|
Event::DeviceEvent {
|
|
|
|
event: DeviceEvent::ModifiersChanged(modifiers),
|
|
|
|
event: DeviceEvent::ModifiersChanged(modifiers),
|
|
|
|
..
|
|
|
|
..
|
|
|
@ -751,7 +751,7 @@ impl WinitPlatform {
|
|
|
|
} if window_id == window.id() => {
|
|
|
|
} if window_id == window.id() => {
|
|
|
|
// We need to track modifiers separately because some system like macOS, will
|
|
|
|
// We need to track modifiers separately because some system like macOS, will
|
|
|
|
// not reliably send modifier states during certain events like ScreenCapture.
|
|
|
|
// not reliably send modifier states during certain events like ScreenCapture.
|
|
|
|
// Gotta let the people show off their pretty imgui widgets!
|
|
|
|
// Gotta let the people show off their pretty imgui_supp widgets!
|
|
|
|
if let OwnedWindowEvent::ModifiersChanged(modifiers) = event {
|
|
|
|
if let OwnedWindowEvent::ModifiersChanged(modifiers) = event {
|
|
|
|
io.key_shift = modifiers.shift();
|
|
|
|
io.key_shift = modifiers.shift();
|
|
|
|
io.key_ctrl = modifiers.ctrl();
|
|
|
|
io.key_ctrl = modifiers.ctrl();
|
|
|
@ -1084,10 +1084,10 @@ impl WinitPlatform {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Frame preparation callback.
|
|
|
|
/// Frame preparation callback.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Call this before calling the imgui-rs context `frame` function.
|
|
|
|
/// Call this before calling the imgui_supp-rs context `frame` function.
|
|
|
|
/// This function performs the following actions:
|
|
|
|
/// This function performs the following actions:
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// * mouse cursor is repositioned (if requested by imgui-rs)
|
|
|
|
/// * mouse cursor is repositioned (if requested by imgui_supp-rs)
|
|
|
|
#[cfg(all(
|
|
|
|
#[cfg(all(
|
|
|
|
not(any(
|
|
|
|
not(any(
|
|
|
|
feature = "winit-24",
|
|
|
|
feature = "winit-24",
|
|
|
@ -1111,10 +1111,10 @@ impl WinitPlatform {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Frame preparation callback.
|
|
|
|
/// Frame preparation callback.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Call this before calling the imgui-rs context `frame` function.
|
|
|
|
/// Call this before calling the imgui_supp-rs context `frame` function.
|
|
|
|
/// This function performs the following actions:
|
|
|
|
/// This function performs the following actions:
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// * mouse cursor is repositioned (if requested by imgui-rs)
|
|
|
|
/// * mouse cursor is repositioned (if requested by imgui_supp-rs)
|
|
|
|
#[cfg(any(
|
|
|
|
#[cfg(any(
|
|
|
|
feature = "winit-20",
|
|
|
|
feature = "winit-20",
|
|
|
|
feature = "winit-22",
|
|
|
|
feature = "winit-22",
|
|
|
@ -1142,10 +1142,10 @@ impl WinitPlatform {
|
|
|
|
|
|
|
|
|
|
|
|
/// Render preparation callback.
|
|
|
|
/// Render preparation callback.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Call this before calling the imgui-rs UI `render_with`/`render` function.
|
|
|
|
/// Call this before calling the imgui_supp-rs UI `render_with`/`render` function.
|
|
|
|
/// This function performs the following actions:
|
|
|
|
/// This function performs the following actions:
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// * mouse cursor is changed and/or hidden (if requested by imgui-rs)
|
|
|
|
/// * mouse cursor is changed and/or hidden (if requested by imgui_supp-rs)
|
|
|
|
pub fn prepare_render(&mut self, ui: &Ui, window: &Window) {
|
|
|
|
pub fn prepare_render(&mut self, ui: &Ui, window: &Window) {
|
|
|
|
let io = ui.io();
|
|
|
|
let io = ui.io();
|
|
|
|
if !io
|
|
|
|
if !io
|