From e0264f8c9df05422e53b5fdfb3d3ebb16ad89e12 Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Wed, 26 Jun 2019 23:13:56 +0300 Subject: [PATCH] Use generated ImGuiIO, and font-related structs --- Cargo.toml | 3 + imgui-sys/src/legacy.rs | 1 - imgui-sys/src/lib.rs | 21 ++- imgui-sys/src/structs.rs | 343 +-------------------------------------- src/fonts.rs | 56 ++++--- src/image.rs | 1 - src/input.rs | 33 +++- src/internal.rs | 39 +++++ src/lib.rs | 112 +++++++------ 9 files changed, 169 insertions(+), 440 deletions(-) create mode 100644 src/internal.rs diff --git a/Cargo.toml b/Cargo.toml index 1434e8f..e5db611 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,9 @@ travis-ci = { repository = "Gekkio/imgui-rs" } [dependencies] imgui-sys = { version = "0.0.24-pre", path = "imgui-sys" } +[dev-dependencies] +memoffset = "0.3" + [workspace] members = [ "imgui-sys", diff --git a/imgui-sys/src/legacy.rs b/imgui-sys/src/legacy.rs index bee2261..140f8c9 100644 --- a/imgui-sys/src/legacy.rs +++ b/imgui-sys/src/legacy.rs @@ -22,7 +22,6 @@ extern "C" { // Main extern "C" { - pub fn igGetIO() -> *mut ImGuiIO; pub fn igGetStyle() -> *mut ImGuiStyle; pub fn igNewFrame(); pub fn igEndFrame(); diff --git a/imgui-sys/src/lib.rs b/imgui-sys/src/lib.rs index 51c5e87..a391d7c 100644 --- a/imgui-sys/src/lib.rs +++ b/imgui-sys/src/lib.rs @@ -13,14 +13,19 @@ mod legacy; mod structs; pub use bindings::{ - ImDrawCallback, ImDrawIdx, ImGuiContext, ImGuiID, ImGuiInputTextCallback, ImGuiInputTextCallbackData, - ImGuiSizeCallback, - ImTextureID, ImU32, ImVec2, ImVec2_Simple, ImVec4, ImVec4_Simple, ImWchar, - ImGuiInputTextCallbackData_DeleteChars, - ImGuiInputTextCallbackData_HasSelection, - ImGuiInputTextCallbackData_ImGuiInputTextCallbackData, - ImGuiInputTextCallbackData_InsertChars, - ImGuiInputTextCallbackData_destroy, + igGetIO, ImDrawCallback, ImDrawIdx, ImFont, ImFontAtlas, ImFontAtlas_AddFont, + ImFontAtlas_AddFontDefault, ImFontAtlas_Clear, ImFontAtlas_GetGlyphRangesChineseFull, + ImFontAtlas_GetGlyphRangesChineseSimplifiedCommon, ImFontAtlas_GetGlyphRangesCyrillic, + ImFontAtlas_GetGlyphRangesDefault, ImFontAtlas_GetGlyphRangesJapanese, + ImFontAtlas_GetGlyphRangesKorean, ImFontAtlas_GetGlyphRangesThai, + ImFontAtlas_GetGlyphRangesVietnamese, ImFontAtlas_GetTexDataAsRGBA32, ImFontConfig, + ImGuiContext, ImGuiID, ImGuiIO, ImGuiIO_AddInputCharacter, ImGuiIO_AddInputCharactersUTF8, + ImGuiIO_ClearInputCharacters, ImGuiInputTextCallback, ImGuiInputTextCallbackData, + ImGuiInputTextCallbackData_DeleteChars, ImGuiInputTextCallbackData_HasSelection, + ImGuiInputTextCallbackData_ImGuiInputTextCallbackData, ImGuiInputTextCallbackData_InsertChars, + ImGuiInputTextCallbackData_destroy, ImGuiSizeCallback, ImTextureID, ImU32, ImVec2, + ImVec2_Simple, ImVec4, ImVec4_Simple, ImVector_ImFontPtr, ImVector_ImWchar, ImVector_char, + ImWchar, }; pub use legacy::*; diff --git a/imgui-sys/src/structs.rs b/imgui-sys/src/structs.rs index ca529a2..9828797 100644 --- a/imgui-sys/src/structs.rs +++ b/imgui-sys/src/structs.rs @@ -6,6 +6,7 @@ use crate::flags::{ ImDrawCornerFlags, ImDrawListFlags, ImFontAtlasFlags, ImGuiBackendFlags, ImGuiConfigFlags, ImGuiInputTextFlags, }; +use crate::ImFont; use crate::{ImDrawCallback, ImDrawIdx, ImGuiID, ImTextureID, ImU32, ImVec2, ImVec4, ImWchar}; /// Font atlas glyph range builder @@ -108,173 +109,6 @@ pub struct ImDrawVert { pub col: ImU32, } -/// Runtime data for a single font within a parent ImFontAtlas -#[repr(C)] -pub struct ImFont { - pub font_size: c_float, - pub scale: c_float, - pub display_offset: ImVec2, - pub glyphs: ImVector, - pub index_advance_x: ImVector, - pub index_lookup: ImVector, - pub fallback_glyph: *const ImFontGlyph, - pub fallback_advance_x: c_float, - pub fallback_char: ImWchar, - - pub config_data_count: c_short, - pub config_data: *mut ImFontConfig, - pub container_font_atlas: *mut ImFontAtlas, - pub ascent: c_float, - pub descent: c_float, - pub dirty_lookup_tables: bool, - pub metrics_total_surface: c_int, -} - -/// Runtime data for multiple fonts, bake multiple fonts into a single texture, TTF/OTF font loader -#[repr(C)] -pub struct ImFontAtlas { - pub locked: bool, - pub flags: ImFontAtlasFlags, - pub tex_id: ImTextureID, - pub tex_desired_width: c_int, - pub tex_glyph_padding: c_int, - - pub tex_pixels_alpha8: *mut c_uchar, - pub tex_pixels_rgba32: *mut c_uint, - pub tex_width: c_int, - pub tex_height: c_int, - pub tex_uv_scale: ImVec2, - pub tex_uv_white_pixel: ImVec2, - pub fonts: ImVector<*mut ImFont>, - pub custom_rects: ImVector, - pub config_data: ImVector, - pub custom_rect_ids: [c_int; 1], -} - -/// Configuration data when adding a font or merging fonts -#[repr(C)] -pub struct ImFontConfig { - pub font_data: *mut c_void, - pub font_data_size: c_int, - pub font_data_owned_by_atlas: bool, - pub font_no: c_int, - pub size_pixels: c_float, - pub oversample_h: c_int, - pub oversample_v: c_int, - pub pixel_snap_h: bool, - pub glyph_extra_spacing: ImVec2, - pub glyph_offset: ImVec2, - pub glyph_ranges: *const ImWchar, - pub glyph_min_advance_x: c_float, - pub glyph_max_advance_x: c_float, - pub merge_mode: bool, - pub rasterizer_flags: c_uint, - pub rasterizer_multiply: c_float, - - name: [c_char; 40], - dst_font: *mut ImFont, -} - -/// Font glyph -#[repr(C)] -#[derive(Copy, Clone, Debug, Default)] -pub struct ImFontGlyph { - pub codepoint: ImWchar, - pub advance_x: c_float, - pub x0: c_float, - pub y0: c_float, - pub x1: c_float, - pub y1: c_float, - pub u0: c_float, - pub v0: c_float, - pub u1: c_float, - pub v1: c_float, -} - -/// Main configuration and I/O between your application and ImGui -#[repr(C)] -pub struct ImGuiIO { - pub config_flags: ImGuiConfigFlags, - pub backend_flags: ImGuiBackendFlags, - pub display_size: ImVec2, - pub delta_time: c_float, - pub ini_saving_rate: c_float, - pub ini_filename: *const c_char, - pub log_filename: *const c_char, - pub mouse_double_click_time: c_float, - pub mouse_double_click_max_dist: c_float, - pub mouse_drag_threshold: c_float, - pub key_map: [c_int; ImGuiKey::COUNT], - pub key_repeat_delay: c_float, - pub key_repeat_rate: c_float, - pub user_data: *mut c_void, - pub fonts: *mut ImFontAtlas, - pub font_global_scale: c_float, - pub font_allow_user_scaling: bool, - pub font_default: *mut ImFont, - pub display_framebuffer_scale: ImVec2, - pub display_visible_min: ImVec2, - pub display_visible_max: ImVec2, - pub mouse_draw_cursor: bool, - pub config_mac_osx_behaviors: bool, - pub config_input_text_cursor_blink: bool, - pub config_resize_windows_from_edges: bool, - pub backend_platform_name: *const c_char, - pub backend_renderer_name: *const c_char, - - pub get_clipboard_text_fn: Option *const c_char>, - pub set_clipboard_text_fn: Option, - pub clipboard_user_data: *mut c_void, - - pub ime_set_input_screen_pos_fn: Option, - pub ime_window_handle: *mut c_void, - - pub render_draw_lists_fn_unused: *mut c_void, - - pub mouse_pos: ImVec2, - pub mouse_down: [bool; 5], - pub mouse_wheel: c_float, - pub mouse_wheel_h: c_float, - pub key_ctrl: bool, - pub key_shift: bool, - pub key_alt: bool, - pub key_super: bool, - pub keys_down: [bool; 512], - pub input_characters: [ImWchar; 16 + 1], - pub nav_inputs: [c_float; ImGuiNavInput::COUNT_INTERNAL], - - 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: c_float, - pub metrics_render_vertices: c_int, - pub metrics_render_indices: c_int, - pub metrics_render_windows: c_int, - pub metrics_active_windows: c_int, - pub metrics_active_allocations: c_int, - pub mouse_delta: ImVec2, - - mouse_pos_prev: ImVec2, - mouse_clicked_pos: [ImVec2; 5], - mouse_clicked_time: [c_float; 5], - mouse_clicked: [bool; 5], - mouse_double_clicked: [bool; 5], - mouse_released: [bool; 5], - mouse_down_owned: [bool; 5], - mouse_down_duration: [c_float; 5], - mouse_down_duration_prev: [c_float; 5], - mouse_drag_max_distance_abs: [ImVec2; 5], - mouse_drag_max_distance_sqr: [c_float; 5], - keys_down_duration: [c_float; 512], - keys_down_duration_prev: [c_float; 512], - nav_inputs_down_duration: [c_float; ImGuiNavInput::COUNT_INTERNAL], - nav_inputs_down_duration_prev: [c_float; ImGuiNavInput::COUNT_INTERNAL], -} - /// Helper to manually clip large list of items #[repr(C)] #[derive(Copy, Clone, Debug)] @@ -462,13 +296,6 @@ extern "C" { pub fn ImGuiStyle_ScaleAllSizes(this: *mut ImGuiStyle, scale_factor: c_float); } -// ImGuiIO -extern "C" { - pub fn ImGuiIO_AddInputCharacter(this: *mut ImGuiIO, c: c_ushort); - pub fn ImGuiIO_AddInputCharactersUTF8(this: *mut ImGuiIO, utf8_chars: *const c_char); - pub fn ImGuiIO_ClearInputCharacters(this: *mut ImGuiIO); -} - // ImGuiTextFilter extern "C" { pub fn ImGuiTextFilter_Draw( @@ -826,111 +653,6 @@ extern "C" { pub fn ImDrawData_ScaleClipRects(this: *mut ImDrawData, sc: ImVec2); } -// ImFontAtlas -extern "C" { - pub fn ImFontAtlas_AddFont( - this: *mut ImFontAtlas, - font_cfg: *const ImFontConfig, - ) -> *mut ImFont; - pub fn ImFontAtlas_AddFontDefault( - this: *mut ImFontAtlas, - font_cfg: *const ImFontConfig, - ) -> *mut ImFont; - pub fn ImFontAtlas_AddFontFromFileTTF( - this: *mut ImFontAtlas, - filename: *const c_char, - size_pixels: c_float, - font_cfg: *const ImFontConfig, - glyph_ranges: *const ImWchar, - ) -> *mut ImFont; - pub fn ImFontAtlas_AddFontFromMemoryTTF( - this: *mut ImFontAtlas, - font_data: *mut c_void, - font_size: c_int, - size_pixels: c_float, - font_cfg: *const ImFontConfig, - glyph_ranges: *const ImWchar, - ) -> *mut ImFont; - pub fn ImFontAtlas_AddFontFromMemoryCompressedTTF( - this: *mut ImFontAtlas, - compressed_font_data: *const c_void, - compressed_font_size: c_int, - size_pixels: c_float, - font_cfg: *const ImFontConfig, - glyph_ranges: *const ImWchar, - ) -> *mut ImFont; - pub fn ImFontAtlas_AddFontFromMemoryCompressedBase85TTF( - this: *mut ImFontAtlas, - compressed_font_data_base85: *const c_char, - size_pixels: c_float, - font_cfg: *const ImFontConfig, - glyph_ranges: *const ImWchar, - ) -> *mut ImFont; - pub fn ImFontAtlas_ClearInputData(this: *mut ImFontAtlas); - pub fn ImFontAtlas_ClearTexData(this: *mut ImFontAtlas); - pub fn ImFontAtlas_ClearFonts(this: *mut ImFontAtlas); - pub fn ImFontAtlas_Clear(this: *mut ImFontAtlas); - pub fn ImFontAtlas_Build(this: *mut ImFontAtlas) -> bool; - pub fn ImFontAtlas_IsBuilt(this: *mut ImFontAtlas) -> bool; - pub fn ImFontAtlas_GetTexDataAsAlpha8( - this: *mut ImFontAtlas, - out_pixels: *mut *mut c_uchar, - out_width: *mut c_int, - out_height: *mut c_int, - out_bytes_per_pixel: *mut c_int, - ); - pub fn ImFontAtlas_GetTexDataAsRGBA32( - this: *mut ImFontAtlas, - out_pixels: *mut *mut c_uchar, - out_width: *mut c_int, - out_height: *mut c_int, - out_bytes_per_pixel: *mut c_int, - ); - pub fn ImFontAtlas_SetTexID(this: *mut ImFontAtlas, id: ImTextureID); - pub fn ImFontAtlas_GetGlyphRangesDefault(this: *mut ImFontAtlas) -> *const ImWchar; - pub fn ImFontAtlas_GetGlyphRangesKorean(this: *mut ImFontAtlas) -> *const ImWchar; - pub fn ImFontAtlas_GetGlyphRangesJapanese(this: *mut ImFontAtlas) -> *const ImWchar; - pub fn ImFontAtlas_GetGlyphRangesChineseFull(this: *mut ImFontAtlas) -> *const ImWchar; - pub fn ImFontAtlas_GetGlyphRangesChineseSimplifiedCommon( - this: *mut ImFontAtlas, - ) -> *const ImWchar; - pub fn ImFontAtlas_GetGlyphRangesCyrillic(this: *mut ImFontAtlas) -> *const ImWchar; - pub fn ImFontAtlas_GetGlyphRangesThai(this: *mut ImFontAtlas) -> *const ImWchar; - pub fn ImFontAtlas_AddCustomRectRegular( - this: *mut ImFontAtlas, - id: c_uint, - width: c_int, - height: c_int, - ) -> c_int; - pub fn ImFontAtlas_AddCustomRectFontGlyph( - this: *mut ImFontAtlas, - font: *mut ImFont, - id: ImWchar, - width: c_int, - height: c_int, - advance_x: c_float, - offset: ImVec2, - ) -> c_int; - pub fn ImFontAtlas_GetCustomRectByIndex( - this: *mut ImFontAtlas, - index: c_int, - ) -> *const CustomRect; - pub fn ImFontAtlas_CalcCustomRectUV( - this: *mut ImFontAtlas, - rect: *const CustomRect, - out_uv_min: *mut ImVec2, - out_uv_max: *mut ImVec2, - ); - pub fn ImFontAtlas_GetMouseCursorTexData( - this: *mut ImFontAtlas, - cursor: ImGuiMouseCursor, - out_offset: *mut ImVec2, - out_size: *mut ImVec2, - out_uv_border: *mut ImVec2, - out_uv_fill: *mut ImVec2, - ) -> bool; -} - // GlyphRangesBuilder extern "C" { pub fn GlyphRangesBuilder_GetBit(this: *mut GlyphRangesBuilder, n: c_int) -> bool; @@ -952,66 +674,3 @@ extern "C" { extern "C" { pub fn CustomRect_IsPacked(this: *mut CustomRect) -> bool; } - -// ImFont -extern "C" { - pub fn ImFont_ClearOutputData(this: *mut ImFont); - pub fn ImFont_BuildLookupTable(this: *mut ImFont); - pub fn ImFont_FindGlyph(this: *mut ImFont, c: ImWchar) -> *const ImFontGlyph; - pub fn ImFont_FindGlyphNoFallback(this: *mut ImFont, c: ImWchar) -> *const ImFontGlyph; - pub fn ImFont_SetFallbackChar(this: *mut ImFont, c: ImWchar); - pub fn ImFont_GetCharAdvance(this: *mut ImFont, c: ImWchar) -> c_float; - pub fn ImFont_IsLoaded(this: *mut ImFont) -> bool; - pub fn ImFont_GetDebugName(this: *mut ImFont) -> *const c_char; - pub fn ImFont_CalcTextSizeA_nonUDT2( - this: *mut ImFont, - size: c_float, - max_width: c_float, - wrap_width: c_float, - text_begin: *const c_char, - text_end: *const c_char, - remaining: *mut *const c_char, - ) -> ImVec2; - pub fn ImFont_CalcWordWrapPositionA( - this: *mut ImFont, - scale: c_float, - text: *const c_char, - text_end: *const c_char, - wrap_width: c_float, - ) -> *const c_char; - pub fn ImFont_RenderChar( - this: *mut ImFont, - draw_list: *mut ImDrawList, - size: c_float, - pos: ImVec2, - col: ImU32, - c: ImWchar, - ); - pub fn ImFont_RenderText( - this: *mut ImFont, - draw_list: *mut ImDrawList, - size: c_float, - pos: ImVec2, - col: ImU32, - clip_rect: ImVec4, - text_begin: *const c_char, - text_end: *const c_char, - wrap_width: c_float, - cpu_fine_clip: bool, - ); - pub fn ImFont_GrowIndex(this: *mut ImFont, new_size: c_int); - pub fn ImFont_AddGlyph( - this: *mut ImFont, - c: ImWchar, - x0: c_float, - y0: c_float, - x1: c_float, - y1: c_float, - u0: c_float, - v0: c_float, - u1: c_float, - v1: c_float, - advance_x: c_float, - ); - pub fn ImFont_AddRemapChar(this: *mut ImFont, dst: ImWchar, src: ImWchar, overwrite_dst: bool); -} diff --git a/src/fonts.rs b/src/fonts.rs index f193c4d..7a912f1 100644 --- a/src/fonts.rs +++ b/src/fonts.rs @@ -3,7 +3,9 @@ use std::marker::PhantomData; use std::mem; use std::os::raw::{c_float, c_int, c_void}; use std::ptr; -use sys; + +use crate::internal::ImVector; +use crate::sys; #[derive(Clone, Eq, PartialEq, Hash, Debug)] enum FontGlyphRangeData { @@ -205,18 +207,18 @@ impl ImFontConfig { fn make_config(self) -> sys::ImFontConfig { let mut config = unsafe { let mut config = mem::zeroed::(); - config.font_data_owned_by_atlas = true; - config.glyph_max_advance_x = f32::MAX as c_float; + config.FontDataOwnedByAtlas = true; + config.GlyphMaxAdvanceX = f32::MAX as c_float; config }; - config.size_pixels = self.size_pixels; - config.oversample_h = self.oversample_h as c_int; - config.oversample_v = self.oversample_v as c_int; - config.pixel_snap_h = self.pixel_snap_h; - config.glyph_extra_spacing = self.glyph_extra_spacing; - config.glyph_offset = self.glyph_offset; - config.merge_mode = self.merge_mode; - config.rasterizer_multiply = self.rasterizer_multiply; + config.SizePixels = self.size_pixels; + config.OversampleH = self.oversample_h as c_int; + config.OversampleV = self.oversample_v as c_int; + config.PixelSnapH = self.pixel_snap_h; + config.GlyphExtraSpacing = self.glyph_extra_spacing; + config.GlyphOffset = self.glyph_offset; + config.MergeMode = self.merge_mode; + config.RasterizerMultiply = self.rasterizer_multiply; config } @@ -268,27 +270,27 @@ impl<'a> ImFont<'a> { } pub fn font_size(&self) -> f32 { - unsafe { (*self.font).font_size } + unsafe { (*self.font).FontSize } } pub fn set_font_size(&mut self, size: f32) -> ImFont { unsafe { - (*self.font).font_size = size; + (*self.font).FontSize = size; } self.chain() } pub fn scale(&self) -> f32 { - unsafe { (*self.font).scale } + unsafe { (*self.font).Scale } } pub fn set_scale(&mut self, size: f32) -> ImFont { unsafe { - (*self.font).scale = size; + (*self.font).Scale = size; } self.chain() } pub fn display_offset(&self) -> (f32, f32) { - unsafe { (*self.font).display_offset.into() } + unsafe { (*self.font).DisplayOffset.into() } } } @@ -329,11 +331,11 @@ impl<'a> ImFontAtlas<'a> { ); unsafe { let mut config = config.make_config(); - assert!(config.size_pixels > 0.0, "Font size cannot be zero."); - config.font_data = data.as_ptr() as *mut c_void; - config.font_data_size = data.len() as c_int; - config.glyph_ranges = range.to_ptr(self.atlas); - config.font_data_owned_by_atlas = false; + assert!(config.SizePixels > 0.0, "Font size cannot be zero."); + config.FontData = data.as_ptr() as *mut c_void; + config.FontDataSize = data.len() as c_int; + config.GlyphRanges = range.to_ptr(self.atlas); + config.FontDataOwnedByAtlas = false; ImFont::from_ptr(sys::ImFontAtlas_AddFont(self.atlas, &config)) } @@ -362,7 +364,7 @@ impl<'a> ImFontAtlas<'a> { /// The number of fonts currently registered in the atlas. pub fn font_count(&self) -> usize { - unsafe { (*self.atlas).fonts.size as usize } + unsafe { (*self.atlas).Fonts.Size as usize } } /// Gets a font from the atlas. @@ -372,7 +374,11 @@ impl<'a> ImFontAtlas<'a> { /// /// Panics if the index is out of range. pub fn index_font(&mut self, index: usize) -> ImFont { - let fonts = unsafe { (*self.atlas).fonts.as_slice() }; + let fonts = unsafe { + let fonts: &sys::ImVector_ImFontPtr = &(*self.atlas).Fonts; + let fonts: &ImVector<*mut sys::ImFont> = ::std::mem::transmute(fonts); + fonts.as_slice() + }; assert!(index < fonts.len(), "Font index is out of range."); unsafe { ImFont::from_ptr(fonts[index]) } } @@ -383,11 +389,11 @@ impl<'a> ImFontAtlas<'a> { } pub fn texture_id(&self) -> usize { - unsafe { (*self.atlas).tex_id as usize } + unsafe { (*self.atlas).TexID as usize } } pub fn set_texture_id(&mut self, value: usize) { unsafe { - (*self.atlas).tex_id = value as *mut c_void; + (*self.atlas).TexID = value as *mut c_void; } } } diff --git a/src/image.rs b/src/image.rs index a56f48c..0b8a282 100644 --- a/src/image.rs +++ b/src/image.rs @@ -109,7 +109,6 @@ impl<'ui> Image<'ui> { } } - /// Represent an image button about to be drawn. /// See [`Ui::image_button`]. /// diff --git a/src/input.rs b/src/input.rs index 278d0c9..00677ca 100644 --- a/src/input.rs +++ b/src/input.rs @@ -150,7 +150,7 @@ extern "C" fn resize_callback(data: *mut sys::ImGuiInputTextCallbackData) -> c_i } } } - return 0 + return 0; } } @@ -182,13 +182,20 @@ impl<'ui, 'p> InputText<'ui, 'p> { let (callback, data): (sys::ImGuiInputTextCallback, _) = { if self.flags.contains(ImGuiInputTextFlags::CallbackResize) { (Some(resize_callback), self.buf as *mut _ as *mut c_void) - } else { - (None, ptr::null_mut()) + } else { + (None, ptr::null_mut()) } }; - + unsafe { - let result = sys::igInputText(self.label.as_ptr(), ptr, capacity, self.flags, callback, data); + let result = sys::igInputText( + self.label.as_ptr(), + ptr, + capacity, + self.flags, + callback, + data, + ); self.buf.refresh_len(); result } @@ -225,13 +232,21 @@ impl<'ui, 'p> InputTextMultiline<'ui, 'p> { let (callback, data): (sys::ImGuiInputTextCallback, _) = { if self.flags.contains(ImGuiInputTextFlags::CallbackResize) { (Some(resize_callback), self.buf as *mut _ as *mut c_void) - } else { - (None, ptr::null_mut()) + } else { + (None, ptr::null_mut()) } }; - + unsafe { - let result = sys::igInputTextMultiline(self.label.as_ptr(), ptr, capacity, self.size, self.flags, callback, data); + let result = sys::igInputTextMultiline( + self.label.as_ptr(), + ptr, + capacity, + self.size, + self.flags, + callback, + data, + ); self.buf.refresh_len(); result } diff --git a/src/internal.rs b/src/internal.rs new file mode 100644 index 0000000..01dfec1 --- /dev/null +++ b/src/internal.rs @@ -0,0 +1,39 @@ +use std::slice; + +#[repr(C)] +pub struct ImVector { + size: i32, + capacity: i32, + pub(crate) data: *mut T, +} + +impl ImVector { + pub unsafe fn as_slice(&self) -> &[T] { + slice::from_raw_parts(self.data, self.size as usize) + } +} + +#[test] +fn test_imvector_memory_layout() { + use std::mem; + assert_eq!( + mem::size_of::>(), + mem::size_of::() + ); + assert_eq!( + mem::align_of::>(), + mem::align_of::() + ); + use memoffset::offset_of; + macro_rules! assert_field_offset { + ($l:ident, $r:ident) => { + assert_eq!( + offset_of!(ImVector, $l), + offset_of!(sys::ImVector_char, $r) + ); + }; + }; + assert_field_offset!(size, Size); + assert_field_offset!(capacity, Capacity); + assert_field_offset!(data, Data); +} diff --git a/src/lib.rs b/src/lib.rs index c8b3556..baf69b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,7 @@ mod drag; mod fonts; mod image; mod input; +mod internal; mod menus; mod plothistogram; mod plotlines; @@ -152,7 +153,7 @@ impl ImGui { unsafe { &mut *sys::igGetStyle() } } pub fn fonts(&mut self) -> ImFontAtlas { - unsafe { ImFontAtlas::from_ptr(self.io_mut().fonts) } + unsafe { ImFontAtlas::from_ptr(self.io_mut().Fonts) } } pub fn prepare_texture<'a, F, T>(&mut self, f: F) -> T where @@ -165,7 +166,7 @@ impl ImGui { let mut bytes_per_pixel: c_int = 0; unsafe { sys::ImFontAtlas_GetTexDataAsRGBA32( - io.fonts, + io.Fonts, &mut pixels, &mut width, &mut height, @@ -184,7 +185,7 @@ impl ImGui { pub fn set_ini_filename(&mut self, value: Option) { { let io = self.io_mut(); - io.ini_filename = match value { + io.IniFilename = match value { Some(ref x) => x.as_ptr(), None => ptr::null(), } @@ -194,7 +195,7 @@ impl ImGui { pub fn set_log_filename(&mut self, value: Option) { { let io = self.io_mut(); - io.log_filename = match value { + io.LogFilename = match value { Some(ref x) => x.as_ptr(), None => ptr::null(), } @@ -203,89 +204,84 @@ impl ImGui { } pub fn set_ini_saving_rate(&mut self, value: f32) { let io = self.io_mut(); - io.ini_saving_rate = value; + io.IniSavingRate = value; } pub fn set_font_global_scale(&mut self, value: f32) { let io = self.io_mut(); - io.font_global_scale = value; + io.FontGlobalScale = value; } pub fn set_mouse_double_click_time(&mut self, value: f32) { let io = self.io_mut(); - io.mouse_double_click_time = value; + io.MouseDoubleClickTime = value; } pub fn set_mouse_double_click_max_dist(&mut self, value: f32) { let io = self.io_mut(); - io.mouse_double_click_max_dist = value; + io.MouseDoubleClickMaxDist = value; } pub fn set_mouse_drag_threshold(&mut self, value: f32) { let io = self.io_mut(); - io.mouse_drag_threshold = value; + io.MouseDragThreshold = value; } pub fn set_key_repeat_delay(&mut self, value: f32) { let io = self.io_mut(); - io.key_repeat_delay = value; + io.KeyRepeatDelay = value; } pub fn set_key_repeat_rate(&mut self, value: f32) { let io = self.io_mut(); - io.key_repeat_rate = value; + io.KeyRepeatRate = value; } pub fn display_size(&self) -> (f32, f32) { let io = self.io(); - (io.display_size.x, io.display_size.y) + (io.DisplaySize.x, io.DisplaySize.y) } pub fn display_framebuffer_scale(&self) -> (f32, f32) { let io = self.io(); - ( - io.display_framebuffer_scale.x, - io.display_framebuffer_scale.y, - ) + (io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y) } pub fn mouse_pos(&self) -> (f32, f32) { let io = self.io(); - (io.mouse_pos.x, io.mouse_pos.y) + (io.MousePos.x, io.MousePos.y) } pub fn set_mouse_pos(&mut self, x: f32, y: f32) { let io = self.io_mut(); - io.mouse_pos.x = x; - io.mouse_pos.y = y; + io.MousePos.x = x; + io.MousePos.y = y; } /// Get mouse's position's delta between the current and the last frame. pub fn mouse_delta(&self) -> (f32, f32) { let io = self.io(); - (io.mouse_delta.x, io.mouse_delta.y) + (io.MouseDelta.x, io.MouseDelta.y) } pub fn mouse_down(&self) -> [bool; 5] { let io = self.io(); - io.mouse_down + io.MouseDown } pub fn set_mouse_down(&mut self, states: [bool; 5]) { let io = self.io_mut(); - io.mouse_down = states; + io.MouseDown = states; } pub fn set_mouse_wheel(&mut self, value: f32) { let io = self.io_mut(); - io.mouse_wheel = value; + io.MouseWheel = value; } /// Get mouse wheel delta pub fn mouse_wheel(&self) -> f32 { let io = self.io(); - io.mouse_wheel + io.MouseWheel } pub fn mouse_drag_delta(&self, button: ImMouseButton) -> (f32, f32) { - let delta = unsafe { - sys::igGetMouseDragDelta_nonUDT2(button as c_int, -1.0) - }; + let delta = unsafe { sys::igGetMouseDragDelta_nonUDT2(button as c_int, -1.0) }; delta.into() } /// Set to `true` to have ImGui draw the cursor in software. /// If `false`, the OS cursor is used (default to `false`). pub fn set_mouse_draw_cursor(&mut self, value: bool) { let io = self.io_mut(); - io.mouse_draw_cursor = value; + io.MouseDrawCursor = value; } pub fn mouse_draw_cursor(&self) -> bool { let io = self.io(); - io.mouse_draw_cursor + io.MouseDrawCursor } /// Set currently displayed cursor. /// Requires support in the windowing back-end if OS cursor is used. @@ -323,43 +319,43 @@ impl ImGui { } pub fn key_ctrl(&self) -> bool { let io = self.io(); - io.key_ctrl + io.KeyCtrl } pub fn set_key_ctrl(&mut self, value: bool) { let io = self.io_mut(); - io.key_ctrl = value; + io.KeyCtrl = value; } pub fn key_shift(&self) -> bool { let io = self.io(); - io.key_shift + io.KeyShift } pub fn set_key_shift(&mut self, value: bool) { let io = self.io_mut(); - io.key_shift = value; + io.KeyShift = value; } pub fn key_alt(&self) -> bool { let io = self.io(); - io.key_alt + io.KeyAlt } pub fn set_key_alt(&mut self, value: bool) { let io = self.io_mut(); - io.key_alt = value; + io.KeyAlt = value; } pub fn key_super(&self) -> bool { let io = self.io(); - io.key_super + io.KeySuper } pub fn set_key_super(&mut self, value: bool) { let io = self.io_mut(); - io.key_super = value; + io.KeySuper = value; } pub fn set_key(&mut self, key: u8, pressed: bool) { let io = self.io_mut(); - io.keys_down[key as usize] = pressed; + io.KeysDown[key as usize] = pressed; } pub fn set_imgui_key(&mut self, key: ImGuiKey, mapping: u8) { let io = self.io_mut(); - io.key_map[key as usize] = i32::from(mapping); + io.KeyMap[key as usize] = i32::from(mapping); } /// Map [`ImGuiKey`] values into user's key index pub fn get_key_index(&self, key: ImGuiKey) -> usize { @@ -404,16 +400,16 @@ impl ImGui { unsafe { sys::igGetFrameCount() } } pub fn get_frame_rate(&self) -> f32 { - self.io().framerate + self.io().Framerate } pub fn frame<'ui, 'a: 'ui>(&'a mut self, frame_size: FrameSize, delta_time: f32) -> Ui<'ui> { { let io = self.io_mut(); - io.display_size.x = frame_size.logical_size.0 as c_float; - io.display_size.y = frame_size.logical_size.1 as c_float; - io.display_framebuffer_scale.x = frame_size.hidpi_factor as c_float; - io.display_framebuffer_scale.y = frame_size.hidpi_factor as c_float; - io.delta_time = delta_time; + io.DisplaySize.x = frame_size.logical_size.0 as c_float; + io.DisplaySize.y = frame_size.logical_size.1 as c_float; + io.DisplayFramebufferScale.x = frame_size.hidpi_factor as c_float; + io.DisplayFramebufferScale.y = frame_size.hidpi_factor as c_float; + io.DeltaTime = delta_time; } unsafe { sys::igNewFrame(); @@ -529,11 +525,11 @@ impl<'ui> Ui<'ui> { } pub fn want_capture_mouse(&self) -> bool { let io = self.imgui.io(); - io.want_capture_mouse + io.WantCaptureMouse } pub fn want_capture_keyboard(&self) -> bool { let io = self.imgui.io(); - io.want_capture_keyboard + io.WantCaptureKeyboard } pub fn set_keyboard_focus_here(&self, offset: i32) { unsafe { @@ -542,19 +538,19 @@ impl<'ui> Ui<'ui> { } pub fn framerate(&self) -> f32 { let io = self.imgui.io(); - io.framerate + io.Framerate } pub fn metrics_render_vertices(&self) -> i32 { let io = self.imgui.io(); - io.metrics_render_vertices + io.MetricsRenderVertices } pub fn metrics_render_indices(&self) -> i32 { let io = self.imgui.io(); - io.metrics_render_indices + io.MetricsRenderIndices } pub fn metrics_active_windows(&self) -> i32 { let io = self.imgui.io(); - io.metrics_active_windows + io.MetricsActiveWindows } pub fn render(self, f: F) -> Result<(), E> where @@ -1269,7 +1265,10 @@ impl<'ui> Ui<'ui> { items: &'p [&'p StringType], height_in_items: i32, ) -> bool { - let items_inner: Vec<*const c_char> = items.into_iter().map(|item| item.as_ref().as_ptr()).collect(); + let items_inner: Vec<*const c_char> = items + .into_iter() + .map(|item| item.as_ref().as_ptr()) + .collect(); unsafe { sys::igCombo( label.as_ptr(), @@ -1291,7 +1290,10 @@ impl<'ui> Ui<'ui> { items: &'p [&'p StringType], height_in_items: i32, ) -> bool { - let items_inner: Vec<*const c_char> = items.into_iter().map(|item| item.as_ref().as_ptr()).collect(); + let items_inner: Vec<*const c_char> = items + .into_iter() + .map(|item| item.as_ref().as_ptr()) + .collect(); unsafe { sys::igListBoxStr_arr( label.as_ptr(), @@ -1664,7 +1666,9 @@ impl<'ui> Ui<'ui> { /// Allow last item to be overlapped by a subsequent item. Both may be activated during the same frame before the later one takes priority. pub fn set_item_allow_overlap(&self) { - unsafe{ sys::igSetItemAllowOverlap(); } + unsafe { + sys::igSetItemAllowOverlap(); + } } /// Group items together as a single item.