Skip to main content

dear_implot3d/style/
palette.rs

1use crate::flags::Marker3D;
2use crate::sys;
3use crate::{Plot3DContext, Plot3DUi};
4
5use super::tokens::{StyleColorToken, StyleVarToken};
6use super::types::{Plot3DColorElement, Plot3DStyleVar};
7use std::marker::PhantomData;
8
9impl Plot3DContext {
10    #[inline]
11    fn with_bound_palette<R>(&self, _caller: &str, f: impl FnOnce() -> R) -> R {
12        self.binding().with_bound_context(|| f())
13    }
14
15    /// Apply ImPlot3D's dark style palette to this context.
16    #[inline]
17    pub fn style_colors_dark(&self) {
18        self.with_bound_palette(
19            "dear-implot3d: Plot3DContext::style_colors_dark()",
20            || unsafe { sys::ImPlot3D_StyleColorsDark(std::ptr::null_mut()) },
21        )
22    }
23
24    /// Apply ImPlot3D's light style palette to this context.
25    #[inline]
26    pub fn style_colors_light(&self) {
27        self.with_bound_palette(
28            "dear-implot3d: Plot3DContext::style_colors_light()",
29            || unsafe { sys::ImPlot3D_StyleColorsLight(std::ptr::null_mut()) },
30        )
31    }
32
33    /// Apply ImPlot3D's classic style palette to this context.
34    #[inline]
35    pub fn style_colors_classic(&self) {
36        self.with_bound_palette(
37            "dear-implot3d: Plot3DContext::style_colors_classic()",
38            || unsafe { sys::ImPlot3D_StyleColorsClassic(std::ptr::null_mut()) },
39        )
40    }
41
42    /// Apply ImPlot3D's auto style palette to this context.
43    #[inline]
44    pub fn style_colors_auto(&self) {
45        self.with_bound_palette(
46            "dear-implot3d: Plot3DContext::style_colors_auto()",
47            || unsafe { sys::ImPlot3D_StyleColorsAuto(std::ptr::null_mut()) },
48        )
49    }
50}
51
52impl<'ui> Plot3DUi<'ui> {
53    /// Push a style color override to this ImPlot3D context's stack.
54    #[inline]
55    pub fn push_style_color(
56        &self,
57        element: Plot3DColorElement,
58        col: [f32; 4],
59    ) -> StyleColorToken<'_> {
60        self.binding.with_bound_context(|| {
61            unsafe {
62                sys::ImPlot3D_PushStyleColor_Vec4(
63                    element as sys::ImPlot3DCol,
64                    crate::imvec4(col[0], col[1], col[2], col[3]),
65                );
66            }
67            StyleColorToken {
68                binding: self.binding.clone(),
69                was_popped: false,
70                _lifetime: PhantomData,
71                _not_send_or_sync: PhantomData,
72            }
73        })
74    }
75
76    /// Push a typed style color override.
77    #[inline]
78    pub fn push_style_color_element(
79        &self,
80        element: Plot3DColorElement,
81        col: [f32; 4],
82    ) -> StyleColorToken<'_> {
83        self.push_style_color(element, col)
84    }
85
86    /// Push a style variable (float variant).
87    #[inline]
88    pub fn push_style_var_f32(&self, var: Plot3DStyleVar, val: f32) -> StyleVarToken<'_> {
89        self.binding.with_bound_context(|| {
90            unsafe { sys::ImPlot3D_PushStyleVar_Float(var as sys::ImPlot3DStyleVar, val) }
91            StyleVarToken {
92                binding: self.binding.clone(),
93                was_popped: false,
94                _lifetime: PhantomData,
95                _not_send_or_sync: PhantomData,
96            }
97        })
98    }
99
100    /// Push the default marker style variable.
101    #[inline]
102    pub fn push_style_var_marker(&self, marker: Marker3D) -> StyleVarToken<'_> {
103        self.binding.with_bound_context(|| {
104            unsafe {
105                sys::ImPlot3D_PushStyleVar_Int(
106                    Plot3DStyleVar::Marker as sys::ImPlot3DStyleVar,
107                    marker as sys::ImPlot3DMarker,
108                )
109            }
110            StyleVarToken {
111                binding: self.binding.clone(),
112                was_popped: false,
113                _lifetime: PhantomData,
114                _not_send_or_sync: PhantomData,
115            }
116        })
117    }
118
119    /// Push a style variable (Vec2 variant).
120    #[inline]
121    pub fn push_style_var_vec2(&self, var: Plot3DStyleVar, val: [f32; 2]) -> StyleVarToken<'_> {
122        self.binding.with_bound_context(|| {
123            unsafe {
124                sys::ImPlot3D_PushStyleVar_Vec2(
125                    var as sys::ImPlot3DStyleVar,
126                    crate::imvec2(val[0], val[1]),
127                )
128            }
129            StyleVarToken {
130                binding: self.binding.clone(),
131                was_popped: false,
132                _lifetime: PhantomData,
133                _not_send_or_sync: PhantomData,
134            }
135        })
136    }
137}
138
139impl Plot3DUi<'_> {
140    /// Set the line style for the next ImPlot3D item submitted through this context.
141    #[inline]
142    pub fn set_next_line_style(&self, col: [f32; 4], weight: f32) {
143        self.with_bound_context(|| {
144            crate::update_next_plot3d_spec(|spec| {
145                spec.LineColor = crate::imvec4(col[0], col[1], col[2], col[3]);
146                spec.LineWeight = weight;
147            })
148        })
149    }
150
151    /// Set the fill style for the next ImPlot3D item submitted through this context.
152    #[inline]
153    pub fn set_next_fill_style(&self, col: [f32; 4], alpha_mod: f32) {
154        self.with_bound_context(|| {
155            crate::update_next_plot3d_spec(|spec| {
156                spec.FillColor = crate::imvec4(col[0], col[1], col[2], col[3]);
157                spec.FillAlpha = alpha_mod;
158            })
159        })
160    }
161
162    /// Set the marker style for the next ImPlot3D item submitted through this context.
163    #[inline]
164    pub fn set_next_marker_style(
165        &self,
166        marker: Marker3D,
167        size: f32,
168        fill: [f32; 4],
169        weight: f32,
170        outline: [f32; 4],
171    ) {
172        self.with_bound_context(|| {
173            crate::update_next_plot3d_spec(|spec| {
174                spec.Marker = marker as sys::ImPlot3DMarker;
175                spec.MarkerSize = size;
176                spec.MarkerFillColor = crate::imvec4(fill[0], fill[1], fill[2], fill[3]);
177                spec.MarkerLineColor =
178                    crate::imvec4(outline[0], outline[1], outline[2], outline[3]);
179                spec.LineWeight = weight;
180            })
181        })
182    }
183}