use crate::{math::*, History};
const MAX_CLICK_DIST: f32 = 6.0;
const MAX_CLICK_DELAY: f64 = 0.3;
#[derive(Clone, Debug, Default)]
pub struct RawInput {
pub mouse_down: bool,
pub mouse_pos: Option<Pos2>,
pub scroll_delta: Vec2,
pub screen_size: Vec2,
pub pixels_per_point: Option<f32>,
pub time: f64,
pub events: Vec<Event>,
}
impl RawInput {
pub fn take(&mut self) -> RawInput {
RawInput {
mouse_down: self.mouse_down,
mouse_pos: self.mouse_pos,
scroll_delta: std::mem::take(&mut self.scroll_delta),
screen_size: self.screen_size,
pixels_per_point: self.pixels_per_point,
time: self.time,
events: std::mem::take(&mut self.events),
}
}
}
#[derive(Clone, Debug, Default)]
pub struct InputState {
pub raw: RawInput,
pub mouse: MouseInput,
pub scroll_delta: Vec2,
pub screen_size: Vec2,
pub pixels_per_point: Option<f32>,
pub time: f64,
pub unstable_dt: f32,
pub predicted_dt: f32,
pub events: Vec<Event>,
}
#[derive(Clone, Debug)]
pub struct MouseInput {
pub down: bool,
pub pressed: bool,
pub released: bool,
pub could_be_click: bool,
pub click: bool,
pub double_click: bool,
pub last_click_time: f64,
pub pos: Option<Pos2>,
pub press_origin: Option<Pos2>,
pub delta: Vec2,
pub velocity: Vec2,
pos_history: History<Pos2>,
}
impl Default for MouseInput {
fn default() -> Self {
Self {
down: false,
pressed: false,
released: false,
could_be_click: false,
click: false,
double_click: false,
last_click_time: std::f64::NEG_INFINITY,
pos: None,
press_origin: None,
delta: Vec2::zero(),
velocity: Vec2::zero(),
pos_history: History::new(1000, 0.1),
}
}
}
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Event {
Copy,
Cut,
Text(String),
Key {
key: Key,
pressed: bool,
},
}
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum Key {
Alt,
Backspace,
Control,
Delete,
Down,
End,
Escape,
Home,
Insert,
Left,
Logo,
PageDown,
PageUp,
Enter,
Right,
Shift,
Tab,
Up,
}
impl InputState {
#[must_use]
pub fn begin_frame(self, new: RawInput) -> InputState {
let mouse = self.mouse.begin_frame(&new);
let unstable_dt = (new.time - self.raw.time) as f32;
InputState {
mouse,
scroll_delta: new.scroll_delta,
screen_size: new.screen_size,
pixels_per_point: new.pixels_per_point.or(self.pixels_per_point),
time: new.time,
unstable_dt,
predicted_dt: 1.0 / 60.0,
events: new.events.clone(),
raw: new,
}
}
pub fn screen_rect(&self) -> Rect {
Rect::from_min_size(pos2(0.0, 0.0), self.screen_size)
}
pub fn wants_repaint(&self) -> bool {
self.mouse.pressed
|| self.mouse.released
|| self.mouse.delta != Vec2::zero()
|| self.scroll_delta != Vec2::zero()
|| !self.events.is_empty()
}
pub fn key_pressed(&self, desired_key: Key) -> bool {
self.events.iter().any(|event| {
matches!(
event,
Event::Key {
key,
pressed: true
} if *key == desired_key
)
})
}
pub fn key_released(&self, desired_key: Key) -> bool {
self.events.iter().any(|event| {
matches!(
event,
Event::Key {
key,
pressed: false
} if *key == desired_key
)
})
}
pub fn pixels_per_point(&self) -> f32 {
self.pixels_per_point.unwrap_or(1.0)
}
pub fn physical_pixel_size(&self) -> f32 {
1.0 / self.pixels_per_point()
}
pub fn aim_radius(&self) -> f32 {
self.physical_pixel_size()
}
}
impl MouseInput {
#[must_use]
pub fn begin_frame(mut self, new: &RawInput) -> MouseInput {
let delta = new
.mouse_pos
.and_then(|new| self.pos.map(|last| new - last))
.unwrap_or_default();
let pressed = !self.down && new.mouse_down;
let released = self.down && !new.mouse_down;
let click = released && self.could_be_click;
let double_click = click && (new.time - self.last_click_time) < MAX_CLICK_DELAY;
let mut press_origin = self.press_origin;
let mut could_be_click = self.could_be_click;
let mut last_click_time = self.last_click_time;
if click {
last_click_time = new.time
}
if pressed {
press_origin = new.mouse_pos;
could_be_click = true;
} else if !self.down || self.pos.is_none() {
press_origin = None;
}
if let (Some(press_origin), Some(mouse_pos)) = (new.mouse_pos, press_origin) {
could_be_click &= press_origin.distance(mouse_pos) < MAX_CLICK_DIST;
} else {
could_be_click = false;
}
if pressed {
self.pos_history.clear();
}
if let Some(mouse_pos) = new.mouse_pos {
self.pos_history.add(new.time, mouse_pos);
} else {
}
self.pos_history.flush(new.time);
let velocity = if self.pos_history.len() >= 3 && self.pos_history.duration() > 0.01 {
self.pos_history.velocity().unwrap_or_default()
} else {
Vec2::default()
};
MouseInput {
down: new.mouse_down && new.mouse_pos.is_some(),
pressed,
released,
could_be_click,
click,
double_click,
last_click_time,
pos: new.mouse_pos,
press_origin,
delta,
velocity,
pos_history: self.pos_history,
}
}
}
impl RawInput {
pub fn ui(&self, ui: &mut crate::Ui) {
let Self {
mouse_down,
mouse_pos,
scroll_delta,
screen_size,
pixels_per_point,
time,
events,
} = self;
ui.label(format!("mouse_down: {}", mouse_down));
ui.label(format!("mouse_pos: {:.1?}", mouse_pos));
ui.label(format!("scroll_delta: {:?} points", scroll_delta));
ui.label(format!("screen_size: {:?} points", screen_size));
ui.label(format!("pixels_per_point: {:?}", pixels_per_point))
.on_hover_text(
"Also called HDPI factor.\nNumber of physical pixels per each logical pixel.",
);
ui.label(format!("time: {:.3} s", time));
ui.label(format!("events: {:?}", events))
.on_hover_text("key presses etc");
}
}
impl InputState {
pub fn ui(&self, ui: &mut crate::Ui) {
let Self {
raw,
mouse,
scroll_delta,
screen_size,
pixels_per_point,
time,
unstable_dt,
predicted_dt,
events,
} = self;
ui.style_mut().body_text_style = crate::paint::TextStyle::Monospace;
ui.collapsing("Raw Input", |ui| raw.ui(ui));
crate::containers::CollapsingHeader::new("mouse")
.default_open(true)
.show(ui, |ui| {
mouse.ui(ui);
});
ui.label(format!("scroll_delta: {:?} points", scroll_delta));
ui.label(format!("screen_size: {:?} points", screen_size));
ui.label(format!(
"{:?} physical pixels for each logical point",
pixels_per_point
));
ui.label(format!("time: {:.3} s", time));
ui.label(format!(
"time since previous frame: {:.1} ms",
1e3 * unstable_dt
));
ui.label(format!("expected dt: {:.1} ms", 1e3 * predicted_dt));
ui.label(format!("events: {:?}", events))
.on_hover_text("key presses etc");
}
}
impl MouseInput {
pub fn ui(&self, ui: &mut crate::Ui) {
let Self {
down,
pressed,
released,
could_be_click,
click,
double_click,
last_click_time,
pos,
press_origin,
delta,
velocity,
pos_history: _,
} = self;
ui.label(format!("down: {}", down));
ui.label(format!("pressed: {}", pressed));
ui.label(format!("released: {}", released));
ui.label(format!("could_be_click: {}", could_be_click));
ui.label(format!("click: {}", click));
ui.label(format!("double_click: {}", double_click));
ui.label(format!("last_click_time: {:.3}", last_click_time));
ui.label(format!("pos: {:?}", pos));
ui.label(format!("press_origin: {:?}", press_origin));
ui.label(format!("delta: {:?}", delta));
ui.label(format!(
"velocity: [{:3.0} {:3.0}] points/sec",
velocity.x, velocity.y
));
}
}