Skip to content

Commit

Permalink
Added initial touch events.
Browse files Browse the repository at this point in the history
  • Loading branch information
simlay committed Nov 19, 2019
1 parent d9ede0d commit 4bdaf02
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 57 deletions.
3 changes: 2 additions & 1 deletion native/src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::input::{keyboard, mouse};
use crate::input::{keyboard, mouse, touch};

/// A user interface event.
///
Expand All @@ -13,4 +13,5 @@ pub enum Event {

/// A mouse event
Mouse(mouse::Event),
Touch(touch::Touch),
}
1 change: 1 addition & 0 deletions native/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Map your system events into input events that the runtime can understand.
pub mod keyboard;
pub mod mouse;
pub mod touch;

mod button_state;

Expand Down
37 changes: 37 additions & 0 deletions native/src/input/touch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// The touch of a mobile device.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Touch {
Started {
/// The X coordinate of the touch position
x: f32,

/// The Y coordinate of the touch position
y: f32,
},
/// The touch cursor was ended
Ended {
/// The X coordinate of the touch position
x: f32,

/// The Y coordinate of the touch position
y: f32,
},

/// The touch was moved.
Moved {
/// The X coordinate of the touch position
x: f32,

/// The Y coordinate of the touch position
y: f32,
},

/// Some canceled button.
Cancelled {
/// The X coordinate of the touch position
x: f32,

/// The Y coordinate of the touch position
y: f32,
},
}
16 changes: 13 additions & 3 deletions native/src/user_interface.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{input::mouse, layout, Element, Event, Layout, Point, Size};
use crate::{
input::{mouse, touch},
layout, Element, Event, Layout, Point, Size,
};

use std::hash::Hasher;

Expand Down Expand Up @@ -207,8 +210,15 @@ where
let mut messages = Vec::new();

for event in events {
if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event {
self.cursor_position = Point::new(x, y);
match event {
Event::Mouse(mouse::Event::CursorMoved { x, y })
| Event::Touch(touch::Touch::Started { x, y })
| Event::Touch(touch::Touch::Ended { x, y })
| Event::Touch(touch::Touch::Moved { x, y })
| Event::Touch(touch::Touch::Cancelled { x, y }) => {
self.cursor_position = Point::new(x, y);
}
_ => {}
}

self.root.widget.on_event(
Expand Down
36 changes: 19 additions & 17 deletions native/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
//! [`State`]: struct.State.html
//! [`Class`]: enum.Class.html
use crate::input::{mouse, ButtonState};
use crate::input::{mouse, touch::Touch, ButtonState};
use crate::{layout, Element, Event, Hasher, Layout, Point, Widget};
use crate::{layout, Element, Event, Hasher, Layout, Length, Point, Widget};
use std::hash::Hash;

Expand Down Expand Up @@ -48,26 +49,27 @@ where
match event {
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state,
}) => {
state: ButtonState::Pressed,
})
| Event::Touch(Touch::Started { .. }) => {
let bounds = layout.bounds();

self.state.is_pressed = bounds.contains(cursor_position);
}
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state: ButtonState::Released,
})
| Event::Touch(Touch::Ended { .. }) => {
if let Some(on_press) = self.on_press.clone() {
let bounds = layout.bounds();
let is_clicked = self.state.is_pressed
&& bounds.contains(cursor_position);

match state {
ButtonState::Pressed => {
self.state.is_pressed =
bounds.contains(cursor_position);
}
ButtonState::Released => {
let is_clicked = self.state.is_pressed
&& bounds.contains(cursor_position);

self.state.is_pressed = false;
self.state.is_pressed = false;

if is_clicked {
messages.push(on_press);
}
}
if is_clicked {
messages.push(on_press);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions native/src/widget/radio.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Create choices using radio buttons.
use crate::input::{mouse, ButtonState};
use crate::input::{mouse, touch, ButtonState};
use crate::{layout, Element, Event, Hasher, Layout, Length, Point, Widget};

use std::hash::Hash;
Expand Down Expand Up @@ -39,7 +39,8 @@ where
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state: ButtonState::Pressed,
}) => {
})
| Event::Touch(touch::Touch::Started { .. }) => {
if layout.bounds().contains(cursor_position) {
messages.push(self.on_click.clone());
}
Expand Down
38 changes: 21 additions & 17 deletions native/src/widget/scrollable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
column,
input::{mouse, ButtonState},
input::{mouse, touch, ButtonState},
layout, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size,
Widget,
};
Expand Down Expand Up @@ -92,22 +92,26 @@ where
match event {
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state,
}) => match state {
ButtonState::Pressed => {
self.state.scroll_to(
cursor_position.y / (bounds.y + bounds.height),
bounds,
content_bounds,
);

self.state.scrollbar_grabbed_at = Some(cursor_position);
}
ButtonState::Released => {
self.state.scrollbar_grabbed_at = None;
}
},
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
state: ButtonState::Pressed,
})
| Event::Touch(touch::Touch::Started { .. }) => {
self.state.scroll_to(
cursor_position.y / (bounds.y + bounds.height),
bounds,
content_bounds,
);

self.state.scrollbar_grabbed_at = Some(cursor_position);
}
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state: ButtonState::Released,
})
| Event::Touch(touch::Touch::Ended { .. }) => {
self.state.scrollbar_grabbed_at = None;
}
Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(touch::Touch::Moved { .. }) => {
if let Some(scrollbar_grabbed_at) =
self.state.scrollbar_grabbed_at
{
Expand Down
30 changes: 17 additions & 13 deletions native/src/widget/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! [`State`]: struct.State.html
use std::hash::Hash;

use crate::input::{mouse, ButtonState};
use crate::input::{mouse, touch::Touch, ButtonState};
use crate::{layout, Element, Event, Hasher, Layout, Length, Point, Widget};

pub use iced_core::slider::*;
Expand Down Expand Up @@ -58,19 +58,23 @@ where
match event {
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state,
}) => match state {
ButtonState::Pressed => {
if layout.bounds().contains(cursor_position) {
change();
self.state.is_dragging = true;
}
}
ButtonState::Released => {
self.state.is_dragging = false;
state: ButtonState::Pressed,
})
| Event::Touch(Touch::Started { .. }) => {
if layout.bounds().contains(cursor_position) {
change();
self.state.is_dragging = true;
}
},
Event::Mouse(mouse::Event::CursorMoved { .. }) => {
}
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state: ButtonState::Released,
})
| Event::Touch(Touch::Ended { .. }) => {
self.state.is_dragging = false;
}
Event::Mouse(mouse::Event::CursorMoved { .. })
| Event::Touch(Touch::Moved { .. }) => {
if self.state.is_dragging {
change();
}
Expand Down
5 changes: 3 additions & 2 deletions native/src/widget/text_input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
input::{keyboard, mouse, ButtonState},
input::{keyboard, mouse, touch, ButtonState},
layout, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size,
Widget,
};
Expand Down Expand Up @@ -51,7 +51,8 @@ where
Event::Mouse(mouse::Event::Input {
button: mouse::Button::Left,
state: ButtonState::Pressed,
}) => {
})
| Event::Touch(touch::Touch::Started { .. }) => {
self.state.is_focused =
layout.bounds().contains(cursor_position);

Expand Down
5 changes: 4 additions & 1 deletion winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub trait Application: Sized {

log::debug!("Resized: {:?}", new_size);
}
event::WindowEvent::RedrawRequested => {
WindowEvent::RedrawRequested => {
debug.render_started();

if let Some(new_size) = new_size.take() {
Expand Down Expand Up @@ -268,6 +268,9 @@ pub trait Application: Sized {
// TODO: Handle animations!
// Maybe we can use `ControlFlow::WaitUntil` for this.
}
WindowEvent::Touch(touch) => {
events.push(Event::Touch(conversion::touch_event(touch)));
}
_ => {}
},
_ => {
Expand Down
23 changes: 22 additions & 1 deletion winit/src/conversion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::input::{keyboard::KeyCode, mouse, ButtonState};
use crate::input::{keyboard::KeyCode, mouse, touch, ButtonState};
use crate::MouseCursor;

pub fn mouse_cursor(mouse_cursor: MouseCursor) -> winit::window::CursorIcon {
Expand Down Expand Up @@ -198,3 +198,24 @@ pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode {
winit::event::VirtualKeyCode::Cut => KeyCode::Cut,
}
}
pub 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,
},
}
}

0 comments on commit 4bdaf02

Please sign in to comment.