Skip to main content

dear_implot3d/
item_style.rs

1use crate::layout::NextItemStyle;
2use crate::plots::Plot3D;
3use crate::{Plot3DDataLayout, sys};
4use dear_imgui_rs::texture::TextureRef;
5
6fn color4(rgba: [f32; 4]) -> sys::ImVec4_c {
7    sys::ImVec4_c {
8        x: rgba[0],
9        y: rgba[1],
10        z: rgba[2],
11        w: rgba[3],
12    }
13}
14
15/// Common style overrides for plot items backed by `ImPlot3DSpec`.
16#[derive(Debug, Clone, Copy, Default, PartialEq)]
17pub struct Plot3DItemStyle {
18    pub(crate) line_color: Option<sys::ImVec4_c>,
19    pub(crate) line_weight: Option<f32>,
20    pub(crate) fill_color: Option<sys::ImVec4_c>,
21    pub(crate) fill_alpha: Option<f32>,
22    pub(crate) marker: Option<sys::ImPlot3DMarker>,
23    pub(crate) marker_size: Option<f32>,
24    pub(crate) marker_line_color: Option<sys::ImVec4_c>,
25    pub(crate) marker_fill_color: Option<sys::ImVec4_c>,
26}
27
28impl Plot3DItemStyle {
29    /// Create an empty style override that keeps ImPlot3D defaults.
30    pub fn new() -> Self {
31        Self::default()
32    }
33
34    /// Override the plot item's line color.
35    pub fn with_line_color(mut self, color: [f32; 4]) -> Self {
36        self.line_color = Some(color4(color));
37        self
38    }
39
40    /// Override the plot item's line width in pixels.
41    pub fn with_line_weight(mut self, weight: f32) -> Self {
42        self.line_weight = Some(weight);
43        self
44    }
45
46    /// Override the plot item's fill color.
47    pub fn with_fill_color(mut self, color: [f32; 4]) -> Self {
48        self.fill_color = Some(color4(color));
49        self
50    }
51
52    /// Override the fill alpha multiplier used by filled regions and marker faces.
53    pub fn with_fill_alpha(mut self, alpha: f32) -> Self {
54        self.fill_alpha = Some(alpha);
55        self
56    }
57
58    /// Override the marker type.
59    pub fn with_marker(mut self, marker: crate::Marker3D) -> Self {
60        self.marker = Some(marker as sys::ImPlot3DMarker);
61        self
62    }
63
64    /// Override the marker size in pixels.
65    pub fn with_marker_size(mut self, size: f32) -> Self {
66        self.marker_size = Some(size);
67        self
68    }
69
70    /// Override the marker outline color.
71    pub fn with_marker_line_color(mut self, color: [f32; 4]) -> Self {
72        self.marker_line_color = Some(color4(color));
73        self
74    }
75
76    /// Override the marker fill color.
77    pub fn with_marker_fill_color(mut self, color: [f32; 4]) -> Self {
78        self.marker_fill_color = Some(color4(color));
79        self
80    }
81
82    pub(crate) fn apply_to_spec(self, spec: &mut sys::ImPlot3DSpec_c) {
83        if let Some(line_color) = self.line_color {
84            spec.LineColor = line_color;
85        }
86        if let Some(line_weight) = self.line_weight {
87            spec.LineWeight = line_weight;
88        }
89        if let Some(fill_color) = self.fill_color {
90            spec.FillColor = fill_color;
91        }
92        if let Some(fill_alpha) = self.fill_alpha {
93            spec.FillAlpha = fill_alpha;
94        }
95        if let Some(marker) = self.marker {
96            spec.Marker = marker;
97        }
98        if let Some(marker_size) = self.marker_size {
99            spec.MarkerSize = marker_size;
100        }
101        if let Some(marker_line_color) = self.marker_line_color {
102            spec.MarkerLineColor = marker_line_color;
103        }
104        if let Some(marker_fill_color) = self.marker_fill_color {
105            spec.MarkerFillColor = marker_fill_color;
106        }
107    }
108}
109
110pub(crate) fn plot3d_spec_with_style(
111    next: &NextItemStyle,
112    style: Plot3DItemStyle,
113    flags: u32,
114    layout: Plot3DDataLayout,
115) -> sys::ImPlot3DSpec_c {
116    let mut spec = next.spec(flags, layout);
117    style.apply_to_spec(&mut spec);
118    spec
119}
120
121/// Shared ImPlot3D item-style builder methods for plot builders backed by `ImPlot3DSpec`.
122pub trait Plot3DItemStyled: Sized {
123    /// The output type returned by style-building methods.
124    type Output;
125
126    fn map_style<F>(self, f: F) -> Self::Output
127    where
128        F: FnOnce(&mut Plot3DItemStyle);
129
130    /// Replace the entire item style override for this plot.
131    fn with_style(self, style: Plot3DItemStyle) -> Self::Output {
132        self.map_style(|current| *current = style)
133    }
134
135    /// Set the line color.
136    fn with_line_color(self, color: [f32; 4]) -> Self::Output {
137        self.map_style(|style| style.line_color = Some(color4(color)))
138    }
139
140    /// Set the line width in pixels.
141    fn with_line_weight(self, weight: f32) -> Self::Output {
142        self.map_style(|style| style.line_weight = Some(weight))
143    }
144
145    /// Set the fill color.
146    fn with_fill_color(self, color: [f32; 4]) -> Self::Output {
147        self.map_style(|style| style.fill_color = Some(color4(color)))
148    }
149
150    /// Set the fill alpha multiplier used for fills and marker faces.
151    fn with_fill_alpha(self, alpha: f32) -> Self::Output {
152        self.map_style(|style| style.fill_alpha = Some(alpha))
153    }
154
155    /// Set the marker type.
156    fn with_marker(self, marker: crate::Marker3D) -> Self::Output {
157        self.map_style(|style| style.marker = Some(marker as sys::ImPlot3DMarker))
158    }
159
160    /// Set the marker size in pixels.
161    fn with_marker_size(self, size: f32) -> Self::Output {
162        self.map_style(|style| style.marker_size = Some(size))
163    }
164
165    /// Set the marker outline color.
166    fn with_marker_line_color(self, color: [f32; 4]) -> Self::Output {
167        self.map_style(|style| style.marker_line_color = Some(color4(color)))
168    }
169
170    /// Set the marker fill color.
171    fn with_marker_fill_color(self, color: [f32; 4]) -> Self::Output {
172        self.map_style(|style| style.marker_fill_color = Some(color4(color)))
173    }
174}
175
176/// Shared ImPlot3D item-flag builder methods for plot builders backed by `ImPlot3DSpec`.
177pub trait Plot3DItemFlagged: Sized {
178    /// The output type returned by item-flag building methods.
179    type Output;
180
181    fn map_item_flags<F>(self, f: F) -> Self::Output
182    where
183        F: FnOnce(&mut crate::Item3DFlags);
184
185    /// Set common item flags such as `NO_LEGEND` / `NO_FIT`.
186    fn with_item_flags(self, flags: crate::Item3DFlags) -> Self::Output {
187        self.map_item_flags(|current| *current = flags)
188    }
189}
190
191/// Styled wrapper for plot types that do not store item-style state directly.
192pub struct StyledPlot3D<T> {
193    inner: T,
194    style: Plot3DItemStyle,
195    item_flags: crate::Item3DFlags,
196}
197
198impl<T> StyledPlot3D<T> {
199    /// Consume the wrapper and return the wrapped plot item.
200    pub fn into_inner(self) -> T {
201        self.inner
202    }
203
204    /// Borrow the wrapped plot item.
205    pub fn inner(&self) -> &T {
206        &self.inner
207    }
208}
209
210impl<T> Plot3DItemStyled for StyledPlot3D<T> {
211    type Output = Self;
212
213    fn map_style<F>(mut self, f: F) -> Self::Output
214    where
215        F: FnOnce(&mut Plot3DItemStyle),
216    {
217        f(&mut self.style);
218        self
219    }
220}
221
222impl<T> Plot3DItemFlagged for StyledPlot3D<T> {
223    type Output = Self;
224
225    fn map_item_flags<F>(mut self, f: F) -> Self::Output
226    where
227        F: FnOnce(&mut crate::Item3DFlags),
228    {
229        f(&mut self.item_flags);
230        self
231    }
232}
233
234impl<T> Plot3D for StyledPlot3D<T>
235where
236    T: Plot3D,
237{
238    fn label(&self) -> &str {
239        self.inner.label()
240    }
241
242    fn try_plot(&self, ui: &crate::Plot3DScope<'_>) -> Result<(), crate::plots::Plot3DError> {
243        ui.next_item.scoped(
244            |spec| {
245                self.style.apply_to_spec(spec);
246                spec.Flags |= self.item_flags.bits() as sys::ImPlot3DItemFlags;
247            },
248            || self.inner.try_plot(ui),
249        )
250    }
251}
252
253macro_rules! impl_wrapped_plot3d_item_styled {
254    ($ty:ty) => {
255        impl Plot3DItemStyled for $ty {
256            type Output = StyledPlot3D<Self>;
257
258            fn map_style<F>(self, f: F) -> Self::Output
259            where
260                F: FnOnce(&mut Plot3DItemStyle),
261            {
262                let mut style = Plot3DItemStyle::default();
263                f(&mut style);
264                StyledPlot3D {
265                    inner: self,
266                    style,
267                    item_flags: crate::Item3DFlags::NONE,
268                }
269            }
270        }
271    };
272}
273
274macro_rules! impl_wrapped_plot3d_item_flagged {
275    ($ty:ty) => {
276        impl Plot3DItemFlagged for $ty {
277            type Output = StyledPlot3D<Self>;
278
279            fn map_item_flags<F>(self, f: F) -> Self::Output
280            where
281                F: FnOnce(&mut crate::Item3DFlags),
282            {
283                let mut item_flags = crate::Item3DFlags::NONE;
284                f(&mut item_flags);
285                StyledPlot3D {
286                    inner: self,
287                    style: Plot3DItemStyle::default(),
288                    item_flags,
289                }
290            }
291        }
292    };
293}
294
295impl_wrapped_plot3d_item_styled!(crate::plots::Line3D<'_>);
296impl_wrapped_plot3d_item_styled!(crate::plots::Scatter3D<'_>);
297impl_wrapped_plot3d_item_styled!(crate::plots::Surface3D<'_>);
298impl_wrapped_plot3d_item_styled!(crate::plots::Triangles3D<'_>);
299impl_wrapped_plot3d_item_styled!(crate::plots::Quads3D<'_>);
300impl_wrapped_plot3d_item_styled!(crate::plots::Mesh3D<'_>);
301impl_wrapped_plot3d_item_flagged!(crate::plots::Line3D<'_>);
302impl_wrapped_plot3d_item_flagged!(crate::plots::Scatter3D<'_>);
303impl_wrapped_plot3d_item_flagged!(crate::plots::Surface3D<'_>);
304impl_wrapped_plot3d_item_flagged!(crate::plots::Triangles3D<'_>);
305impl_wrapped_plot3d_item_flagged!(crate::plots::Quads3D<'_>);
306impl_wrapped_plot3d_item_flagged!(crate::plots::Mesh3D<'_>);
307
308impl<'a, T> Plot3DItemStyled for crate::plots::Image3DByAxes<'a, T>
309where
310    T: Into<TextureRef<'a>> + Copy,
311{
312    type Output = StyledPlot3D<Self>;
313
314    fn map_style<F>(self, f: F) -> Self::Output
315    where
316        F: FnOnce(&mut Plot3DItemStyle),
317    {
318        let mut style = Plot3DItemStyle::default();
319        f(&mut style);
320        StyledPlot3D {
321            inner: self,
322            style,
323            item_flags: crate::Item3DFlags::NONE,
324        }
325    }
326}
327
328impl<'a, T> Plot3DItemFlagged for crate::plots::Image3DByAxes<'a, T>
329where
330    T: Into<TextureRef<'a>> + Copy,
331{
332    type Output = StyledPlot3D<Self>;
333
334    fn map_item_flags<F>(self, f: F) -> Self::Output
335    where
336        F: FnOnce(&mut crate::Item3DFlags),
337    {
338        let mut item_flags = crate::Item3DFlags::NONE;
339        f(&mut item_flags);
340        StyledPlot3D {
341            inner: self,
342            style: Plot3DItemStyle::default(),
343            item_flags,
344        }
345    }
346}
347
348impl<'a, T> Plot3DItemStyled for crate::plots::Image3DByCorners<'a, T>
349where
350    T: Into<TextureRef<'a>> + Copy,
351{
352    type Output = StyledPlot3D<Self>;
353
354    fn map_style<F>(self, f: F) -> Self::Output
355    where
356        F: FnOnce(&mut Plot3DItemStyle),
357    {
358        let mut style = Plot3DItemStyle::default();
359        f(&mut style);
360        StyledPlot3D {
361            inner: self,
362            style,
363            item_flags: crate::Item3DFlags::NONE,
364        }
365    }
366}
367
368impl<'a, T> Plot3DItemFlagged for crate::plots::Image3DByCorners<'a, T>
369where
370    T: Into<TextureRef<'a>> + Copy,
371{
372    type Output = StyledPlot3D<Self>;
373
374    fn map_item_flags<F>(self, f: F) -> Self::Output
375    where
376        F: FnOnce(&mut crate::Item3DFlags),
377    {
378        let mut item_flags = crate::Item3DFlags::NONE;
379        f(&mut item_flags);
380        StyledPlot3D {
381            inner: self,
382            style: Plot3DItemStyle::default(),
383            item_flags,
384        }
385    }
386}
387
388macro_rules! impl_builder_plot3d_item_styled {
389    ($ty:ty) => {
390        impl Plot3DItemStyled for $ty {
391            type Output = Self;
392
393            fn map_style<F>(mut self, f: F) -> Self::Output
394            where
395                F: FnOnce(&mut Plot3DItemStyle),
396            {
397                f(&mut self.style);
398                self
399            }
400        }
401    };
402}
403
404macro_rules! impl_builder_plot3d_item_flagged {
405    ($ty:ty) => {
406        impl Plot3DItemFlagged for $ty {
407            type Output = Self;
408
409            fn map_item_flags<F>(mut self, f: F) -> Self::Output
410            where
411                F: FnOnce(&mut crate::Item3DFlags),
412            {
413                f(&mut self.item_flags);
414                self
415            }
416        }
417    };
418}
419
420impl_builder_plot3d_item_styled!(crate::Surface3DBuilder<'_>);
421impl_builder_plot3d_item_styled!(crate::Image3DByAxesBuilder<'_, '_>);
422impl_builder_plot3d_item_styled!(crate::Image3DByCornersBuilder<'_, '_>);
423impl_builder_plot3d_item_styled!(crate::Mesh3DBuilder<'_>);
424impl_builder_plot3d_item_flagged!(crate::Surface3DBuilder<'_>);
425impl_builder_plot3d_item_flagged!(crate::Image3DByAxesBuilder<'_, '_>);
426impl_builder_plot3d_item_flagged!(crate::Image3DByCornersBuilder<'_, '_>);
427impl_builder_plot3d_item_flagged!(crate::Mesh3DBuilder<'_>);
428
429#[cfg(test)]
430mod tests {
431    use super::{NextItemStyle, Plot3DItemStyle, plot3d_spec_with_style};
432    use crate::{Marker3D, Plot3DDataLayout, Plot3DDataOffset, Plot3DDataStride};
433
434    #[test]
435    fn plot3d_item_style_applies_fields() {
436        let style = Plot3DItemStyle::new()
437            .with_line_color([0.1, 0.2, 0.3, 0.4])
438            .with_line_weight(2.5)
439            .with_fill_color([0.5, 0.6, 0.7, 0.8])
440            .with_fill_alpha(0.35)
441            .with_marker(Marker3D::Auto)
442            .with_marker_size(6.0)
443            .with_marker_line_color([0.9, 0.1, 0.2, 1.0])
444            .with_marker_fill_color([0.3, 0.4, 0.5, 0.6]);
445
446        let layout =
447            Plot3DDataLayout::new(Plot3DDataOffset::samples(7), Plot3DDataStride::bytes(16));
448        let spec = plot3d_spec_with_style(&NextItemStyle::default(), style, 0, layout);
449
450        assert_eq!(spec.LineColor.x, 0.1);
451        assert_eq!(spec.LineColor.y, 0.2);
452        assert_eq!(spec.LineColor.z, 0.3);
453        assert_eq!(spec.LineColor.w, 0.4);
454        assert_eq!(spec.LineWeight, 2.5);
455        assert_eq!(spec.FillColor.x, 0.5);
456        assert_eq!(spec.FillColor.y, 0.6);
457        assert_eq!(spec.FillColor.z, 0.7);
458        assert_eq!(spec.FillColor.w, 0.8);
459        assert_eq!(spec.FillAlpha, 0.35);
460        assert_eq!(spec.Marker, crate::sys::ImPlot3DMarker_Auto as _);
461        assert_eq!(spec.MarkerSize, 6.0);
462        assert_eq!(spec.MarkerLineColor.x, 0.9);
463        assert_eq!(spec.MarkerFillColor.z, 0.5);
464        assert_eq!(spec.Offset, 7);
465        assert_eq!(spec.Stride, 16);
466    }
467
468    #[test]
469    fn scoped_style_consumes_outer_overrides_once_and_preserves_the_next_submission() {
470        let next = NextItemStyle::default();
471        let layout = Plot3DDataLayout::DEFAULT;
472        next.update(|s| {
473            s.FillAlpha = 0.25;
474            s.Flags = crate::Item3DFlags::NO_LEGEND.bits() as _;
475        });
476        next.scoped(
477            |s| s.LineWeight = 3.0,
478            || {
479                let submitted = next.spec(crate::Line3DFlags::SEGMENTS.bits(), layout);
480                assert_eq!((submitted.FillAlpha, submitted.LineWeight), (0.25, 3.0));
481                assert_eq!(
482                    submitted.Flags as u32,
483                    crate::Item3DFlags::NO_LEGEND.bits() | crate::Line3DFlags::SEGMENTS.bits()
484                );
485                next.update(|s| s.MarkerSize = 9.0);
486            },
487        );
488        let following = next.spec(0, layout);
489        assert_eq!(following.MarkerSize, 9.0);
490        assert_eq!(following.FillAlpha, -1.0);
491    }
492}