8000 Modified scene.show to calculate the mouse position inside the scene, and created an example scene_circle_painter to demonstrate a usecase by Stretchyfish · Pull Request #7149 · emilk/egui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Modified scene.show to calculate the mouse position inside the scene, and created an example scene_circle_painter to demonstrate a usecase #7149

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Stretchyfish
Copy link

Features/changes added

I have modified egui/crates/egui/src/containers/scene.rs, to now also calculate the mouse position inside the scene, dependent on both translation and scale. This has been accomplished by having scene.show also have a mutable borrow of a mouse_position as input, and using the to_global variable inside the show function to calculate the scene mouse position.

Along with this, I have created an example called scene_circle_placer to demonstrate a use case, and have fixed errors that appeared in crates/egui_demo_lib/src/demo/scene.rs due to the change.

Reason

Currently, there is no intuitive way of getting the position of the mouse inside of the scene. It has been possible for me to find the position using the scene response, but the position is not correct when hovering over other interactive elements, such as buttons, making it difficult to create for example dragging behavior of drawn elements.

Issues fixed with this PR

#7060

Note

Due to the nature of the mouse position, I have not been able to find a way to avoid using unwrap, however, I have been very careful not to create any unaccounted situations.
Cargo fmt and cargo clippy has been run successfully.
The command UPDATE_SNAPSHOTS=true cargo test --workspace --all-features was run successfully, and this was the output from ./scripts/check.sh

+ export 'RUSTFLAGS=-D warnings'
+ RUSTFLAGS='-D warnings'
+ export 'RUSTDOCFLAGS=-D warnings'
+ RUSTDOCFLAGS='-D warnings'
+ typos
./scripts/check.sh: line 18: typos: command not found

&mut self.scene_rect,
&mut mouse_position_in_scene,
|ui| {
8000 for circle_pos in &self.circles_in_scene {
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of changing the Scene API, you can get the mouse coordinate with:

mouse_position_in_scene =
    ctx.layer_transform_from_global(ui.painter().layer_id()).unwrap_or_default() *
    ui.input(|i| i.pointer.latest_pos());

If you add a couple of helpers to Painter this could be shortend to

mouse_position_in_scene =
    ui.painter().layer_transform_from_global() * ui.input(|i| i.pointer.latest_pos());

Copy link
Owner
@emilk emilk left a comment

Choose a reason for hiding this comment

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

I appreciate the demo, but we can make the API simpler and more versatile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0