Turn Touch into a struct and add finger id

This commit is contained in:
Héctor Ramón Jiménez 2020-03-19 12:17:16 +01:00
parent e19a07d400
commit d3572e1b81
13 changed files with 151 additions and 112 deletions

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
input::{keyboard, mouse, touch}, input::{keyboard, mouse, Touch},
window, window,
}; };
@ -9,7 +9,7 @@ use crate::{
/// additional events, feel free to [open an issue] and share your use case!_ /// additional events, feel free to [open an issue] and share your use case!_
/// ///
/// [open an issue]: https://github.com/hecrj/iced/issues /// [open an issue]: https://github.com/hecrj/iced/issues
#[derive(PartialEq, Clone, Debug)] #[derive(Debug, Clone, PartialEq)]
pub enum Event { pub enum Event {
/// A keyboard event /// A keyboard event
Keyboard(keyboard::Event), Keyboard(keyboard::Event),
@ -21,5 +21,5 @@ pub enum Event {
Window(window::Event), Window(window::Event),
/// A touch event /// A touch event
Touch(touch::Touch), Touch(Touch),
} }

View File

@ -6,3 +6,4 @@ pub mod touch;
mod button_state; mod button_state;
pub use button_state::ButtonState; pub use button_state::ButtonState;
pub use touch::Touch;

View File

@ -1,5 +1,5 @@
use super::Button; use super::Button;
use crate::input::ButtonState; use crate::{input::ButtonState, Point};
/// A mouse event. /// A mouse event.
/// ///
@ -17,11 +17,8 @@ pub enum Event {
/// The mouse cursor was moved /// The mouse cursor was moved
CursorMoved { CursorMoved {
/// The X coordinate of the mouse position /// The new position of the mouse cursor
x: f32, position: Point,
/// The Y coordinate of the mouse position
y: f32,
}, },
/// A mouse button was pressed or released. /// A mouse button was pressed or released.

View File

@ -1,39 +1,36 @@
//! Build touch events. //! Build touch events.
/// The touch of a mobile device.
use crate::Point;
/// A touch interaction.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum Touch { pub struct Touch {
/// The touch cursor was started /// The finger of the touch.
Started { pub finger: Finger,
/// The X coordinate of the touch position
x: f32,
/// The Y coordinate of the touch position /// The position of the touch.
y: f32, pub position: Point,
},
/// The touch cursor was ended
Ended {
/// The X coordinate of the touch position
x: f32,
/// The Y coordinate of the touch position /// The state of the touch.
y: f32, pub phase: Phase,
}, }
/// The touch was moved. /// A unique identifier representing a finger on a touch interaction.
Moved { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
/// The X coordinate of the touch position pub struct Finger(pub u64);
x: f32,
/// The state of a touch interaction.
/// The Y coordinate of the touch position #[derive(Debug, Clone, Copy, PartialEq, Eq)]
y: f32, pub enum Phase {
}, /// A touch interaction was started.
Started,
/// Some canceled button.
Cancelled { /// An on-going touch interaction was moved.
/// The X coordinate of the touch position Moved,
x: f32,
/// A touch interaction was ended.
/// The Y coordinate of the touch position Ended,
y: f32,
}, /// A touch interaction was canceled.
Canceled,
} }

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
input::{mouse, touch}, input::{mouse, Touch},
layout, Clipboard, Element, Event, Layout, Point, Size, layout, Clipboard, Element, Event, Layout, Point, Size,
}; };
@ -183,12 +183,9 @@ where
for event in events { for event in events {
match event { match event {
Event::Mouse(mouse::Event::CursorMoved { x, y }) Event::Mouse(mouse::Event::CursorMoved { position })
| Event::Touch(touch::Touch::Started { x, y }) | Event::Touch(Touch { position, .. }) => {
| Event::Touch(touch::Touch::Ended { x, y }) self.cursor_position = position;
| Event::Touch(touch::Touch::Moved { x, y })
| Event::Touch(touch::Touch::Cancelled { x, y }) => {
self.cursor_position = Point::new(x, y);
} }
_ => {} _ => {}
} }

View File

@ -5,7 +5,7 @@
//! [`Button`]: struct.Button.html //! [`Button`]: struct.Button.html
//! [`State`]: struct.State.html //! [`State`]: struct.State.html
use crate::{ use crate::{
input::{mouse, touch::Touch, ButtonState}, input::{mouse, touch, ButtonState, Touch},
layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, layout, Clipboard, Element, Event, Hasher, Layout, Length, Point,
Rectangle, Widget, Rectangle, Widget,
}; };
@ -189,16 +189,24 @@ where
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Pressed, state: ButtonState::Pressed,
}) })
| Event::Touch(Touch::Started { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Started,
..
}) => {
if self.on_press.is_some() {
let bounds = layout.bounds(); let bounds = layout.bounds();
self.state.is_pressed = bounds.contains(cursor_position); self.state.is_pressed = bounds.contains(cursor_position);
} }
}
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Released, state: ButtonState::Released,
}) })
| Event::Touch(Touch::Ended { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Ended,
..
}) => {
if let Some(on_press) = self.on_press.clone() { if let Some(on_press) = self.on_press.clone() {
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_clicked = self.state.is_pressed let is_clicked = self.state.is_pressed
@ -211,6 +219,12 @@ where
} }
} }
} }
Event::Touch(Touch {
phase: touch::Phase::Canceled,
..
}) => {
self.state.is_pressed = false;
}
_ => {} _ => {}
} }
} }

View File

@ -2,7 +2,7 @@
use std::hash::Hash; use std::hash::Hash;
use crate::{ use crate::{
input::{mouse, touch::Touch, ButtonState}, input::{mouse, touch, ButtonState, Touch},
layout, row, text, Align, Clipboard, Element, Event, Font, Hasher, layout, row, text, Align, Clipboard, Element, Event, Font, Hasher,
HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text,
VerticalAlignment, Widget, VerticalAlignment, Widget,
@ -156,7 +156,10 @@ where
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Pressed, state: ButtonState::Pressed,
}) })
| Event::Touch(Touch::Started { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Started,
..
}) => {
let mouse_over = layout.bounds().contains(cursor_position); let mouse_over = layout.bounds().contains(cursor_position);
if mouse_over { if mouse_over {

View File

@ -1,6 +1,6 @@
//! Create choices using radio buttons. //! Create choices using radio buttons.
use crate::{ use crate::{
input::{mouse, touch, ButtonState}, input::{mouse, touch, ButtonState, Touch},
layout, row, text, Align, Clipboard, Element, Event, Font, Hasher, layout, row, text, Align, Clipboard, Element, Event, Font, Hasher,
HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text, HorizontalAlignment, Layout, Length, Point, Rectangle, Row, Text,
VerticalAlignment, Widget, VerticalAlignment, Widget,
@ -122,7 +122,10 @@ where
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Pressed, state: ButtonState::Pressed,
}) })
| Event::Touch(touch::Touch::Started { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Started,
..
}) => {
if layout.bounds().contains(cursor_position) { if layout.bounds().contains(cursor_position) {
messages.push(self.on_click.clone()); messages.push(self.on_click.clone());
} }

View File

@ -1,7 +1,7 @@
//! Navigate an endless amount of content with a scrollbar. //! Navigate an endless amount of content with a scrollbar.
use crate::{ use crate::{
column, column,
input::{mouse, touch, ButtonState}, input::{mouse, touch, ButtonState, Touch},
layout, Align, Clipboard, Column, Element, Event, Hasher, Layout, Length, layout, Align, Clipboard, Column, Element, Event, Hasher, Layout, Length,
Point, Rectangle, Size, Widget, Point, Rectangle, Size, Widget,
}; };
@ -175,22 +175,26 @@ where
} }
} }
} }
Event::Touch(touch::Touch::Started { .. }) => { Event::Touch(Touch { phase, .. }) => match phase {
self.state.scroll_box_touched_at = Some(cursor_position); touch::Phase::Started => {
self.state.scroll_box_touched_at =
Some(cursor_position);
} }
Event::Touch(touch::Touch::Moved { .. }) => { touch::Phase::Moved => {
if let Some(scroll_box_touched_at) = if let Some(scroll_box_touched_at) =
self.state.scroll_box_touched_at self.state.scroll_box_touched_at
{ {
let delta = cursor_position.y - scroll_box_touched_at.y; let delta =
cursor_position.y - scroll_box_touched_at.y;
self.state.scroll(delta, bounds, content_bounds); self.state.scroll(delta, bounds, content_bounds);
self.state.scroll_box_touched_at = self.state.scroll_box_touched_at =
Some(cursor_position); Some(cursor_position);
} }
} }
Event::Touch(touch::Touch::Ended { .. }) => { touch::Phase::Ended | touch::Phase::Canceled => {
self.state.scroll_box_touched_at = None; self.state.scroll_box_touched_at = None;
} }
},
_ => {} _ => {}
} }
} }
@ -208,14 +212,24 @@ where
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Released, state: ButtonState::Released,
}) })
| Event::Touch(touch::Touch::Ended { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Ended,
..
})
| Event::Touch(Touch {
phase: touch::Phase::Canceled,
..
}) => {
self.state.scroller_grabbed_at = None; self.state.scroller_grabbed_at = None;
} }
Event::Mouse(mouse::Event::Input { Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Pressed, state: ButtonState::Pressed,
}) })
| Event::Touch(touch::Touch::Started { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Started,
..
}) => {
self.state.scroll_to( self.state.scroll_to(
cursor_position.y / (bounds.y + bounds.height), cursor_position.y / (bounds.y + bounds.height),
bounds, bounds,
@ -223,7 +237,10 @@ where
); );
} }
Event::Mouse(mouse::Event::CursorMoved { .. }) Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Touch::Moved { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Moved,
..
}) => {
if let (Some(scrollbar), Some(scroller_grabbed_at)) = if let (Some(scrollbar), Some(scroller_grabbed_at)) =
(scrollbar, self.state.scroller_grabbed_at) (scrollbar, self.state.scroller_grabbed_at)
{ {
@ -245,7 +262,10 @@ where
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Pressed, state: ButtonState::Pressed,
}) })
| Event::Touch(touch::Touch::Started { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Started,
..
}) => {
if let Some(scrollbar) = scrollbar { if let Some(scrollbar) = scrollbar {
if let Some(scroller_grabbed_at) = if let Some(scroller_grabbed_at) =
scrollbar.grab_scroller(cursor_position) scrollbar.grab_scroller(cursor_position)

View File

@ -5,7 +5,7 @@
//! [`Slider`]: struct.Slider.html //! [`Slider`]: struct.Slider.html
//! [`State`]: struct.State.html //! [`State`]: struct.State.html
use crate::{ use crate::{
input::{mouse, touch::Touch, ButtonState}, input::{mouse, touch, ButtonState, Touch},
layout, Clipboard, Element, Event, Hasher, Layout, Length, Point, layout, Clipboard, Element, Event, Hasher, Layout, Length, Point,
Rectangle, Size, Widget, Rectangle, Size, Widget,
}; };
@ -168,7 +168,10 @@ where
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Pressed, state: ButtonState::Pressed,
}) })
| Event::Touch(Touch::Started { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Started,
..
}) => {
if layout.bounds().contains(cursor_position) { if layout.bounds().contains(cursor_position) {
change(); change();
self.state.is_dragging = true; self.state.is_dragging = true;
@ -178,11 +181,17 @@ where
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Released, state: ButtonState::Released,
}) })
| Event::Touch(Touch::Ended { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Ended,
..
}) => {
self.state.is_dragging = false; self.state.is_dragging = false;
} }
Event::Mouse(mouse::Event::CursorMoved { .. }) Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(Touch::Moved { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Moved,
..
}) => {
if self.state.is_dragging { if self.state.is_dragging {
change(); change();
} }

View File

@ -5,7 +5,7 @@
//! [`TextInput`]: struct.TextInput.html //! [`TextInput`]: struct.TextInput.html
//! [`State`]: struct.State.html //! [`State`]: struct.State.html
use crate::{ use crate::{
input::{keyboard, mouse, touch, ButtonState}, input::{keyboard, mouse, touch, ButtonState, Touch},
layout, Clipboard, Element, Event, Font, Hasher, Layout, Length, Point, layout, Clipboard, Element, Event, Font, Hasher, Layout, Length, Point,
Rectangle, Size, Widget, Rectangle, Size, Widget,
}; };
@ -203,7 +203,10 @@ where
button: mouse::Button::Left, button: mouse::Button::Left,
state: ButtonState::Pressed, state: ButtonState::Pressed,
}) })
| Event::Touch(touch::Touch::Started { .. }) => { | Event::Touch(Touch {
phase: touch::Phase::Started,
..
}) => {
let is_clicked = layout.bounds().contains(cursor_position); let is_clicked = layout.bounds().contains(cursor_position);
if is_clicked { if is_clicked {

View File

@ -19,7 +19,7 @@ wgpu_glyph = "0.7"
glyph_brush = "0.6" glyph_brush = "0.6"
raw-window-handle = "0.3" raw-window-handle = "0.3"
glam = "0.8" glam = "0.8"
font-kit = "0.5.0" font-kit = "0.5"
log = "0.4" log = "0.4"
guillotiere = "0.4" guillotiere = "0.4"

View File

@ -5,9 +5,9 @@
use crate::{ use crate::{
input::{ input::{
keyboard::{self, KeyCode, ModifiersState}, keyboard::{self, KeyCode, ModifiersState},
mouse, touch, ButtonState, mouse, touch, ButtonState, Touch,
}, },
window, Event, Mode, MouseCursor, window, Event, Mode, MouseCursor, Point,
}; };
/// Converts a winit window event into an iced event. /// Converts a winit window event into an iced event.
@ -31,8 +31,7 @@ pub fn window_event(
let position = position.to_logical::<f64>(scale_factor); let position = position.to_logical::<f64>(scale_factor);
Some(Event::Mouse(mouse::Event::CursorMoved { Some(Event::Mouse(mouse::Event::CursorMoved {
x: position.x as f32, position: Point::new(position.x as f32, position.y as f32),
y: position.y as f32,
})) }))
} }
WindowEvent::MouseInput { button, state, .. } => { WindowEvent::MouseInput { button, state, .. } => {
@ -84,9 +83,7 @@ pub fn window_event(
WindowEvent::HoveredFileCancelled => { WindowEvent::HoveredFileCancelled => {
Some(Event::Window(window::Event::FilesHoveredLeft)) Some(Event::Window(window::Event::FilesHoveredLeft))
} }
WindowEvent::Touch(touch) => { WindowEvent::Touch(touch) => Some(Event::Touch(touch_event(touch))),
Some(Event::Touch(touch_event(touch)))
}
_ => None, _ => None,
} }
} }
@ -162,6 +159,25 @@ pub fn modifiers_state(
} }
} }
/// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
pub fn touch_event(touch: winit::event::Touch) -> Touch {
let phase = match touch.phase {
winit::event::TouchPhase::Started => touch::Phase::Started,
winit::event::TouchPhase::Moved => touch::Phase::Moved,
winit::event::TouchPhase::Ended => touch::Phase::Ended,
winit::event::TouchPhase::Cancelled => touch::Phase::Canceled,
};
Touch {
finger: touch::Finger(touch.id),
position: Point::new(touch.location.x as f32, touch.location.y as f32),
phase,
}
}
/// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code.
/// ///
/// [`winit`]: https://github.com/rust-windowing/winit /// [`winit`]: https://github.com/rust-windowing/winit
@ -345,24 +361,3 @@ pub(crate) fn is_private_use_character(c: char) -> bool {
_ => false, _ => false,
} }
} }
pub(crate) fn touch_event(touch: winit::event::Touch) -> touch::Touch {
let location = touch.location;
match touch.phase {
winit::event::TouchPhase::Started => touch::Touch::Started {
x: location.x as f32,
y: location.y as f32,
},
winit::event::TouchPhase::Ended => touch::Touch::Ended {
x: location.x as f32,
y: location.y as f32,
},
winit::event::TouchPhase::Moved => touch::Touch::Moved {
x: location.x as f32,
y: location.y as f32,
},
winit::event::TouchPhase::Cancelled => touch::Touch::Cancelled {
x: location.x as f32,
y: location.y as f32,
},
}
}