mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 13:50:04 -04:00
refactor: group mouse input into own struct
This commit is contained in:
@@ -45,28 +45,7 @@ pub struct RawInput {
|
||||
/// What emigui maintains
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct GuiInput {
|
||||
// TODO: mouse: Mouse as separate
|
||||
//
|
||||
/// Is the button currently down?
|
||||
/// true the frame when it is pressed,
|
||||
/// false the frame it is released.
|
||||
pub mouse_down: bool,
|
||||
|
||||
/// The mouse went from !down to down
|
||||
pub mouse_pressed: bool,
|
||||
|
||||
/// The mouse went from down to !down
|
||||
pub mouse_released: bool,
|
||||
|
||||
/// Current position of the mouse in points.
|
||||
/// None for touch screens when finger is not down.
|
||||
pub mouse_pos: Option<Pos2>,
|
||||
|
||||
/// How much the mouse moved compared to last frame, in points.
|
||||
pub mouse_move: Vec2,
|
||||
|
||||
/// Current velocity of mouse cursor, if any.
|
||||
pub mouse_velocity: Vec2,
|
||||
pub mouse: MouseInput,
|
||||
|
||||
/// How many pixels the user scrolled
|
||||
pub scroll_delta: Vec2,
|
||||
@@ -99,6 +78,31 @@ pub struct GuiInput {
|
||||
pub web: Option<Web>,
|
||||
}
|
||||
|
||||
/// What emigui maintains
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct MouseInput {
|
||||
/// Is the button currently down?
|
||||
/// true the frame when it is pressed,
|
||||
/// false the frame it is released.
|
||||
pub down: bool,
|
||||
|
||||
/// The mouse went from !down to down
|
||||
pub pressed: bool,
|
||||
|
||||
/// The mouse went from down to !down
|
||||
pub released: bool,
|
||||
|
||||
/// Current position of the mouse in points.
|
||||
/// None for touch screens when finger is not down.
|
||||
pub pos: Option<Pos2>,
|
||||
|
||||
/// How much the mouse moved compared to last frame, in points.
|
||||
pub delta: Vec2,
|
||||
|
||||
/// Current velocity of mouse cursor.
|
||||
pub velocity: Vec2,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Deserialize)]
|
||||
#[serde(default)]
|
||||
pub struct Web {
|
||||
@@ -147,22 +151,9 @@ pub enum Key {
|
||||
|
||||
impl GuiInput {
|
||||
pub fn from_last_and_new(last: &RawInput, new: &RawInput) -> GuiInput {
|
||||
let mouse_move = new
|
||||
.mouse_pos
|
||||
.and_then(|new| last.mouse_pos.map(|last| new - last))
|
||||
.unwrap_or_default();
|
||||
let dt = (new.time - last.time) as f32;
|
||||
let mut mouse_velocity = mouse_move / dt;
|
||||
if !mouse_velocity.is_finite() {
|
||||
mouse_velocity = Vec2::zero();
|
||||
}
|
||||
GuiInput {
|
||||
mouse_down: new.mouse_down && new.mouse_pos.is_some(),
|
||||
mouse_pressed: !last.mouse_down && new.mouse_down,
|
||||
mouse_released: last.mouse_down && !new.mouse_down,
|
||||
mouse_pos: new.mouse_pos,
|
||||
mouse_move,
|
||||
mouse_velocity,
|
||||
mouse: MouseInput::from_last_and_new(last, new),
|
||||
scroll_delta: new.scroll_delta,
|
||||
screen_size: new.screen_size,
|
||||
pixels_per_point: new.pixels_per_point.unwrap_or(1.0),
|
||||
@@ -177,6 +168,28 @@ impl GuiInput {
|
||||
}
|
||||
}
|
||||
|
||||
impl MouseInput {
|
||||
pub fn from_last_and_new(last: &RawInput, new: &RawInput) -> MouseInput {
|
||||
let delta = new
|
||||
.mouse_pos
|
||||
.and_then(|new| last.mouse_pos.map(|last| new - last))
|
||||
.unwrap_or_default();
|
||||
let dt = (new.time - last.time) as f32;
|
||||
let mut velocity = delta / dt;
|
||||
if !velocity.is_finite() {
|
||||
velocity = Vec2::zero();
|
||||
}
|
||||
MouseInput {
|
||||
down: new.mouse_down && new.mouse_pos.is_some(),
|
||||
pressed: !last.mouse_down && new.mouse_down,
|
||||
released: last.mouse_down && !new.mouse_down,
|
||||
pos: new.mouse_pos,
|
||||
delta,
|
||||
velocity,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RawInput {
|
||||
pub fn ui(&self, ui: &mut crate::Ui) {
|
||||
use crate::label;
|
||||
@@ -200,16 +213,11 @@ impl RawInput {
|
||||
impl GuiInput {
|
||||
pub fn ui(&self, ui: &mut crate::Ui) {
|
||||
use crate::label;
|
||||
ui.add(label!("mouse_down: {}", self.mouse_down));
|
||||
ui.add(label!("mouse_pressed: {}", self.mouse_pressed));
|
||||
ui.add(label!("mouse_released: {}", self.mouse_released));
|
||||
ui.add(label!("mouse_pos: {:?}", self.mouse_pos));
|
||||
ui.add(label!("mouse_move: {:?}", self.mouse_move));
|
||||
ui.add(label!(
|
||||
"mouse_velocity: [{:3.0} {:3.0}] points/sec",
|
||||
self.mouse_velocity.x,
|
||||
self.mouse_velocity.y
|
||||
));
|
||||
crate::containers::CollapsingHeader::new("mouse")
|
||||
.default_open(true)
|
||||
.show(ui, |ui| {
|
||||
self.mouse.ui(ui);
|
||||
});
|
||||
ui.add(label!("scroll_delta: {:?}", self.scroll_delta));
|
||||
ui.add(label!("screen_size: {:?}", self.screen_size));
|
||||
ui.add(label!("pixels_per_point: {}", self.pixels_per_point));
|
||||
@@ -223,6 +231,22 @@ impl GuiInput {
|
||||
}
|
||||
}
|
||||
|
||||
impl MouseInput {
|
||||
pub fn ui(&self, ui: &mut crate::Ui) {
|
||||
use crate::label;
|
||||
ui.add(label!("down: {}", self.down));
|
||||
ui.add(label!("pressed: {}", self.pressed));
|
||||
ui.add(label!("released: {}", self.released));
|
||||
ui.add(label!("pos: {:?}", self.pos));
|
||||
ui.add(label!("delta: {:?}", self.delta));
|
||||
ui.add(label!(
|
||||
"velocity: [{:3.0} {:3.0}] points/sec",
|
||||
self.velocity.x,
|
||||
self.velocity.y
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
impl Web {
|
||||
pub fn ui(&self, ui: &mut crate::Ui) {
|
||||
use crate::label;
|
||||
|
||||
Reference in New Issue
Block a user