1#![allow(non_snake_case)]
4
5use std::rc::Rc;
6use std::sync::Arc;
7use strata_dom::{Attribute, ClassList, NodeBlueprint, Tag, View};
8use strata_macro::{component, rsx};
9use strata_render::orbit_controls::OrbitControls;
10pub use strata_render::scene3d::{
11 AmbientLight, BoxGeometry, Camera3D, Canvas3D, Canvas3DBuilder, CylinderGeometry,
12 DirectionalLight, Geometry3D, IntoScene3DProp, Light3D, Material3D, Mesh, Mesh3D,
13 OrthographicCamera, PerspectiveCamera, PlaneGeometry, PointLight, Scene3D, ShaderMaterial,
14 ShaderSource, SphereGeometry, StandardMaterial, Transform3D,
15};
16
17pub use strata_render::scene3d::IntoCameraChangeProp;
18
19pub fn SceneViewport(
25 scene: Option<Scene3D>,
26 width: Option<f32>,
27 height: Option<f32>,
28 class: Option<&'static str>,
29 on_camera_change: Option<Rc<dyn Fn(&OrbitControls)>>,
30 children: Option<Vec<NodeBlueprint>>,
31) -> View {
32 build_scene_viewport(
33 scene.map(Arc::new),
34 width,
35 height,
36 class.map(String::from),
37 None,
38 on_camera_change,
39 children.unwrap_or_default(),
40 )
41}
42
43#[allow(non_snake_case)]
44pub struct SceneViewport {}
45
46impl SceneViewport {
47 pub fn builder() -> SceneViewportBuilder {
48 SceneViewportBuilder::default()
49 }
50}
51
52#[derive(Default)]
53pub struct SceneViewportBuilder {
54 pub scene: Option<Arc<Scene3D>>,
55 pub width: Option<f32>,
56 pub height: Option<f32>,
57 pub class: Option<String>,
58 pub style: Option<String>,
59 pub on_camera_change: Option<Rc<dyn Fn(&OrbitControls)>>,
60 pub children: Option<Vec<NodeBlueprint>>,
61}
62
63impl SceneViewportBuilder {
64 pub fn scene(mut self, scene: impl IntoScene3DProp) -> Self {
65 self.scene = scene.into_scene3d_prop();
66 self
67 }
68
69 pub fn width(mut self, width: impl Into<Option<f32>>) -> Self {
70 self.width = width.into();
71 self
72 }
73
74 pub fn height(mut self, height: impl Into<Option<f32>>) -> Self {
75 self.height = height.into();
76 self
77 }
78
79 pub fn class(mut self, class: impl Into<String>) -> Self {
80 self.class = Some(class.into());
81 self
82 }
83
84 pub fn style(mut self, style: impl Into<String>) -> Self {
85 self.style = Some(style.into());
86 self
87 }
88
89 pub fn on_camera_change(mut self, cb: impl IntoCameraChangeProp) -> Self {
90 self.on_camera_change = cb.into_camera_change_prop();
91 self
92 }
93
94 pub fn on_camera_change_fn(mut self, cb: impl Fn(&OrbitControls) + 'static) -> Self {
95 self.on_camera_change = Some(Rc::new(cb));
96 self
97 }
98
99 pub fn children(mut self, children: Vec<NodeBlueprint>) -> Self {
100 self.children = Some(children);
101 self
102 }
103
104 pub fn build(self) -> View {
105 build_scene_viewport(
106 self.scene,
107 self.width,
108 self.height,
109 self.class,
110 self.style,
111 self.on_camera_change,
112 self.children.unwrap_or_default(),
113 )
114 }
115}
116
117fn build_scene_viewport(
118 scene: Option<Arc<Scene3D>>,
119 width: Option<f32>,
120 height: Option<f32>,
121 class: Option<String>,
122 extra_style: Option<String>,
123 on_camera_change: Option<Rc<dyn Fn(&OrbitControls)>>,
124 hud_children: Vec<NodeBlueprint>,
125) -> View {
126 let custom = class.as_deref().unwrap_or("");
127
128 let mut style_str = String::new();
130 if let Some(w) = width {
131 style_str.push_str(&format!("width: {}px; ", w));
132 }
133 if let Some(h) = height {
134 style_str.push_str(&format!("height: {}px; ", h));
135 }
136 if let Some(s) = extra_style {
137 style_str.push_str(&s);
138 }
139
140 let mut canvas_attrs = Vec::with_capacity(6);
142 if let Some(ref scene_arc) = scene {
143 canvas_attrs.push(Attribute::property("data-strata-canvas3d", "true"));
144 let cloned: Arc<Scene3D> = Arc::clone(scene_arc);
145 let payload: Arc<dyn std::any::Any + Send + Sync> = cloned;
146 canvas_attrs.push(Attribute::scene_payload_arc(payload));
147 }
148
149 if let Some(cb) = on_camera_change {
150 strata_render::scene3d::attach_orbit_controls_listeners(
151 &mut canvas_attrs,
152 scene.as_deref(),
153 width,
154 height,
155 cb,
156 );
157 }
158
159 let canvas_backing = NodeBlueprint::element(
160 Tag::Canvas,
161 ClassList::from("absolute inset-0 w-full h-full z-0"),
162 canvas_attrs,
163 Vec::new(),
164 );
165
166 let mut root_attrs = Vec::new();
167 if !style_str.trim().is_empty() {
168 root_attrs.push(Attribute::property("style", style_str.trim()));
169 }
170
171 let mut children = Vec::with_capacity(1 + hud_children.len());
175 children.push(canvas_backing);
176 children.extend(hud_children);
177
178 View::new(NodeBlueprint::element(
179 Tag::Div,
180 ClassList::from(
181 format!("relative {}", custom)
182 .split_whitespace()
183 .map(String::from)
184 .collect::<Vec<String>>(),
185 ),
186 root_attrs,
187 children,
188 ))
189}
190
191#[component]
193pub fn HudCard(
194 title: &'static str,
195 class: Option<&'static str>,
196 children: Option<Vec<NodeBlueprint>>,
197) -> View {
198 let custom = class.unwrap_or("");
199 let card_children = children.unwrap_or_default();
200
201 rsx! {
202 <div class={format!("pointer-events-auto backdrop-blur-md bg-neutral-900/75 border border-neutral-700/60 rounded-xl p-3 shadow-lg text-neutral-100 {}", custom)}>
203 <div class="text-xs font-semibold text-neutral-400 uppercase tracking-wider mb-1">
204 {title}
205 </div>
206 {card_children}
207 </div>
208 }
209}