use bitflags::bitflags;
use std::f32;
use std::ops::{Index, IndexMut};
use std::os::raw::{c_char, c_int, c_void};
use std::time::Duration;
use crate::fonts::atlas::FontAtlas;
use crate::fonts::font::Font;
use crate::input::keyboard::Key;
use crate::input::mouse::MouseButton;
use crate::internal::{ImVector, RawCast};
use crate::sys;
bitflags! {
#[repr(transparent)]
pub struct ConfigFlags: u32 {
const NAV_ENABLE_KEYBOARD = sys::ImGuiConfigFlags_NavEnableKeyboard;
const NAV_ENABLE_GAMEPAD = sys::ImGuiConfigFlags_NavEnableGamepad;
const NAV_ENABLE_SET_MOUSE_POS = sys::ImGuiConfigFlags_NavEnableSetMousePos;
const NAV_NO_CAPTURE_KEYBOARD = sys::ImGuiConfigFlags_NavNoCaptureKeyboard;
const NO_MOUSE = sys::ImGuiConfigFlags_NoMouse;
const NO_MOUSE_CURSOR_CHANGE = sys::ImGuiConfigFlags_NoMouseCursorChange;
const IS_SRGB = sys::ImGuiConfigFlags_IsSRGB;
const IS_TOUCH_SCREEN = sys::ImGuiConfigFlags_IsTouchScreen;
}
}
bitflags! {
#[repr(transparent)]
pub struct BackendFlags: u32 {
const HAS_GAMEPAD = sys::ImGuiBackendFlags_HasGamepad;
const HAS_MOUSE_CURSORS = sys::ImGuiBackendFlags_HasMouseCursors;
const HAS_SET_MOUSE_POS = sys::ImGuiBackendFlags_HasSetMousePos;
const RENDERER_HAS_VTX_OFFSET = sys::ImGuiBackendFlags_RendererHasVtxOffset;
}
}
#[repr(u32)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum NavInput {
Activate = sys::ImGuiNavInput_Activate,
Cancel = sys::ImGuiNavInput_Cancel,
Input = sys::ImGuiNavInput_Input,
Menu = sys::ImGuiNavInput_Menu,
DpadLeft = sys::ImGuiNavInput_DpadLeft,
DpadRight = sys::ImGuiNavInput_DpadRight,
DpadUp = sys::ImGuiNavInput_DpadUp,
DpadDown = sys::ImGuiNavInput_DpadDown,
LStickLeft = sys::ImGuiNavInput_LStickLeft,
LStickRight = sys::ImGuiNavInput_LStickRight,
LStickUp = sys::ImGuiNavInput_LStickUp,
LStickDown = sys::ImGuiNavInput_LStickDown,
FocusPrev = sys::ImGuiNavInput_FocusPrev,
FocusNext = sys::ImGuiNavInput_FocusNext,
TweakSlow = sys::ImGuiNavInput_TweakSlow,
TweakFast = sys::ImGuiNavInput_TweakFast,
}
impl NavInput {
pub const VARIANTS: [NavInput; NavInput::COUNT] = [
NavInput::Activate,
NavInput::Cancel,
NavInput::Input,
NavInput::Menu,
NavInput::DpadLeft,
NavInput::DpadRight,
NavInput::DpadUp,
NavInput::DpadDown,
NavInput::LStickLeft,
NavInput::LStickRight,
NavInput::LStickUp,
NavInput::LStickDown,
NavInput::FocusPrev,
NavInput::FocusNext,
NavInput::TweakSlow,
NavInput::TweakFast,
];
const INTERNAL_COUNT: usize = 5;
pub const COUNT: usize = sys::ImGuiNavInput_COUNT as usize - NavInput::INTERNAL_COUNT;
}
#[test]
fn test_nav_input_variants() {
for (idx, &value) in NavInput::VARIANTS.iter().enumerate() {
assert_eq!(idx, value as usize);
}
}
#[repr(C)]
pub struct Io {
pub config_flags: ConfigFlags,
pub backend_flags: BackendFlags,
pub display_size: [f32; 2],
pub delta_time: f32,
pub ini_saving_rate: f32,
pub(crate) ini_filename: *const c_char,
pub(crate) log_filename: *const c_char,
pub mouse_double_click_time: f32,
pub mouse_double_click_max_dist: f32,
pub mouse_drag_threshold: f32,
pub key_map: [u32; sys::ImGuiKey_COUNT as usize],
pub key_repeat_delay: f32,
pub key_repeat_rate: f32,
user_data: *mut c_void,
pub(crate) fonts: *mut FontAtlas,
pub font_global_scale: f32,
pub font_allow_user_scaling: bool,
pub(crate) font_default: *mut Font,
pub display_framebuffer_scale: [f32; 2],
pub mouse_draw_cursor: bool,
pub config_mac_os_behaviors: bool,
pub config_input_text_cursor_blink: bool,
pub config_drag_click_to_input_text: bool,
pub config_windows_resize_from_edges: bool,
pub config_windows_move_from_title_bar_only: bool,
pub config_memory_compact_timer: f32,
pub(crate) backend_platform_name: *const c_char,
pub(crate) backend_renderer_name: *const c_char,
backend_platform_user_data: *mut c_void,
backend_renderer_user_data: *mut c_void,
backend_language_user_data: *mut c_void,
pub(crate) get_clipboard_text_fn:
Option<unsafe extern "C" fn(user_data: *mut c_void) -> *const c_char>,
pub(crate) set_clipboard_text_fn:
Option<unsafe extern "C" fn(user_data: *mut c_void, text: *const c_char)>,
pub(crate) clipboard_user_data: *mut c_void,
ime_set_input_screen_pos_fn: Option<unsafe extern "C" fn(x: c_int, y: c_int)>,
ime_window_handle: *mut c_void,
pub mouse_pos: [f32; 2],
pub mouse_down: [bool; 5],
pub mouse_wheel: f32,
pub mouse_wheel_h: f32,
pub key_ctrl: bool,
pub key_shift: bool,
pub key_alt: bool,
pub key_super: bool,
pub keys_down: [bool; 512],
pub nav_inputs: [f32; NavInput::COUNT + NavInput::INTERNAL_COUNT],
pub want_capture_mouse: bool,
pub want_capture_keyboard: bool,
pub want_text_input: bool,
pub want_set_mouse_pos: bool,
pub want_save_ini_settings: bool,
pub nav_active: bool,
pub nav_visible: bool,
pub framerate: f32,
pub metrics_render_vertices: i32,
pub metrics_render_indices: i32,
pub metrics_render_windows: i32,
pub metrics_active_windows: i32,
pub metrics_active_allocations: i32,
pub mouse_delta: [f32; 2],
key_mods: sys::ImGuiKeyModFlags,
mouse_pos_prev: [f32; 2],
mouse_clicked_pos: [[f32; 2]; 5],
mouse_clicked_time: [f64; 5],
mouse_clicked: [bool; 5],
mouse_double_clicked: [bool; 5],
mouse_released: [bool; 5],
mouse_down_owned: [bool; 5],
mouse_down_was_double_click: [bool; 5],
mouse_down_duration: [f32; 5],
mouse_down_duration_prev: [f32; 5],
mouse_drag_max_distance_abs: [[f32; 2]; 5],
mouse_drag_max_distance_sqr: [f32; 5],
keys_down_duration: [f32; 512],
keys_down_duration_prev: [f32; 512],
nav_inputs_down_duration: [f32; NavInput::COUNT + NavInput::INTERNAL_COUNT],
nav_inputs_down_duration_prev: [f32; NavInput::COUNT + NavInput::INTERNAL_COUNT],
pen_pressure: f32,
input_queue_surrogate: sys::ImWchar16,
input_queue_characters: ImVector<sys::ImWchar>,
}
unsafe impl RawCast<sys::ImGuiIO> for Io {}
impl Io {
#[doc(alias = "AddInputCharactersUTF8")]
pub fn add_input_character(&mut self, character: char) {
let mut buf = [0; 5];
character.encode_utf8(&mut buf);
unsafe {
sys::ImGuiIO_AddInputCharactersUTF8(self.raw_mut(), buf.as_ptr() as *const _);
}
}
#[doc(alias = "ClearCharacters")]
pub fn clear_input_characters(&mut self) {
unsafe {
sys::ImGuiIO_ClearInputCharacters(self.raw_mut());
}
}
pub fn peek_input_characters(&self) -> String {
let c16_slice = self.input_queue_characters.as_slice();
String::from_utf16(c16_slice).unwrap()
}
pub fn update_delta_time(&mut self, delta: Duration) {
let delta_s = delta.as_secs() as f32 + delta.subsec_nanos() as f32 / 1_000_000_000.0;
if delta_s > 0.0 {
self.delta_time = delta_s;
} else {
self.delta_time = f32::MIN_POSITIVE;
}
self.delta_time = delta_s;
}
}
impl Index<Key> for Io {
type Output = u32;
fn index(&self, index: Key) -> &u32 {
&self.key_map[index as usize]
}
}
impl IndexMut<Key> for Io {
fn index_mut(&mut self, index: Key) -> &mut u32 {
&mut self.key_map[index as usize]
}
}
impl Index<NavInput> for Io {
type Output = f32;
fn index(&self, index: NavInput) -> &f32 {
&self.nav_inputs[index as usize]
}
}
impl IndexMut<NavInput> for Io {
fn index_mut(&mut self, index: NavInput) -> &mut f32 {
&mut self.nav_inputs[index as usize]
}
}
impl Index<MouseButton> for Io {
type Output = bool;
fn index(&self, index: MouseButton) -> &bool {
&self.mouse_down[index as usize]
}
}
impl IndexMut<MouseButton> for Io {
fn index_mut(&mut self, index: MouseButton) -> &mut bool {
&mut self.mouse_down[index as usize]
}
}
#[test]
#[cfg(test)]
fn test_io_memory_layout() {
use std::mem;
assert_eq!(mem::size_of::<Io>(), mem::size_of::<sys::ImGuiIO>());
assert_eq!(mem::align_of::<Io>(), mem::align_of::<sys::ImGuiIO>());
use sys::ImGuiIO;
macro_rules! assert_field_offset {
($l:ident, $r:ident) => {
assert_eq!(
memoffset::offset_of!(Io, $l),
memoffset::offset_of!(ImGuiIO, $r)
);
};
}
assert_field_offset!(config_flags, ConfigFlags);
assert_field_offset!(backend_flags, BackendFlags);
assert_field_offset!(display_size, DisplaySize);
assert_field_offset!(delta_time, DeltaTime);
assert_field_offset!(ini_saving_rate, IniSavingRate);
assert_field_offset!(ini_filename, IniFilename);
assert_field_offset!(log_filename, LogFilename);
assert_field_offset!(mouse_double_click_time, MouseDoubleClickTime);
assert_field_offset!(mouse_double_click_max_dist, MouseDoubleClickMaxDist);
assert_field_offset!(mouse_drag_threshold, MouseDragThreshold);
assert_field_offset!(key_map, KeyMap);
assert_field_offset!(key_repeat_delay, KeyRepeatDelay);
assert_field_offset!(key_repeat_rate, KeyRepeatRate);
assert_field_offset!(user_data, UserData);
assert_field_offset!(fonts, Fonts);
assert_field_offset!(font_global_scale, FontGlobalScale);
assert_field_offset!(font_allow_user_scaling, FontAllowUserScaling);
assert_field_offset!(font_default, FontDefault);
assert_field_offset!(display_framebuffer_scale, DisplayFramebufferScale);
assert_field_offset!(mouse_draw_cursor, MouseDrawCursor);
assert_field_offset!(config_mac_os_behaviors, ConfigMacOSXBehaviors);
assert_field_offset!(config_input_text_cursor_blink, ConfigInputTextCursorBlink);
assert_field_offset!(
config_windows_resize_from_edges,
ConfigWindowsResizeFromEdges
);
assert_field_offset!(
config_windows_move_from_title_bar_only,
ConfigWindowsMoveFromTitleBarOnly
);
assert_field_offset!(backend_platform_name, BackendPlatformName);
assert_field_offset!(backend_renderer_name, BackendRendererName);
assert_field_offset!(backend_platform_user_data, BackendPlatformUserData);
assert_field_offset!(backend_renderer_user_data, BackendRendererUserData);
assert_field_offset!(backend_language_user_data, BackendLanguageUserData);
assert_field_offset!(get_clipboard_text_fn, GetClipboardTextFn);
assert_field_offset!(set_clipboard_text_fn, SetClipboardTextFn);
assert_field_offset!(clipboard_user_data, ClipboardUserData);
assert_field_offset!(ime_set_input_screen_pos_fn, ImeSetInputScreenPosFn);
assert_field_offset!(ime_window_handle, ImeWindowHandle);
assert_field_offset!(mouse_pos, MousePos);
assert_field_offset!(mouse_down, MouseDown);
assert_field_offset!(mouse_wheel, MouseWheel);
assert_field_offset!(mouse_wheel_h, MouseWheelH);
assert_field_offset!(key_ctrl, KeyCtrl);
assert_field_offset!(key_shift, KeyShift);
assert_field_offset!(key_alt, KeyAlt);
assert_field_offset!(key_super, KeySuper);
assert_field_offset!(keys_down, KeysDown);
assert_field_offset!(nav_inputs, NavInputs);
assert_field_offset!(want_capture_mouse, WantCaptureMouse);
assert_field_offset!(want_capture_keyboard, WantCaptureKeyboard);
assert_field_offset!(want_text_input, WantTextInput);
assert_field_offset!(want_set_mouse_pos, WantSetMousePos);
assert_field_offset!(want_save_ini_settings, WantSaveIniSettings);
assert_field_offset!(nav_active, NavActive);
assert_field_offset!(nav_visible, NavVisible);
assert_field_offset!(framerate, Framerate);
assert_field_offset!(metrics_render_vertices, MetricsRenderVertices);
assert_field_offset!(metrics_render_indices, MetricsRenderIndices);
assert_field_offset!(metrics_render_windows, MetricsRenderWindows);
assert_field_offset!(metrics_active_windows, MetricsActiveWindows);
assert_field_offset!(metrics_active_allocations, MetricsActiveAllocations);
assert_field_offset!(mouse_delta, MouseDelta);
assert_field_offset!(key_mods, KeyMods);
assert_field_offset!(mouse_pos_prev, MousePosPrev);
assert_field_offset!(mouse_clicked_pos, MouseClickedPos);
assert_field_offset!(mouse_clicked_time, MouseClickedTime);
assert_field_offset!(mouse_clicked, MouseClicked);
assert_field_offset!(mouse_double_clicked, MouseDoubleClicked);
assert_field_offset!(mouse_released, MouseReleased);
assert_field_offset!(mouse_down_owned, MouseDownOwned);
assert_field_offset!(mouse_down_was_double_click, MouseDownWasDoubleClick);
assert_field_offset!(mouse_down_duration, MouseDownDuration);
assert_field_offset!(mouse_down_duration_prev, MouseDownDurationPrev);
assert_field_offset!(mouse_drag_max_distance_abs, MouseDragMaxDistanceAbs);
assert_field_offset!(mouse_drag_max_distance_sqr, MouseDragMaxDistanceSqr);
assert_field_offset!(keys_down_duration, KeysDownDuration);
assert_field_offset!(keys_down_duration_prev, KeysDownDurationPrev);
assert_field_offset!(nav_inputs_down_duration, NavInputsDownDuration);
assert_field_offset!(nav_inputs_down_duration_prev, NavInputsDownDurationPrev);
assert_field_offset!(pen_pressure, PenPressure);
assert_field_offset!(input_queue_surrogate, InputQueueSurrogate);
assert_field_offset!(input_queue_characters, InputQueueCharacters);
}