#![deny(missing_docs)]
#![deny(unused_results)]
#![deny(rust_2018_idioms)]
pub mod c {
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unused)]
#![allow(missing_docs)]
#![allow(unused_results)]
#![allow(rust_2018_idioms)]
#![allow(rustdoc::broken_intra_doc_links)]
#![allow(clippy::all)]
#[doc(inline)]
pub use imgui_sys::*;
}
#[derive(Default, Copy, Clone, Debug)]
pub struct WindowFlags {
flags: crate::c::ImGuiWindowFlags,
show_window_close: bool,
}
impl WindowFlags {
pub fn flags(&self) -> crate::c::ImGuiWindowFlags {
self.flags
}
pub fn no_title_bar(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiWindowFlags__ImGuiWindowFlags_NoTitleBar,
enable,
)
}
pub fn no_scrollbar(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiWindowFlags__ImGuiWindowFlags_NoScrollbar,
enable,
)
}
pub fn no_move(self, enable: bool) -> Self {
self.set_flag(crate::c::ImGuiWindowFlags__ImGuiWindowFlags_NoMove, enable)
}
pub fn no_resize(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiWindowFlags__ImGuiWindowFlags_NoResize,
enable,
)
}
pub fn no_collapse(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiWindowFlags__ImGuiWindowFlags_NoCollapse,
enable,
)
}
pub fn no_nav(self, enable: bool) -> Self {
self.set_flag(crate::c::ImGuiWindowFlags__ImGuiWindowFlags_NoNav, enable)
}
pub fn no_background(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiWindowFlags__ImGuiWindowFlags_NoBackground,
enable,
)
}
pub fn no_bring_to_front_on_focus(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiWindowFlags__ImGuiWindowFlags_NoBringToFrontOnFocus,
enable,
)
}
pub fn show_window_close(self, enable: bool) -> Self {
Self {
flags: self.flags,
show_window_close: enable,
}
}
const fn set_flag(self, flag: u32, enable: bool) -> Self {
if enable {
Self {
flags: self.flags | flag as i32,
show_window_close: self.show_window_close,
}
} else {
Self {
flags: self.flags & !flag as i32,
show_window_close: self.show_window_close,
}
}
}
}
pub trait Renderable {
fn render(&self);
}
pub fn begin(name: &str, open: Option<&mut bool>, flags: WindowFlags) -> bool {
unsafe {
let name = std::ffi::CString::new(name).unwrap();
let show_window_close = match open {
Some(open) => open as *mut _,
None => std::ptr::null_mut(),
};
c::igBegin(name.as_ptr(), show_window_close, flags.flags())
}
}
#[inline(always)]
pub fn begin_default(name: &str) -> bool {
begin(name, None, WindowFlags::default())
}
pub mod io {
use std::ffi::CStr;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConfigFlags(crate::c::ImGuiConfigFlags);
impl ConfigFlags {
pub fn flags(&self) -> crate::c::ImGuiConfigFlags {
self.0
}
pub fn allow_keyboard_navigation(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiConfigFlags__ImGuiConfigFlags_NavEnableKeyboard,
enable,
)
}
pub fn allow_gamepad_navigation(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiConfigFlags__ImGuiConfigFlags_NavEnableGamepad,
enable,
)
}
pub fn allow_mouse_navigation(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiConfigFlags__ImGuiConfigFlags_NavEnableSetMousePos,
enable,
)
}
pub fn allow_docking(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiConfigFlags__ImGuiConfigFlags_DockingEnable,
enable,
)
}
pub fn allow_viewports(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiConfigFlags__ImGuiConfigFlags_ViewportsEnable,
enable,
)
}
pub fn no_mouse(self, enable: bool) -> Self {
self.set_flag(crate::c::ImGuiConfigFlags__ImGuiConfigFlags_NoMouse, enable)
}
pub fn no_mouse_cursor_change(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiConfigFlags__ImGuiConfigFlags_NoMouseCursorChange,
enable,
)
}
pub fn dpi_scale_viewports(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiConfigFlags__ImGuiConfigFlags_DpiEnableScaleViewports,
enable,
)
}
pub fn dpi_scale_fonts(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiConfigFlags__ImGuiConfigFlags_DpiEnableScaleFonts,
enable,
)
}
pub fn set_srgb(self, enable: bool) -> Self {
self.set_flag(crate::c::ImGuiConfigFlags__ImGuiConfigFlags_IsSRGB, enable)
}
pub fn set_touchscreen(self, enable: bool) -> Self {
self.set_flag(
crate::c::ImGuiConfigFlags__ImGuiConfigFlags_IsTouchScreen,
enable,
)
}
const fn set_flag(self, flag: u32, enable: bool) -> Self {
if enable {
Self(self.0 | flag as i32)
} else {
Self(self.0 & !flag as i32)
}
}
}
pub fn set_io_config(config: ConfigFlags) {
unsafe {
let io = crate::c::igGetIO();
(*io).ConfigFlags = config.flags();
}
}
pub fn set_ini_filename_cstr(filename: &'static CStr) {
use crate::c;
unsafe {
let io = c::igGetIO();
(*io).IniFilename = filename.as_ptr();
}
}
pub unsafe fn set_ini_filename(filename: &'static str) {
let cstr = std::ffi::CStr::from_ptr(filename.as_ptr() as *const _);
set_ini_filename_cstr(cstr);
}
}
pub mod backend {
use crate::c;
use astd::ffi::cstr;
pub trait Backend {
fn init_for_window(&self, window: &mut glfw::Window);
fn new_frame(&self);
fn render(&self);
fn shutdown(&self);
}
#[cfg(feature = "opengl")]
pub struct OpenGL;
#[cfg(feature = "opengl")]
impl Backend for OpenGL {
fn init_for_window(&self, window: &mut glfw::Window) {
unsafe {
_ = c::backend::glfw::imgui_glfw_init_for_opengl(window.ptr_mut(), true);
_ = c::backend::opengl::imgui_opengl3_init(cstr!("#version 410").as_ptr());
}
}
fn new_frame(&self) {
unsafe {
_ = c::backend::opengl::imgui_opengl3_new_frame();
_ = c::backend::glfw::imgui_glfw_new_frame();
}
}
fn render(&self) {
unsafe {
_ = c::backend::opengl::imgui_opengl3_render_draw_data(c::igGetDrawData());
}
}
fn shutdown(&self) {
unsafe {
_ = c::backend::opengl::imgui_opengl3_shutdown();
}
}
}
}
pub struct Context(*mut c::ImGuiContext);
impl Default for Context {
fn default() -> Self {
Self::new()
}
}
impl Context {
pub fn new() -> Self {
unsafe { Self(c::igCreateContext(std::ptr::null_mut())) }
}
}
impl Drop for Context {
fn drop(&mut self) {
unsafe { c::igDestroyContext(self.0) }
}
}
pub fn new_frame() {
unsafe {
c::igNewFrame();
}
}
pub fn end_frame() {
unsafe {
c::igEndFrame();
}
}
#[macro_export]
macro_rules! text {
($($arg:tt)*) => {
unsafe {
use std::ffi::CString;
let s = format!($($arg)*);
let cs = CString::new(s).unwrap();
$crate::c::igText(cs.as_ptr());
}
};
}