use std::sync::Arc;
use crate::{error::CreationError, key_code::VirtualKeyCode, string::UlString, Library};
#[derive(Clone, Copy)]
pub enum KeyEventType {
RawKeyDown = ul_sys::ULKeyEventType_kKeyEventType_RawKeyDown as isize,
KeyDown = ul_sys::ULKeyEventType_kKeyEventType_KeyDown as isize,
KeyUp = ul_sys::ULKeyEventType_kKeyEventType_KeyUp as isize,
Char = ul_sys::ULKeyEventType_kKeyEventType_Char as isize,
}
pub struct KeyEventModifiers {
pub alt: bool,
pub ctrl: bool,
pub meta: bool,
pub shift: bool,
}
impl KeyEventModifiers {
fn to_u32(&self) -> u32 {
let mut n = 0;
if self.alt {
n |= 1 << 0;
}
if self.ctrl {
n |= 1 << 1;
}
if self.meta {
n |= 1 << 2;
}
if self.shift {
n |= 1 << 3;
}
n
}
}
pub struct KeyEventCreationInfo<'a, 'b> {
pub ty: KeyEventType,
pub modifiers: KeyEventModifiers,
pub virtual_key_code: VirtualKeyCode,
pub native_key_code: i32,
pub text: &'a str,
pub unmodified_text: &'b str,
pub is_keypad: bool,
pub is_auto_repeat: bool,
pub is_system_key: bool,
}
pub struct KeyEvent {
lib: Arc<Library>,
internal: ul_sys::ULKeyEvent,
}
impl KeyEvent {
pub fn new(
lib: Arc<Library>,
creation_info: KeyEventCreationInfo,
) -> Result<KeyEvent, CreationError> {
let ul_string_text = unsafe { UlString::from_str(lib.clone(), creation_info.text) }?;
let ul_string_unmodified_text =
unsafe { UlString::from_str(lib.clone(), creation_info.unmodified_text) }?;
let internal = unsafe {
lib.ultralight().ulCreateKeyEvent(
creation_info.ty as u32,
creation_info.modifiers.to_u32(),
creation_info.virtual_key_code.into(),
creation_info.native_key_code,
ul_string_text.to_ul(),
ul_string_unmodified_text.to_ul(),
creation_info.is_keypad,
creation_info.is_auto_repeat,
creation_info.is_system_key,
)
};
if internal.is_null() {
Err(CreationError::NullReference)
} else {
Ok(Self { lib, internal })
}
}
pub(crate) unsafe fn to_ul(&self) -> ul_sys::ULKeyEvent {
self.internal
}
}
impl Drop for KeyEvent {
fn drop(&mut self) {
unsafe {
self.lib.ultralight().ulDestroyKeyEvent(self.internal);
}
}
}
#[derive(Clone, Copy)]
pub enum MouseEventType {
MouseMoved = ul_sys::ULMouseEventType_kMouseEventType_MouseMoved as isize,
MouseDown = ul_sys::ULMouseEventType_kMouseEventType_MouseDown as isize,
MouseUp = ul_sys::ULMouseEventType_kMouseEventType_MouseUp as isize,
}
#[derive(Clone, Copy)]
pub enum MouseButton {
None = ul_sys::ULMouseButton_kMouseButton_None as isize,
Left = ul_sys::ULMouseButton_kMouseButton_Left as isize,
Middle = ul_sys::ULMouseButton_kMouseButton_Middle as isize,
Right = ul_sys::ULMouseButton_kMouseButton_Right as isize,
}
pub struct MouseEvent {
lib: Arc<Library>,
internal: ul_sys::ULMouseEvent,
}
impl MouseEvent {
pub fn new(
lib: Arc<Library>,
ty: MouseEventType,
x: i32,
y: i32,
button: MouseButton,
) -> Result<MouseEvent, CreationError> {
let internal = unsafe {
lib.ultralight()
.ulCreateMouseEvent(ty as u32, x, y, button as u32)
};
if internal.is_null() {
Err(CreationError::NullReference)
} else {
Ok(Self { lib, internal })
}
}
pub(crate) unsafe fn to_ul(&self) -> ul_sys::ULMouseEvent {
self.internal
}
}
impl Drop for MouseEvent {
fn drop(&mut self) {
unsafe {
self.lib.ultralight().ulDestroyMouseEvent(self.internal);
}
}
}
#[derive(Clone, Copy)]
pub enum ScrollEventType {
ScrollByPixel = ul_sys::ULScrollEventType_kScrollEventType_ScrollByPixel as isize,
ScrollByPage = ul_sys::ULScrollEventType_kScrollEventType_ScrollByPage as isize,
}
pub struct ScrollEvent {
lib: Arc<Library>,
internal: ul_sys::ULScrollEvent,
}
impl ScrollEvent {
pub fn new(
lib: Arc<Library>,
ty: ScrollEventType,
delta_x: i32,
delta_y: i32,
) -> Result<ScrollEvent, CreationError> {
let internal = unsafe {
lib.ultralight()
.ulCreateScrollEvent(ty as u32, delta_x, delta_y)
};
if internal.is_null() {
Err(CreationError::NullReference)
} else {
Ok(Self { lib, internal })
}
}
pub(crate) unsafe fn to_ul(&self) -> ul_sys::ULScrollEvent {
self.internal
}
}
impl Drop for ScrollEvent {
fn drop(&mut self) {
unsafe {
self.lib.ultralight().ulDestroyScrollEvent(self.internal);
}
}
}
#[derive(Clone, Copy)]
pub enum GamepadEventType {
Connected = ul_sys::ULGamepadEventType_kGamepadEventType_Connected as isize,
Disconnected = ul_sys::ULGamepadEventType_kGamepadEventType_Disconnected as isize,
}
pub struct GamepadEvent {
lib: Arc<Library>,
internal: ul_sys::ULGamepadEvent,
}
impl GamepadEvent {
pub fn new(
lib: Arc<Library>,
index: u32,
ty: GamepadEventType,
) -> Result<GamepadEvent, CreationError> {
let internal = unsafe { lib.ultralight().ulCreateGamepadEvent(index, ty as u32) };
if internal.is_null() {
Err(CreationError::NullReference)
} else {
Ok(Self { lib, internal })
}
}
pub(crate) unsafe fn to_ul(&self) -> ul_sys::ULGamepadEvent {
self.internal
}
}
impl Drop for GamepadEvent {
fn drop(&mut self) {
unsafe {
self.lib.ultralight().ulDestroyGamepadEvent(self.internal);
}
}
}
pub struct GamepadAxisEvent {
lib: Arc<Library>,
internal: ul_sys::ULGamepadAxisEvent,
}
impl GamepadAxisEvent {
pub fn new(
lib: Arc<Library>,
index: u32,
axis_index: u32,
value: f64,
) -> Result<GamepadAxisEvent, CreationError> {
let internal = unsafe {
lib.ultralight()
.ulCreateGamepadAxisEvent(index, axis_index, value)
};
if internal.is_null() {
Err(CreationError::NullReference)
} else {
Ok(Self { lib, internal })
}
}
pub(crate) unsafe fn to_ul(&self) -> ul_sys::ULGamepadAxisEvent {
self.internal
}
}
impl Drop for GamepadAxisEvent {
fn drop(&mut self) {
unsafe {
self.lib
.ultralight()
.ulDestroyGamepadAxisEvent(self.internal);
}
}
}
pub struct GamepadButtonEvent {
lib: Arc<Library>,
internal: ul_sys::ULGamepadButtonEvent,
}
impl GamepadButtonEvent {
pub fn new(
lib: Arc<Library>,
index: u32,
button_index: u32,
value: f64,
) -> Result<GamepadButtonEvent, CreationError> {
let internal = unsafe {
lib.ultralight()
.ulCreateGamepadButtonEvent(index, button_index, value)
};
if internal.is_null() {
Err(CreationError::NullReference)
} else {
Ok(Self { lib, internal })
}
}
pub(crate) unsafe fn to_ul(&self) -> ul_sys::ULGamepadButtonEvent {
self.internal
}
}
impl Drop for GamepadButtonEvent {
fn drop(&mut self) {
unsafe {
self.lib
.ultralight()
.ulDestroyGamepadButtonEvent(self.internal);
}
}
}