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

Feature request: Getting window position from pixel position #366

Open
TrolledWoods opened this issue Jun 20, 2023 · 1 comment · May be fixed by #375
Open

Feature request: Getting window position from pixel position #366

TrolledWoods opened this issue Jun 20, 2023 · 1 comment · May be fixed by #375
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@TrolledWoods
Copy link

I'm sure this usecase is very esoteric, but I'm using egui with pixels and I wanted to put egui objects on the same screen location as objects I'm drawing on the pixel buffer, but to do that I need some way of going from pixel positions to window positions, and surprisingly there doesn't seem to be a way right now, even though there is a way to go from window position to pixel position via window_pos_to_pixel. Even without a crazy usecase like that, there are probably other scenarios where this would be something you want.

@parasyte parasyte added enhancement New feature or request good first issue Good for newcomers labels Jun 20, 2023
@parasyte
Copy link
Owner

Sounds like a reasonable request. The coordinate transformation depends on the scaling matrix, which is an internal implementation detail. (Technically the matrix can be derived from the clipping rectangle, but that's not recommended or intentional.)

For anyone who wishes to implement this feature right away, the window_pos_to_pixel method is the right starting point. The pos vector just needs to be computed slightly differently, and everything else can be the same. In fact, it might be a good idea to reuse most of the code for both cases (if it doesn't require too much accidental complexity).

pixels/src/lib.rs

Lines 573 to 606 in 39e84aa

pub fn window_pos_to_pixel(
&self,
physical_position: (f32, f32),
) -> Result<(usize, usize), (isize, isize)> {
let physical_width = self.surface_size.width as f32;
let physical_height = self.surface_size.height as f32;
let pixels_width = self.context.texture_extent.width as f32;
let pixels_height = self.context.texture_extent.height as f32;
let pos = ultraviolet::Vec4::new(
(physical_position.0 / physical_width - 0.5) * pixels_width,
(physical_position.1 / physical_height - 0.5) * pixels_height,
0.0,
1.0,
);
let pos = self.scaling_matrix_inverse * pos;
let offset_width = pixels_width.min(physical_width) / 2.0;
let offset_height = pixels_height.min(physical_height) / 2.0;
let pixel_x = (pos.x / pos.w + offset_width).floor() as isize;
let pixel_y = (pos.y / pos.w + offset_height).floor() as isize;
if pixel_x < 0
|| pixel_x >= self.context.texture_extent.width as isize
|| pixel_y < 0
|| pixel_y >= self.context.texture_extent.height as isize
{
Err((pixel_x, pixel_y))
} else {
Ok((pixel_x as usize, pixel_y as usize))
}
}

@omprakaash omprakaash linked a pull request Oct 1, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants