2020-10-11 15:17:29 +00:00
|
|
|
//! Module for handling the ImPlot context. This is modeled quite directly after how
|
|
|
|
//! this is dealt with in imgui-rs, because it follows the same concepts and doing this
|
|
|
|
//! also helps readability if one is already familiar with the imgui code.
|
|
|
|
|
2020-10-11 14:25:48 +00:00
|
|
|
use parking_lot::ReentrantMutex;
|
|
|
|
|
2020-10-26 17:54:48 +00:00
|
|
|
use crate::sys;
|
2020-10-11 14:25:48 +00:00
|
|
|
use crate::PlotUi;
|
2020-10-11 13:22:55 +00:00
|
|
|
/// An implot context.
|
|
|
|
///
|
|
|
|
/// A context is required to do most of the things this library provides. While this was created
|
2020-10-11 15:17:29 +00:00
|
|
|
/// implicitly in earlier versions of the library, it is now created explicitly. These contexts
|
|
|
|
/// cannot currently be disabled through the high level API. This could be implemented though,
|
|
|
|
/// if you need multiple contexts that you can switch around between, file an issue.
|
2021-01-13 20:13:40 +00:00
|
|
|
#[rustversion::attr(since(1.48), doc(alias = "ImPlotContext"))]
|
2020-10-11 13:22:55 +00:00
|
|
|
pub struct Context {
|
|
|
|
raw: *mut sys::ImPlotContext,
|
|
|
|
}
|
|
|
|
|
2020-10-26 17:54:48 +00:00
|
|
|
lazy_static::lazy_static! {
|
2020-10-11 14:25:48 +00:00
|
|
|
// This mutex is used to guard any accesses to the context
|
2020-10-11 13:42:30 +00:00
|
|
|
static ref CTX_MUTEX: ReentrantMutex<()> = ReentrantMutex::new(());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn no_current_context() -> bool {
|
|
|
|
let ctx = unsafe { sys::ImPlot_GetCurrentContext() };
|
|
|
|
ctx.is_null()
|
|
|
|
}
|
|
|
|
|
2020-10-11 13:22:55 +00:00
|
|
|
impl Context {
|
2020-10-11 15:17:29 +00:00
|
|
|
/// Create a context. This will also activate the context in ImPlot, and hence creating
|
|
|
|
/// a second context when one already exists is an error and will panic.
|
2020-10-11 13:22:55 +00:00
|
|
|
pub fn create() -> Self {
|
2020-10-11 13:42:30 +00:00
|
|
|
let _guard = CTX_MUTEX.lock();
|
|
|
|
assert!(
|
|
|
|
no_current_context(),
|
|
|
|
"A new active context cannot be created, because another one already exists"
|
|
|
|
);
|
|
|
|
|
2020-10-11 13:22:55 +00:00
|
|
|
let ctx = unsafe { sys::ImPlot_CreateContext() };
|
|
|
|
unsafe {
|
|
|
|
sys::ImPlot_SetCurrentContext(ctx);
|
|
|
|
}
|
|
|
|
Self { raw: ctx }
|
|
|
|
}
|
2020-10-11 14:25:48 +00:00
|
|
|
|
2020-10-11 15:17:29 +00:00
|
|
|
/// Get a "plot ui" struct, this will be used to build actual plots and is quite
|
|
|
|
/// analogous to imgui-rs' "Ui" struct.
|
2020-10-11 14:25:48 +00:00
|
|
|
pub fn get_plot_ui(&self) -> PlotUi {
|
|
|
|
PlotUi { context: self }
|
|
|
|
}
|
2020-11-29 18:21:12 +00:00
|
|
|
|
|
|
|
/// Use light colors for the implot style.
|
|
|
|
///
|
|
|
|
/// This will eventually be exposed more thoroughly in the form of ImPlotStyle,
|
|
|
|
/// but for now this allows one to at least easily set the color preset.
|
|
|
|
pub fn use_light_colors(&self) {
|
|
|
|
unsafe {
|
|
|
|
let style = sys::ImPlot_GetStyle();
|
|
|
|
assert_ne!(style, std::ptr::null_mut());
|
|
|
|
sys::ImPlot_StyleColorsLight(style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use dark colors for the implot style.
|
|
|
|
///
|
|
|
|
/// This will eventually be exposed more thoroughly in the form of ImPlotStyle,
|
|
|
|
/// but for now this allows one to at least easily set the color preset.
|
|
|
|
pub fn use_dark_colors(&self) {
|
|
|
|
unsafe {
|
|
|
|
let style = sys::ImPlot_GetStyle();
|
|
|
|
assert_ne!(style, std::ptr::null_mut());
|
|
|
|
sys::ImPlot_StyleColorsDark(style);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Use classic colors for the implot style.
|
|
|
|
///
|
|
|
|
/// This will eventually be exposed more thoroughly in the form of ImPlotStyle,
|
|
|
|
/// but for now this allows one to at least easily set the color preset.
|
|
|
|
pub fn use_classic_colors(&self) {
|
|
|
|
unsafe {
|
|
|
|
let style = sys::ImPlot_GetStyle();
|
|
|
|
assert_ne!(style, std::ptr::null_mut());
|
|
|
|
sys::ImPlot_StyleColorsClassic(style);
|
|
|
|
}
|
|
|
|
}
|
2020-10-11 13:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Context {
|
|
|
|
fn drop(&mut self) {
|
2020-10-11 13:42:30 +00:00
|
|
|
let _guard = CTX_MUTEX.lock();
|
2020-10-11 13:22:55 +00:00
|
|
|
unsafe {
|
|
|
|
sys::ImPlot_DestroyContext(self.raw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|