Skip to main content

dear_implot3d/style/
item_arrays.rs

1use crate::sys;
2use std::borrow::Cow;
3
4/// One-shot array-backed item style overrides for the next ImPlot3D submission.
5#[derive(Debug, Clone, Default, PartialEq)]
6pub struct Plot3DItemArrayStyle<'a> {
7    line_colors: Option<Cow<'a, [u32]>>,
8    fill_colors: Option<Cow<'a, [u32]>>,
9    marker_sizes: Option<Cow<'a, [f32]>>,
10    marker_line_colors: Option<Cow<'a, [u32]>>,
11    marker_fill_colors: Option<Cow<'a, [u32]>>,
12}
13
14impl<'a> Plot3DItemArrayStyle<'a> {
15    /// Create an empty array-style override.
16    pub fn new() -> Self {
17        Self::default()
18    }
19
20    /// Override per-index line colors using Dear ImGui packed colors (`ImU32` / ABGR).
21    pub fn with_line_colors(mut self, colors: &'a [u32]) -> Self {
22        self.line_colors = Some(Cow::Borrowed(colors));
23        self
24    }
25
26    /// Override per-index fill colors using Dear ImGui packed colors (`ImU32` / ABGR).
27    pub fn with_fill_colors(mut self, colors: &'a [u32]) -> Self {
28        self.fill_colors = Some(Cow::Borrowed(colors));
29        self
30    }
31
32    /// Override per-index marker sizes in pixels.
33    pub fn with_marker_sizes(mut self, sizes: &'a [f32]) -> Self {
34        self.marker_sizes = Some(Cow::Borrowed(sizes));
35        self
36    }
37
38    /// Override per-index marker outline colors using Dear ImGui packed colors (`ImU32` / ABGR).
39    pub fn with_marker_line_colors(mut self, colors: &'a [u32]) -> Self {
40        self.marker_line_colors = Some(Cow::Borrowed(colors));
41        self
42    }
43
44    /// Override per-index marker fill colors using Dear ImGui packed colors (`ImU32` / ABGR).
45    pub fn with_marker_fill_colors(mut self, colors: &'a [u32]) -> Self {
46        self.marker_fill_colors = Some(Cow::Borrowed(colors));
47        self
48    }
49
50    fn apply_to_spec(&self, spec: &mut sys::ImPlot3DSpec_c) {
51        spec.LineColors = self
52            .line_colors
53            .as_ref()
54            .map_or(std::ptr::null_mut(), |colors| colors.as_ptr() as *mut _);
55        spec.FillColors = self
56            .fill_colors
57            .as_ref()
58            .map_or(std::ptr::null_mut(), |colors| colors.as_ptr() as *mut _);
59        spec.MarkerSizes = self
60            .marker_sizes
61            .as_ref()
62            .map_or(std::ptr::null_mut(), |sizes| sizes.as_ptr() as *mut _);
63        spec.MarkerLineColors = self
64            .marker_line_colors
65            .as_ref()
66            .map_or(std::ptr::null_mut(), |colors| colors.as_ptr() as *mut _);
67        spec.MarkerFillColors = self
68            .marker_fill_colors
69            .as_ref()
70            .map_or(std::ptr::null_mut(), |colors| colors.as_ptr() as *mut _);
71    }
72}
73
74/// Apply array-backed item styling to the next ImPlot3D submission executed inside `f`.
75pub fn with_next_plot3d_item_array_style<'a, R>(
76    style: Plot3DItemArrayStyle<'a>,
77    f: impl FnOnce() -> R,
78) -> R {
79    let previous = crate::take_next_plot3d_spec();
80    let mut spec = previous.unwrap_or_else(crate::default_plot3d_spec);
81    style.apply_to_spec(&mut spec);
82    crate::set_next_plot3d_spec(Some(spec));
83
84    let out = f();
85
86    if crate::take_next_plot3d_spec().is_some() {
87        crate::set_next_plot3d_spec(previous);
88    }
89
90    out
91}