use embedder_traits::TouchEventResult;
use embedder_traits::TouchId;
use euclid::{Point2D, Scale, Vector2D};
use log::warn;
use webrender_api::units::DevicePixel;
use self::TouchState::*;
const TOUCH_PAN_MIN_SCREEN_PX: f32 = 20.0;
pub struct TouchHandler {
pub state: TouchState,
pub active_touch_points: Vec<TouchPoint>,
}
#[derive(Clone, Copy, Debug)]
pub struct TouchPoint {
pub id: TouchId,
pub point: Point2D<f32, DevicePixel>,
}
impl TouchPoint {
pub fn new(id: TouchId, point: Point2D<f32, DevicePixel>) -> Self {
TouchPoint { id, point }
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TouchState {
Nothing,
WaitingForScript,
DefaultPrevented,
Touching,
Panning,
Pinching,
MultiTouch,
}
#[derive(Clone, Copy, Debug)]
pub enum TouchAction {
Click,
Scroll(Vector2D<f32, DevicePixel>),
Zoom(f32, Vector2D<f32, DevicePixel>),
DispatchEvent,
NoAction,
}
impl Default for TouchHandler {
fn default() -> Self {
Self {
state: Nothing,
active_touch_points: Vec::new(),
}
}
}
impl TouchHandler {
pub fn new() -> Self {
Self::default()
}
pub fn on_touch_down(&mut self, id: TouchId, point: Point2D<f32, DevicePixel>) {
let point = TouchPoint::new(id, point);
self.active_touch_points.push(point);
self.state = match self.state {
Nothing => WaitingForScript,
Touching | Panning => Pinching,
WaitingForScript => WaitingForScript,
DefaultPrevented => DefaultPrevented,
Pinching | MultiTouch => MultiTouch,
};
}
pub fn on_touch_move(&mut self, id: TouchId, point: Point2D<f32, DevicePixel>) -> TouchAction {
let idx = match self.active_touch_points.iter_mut().position(|t| t.id == id) {
Some(i) => i,
None => {
warn!("Got a touchmove event for a non-active touch point");
return TouchAction::NoAction;
}
};
let old_point = self.active_touch_points[idx].point;
let action = match self.state {
Touching => {
let delta = point - old_point;
if delta.x.abs() > TOUCH_PAN_MIN_SCREEN_PX
|| delta.y.abs() > TOUCH_PAN_MIN_SCREEN_PX
{
self.state = Panning;
TouchAction::Scroll(delta)
} else {
TouchAction::NoAction
}
}
Panning => {
let delta = point - old_point;
TouchAction::Scroll(delta)
}
DefaultPrevented => TouchAction::DispatchEvent,
Pinching => {
let (d0, c0) = self.pinch_distance_and_center();
self.active_touch_points[idx].point = point;
let (d1, c1) = self.pinch_distance_and_center();
let magnification = d1 / d0;
let scroll_delta = c1 - c0 * Scale::new(magnification);
TouchAction::Zoom(magnification, scroll_delta)
}
WaitingForScript => TouchAction::NoAction,
MultiTouch => TouchAction::NoAction,
Nothing => unreachable!(),
};
if self.state != Touching && self.state != WaitingForScript {
self.active_touch_points[idx].point = point;
}
action
}
pub fn on_touch_up(&mut self, id: TouchId, _point: Point2D<f32, DevicePixel>) -> TouchAction {
match self.active_touch_points.iter().position(|t| t.id == id) {
Some(i) => {
self.active_touch_points.swap_remove(i);
}
None => {
warn!("Got a touch up event for a non-active touch point");
}
}
match self.state {
Touching => {
self.state = Nothing;
TouchAction::Click
}
Nothing | Panning => {
self.state = Nothing;
TouchAction::NoAction
}
Pinching => {
self.state = Panning;
TouchAction::NoAction
}
WaitingForScript | DefaultPrevented | MultiTouch => {
if self.active_touch_points.is_empty() {
self.state = Nothing;
}
TouchAction::NoAction
}
}
}
pub fn on_touch_cancel(&mut self, id: TouchId, _point: Point2D<f32, DevicePixel>) {
match self.active_touch_points.iter().position(|t| t.id == id) {
Some(i) => {
self.active_touch_points.swap_remove(i);
}
None => {
warn!("Got a touchcancel event for a non-active touch point");
return;
}
}
match self.state {
Nothing => {}
Touching | Panning => {
self.state = Nothing;
}
Pinching => {
self.state = Panning;
}
WaitingForScript | DefaultPrevented | MultiTouch => {
if self.active_touch_points.is_empty() {
self.state = Nothing;
}
}
}
}
pub fn on_event_processed(&mut self, result: TouchEventResult) {
if let WaitingForScript = self.state {
self.state = match result {
TouchEventResult::DefaultPrevented(_, _) => DefaultPrevented,
TouchEventResult::DefaultAllowed(_, _) => match self.touch_count() {
1 => Touching,
2 => Pinching,
_ => MultiTouch,
},
}
}
}
fn touch_count(&self) -> usize {
self.active_touch_points.len()
}
fn pinch_distance_and_center(&self) -> (f32, Point2D<f32, DevicePixel>) {
debug_assert_eq!(self.touch_count(), 2);
let p0 = self.active_touch_points[0].point;
let p1 = self.active_touch_points[1].point;
let center = p0.lerp(p1, 0.5);
let distance = (p0 - p1).length();
(distance, center)
}
}