From 672bbc00521897cb8581cb1a180ca9fe19ab7ad1 Mon Sep 17 00:00:00 2001 From: Ilya Averyanov Date: Sat, 6 Mar 2021 02:09:41 +0300 Subject: [PATCH 1/2] Add converting from [f64; 2], (f64, f64), [..] for ImPlotRange #15 --- implot-sys/src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/implot-sys/src/lib.rs b/implot-sys/src/lib.rs index 4055c20..ecb5db7 100644 --- a/implot-sys/src/lib.rs +++ b/implot-sys/src/lib.rs @@ -6,4 +6,55 @@ #[cfg(test)] use imgui_sys as _; +use std::ops::Range; include!("bindings.rs"); + +impl From> for ImPlotRange { + fn from(from: Range) -> Self { + ImPlotRange { + Min: from.start, + Max: from.end, + } + } +} + +impl From<[f64; 2]> for ImPlotRange { + fn from(from: [f64; 2]) -> Self { + ImPlotRange { + Min: from[0], + Max: from[1], + } + } +} + +impl From<(f64, f64)> for ImPlotRange { + fn from(from: (f64, f64)) -> Self { + ImPlotRange { + Min: from.0, + Max: from.1, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_plot_range_from_range() { + let r = 5.0..7.0; + let im_range: ImPlotRange = r.clone().into(); + assert_eq!(im_range.Min, r.start); + assert_eq!(im_range.Max, r.end); + + let arr = [7.0, 8.0]; + let im_range: ImPlotRange = arr.clone().into(); + assert_eq!(im_range.Min, arr[0]); + assert_eq!(im_range.Max, arr[1]); + + let tuple = (12.0, 19.0); + let im_range: ImPlotRange = tuple.clone().into(); + assert_eq!(im_range.Min, tuple.0); + assert_eq!(im_range.Max, tuple.1); + } +} From e53dd6a0dd6d3ee2a7a677e2365909d02f11346c Mon Sep 17 00:00:00 2001 From: Ilya Averyanov Date: Sat, 6 Mar 2021 02:11:42 +0300 Subject: [PATCH 2/2] Use same convention for size as in imgui-rs --- .../examples-shared/src/bar_plots.rs | 4 ++-- .../examples-shared/src/heatmaps.rs | 2 +- .../examples-shared/src/line_plots.rs | 16 ++++++++-------- .../examples-shared/src/scatter_plots.rs | 4 ++-- .../examples-shared/src/stairs_plots.rs | 2 +- .../examples-shared/src/stem_plots.rs | 2 +- .../examples-shared/src/text_plots.rs | 2 +- src/plot.rs | 19 ++++++------------- 8 files changed, 22 insertions(+), 29 deletions(-) diff --git a/implot-examples/examples-shared/src/bar_plots.rs b/implot-examples/examples-shared/src/bar_plots.rs index ce5d35a..6aab4c4 100644 --- a/implot-examples/examples-shared/src/bar_plots.rs +++ b/implot-examples/examples-shared/src/bar_plots.rs @@ -10,7 +10,7 @@ pub fn show_basic_vertical_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Vertical bar plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { // If this is called outside a plot build callback, the program will panic. let axis_positions = vec![0.2, 0.4, 0.6, 0.8]; @@ -27,7 +27,7 @@ pub fn show_basic_horizontal_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Horizontal bar plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { // If this is called outside a plot build callback, the program will panic. let axis_positions = vec![0.2, 0.4, 0.6, 0.8]; diff --git a/implot-examples/examples-shared/src/heatmaps.rs b/implot-examples/examples-shared/src/heatmaps.rs index e539b58..665f0d8 100644 --- a/implot-examples/examples-shared/src/heatmaps.rs +++ b/implot-examples/examples-shared/src/heatmaps.rs @@ -10,7 +10,7 @@ pub fn show_basic_heatmap(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Heatmap plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { let values = (0..100).map(|x| 0.1 * x as f64).collect::>(); PlotHeatmap::new("my favourite heatmap") diff --git a/implot-examples/examples-shared/src/line_plots.rs b/implot-examples/examples-shared/src/line_plots.rs index be744f4..c8db9d9 100644 --- a/implot-examples/examples-shared/src/line_plots.rs +++ b/implot-examples/examples-shared/src/line_plots.rs @@ -19,7 +19,7 @@ pub fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Simple line plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { // If this is called outside a plot build callback, the program will panic. let x_positions = vec![0.1, 0.9]; @@ -36,7 +36,7 @@ pub fn show_two_yaxis_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Multiple Y axis plots") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .with_plot_flags(&(PlotFlags::NONE | PlotFlags::Y_AXIS_2)) .y_limits( &ImPlotRange { Min: 0.0, Max: 1.0 }, @@ -68,7 +68,7 @@ pub fn show_axis_equal_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Axis equal line plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .with_plot_flags(&(PlotFlags::NONE | PlotFlags::AXIS_EQUAL)) .build(plot_ui, || { // If this is called outside a plot build callback, the program will panic. @@ -110,7 +110,7 @@ pub fn show_configurable_plot(ui: &Ui, plot_ui: &PlotUi) { // Axis labels Plot::new("Configured line plot") - .size(x_size, y_size) + .size([x_size, y_size]) .x_label(&x_label) .y_label(&y_label) .x_limits( @@ -161,7 +161,7 @@ pub fn show_query_features_plot(ui: &Ui, plot_ui: &PlotUi) { // Draw a plot Plot::new("Plot querying") - .size(content_width, 300.0) + .size([content_width, 300.0]) .x_limits(&ImPlotRange { Min: 0.0, Max: 5.0 }, Condition::FirstUseEver) .y_limits( &ImPlotRange { Min: 0.0, Max: 5.0 }, @@ -246,7 +246,7 @@ pub fn show_style_plot(ui: &Ui, plot_ui: &PlotUi) { // variables can be done outside of plot calls as well. let style = push_style_color(&PlotColorElement::PlotBg, 1.0, 1.0, 1.0, 0.2); Plot::new("Style demo plot") - .size(content_width, 300.0) + .size([content_width, 300.0]) .x_limits(&ImPlotRange { Min: 0.0, Max: 6.0 }, Condition::Always) .y_limits( &ImPlotRange { @@ -291,7 +291,7 @@ pub fn show_colormaps_plot(ui: &Ui, plot_ui: &PlotUi) { set_colormap_from_preset(Colormap::Plasma, 1); Plot::new("Colormap demo plot") - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { (1..10) .map(|x| x as f64 * 0.1) @@ -318,7 +318,7 @@ pub fn show_colormaps_plot(ui: &Ui, plot_ui: &PlotUi) { ]); Plot::new("Colormap demo plot #2") - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { (1..10) .map(|x| x as f64 * 0.1) diff --git a/implot-examples/examples-shared/src/scatter_plots.rs b/implot-examples/examples-shared/src/scatter_plots.rs index 62528f5..43257f5 100644 --- a/implot-examples/examples-shared/src/scatter_plots.rs +++ b/implot-examples/examples-shared/src/scatter_plots.rs @@ -12,7 +12,7 @@ pub fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Simple scatter plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { // If this is called outside a plot build callback, the program will panic. let x_positions = vec![0.1, 0.2, 0.1, 0.5, 0.9]; @@ -29,7 +29,7 @@ pub fn show_custom_markers_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Multi-marker scatter plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { // Change to cross marker for one scatter plot call let x_positions = vec![0.1, 0.2, 0.1, 0.5, 0.9]; diff --git a/implot-examples/examples-shared/src/stairs_plots.rs b/implot-examples/examples-shared/src/stairs_plots.rs index 215ca81..0fc39d6 100644 --- a/implot-examples/examples-shared/src/stairs_plots.rs +++ b/implot-examples/examples-shared/src/stairs_plots.rs @@ -12,7 +12,7 @@ pub fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Simple stairs plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { // If this is called outside a plot build callback, the program will panic. let x_positions = vec![0.1, 0.2, 0.5]; diff --git a/implot-examples/examples-shared/src/stem_plots.rs b/implot-examples/examples-shared/src/stem_plots.rs index dc99c64..23d9f97 100644 --- a/implot-examples/examples-shared/src/stem_plots.rs +++ b/implot-examples/examples-shared/src/stem_plots.rs @@ -10,7 +10,7 @@ pub fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Stem plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { // If this is called outside a plot build callback, the program will panic. let axis_positions = vec![0.2, 0.4, 0.6, 0.8, 0.9, 0.93]; diff --git a/implot-examples/examples-shared/src/text_plots.rs b/implot-examples/examples-shared/src/text_plots.rs index 5d2c36f..cc3e0d9 100644 --- a/implot-examples/examples-shared/src/text_plots.rs +++ b/implot-examples/examples-shared/src/text_plots.rs @@ -12,7 +12,7 @@ pub fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) { Plot::new("Simple text plot") // The size call could also be omitted, though the defaults don't consider window // width, which is why we're not doing so here. - .size(content_width, 300.0) + .size([content_width, 300.0]) .build(plot_ui, || { // The text passed to "new" is what gets displayed. let x_position: f64 = 0.5; diff --git a/src/plot.rs b/src/plot.rs index 9d787dd..4dd748d 100644 --- a/src/plot.rs +++ b/src/plot.rs @@ -97,10 +97,8 @@ pub struct Plot { /// Title of the plot, shown on top. Stored as ImString because that's what we'll use /// afterwards, and this ensures the ImString itself will stay alive long enough for the plot. title: ImString, - /// Size of the plot in x direction, in the same units imgui uses. - size_x: f32, - /// Size of the plot in y direction, in the same units imgui uses. - size_y: f32, + /// Size of the plot in [x, y] direction, in the same units imgui uses. + size: [f32; 2], /// Label of the x axis, shown on the bottom. Stored as ImString because that's what we'll use /// afterwards, and this ensures the ImString itself will stay alive long enough for the plot. x_label: ImString, @@ -162,8 +160,7 @@ impl Plot { // TODO(4bb4) question these defaults, maybe remove some of them Self { title: im_str!("{}", title), - size_x: DEFAULT_PLOT_SIZE_X, - size_y: DEFAULT_PLOT_SIZE_Y, + size: [DEFAULT_PLOT_SIZE_X, DEFAULT_PLOT_SIZE_Y], x_label: im_str!("").into(), y_label: im_str!("").into(), x_limits: None, @@ -186,9 +183,8 @@ impl Plot { /// Sets the plot size, given as [size_x, size_y]. Units are the same as /// what imgui uses. TODO(4b4) ... which is? I'm not sure it's pixels #[inline] - pub fn size(mut self, size_x: f32, size_y: f32) -> Self { - self.size_x = size_x; - self.size_y = size_y; + pub fn size(mut self, size: [f32; 2]) -> Self { + self.size = size; self } @@ -427,10 +423,7 @@ impl Plot { self.title.as_ptr(), self.x_label.as_ptr(), self.y_label.as_ptr(), - sys::ImVec2 { - x: self.size_x as f32, - y: self.size_y as f32, - }, + self.size.into(), self.plot_flags, self.x_flags, self.y_flags[0],