1pub mod context;
20pub mod import;
21pub mod layout;
22pub mod outliner;
23pub mod panels;
24pub mod project;
25pub mod undo;
26pub mod vfx;
27pub mod workspace;
28
29pub use context::EditorContext;
30pub use import::{import_gltf, import_summary, register_import_tools, ImportResult};
31pub use layout::{activate_tab, Command as LayoutCommand, LayoutManifest, Menu, Preset, Shortcut};
32pub use outliner::{
33 register_outliner_tools, ComponentRegistry, Name, Outliner, SharedRegistry, Visible,
34};
35pub use panels::PanelState;
36pub use project::{register_project_tools, ProjectDoc, SharedProjectDoc};
37pub use undo::{
38 register_undo_tools, Command, CommandStack, SetCameraCommand, SetSelectionCommand,
39 SetViewModeCommand, SharedCommandStack,
40};
41pub use vfx::{register_vfx_tools, AnzeigeKnoten, SharedVfxDoc, VfxDoc, VORSCHAU_DT};
42pub use workspace::Workspace;
43
44use std::sync::{Arc, Mutex, MutexGuard};
45
46use glam::{Mat4, Vec3};
47use re_agent::capability::{CapabilityRegistry, ToolSpec};
48use re_render::Camera;
49use serde_json::json;
50
51#[derive(Clone, Debug)]
55pub struct EditorCamera {
56 pub position: Vec3,
57 pub yaw: f32,
58 pub pitch: f32,
59}
60
61impl EditorCamera {
62 pub fn new(position: Vec3) -> Self {
63 Self {
64 position,
65 yaw: 0.0,
66 pitch: 0.0,
67 }
68 }
69
70 pub fn forward(&self) -> Vec3 {
72 Vec3::new(
73 self.pitch.cos() * self.yaw.sin(),
74 self.pitch.sin(),
75 self.pitch.cos() * self.yaw.cos(),
76 )
77 }
78
79 pub fn right(&self) -> Vec3 {
80 self.forward().cross(Vec3::Y).normalize_or_zero()
81 }
82
83 pub fn up(&self) -> Vec3 {
84 Vec3::Y
85 }
86
87 pub fn translate(&mut self, delta: Vec3) {
89 self.position += delta;
90 }
91
92 pub fn look(&mut self, dyaw: f32, dpitch: f32) {
94 self.yaw += dyaw;
95 self.pitch = (self.pitch + dpitch).clamp(-1.55, 1.55);
96 }
97
98 pub fn camera(&self, width: u32, height: u32) -> Camera {
100 let mut cam = Camera::perspective(
101 self.position,
102 self.position + self.forward(),
103 Vec3::Y,
104 60f32.to_radians(),
105 width.max(1) as f32 / height.max(1) as f32,
106 );
107 cam.clear_color = [0.52, 0.68, 0.85, 1.0];
108 cam
109 }
110
111 pub fn view_matrix(&self) -> Mat4 {
113 Mat4::look_at_rh(self.position, self.position + self.forward(), Vec3::Y)
114 }
115}
116
117#[derive(Clone, Debug, Default)]
119pub struct Selection {
120 ids: std::collections::BTreeSet<usize>,
121}
122
123impl Selection {
124 pub fn new() -> Self {
125 Self::default()
126 }
127
128 pub fn add(&mut self, id: usize) {
129 self.ids.insert(id);
130 }
131
132 pub fn remove(&mut self, id: usize) {
133 self.ids.remove(&id);
134 }
135
136 pub fn toggle(&mut self, id: usize) {
137 if self.ids.contains(&id) {
138 self.ids.remove(&id);
139 } else {
140 self.ids.insert(id);
141 }
142 }
143
144 pub fn clear(&mut self) {
145 self.ids.clear();
146 }
147
148 pub fn contains(&self, id: usize) -> bool {
149 self.ids.contains(&id)
150 }
151
152 pub fn ids(&self) -> Vec<usize> {
153 self.ids.iter().copied().collect()
154 }
155
156 pub fn len(&self) -> usize {
157 self.ids.len()
158 }
159
160 pub fn is_empty(&self) -> bool {
161 self.ids.is_empty()
162 }
163}
164
165#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
167pub enum ViewMode {
168 #[default]
170 Fly,
171 Orbit,
173}
174
175#[derive(Clone, Debug)]
177pub struct EditorState {
178 pub camera: EditorCamera,
179 pub selection: Selection,
180 pub view_mode: ViewMode,
181 pub frame_count: u64,
182}
183
184impl EditorState {
185 pub fn new(start: Vec3) -> Self {
186 Self {
187 camera: EditorCamera::new(start),
188 selection: Selection::new(),
189 view_mode: ViewMode::default(),
190 frame_count: 0,
191 }
192 }
193
194 pub fn tick(&mut self) {
195 self.frame_count += 1;
196 }
197}
198
199pub type SharedEditorState = Arc<Mutex<EditorState>>;
201
202fn lock(state: &SharedEditorState) -> Result<MutexGuard<'_, EditorState>, String> {
203 state
204 .lock()
205 .map_err(|_| "Editor-Sperre vergiftet".to_string())
206}
207
208pub fn register_editor_tools(reg: &mut CapabilityRegistry, state: SharedEditorState) {
211 {
212 let s = Arc::clone(&state);
213 reg.register(
214 ToolSpec::new(
215 "editor.camera.get",
216 "Aktuelle Editor-Kameraposition und Blickrichtung",
217 json!({ "typ": "object", "felder": {} }),
218 ),
219 move |_| {
220 let st = lock(&s)?;
221 Ok(json!({
222 "position": { "x": st.camera.position.x, "y": st.camera.position.y, "z": st.camera.position.z },
223 "yaw": st.camera.yaw,
224 "pitch": st.camera.pitch,
225 "forward": { "x": st.camera.forward().x, "y": st.camera.forward().y, "z": st.camera.forward().z },
226 }))
227 },
228 );
229 }
230 {
231 let s = Arc::clone(&state);
232 reg.register(
233 ToolSpec::new(
234 "editor.camera.set",
235 "Editor-Kamera setzen (Position + Blickwinkel)",
236 json!({ "typ": "object", "felder": {
237 "x": { "typ": "number" }, "y": { "typ": "number" }, "z": { "typ": "number" },
238 "yaw": { "typ": "number" }, "pitch": { "typ": "number" }
239 } }),
240 ),
241 move |args| {
242 let mut st = lock(&s)?;
243 if let Some(v) = args.get("x").and_then(|v| v.as_f64()) {
244 st.camera.position.x = v as f32;
245 }
246 if let Some(v) = args.get("y").and_then(|v| v.as_f64()) {
247 st.camera.position.y = v as f32;
248 }
249 if let Some(v) = args.get("z").and_then(|v| v.as_f64()) {
250 st.camera.position.z = v as f32;
251 }
252 if let Some(v) = args.get("yaw").and_then(|v| v.as_f64()) {
253 st.camera.yaw = v as f32;
254 }
255 if let Some(v) = args.get("pitch").and_then(|v| v.as_f64()) {
256 st.camera.pitch = v as f32;
257 }
258 Ok(json!({ "ok": true }))
259 },
260 );
261 }
262 {
263 let s = Arc::clone(&state);
264 reg.register(
265 ToolSpec::new(
266 "editor.selection.set",
267 "Selektion ersetzen (Liste von Instanz-IDs)",
268 json!({ "typ": "object", "felder": { "ids": { "typ": "array" } } }),
269 ),
270 move |args| {
271 let mut st = lock(&s)?;
272 st.selection.clear();
273 if let Some(arr) = args.get("ids").and_then(|v| v.as_array()) {
274 for v in arr {
275 if let Some(id) = v.as_u64() {
276 st.selection.add(id as usize);
277 }
278 }
279 }
280 Ok(json!({ "anzahl": st.selection.len() }))
281 },
282 );
283 }
284 {
285 let s = Arc::clone(&state);
286 reg.register(
287 ToolSpec::simple("editor.selection.clear", "Selektion leeren"),
288 move |_| {
289 let mut st = lock(&s)?;
290 st.selection.clear();
291 Ok(json!({ "ok": true }))
292 },
293 );
294 }
295 {
296 let s = Arc::clone(&state);
297 reg.register(
298 ToolSpec::simple("editor.stats", "Editor-Zustand: Frames, Kamera, Selektion"),
299 move |_| {
300 let st = lock(&s)?;
301 Ok(json!({
302 "frames": st.frame_count,
303 "kamera": { "x": st.camera.position.x, "y": st.camera.position.y, "z": st.camera.position.z },
304 "selektion": st.selection.ids(),
305 "modus": format!("{:?}", st.view_mode),
306 }))
307 },
308 );
309 }
310}
311
312#[cfg(test)]
313mod tests {
314 use super::*;
315 use re_agent::CapabilityRegistry;
316 use serde_json::json;
317
318 #[test]
319 fn kamera_vorwaerts_und_blick_sind_deterministisch() {
320 let mut c = EditorCamera::new(Vec3::ZERO);
321 c.look(0.5, 0.2);
322 let f1 = c.forward();
323 c.translate(c.forward() * 3.0);
324 assert_eq!(c.position, f1 * 3.0);
325 assert_eq!(c.forward(), f1);
327 }
328
329 #[test]
330 fn pitch_wird_begrenzt() {
331 let mut c = EditorCamera::new(Vec3::ZERO);
332 c.look(0.0, 10.0);
333 assert!(c.pitch <= 1.55);
334 c.look(0.0, -10.0);
335 assert!(c.pitch >= -1.55);
336 }
337
338 #[test]
339 fn selektion_toggle_und_clear() {
340 let mut s = Selection::new();
341 s.toggle(3);
342 assert!(s.contains(3));
343 s.toggle(3);
344 assert!(!s.contains(3));
345 s.add(1);
346 s.add(2);
347 assert_eq!(s.ids(), vec![1, 2]);
348 s.clear();
349 assert!(s.is_empty());
350 }
351
352 #[test]
353 fn capabilities_runden_trip() {
354 let state = Arc::new(Mutex::new(EditorState::new(Vec3::new(1.0, 2.0, 3.0))));
355 let mut reg = CapabilityRegistry::new();
356 register_editor_tools(&mut reg, Arc::clone(&state));
357 for name in [
358 "editor.camera.get",
359 "editor.camera.set",
360 "editor.selection.set",
361 "editor.selection.clear",
362 "editor.stats",
363 ] {
364 assert!(reg.has(name), "fehlt: {name}");
365 }
366 let got = reg.call("editor.camera.get", json!({})).unwrap();
367 assert_eq!(got["position"]["x"], json!(1.0));
368 reg.call("editor.camera.set", json!({ "x": 5.0, "yaw": 0.7 }))
369 .unwrap();
370 let got = reg.call("editor.camera.get", json!({})).unwrap();
371 assert_eq!(got["position"]["x"], json!(5.0));
372 assert!((got["yaw"].as_f64().unwrap() - 0.7).abs() < 1e-5);
373 reg.call("editor.selection.set", json!({ "ids": [1, 2, 2] }))
374 .unwrap();
375 let st = reg.call("editor.stats", json!({})).unwrap();
376 assert_eq!(st["selektion"], json!([1, 2]));
377 reg.call("editor.selection.clear", json!({})).unwrap();
378 assert_eq!(
379 reg.call("editor.stats", json!({})).unwrap()["selektion"],
380 json!([])
381 );
382 }
383
384 #[test]
385 fn view_matrix_ist_endlich() {
386 let c = EditorCamera::new(Vec3::new(1.0, 2.0, 3.0));
387 let m = c.view_matrix();
388 for col in 0..4 {
389 for row in 0..4 {
390 assert!(m.col(col)[row].is_finite());
391 }
392 }
393 }
394}