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

Plot interaction methods #766

Merged
merged 17 commits into from
Nov 13, 2021
Merged

Plot interaction methods #766

merged 17 commits into from
Nov 13, 2021

Conversation

EmbersArc
Copy link
Contributor

Closes #734.

I went with option 2 described in #734. This is a small breaking change and updating shouldn't be difficult. In the long run we can use this to add even more methods for interacting with the plot.

Might be a bit rough around the edges so feel free to suggest improvements.

@emilk
Copy link
Owner

emilk commented Oct 22, 2021

Sorry for the slow review - I'll get to it after the next egui release! 😓

@emilk
Copy link
Owner

emilk commented Oct 25, 2021

I'm a bit worried about Plot being split in two (Plot and PlotUi), and becoming so different from how other widgets work. But then again, Plot is very different from other widgets, so perhaps this is motivated.

I wrote yet another alternative in #734 (comment)

This just seems like a very big change for something as simple as "read point position so I can show it the next frame" (what this PR demos). Is there actually something more complex you want to accomplish?

@emilk
Copy link
Owner

emilk commented Oct 25, 2021

Btw, I've just created a egui discord server where I was hoping we could more easily have design discussions like these - perhaps you care to join it? https://discord.gg/qzvmcrUe

@coderedart
Copy link
Contributor

Btw, I've just created a egui discord server where I was hoping we could more easily have design discussions like these - perhaps you care to join it? https://discord.gg/qzvmcrUe

Finally! I was secretly hoping for a egui discord for a long time. I would recommend that you put this as a badge in readme where it shows how many people are online in discord

@emilk emilk mentioned this pull request Oct 31, 2021
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.

Overall this looks great!

What do you think about using Value instead of Pos2 for the values in plot-coordinates?

We could add helpers to Value (to_pos2 and to_vec2) instead.

Value could also do with a better name, but that's another problem :)

CHANGELOG.md Outdated
@@ -43,6 +44,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
* MSRV (Minimum Supported Rust Version) is now `1.54.0`.
* By default, `DragValue`:s no longer show a tooltip when hovered. Change with `Style::explanation_tooltips`.
* Smaller and nicer color picker.
* Plots now provide a `build` method that has to be used to add items to and show the plot.
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks to my slow review, these lines now need to be moved to new ## Unreleased header! Please also add a PR link (it's something new I'm trying to do).

CHANGELOG.md Outdated
@@ -30,6 +30,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
* Add `ui.add_enabled_ui(bool, |ui| …)` to create a possibly disabled UI section.
* Add feature `"serialize"` separatedly from `"persistence"`.
* Add `egui::widgets::global_dark_light_mode_buttons` to easily add buttons for switching the egui theme.
* Add interaction methods to the plot.
Copy link
Owner

Choose a reason for hiding this comment

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

Can we improve this wording a bit? Maybe "You can now read the Plot coordinates of the mouse when building a Plot." ? 🤷

impl Widget for Plot {
fn ui(self, ui: &mut Ui) -> Response {
/// Interact with and add items to the plot and finally draw it.
pub fn build(self, ui: &mut Ui, build_fn: impl FnOnce(&mut PlotUi)) -> Response {
Copy link
Owner

Choose a reason for hiding this comment

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

For better or for worse, the name show is used for the similar thing in CollapsingHeader, Window etc. I think we should be consistent with that.

egui/src/widgets/plot/mod.rs Show resolved Hide resolved
egui/src/widgets/plot/mod.rs Outdated Show resolved Hide resolved
}

/// Transform the screen coordinates to plot coordinates.
pub fn screen_to_plot_coordinates(&self, position: Pos2) -> Pos2 {
Copy link
Owner

Choose a reason for hiding this comment

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

I prefer x_from_y to y_to_x for a few reasons:

For one, it is more consistent with stlib (let string = String::from_utf8(utf8); etc)

But more importantly, it puts the spaces in sequence:

             V-------------V
let pos_in_plot = plot_ui.plot_from_screen(pos_in_screen);
                                      ^--------------^

vs

             V-----------------------V
let pos_in_plot = plot_ui.screen_to_plot(pos_in_screen);
                            ^---------------------^

}

/// The pointer position in plot coordinates, if the pointer is inside the plot area.
pub fn pointer_coordinate(&self) -> Option<Pos2> {
Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if we should not use Value here instead of Pos2. f32 is not enough accuracy in many situations. For instance, if I am plotting things as a function of time I may be using seconds since unix epoch as the X coordinate, and that won't round very well to f32.

The added type-safety may also be nice (we can see from the type in what space it is in).

@EmbersArc
Copy link
Contributor Author

I had completely missed your review and only just saw it. Sorry about the delay. I agree with all of your suggestions and have made the changes.

@EmbersArc
Copy link
Contributor Author

I tested this today and am pretty happy with how it works. I'd like to add the following features as well though:

  • Access to Ui inside the closure as well. Either through the plot_ui struct or as a second argument.
  • A plot_to_screen conversion, to enable custom drawing in screen coordinates based on items in the plot.
  • A way to get the current plot bounds.

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.

Excellent!

@emilk emilk merged commit 0bad1d0 into emilk:master Nov 13, 2021
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.

Plot: Get mouse position
3 participants