Skip to content

Commit

Permalink
Always compute pointer position with current zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
timstr committed Nov 16, 2024
1 parent fe368ba commit 4e4c5d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,11 @@ impl State {
window: &Window,
pos_in_pixels: winit::dpi::PhysicalPosition<f64>,
) {
let pixels_per_point = pixels_per_point(&self.egui_ctx, window);
let scale_factor = window.scale_factor() as f32;

let pos_in_points = egui::pos2(
pos_in_pixels.x as f32 / pixels_per_point,
pos_in_pixels.y as f32 / pixels_per_point,
pos_in_pixels.x as f32 / scale_factor,
pos_in_pixels.y as f32 / scale_factor,
);
self.pointer_pos_in_points = Some(pos_in_points);

Expand Down
20 changes: 13 additions & 7 deletions crates/egui/src/input_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,9 @@ pub struct PointerState {
/// Latest known time
time: f64,

/// Latest known zoom factor
zoom_factor: f32,

// Consider a finger tapping a touch screen.
// What position should we report?
// The location of the touch, or `None`, because the finger is gone?
Expand Down Expand Up @@ -876,7 +879,8 @@ pub struct PointerState {
impl Default for PointerState {
fn default() -> Self {
Self {
time: -f64::INFINITY,
time: f64::NEG_INFINITY,
zoom_factor: f32::NEG_INFINITY,
latest_pos: None,
interact_pos: None,
delta: Vec2::ZERO,
Expand Down Expand Up @@ -909,6 +913,7 @@ impl PointerState {
let was_decidedly_dragging = self.is_decidedly_dragging();

self.time = time;
self.zoom_factor = options.zoom_factor;
self.input_options = options.input_options.clone();

self.pointer_events.clear();
Expand Down Expand Up @@ -1051,7 +1056,7 @@ impl PointerState {
/// How much the pointer moved compared to last frame, in points.
#[inline(always)]
pub fn delta(&self) -> Vec2 {
self.delta
self.delta / self.zoom_factor
}

/// How much the mouse moved since the last frame, in unspecified units.
Expand All @@ -1068,7 +1073,7 @@ impl PointerState {
/// but can be ZERO when frame-rate is bad.
#[inline(always)]
pub fn velocity(&self) -> Vec2 {
self.velocity
self.velocity / self.zoom_factor
}

/// Current direction of the pointer.
Expand All @@ -1083,7 +1088,7 @@ impl PointerState {
/// `None` if no mouse button is down.
#[inline(always)]
pub fn press_origin(&self) -> Option<Pos2> {
self.press_origin
self.press_origin.map(|p| p / self.zoom_factor)
}

/// When did the current click/drag originate?
Expand All @@ -1097,13 +1102,13 @@ impl PointerState {
/// When tapping a touch screen, this will be `None`.
#[inline(always)]
pub fn latest_pos(&self) -> Option<Pos2> {
self.latest_pos
self.latest_pos.map(|p| p / self.zoom_factor)
}

/// If it is a good idea to show a tooltip, where is pointer?
#[inline(always)]
pub fn hover_pos(&self) -> Option<Pos2> {
self.latest_pos
self.latest_pos.map(|p| p / self.zoom_factor)
}

/// If you detect a click or drag and wants to know where it happened, use this.
Expand All @@ -1113,7 +1118,7 @@ impl PointerState {
/// When tapping a touch screen, this will be the location of the touch.
#[inline(always)]
pub fn interact_pos(&self) -> Option<Pos2> {
self.interact_pos
self.interact_pos.map(|p| p / self.zoom_factor)
}

/// Do we have a pointer?
Expand Down Expand Up @@ -1422,6 +1427,7 @@ impl PointerState {
pub fn ui(&self, ui: &mut crate::Ui) {
let Self {
time: _,
zoom_factor: _,
latest_pos,
interact_pos,
delta,
Expand Down

0 comments on commit 4e4c5d6

Please sign in to comment.