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