diff options
author | 2020-12-17 05:19:14 +0100 | |
---|---|---|
committer | 2020-12-17 05:19:14 +0100 | |
commit | 89e604d1602251f1ec16521fd714d60807217df7 (patch) | |
tree | 64d2f7683e9b465b4e85d8e12a5f01f354654fe1 /winit | |
parent | a42b3c6998274e75fd10f5ff3cecbdb37b9e3895 (diff) | |
parent | 277ae74d6817ee6192b5f7a44de19dcbdf8d58f7 (diff) | |
download | iced-89e604d1602251f1ec16521fd714d60807217df7.tar.gz iced-89e604d1602251f1ec16521fd714d60807217df7.tar.bz2 iced-89e604d1602251f1ec16521fd714d60807217df7.zip |
Merge pull request #57 from simlay/ios-support-wip
Touch support
Diffstat (limited to '')
-rw-r--r-- | winit/src/application/state.rs | 7 | ||||
-rw-r--r-- | winit/src/conversion.rs | 58 |
2 files changed, 54 insertions, 11 deletions
diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs index 58bc7ed6..46297370 100644 --- a/winit/src/application/state.rs +++ b/winit/src/application/state.rs @@ -2,7 +2,7 @@ use crate::conversion; use crate::{Application, Color, Debug, Mode, Point, Size, Viewport}; use std::marker::PhantomData; -use winit::event::WindowEvent; +use winit::event::{Touch, WindowEvent}; use winit::window::Window; /// The state of a windowed [`Application`]. @@ -128,7 +128,10 @@ impl<A: Application> State<A> { self.viewport_version = self.viewport_version.wrapping_add(1); } - WindowEvent::CursorMoved { position, .. } => { + WindowEvent::CursorMoved { position, .. } + | WindowEvent::Touch(Touch { + location: position, .. + }) => { self.cursor_position = *position; } WindowEvent::CursorLeft { .. } => { diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs index 138bc64d..f073c474 100644 --- a/winit/src/conversion.rs +++ b/winit/src/conversion.rs @@ -2,10 +2,11 @@ //! //! [`winit`]: https://github.com/rust-windowing/winit //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -use crate::{ - keyboard::{self, KeyCode, Modifiers}, - mouse, window, Event, Mode, Point, -}; +use crate::keyboard; +use crate::mouse; +use crate::touch; +use crate::window; +use crate::{Event, Mode, Point}; /// Converts a winit window event into an iced event. pub fn window_event( @@ -36,8 +37,7 @@ pub fn window_event( let position = position.to_logical::<f64>(scale_factor); Some(Event::Mouse(mouse::Event::CursorMoved { - x: position.x as f32, - y: position.y as f32, + position: Point::new(position.x as f32, position.y as f32), })) } WindowEvent::CursorEntered { .. } => { @@ -118,6 +118,9 @@ pub fn window_event( WindowEvent::HoveredFileCancelled => { Some(Event::Window(window::Event::FilesHoveredLeft)) } + WindowEvent::Touch(touch) => { + Some(Event::Touch(touch_event(*touch, scale_factor))) + } _ => None, } } @@ -181,8 +184,10 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button { /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -pub fn modifiers(modifiers: winit::event::ModifiersState) -> Modifiers { - Modifiers { +pub fn modifiers( + modifiers: winit::event::ModifiersState, +) -> keyboard::Modifiers { + keyboard::Modifiers { shift: modifiers.shift(), control: modifiers.ctrl(), alt: modifiers.alt(), @@ -200,11 +205,46 @@ pub fn cursor_position( Point::new(logical_position.x, logical_position.y) } +/// 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, + scale_factor: f64, +) -> touch::Event { + let id = touch::Finger(touch.id); + let position = { + let location = touch.location.to_logical::<f64>(scale_factor); + + Point::new(location.x as f32, location.y as f32) + }; + + match touch.phase { + winit::event::TouchPhase::Started => { + touch::Event::FingerPressed { id, position } + } + winit::event::TouchPhase::Moved => { + touch::Event::FingerMoved { id, position } + } + winit::event::TouchPhase::Ended => { + touch::Event::FingerLifted { id, position } + } + winit::event::TouchPhase::Cancelled => { + touch::Event::FingerLost { id, position } + } + } +} + /// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code. /// /// [`winit`]: https://github.com/rust-windowing/winit /// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native -pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode { +pub fn key_code( + virtual_keycode: winit::event::VirtualKeyCode, +) -> keyboard::KeyCode { + use keyboard::KeyCode; + match virtual_keycode { winit::event::VirtualKeyCode::Key1 => KeyCode::Key1, winit::event::VirtualKeyCode::Key2 => KeyCode::Key2, |