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

Always compute pointer position with current zoom #5380

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all feels like the wrong solution to me.

In egui, everything is in the same global coordinate space called ui points.

If self.latest_post needs dividing with zoom_factor, that's a bug in how latest_pos is set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your eyes. Indeed my actual problem was already solved, I just hadn't looked around enough. Closing this MR and the linked issue now.

}

/// 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
Loading