EditorScene.as 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. // Urho3D editor scene handling
  2. #include "Scripts/Editor/EditorSceneWindow.as"
  3. #include "Scripts/Editor/EditorNodeWindow.as"
  4. const int PICK_GEOMETRIES = 0;
  5. const int PICK_LIGHTS = 1;
  6. const int PICK_ZONES = 2;
  7. const int PICK_RIGIDBODIES = 3;
  8. const int MAX_PICK_MODES = 4;
  9. Scene@ editorScene;
  10. String sceneFileName;
  11. String instantiateFileName;
  12. CreateMode instantiateMode = REPLICATED;
  13. bool sceneModified = false;
  14. bool runUpdate = false;
  15. Array<Node@> selectedNodes;
  16. Array<Component@> selectedComponents;
  17. Node@ editNode;
  18. Array<Node@> editNodes;
  19. Array<Component@> editComponents;
  20. uint numEditableComponentsPerNode = 1;
  21. Array<XMLFile@> copyBuffer;
  22. bool copyBufferLocal = false;
  23. bool inSelectionModify = false;
  24. void ClearSelection()
  25. {
  26. selectedNodes.Clear();
  27. selectedComponents.Clear();
  28. editNode = null;
  29. editNodes.Clear();
  30. editComponents.Clear();
  31. numEditableComponentsPerNode = 1;
  32. HideGizmo();
  33. }
  34. void CreateScene()
  35. {
  36. // Create a scene only once here
  37. editorScene = Scene("");
  38. // Allow access to the scene from the console
  39. script.defaultScene = editorScene;
  40. // Always pause the scene, and do updates manually
  41. editorScene.active = false;
  42. // Camera is not bounded to a scene but still need to be created once here
  43. CreateCamera();
  44. }
  45. void ResetScene()
  46. {
  47. ClearSelection();
  48. suppressSceneChanges = true;
  49. // Create a scene with default values, these will be overridden when loading scenes
  50. editorScene.Clear();
  51. editorScene.name = "";
  52. Octree@ octree = editorScene.CreateComponent("Octree");
  53. PhysicsWorld@ physicsWorld = editorScene.CreateComponent("PhysicsWorld");
  54. octree.Resize(BoundingBox(-1000.0, 1000.0), 8);
  55. editorScene.CreateComponent("DebugRenderer");
  56. UpdateSceneWindow();
  57. UpdateNodeWindow();
  58. suppressSceneChanges = false;
  59. runUpdate = false;
  60. sceneFileName = "";
  61. UpdateWindowTitle();
  62. ResetCamera();
  63. CreateGizmo();
  64. }
  65. void SetResourcePath(String newPath, bool usePreferredDir = true)
  66. {
  67. if (newPath.empty)
  68. return;
  69. if (usePreferredDir)
  70. newPath = AddTrailingSlash(cache.GetPreferredResourceDir(newPath));
  71. else
  72. newPath = AddTrailingSlash(newPath);
  73. if (newPath == sceneResourcePath)
  74. return;
  75. cache.ReleaseAllResources(false);
  76. renderer.ReloadShaders();
  77. // Remove the old scene resource path if any. However make sure that the default data paths do not get removed
  78. if (!sceneResourcePath.empty && !sceneResourcePath.Contains(fileSystem.programDir))
  79. cache.RemoveResourceDir(sceneResourcePath);
  80. cache.AddResourceDir(newPath);
  81. sceneResourcePath = newPath;
  82. }
  83. bool LoadScene(const String&in fileName)
  84. {
  85. if (fileName.empty)
  86. return false;
  87. ui.cursor.shape = CS_BUSY;
  88. // Always load the scene from the filesystem, not from resource paths
  89. if (!fileSystem.FileExists(fileName))
  90. {
  91. log.Error("No such scene: " + fileName);
  92. return false;
  93. }
  94. File file(fileName, FILE_READ);
  95. if (!file.open)
  96. return false;
  97. // Clear the old scene
  98. suppressSceneChanges = true;
  99. ClearSelection();
  100. editorScene.Clear();
  101. // Add the new resource path
  102. SetResourcePath(GetPath(fileName));
  103. String extension = GetExtension(fileName);
  104. bool loaded;
  105. if (extension != ".xml")
  106. loaded = editorScene.Load(file);
  107. else
  108. loaded = editorScene.LoadXML(file);
  109. // Always pause the scene, and do updates manually
  110. editorScene.active = false;
  111. sceneFileName = fileName;
  112. sceneModified = false;
  113. runUpdate = false;
  114. UpdateWindowTitle();
  115. UpdateSceneWindow();
  116. UpdateNodeWindow();
  117. suppressSceneChanges = false;
  118. ResetCamera();
  119. CreateGizmo();
  120. return loaded;
  121. }
  122. void SaveScene(const String&in fileName)
  123. {
  124. if (fileName.empty || GetFileName(fileName).empty)
  125. return;
  126. // Unpause when saving so that the scene will work properly when loaded outside the editor
  127. editorScene.active = true;
  128. File file(fileName, FILE_WRITE);
  129. String extension = GetExtension(fileName);
  130. if (extension != ".xml")
  131. editorScene.Save(file);
  132. else
  133. editorScene.SaveXML(file);
  134. editorScene.active = false;
  135. sceneFileName = fileName;
  136. sceneModified = false;
  137. UpdateWindowTitle();
  138. }
  139. void LoadNode(const String&in fileName)
  140. {
  141. if (fileName.empty)
  142. return;
  143. if (!fileSystem.FileExists(fileName))
  144. {
  145. log.Error("No such node file " + fileName);
  146. return;
  147. }
  148. File file(fileName, FILE_READ);
  149. if (!file.open)
  150. return;
  151. // Before instantiating, set resource path if empty
  152. if (sceneResourcePath.empty)
  153. SetResourcePath(GetPath(fileName));
  154. Vector3 position = GetNewNodePosition();
  155. Node@ newNode;
  156. String extension = GetExtension(fileName);
  157. if (extension != ".xml")
  158. newNode = editorScene.Instantiate(file, position, Quaternion(), instantiateMode);
  159. else
  160. newNode = editorScene.InstantiateXML(file, position, Quaternion(), instantiateMode);
  161. if (newNode !is null)
  162. {
  163. FocusNode(newNode);
  164. instantiateFileName = fileName;
  165. }
  166. }
  167. void SaveNode(const String&in fileName)
  168. {
  169. if (fileName.empty || GetFileName(fileName).empty)
  170. return;
  171. if (selectedNodes.length == 1)
  172. {
  173. File file(fileName, FILE_WRITE);
  174. if (!file.open)
  175. return;
  176. String extension = GetExtension(fileName);
  177. if (extension != ".xml")
  178. selectedNodes[0].Save(file);
  179. else
  180. selectedNodes[0].SaveXML(file);
  181. instantiateFileName = fileName;
  182. }
  183. }
  184. void UpdateScene(float timeStep)
  185. {
  186. if (runUpdate)
  187. editorScene.Update(timeStep);
  188. }
  189. void BeginModify(uint nodeID)
  190. {
  191. // Undo/Redo can be implemented here
  192. }
  193. void EndModify(uint nodeID)
  194. {
  195. // Undo/Redo can be implemented here
  196. if (!sceneModified)
  197. {
  198. sceneModified = true;
  199. UpdateWindowTitle();
  200. }
  201. }
  202. void BeginSelectionModify()
  203. {
  204. // A large operation on selected nodes is about to begin. Disable intermediate selection updates
  205. inSelectionModify = true;
  206. }
  207. void EndSelectionModify()
  208. {
  209. // The large operation on selected nodes has ended. Update node/component selection now
  210. inSelectionModify = false;
  211. HandleSceneWindowSelectionChange();
  212. }
  213. bool SceneDelete()
  214. {
  215. if (!CheckSceneWindowFocus() || (selectedComponents.empty && selectedNodes.empty))
  216. return false;
  217. BeginSelectionModify();
  218. // Clear the selection now to prevent repopulation of selectedNodes and selectedComponents combo
  219. hierarchyList.ClearSelection();
  220. // Remove nodes
  221. for (uint i = 0; i < selectedNodes.length; ++i)
  222. {
  223. Node@ node = selectedNodes[i];
  224. if (node.parent is null || node.scene is null)
  225. continue; // Root or already deleted
  226. uint id = node.id;
  227. uint nodeIndex = GetNodeListIndex(node);
  228. BeginModify(id);
  229. node.Remove();
  230. EndModify(id);
  231. // If deleting only one node, select the next item in the same index
  232. if (selectedNodes.length == 1 && selectedComponents.empty)
  233. hierarchyList.selection = nodeIndex;
  234. }
  235. // Then remove components, if they still remain
  236. for (uint i = 0; i < selectedComponents.length; ++i)
  237. {
  238. Component@ component = selectedComponents[i];
  239. Node@ node = component.node;
  240. if (node is null)
  241. continue; // Already deleted
  242. uint index = GetComponentListIndex(component);
  243. uint nodeIndex = GetNodeListIndex(node);
  244. if (index == NO_ITEM || nodeIndex == NO_ITEM)
  245. continue;
  246. // Do not allow to remove the Octree, PhysicsWorld or DebugRenderer from the root node
  247. if (node is editorScene && (component.typeName == "Octree" || component.typeName == "PhysicsWorld" ||
  248. component.typeName == "DebugRenderer"))
  249. continue;
  250. uint id = node.id;
  251. BeginModify(id);
  252. node.RemoveComponent(component);
  253. EndModify(id);
  254. // If deleting only one component, select the next item in the same index
  255. if (selectedComponents.length == 1 && selectedNodes.empty)
  256. hierarchyList.selection = index;
  257. }
  258. EndSelectionModify();
  259. return true;
  260. }
  261. bool SceneCut()
  262. {
  263. if (SceneCopy())
  264. return SceneDelete();
  265. else
  266. return false;
  267. }
  268. bool SceneCopy()
  269. {
  270. if ((selectedNodes.empty && selectedComponents.empty) || !CheckSceneWindowFocus())
  271. return false;
  272. // Must have either only components, or only nodes
  273. if (!selectedNodes.empty && !selectedComponents.empty)
  274. return false;
  275. copyBuffer.Clear();
  276. // Copy components
  277. if (!selectedComponents.empty)
  278. {
  279. for (uint i = 0; i < selectedComponents.length; ++i)
  280. {
  281. XMLFile@ xml = XMLFile();
  282. XMLElement rootElem = xml.CreateRoot("component");
  283. selectedComponents[i].SaveXML(rootElem);
  284. rootElem.SetBool("local", selectedComponents[i].id >= FIRST_LOCAL_ID);
  285. copyBuffer.Push(xml);
  286. }
  287. return true;
  288. }
  289. // Copy node. The root node can not be copied
  290. else
  291. {
  292. for (uint i = 0; i < selectedNodes.length; ++i)
  293. {
  294. if (selectedNodes[i] is editorScene)
  295. return false;
  296. }
  297. for (uint i = 0; i < selectedNodes.length; ++i)
  298. {
  299. XMLFile@ xml = XMLFile();
  300. XMLElement rootElem = xml.CreateRoot("node");
  301. selectedNodes[i].SaveXML(rootElem);
  302. rootElem.SetBool("local", selectedNodes[i].id >= FIRST_LOCAL_ID);
  303. copyBuffer.Push(xml);
  304. }
  305. return true;
  306. }
  307. }
  308. bool ScenePaste()
  309. {
  310. if (editNode is null || !CheckSceneWindowFocus() || copyBuffer.empty)
  311. return false;
  312. bool pasteComponents = false;
  313. for (uint i = 0; i < copyBuffer.length; ++i)
  314. {
  315. XMLElement rootElem = copyBuffer[i].root;
  316. String mode = rootElem.name;
  317. if (mode == "component")
  318. {
  319. pasteComponents = true;
  320. // If this is the root node, do not allow to create duplicate scene-global components
  321. if (editNode is editorScene && CheckForExistingGlobalComponent(editNode, rootElem.GetAttribute("type")))
  322. return false;
  323. BeginModify(editNode.id);
  324. // If copied component was local, make the new local too
  325. Component@ newComponent = editNode.CreateComponent(rootElem.GetAttribute("type"), rootElem.GetBool("local") ? LOCAL :
  326. REPLICATED);
  327. if (newComponent is null)
  328. {
  329. EndModify(editNode.id);
  330. return false;
  331. }
  332. newComponent.LoadXML(rootElem);
  333. newComponent.ApplyAttributes();
  334. EndModify(editNode.id);
  335. }
  336. else if (mode == "node")
  337. {
  338. // Make the paste go always to the root node, no matter of the selected node
  339. BeginModify(editorScene.id);
  340. // If copied node was local, make the new local too
  341. Node@ newNode = editorScene.CreateChild("", rootElem.GetBool("local") ? LOCAL : REPLICATED);
  342. BeginModify(newNode.id);
  343. newNode.LoadXML(rootElem);
  344. newNode.ApplyAttributes();
  345. EndModify(newNode.id);
  346. EndModify(editorScene.id);
  347. }
  348. }
  349. return true;
  350. }
  351. void SceneUnparent()
  352. {
  353. if (!CheckSceneWindowFocus() || !selectedComponents.empty || selectedNodes.empty)
  354. return;
  355. // Parent selected nodes to root
  356. Array<Node@> changedNodes;
  357. for (uint i = 0; i < selectedNodes.length; ++i)
  358. {
  359. Node@ sourceNode = selectedNodes[i];
  360. if (sourceNode.parent is null || sourceNode.parent is editorScene)
  361. continue; // Root or already parented to root
  362. // Perform the reparenting, continue loop even if action fails
  363. SceneChangeParent(sourceNode, editorScene);
  364. changedNodes.Push(sourceNode);
  365. }
  366. // Reselect the changed nodes at their new position in the list
  367. for (uint i = 0; i < changedNodes.length; ++i)
  368. hierarchyList.AddSelection(GetNodeListIndex(changedNodes[i]));
  369. }
  370. bool SceneChangeParent(Node@ sourceNode, Node@ targetNode)
  371. {
  372. // Perform the reparenting
  373. BeginModify(targetNode.id);
  374. BeginModify(sourceNode.id);
  375. sourceNode.parent = targetNode;
  376. EndModify(sourceNode.id);
  377. EndModify(targetNode.id);
  378. // Return true if success
  379. return sourceNode.parent is targetNode;
  380. }
  381. void SceneResetPosition()
  382. {
  383. if (editNode !is null)
  384. {
  385. editNode.position = Vector3(0.0, 0.0, 0.0);
  386. UpdateNodeAttributes();
  387. }
  388. }
  389. void SceneResetRotation()
  390. {
  391. if (editNode !is null)
  392. {
  393. editNode.rotation = Quaternion();
  394. UpdateNodeAttributes();
  395. }
  396. }
  397. void SceneResetScale()
  398. {
  399. if (editNode !is null)
  400. {
  401. editNode.scale = Vector3(1.0, 1.0, 1.0);
  402. UpdateNodeAttributes();
  403. }
  404. }
  405. void SceneSelectAll()
  406. {
  407. if (!hierarchyList.selections.empty)
  408. {
  409. BeginSelectionModify();
  410. hierarchyList.ClearSelection();
  411. EndSelectionModify();
  412. }
  413. else
  414. {
  415. BeginSelectionModify();
  416. Array<Node@> rootLevelNodes = editorScene.GetChildren();
  417. Array<uint> indices;
  418. for (uint i = 0; i < rootLevelNodes.length; ++i)
  419. indices.Push(GetNodeListIndex(rootLevelNodes[i]));
  420. hierarchyList.SetSelections(indices);
  421. EndSelectionModify();
  422. }
  423. }