1use std::sync::{Arc, Mutex};
11
12use glam::Vec3;
13use re_agent::capability::CapabilityRegistry;
14use re_core::transform::Transform;
15use re_ecs::World;
16use serde_json::Value;
17
18use crate::outliner::SharedWorld;
19use crate::project::ProjectDoc;
20use crate::vfx::{register_vfx_tools, SharedVfxDoc};
21use crate::workspace::Workspace;
22use crate::{
23 register_editor_tools, register_import_tools, register_outliner_tools, register_project_tools,
24 register_undo_tools, CommandStack, ComponentRegistry, EditorState, Name, SharedCommandStack,
25 SharedEditorState, SharedProjectDoc, SharedRegistry, Visible,
26};
27
28const UNDO_LIMIT: usize = 128;
30
31pub struct EditorContext {
33 pub state: SharedEditorState,
34 pub undo: SharedCommandStack<EditorState>,
35 pub project: SharedProjectDoc,
36 pub world: SharedWorld,
37 pub registry: SharedRegistry,
38 pub vfx: SharedVfxDoc,
39 pub workspace: Arc<Workspace>,
40 pub capabilities: CapabilityRegistry,
41}
42
43impl EditorContext {
44 pub fn new(start: Vec3) -> Self {
47 let state: SharedEditorState = Arc::new(Mutex::new(EditorState::new(start)));
48 let undo: SharedCommandStack<EditorState> =
49 Arc::new(Mutex::new(CommandStack::new(UNDO_LIMIT)));
50 let project: SharedProjectDoc = Arc::new(Mutex::new(ProjectDoc::new_empty()));
51 let world: SharedWorld = Arc::new(Mutex::new(World::new()));
52 let vfx: SharedVfxDoc = Arc::new(Mutex::new(crate::vfx::VfxDoc::new()));
53
54 let mut reg = ComponentRegistry::new();
55 reg.register::<Transform>("Transform");
56 reg.register::<Name>("Name");
57 reg.register::<Visible>("Visible");
58 let registry: SharedRegistry = Arc::new(reg);
59
60 let mut capabilities = CapabilityRegistry::new();
61 let workspace = Arc::new(Workspace::from_env_or_cwd());
62 register_editor_tools(&mut capabilities, state.clone());
63 register_undo_tools(&mut capabilities, state.clone(), undo.clone());
64 register_project_tools(&mut capabilities, project.clone(), workspace.clone());
65 register_import_tools(&mut capabilities, project.clone(), workspace.clone());
66 register_outliner_tools(&mut capabilities, world.clone(), registry.clone());
67 register_vfx_tools(&mut capabilities, vfx.clone(), workspace.clone());
68
69 Self {
70 state,
71 undo,
72 project,
73 world,
74 registry,
75 vfx,
76 workspace,
77 capabilities,
78 }
79 }
80
81 pub fn has(&self, name: &str) -> bool {
83 self.capabilities.has(name)
84 }
85
86 pub fn call(&self, name: &str, args: Value) -> Result<Value, String> {
89 self.capabilities
90 .call(name, args)
91 .map_err(|e| e.to_string())
92 }
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98 use serde_json::json;
99
100 #[test]
101 fn kontext_adressiert_alle_werkzeugfamilien() {
102 let ctx = EditorContext::new(Vec3::ZERO);
103 for name in [
104 "editor.camera.get",
105 "editor.camera.set",
106 "editor.selection.set",
107 "editor.undo",
108 "editor.redo",
109 "editor.history",
110 "project.new",
111 "project.load",
112 "project.save",
113 "project.info",
114 "import.gltf",
115 "import.obj",
116 "import.preview",
117 "ecs.entities",
118 "ecs.entity.components",
119 "ecs.entity.get",
120 "ecs.entity.set",
121 "vfx.get",
122 "vfx.node.add",
123 "vfx.validate",
124 "vfx.play",
125 "vfx.save",
126 ] {
127 assert!(ctx.has(name), "fehlt: {name}");
128 }
129 }
130
131 #[test]
132 fn kamera_ueber_registry() {
133 let ctx = EditorContext::new(Vec3::ZERO);
134 ctx.call("editor.camera.set", json!({ "x": 3.0, "yaw": 0.4 }))
135 .unwrap();
136 let got = ctx.call("editor.camera.get", json!({})).unwrap();
137 assert_eq!(got["position"]["x"], json!(3.0));
138 let hist = ctx.call("editor.history", json!({})).unwrap();
141 assert_eq!(hist["anzahl"], json!(0));
142 }
143
144 #[test]
145 fn projekt_und_ecs_teilen_den_kontext() {
146 let ctx = EditorContext::new(Vec3::ZERO);
147 ctx.call("project.new", json!({ "name": "Kontext" }))
148 .unwrap();
149 let info = ctx.call("project.info", json!({})).unwrap();
150 assert_eq!(info["name"], json!("Kontext"));
151
152 {
155 let mut w = ctx.world.lock().unwrap();
156 let e = w.spawn();
157 w.insert(e, Transform::default());
158 w.insert(e, Name::new("Einheit"));
159 }
160 let ents = ctx.call("ecs.entities", json!({})).unwrap();
161 assert_eq!(ents["anzahl"], json!(1));
162 let comps = ctx
163 .call("ecs.entity.components", json!({ "entity": 0 }))
164 .unwrap();
165 assert!(comps["components"]
166 .as_array()
167 .unwrap()
168 .contains(&json!("Name")));
169 }
170}