Skip to content

Commit

Permalink
Merge pull request #257 from podusowski/multi_touch_offset
Browse files Browse the repository at this point in the history
Multi touch offset
  • Loading branch information
podusowski authored Feb 21, 2025
2 parents d7b6a9b + cd19240 commit 1ad2980
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file.

## Unreleased

* Fixed zooming on touchscreens so that it follows the center of the touch points.

## 0.34.0

* `egui` updated to 0.31.0
Expand Down
15 changes: 14 additions & 1 deletion walkers/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl Map<'_, '_, '_> {
&& self.zoom_gesture_enabled
{
// Displacement of mouse pointer relative to widget center
let offset = response.hover_pos().map(|p| p - response.rect.center());
let offset = input_offset(ui, response);

let pos = self
.memory
Expand Down Expand Up @@ -493,6 +493,19 @@ fn calculate_meters_per_pixel(latitude: f64, zoom: f64) -> f64 {
pixel_per_meter_equator / latitude_rad.cos()
}

/// Get the offset of the input (either mouse or touch) relative to the center.
fn input_offset(ui: &mut Ui, response: &Response) -> Option<Vec2> {
let mouse_offset = response.hover_pos();
let touch_offset = ui
.input(|input| input.multi_touch())
.map(|multi_touch| multi_touch.center_pos);

// On touch we get both, so make touch the priority.
touch_offset
.or(mouse_offset)
.map(|pos| pos - response.rect.center())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 1ad2980

Please sign in to comment.