Touch zoom support
@@ -58,13 +58,14 @@ impl Window
let performance = self.window.performance().ok_or(Error::PerformanceError {message: "Performance (for timing) is not found on the window.".to_string()})?;
let mut last_time = performance.now();
let last_position = Rc::new(RefCell::new(None));
+ let last_zoom = Rc::new(RefCell::new(None));
self.add_mousedown_event_listener(events.clone())?;
self.add_touchstart_event_listener(events.clone(), last_position.clone())?;
self.add_mouseup_event_listener(events.clone())?;
self.add_touchend_event_listener(events.clone(), last_position.clone())?;
self.add_mousemove_event_listener(events.clone())?;
- self.add_touchmove_event_listener(events.clone(), last_position.clone())?;
+ self.add_touchmove_event_listener(events.clone(), last_position.clone(), last_zoom.clone())?;
self.add_mousewheel_event_listener(events.clone())?;
self.add_key_down_event_listener(events.clone())?;
self.add_key_up_event_listener(events.clone())?;
@@ -165,14 +166,31 @@ impl Window
Ok(())
}
- fn add_touchmove_event_listener(&self, events: Rc<RefCell<Vec<Event>>>, last_position: Rc<RefCell<Option<(i32, i32)>>>) -> Result<(), Error>
+ fn add_touchmove_event_listener(&self, events: Rc<RefCell<Vec<Event>>>, last_position: Rc<RefCell<Option<(i32, i32)>>>, last_zoom: Rc<RefCell<Option<f64>>>) -> Result<(), Error>
{
let closure = Closure::wrap(Box::new(move |event: web_sys::TouchEvent| {
+ if event.touches().length() == 1 {
let touch = event.touches().item(0).unwrap();
if let Some((x,y)) = *last_position.borrow() {
(*events).borrow_mut().push(Event::MouseMotion {delta: ((touch.page_x() - x) as f64, (touch.page_y() - y) as f64)});
}
*last_position.borrow_mut() = Some((touch.page_x(), touch.page_y()));
+ *last_zoom.borrow_mut() = None;
+ }
+ else if event.touches().length() == 2 {
+ let touch0 = event.touches().item(0).unwrap();
+ let touch1 = event.touches().item(1).unwrap();
+ let zoom = f64::sqrt(f64::powi((touch0.page_x() - touch1.page_x()) as f64, 2) + f64::powi((touch0.page_y() - touch1.page_y()) as f64, 2));
+ if let Some(old_zoom) = *last_zoom.borrow() {
+ (*events).borrow_mut().push(Event::MouseWheel {delta: zoom - old_zoom});
+ }
+ *last_zoom.borrow_mut() = Some(zoom);
+ *last_position.borrow_mut() = None;
+ }
+ else {
+ *last_zoom.borrow_mut() = None;
+ *last_position.borrow_mut() = None;
+ }
}) as Box<dyn FnMut(_)>);
self.canvas.add_event_listener_with_callback("touchmove", closure.as_ref().unchecked_ref())
.map_err(|e| Error::EventListenerError {message: format!("Unable to add touch move event listener. Error code: {:?}", e)})?;
