1use std::borrow::Cow;
2
3use dear_imgui_rs::texture::TextureRef;
4
5use crate::item_style::{Plot3DItemStyle, plot3d_spec_with_style};
6use crate::{Image3DFlags, Item3DFlags, Plot3DDataLayout, Plot3DScope, imvec2, imvec4, sys};
7
8pub struct Image3DByAxesBuilder<'ui, 'tex> {
10 pub(crate) _ui: &'ui Plot3DScope<'ui>,
11 pub(crate) label: Cow<'ui, str>,
12 pub(crate) texture: TextureRef<'tex>,
13 pub(crate) center: [f32; 3],
14 pub(crate) axis_u: [f32; 3],
15 pub(crate) axis_v: [f32; 3],
16 pub(crate) uv0: [f32; 2],
17 pub(crate) uv1: [f32; 2],
18 pub(crate) tint: [f32; 4],
19 pub(crate) flags: Image3DFlags,
20 pub(crate) item_flags: Item3DFlags,
21 pub(crate) style: Plot3DItemStyle,
22}
23
24impl<'ui, 'tex> Image3DByAxesBuilder<'ui, 'tex> {
25 pub fn uv(mut self, uv0: [f32; 2], uv1: [f32; 2]) -> Self {
26 self.uv0 = uv0;
27 self.uv1 = uv1;
28 self
29 }
30 pub fn tint(mut self, col: [f32; 4]) -> Self {
31 self.tint = col;
32 self
33 }
34 pub fn flags(mut self, flags: Image3DFlags) -> Self {
35 self.flags = flags;
36 self
37 }
38 pub fn plot(self) {
39 self._ui.with_bound_context(|| {
40 let tex_ref = to_sys_texture_ref(self._ui, self.texture);
41 let label = self.label.as_ref();
42 let label = if label.contains('\0') { "image" } else { label };
43 dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
44 let spec = plot3d_spec_with_style(
45 &self._ui.next_item,
46 self.style,
47 self.flags.bits() | self.item_flags.bits(),
48 Plot3DDataLayout::DEFAULT,
49 );
50 sys::ImPlot3D_PlotImage_Vec2(
51 label_ptr,
52 tex_ref,
53 sys::ImPlot3DPoint_c {
54 x: self.center[0] as f64,
55 y: self.center[1] as f64,
56 z: self.center[2] as f64,
57 },
58 sys::ImPlot3DPoint_c {
59 x: self.axis_u[0] as f64,
60 y: self.axis_u[1] as f64,
61 z: self.axis_u[2] as f64,
62 },
63 sys::ImPlot3DPoint_c {
64 x: self.axis_v[0] as f64,
65 y: self.axis_v[1] as f64,
66 z: self.axis_v[2] as f64,
67 },
68 imvec2(self.uv0[0], self.uv0[1]),
69 imvec2(self.uv1[0], self.uv1[1]),
70 imvec4(self.tint[0], self.tint[1], self.tint[2], self.tint[3]),
71 spec,
72 );
73 })
74 })
75 }
76}
77
78pub struct Image3DByCornersBuilder<'ui, 'tex> {
80 pub(crate) _ui: &'ui Plot3DScope<'ui>,
81 pub(crate) label: Cow<'ui, str>,
82 pub(crate) texture: TextureRef<'tex>,
83 pub(crate) p0: [f32; 3],
84 pub(crate) p1: [f32; 3],
85 pub(crate) p2: [f32; 3],
86 pub(crate) p3: [f32; 3],
87 pub(crate) uv0: [f32; 2],
88 pub(crate) uv1: [f32; 2],
89 pub(crate) uv2: [f32; 2],
90 pub(crate) uv3: [f32; 2],
91 pub(crate) tint: [f32; 4],
92 pub(crate) flags: Image3DFlags,
93 pub(crate) item_flags: Item3DFlags,
94 pub(crate) style: Plot3DItemStyle,
95}
96
97impl<'ui, 'tex> Image3DByCornersBuilder<'ui, 'tex> {
98 pub fn uvs(mut self, uv0: [f32; 2], uv1: [f32; 2], uv2: [f32; 2], uv3: [f32; 2]) -> Self {
99 self.uv0 = uv0;
100 self.uv1 = uv1;
101 self.uv2 = uv2;
102 self.uv3 = uv3;
103 self
104 }
105 pub fn tint(mut self, col: [f32; 4]) -> Self {
106 self.tint = col;
107 self
108 }
109 pub fn flags(mut self, flags: Image3DFlags) -> Self {
110 self.flags = flags;
111 self
112 }
113 pub fn plot(self) {
114 self._ui.with_bound_context(|| {
115 let tex_ref = to_sys_texture_ref(self._ui, self.texture);
116 let label = self.label.as_ref();
117 let label = if label.contains('\0') { "image" } else { label };
118 dear_imgui_rs::with_scratch_txt(label, |label_ptr| unsafe {
119 let spec = plot3d_spec_with_style(
120 &self._ui.next_item,
121 self.style,
122 self.flags.bits() | self.item_flags.bits(),
123 Plot3DDataLayout::DEFAULT,
124 );
125 sys::ImPlot3D_PlotImage_Plot3DPoint(
126 label_ptr,
127 tex_ref,
128 sys::ImPlot3DPoint_c {
129 x: self.p0[0] as f64,
130 y: self.p0[1] as f64,
131 z: self.p0[2] as f64,
132 },
133 sys::ImPlot3DPoint_c {
134 x: self.p1[0] as f64,
135 y: self.p1[1] as f64,
136 z: self.p1[2] as f64,
137 },
138 sys::ImPlot3DPoint_c {
139 x: self.p2[0] as f64,
140 y: self.p2[1] as f64,
141 z: self.p2[2] as f64,
142 },
143 sys::ImPlot3DPoint_c {
144 x: self.p3[0] as f64,
145 y: self.p3[1] as f64,
146 z: self.p3[2] as f64,
147 },
148 imvec2(self.uv0[0], self.uv0[1]),
149 imvec2(self.uv1[0], self.uv1[1]),
150 imvec2(self.uv2[0], self.uv2[1]),
151 imvec2(self.uv3[0], self.uv3[1]),
152 imvec4(self.tint[0], self.tint[1], self.tint[2], self.tint[3]),
153 spec,
154 );
155 })
156 })
157 }
158}
159
160impl<'ui> Plot3DScope<'ui> {
161 pub fn image_by_axes<'tex, T: Into<TextureRef<'tex>>>(
163 &'ui self,
164 label: impl Into<Cow<'ui, str>>,
165 tex: T,
166 center: [f32; 3],
167 axis_u: [f32; 3],
168 axis_v: [f32; 3],
169 ) -> Image3DByAxesBuilder<'ui, 'tex> {
170 Image3DByAxesBuilder {
171 _ui: self,
172 label: label.into(),
173 texture: tex.into(),
174 center,
175 axis_u,
176 axis_v,
177 uv0: [0.0, 0.0],
178 uv1: [1.0, 1.0],
179 tint: [1.0, 1.0, 1.0, 1.0],
180 flags: Image3DFlags::NONE,
181 item_flags: Item3DFlags::NONE,
182 style: Plot3DItemStyle::default(),
183 }
184 }
185
186 pub fn image_by_corners<'tex, T: Into<TextureRef<'tex>>>(
188 &'ui self,
189 label: impl Into<Cow<'ui, str>>,
190 tex: T,
191 p0: [f32; 3],
192 p1: [f32; 3],
193 p2: [f32; 3],
194 p3: [f32; 3],
195 ) -> Image3DByCornersBuilder<'ui, 'tex> {
196 Image3DByCornersBuilder {
197 _ui: self,
198 label: label.into(),
199 texture: tex.into(),
200 p0,
201 p1,
202 p2,
203 p3,
204 uv0: [0.0, 0.0],
205 uv1: [1.0, 0.0],
206 uv2: [1.0, 1.0],
207 uv3: [0.0, 1.0],
208 tint: [1.0, 1.0, 1.0, 1.0],
209 flags: Image3DFlags::NONE,
210 item_flags: Item3DFlags::NONE,
211 style: Plot3DItemStyle::default(),
212 }
213 }
214}
215
216fn to_sys_texture_ref(ui: &Plot3DScope<'_>, texture: TextureRef<'_>) -> sys::ImTextureRef_c {
217 let texture = unsafe { ui.ui.imgui.resolve_texture_ref_raw(texture) }
218 .unwrap_or_else(|error| panic!("ImPlot3D image rejected texture: {error}"));
219 sys::ImTextureRef_c {
220 _TexData: texture._TexData as *mut sys::ImTextureData,
221 _TexID: texture._TexID as sys::ImTextureID,
222 }
223}