Skip to main content

dear_implot3d/style/
item_arrays.rs

1use crate::{Plot3DScope, sys};
2use std::borrow::Cow;
3
4/// One-shot array-backed item style overrides for the next ImPlot3D submission.
5/// An empty slice uses the native scalar style for that property.
6#[derive(Debug, Clone, Default, PartialEq)]
7pub struct Plot3DItemArrayStyle<'a> {
8    line_colors: Option<Cow<'a, [u32]>>,
9    fill_colors: Option<Cow<'a, [u32]>>,
10    marker_sizes: Option<Cow<'a, [f32]>>,
11    marker_line_colors: Option<Cow<'a, [u32]>>,
12    marker_fill_colors: Option<Cow<'a, [u32]>>,
13}
14
15impl<'a> Plot3DItemArrayStyle<'a> {
16    /// Create an empty array-style override.
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Override per-index line colors using Dear ImGui packed colors (`ImU32` / ABGR).
22    pub fn with_line_colors(mut self, colors: &'a [u32]) -> Self {
23        self.line_colors = Some(Cow::Borrowed(colors));
24        self
25    }
26
27    /// Override per-index fill colors using Dear ImGui packed colors (`ImU32` / ABGR).
28    pub fn with_fill_colors(mut self, colors: &'a [u32]) -> Self {
29        self.fill_colors = Some(Cow::Borrowed(colors));
30        self
31    }
32
33    /// Override per-index marker sizes in pixels.
34    pub fn with_marker_sizes(mut self, sizes: &'a [f32]) -> Self {
35        self.marker_sizes = Some(Cow::Borrowed(sizes));
36        self
37    }
38
39    /// Override per-index marker outline colors using Dear ImGui packed colors (`ImU32` / ABGR).
40    pub fn with_marker_line_colors(mut self, colors: &'a [u32]) -> Self {
41        self.marker_line_colors = Some(Cow::Borrowed(colors));
42        self
43    }
44
45    /// Override per-index marker fill colors using Dear ImGui packed colors (`ImU32` / ABGR).
46    pub fn with_marker_fill_colors(mut self, colors: &'a [u32]) -> Self {
47        self.marker_fill_colors = Some(Cow::Borrowed(colors));
48        self
49    }
50
51    pub(super) fn apply_to_spec(&self, spec: &mut sys::ImPlot3DSpec_c) {
52        spec.LineColors = self
53            .line_colors
54            .as_deref()
55            .filter(|values| !values.is_empty())
56            .map_or(std::ptr::null_mut(), |colors| colors.as_ptr() as *mut _);
57        spec.FillColors = self
58            .fill_colors
59            .as_deref()
60            .filter(|values| !values.is_empty())
61            .map_or(std::ptr::null_mut(), |colors| colors.as_ptr() as *mut _);
62        spec.MarkerSizes = self
63            .marker_sizes
64            .as_deref()
65            .filter(|values| !values.is_empty())
66            .map_or(std::ptr::null_mut(), |sizes| sizes.as_ptr() as *mut _);
67        spec.MarkerLineColors = self
68            .marker_line_colors
69            .as_deref()
70            .filter(|values| !values.is_empty())
71            .map_or(std::ptr::null_mut(), |colors| colors.as_ptr() as *mut _);
72        spec.MarkerFillColors = self
73            .marker_fill_colors
74            .as_deref()
75            .filter(|values| !values.is_empty())
76            .map_or(std::ptr::null_mut(), |colors| colors.as_ptr() as *mut _);
77    }
78}
79
80impl<'plot> Plot3DScope<'plot> {
81    /// Apply array-backed item styling to the next ImPlot3D submission executed inside `f`.
82    ///
83    /// This is closure-scoped so borrowed slices stay valid for the entire next
84    /// plot call and are restored even if `f` panics before submitting an item.
85    ///
86    /// # Safety
87    ///
88    /// Every non-empty style array must contain enough elements for every index the submitted plot
89    /// may read. Upstream accepts only pointers and carries no array lengths, so this cannot be
90    /// validated by this method. In particular, a surface's wireframe can have more edges than
91    /// grid points. Only submissions to this plot consume the override. The native item reads
92    /// its arrays during submission and releases them in `EndItem`.
93    pub unsafe fn with_next_plot3d_item_array_style<'a, R>(
94        &self,
95        style: Plot3DItemArrayStyle<'a>,
96        f: impl FnOnce(&Plot3DScope<'plot>) -> R,
97    ) -> R {
98        self.next_item
99            .scoped(|spec| style.apply_to_spec(spec), || f(self))
100    }
101}