Use generated ImGuiIO, and font-related structs

This commit is contained in:
Joonas Javanainen 2019-06-26 23:13:56 +03:00
parent 3ca1b7b4ec
commit e0264f8c9d
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
9 changed files with 169 additions and 440 deletions

View File

@ -16,6 +16,9 @@ travis-ci = { repository = "Gekkio/imgui-rs" }
[dependencies] [dependencies]
imgui-sys = { version = "0.0.24-pre", path = "imgui-sys" } imgui-sys = { version = "0.0.24-pre", path = "imgui-sys" }
[dev-dependencies]
memoffset = "0.3"
[workspace] [workspace]
members = [ members = [
"imgui-sys", "imgui-sys",

View File

@ -22,7 +22,6 @@ extern "C" {
// Main // Main
extern "C" { extern "C" {
pub fn igGetIO() -> *mut ImGuiIO;
pub fn igGetStyle() -> *mut ImGuiStyle; pub fn igGetStyle() -> *mut ImGuiStyle;
pub fn igNewFrame(); pub fn igNewFrame();
pub fn igEndFrame(); pub fn igEndFrame();

View File

@ -13,14 +13,19 @@ mod legacy;
mod structs; mod structs;
pub use bindings::{ pub use bindings::{
ImDrawCallback, ImDrawIdx, ImGuiContext, ImGuiID, ImGuiInputTextCallback, ImGuiInputTextCallbackData, igGetIO, ImDrawCallback, ImDrawIdx, ImFont, ImFontAtlas, ImFontAtlas_AddFont,
ImGuiSizeCallback, ImFontAtlas_AddFontDefault, ImFontAtlas_Clear, ImFontAtlas_GetGlyphRangesChineseFull,
ImTextureID, ImU32, ImVec2, ImVec2_Simple, ImVec4, ImVec4_Simple, ImWchar, ImFontAtlas_GetGlyphRangesChineseSimplifiedCommon, ImFontAtlas_GetGlyphRangesCyrillic,
ImGuiInputTextCallbackData_DeleteChars, ImFontAtlas_GetGlyphRangesDefault, ImFontAtlas_GetGlyphRangesJapanese,
ImGuiInputTextCallbackData_HasSelection, ImFontAtlas_GetGlyphRangesKorean, ImFontAtlas_GetGlyphRangesThai,
ImGuiInputTextCallbackData_ImGuiInputTextCallbackData, ImFontAtlas_GetGlyphRangesVietnamese, ImFontAtlas_GetTexDataAsRGBA32, ImFontConfig,
ImGuiInputTextCallbackData_InsertChars, ImGuiContext, ImGuiID, ImGuiIO, ImGuiIO_AddInputCharacter, ImGuiIO_AddInputCharactersUTF8,
ImGuiInputTextCallbackData_destroy, 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::*; pub use legacy::*;

View File

@ -6,6 +6,7 @@ use crate::flags::{
ImDrawCornerFlags, ImDrawListFlags, ImFontAtlasFlags, ImGuiBackendFlags, ImGuiConfigFlags, ImDrawCornerFlags, ImDrawListFlags, ImFontAtlasFlags, ImGuiBackendFlags, ImGuiConfigFlags,
ImGuiInputTextFlags, ImGuiInputTextFlags,
}; };
use crate::ImFont;
use crate::{ImDrawCallback, ImDrawIdx, ImGuiID, ImTextureID, ImU32, ImVec2, ImVec4, ImWchar}; use crate::{ImDrawCallback, ImDrawIdx, ImGuiID, ImTextureID, ImU32, ImVec2, ImVec4, ImWchar};
/// Font atlas glyph range builder /// Font atlas glyph range builder
@ -108,173 +109,6 @@ pub struct ImDrawVert {
pub col: ImU32, 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<ImFontGlyph>,
pub index_advance_x: ImVector<c_float>,
pub index_lookup: ImVector<ImWchar>,
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<CustomRect>,
pub config_data: ImVector<ImFontConfig>,
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<extern "C" fn(user_data: *mut c_void) -> *const c_char>,
pub set_clipboard_text_fn: Option<extern "C" fn(user_data: *mut c_void, text: *const c_char)>,
pub clipboard_user_data: *mut c_void,
pub ime_set_input_screen_pos_fn: Option<extern "C" fn(x: c_int, y: c_int)>,
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 /// Helper to manually clip large list of items
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@ -462,13 +296,6 @@ extern "C" {
pub fn ImGuiStyle_ScaleAllSizes(this: *mut ImGuiStyle, scale_factor: c_float); 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 // ImGuiTextFilter
extern "C" { extern "C" {
pub fn ImGuiTextFilter_Draw( pub fn ImGuiTextFilter_Draw(
@ -826,111 +653,6 @@ extern "C" {
pub fn ImDrawData_ScaleClipRects(this: *mut ImDrawData, sc: ImVec2); 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 // GlyphRangesBuilder
extern "C" { extern "C" {
pub fn GlyphRangesBuilder_GetBit(this: *mut GlyphRangesBuilder, n: c_int) -> bool; pub fn GlyphRangesBuilder_GetBit(this: *mut GlyphRangesBuilder, n: c_int) -> bool;
@ -952,66 +674,3 @@ extern "C" {
extern "C" { extern "C" {
pub fn CustomRect_IsPacked(this: *mut CustomRect) -> bool; 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);
}

View File

@ -3,7 +3,9 @@ use std::marker::PhantomData;
use std::mem; use std::mem;
use std::os::raw::{c_float, c_int, c_void}; use std::os::raw::{c_float, c_int, c_void};
use std::ptr; use std::ptr;
use sys;
use crate::internal::ImVector;
use crate::sys;
#[derive(Clone, Eq, PartialEq, Hash, Debug)] #[derive(Clone, Eq, PartialEq, Hash, Debug)]
enum FontGlyphRangeData { enum FontGlyphRangeData {
@ -205,18 +207,18 @@ impl ImFontConfig {
fn make_config(self) -> sys::ImFontConfig { fn make_config(self) -> sys::ImFontConfig {
let mut config = unsafe { let mut config = unsafe {
let mut config = mem::zeroed::<sys::ImFontConfig>(); let mut config = mem::zeroed::<sys::ImFontConfig>();
config.font_data_owned_by_atlas = true; config.FontDataOwnedByAtlas = true;
config.glyph_max_advance_x = f32::MAX as c_float; config.GlyphMaxAdvanceX = f32::MAX as c_float;
config config
}; };
config.size_pixels = self.size_pixels; config.SizePixels = self.size_pixels;
config.oversample_h = self.oversample_h as c_int; config.OversampleH = self.oversample_h as c_int;
config.oversample_v = self.oversample_v as c_int; config.OversampleV = self.oversample_v as c_int;
config.pixel_snap_h = self.pixel_snap_h; config.PixelSnapH = self.pixel_snap_h;
config.glyph_extra_spacing = self.glyph_extra_spacing; config.GlyphExtraSpacing = self.glyph_extra_spacing;
config.glyph_offset = self.glyph_offset; config.GlyphOffset = self.glyph_offset;
config.merge_mode = self.merge_mode; config.MergeMode = self.merge_mode;
config.rasterizer_multiply = self.rasterizer_multiply; config.RasterizerMultiply = self.rasterizer_multiply;
config config
} }
@ -268,27 +270,27 @@ impl<'a> ImFont<'a> {
} }
pub fn font_size(&self) -> f32 { 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 { pub fn set_font_size(&mut self, size: f32) -> ImFont {
unsafe { unsafe {
(*self.font).font_size = size; (*self.font).FontSize = size;
} }
self.chain() self.chain()
} }
pub fn scale(&self) -> f32 { pub fn scale(&self) -> f32 {
unsafe { (*self.font).scale } unsafe { (*self.font).Scale }
} }
pub fn set_scale(&mut self, size: f32) -> ImFont { pub fn set_scale(&mut self, size: f32) -> ImFont {
unsafe { unsafe {
(*self.font).scale = size; (*self.font).Scale = size;
} }
self.chain() self.chain()
} }
pub fn display_offset(&self) -> (f32, f32) { 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 { unsafe {
let mut config = config.make_config(); let mut config = config.make_config();
assert!(config.size_pixels > 0.0, "Font size cannot be zero."); assert!(config.SizePixels > 0.0, "Font size cannot be zero.");
config.font_data = data.as_ptr() as *mut c_void; config.FontData = data.as_ptr() as *mut c_void;
config.font_data_size = data.len() as c_int; config.FontDataSize = data.len() as c_int;
config.glyph_ranges = range.to_ptr(self.atlas); config.GlyphRanges = range.to_ptr(self.atlas);
config.font_data_owned_by_atlas = false; config.FontDataOwnedByAtlas = false;
ImFont::from_ptr(sys::ImFontAtlas_AddFont(self.atlas, &config)) 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. /// The number of fonts currently registered in the atlas.
pub fn font_count(&self) -> usize { 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. /// Gets a font from the atlas.
@ -372,7 +374,11 @@ impl<'a> ImFontAtlas<'a> {
/// ///
/// Panics if the index is out of range. /// Panics if the index is out of range.
pub fn index_font(&mut self, index: usize) -> ImFont { 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."); assert!(index < fonts.len(), "Font index is out of range.");
unsafe { ImFont::from_ptr(fonts[index]) } unsafe { ImFont::from_ptr(fonts[index]) }
} }
@ -383,11 +389,11 @@ impl<'a> ImFontAtlas<'a> {
} }
pub fn texture_id(&self) -> usize { 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) { pub fn set_texture_id(&mut self, value: usize) {
unsafe { unsafe {
(*self.atlas).tex_id = value as *mut c_void; (*self.atlas).TexID = value as *mut c_void;
} }
} }
} }

View File

@ -109,7 +109,6 @@ impl<'ui> Image<'ui> {
} }
} }
/// Represent an image button about to be drawn. /// Represent an image button about to be drawn.
/// See [`Ui::image_button`]. /// See [`Ui::image_button`].
/// ///

View File

@ -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, _) = { let (callback, data): (sys::ImGuiInputTextCallback, _) = {
if self.flags.contains(ImGuiInputTextFlags::CallbackResize) { if self.flags.contains(ImGuiInputTextFlags::CallbackResize) {
(Some(resize_callback), self.buf as *mut _ as *mut c_void) (Some(resize_callback), self.buf as *mut _ as *mut c_void)
} else { } else {
(None, ptr::null_mut()) (None, ptr::null_mut())
} }
}; };
unsafe { 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(); self.buf.refresh_len();
result result
} }
@ -225,13 +232,21 @@ impl<'ui, 'p> InputTextMultiline<'ui, 'p> {
let (callback, data): (sys::ImGuiInputTextCallback, _) = { let (callback, data): (sys::ImGuiInputTextCallback, _) = {
if self.flags.contains(ImGuiInputTextFlags::CallbackResize) { if self.flags.contains(ImGuiInputTextFlags::CallbackResize) {
(Some(resize_callback), self.buf as *mut _ as *mut c_void) (Some(resize_callback), self.buf as *mut _ as *mut c_void)
} else { } else {
(None, ptr::null_mut()) (None, ptr::null_mut())
} }
}; };
unsafe { 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(); self.buf.refresh_len();
result result
} }

39
src/internal.rs Normal file
View File

@ -0,0 +1,39 @@
use std::slice;
#[repr(C)]
pub struct ImVector<T> {
size: i32,
capacity: i32,
pub(crate) data: *mut T,
}
impl<T> ImVector<T> {
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::<ImVector<u8>>(),
mem::size_of::<sys::ImVector_char>()
);
assert_eq!(
mem::align_of::<ImVector<u8>>(),
mem::align_of::<sys::ImVector_char>()
);
use memoffset::offset_of;
macro_rules! assert_field_offset {
($l:ident, $r:ident) => {
assert_eq!(
offset_of!(ImVector<u8>, $l),
offset_of!(sys::ImVector_char, $r)
);
};
};
assert_field_offset!(size, Size);
assert_field_offset!(capacity, Capacity);
assert_field_offset!(data, Data);
}

View File

@ -50,6 +50,7 @@ mod drag;
mod fonts; mod fonts;
mod image; mod image;
mod input; mod input;
mod internal;
mod menus; mod menus;
mod plothistogram; mod plothistogram;
mod plotlines; mod plotlines;
@ -152,7 +153,7 @@ impl ImGui {
unsafe { &mut *sys::igGetStyle() } unsafe { &mut *sys::igGetStyle() }
} }
pub fn fonts(&mut self) -> ImFontAtlas { 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 pub fn prepare_texture<'a, F, T>(&mut self, f: F) -> T
where where
@ -165,7 +166,7 @@ impl ImGui {
let mut bytes_per_pixel: c_int = 0; let mut bytes_per_pixel: c_int = 0;
unsafe { unsafe {
sys::ImFontAtlas_GetTexDataAsRGBA32( sys::ImFontAtlas_GetTexDataAsRGBA32(
io.fonts, io.Fonts,
&mut pixels, &mut pixels,
&mut width, &mut width,
&mut height, &mut height,
@ -184,7 +185,7 @@ impl ImGui {
pub fn set_ini_filename(&mut self, value: Option<ImString>) { pub fn set_ini_filename(&mut self, value: Option<ImString>) {
{ {
let io = self.io_mut(); let io = self.io_mut();
io.ini_filename = match value { io.IniFilename = match value {
Some(ref x) => x.as_ptr(), Some(ref x) => x.as_ptr(),
None => ptr::null(), None => ptr::null(),
} }
@ -194,7 +195,7 @@ impl ImGui {
pub fn set_log_filename(&mut self, value: Option<ImString>) { pub fn set_log_filename(&mut self, value: Option<ImString>) {
{ {
let io = self.io_mut(); let io = self.io_mut();
io.log_filename = match value { io.LogFilename = match value {
Some(ref x) => x.as_ptr(), Some(ref x) => x.as_ptr(),
None => ptr::null(), None => ptr::null(),
} }
@ -203,89 +204,84 @@ impl ImGui {
} }
pub fn set_ini_saving_rate(&mut self, value: f32) { pub fn set_ini_saving_rate(&mut self, value: f32) {
let io = self.io_mut(); let io = self.io_mut();
io.ini_saving_rate = value; io.IniSavingRate = value;
} }
pub fn set_font_global_scale(&mut self, value: f32) { pub fn set_font_global_scale(&mut self, value: f32) {
let io = self.io_mut(); let io = self.io_mut();
io.font_global_scale = value; io.FontGlobalScale = value;
} }
pub fn set_mouse_double_click_time(&mut self, value: f32) { pub fn set_mouse_double_click_time(&mut self, value: f32) {
let io = self.io_mut(); 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) { pub fn set_mouse_double_click_max_dist(&mut self, value: f32) {
let io = self.io_mut(); 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) { pub fn set_mouse_drag_threshold(&mut self, value: f32) {
let io = self.io_mut(); let io = self.io_mut();
io.mouse_drag_threshold = value; io.MouseDragThreshold = value;
} }
pub fn set_key_repeat_delay(&mut self, value: f32) { pub fn set_key_repeat_delay(&mut self, value: f32) {
let io = self.io_mut(); let io = self.io_mut();
io.key_repeat_delay = value; io.KeyRepeatDelay = value;
} }
pub fn set_key_repeat_rate(&mut self, value: f32) { pub fn set_key_repeat_rate(&mut self, value: f32) {
let io = self.io_mut(); let io = self.io_mut();
io.key_repeat_rate = value; io.KeyRepeatRate = value;
} }
pub fn display_size(&self) -> (f32, f32) { pub fn display_size(&self) -> (f32, f32) {
let io = self.io(); 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) { pub fn display_framebuffer_scale(&self) -> (f32, f32) {
let io = self.io(); let io = self.io();
( (io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y)
io.display_framebuffer_scale.x,
io.display_framebuffer_scale.y,
)
} }
pub fn mouse_pos(&self) -> (f32, f32) { pub fn mouse_pos(&self) -> (f32, f32) {
let io = self.io(); 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) { pub fn set_mouse_pos(&mut self, x: f32, y: f32) {
let io = self.io_mut(); let io = self.io_mut();
io.mouse_pos.x = x; io.MousePos.x = x;
io.mouse_pos.y = y; io.MousePos.y = y;
} }
/// Get mouse's position's delta between the current and the last frame. /// Get mouse's position's delta between the current and the last frame.
pub fn mouse_delta(&self) -> (f32, f32) { pub fn mouse_delta(&self) -> (f32, f32) {
let io = self.io(); 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] { pub fn mouse_down(&self) -> [bool; 5] {
let io = self.io(); let io = self.io();
io.mouse_down io.MouseDown
} }
pub fn set_mouse_down(&mut self, states: [bool; 5]) { pub fn set_mouse_down(&mut self, states: [bool; 5]) {
let io = self.io_mut(); let io = self.io_mut();
io.mouse_down = states; io.MouseDown = states;
} }
pub fn set_mouse_wheel(&mut self, value: f32) { pub fn set_mouse_wheel(&mut self, value: f32) {
let io = self.io_mut(); let io = self.io_mut();
io.mouse_wheel = value; io.MouseWheel = value;
} }
/// Get mouse wheel delta /// Get mouse wheel delta
pub fn mouse_wheel(&self) -> f32 { pub fn mouse_wheel(&self) -> f32 {
let io = self.io(); let io = self.io();
io.mouse_wheel io.MouseWheel
} }
pub fn mouse_drag_delta(&self, button: ImMouseButton) -> (f32, f32) { pub fn mouse_drag_delta(&self, button: ImMouseButton) -> (f32, f32) {
let delta = unsafe { let delta = unsafe { sys::igGetMouseDragDelta_nonUDT2(button as c_int, -1.0) };
sys::igGetMouseDragDelta_nonUDT2(button as c_int, -1.0)
};
delta.into() delta.into()
} }
/// Set to `true` to have ImGui draw the cursor in software. /// Set to `true` to have ImGui draw the cursor in software.
/// If `false`, the OS cursor is used (default to `false`). /// If `false`, the OS cursor is used (default to `false`).
pub fn set_mouse_draw_cursor(&mut self, value: bool) { pub fn set_mouse_draw_cursor(&mut self, value: bool) {
let io = self.io_mut(); let io = self.io_mut();
io.mouse_draw_cursor = value; io.MouseDrawCursor = value;
} }
pub fn mouse_draw_cursor(&self) -> bool { pub fn mouse_draw_cursor(&self) -> bool {
let io = self.io(); let io = self.io();
io.mouse_draw_cursor io.MouseDrawCursor
} }
/// Set currently displayed cursor. /// Set currently displayed cursor.
/// Requires support in the windowing back-end if OS cursor is used. /// Requires support in the windowing back-end if OS cursor is used.
@ -323,43 +319,43 @@ impl ImGui {
} }
pub fn key_ctrl(&self) -> bool { pub fn key_ctrl(&self) -> bool {
let io = self.io(); let io = self.io();
io.key_ctrl io.KeyCtrl
} }
pub fn set_key_ctrl(&mut self, value: bool) { pub fn set_key_ctrl(&mut self, value: bool) {
let io = self.io_mut(); let io = self.io_mut();
io.key_ctrl = value; io.KeyCtrl = value;
} }
pub fn key_shift(&self) -> bool { pub fn key_shift(&self) -> bool {
let io = self.io(); let io = self.io();
io.key_shift io.KeyShift
} }
pub fn set_key_shift(&mut self, value: bool) { pub fn set_key_shift(&mut self, value: bool) {
let io = self.io_mut(); let io = self.io_mut();
io.key_shift = value; io.KeyShift = value;
} }
pub fn key_alt(&self) -> bool { pub fn key_alt(&self) -> bool {
let io = self.io(); let io = self.io();
io.key_alt io.KeyAlt
} }
pub fn set_key_alt(&mut self, value: bool) { pub fn set_key_alt(&mut self, value: bool) {
let io = self.io_mut(); let io = self.io_mut();
io.key_alt = value; io.KeyAlt = value;
} }
pub fn key_super(&self) -> bool { pub fn key_super(&self) -> bool {
let io = self.io(); let io = self.io();
io.key_super io.KeySuper
} }
pub fn set_key_super(&mut self, value: bool) { pub fn set_key_super(&mut self, value: bool) {
let io = self.io_mut(); let io = self.io_mut();
io.key_super = value; io.KeySuper = value;
} }
pub fn set_key(&mut self, key: u8, pressed: bool) { pub fn set_key(&mut self, key: u8, pressed: bool) {
let io = self.io_mut(); 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) { pub fn set_imgui_key(&mut self, key: ImGuiKey, mapping: u8) {
let io = self.io_mut(); 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 /// Map [`ImGuiKey`] values into user's key index
pub fn get_key_index(&self, key: ImGuiKey) -> usize { pub fn get_key_index(&self, key: ImGuiKey) -> usize {
@ -404,16 +400,16 @@ impl ImGui {
unsafe { sys::igGetFrameCount() } unsafe { sys::igGetFrameCount() }
} }
pub fn get_frame_rate(&self) -> f32 { 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> { pub fn frame<'ui, 'a: 'ui>(&'a mut self, frame_size: FrameSize, delta_time: f32) -> Ui<'ui> {
{ {
let io = self.io_mut(); let io = self.io_mut();
io.display_size.x = frame_size.logical_size.0 as c_float; io.DisplaySize.x = frame_size.logical_size.0 as c_float;
io.display_size.y = frame_size.logical_size.1 as c_float; io.DisplaySize.y = frame_size.logical_size.1 as c_float;
io.display_framebuffer_scale.x = frame_size.hidpi_factor as c_float; io.DisplayFramebufferScale.x = frame_size.hidpi_factor as c_float;
io.display_framebuffer_scale.y = frame_size.hidpi_factor as c_float; io.DisplayFramebufferScale.y = frame_size.hidpi_factor as c_float;
io.delta_time = delta_time; io.DeltaTime = delta_time;
} }
unsafe { unsafe {
sys::igNewFrame(); sys::igNewFrame();
@ -529,11 +525,11 @@ impl<'ui> Ui<'ui> {
} }
pub fn want_capture_mouse(&self) -> bool { pub fn want_capture_mouse(&self) -> bool {
let io = self.imgui.io(); let io = self.imgui.io();
io.want_capture_mouse io.WantCaptureMouse
} }
pub fn want_capture_keyboard(&self) -> bool { pub fn want_capture_keyboard(&self) -> bool {
let io = self.imgui.io(); let io = self.imgui.io();
io.want_capture_keyboard io.WantCaptureKeyboard
} }
pub fn set_keyboard_focus_here(&self, offset: i32) { pub fn set_keyboard_focus_here(&self, offset: i32) {
unsafe { unsafe {
@ -542,19 +538,19 @@ impl<'ui> Ui<'ui> {
} }
pub fn framerate(&self) -> f32 { pub fn framerate(&self) -> f32 {
let io = self.imgui.io(); let io = self.imgui.io();
io.framerate io.Framerate
} }
pub fn metrics_render_vertices(&self) -> i32 { pub fn metrics_render_vertices(&self) -> i32 {
let io = self.imgui.io(); let io = self.imgui.io();
io.metrics_render_vertices io.MetricsRenderVertices
} }
pub fn metrics_render_indices(&self) -> i32 { pub fn metrics_render_indices(&self) -> i32 {
let io = self.imgui.io(); let io = self.imgui.io();
io.metrics_render_indices io.MetricsRenderIndices
} }
pub fn metrics_active_windows(&self) -> i32 { pub fn metrics_active_windows(&self) -> i32 {
let io = self.imgui.io(); let io = self.imgui.io();
io.metrics_active_windows io.MetricsActiveWindows
} }
pub fn render<F, E>(self, f: F) -> Result<(), E> pub fn render<F, E>(self, f: F) -> Result<(), E>
where where
@ -1269,7 +1265,10 @@ impl<'ui> Ui<'ui> {
items: &'p [&'p StringType], items: &'p [&'p StringType],
height_in_items: i32, height_in_items: i32,
) -> bool { ) -> 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 { unsafe {
sys::igCombo( sys::igCombo(
label.as_ptr(), label.as_ptr(),
@ -1291,7 +1290,10 @@ impl<'ui> Ui<'ui> {
items: &'p [&'p StringType], items: &'p [&'p StringType],
height_in_items: i32, height_in_items: i32,
) -> bool { ) -> 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 { unsafe {
sys::igListBoxStr_arr( sys::igListBoxStr_arr(
label.as_ptr(), 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. /// 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) { pub fn set_item_allow_overlap(&self) {
unsafe{ sys::igSetItemAllowOverlap(); } unsafe {
sys::igSetItemAllowOverlap();
}
} }
/// Group items together as a single item. /// Group items together as a single item.