Added main crate and some initial code for idiomatic bindings

This commit is contained in:
4bb4 2020-08-04 22:05:04 +02:00
parent db4d307b85
commit ee910152b7
6 changed files with 130 additions and 43 deletions

23
Cargo.toml Normal file
View file

@ -0,0 +1,23 @@
[package]
name = "implot"
version = "0.1.0"
edition = "2018"
authors = ["Sandro Merkli", "implot-rs contributors"]
description = "High-level Rust bindings to implot"
homepage = "https://github.com/4bb4/implot-rs"
repository = "https://github.com/4bb4/implot-rs"
license = "MIT/Apache-2.0"
categories = ["gui", "api-bindings"]
[dependencies]
implot-sys = { version = "0.1.0", path = "implot-sys" }
[workspace]
members = [
"implot-sys",
]
exclude = [
"implot-sys-bindgen",
"testexample",
]

View file

@ -4,6 +4,8 @@ version = "0.1.0"
edition = "2018" edition = "2018"
authors = ["Sandro Merkli", "implot-rs contributors"] authors = ["Sandro Merkli", "implot-rs contributors"]
description = "Raw FFI bindings to implot" description = "Raw FFI bindings to implot"
homepage = "https://github.com/4bb4/implot-rs"
repository = "https://github.com/4bb4/implot-rs"
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
categories = ["gui", "external-ffi-bindings"] categories = ["gui", "external-ffi-bindings"]
build = "build.rs" build = "build.rs"

View file

@ -2,4 +2,6 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(non_snake_case)] #![allow(non_snake_case)]
pub use imgui;
include!("bindings.rs"); include!("bindings.rs");

92
src/lib.rs Normal file
View file

@ -0,0 +1,92 @@
//! # Rust bindings to ImPlot
//!
//! This crate contains idiomatic bindings to the C++ implot library, which
//! use the bindings exposed by the `implot-sys` crate.
pub extern crate implot_sys as sys;
use sys::imgui::im_str;
/// Struct to represent an ImPlot.
pub struct Plot {
/// Title of the plot, shown on top.
title: String,
/// Label of the x axis, shown on the bottom
x_label: String,
/// Label of the y axis, shown on the left
y_label: String,
/// Size of the plot in x direction, in pixels.
size_x: u32,
/// Size of the plot in y direction, in pixels.
size_y: u32,
/// Flags relating to the plot TODO(4bb4) make those into bitflags
plot_flags: sys::ImPlotFlags,
/// Flags relating to the first x axis of the plot TODO(4bb4) make those into bitflags
x_flags: sys::ImPlotAxisFlags,
/// Flags relating to the first y axis of the plot TODO(4bb4) make those into bitflags
y_flags: sys::ImPlotAxisFlags,
/// Flags relating to the second x axis of the plot (if present, otherwise ignored)
/// TODO(4bb4) make those into bitflags
x2_flags: sys::ImPlotAxisFlags,
/// Flags relating to the second y axis of the plot (if present, otherwise ignored)
/// TODO(4bb4) make those into bitflags
y2_flags: sys::ImPlotAxisFlags,
}
impl Plot {
/// Create a new plot with some defaults set. Does not draw anything yet.
pub fn new() -> Self {
// TODO(4bb4) question these defaults, maybe remove some of them
Self {
title: "".to_owned(),
x_label: "".to_owned(),
y_label: "".to_owned(),
size_x: 400,
size_y: 400,
plot_flags: 0xFF,
x_flags: 7,
y_flags: 7,
x2_flags: 0,
y2_flags: 0,
}
}
/// Attempt to show the plot. Only do things with it and call `end()` after that
/// if this returns `true`.
pub fn begin(&self) -> bool {
unsafe {
sys::ImPlot_BeginPlot(
im_str!("{}", self.title).as_ptr(),
im_str!("{}", self.x_label).as_ptr(),
im_str!("{}", self.y_label).as_ptr(),
sys::ImVec2 {
x: self.size_x as f32,
y: self.size_y as f32,
},
self.plot_flags,
self.x_flags,
self.y_flags,
self.x2_flags,
self.y2_flags,
)
}
}
/// End a previously-`begin()`ed plot. Only call if `begin()` returned `true`.
pub fn end(&self) {
unsafe { sys::ImPlot_EndPlot() }
}
}
// TODO(4bb4) convert to struct and add methods to set title and flags
/// Plot a line. Use this between Plot::begin() and Plot::end().
pub fn plot_line(x: &Vec<f64>, y: &Vec<f64>, label: &str) {
unsafe {
implot_sys::ImPlot_PlotLinedoublePtrdoublePtr(
im_str!("{}", label).as_ptr() as *const i8,
x.as_ptr(),
y.as_ptr(),
x.len().min(y.len()) as i32, // "as" casts saturate as of Rust 1.45
0,
8,
);
}
}

View file

@ -19,3 +19,4 @@ imgui-glium-renderer = { git = "https://github.com/Gekkio/imgui-rs/", branch = "
imgui-winit-support = { git = "https://github.com/Gekkio/imgui-rs/", branch = "master" } imgui-winit-support = { git = "https://github.com/Gekkio/imgui-rs/", branch = "master" }
implot-sys = { path = "../implot-sys" } implot-sys = { path = "../implot-sys" }
implot = { path = "../" }

View file

@ -1,5 +1,5 @@
use imgui::*; use imgui::*;
use implot_sys; use implot;
mod support; mod support;
@ -19,48 +19,15 @@ fn main() {
mouse_pos[0], mouse_pos[1] mouse_pos[0], mouse_pos[1]
)); ));
// TODO(4bb4) Replace this with safe bindings once those are written // Demo some implot stuff :D
let x_values = vec![1.0, 2.0, 4.0, 5.0];
let x_values: [f64; 4] = [1.0, 2.0, 4.0, 5.0]; let y_values = vec![1.0, 0.0, 0.0, 1.0];
let y_values: [f64; 4] = [1.0, 0.0, 0.0, 1.0]; let plot = implot::Plot::new();
unsafe { if plot.begin() {
if implot_sys::ImPlot_BeginPlot( implot::plot_line(&vec![2.0, 2.0], &vec![2.0, 1.0], "Left eye");
im_str!("My Plot").as_ptr() as *const i8, implot::plot_line(&vec![4.0, 4.0], &vec![2.0, 1.0], "Right eye");
im_str!("x").as_ptr() as *const i8, implot::plot_line(&x_values, &y_values, "Mouth");
im_str!("y").as_ptr() as *const i8, plot.end();
implot_sys::ImVec2 { x: 600.0, y: 600.0 },
0xFF,
7,
7,
0,
0,
) {
implot_sys::ImPlot_PlotLinedoublePtrdoublePtr(
im_str!("Mouth").as_ptr() as *const i8,
x_values.as_ptr(),
y_values.as_ptr(),
x_values.len() as i32,
0,
8,
);
implot_sys::ImPlot_PlotLinedoublePtrdoublePtr(
im_str!("Left eye").as_ptr() as *const i8,
[2.0, 2.0].as_ptr(),
[2.0, 1.0].as_ptr(),
2i32,
0,
8,
);
implot_sys::ImPlot_PlotLinedoublePtrdoublePtr(
im_str!("Right eye").as_ptr() as *const i8,
[4.0, 4.0].as_ptr(),
[2.0, 1.0].as_ptr(),
2i32,
0,
8,
);
implot_sys::ImPlot_EndPlot();
}
} }
}); });
}); });