#![cfg_attr(test, allow(clippy::float_cmp))]
#![deny(rust_2018_idioms)]
pub extern crate imgui_sys as sys;
use std::cell;
use std::os::raw::c_char;
pub use self::clipboard::*;
pub use self::color::ImColor32;
pub use self::context::*;
pub use self::drag_drop::{DragDropFlags, DragDropSource, DragDropTarget};
pub use self::draw_list::{ChannelsSplit, DrawListMut};
pub use self::fonts::atlas::*;
pub use self::fonts::font::*;
pub use self::fonts::glyph::*;
pub use self::fonts::glyph_ranges::*;
pub use self::input::keyboard::*;
pub use self::input::mouse::*;
pub use self::input_widget::*;
pub use self::io::*;
pub use self::layout::*;
pub use self::list_clipper::ListClipper;
pub use self::plothistogram::PlotHistogram;
pub use self::plotlines::PlotLines;
pub use self::popups::*;
pub use self::render::draw_data::*;
pub use self::render::renderer::*;
pub use self::stacks::*;
pub use self::string::*;
pub use self::style::*;
#[cfg(feature = "tables-api")]
pub use self::tables::*;
pub use self::text_filter::*;
pub use self::utils::*;
pub use self::widget::color_editors::*;
pub use self::widget::combo_box::*;
pub use self::widget::drag::*;
pub use self::widget::image::*;
pub use self::widget::list_box::*;
pub use self::widget::menu::*;
pub use self::widget::misc::*;
pub use self::widget::progress_bar::*;
pub use self::widget::selectable::*;
pub use self::widget::slider::*;
pub use self::widget::tab::*;
pub use self::widget::tree::*;
pub use self::window::child_window::*;
pub use self::window::*;
use internal::RawCast;
use math::*;
#[macro_use]
mod string;
#[macro_use]
mod tokens;
mod clipboard;
pub mod color;
mod columns;
mod context;
pub mod drag_drop;
pub mod draw_list;
mod fonts;
mod input;
mod input_widget;
pub mod internal;
mod io;
mod layout;
mod list_clipper;
mod math;
mod plothistogram;
mod plotlines;
mod popups;
mod render;
mod stacks;
mod style;
#[cfg(feature = "tables-api")]
mod tables;
#[cfg(test)]
mod test;
pub mod text_filter;
mod utils;
mod widget;
mod window;
#[doc(hidden)]
pub use core as __core;
#[doc(alias = "GetVersion")]
pub fn dear_imgui_version() -> &'static str {
unsafe {
let bytes = std::ffi::CStr::from_ptr(sys::igGetVersion()).to_bytes();
std::str::from_utf8_unchecked(bytes)
}
}
impl Context {
#[doc(alias = "GetTime")]
pub fn time(&self) -> f64 {
unsafe { sys::igGetTime() }
}
#[doc(alias = "GetFrameCount")]
pub fn frame_count(&self) -> i32 {
unsafe { sys::igGetFrameCount() }
}
}
#[derive(Debug)]
pub struct Ui {
buffer: cell::UnsafeCell<string::UiBuffer>,
}
impl Ui {
pub unsafe fn scratch_buffer(&self) -> &cell::UnsafeCell<string::UiBuffer> {
&self.buffer
}
fn scratch_txt(&self, txt: impl AsRef<str>) -> *const sys::cty::c_char {
unsafe {
let handle = &mut *self.buffer.get();
handle.scratch_txt(txt)
}
}
fn scratch_txt_opt(&self, txt: Option<impl AsRef<str>>) -> *const sys::cty::c_char {
unsafe {
let handle = &mut *self.buffer.get();
handle.scratch_txt_opt(txt)
}
}
fn scratch_txt_two(
&self,
txt_0: impl AsRef<str>,
txt_1: impl AsRef<str>,
) -> (*const sys::cty::c_char, *const sys::cty::c_char) {
unsafe {
let handle = &mut *self.buffer.get();
handle.scratch_txt_two(txt_0, txt_1)
}
}
fn scratch_txt_with_opt(
&self,
txt_0: impl AsRef<str>,
txt_1: Option<impl AsRef<str>>,
) -> (*const sys::cty::c_char, *const sys::cty::c_char) {
unsafe {
let handle = &mut *self.buffer.get();
handle.scratch_txt_with_opt(txt_0, txt_1)
}
}
#[doc(alias = "GetIO")]
pub fn io(&self) -> &Io {
unsafe { &*(sys::igGetIO() as *const Io) }
}
pub fn fonts(&self) -> &FontAtlas {
unsafe { &*(self.io().fonts as *const FontAtlas) }
}
pub fn clone_style(&self) -> Style {
unsafe { *self.style() }
}
#[deprecated(
since = "0.9.0",
note = "use `Context::render` to render frames, or `end_frame_early` to not render at all"
)]
pub fn render(&mut self) {
self.end_frame_early();
}
pub fn end_frame_early(&mut self) {
unsafe {
sys::igEndFrame();
}
}
}
impl Ui {
#[doc(alias = "ShowDemoWindow")]
pub fn show_demo_window(&self, opened: &mut bool) {
unsafe {
sys::igShowDemoWindow(opened);
}
}
#[doc(alias = "ShowAboutWindow")]
pub fn show_about_window(&self, opened: &mut bool) {
unsafe {
sys::igShowAboutWindow(opened);
}
}
#[doc(alias = "ShowMetricsWindow")]
pub fn show_metrics_window(&self, opened: &mut bool) {
unsafe {
sys::igShowMetricsWindow(opened);
}
}
#[doc(alias = "ShowStyleEditor")]
pub fn show_style_editor(&self, style: &mut Style) {
unsafe {
sys::igShowStyleEditor(style.raw_mut());
}
}
#[doc(alias = "ShowStyleEditor")]
pub fn show_default_style_editor(&self) {
unsafe { sys::igShowStyleEditor(std::ptr::null_mut()) };
}
#[doc(alias = "ShowUserGuide")]
pub fn show_user_guide(&self) {
unsafe { sys::igShowUserGuide() };
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
pub struct Id(pub(crate) u32);
impl Id {
#[deprecated(since="0.8", note="Use ui.new_id_int(...)")]
#[allow(non_snake_case)]
pub fn Int(input: i32, ui: &Ui) -> Self {
ui.new_id_int(input)
}
#[deprecated(since="0.8", note="Use ui.new_id_str(...)")]
#[allow(non_snake_case)]
pub fn Str(input: impl AsRef<str>, ui: &Ui) -> Self {
ui.new_id_str(input)
}
#[deprecated(since="0.8", note="Use ui.new_id_ptr(...)")]
#[allow(non_snake_case)]
pub fn Ptr<T>(input: &T, ui: &Ui) -> Self {
ui.new_id_ptr(input)
}
}
impl Ui {
pub fn new_id(&self, input: usize) -> Id {
let p = input as *const std::os::raw::c_void;
let value = unsafe { sys::igGetID_Ptr(p) };
Id(value)
}
pub fn new_id_int(&self, input: i32) -> Id {
let p = input as *const std::os::raw::c_void;
let value = unsafe { sys::igGetID_Ptr(p) };
Id(value)
}
pub fn new_id_ptr<T>(&self, input: &T) -> Id {
let p = input as *const T as *const sys::cty::c_void;
let value = unsafe { sys::igGetID_Ptr(p) };
Id(value)
}
pub fn new_id_str(&self, s: impl AsRef<str>) -> Id {
let s = s.as_ref();
let s1 = s.as_ptr() as *const std::os::raw::c_char;
let value = unsafe {
let s2 = s1.add(s.len());
sys::igGetID_StrStr(s1, s2)
};
Id(value)
}
}
impl Ui {
pub fn window<Label: AsRef<str>>(&self, name: Label) -> Window<'_, '_, Label> {
#[allow(deprecated)]
Window::new(self, name)
}
pub fn child_window<Label: AsRef<str>>(&self, name: Label) -> ChildWindow<'_> {
#[allow(deprecated)]
ChildWindow::new(self, name)
}
pub fn child_window_id(&self, id: Id) -> ChildWindow<'_> {
ChildWindow::new_id(self, id)
}
}
impl<'ui> Ui {
#[doc(alias = "InputText", alias = "InputTextWithHint")]
pub fn input_text<'p, L: AsRef<str>>(
&'ui self,
label: L,
buf: &'p mut String,
) -> InputText<'ui, 'p, L> {
InputText::new(self, label, buf)
}
#[doc(alias = "InputText", alias = "InputTextMultiline")]
pub fn input_text_multiline<'p, L: AsRef<str>>(
&'ui self,
label: L,
buf: &'p mut String,
size: [f32; 2],
) -> InputTextMultiline<'ui, 'p, L> {
InputTextMultiline::new(self, label, buf, size)
}
#[doc(alias = "InputFloat")]
pub fn input_float<'p, L: AsRef<str>>(
&'ui self,
label: L,
value: &'p mut f32,
) -> InputScalar<'ui, 'p, f32, L> {
self.input_scalar(label, value)
}
#[doc(alias = "InputFloat2")]
pub fn input_float2<'p, L, T>(
&'ui self,
label: L,
value: &'p mut T,
) -> InputFloat2<'ui, 'p, L, T>
where
L: AsRef<str>,
T: Copy + Into<MintVec2>,
MintVec2: Into<T> + Into<[f32; 2]>,
{
InputFloat2::new(self, label, value)
}
#[doc(alias = "InputFloat3")]
pub fn input_float3<'p, L, T>(
&'ui self,
label: L,
value: &'p mut T,
) -> InputFloat3<'ui, 'p, L, T>
where
L: AsRef<str>,
T: Copy + Into<MintVec3>,
MintVec3: Into<T> + Into<[f32; 3]>,
{
InputFloat3::new(self, label, value)
}
#[doc(alias = "InputFloat4")]
pub fn input_float4<'p, L, T>(
&'ui self,
label: L,
value: &'p mut T,
) -> InputFloat4<'ui, 'p, L, T>
where
L: AsRef<str>,
T: Copy + Into<MintVec4>,
MintVec4: Into<T> + Into<[f32; 4]>,
{
InputFloat4::new(self, label, value)
}
#[doc(alias = "InputInt")]
pub fn input_int<'p, L: AsRef<str>>(
&'ui self,
label: L,
value: &'p mut i32,
) -> InputScalar<'ui, 'p, i32, L> {
self.input_scalar(label, value).step(1)
}
#[doc(alias = "InputInt2")]
pub fn input_int2<'p, L, T>(&'ui self, label: L, value: &'p mut T) -> InputInt2<'ui, 'p, L, T>
where
L: AsRef<str>,
T: Copy + Into<MintIVec2>,
MintIVec2: Into<T> + Into<[i32; 2]>,
{
InputInt2::new(self, label, value)
}
#[doc(alias = "InputInt3")]
pub fn input_int3<'p, L, T>(&'ui self, label: L, value: &'p mut T) -> InputInt3<'ui, 'p, L, T>
where
L: AsRef<str>,
T: Copy + Into<MintIVec3>,
MintIVec3: Into<T> + Into<[i32; 3]>,
{
InputInt3::new(self, label, value)
}
#[doc(alias = "InputInt4")]
pub fn input_int4<'p, L, T>(&'ui self, label: L, value: &'p mut T) -> InputInt4<'ui, 'p, L, T>
where
L: AsRef<str>,
T: Copy + Into<MintIVec4>,
MintIVec4: Into<T> + Into<[i32; 4]>,
{
InputInt4::new(self, label, value)
}
#[doc(alias = "InputScalar")]
pub fn input_scalar<'p, L, T>(
&'ui self,
label: L,
value: &'p mut T,
) -> InputScalar<'ui, 'p, T, L>
where
L: AsRef<str>,
T: internal::DataTypeKind,
{
InputScalar::new(self, label, value)
}
#[doc(alias = "InputScalarN")]
pub fn input_scalar_n<'p, L, T>(
&'ui self,
label: L,
values: &'p mut [T],
) -> InputScalarN<'ui, 'p, T, L>
where
L: AsRef<str>,
T: internal::DataTypeKind,
{
InputScalarN::new(self, label, values)
}
}
create_token!(
pub struct TooltipToken<'ui>;
drop { sys::igEndTooltip() }
);
impl Ui {
#[doc(alias = "BeginTooltip", alias = "EndTootip")]
pub fn tooltip<F: FnOnce()>(&self, f: F) {
unsafe { sys::igBeginTooltip() };
f();
unsafe { sys::igEndTooltip() };
}
#[doc(alias = "BeginTooltip")]
pub fn begin_tooltip(&self) -> TooltipToken<'_> {
unsafe { sys::igBeginTooltip() };
TooltipToken::new(self)
}
#[doc(alias = "BeginTooltip", alias = "EndTootip")]
pub fn tooltip_text<T: AsRef<str>>(&self, text: T) {
self.tooltip(|| self.text(text));
}
}
create_token!(
pub struct DisabledToken<'ui>;
drop { sys::igEndDisabled() }
);
impl Ui {
#[doc(alias = "BeginDisabled")]
pub fn begin_disabled(&self, disabled: bool) -> DisabledToken<'_> {
unsafe { sys::igBeginDisabled(disabled) };
DisabledToken::new(self)
}
#[doc(alias = "BeginDisabled")]
pub fn begin_enabled(&self, enabled: bool) -> DisabledToken<'_> {
self.begin_disabled(!enabled)
}
#[doc(alias = "BeginDisabled", alias = "EndDisabled")]
pub fn disabled<F: FnOnce()>(&self, disabled: bool, f: F) {
unsafe { sys::igBeginDisabled(disabled) };
f();
unsafe { sys::igEndDisabled() };
}
#[doc(alias = "BeginDisabled", alias = "EndDisabled")]
pub fn enabled<F: FnOnce()>(&self, enabled: bool, f: F) {
self.disabled(!enabled, f)
}
}
impl Ui {
#[doc(alias = "ListBox")]
pub fn list_box<'p, StringType: AsRef<str> + ?Sized>(
&self,
label: impl AsRef<str>,
current_item: &mut i32,
items: &'p [&'p StringType],
height_in_items: i32,
) -> bool {
let (label_ptr, items_inner) = unsafe {
let handle = &mut *self.scratch_buffer().get();
handle.refresh_buffer();
let label_ptr = handle.push(label);
let items_inner: Vec<_> = items.iter().map(|&v| handle.push(v)).collect();
(label_ptr, items_inner)
};
unsafe {
sys::igListBox_Str_arr(
label_ptr,
current_item,
items_inner.as_ptr() as *mut *const c_char,
items_inner.len() as i32,
height_in_items,
)
}
}
}
impl<'ui> Ui {
#[doc(alias = "PlotLines")]
pub fn plot_lines<'p, Label: AsRef<str>>(
&'ui self,
label: Label,
values: &'p [f32],
) -> PlotLines<'ui, 'p, Label> {
PlotLines::new(self, label, values)
}
#[doc(alias = "PlotHistogram")]
pub fn plot_histogram<'p, Label: AsRef<str>>(
&'ui self,
label: Label,
values: &'p [f32],
) -> PlotHistogram<'ui, 'p, Label> {
PlotHistogram::new(self, label, values)
}
#[doc(alias = "CalcTextSize")]
pub fn calc_text_size<T: AsRef<str>>(&self, text: T) -> [f32; 2] {
self.calc_text_size_with_opts(text, false, -1.0)
}
#[doc(alias = "CalcTextSize")]
pub fn calc_text_size_with_opts<T: AsRef<str>>(
&self,
text: T,
hide_text_after_double_hash: bool,
wrap_width: f32,
) -> [f32; 2] {
let mut out = sys::ImVec2::zero();
let text = text.as_ref();
unsafe {
let start = text.as_ptr();
let end = start.add(text.len());
sys::igCalcTextSize(
&mut out,
start as *const c_char,
end as *const c_char,
hide_text_after_double_hash,
wrap_width,
)
};
out.into()
}
}
impl Ui {
#[must_use]
#[doc(alias = "GetWindowDrawList")]
pub fn get_window_draw_list(&self) -> DrawListMut<'_> {
DrawListMut::window(self)
}
#[must_use]
#[doc(alias = "GetBackgroundDrawList")]
pub fn get_background_draw_list(&self) -> DrawListMut<'_> {
DrawListMut::background(self)
}
#[must_use]
#[doc(alias = "GetForegroundDrawList")]
pub fn get_foreground_draw_list(&self) -> DrawListMut<'_> {
DrawListMut::foreground(self)
}
}
#[repr(i8)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Condition {
Never = -1,
Always = sys::ImGuiCond_Always as i8,
Once = sys::ImGuiCond_Once as i8,
FirstUseEver = sys::ImGuiCond_FirstUseEver as i8,
Appearing = sys::ImGuiCond_Appearing as i8,
}
#[repr(i32)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Direction {
None = sys::ImGuiDir_None,
Left = sys::ImGuiDir_Left,
Right = sys::ImGuiDir_Right,
Up = sys::ImGuiDir_Up,
Down = sys::ImGuiDir_Down,
}