use crate::{AppControl, ConstInit, MaybeTimed};
use crate::{EventKey, EventMouse, EventPointer, EventTimestamp, EventWheel, EventWindow};
crate::enumset! {
#[doc = crate::_tags!(event uid member)]
#[doc = crate::_doc_meta!{
location("ui/event"),
test_size_of(EventTag = 1|8; niche Option),
}]
#[repr(u8)]
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum EventTag(
#[doc = crate::_tags!(event data_structure)]
#[doc = crate::_doc_meta!{
location("ui/event"),
test_size_of(EventTagSet = 1|8),
}]
pub EventTagSet: u8
) {
None,
Key,
Mouse,
Pointer,
Wheel,
Window,
Control,
}
impl set #[doc = "Common event category groups."] {
pub const INPUT: Self = Self::Key
.with(Self::Mouse)
.with(Self::Pointer)
.with(Self::Wheel);
pub const POINTING: Self = Self::Mouse
.with(Self::Pointer)
.with(Self::Wheel);
}
}
impl Default for EventTag {
fn default() -> Self {
Self::None
}
}
#[doc = crate::_tags!(event time maybe)]
#[doc = crate::_doc_meta!{
location("ui/event"),
#[cfg(not(feature = "alloc"))]
test_size_of(EventKindTimed = 40|320; niche Option),
#[cfg(feature = "alloc")]
test_size_of(EventKindTimed = 48|384; niche Option),
}]
pub type EventKindTimed = MaybeTimed<EventKind, EventTimestamp>;
#[doc = crate::_tags!(event)]
#[doc = crate::_doc_meta!{
location("ui/event"),
#[cfg(not(feature = "alloc"))]
test_size_of(EventKind = 36|288; niche Option),
#[cfg(feature = "alloc")]
test_size_of(EventKind = 40|320; niche Option),
}]
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub enum EventKind {
#[default]
None,
Key(EventKey),
Mouse(EventMouse),
Pointer(EventPointer),
Wheel(EventWheel),
Window(EventWindow),
Control(AppControl),
}
impl ConstInit for EventKind {
const INIT: Self = Self::None;
}
impl EventKind {
#[must_use]
#[inline(always)]
pub const fn has_tag(&self, tag: EventTag) -> bool {
self.tag().to_set().contains(tag.to_set())
}
#[must_use]
#[inline(always)]
pub const fn is_in(&self, set: EventTagSet) -> bool {
self.tag().is_in(set)
}
pub const fn tag(&self) -> EventTag {
match self {
Self::None => EventTag::None,
Self::Key(_) => EventTag::Key,
Self::Mouse(_) => EventTag::Mouse,
Self::Pointer(_) => EventTag::Pointer,
Self::Wheel(_) => EventTag::Wheel,
Self::Window(_) => EventTag::Window,
Self::Control(_) => EventTag::Control,
}
}
}
#[rustfmt::skip]
impl EventKind {
#[must_use] #[inline(always)]
pub const fn is_none(&self) -> bool { matches![self, EventKind::None] }
#[must_use] #[inline(always)]
pub const fn is_some(&self) -> bool { !matches![self, EventKind::None] }
#[must_use] #[inline(always)]
pub const fn is_key(&self) -> bool { matches![self, EventKind::Key(_)] }
#[must_use] #[inline(always)]
pub const fn is_mouse(&self) -> bool { matches![self, EventKind::Mouse(_)] }
#[must_use] #[inline(always)]
pub const fn is_pointer(&self) -> bool { matches![self, EventKind::Pointer(_)] }
#[must_use] #[inline(always)]
pub const fn is_wheel(&self) -> bool { matches![self, EventKind::Wheel(_)] }
#[must_use] #[inline(always)]
pub const fn is_window(&self) -> bool { matches![self, EventKind::Window(_)] }
#[must_use] #[inline(always)]
pub const fn is_control(&self) -> bool { matches![self, EventKind::Control(_)] }
#[must_use] #[inline(always)]
pub const fn some_key(&self) -> Option<&EventKey> {
if let EventKind::Key(e) = &self { Some(e) } else { None }
}
#[must_use] #[inline(always)]
pub const fn some_mouse(&self) -> Option<&EventMouse> {
if let EventKind::Mouse(e) = &self { Some(e) } else { None }
}
#[must_use] #[inline(always)]
pub const fn some_pointer(&self) -> Option<&EventPointer> {
if let EventKind::Pointer(e) = &self { Some(e) } else { None }
}
#[must_use] #[inline(always)]
pub const fn some_wheel(&self) -> Option<&EventWheel> {
if let EventKind::Wheel(e) = &self { Some(e) } else { None }
}
#[must_use] #[inline(always)]
pub const fn some_window(&self) -> Option<&EventWindow> {
if let EventKind::Window(e) = &self { Some(e) } else { None }
}
#[must_use] #[inline(always)]
pub const fn some_control(&self) -> Option<&AppControl> {
if let EventKind::Control(e) = &self { Some(e) } else { None }
}
}