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