use crate::{Id, sys};
use std::marker::PhantomData;
use std::ptr::NonNull;
#[derive(Copy, Clone, Debug)]
pub struct StateStorage<'ui> {
raw: NonNull<sys::ImGuiStorage>,
_phantom: PhantomData<&'ui mut sys::ImGuiStorage>,
}
impl<'ui> StateStorage<'ui> {
pub unsafe fn from_raw(raw: *mut sys::ImGuiStorage) -> Self {
let raw = NonNull::new(raw).expect("StateStorage::from_raw() requires non-null pointer");
Self {
raw,
_phantom: PhantomData,
}
}
pub fn as_raw(self) -> *mut sys::ImGuiStorage {
self.raw.as_ptr()
}
pub fn clear(&mut self) {
unsafe { sys::ImGuiStorage_Clear(self.raw.as_ptr()) }
}
pub fn get_int(&self, key: Id, default: i32) -> i32 {
unsafe { sys::ImGuiStorage_GetInt(self.raw.as_ptr(), key.raw(), default) }
}
pub fn set_int(&mut self, key: Id, value: i32) {
unsafe { sys::ImGuiStorage_SetInt(self.raw.as_ptr(), key.raw(), value) }
}
pub fn get_bool(&self, key: Id, default: bool) -> bool {
unsafe { sys::ImGuiStorage_GetBool(self.raw.as_ptr(), key.raw(), default) }
}
pub fn set_bool(&mut self, key: Id, value: bool) {
unsafe { sys::ImGuiStorage_SetBool(self.raw.as_ptr(), key.raw(), value) }
}
pub fn get_float(&self, key: Id, default: f32) -> f32 {
unsafe { sys::ImGuiStorage_GetFloat(self.raw.as_ptr(), key.raw(), default) }
}
pub fn set_float(&mut self, key: Id, value: f32) {
unsafe { sys::ImGuiStorage_SetFloat(self.raw.as_ptr(), key.raw(), value) }
}
}
#[derive(Debug, Default)]
pub struct OwnedStateStorage {
raw: sys::ImGuiStorage,
}
impl OwnedStateStorage {
pub fn new() -> Self {
Self::default()
}
pub fn as_mut(&mut self) -> &mut sys::ImGuiStorage {
&mut self.raw
}
pub fn as_ref(&self) -> &sys::ImGuiStorage {
&self.raw
}
pub fn as_raw_mut(&mut self) -> *mut sys::ImGuiStorage {
&mut self.raw as *mut sys::ImGuiStorage
}
pub fn as_raw(&self) -> *const sys::ImGuiStorage {
&self.raw as *const sys::ImGuiStorage
}
}
impl Drop for OwnedStateStorage {
fn drop(&mut self) {
unsafe { sys::ImGuiStorage_Clear(self.as_raw_mut()) }
}
}
#[must_use]
pub struct StateStorageToken {
prev: *mut sys::ImGuiStorage,
}
impl Drop for StateStorageToken {
fn drop(&mut self) {
unsafe { sys::igSetStateStorage(self.prev) }
}
}
impl crate::ui::Ui {
#[doc(alias = "GetStateStorage")]
pub fn state_storage(&self) -> StateStorage<'_> {
unsafe { StateStorage::from_raw(sys::igGetStateStorage()) }
}
#[doc(alias = "SetStateStorage")]
pub fn push_state_storage(&self, storage: &mut sys::ImGuiStorage) -> StateStorageToken {
unsafe {
let prev = sys::igGetStateStorage();
sys::igSetStateStorage(storage as *mut sys::ImGuiStorage);
StateStorageToken { prev }
}
}
#[doc(alias = "SetNextItemStorageID")]
pub fn set_next_item_storage_id(&self, storage_id: Id) {
unsafe { sys::igSetNextItemStorageID(storage_id.raw()) }
}
}