Linked context into things, adapted examples.

This commit is contained in:
4bb4 2020-10-11 16:25:48 +02:00
parent 5d4d570507
commit ea960bd30d
7 changed files with 85 additions and 69 deletions

View file

@ -2,18 +2,18 @@
//! features of the libray, see the line_plots example.
use imgui::{im_str, CollapsingHeader, Condition, Ui, Window};
use implot::{Context, Plot, PlotBars};
use implot::{Context, Plot, PlotBars, PlotUi};
mod support;
fn show_basic_vertical_plot(ui: &Ui) {
fn show_basic_vertical_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!("This header shows a simple vertical bar plot."));
let content_width = ui.window_content_region_width();
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)
.build(|| {
.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];
let values = vec![0.1, 0.2, 0.3, 0.4];
@ -23,14 +23,14 @@ fn show_basic_vertical_plot(ui: &Ui) {
});
}
fn show_basic_horizontal_plot(ui: &Ui) {
fn show_basic_horizontal_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!("This header shows a simple horizontal bar plot."));
let content_width = ui.window_content_region_width();
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)
.build(|| {
.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];
let values = vec![0.1, 0.2, 0.3, 0.4];
@ -44,8 +44,11 @@ fn show_basic_horizontal_plot(ui: &Ui) {
fn main() {
let system = support::init(file!());
let mut showing_demo = false;
let _plotcontext = Context::create(); // TODO(4bb4) use this as soon as things have been adapted
let plotcontext = Context::create();
system.main_loop(move |_, ui| {
// The context is moved into the closure after creation so plot_ui is valid.
let plot_ui = plotcontext.get_plot_ui();
Window::new(im_str!("Bar plots example"))
.size([430.0, 450.0], Condition::FirstUseEver)
.build(ui, || {
@ -62,11 +65,11 @@ fn main() {
// Show individual examples in collapsed headers
if CollapsingHeader::new(im_str!("Basic vertical plot")).build(&ui) {
show_basic_vertical_plot(&ui);
show_basic_vertical_plot(&ui, &plot_ui);
}
if CollapsingHeader::new(im_str!("Basic horizontal plot")).build(&ui) {
show_basic_horizontal_plot(&ui);
show_basic_horizontal_plot(&ui, &plot_ui);
}
});

View file

@ -6,12 +6,12 @@ use implot::{
get_plot_limits, get_plot_mouse_position, get_plot_query, is_plot_hovered, is_plot_queried,
push_style_color, push_style_var_f32, push_style_var_i32, set_colormap_from_preset,
set_colormap_from_vec, AxisFlags, Colormap, Context, ImPlotLimits, ImPlotPoint, ImPlotRange,
ImVec4, Marker, Plot, PlotColorElement, PlotFlags, PlotLine, StyleVar,
ImVec4, Marker, Plot, PlotColorElement, PlotFlags, PlotLine, PlotUi, StyleVar,
};
mod support;
fn show_basic_plot(ui: &Ui) {
fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!(
"This header just plots a line with as little code as possible."
));
@ -20,7 +20,7 @@ fn show_basic_plot(ui: &Ui) {
// 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)
.build(|| {
.build(plot_ui, || {
// If this is called outside a plot build callback, the program will panic.
let x_positions = vec![0.1, 0.9];
let y_positions = vec![0.1, 0.9];
@ -28,7 +28,7 @@ fn show_basic_plot(ui: &Ui) {
});
}
fn show_configurable_plot(ui: &Ui) {
fn show_configurable_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!(
"This header demos what we can configure about plots."
));
@ -87,12 +87,12 @@ fn show_configurable_plot(ui: &Ui) {
.with_plot_flags(&plot_flags)
.with_x_axis_flags(&x_axis_flags)
.with_y_axis_flags(&y_axis_flags)
.build(|| {
.build(plot_ui, || {
PlotLine::new("A line").plot(&vec![2.1, 2.9], &vec![1.1, 1.9]);
});
}
fn show_query_features_plot(ui: &Ui) {
fn show_query_features_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!(
"This header demos how to use the querying features."
));
@ -108,7 +108,8 @@ fn show_query_features_plot(ui: &Ui) {
.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 }, Condition::FirstUseEver)
.build(|| {
.with_plot_flags(&(PlotFlags::NONE | PlotFlags::QUERY))
.build(plot_ui, || {
if is_plot_hovered() {
hover_pos = Some(get_plot_mouse_position());
}
@ -133,7 +134,7 @@ fn show_query_features_plot(ui: &Ui) {
}
}
fn show_style_plot(ui: &Ui) {
fn show_style_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!(
"This header demos how to use the styling features."
));
@ -156,7 +157,7 @@ fn show_style_plot(ui: &Ui) {
)
.with_plot_flags(&(PlotFlags::NONE))
.with_y_axis_flags(&(AxisFlags::NONE))
.build(|| {
.build(plot_ui, || {
// Markers can be selected as shown here. The markers are internally represented
// as an u32, hence this calling style.
let markerchoice = push_style_var_i32(&StyleVar::Marker, Marker::Cross as i32);
@ -178,7 +179,7 @@ fn show_style_plot(ui: &Ui) {
style.pop();
}
fn show_colormaps_plot(ui: &Ui) {
fn show_colormaps_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!("This header demos how to select colormaps."));
let content_width = ui.window_content_region_width();
@ -190,7 +191,7 @@ fn show_colormaps_plot(ui: &Ui) {
Plot::new("Colormap demo plot")
.size(content_width, 300.0)
.build(|| {
.build(plot_ui, || {
(1..10)
.map(|x| x as f64 * 0.1)
.map(|x| PlotLine::new(&format!("{:3.3}", x)).plot(&vec![0.1, 0.9], &vec![x, x]))
@ -217,7 +218,7 @@ fn show_colormaps_plot(ui: &Ui) {
Plot::new("Colormap demo plot #2")
.size(content_width, 300.0)
.build(|| {
.build(plot_ui, || {
(1..10)
.map(|x| x as f64 * 0.1)
.map(|x| PlotLine::new(&format!("{:3.3}", x)).plot(&vec![0.1, 0.9], &vec![x, x]))
@ -232,8 +233,11 @@ fn show_colormaps_plot(ui: &Ui) {
fn main() {
let system = support::init(file!());
let mut showing_demo = false;
let _plotcontext = Context::create(); // TODO(4bb4) use this as soon as things have been adapted
let plotcontext = Context::create();
system.main_loop(move |_, ui| {
// The context is moved into the closure after creation so plot_ui is valid.
let plot_ui = plotcontext.get_plot_ui();
Window::new(im_str!("Line plots example"))
.size([430.0, 450.0], Condition::FirstUseEver)
.build(ui, || {
@ -250,19 +254,19 @@ fn main() {
// Show individual examples in collapsed headers
if CollapsingHeader::new(im_str!("Basic lineplot")).build(&ui) {
show_basic_plot(&ui);
show_basic_plot(&ui, &plot_ui);
}
if CollapsingHeader::new(im_str!("Configurable lineplot")).build(&ui) {
show_configurable_plot(&ui);
show_configurable_plot(&ui, &plot_ui);
}
if CollapsingHeader::new(im_str!("Querying a plot")).build(&ui) {
show_query_features_plot(&ui);
show_query_features_plot(&ui, &plot_ui);
}
if CollapsingHeader::new(im_str!("Styling a plot")).build(&ui) {
show_style_plot(&ui);
show_style_plot(&ui, &plot_ui);
}
if CollapsingHeader::new(im_str!("Colormap selection")).build(&ui) {
show_colormaps_plot(&ui);
show_colormaps_plot(&ui, &plot_ui);
}
});

View file

@ -3,12 +3,12 @@
use imgui::{im_str, CollapsingHeader, Condition, Ui, Window};
use implot::{
push_style_var_f32, push_style_var_i32, Context, Marker, Plot, PlotScatter, StyleVar,
push_style_var_f32, push_style_var_i32, Context, Marker, Plot, PlotScatter, PlotUi, StyleVar,
};
mod support;
fn show_basic_plot(ui: &Ui) {
fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!(
"This header just draws a scatter plot with as little code as possible."
));
@ -17,7 +17,7 @@ fn show_basic_plot(ui: &Ui) {
// 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)
.build(|| {
.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];
let y_positions = vec![0.1, 0.1, 0.3, 0.3, 0.9];
@ -25,7 +25,7 @@ fn show_basic_plot(ui: &Ui) {
});
}
fn show_custom_markers_plot(ui: &Ui) {
fn show_custom_markers_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!(
"This header shows how markers can be used in scatter plots."
));
@ -34,7 +34,7 @@ fn show_custom_markers_plot(ui: &Ui) {
// 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)
.build(|| {
.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];
let y_positions = vec![0.1, 0.1, 0.3, 0.3, 0.9];
@ -59,8 +59,11 @@ fn show_custom_markers_plot(ui: &Ui) {
fn main() {
let system = support::init(file!());
let mut showing_demo = false;
let _plotcontext = Context::create(); // TODO(4bb4) use this as soon as things have been adapted
let plotcontext = Context::create();
system.main_loop(move |_, ui| {
// The context is moved into the closure after creation so plot_ui is valid.
let plot_ui = plotcontext.get_plot_ui();
Window::new(im_str!("Scatter plots example"))
.size([430.0, 450.0], Condition::FirstUseEver)
.build(ui, || {
@ -77,11 +80,11 @@ fn main() {
// Show individual examples in collapsed headers
if CollapsingHeader::new(im_str!("Basic scatter plot")).build(&ui) {
show_basic_plot(&ui);
show_basic_plot(&ui, &plot_ui);
}
if CollapsingHeader::new(im_str!("Custom markers")).build(&ui) {
show_custom_markers_plot(&ui);
show_custom_markers_plot(&ui, &plot_ui);
}
});

View file

@ -2,11 +2,11 @@
//! features of the libray, see the line_plots example.
use imgui::{im_str, CollapsingHeader, Condition, Ui, Window};
use implot::{Context, Plot, PlotText};
use implot::{Context, Plot, PlotText, PlotUi};
mod support;
fn show_basic_plot(ui: &Ui) {
fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) {
ui.text(im_str!(
"This header just plots some text with as little code as possible."
));
@ -15,7 +15,7 @@ fn show_basic_plot(ui: &Ui) {
// 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)
.build(|| {
.build(plot_ui, || {
// The text passed to "new" is what gets displayed.
let x_position: f64 = 0.5;
let y_position: f64 = 0.2;
@ -33,8 +33,11 @@ fn show_basic_plot(ui: &Ui) {
fn main() {
let system = support::init(file!());
let mut showing_demo = false;
let _plotcontext = Context::create(); // TODO(4bb4) use this as soon as things have been adapted
let plotcontext = Context::create();
system.main_loop(move |_, ui| {
// The context is moved into the closure after creation so plot_ui is valid.
let plot_ui = plotcontext.get_plot_ui();
Window::new(im_str!("Text plots example"))
.size([430.0, 450.0], Condition::FirstUseEver)
.build(ui, || {
@ -51,7 +54,7 @@ fn main() {
// Show individual examples in collapsed headers
if CollapsingHeader::new(im_str!("Basic text plot")).build(&ui) {
show_basic_plot(&ui);
show_basic_plot(&ui, &plot_ui);
}
});