use crate::{XAxis, YAxis, compat_ffi, sys};
use dear_imgui_rs::with_scratch_txt;
pub fn is_plot_hovered() -> bool {
unsafe { sys::ImPlot_IsPlotHovered() }
}
pub fn is_subplots_hovered() -> bool {
unsafe { sys::ImPlot_IsSubplotsHovered() }
}
pub fn is_legend_entry_hovered(label: &str) -> bool {
let label = if label.contains('\0') { "" } else { label };
with_scratch_txt(label, |ptr| unsafe {
sys::ImPlot_IsLegendEntryHovered(ptr)
})
}
pub fn get_plot_mouse_position(y_axis_choice: Option<crate::YAxisChoice>) -> sys::ImPlotPoint {
let x_axis = 0; let y_axis = match y_axis_choice {
Some(crate::YAxisChoice::First) => 3, Some(crate::YAxisChoice::Second) => 4, Some(crate::YAxisChoice::Third) => 5, None => 3, };
unsafe { sys::ImPlot_GetPlotMousePos(x_axis as sys::ImAxis, y_axis as sys::ImAxis) }
}
pub fn get_plot_mouse_position_axes(x_axis: XAxis, y_axis: YAxis) -> sys::ImPlotPoint {
unsafe { sys::ImPlot_GetPlotMousePos(x_axis as sys::ImAxis, y_axis as sys::ImAxis) }
}
pub fn pixels_to_plot(
pixel_position: [f32; 2],
y_axis_choice: Option<crate::YAxisChoice>,
) -> sys::ImPlotPoint {
let y_index = match y_axis_choice {
Some(crate::YAxisChoice::First) => 0,
Some(crate::YAxisChoice::Second) => 1,
Some(crate::YAxisChoice::Third) => 2,
None => 0,
};
unsafe {
let plot = sys::ImPlot_GetCurrentPlot();
if plot.is_null() {
return sys::ImPlotPoint { x: 0.0, y: 0.0 };
}
let x_axis_ptr = sys::ImPlotPlot_XAxis_Nil(plot, 0);
let y_axis_ptr = sys::ImPlotPlot_YAxis_Nil(plot, y_index);
let x = sys::ImPlotAxis_PixelsToPlot(x_axis_ptr, pixel_position[0]);
let y = sys::ImPlotAxis_PixelsToPlot(y_axis_ptr, pixel_position[1]);
sys::ImPlotPoint { x, y }
}
}
pub fn pixels_to_plot_axes(
pixel_position: [f32; 2],
x_axis: XAxis,
y_axis: YAxis,
) -> sys::ImPlotPoint {
unsafe {
let plot = sys::ImPlot_GetCurrentPlot();
if plot.is_null() {
return sys::ImPlotPoint { x: 0.0, y: 0.0 };
}
let x_axis_ptr = sys::ImPlotPlot_XAxis_Nil(plot, x_axis as i32);
let y_axis_ptr = sys::ImPlotPlot_YAxis_Nil(plot, y_axis.to_index());
let x = sys::ImPlotAxis_PixelsToPlot(x_axis_ptr, pixel_position[0]);
let y = sys::ImPlotAxis_PixelsToPlot(y_axis_ptr, pixel_position[1]);
sys::ImPlotPoint { x, y }
}
}
pub fn plot_to_pixels(
plot_position: sys::ImPlotPoint,
y_axis_choice: Option<crate::YAxisChoice>,
) -> [f32; 2] {
let y_index = match y_axis_choice {
Some(crate::YAxisChoice::First) => 0,
Some(crate::YAxisChoice::Second) => 1,
Some(crate::YAxisChoice::Third) => 2,
None => 0,
};
unsafe {
let plot = sys::ImPlot_GetCurrentPlot();
if plot.is_null() {
return [0.0, 0.0];
}
let x_axis_ptr = sys::ImPlotPlot_XAxis_Nil(plot, 0);
let y_axis_ptr = sys::ImPlotPlot_YAxis_Nil(plot, y_index);
let px = sys::ImPlotAxis_PlotToPixels(x_axis_ptr, plot_position.x);
let py = sys::ImPlotAxis_PlotToPixels(y_axis_ptr, plot_position.y);
[px, py]
}
}
pub fn plot_to_pixels_axes(
plot_position: sys::ImPlotPoint,
x_axis: XAxis,
y_axis: YAxis,
) -> [f32; 2] {
unsafe {
let plot = sys::ImPlot_GetCurrentPlot();
if plot.is_null() {
return [0.0, 0.0];
}
let x_axis_ptr = sys::ImPlotPlot_XAxis_Nil(plot, x_axis as i32);
let y_axis_ptr = sys::ImPlotPlot_YAxis_Nil(plot, y_axis.to_index());
let px = sys::ImPlotAxis_PlotToPixels(x_axis_ptr, plot_position.x);
let py = sys::ImPlotAxis_PlotToPixels(y_axis_ptr, plot_position.y);
[px, py]
}
}
pub fn get_plot_limits(
_x_axis_choice: Option<crate::YAxisChoice>,
y_axis_choice: Option<crate::YAxisChoice>,
) -> sys::ImPlotRect {
let x_axis = 0; let y_axis = match y_axis_choice {
Some(crate::YAxisChoice::First) => 3, Some(crate::YAxisChoice::Second) => 4, Some(crate::YAxisChoice::Third) => 5, None => 3, };
unsafe { sys::ImPlot_GetPlotLimits(x_axis, y_axis) }
}
pub fn is_plot_selected() -> bool {
unsafe { sys::ImPlot_IsPlotSelected() }
}
pub fn get_plot_selection_axes(x_axis: XAxis, y_axis: YAxis) -> Option<sys::ImPlotRect> {
if !is_plot_selected() {
return None;
}
let rect = unsafe { sys::ImPlot_GetPlotSelection(x_axis as i32, y_axis as i32) };
Some(rect)
}
pub fn annotation_point(
x: f64,
y: f64,
color: [f32; 4],
pixel_offset: [f32; 2],
clamp: bool,
round: bool,
) {
let col = sys::ImVec4_c {
x: color[0],
y: color[1],
z: color[2],
w: color[3],
};
let off = sys::ImVec2_c {
x: pixel_offset[0],
y: pixel_offset[1],
};
unsafe { sys::ImPlot_Annotation_Bool(x, y, col, off, clamp, round) }
}
pub fn annotation_text(
x: f64,
y: f64,
color: [f32; 4],
pixel_offset: [f32; 2],
clamp: bool,
text: &str,
) {
let col = sys::ImVec4_c {
x: color[0],
y: color[1],
z: color[2],
w: color[3],
};
let off = sys::ImVec2_c {
x: pixel_offset[0],
y: pixel_offset[1],
};
assert!(!text.contains('\0'), "text contained NUL");
with_scratch_txt(text, |ptr| unsafe {
compat_ffi::ImPlot_Annotation_Str0(x, y, col, off, clamp, ptr)
})
}
pub fn tag_x(x: f64, color: [f32; 4], round: bool) {
let col = sys::ImVec4_c {
x: color[0],
y: color[1],
z: color[2],
w: color[3],
};
unsafe { sys::ImPlot_TagX_Bool(x, col, round) }
}
pub fn tag_x_text(x: f64, color: [f32; 4], text: &str) {
let col = sys::ImVec4_c {
x: color[0],
y: color[1],
z: color[2],
w: color[3],
};
assert!(!text.contains('\0'), "text contained NUL");
with_scratch_txt(text, |ptr| unsafe {
compat_ffi::ImPlot_TagX_Str0(x, col, ptr)
})
}
pub fn tag_y(y: f64, color: [f32; 4], round: bool) {
let col = sys::ImVec4_c {
x: color[0],
y: color[1],
z: color[2],
w: color[3],
};
unsafe { sys::ImPlot_TagY_Bool(y, col, round) }
}
pub fn tag_y_text(y: f64, color: [f32; 4], text: &str) {
let col = sys::ImVec4_c {
x: color[0],
y: color[1],
z: color[2],
w: color[3],
};
assert!(!text.contains('\0'), "text contained NUL");
with_scratch_txt(text, |ptr| unsafe {
compat_ffi::ImPlot_TagY_Str0(y, col, ptr)
})
}
pub fn get_plot_limits_axes(x_axis: XAxis, y_axis: YAxis) -> sys::ImPlotRect {
unsafe { sys::ImPlot_GetPlotLimits(x_axis as i32, y_axis as i32) }
}
pub fn is_axis_hovered(axis: i32) -> bool {
unsafe { sys::ImPlot_IsAxisHovered(axis) }
}
pub fn is_plot_x_axis_hovered() -> bool {
is_axis_hovered(XAxis::X1 as i32)
}
pub fn is_plot_x_axis_hovered_axis(x_axis: XAxis) -> bool {
is_axis_hovered(x_axis as i32)
}
pub fn is_plot_y_axis_hovered(y_axis_choice: Option<crate::YAxisChoice>) -> bool {
let y_axis = match y_axis_choice {
Some(crate::YAxisChoice::First) => 3, Some(crate::YAxisChoice::Second) => 4, Some(crate::YAxisChoice::Third) => 5, None => 3, };
is_axis_hovered(y_axis)
}
pub fn is_plot_y_axis_hovered_axis(y_axis: YAxis) -> bool {
is_axis_hovered(y_axis as i32)
}
#[cfg(feature = "demo")]
pub fn show_demo_window(show: &mut bool) {
unsafe { sys::ImPlot_ShowDemoWindow(show) }
}
#[cfg(not(feature = "demo"))]
pub fn show_demo_window(_show: &mut bool) {}
pub fn show_user_guide() {
unsafe { sys::ImPlot_ShowUserGuide() }
}
pub fn show_metrics_window(open: &mut bool) {
unsafe { sys::ImPlot_ShowMetricsWindow(open as *mut bool) }
}
pub fn get_plot_pos() -> [f32; 2] {
let out = unsafe { crate::compat_ffi::ImPlot_GetPlotPos() };
[out.x, out.y]
}
pub fn get_plot_size() -> [f32; 2] {
let out = unsafe { crate::compat_ffi::ImPlot_GetPlotSize() };
[out.x, out.y]
}
pub fn get_plot_draw_list() -> *mut sys::ImDrawList {
unsafe { sys::ImPlot_GetPlotDrawList() }
}
pub fn push_plot_clip_rect(expand: f32) {
unsafe { sys::ImPlot_PushPlotClipRect(expand) }
}
pub fn pop_plot_clip_rect() {
unsafe { sys::ImPlot_PopPlotClipRect() }
}
#[derive(Debug, Clone, Copy, Default)]
pub struct DragResult {
pub changed: bool,
pub clicked: bool,
pub hovered: bool,
pub held: bool,
}
fn color4(rgba: [f32; 4]) -> sys::ImVec4_c {
sys::ImVec4_c {
x: rgba[0],
y: rgba[1],
z: rgba[2],
w: rgba[3],
}
}
pub fn drag_point(
id: i32,
x: &mut f64,
y: &mut f64,
color: [f32; 4],
size: f32,
flags: crate::DragToolFlags,
) -> DragResult {
let mut clicked = false;
let mut hovered = false;
let mut held = false;
let changed = unsafe {
sys::ImPlot_DragPoint(
id,
x as *mut f64,
y as *mut f64,
color4(color),
size,
flags.bits() as i32,
&mut clicked as *mut bool,
&mut hovered as *mut bool,
&mut held as *mut bool,
)
};
DragResult {
changed,
clicked,
hovered,
held,
}
}
pub fn drag_line_x(
id: i32,
x: &mut f64,
color: [f32; 4],
thickness: f32,
flags: crate::DragToolFlags,
) -> DragResult {
let mut clicked = false;
let mut hovered = false;
let mut held = false;
let changed = unsafe {
sys::ImPlot_DragLineX(
id,
x as *mut f64,
color4(color),
thickness,
flags.bits() as i32,
&mut clicked as *mut bool,
&mut hovered as *mut bool,
&mut held as *mut bool,
)
};
DragResult {
changed,
clicked,
hovered,
held,
}
}
pub fn drag_line_y(
id: i32,
y: &mut f64,
color: [f32; 4],
thickness: f32,
flags: crate::DragToolFlags,
) -> DragResult {
let mut clicked = false;
let mut hovered = false;
let mut held = false;
let changed = unsafe {
sys::ImPlot_DragLineY(
id,
y as *mut f64,
color4(color),
thickness,
flags.bits() as i32,
&mut clicked as *mut bool,
&mut hovered as *mut bool,
&mut held as *mut bool,
)
};
DragResult {
changed,
clicked,
hovered,
held,
}
}