Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose touch events in canvas widget #1305

Merged
merged 7 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ members = [
"examples/geometry",
"examples/integration_opengl",
"examples/integration_wgpu",
"examples/multitouch",
"examples/pane_grid",
"examples/pick_list",
"examples/pokedex",
Expand Down
17 changes: 17 additions & 0 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ fn view_controls<'a>(

mod grid {
use crate::Preset;
use iced::touch;
use iced::widget::canvas;
use iced::widget::canvas::event::{self, Event};
use iced::widget::canvas::{
Expand Down Expand Up @@ -423,6 +424,22 @@ mod grid {
};

match event {
Event::Touch(touch_event) => match touch_event {
touch::Event::FingerMoved { .. } => {
let message = {
*interaction = if is_populated {
Interaction::Erasing
} else {
Interaction::Drawing
};

populate.or(unpopulate)
};

(event::Status::Captured, message)
}
_ => (event::Status::Ignored, None),
},
Event::Mouse(mouse_event) => match mouse_event {
mouse::Event::ButtonPressed(button) => {
let message = match button {
Expand Down
12 changes: 12 additions & 0 deletions examples/multitouch/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "multitouch"
version = "0.1.0"
authors = ["Artur Sapek <artur@kraken.com>"]
edition = "2021"
publish = false

[dependencies]
iced = { path = "../..", features = ["canvas", "tokio", "debug"] }
tokio = { version = "1.0", features = ["sync"] }
env_logger = "0.9"
voronoi = "0.1.4"
208 changes: 208 additions & 0 deletions examples/multitouch/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
//! This example shows how to use touch events in `Canvas` to draw
//! a circle around each fingertip. This only works on touch-enabled
//! computers like Microsoft Surface.
use iced::widget::canvas::event;
use iced::widget::canvas::{self, Canvas, Cursor, Geometry, Stroke};
use iced::{
executor, touch, window, Application, Color, Command, Element, Length,
Point, Rectangle, Settings, Subscription, Theme,
};

use std::collections::HashMap;

pub fn main() -> iced::Result {
env_logger::builder().format_timestamp(None).init();

Multitouch::run(Settings {
antialiasing: true,
window: window::Settings {
position: window::Position::Centered,
..window::Settings::default()
},
..Settings::default()
})
}

struct Multitouch {
state: State,
}

#[derive(Debug)]
struct State {
cache: canvas::Cache,
fingers: HashMap<touch::Finger, Point>,
}

impl State {
fn new() -> Self {
Self {
cache: canvas::Cache::new(),
fingers: HashMap::new(),
}
}
}

#[derive(Debug)]
enum Message {
FingerPressed { id: touch::Finger, position: Point },
FingerLifted { id: touch::Finger },
}

impl Application for Multitouch {
type Executor = executor::Default;
type Message = Message;
type Theme = Theme;
type Flags = ();

fn new(_flags: ()) -> (Self, Command<Message>) {
(
Multitouch {
state: State::new(),
},
Command::none(),
)
}

fn title(&self) -> String {
String::from("Multitouch - Iced")
}

fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::FingerPressed { id, position } => {
self.state.fingers.insert(id, position.clone());
self.state.cache.clear();
}
Message::FingerLifted { id } => {
self.state.fingers.remove(&id);
self.state.cache.clear();
}
}

Command::none()
}

fn subscription(&self) -> Subscription<Message> {
Subscription::none()
}

fn view(&self) -> Element<Message> {
Canvas::new(&self.state)
.width(Length::Fill)
.height(Length::Fill)
.into()
}
}

impl<'a> canvas::Program<Message> for State {
type State = ();

fn update(
&self,
_state: &mut Self::State,
event: event::Event,
_bounds: Rectangle,
_cursor: Cursor,
) -> (event::Status, Option<Message>) {
match event {
event::Event::Touch(touch_event) => match touch_event {
touch::Event::FingerPressed { id, position }
| touch::Event::FingerMoved { id, position } => (
event::Status::Captured,
Some(Message::FingerPressed { id, position }),
),
touch::Event::FingerLifted { id, .. }
| touch::Event::FingerLost { id, .. } => (
event::Status::Captured,
Some(Message::FingerLifted { id }),
),
},
_ => (event::Status::Ignored, None),
}
}

fn draw(
&self,
_state: &Self::State,
_theme: &Theme,
bounds: Rectangle,
_cursor: Cursor,
) -> Vec<Geometry> {
let fingerweb = self.cache.draw(bounds.size(), |frame| {
if self.fingers.len() < 2 {
return;
}

// Collect tuples of (id, point);
let mut zones: Vec<(u64, Point)> = self
.fingers
.iter()
.map(|(id, pt)| (id.0, pt.clone()))
.collect();

// Sort by ID
zones.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());

// Generate sorted list of points
let vpoints: Vec<voronoi::Point> =
zones.iter().map(|zone| to_voronoi_point(&zone.1)).collect();

let diagram = voronoi::voronoi(vpoints, 700.0);
let polys = voronoi::make_polygons(&diagram);

for (poly, zone) in polys.iter().zip(zones) {
let mut builder = canvas::path::Builder::new();

for (index, pt) in poly.iter().enumerate() {
let pt = from_voronoi_point(pt);

match index {
0 => builder.move_to(pt),
_ => builder.line_to(pt),
}
}

let path = builder.build();

let color_r = (10 % zone.0) as f32 / 20.0;
let color_g = (10 % (zone.0 + 8)) as f32 / 20.0;
let color_b = (10 % (zone.0 + 3)) as f32 / 20.0;

frame.fill(
&path,
Color {
r: color_r,
g: color_g,
b: color_b,
a: 1.0,
},
);

frame.stroke(
&path,
Stroke {
color: Color::BLACK,
width: 3.0,
..Stroke::default()
},
);
}
});

vec![fingerweb]
}
}

fn to_voronoi_point(pt: &iced::Point) -> voronoi::Point {
voronoi::Point::new(pt.x.into(), pt.y.into())
}

fn from_voronoi_point(pt: &voronoi::Point) -> iced::Point {
let x: f64 = pt.x.into();
let y: f64 = pt.y.into();

Point {
x: x as f32,
y: y as f32,
}
}
3 changes: 3 additions & 0 deletions graphics/src/widget/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ where
iced_native::Event::Mouse(mouse_event) => {
Some(Event::Mouse(mouse_event))
}
iced_native::Event::Touch(touch_event) => {
Some(Event::Touch(touch_event))
}
iced_native::Event::Keyboard(keyboard_event) => {
Some(Event::Keyboard(keyboard_event))
}
Expand Down
4 changes: 4 additions & 0 deletions graphics/src/widget/canvas/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Handle events of a canvas.
use iced_native::keyboard;
use iced_native::mouse;
use iced_native::touch;

pub use iced_native::event::Status;

Expand All @@ -12,6 +13,9 @@ pub enum Event {
/// A mouse event.
Mouse(mouse::Event),

/// A touch event.
Touch(touch::Event),

/// A keyboard event.
Keyboard(keyboard::Event),
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ pub mod mouse;
pub mod overlay;
pub mod settings;
pub mod time;
pub mod touch;
pub mod widget;
pub mod window;

Expand Down
2 changes: 2 additions & 0 deletions src/touch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//! Listen and react to touch events.
pub use crate::runtime::touch::{Event, Finger};