use crate::sys;
use crate::Ui;
#[repr(u32)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum Key {
Tab = sys::ImGuiKey_Tab,
LeftArrow = sys::ImGuiKey_LeftArrow,
RightArrow = sys::ImGuiKey_RightArrow,
UpArrow = sys::ImGuiKey_UpArrow,
DownArrow = sys::ImGuiKey_DownArrow,
PageUp = sys::ImGuiKey_PageUp,
PageDown = sys::ImGuiKey_PageDown,
Home = sys::ImGuiKey_Home,
End = sys::ImGuiKey_End,
Insert = sys::ImGuiKey_Insert,
Delete = sys::ImGuiKey_Delete,
Backspace = sys::ImGuiKey_Backspace,
Space = sys::ImGuiKey_Space,
Enter = sys::ImGuiKey_Enter,
Escape = sys::ImGuiKey_Escape,
KeyPadEnter = sys::ImGuiKey_KeyPadEnter,
A = sys::ImGuiKey_A,
C = sys::ImGuiKey_C,
V = sys::ImGuiKey_V,
X = sys::ImGuiKey_X,
Y = sys::ImGuiKey_Y,
Z = sys::ImGuiKey_Z,
}
impl Key {
pub const VARIANTS: [Key; Key::COUNT] = [
Key::Tab,
Key::LeftArrow,
Key::RightArrow,
Key::UpArrow,
Key::DownArrow,
Key::PageUp,
Key::PageDown,
Key::Home,
Key::End,
Key::Insert,
Key::Delete,
Key::Backspace,
Key::Space,
Key::Enter,
Key::Escape,
Key::KeyPadEnter,
Key::A,
Key::C,
Key::V,
Key::X,
Key::Y,
Key::Z,
];
pub const COUNT: usize = sys::ImGuiKey_COUNT as usize;
}
#[test]
fn test_key_variants() {
for (idx, &value) in Key::VARIANTS.iter().enumerate() {
assert_eq!(idx, value as usize);
}
}
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
pub enum FocusedWidget {
Previous,
Next,
Offset(u32),
}
impl FocusedWidget {
#[inline]
fn as_offset(self) -> i32 {
match self {
FocusedWidget::Previous => -1,
FocusedWidget::Next => 0,
FocusedWidget::Offset(offset) => offset as i32,
}
}
}
impl<'ui> Ui<'ui> {
#[inline]
#[doc(alias = "GetKeyIndex")]
fn key_index(&self, key: Key) -> i32 {
unsafe { sys::igGetKeyIndex(key as i32) }
}
#[inline]
#[doc(alias = "IsKeyDown")]
pub fn is_key_down(&self, key: Key) -> bool {
let key_index = self.key_index(key);
self.is_key_index_down(key_index)
}
#[inline]
#[doc(alias = "IsKeyDown")]
pub fn is_key_index_down(&self, key_index: i32) -> bool {
unsafe { sys::igIsKeyDown(key_index) }
}
#[inline]
#[doc(alias = "IsKeyPressed")]
pub fn is_key_pressed(&self, key: Key) -> bool {
let key_index = self.key_index(key);
self.is_key_index_pressed(key_index)
}
#[inline]
#[doc(alias = "IsKeyPressed")]
pub fn is_key_index_pressed(&self, key_index: i32) -> bool {
unsafe { sys::igIsKeyPressed(key_index, true) }
}
#[inline]
#[doc(alias = "IsKeyPressed")]
pub fn is_key_pressed_no_repeat(&self, key: Key) -> bool {
let key_index = self.key_index(key);
self.is_key_index_pressed_no_repeat(key_index)
}
#[inline]
#[doc(alias = "IsKeyPressed")]
pub fn is_key_index_pressed_no_repeat(&self, key_index: i32) -> bool {
unsafe { sys::igIsKeyPressed(key_index, false) }
}
#[inline]
#[doc(alias = "IsKeyReleased")]
pub fn is_key_released(&self, key: Key) -> bool {
let key_index = self.key_index(key);
self.is_key_index_released(key_index)
}
#[inline]
#[doc(alias = "IsKeyReleased")]
pub fn is_key_index_released(&self, key_index: i32) -> bool {
unsafe { sys::igIsKeyReleased(key_index) }
}
#[inline]
#[doc(alias = "GetKeyPressedAmount")]
pub fn key_pressed_amount(&self, key: Key, repeat_delay: f32, rate: f32) -> u32 {
let key_index = self.key_index(key);
self.key_index_pressed_amount(key_index, repeat_delay, rate)
}
#[inline]
#[doc(alias = "GetKeyPressedAmount")]
pub fn key_index_pressed_amount(&self, key_index: i32, repeat_delay: f32, rate: f32) -> u32 {
unsafe { sys::igGetKeyPressedAmount(key_index, repeat_delay, rate) as u32 }
}
#[inline]
#[doc(alias = "SetKeyboardFocusHere")]
pub fn set_keyboard_focus_here(&self) {
self.set_keyboard_focus_here_with_offset(FocusedWidget::Next);
}
#[inline]
#[doc(alias = "SetKeyboardFocusHere")]
pub fn set_keyboard_focus_here_with_offset(&self, target_widget: FocusedWidget) {
unsafe {
sys::igSetKeyboardFocusHere(target_widget.as_offset());
}
}
}