use crate::emath::*;
#[derive(Clone, Debug)]
pub struct RawInput {
pub scroll_delta: Vec2,
pub zoom_delta: f32,
#[deprecated = "Use instead: `screen_rect: Some(Rect::from_pos_size(Default::default(), screen_size))`"]
pub screen_size: Vec2,
pub screen_rect: Option<Rect>,
pub pixels_per_point: Option<f32>,
pub time: Option<f64>,
pub predicted_dt: f32,
pub modifiers: Modifiers,
pub events: Vec<Event>,
}
impl Default for RawInput {
fn default() -> Self {
#![allow(deprecated)] Self {
scroll_delta: Vec2::ZERO,
zoom_delta: 1.0,
screen_size: Default::default(),
screen_rect: None,
pixels_per_point: None,
time: None,
predicted_dt: 1.0 / 60.0,
modifiers: Modifiers::default(),
events: vec![],
}
}
}
impl RawInput {
pub fn take(&mut self) -> RawInput {
#![allow(deprecated)] let zoom = self.zoom_delta;
self.zoom_delta = 1.0;
RawInput {
scroll_delta: std::mem::take(&mut self.scroll_delta),
zoom_delta: zoom,
screen_size: self.screen_size,
screen_rect: self.screen_rect.take(),
pixels_per_point: self.pixels_per_point.take(),
time: self.time.take(),
predicted_dt: self.predicted_dt,
modifiers: self.modifiers,
events: std::mem::take(&mut self.events),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Event {
Copy,
Cut,
Text(String),
Key {
key: Key,
pressed: bool,
modifiers: Modifiers,
},
PointerMoved(Pos2),
PointerButton {
pos: Pos2,
button: PointerButton,
pressed: bool,
modifiers: Modifiers,
},
PointerGone,
CompositionStart,
CompositionUpdate(String),
CompositionEnd(String),
Touch {
device_id: TouchDeviceId,
id: TouchId,
phase: TouchPhase,
pos: Pos2,
force: f32,
},
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PointerButton {
Primary = 0,
Secondary = 1,
Middle = 2,
}
pub const NUM_POINTER_BUTTONS: usize = 3;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct Modifiers {
pub alt: bool,
pub ctrl: bool,
pub shift: bool,
pub mac_cmd: bool,
pub command: bool,
}
impl Modifiers {
#[inline(always)]
pub fn is_none(&self) -> bool {
self == &Self::default()
}
#[inline(always)]
pub fn any(&self) -> bool {
!self.is_none()
}
#[inline(always)]
pub fn shift_only(&self) -> bool {
self.shift && !(self.alt || self.command)
}
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
pub enum Key {
ArrowDown,
ArrowLeft,
ArrowRight,
ArrowUp,
Escape,
Tab,
Backspace,
Enter,
Space,
Insert,
Delete,
Home,
End,
PageUp,
PageDown,
Num0,
Num1,
Num2,
Num3,
Num4,
Num5,
Num6,
Num7,
Num8,
Num9,
A, B,
C,
D,
E,
F,
G,
H,
I,
J,
K, L,
M,
N,
O,
P,
Q,
R,
S,
T,
U, V,
W, X,
Y,
Z, }
impl RawInput {
pub fn ui(&self, ui: &mut crate::Ui) {
#![allow(deprecated)] let Self {
scroll_delta,
zoom_delta,
screen_size: _,
screen_rect,
pixels_per_point,
time,
predicted_dt,
modifiers,
events,
} = self;
ui.label(format!("scroll_delta: {:?} points", scroll_delta));
ui.label(format!("zoom_delta: {:.3?} x", zoom_delta));
ui.label(format!("screen_rect: {:?} points", screen_rect));
ui.label(format!("pixels_per_point: {:?}", pixels_per_point))
.on_hover_text(
"Also called HDPI factor.\nNumber of physical pixels per each logical pixel.",
);
if let Some(time) = time {
ui.label(format!("time: {:.3} s", time));
} else {
ui.label("time: None");
}
ui.label(format!("predicted_dt: {:.1} ms", 1e3 * predicted_dt));
ui.label(format!("modifiers: {:#?}", modifiers));
ui.label(format!("events: {:?}", events))
.on_hover_text("key presses etc");
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct TouchDeviceId(pub u64);
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct TouchId(pub u64);
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TouchPhase {
Start,
Move,
End,
Cancel,
}
impl From<u64> for TouchId {
fn from(id: u64) -> Self {
Self(id)
}
}
impl From<i32> for TouchId {
fn from(id: i32) -> Self {
Self(id as u64)
}
}
impl From<u32> for TouchId {
fn from(id: u32) -> Self {
Self(id as u64)
}
}