Moved files around, have not adapted code yet though.
This commit is contained in:
parent
7b36501ed3
commit
3b702076cc
10 changed files with 1 additions and 1 deletions
22
implot-examples/implot-glium-examples/Cargo.toml
Normal file
22
implot-examples/implot-glium-examples/Cargo.toml
Normal file
|
@ -0,0 +1,22 @@
|
|||
[package]
|
||||
name = "implot-glium-examples"
|
||||
version = "0.0.0"
|
||||
edition = "2018"
|
||||
authors = ["Sandro Merkli", "implot-rs contributors"]
|
||||
description = "implot examples, with backend code from the imgui examples in imgui-examples"
|
||||
homepage = "https://github.com/4bb4/implot-rs"
|
||||
repository = "https://github.com/4bb4/implot-rs"
|
||||
license = "MIT/Apache-2.0"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
clipboard = "0.5"
|
||||
glium = { version = "0.27", default-features = true }
|
||||
image = "0.23"
|
||||
imgui-sys = "0.5.0"
|
||||
imgui = "0.5.0"
|
||||
imgui-glium-renderer = "0.5.0"
|
||||
imgui-winit-support = "0.5.0"
|
||||
|
||||
implot-sys = { path = "../implot-sys" }
|
||||
implot = { path = "../" }
|
80
implot-examples/implot-glium-examples/examples/bar_plots.rs
Normal file
80
implot-examples/implot-glium-examples/examples/bar_plots.rs
Normal file
|
@ -0,0 +1,80 @@
|
|||
//! This example demonstrates how bar plots are to be used. For more general
|
||||
//! features of the libray, see the line_plots example.
|
||||
|
||||
use imgui::{im_str, CollapsingHeader, Condition, Ui, Window};
|
||||
use implot::{Context, Plot, PlotBars, PlotUi};
|
||||
|
||||
mod support;
|
||||
|
||||
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(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];
|
||||
PlotBars::new("legend label")
|
||||
.with_bar_width(0.1)
|
||||
.plot(&axis_positions, &values);
|
||||
});
|
||||
}
|
||||
|
||||
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(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];
|
||||
PlotBars::new("legend label")
|
||||
.with_bar_width(0.05)
|
||||
.with_horizontal_bars()
|
||||
.plot(&axis_positions, &values);
|
||||
});
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let system = support::init(file!());
|
||||
let mut showing_demo = false;
|
||||
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, || {
|
||||
ui.text(im_str!("Hello from implot-rs!"));
|
||||
ui.text_wrapped(im_str!(
|
||||
"The headers here demo the bar plotting features of the library. \
|
||||
Have a look at the example source code to see how they are implemented.\n\
|
||||
Check out the demo from ImPlot itself first \
|
||||
(by enabling the 'Show demo' checkbox) for instructions \
|
||||
on how to interact with ImPlot plots."
|
||||
));
|
||||
|
||||
ui.checkbox(im_str!("Show demo"), &mut showing_demo);
|
||||
|
||||
// Show individual examples in collapsed headers
|
||||
if CollapsingHeader::new(im_str!("Basic vertical plot")).build(&ui) {
|
||||
show_basic_vertical_plot(&ui, &plot_ui);
|
||||
}
|
||||
|
||||
if CollapsingHeader::new(im_str!("Basic horizontal plot")).build(&ui) {
|
||||
show_basic_horizontal_plot(&ui, &plot_ui);
|
||||
}
|
||||
});
|
||||
|
||||
if showing_demo {
|
||||
implot::show_demo_window(&mut showing_demo);
|
||||
}
|
||||
});
|
||||
}
|
321
implot-examples/implot-glium-examples/examples/line_plots.rs
Normal file
321
implot-examples/implot-glium-examples/examples/line_plots.rs
Normal file
|
@ -0,0 +1,321 @@
|
|||
//! This example demonstrates how line plots are to be used, along with some querying features
|
||||
//! that will be applicable to all kinds of plots.
|
||||
|
||||
use imgui::{im_str, CollapsingHeader, Condition, Ui, Window};
|
||||
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, set_plot_y_axis, AxisFlags, Colormap, Context, ImPlotLimits,
|
||||
ImPlotPoint, ImPlotRange, ImVec4, Marker, Plot, PlotColorElement, PlotFlags, PlotLine, PlotUi,
|
||||
StyleVar, YAxisChoice,
|
||||
};
|
||||
|
||||
mod support;
|
||||
|
||||
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."
|
||||
));
|
||||
let content_width = ui.window_content_region_width();
|
||||
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)
|
||||
.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];
|
||||
PlotLine::new("legend label").plot(&x_positions, &y_positions);
|
||||
});
|
||||
}
|
||||
|
||||
fn show_two_yaxis_plot(ui: &Ui, plot_ui: &PlotUi) {
|
||||
ui.text(im_str!(
|
||||
"This header shows how to create a plot with multiple Y axes."
|
||||
));
|
||||
let content_width = ui.window_content_region_width();
|
||||
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)
|
||||
.with_plot_flags(&(PlotFlags::NONE | PlotFlags::Y_AXIS_2))
|
||||
.y_limits(
|
||||
&ImPlotRange { Min: 0.0, Max: 1.0 },
|
||||
YAxisChoice::First,
|
||||
Condition::Always,
|
||||
)
|
||||
.y_limits(
|
||||
&ImPlotRange { Min: 1.0, Max: 3.5 },
|
||||
YAxisChoice::Second,
|
||||
Condition::Always,
|
||||
)
|
||||
.build(plot_ui, || {
|
||||
let x_positions = vec![0.1, 0.9];
|
||||
|
||||
// The first Y axis is the default
|
||||
let y_positions = vec![0.1, 0.9];
|
||||
PlotLine::new("legend label").plot(&x_positions, &y_positions);
|
||||
|
||||
// Now we switch to the second axis for the next call
|
||||
set_plot_y_axis(YAxisChoice::Second);
|
||||
let y_positions = vec![3.3, 1.2];
|
||||
PlotLine::new("legend label two").plot(&x_positions, &y_positions);
|
||||
});
|
||||
}
|
||||
|
||||
fn show_configurable_plot(ui: &Ui, plot_ui: &PlotUi) {
|
||||
ui.text(im_str!(
|
||||
"This header demos what we can configure about plots."
|
||||
));
|
||||
|
||||
// Settings for the plot
|
||||
// - X and Y size in pixels
|
||||
let x_size = 300.0;
|
||||
let y_size = 200.0;
|
||||
// - Strings for the axis labels
|
||||
let x_label = "X label!";
|
||||
let y_label = "Y label!";
|
||||
// - Plot limits
|
||||
let x_min = 2.0;
|
||||
let x_max = 3.0;
|
||||
let y_min = 1.0;
|
||||
let y_max = 2.0;
|
||||
// - Plot flags, see the PlotFlags docs for more info
|
||||
let plot_flags = PlotFlags::NONE;
|
||||
// - Axis flags, see the AxisFlags docs for more info. All flags are bitflags-created,
|
||||
// so they support a bunch of convenient operations, see https://docs.rs/bitflags
|
||||
let x_axis_flags = AxisFlags::NONE;
|
||||
let y_axis_flags = AxisFlags::NONE;
|
||||
|
||||
// - Unlabelled X axis ticks
|
||||
let x_ticks = vec![2.2, 2.5, 2.8];
|
||||
|
||||
// - Labelled Y axis ticks
|
||||
let y_ticks = vec![(1.1, "A".to_owned()), (1.4, "B".to_owned())];
|
||||
|
||||
// Axis labels
|
||||
Plot::new("Configured line plot")
|
||||
.size(x_size, y_size)
|
||||
.x_label(&x_label)
|
||||
.y_label(&y_label)
|
||||
.x_limits(
|
||||
&ImPlotRange {
|
||||
Min: x_min,
|
||||
Max: x_max,
|
||||
},
|
||||
// Always means that the limits stay what we force them to here, even if the user
|
||||
// scrolls or drags in the plot with the mouse. FirstUseEver sets the limits the
|
||||
// first time the plot is drawn, but the user can then modify them and the change
|
||||
// will stick.
|
||||
Condition::Always,
|
||||
)
|
||||
.y_limits(
|
||||
&ImPlotRange {
|
||||
Min: y_min,
|
||||
Max: y_max,
|
||||
},
|
||||
YAxisChoice::First,
|
||||
Condition::Always,
|
||||
)
|
||||
.x_ticks(&x_ticks, false)
|
||||
.y_ticks_with_labels(YAxisChoice::First, &y_ticks, false)
|
||||
// If any of these flag setting calls are omitted, the defaults are used.
|
||||
.with_plot_flags(&plot_flags)
|
||||
.with_x_axis_flags(&x_axis_flags)
|
||||
.with_y_axis_flags(YAxisChoice::First, &y_axis_flags)
|
||||
.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, plot_ui: &PlotUi) {
|
||||
ui.text(im_str!(
|
||||
"This header demos how to use the querying features."
|
||||
));
|
||||
let content_width = ui.window_content_region_width();
|
||||
|
||||
// Create some containers for exfiltrating data from the closure below
|
||||
let mut hover_pos: Option<ImPlotPoint> = None;
|
||||
let mut plot_limits: Option<ImPlotLimits> = None;
|
||||
let mut query_limits: Option<ImPlotLimits> = None;
|
||||
|
||||
// Draw a plot
|
||||
Plot::new("Plot querying")
|
||||
.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 },
|
||||
YAxisChoice::First,
|
||||
Condition::FirstUseEver,
|
||||
)
|
||||
.with_plot_flags(&(PlotFlags::NONE | PlotFlags::QUERY))
|
||||
.build(plot_ui, || {
|
||||
if is_plot_hovered() {
|
||||
hover_pos = Some(get_plot_mouse_position(None));
|
||||
}
|
||||
|
||||
if is_plot_queried() {
|
||||
query_limits = Some(get_plot_query(None));
|
||||
}
|
||||
plot_limits = Some(get_plot_limits(None));
|
||||
});
|
||||
|
||||
// Print some previously-exfiltrated info. This is because calling
|
||||
// things like is_plot_hovered or get_plot_mouse_position() outside
|
||||
// of an actual Plot is not allowed.
|
||||
if let Some(pos) = hover_pos {
|
||||
ui.text(im_str!("hovered at {}, {}", pos.x, pos.y));
|
||||
}
|
||||
if let Some(limits) = plot_limits {
|
||||
ui.text(im_str!("Plot limits are {:#?}", limits));
|
||||
}
|
||||
if let Some(query) = query_limits {
|
||||
ui.text(im_str!("Query limits are {:#?}", query));
|
||||
}
|
||||
}
|
||||
|
||||
fn show_style_plot(ui: &Ui, plot_ui: &PlotUi) {
|
||||
ui.text(im_str!(
|
||||
"This header demos how to use the styling features."
|
||||
));
|
||||
let content_width = ui.window_content_region_width();
|
||||
|
||||
// The style stack works the same as for other imgui things - we can push
|
||||
// things to have them apply, then pop again to undo the change. In implot-rs,
|
||||
// pushing returns a value on which we have to call .pop() later. Pushing
|
||||
// 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)
|
||||
.x_limits(&ImPlotRange { Min: 0.0, Max: 6.0 }, Condition::Always)
|
||||
.y_limits(
|
||||
&ImPlotRange {
|
||||
Min: -1.0,
|
||||
Max: 3.0,
|
||||
},
|
||||
YAxisChoice::First,
|
||||
Condition::Always,
|
||||
)
|
||||
.with_plot_flags(&(PlotFlags::NONE))
|
||||
.with_y_axis_flags(YAxisChoice::First, &(AxisFlags::NONE))
|
||||
.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);
|
||||
PlotLine::new("Left eye").plot(&vec![2.0, 2.0], &vec![2.0, 1.0]);
|
||||
// Calling pop() on the return value of the push above will undo the marker choice.
|
||||
markerchoice.pop();
|
||||
|
||||
// Line weights can be set the same way, along with some other things - see
|
||||
// the docs of StyleVar for more info.
|
||||
let lineweight = push_style_var_f32(&StyleVar::LineWeight, 5.0);
|
||||
PlotLine::new("Right eye").plot(&vec![4.0, 4.0], &vec![2.0, 1.0]);
|
||||
lineweight.pop();
|
||||
|
||||
let x_values = vec![1.0, 2.0, 4.0, 5.0];
|
||||
let y_values = vec![1.0, 0.0, 0.0, 1.0];
|
||||
PlotLine::new("Mouth").plot(&x_values, &y_values);
|
||||
});
|
||||
|
||||
style.pop();
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
// Select a colormap from the presets. The presets are listed in the Colormap enum
|
||||
// and usually have something from 9 to 11 colors in them, with the second number
|
||||
// being the option to resample the colormap to a custom number of colors if picked
|
||||
// higher than 1.
|
||||
set_colormap_from_preset(Colormap::Plasma, 1);
|
||||
|
||||
Plot::new("Colormap demo plot")
|
||||
.size(content_width, 300.0)
|
||||
.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]))
|
||||
.count();
|
||||
});
|
||||
|
||||
// One can also specify a colormap as a vector of RGBA colors. ImPlot uses ImVec4 for this,
|
||||
// so we follow suit. Make sure to set the last number (w in ImVec4) to 1.0 to see anything -
|
||||
// it's the alpha channel.
|
||||
set_colormap_from_vec(vec![
|
||||
ImVec4 {
|
||||
x: 0.9,
|
||||
y: 0.9,
|
||||
z: 0.0,
|
||||
w: 1.0,
|
||||
},
|
||||
ImVec4 {
|
||||
x: 0.0,
|
||||
y: 0.9,
|
||||
z: 0.9,
|
||||
w: 1.0,
|
||||
},
|
||||
]);
|
||||
|
||||
Plot::new("Colormap demo plot #2")
|
||||
.size(content_width, 300.0)
|
||||
.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]))
|
||||
.count();
|
||||
});
|
||||
|
||||
// Colormaps are not pushed, they are simply set, because they don't stack or anything.
|
||||
// We can reset to the default by just setting the "Standard" preset.
|
||||
set_colormap_from_preset(Colormap::Standard, 0);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let system = support::init(file!());
|
||||
let mut showing_demo = false;
|
||||
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, || {
|
||||
ui.text(im_str!("Hello from implot-rs!"));
|
||||
ui.text_wrapped(im_str!(
|
||||
"The headers here demo the line plotting features of the library. \
|
||||
Have a look at the example source code to see how they are implemented.\n\
|
||||
Check out the demo from ImPlot itself first \
|
||||
(by enabling the 'Show demo' checkbox) for instructions \
|
||||
on how to interact with ImPlot plots."
|
||||
));
|
||||
|
||||
ui.checkbox(im_str!("Show demo"), &mut showing_demo);
|
||||
|
||||
// Show individual examples in collapsed headers
|
||||
if CollapsingHeader::new(im_str!("Basic lineplot")).build(&ui) {
|
||||
show_basic_plot(&ui, &plot_ui);
|
||||
}
|
||||
if CollapsingHeader::new(im_str!("Configurable lineplot")).build(&ui) {
|
||||
show_configurable_plot(&ui, &plot_ui);
|
||||
}
|
||||
if CollapsingHeader::new(im_str!("Querying a plot")).build(&ui) {
|
||||
show_query_features_plot(&ui, &plot_ui);
|
||||
}
|
||||
if CollapsingHeader::new(im_str!("Styling a plot")).build(&ui) {
|
||||
show_style_plot(&ui, &plot_ui);
|
||||
}
|
||||
if CollapsingHeader::new(im_str!("Colormap selection")).build(&ui) {
|
||||
show_colormaps_plot(&ui, &plot_ui);
|
||||
}
|
||||
if CollapsingHeader::new(im_str!("Multiple Y Axes")).build(&ui) {
|
||||
show_two_yaxis_plot(&ui, &plot_ui);
|
||||
}
|
||||
});
|
||||
|
||||
if showing_demo {
|
||||
implot::show_demo_window(&mut showing_demo);
|
||||
}
|
||||
});
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
//! This example demonstrates how scatter plots are to be used. For more general
|
||||
//! features of the libray, see the line_plots example.
|
||||
|
||||
use imgui::{im_str, CollapsingHeader, Condition, Ui, Window};
|
||||
use implot::{
|
||||
push_style_var_f32, push_style_var_i32, Context, Marker, Plot, PlotScatter, PlotUi, StyleVar,
|
||||
};
|
||||
|
||||
mod support;
|
||||
|
||||
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."
|
||||
));
|
||||
let content_width = ui.window_content_region_width();
|
||||
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)
|
||||
.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];
|
||||
PlotScatter::new("legend label").plot(&x_positions, &y_positions);
|
||||
});
|
||||
}
|
||||
|
||||
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."
|
||||
));
|
||||
let content_width = ui.window_content_region_width();
|
||||
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)
|
||||
.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];
|
||||
let markerchoice = push_style_var_i32(&StyleVar::Marker, Marker::Cross as i32);
|
||||
PlotScatter::new("legend label 1").plot(&x_positions, &y_positions);
|
||||
markerchoice.pop();
|
||||
|
||||
// One can combine things like marker size and markor choice
|
||||
let x_positions = vec![0.4, 0.1];
|
||||
let y_positions = vec![0.5, 0.3];
|
||||
let marker_choice = push_style_var_i32(&StyleVar::Marker, Marker::Diamond as i32);
|
||||
let marker_size = push_style_var_f32(&StyleVar::MarkerSize, 12.0);
|
||||
PlotScatter::new("legend label 2").plot(&x_positions, &y_positions);
|
||||
|
||||
// TODO(4bb4) check if these have to be in reverse push order. Does not
|
||||
// seem to be the case.
|
||||
marker_size.pop();
|
||||
marker_choice.pop();
|
||||
});
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let system = support::init(file!());
|
||||
let mut showing_demo = false;
|
||||
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, || {
|
||||
ui.text(im_str!("Hello from implot-rs!"));
|
||||
ui.text_wrapped(im_str!(
|
||||
"The headers here demo the scatter plotting features of the library. \
|
||||
Have a look at the example source code to see how they are implemented.\n\
|
||||
Check out the demo from ImPlot itself first \
|
||||
(by enabling the 'Show demo' checkbox) for instructions \
|
||||
on how to interact with ImPlot plots."
|
||||
));
|
||||
|
||||
ui.checkbox(im_str!("Show demo"), &mut showing_demo);
|
||||
|
||||
// Show individual examples in collapsed headers
|
||||
if CollapsingHeader::new(im_str!("Basic scatter plot")).build(&ui) {
|
||||
show_basic_plot(&ui, &plot_ui);
|
||||
}
|
||||
|
||||
if CollapsingHeader::new(im_str!("Custom markers")).build(&ui) {
|
||||
show_custom_markers_plot(&ui, &plot_ui);
|
||||
}
|
||||
});
|
||||
|
||||
if showing_demo {
|
||||
implot::show_demo_window(&mut showing_demo);
|
||||
}
|
||||
});
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
// Taken directly from imgui-rs examples at
|
||||
//
|
||||
// https://github.com/Gekkio/imgui-rs/tree/master/imgui-examples/examples/support
|
||||
//
|
||||
// Not my code. Originally by Joonas Javanainen and the ImGUI-rs contributors
|
||||
use clipboard::{ClipboardContext, ClipboardProvider};
|
||||
use imgui::{ClipboardBackend, ImStr, ImString};
|
||||
|
||||
pub struct ClipboardSupport(ClipboardContext);
|
||||
|
||||
pub fn init() -> Option<ClipboardSupport> {
|
||||
ClipboardContext::new()
|
||||
.ok()
|
||||
.map(|ctx| ClipboardSupport(ctx))
|
||||
}
|
||||
|
||||
impl ClipboardBackend for ClipboardSupport {
|
||||
fn get(&mut self) -> Option<ImString> {
|
||||
self.0.get_contents().ok().map(|text| text.into())
|
||||
}
|
||||
fn set(&mut self, text: &ImStr) {
|
||||
let _ = self.0.set_contents(text.to_str().to_owned());
|
||||
}
|
||||
}
|
133
implot-examples/implot-glium-examples/examples/support/mod.rs
Normal file
133
implot-examples/implot-glium-examples/examples/support/mod.rs
Normal file
|
@ -0,0 +1,133 @@
|
|||
// Taken directly from imgui-rs examples at
|
||||
//
|
||||
// https://github.com/Gekkio/imgui-rs/tree/master/imgui-examples/examples/support
|
||||
//
|
||||
// Not my code. Originally by Joonas Javanainen and the ImGUI-rs contributors
|
||||
use glium::glutin;
|
||||
use glium::glutin::event::{Event, WindowEvent};
|
||||
use glium::glutin::event_loop::{ControlFlow, EventLoop};
|
||||
use glium::glutin::window::WindowBuilder;
|
||||
use glium::{Display, Surface};
|
||||
use imgui::{Context, FontConfig, FontSource, Ui};
|
||||
use imgui_glium_renderer::Renderer;
|
||||
use imgui_winit_support::{HiDpiMode, WinitPlatform};
|
||||
use std::time::Instant;
|
||||
|
||||
mod clipboard;
|
||||
|
||||
pub struct System {
|
||||
pub event_loop: EventLoop<()>,
|
||||
pub display: glium::Display,
|
||||
pub imgui: Context,
|
||||
pub platform: WinitPlatform,
|
||||
pub renderer: Renderer,
|
||||
pub font_size: f32,
|
||||
}
|
||||
|
||||
pub fn init(title: &str) -> System {
|
||||
let title = match title.rfind('/') {
|
||||
Some(idx) => title.split_at(idx + 1).1,
|
||||
None => title,
|
||||
};
|
||||
let event_loop = EventLoop::new();
|
||||
let context = glutin::ContextBuilder::new().with_vsync(true);
|
||||
let builder = WindowBuilder::new()
|
||||
.with_title(title.to_owned())
|
||||
.with_inner_size(glutin::dpi::LogicalSize::new(1024f64, 768f64));
|
||||
let display =
|
||||
Display::new(builder, context, &event_loop).expect("Failed to initialize display");
|
||||
|
||||
let mut imgui = Context::create();
|
||||
imgui.set_ini_filename(None);
|
||||
|
||||
if let Some(backend) = clipboard::init() {
|
||||
imgui.set_clipboard_backend(Box::new(backend));
|
||||
} else {
|
||||
eprintln!("Failed to initialize clipboard");
|
||||
}
|
||||
|
||||
let mut platform = WinitPlatform::init(&mut imgui);
|
||||
{
|
||||
let gl_window = display.gl_window();
|
||||
let window = gl_window.window();
|
||||
platform.attach_window(imgui.io_mut(), &window, HiDpiMode::Rounded);
|
||||
}
|
||||
|
||||
let hidpi_factor = platform.hidpi_factor();
|
||||
let font_size = (13.0 * hidpi_factor) as f32;
|
||||
imgui.fonts().add_font(&[FontSource::DefaultFontData {
|
||||
config: Some(FontConfig {
|
||||
size_pixels: font_size,
|
||||
..FontConfig::default()
|
||||
}),
|
||||
}]);
|
||||
|
||||
imgui.io_mut().font_global_scale = (1.0 / hidpi_factor) as f32;
|
||||
|
||||
let renderer = Renderer::init(&mut imgui, &display).expect("Failed to initialize renderer");
|
||||
|
||||
System {
|
||||
event_loop,
|
||||
display,
|
||||
imgui,
|
||||
platform,
|
||||
renderer,
|
||||
font_size,
|
||||
}
|
||||
}
|
||||
|
||||
impl System {
|
||||
pub fn main_loop<F: FnMut(&mut bool, &mut Ui) + 'static>(self, mut run_ui: F) {
|
||||
let System {
|
||||
event_loop,
|
||||
display,
|
||||
mut imgui,
|
||||
mut platform,
|
||||
mut renderer,
|
||||
..
|
||||
} = self;
|
||||
let mut last_frame = Instant::now();
|
||||
|
||||
event_loop.run(move |event, _, control_flow| match event {
|
||||
Event::NewEvents(_) => {
|
||||
let now = Instant::now();
|
||||
imgui.io_mut().update_delta_time(now - last_frame);
|
||||
last_frame = now;
|
||||
}
|
||||
Event::MainEventsCleared => {
|
||||
let gl_window = display.gl_window();
|
||||
platform
|
||||
.prepare_frame(imgui.io_mut(), &gl_window.window())
|
||||
.expect("Failed to prepare frame");
|
||||
gl_window.window().request_redraw();
|
||||
}
|
||||
Event::RedrawRequested(_) => {
|
||||
let mut ui = imgui.frame();
|
||||
|
||||
let mut run = true;
|
||||
run_ui(&mut run, &mut ui);
|
||||
if !run {
|
||||
*control_flow = ControlFlow::Exit;
|
||||
}
|
||||
|
||||
let gl_window = display.gl_window();
|
||||
let mut target = display.draw();
|
||||
target.clear_color_srgb(1.0, 1.0, 1.0, 1.0);
|
||||
platform.prepare_render(&ui, gl_window.window());
|
||||
let draw_data = ui.render();
|
||||
renderer
|
||||
.render(&mut target, draw_data)
|
||||
.expect("Rendering failed");
|
||||
target.finish().expect("Failed to swap buffers");
|
||||
}
|
||||
Event::WindowEvent {
|
||||
event: WindowEvent::CloseRequested,
|
||||
..
|
||||
} => *control_flow = ControlFlow::Exit,
|
||||
event => {
|
||||
let gl_window = display.gl_window();
|
||||
platform.handle_event(imgui.io_mut(), gl_window.window(), &event);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
65
implot-examples/implot-glium-examples/examples/text_plots.rs
Normal file
65
implot-examples/implot-glium-examples/examples/text_plots.rs
Normal file
|
@ -0,0 +1,65 @@
|
|||
//! This example demonstrates how the text plotting features are to be used. For more general
|
||||
//! features of the libray, see the line_plots example.
|
||||
|
||||
use imgui::{im_str, CollapsingHeader, Condition, Ui, Window};
|
||||
use implot::{Context, Plot, PlotText, PlotUi};
|
||||
|
||||
mod support;
|
||||
|
||||
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."
|
||||
));
|
||||
let content_width = ui.window_content_region_width();
|
||||
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)
|
||||
.build(plot_ui, || {
|
||||
// The text passed to "new" is what gets displayed.
|
||||
let x_position: f64 = 0.5;
|
||||
let y_position: f64 = 0.2;
|
||||
let vertical: bool = false;
|
||||
PlotText::new("horizontal displayed text").plot(x_position, y_position, vertical);
|
||||
|
||||
// The text passed to "new" is what gets displayed.
|
||||
let x_position: f64 = 0.2;
|
||||
let y_position: f64 = 0.2;
|
||||
let vertical: bool = true;
|
||||
PlotText::new("vertical displayed text").plot(x_position, y_position, vertical);
|
||||
});
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let system = support::init(file!());
|
||||
let mut showing_demo = false;
|
||||
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, || {
|
||||
ui.text(im_str!("Hello from implot-rs!"));
|
||||
ui.text_wrapped(im_str!(
|
||||
"The headers here demo the text plotting features of the library. \
|
||||
Have a look at the example source code to see how they are implemented.\n\
|
||||
Check out the demo from ImPlot itself first \
|
||||
(by enabling the 'Show demo' checkbox) for instructions \
|
||||
on how to interact with ImPlot plots."
|
||||
));
|
||||
|
||||
ui.checkbox(im_str!("Show demo"), &mut showing_demo);
|
||||
|
||||
// Show individual examples in collapsed headers
|
||||
if CollapsingHeader::new(im_str!("Basic text plot")).build(&ui) {
|
||||
show_basic_plot(&ui, &plot_ui);
|
||||
}
|
||||
});
|
||||
|
||||
if showing_demo {
|
||||
implot::show_demo_window(&mut showing_demo);
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue