EditorScene.as 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /// Urho3D editor scene handling
  2. #include "Scripts/Editor/EditorHierarchyWindow.as"
  3. #include "Scripts/Editor/EditorInspectorWindow.as"
  4. #include "Scripts/Editor/EditorCubeCapture.as"
  5. const int PICK_GEOMETRIES = 0;
  6. const int PICK_LIGHTS = 1;
  7. const int PICK_ZONES = 2;
  8. const int PICK_RIGIDBODIES = 3;
  9. const int PICK_UI_ELEMENTS = 4;
  10. const int MAX_PICK_MODES = 5;
  11. const int MAX_UNDOSTACK_SIZE = 256;
  12. Scene@ editorScene;
  13. String instantiateFileName;
  14. CreateMode instantiateMode = REPLICATED;
  15. bool sceneModified = false;
  16. bool runUpdate = false;
  17. Array<Node@> selectedNodes;
  18. Array<Component@> selectedComponents;
  19. Node@ editNode;
  20. Array<Node@> editNodes;
  21. Array<Component@> editComponents;
  22. uint numEditableComponentsPerNode = 1;
  23. Array<XMLFile@> sceneCopyBuffer;
  24. bool suppressSceneChanges = false;
  25. bool inSelectionModify = false;
  26. bool skipMruScene = false;
  27. Array<EditActionGroup> undoStack;
  28. uint undoStackPos = 0;
  29. bool revertOnPause = false;
  30. XMLFile@ revertData;
  31. Vector3 lastOffsetForSmartDuplicate;
  32. void ClearSceneSelection()
  33. {
  34. selectedNodes.Clear();
  35. selectedComponents.Clear();
  36. editNode = null;
  37. editNodes.Clear();
  38. editComponents.Clear();
  39. numEditableComponentsPerNode = 1;
  40. HideGizmo();
  41. }
  42. void CreateScene()
  43. {
  44. // Create a scene only once here
  45. editorScene = Scene();
  46. // Allow access to the scene from the console
  47. script.defaultScene = editorScene;
  48. // Always pause the scene, and do updates manually
  49. editorScene.updateEnabled = false;
  50. }
  51. bool ResetScene()
  52. {
  53. ui.cursor.shape = CS_BUSY;
  54. if (messageBoxCallback is null && sceneModified)
  55. {
  56. MessageBox@ messageBox = MessageBox("Scene has been modified.\nContinue to reset?", "Warning");
  57. if (messageBox.window !is null)
  58. {
  59. Button@ cancelButton = messageBox.window.GetChild("CancelButton", true);
  60. cancelButton.visible = true;
  61. cancelButton.focus = true;
  62. SubscribeToEvent(messageBox, "MessageACK", "HandleMessageAcknowledgement");
  63. messageBoxCallback = @ResetScene;
  64. return false;
  65. }
  66. }
  67. else
  68. messageBoxCallback = null;
  69. suppressSceneChanges = true;
  70. // Create a scene with default values, these will be overridden when loading scenes
  71. editorScene.Clear();
  72. editorScene.CreateComponent("Octree");
  73. editorScene.CreateComponent("DebugRenderer");
  74. // Release resources that became unused after the scene clear
  75. cache.ReleaseAllResources(false);
  76. sceneModified = false;
  77. revertData = null;
  78. StopSceneUpdate();
  79. UpdateWindowTitle();
  80. DisableInspectorLock();
  81. UpdateHierarchyItem(editorScene, true);
  82. ClearEditActions();
  83. suppressSceneChanges = false;
  84. ResetCamera();
  85. CreateGizmo();
  86. CreateGrid();
  87. SetActiveViewport(viewports[0]);
  88. return true;
  89. }
  90. void SetResourcePath(String newPath, bool usePreferredDir = true, bool additive = false)
  91. {
  92. if (newPath.empty)
  93. return;
  94. if (!IsAbsolutePath(newPath))
  95. newPath = fileSystem.currentDir + newPath;
  96. if (usePreferredDir)
  97. newPath = AddTrailingSlash(cache.GetPreferredResourceDir(newPath));
  98. else
  99. newPath = AddTrailingSlash(newPath);
  100. if (newPath == sceneResourcePath)
  101. return;
  102. // Remove the old scene resource path if any. However make sure that the default data paths do not get removed
  103. if (!additive)
  104. {
  105. cache.ReleaseAllResources(false);
  106. renderer.ReloadShaders();
  107. String check = AddTrailingSlash(sceneResourcePath);
  108. bool isDefaultResourcePath = check.Compare(fileSystem.programDir + "Data/", false) == 0 ||
  109. check.Compare(fileSystem.programDir + "CoreData/", false) == 0;
  110. if (!sceneResourcePath.empty && !isDefaultResourcePath)
  111. cache.RemoveResourceDir(sceneResourcePath);
  112. }
  113. else
  114. {
  115. // If additive (path of a loaded prefab) check that the new path isn't already part of an old path
  116. Array<String>@ resourceDirs = cache.resourceDirs;
  117. for (uint i = 0; i < resourceDirs.length; ++i)
  118. {
  119. if (newPath.StartsWith(resourceDirs[i], false))
  120. return;
  121. }
  122. }
  123. // Add resource path as first priority so that it takes precedence over the default data paths
  124. cache.AddResourceDir(newPath, 0);
  125. RebuildResourceDatabase();
  126. if (!additive)
  127. {
  128. sceneResourcePath = newPath;
  129. uiScenePath = GetResourceSubPath(newPath, "Scenes");
  130. uiElementPath = GetResourceSubPath(newPath, "UI");
  131. uiNodePath = GetResourceSubPath(newPath, "Objects");
  132. uiScriptPath = GetResourceSubPath(newPath, "Scripts");
  133. uiParticlePath = GetResourceSubPath(newPath, "Particle");
  134. }
  135. }
  136. String GetResourceSubPath(String basePath, const String&in subPath)
  137. {
  138. basePath = AddTrailingSlash(basePath);
  139. if (fileSystem.DirExists(basePath + subPath))
  140. return AddTrailingSlash(basePath + subPath);
  141. else
  142. return basePath;
  143. }
  144. bool LoadScene(const String&in fileName)
  145. {
  146. if (fileName.empty)
  147. return false;
  148. ui.cursor.shape = CS_BUSY;
  149. // Always load the scene from the filesystem, not from resource paths
  150. if (!fileSystem.FileExists(fileName))
  151. {
  152. MessageBox("No such scene.\n" + fileName);
  153. return false;
  154. }
  155. File file(fileName, FILE_READ);
  156. if (!file.open)
  157. {
  158. MessageBox("Could not open file.\n" + fileName);
  159. return false;
  160. }
  161. // Add the scene's resource path in case it's necessary
  162. String newScenePath = GetPath(fileName);
  163. if (!rememberResourcePath || !sceneResourcePath.StartsWith(newScenePath, false))
  164. SetResourcePath(newScenePath);
  165. suppressSceneChanges = true;
  166. sceneModified = false;
  167. revertData = null;
  168. StopSceneUpdate();
  169. String extension = GetExtension(fileName);
  170. bool loaded;
  171. if (extension == ".xml")
  172. loaded = editorScene.LoadXML(file);
  173. else if (extension == ".json")
  174. loaded = editorScene.LoadJSON(file);
  175. else
  176. loaded = editorScene.Load(file);
  177. // Release resources which are not used by the new scene
  178. cache.ReleaseAllResources(false);
  179. // Always pause the scene, and do updates manually
  180. editorScene.updateEnabled = false;
  181. UpdateWindowTitle();
  182. DisableInspectorLock();
  183. UpdateHierarchyItem(editorScene, true);
  184. CollapseHierarchy();
  185. ClearEditActions();
  186. suppressSceneChanges = false;
  187. // global variable to mostly bypass adding mru upon importing tempscene
  188. if (!skipMruScene)
  189. UpdateSceneMru(fileName);
  190. skipMruScene = false;
  191. ResetCamera();
  192. CreateGizmo();
  193. CreateGrid();
  194. SetActiveViewport(viewports[0]);
  195. return loaded;
  196. }
  197. bool SaveScene(const String&in fileName)
  198. {
  199. if (fileName.empty)
  200. return false;
  201. ui.cursor.shape = CS_BUSY;
  202. // Unpause when saving so that the scene will work properly when loaded outside the editor
  203. editorScene.updateEnabled = true;
  204. MakeBackup(fileName);
  205. File file(fileName, FILE_WRITE);
  206. String extension = GetExtension(fileName);
  207. bool success;
  208. if (extension == ".xml")
  209. success = editorScene.SaveXML(file);
  210. else if (extension == ".json")
  211. success = editorScene.SaveJSON(file);
  212. else
  213. success = editorScene.Save(file);
  214. RemoveBackup(success, fileName);
  215. editorScene.updateEnabled = false;
  216. if (success)
  217. {
  218. UpdateSceneMru(fileName);
  219. sceneModified = false;
  220. UpdateWindowTitle();
  221. }
  222. else
  223. MessageBox("Could not save scene successfully!\nSee Urho3D.log for more detail.");
  224. return success;
  225. }
  226. bool SaveSceneWithExistingName()
  227. {
  228. if (editorScene.fileName.empty || editorScene.fileName == TEMP_SCENE_NAME)
  229. return PickFile();
  230. else
  231. return SaveScene(editorScene.fileName);
  232. }
  233. Node@ CreateNode(CreateMode mode, bool raycastToMouse = false)
  234. {
  235. Node@ newNode = null;
  236. if (editNode !is null)
  237. newNode = editNode.CreateChild("", mode);
  238. else
  239. newNode = editorScene.CreateChild("", mode);
  240. newNode.worldPosition = GetNewNodePosition(raycastToMouse);
  241. // Create an undo action for the create
  242. CreateNodeAction action;
  243. action.Define(newNode);
  244. SaveEditAction(action);
  245. SetSceneModified();
  246. FocusNode(newNode);
  247. return newNode;
  248. }
  249. void CreateComponent(const String&in componentType)
  250. {
  251. // If this is the root node, do not allow to create duplicate scene-global components
  252. if (editNode is editorScene && CheckForExistingGlobalComponent(editNode, componentType))
  253. return;
  254. // Group for storing undo actions
  255. EditActionGroup group;
  256. // For now, make a local node's all components local
  257. /// \todo Allow to specify the createmode
  258. for (uint i = 0; i < editNodes.length; ++i)
  259. {
  260. Component@ newComponent = editNodes[i].CreateComponent(componentType, editNodes[i].id < FIRST_LOCAL_ID ? REPLICATED : LOCAL);
  261. if (newComponent !is null)
  262. {
  263. // Some components such as CollisionShape do not create their internal object before the first call to ApplyAttributes()
  264. // to prevent unnecessary initialization with default values. Call now
  265. newComponent.ApplyAttributes();
  266. CreateComponentAction action;
  267. action.Define(newComponent);
  268. group.actions.Push(action);
  269. }
  270. }
  271. SaveEditActionGroup(group);
  272. SetSceneModified();
  273. // Although the edit nodes selection are not changed, call to ensure attribute inspector notices new components of the edit nodes
  274. HandleHierarchyListSelectionChange();
  275. }
  276. void CreateLoadedComponent(Component@ component)
  277. {
  278. if (component is null) return;
  279. CreateComponentAction action;
  280. action.Define(component);
  281. SaveEditAction(action);
  282. SetSceneModified();
  283. FocusComponent(component);
  284. }
  285. Node@ LoadNode(const String&in fileName, Node@ parent = null, bool raycastToMouse = false)
  286. {
  287. if (fileName.empty)
  288. return null;
  289. if (!fileSystem.FileExists(fileName))
  290. {
  291. MessageBox("No such node file.\n" + fileName);
  292. return null;
  293. }
  294. File file(fileName, FILE_READ);
  295. if (!file.open)
  296. {
  297. MessageBox("Could not open file.\n" + fileName);
  298. return null;
  299. }
  300. ui.cursor.shape = CS_BUSY;
  301. // Before instantiating, add object's resource path if necessary
  302. SetResourcePath(GetPath(fileName), true, true);
  303. Node@ newNode = InstantiateNodeFromFile(file, GetNewNodePosition(raycastToMouse), Quaternion(), 1, parent, instantiateMode);
  304. if (newNode !is null)
  305. {
  306. FocusNode(newNode);
  307. instantiateFileName = fileName;
  308. }
  309. return newNode;
  310. }
  311. Node@ InstantiateNodeFromFile(File@ file, const Vector3& position, const Quaternion& rotation, float scaleMod = 1.0f, Node@ parent = null, CreateMode mode = REPLICATED)
  312. {
  313. if (file is null)
  314. return null;
  315. Node@ newNode;
  316. uint numSceneComponent = editorScene.numComponents;
  317. suppressSceneChanges = true;
  318. String extension = GetExtension(file.name);
  319. if (extension == ".xml")
  320. newNode = editorScene.InstantiateXML(file, position, rotation, mode);
  321. else if (extension == ".json")
  322. newNode = editorScene.InstantiateJSON(file, position, rotation, mode);
  323. else
  324. newNode = editorScene.Instantiate(file, position, rotation, mode);
  325. suppressSceneChanges = false;
  326. if (parent !is null)
  327. newNode.parent = parent;
  328. if (newNode !is null)
  329. {
  330. newNode.scale = newNode.scale * scaleMod;
  331. AdjustNodePositionByAABB(newNode);
  332. // Create an undo action for the load
  333. CreateNodeAction action;
  334. action.Define(newNode);
  335. SaveEditAction(action);
  336. SetSceneModified();
  337. if (numSceneComponent != editorScene.numComponents)
  338. UpdateHierarchyItem(editorScene);
  339. else
  340. UpdateHierarchyItem(newNode);
  341. }
  342. return newNode;
  343. }
  344. void AdjustNodePositionByAABB(Node@ newNode)
  345. {
  346. if (alignToAABBBottom)
  347. {
  348. Drawable@ drawable = GetFirstDrawable(newNode);
  349. if (drawable !is null)
  350. {
  351. BoundingBox aabb = drawable.worldBoundingBox;
  352. Vector3 aabbBottomCenter(aabb.center.x, aabb.min.y, aabb.center.z);
  353. Vector3 offset = aabbBottomCenter - newNode.worldPosition;
  354. newNode.worldPosition = newNode.worldPosition - offset;
  355. }
  356. }
  357. }
  358. bool SaveNode(const String&in fileName)
  359. {
  360. if (fileName.empty)
  361. return false;
  362. ui.cursor.shape = CS_BUSY;
  363. MakeBackup(fileName);
  364. File file(fileName, FILE_WRITE);
  365. if (!file.open)
  366. {
  367. MessageBox("Could not open file.\n" + fileName);
  368. return false;
  369. }
  370. String extension = GetExtension(fileName);
  371. bool success;
  372. if (extension == ".xml")
  373. success = editNode.SaveXML(file);
  374. else if (extension == ".json")
  375. success = editNode.SaveJSON(file);
  376. else
  377. success = editNode.Save(file);
  378. RemoveBackup(success, fileName);
  379. if (success)
  380. instantiateFileName = fileName;
  381. else
  382. MessageBox("Could not save node successfully!\nSee Urho3D.log for more detail.");
  383. return success;
  384. }
  385. void UpdateScene(float timeStep)
  386. {
  387. if (runUpdate)
  388. editorScene.Update(timeStep);
  389. }
  390. void StopSceneUpdate()
  391. {
  392. runUpdate = false;
  393. audio.Stop();
  394. toolBarDirty = true;
  395. // If scene should revert on update stop, load saved data now
  396. if (revertOnPause && revertData !is null)
  397. {
  398. suppressSceneChanges = true;
  399. editorScene.Clear();
  400. editorScene.LoadXML(revertData.root);
  401. CreateGrid();
  402. UpdateHierarchyItem(editorScene, true);
  403. ClearEditActions();
  404. suppressSceneChanges = false;
  405. }
  406. revertData = null;
  407. }
  408. void StartSceneUpdate()
  409. {
  410. runUpdate = true;
  411. // Run audio playback only when scene is updating, so that audio components' time-dependent attributes stay constant when
  412. // paused (similar to physics)
  413. audio.Play();
  414. toolBarDirty = true;
  415. // Save scene data for reverting if enabled
  416. if (revertOnPause)
  417. {
  418. revertData = XMLFile();
  419. XMLElement root = revertData.CreateRoot("scene");
  420. editorScene.SaveXML(root);
  421. }
  422. else
  423. revertData = null;
  424. }
  425. bool ToggleSceneUpdate()
  426. {
  427. if (!runUpdate)
  428. StartSceneUpdate();
  429. else
  430. StopSceneUpdate();
  431. return true;
  432. }
  433. bool ShowLayerMover()
  434. {
  435. if (ui.focusElement is null)
  436. return ShowLayerEditor();
  437. else
  438. return false;
  439. }
  440. void SetSceneModified()
  441. {
  442. if (!sceneModified)
  443. {
  444. sceneModified = true;
  445. UpdateWindowTitle();
  446. }
  447. }
  448. bool SceneDelete()
  449. {
  450. ui.cursor.shape = CS_BUSY;
  451. BeginSelectionModify();
  452. // Clear the selection now to prevent repopulation of selectedNodes and selectedComponents combo
  453. hierarchyList.ClearSelection();
  454. // Group for storing undo actions
  455. EditActionGroup group;
  456. // Remove nodes
  457. for (uint i = 0; i < selectedNodes.length; ++i)
  458. {
  459. Node@ node = selectedNodes[i];
  460. if (node.parent is null || node.scene is null)
  461. continue; // Root or already deleted
  462. uint nodeIndex = GetListIndex(node);
  463. // Create undo action
  464. DeleteNodeAction action;
  465. action.Define(node);
  466. group.actions.Push(action);
  467. node.Remove();
  468. SetSceneModified();
  469. // If deleting only one node, select the next item in the same index
  470. if (selectedNodes.length == 1 && selectedComponents.empty)
  471. hierarchyList.selection = nodeIndex;
  472. }
  473. // Then remove components, if they still remain
  474. for (uint i = 0; i < selectedComponents.length; ++i)
  475. {
  476. Component@ component = selectedComponents[i];
  477. Node@ node = component.node;
  478. if (node is null)
  479. continue; // Already deleted
  480. uint index = GetComponentListIndex(component);
  481. uint nodeIndex = GetListIndex(node);
  482. if (index == NO_ITEM || nodeIndex == NO_ITEM)
  483. continue;
  484. // Do not allow to remove the Octree, DebugRenderer or MaterialCache2D or DrawableProxy2D from the root node
  485. if (node is editorScene && (component.typeName == "Octree" || component.typeName == "DebugRenderer" ||
  486. component.typeName == "MaterialCache2D" || component.typeName == "DrawableProxy2D"))
  487. continue;
  488. // Create undo action
  489. DeleteComponentAction action;
  490. action.Define(component);
  491. group.actions.Push(action);
  492. node.RemoveComponent(component);
  493. SetSceneModified();
  494. // If deleting only one component, select the next item in the same index
  495. if (selectedComponents.length == 1 && selectedNodes.empty)
  496. hierarchyList.selection = index;
  497. }
  498. SaveEditActionGroup(group);
  499. EndSelectionModify();
  500. return true;
  501. }
  502. bool SceneCut()
  503. {
  504. return SceneCopy() && SceneDelete();
  505. }
  506. bool SceneCopy()
  507. {
  508. ui.cursor.shape = CS_BUSY;
  509. sceneCopyBuffer.Clear();
  510. // Copy components
  511. if (!selectedComponents.empty)
  512. {
  513. for (uint i = 0; i < selectedComponents.length; ++i)
  514. {
  515. XMLFile@ xml = XMLFile();
  516. XMLElement rootElem = xml.CreateRoot("component");
  517. selectedComponents[i].SaveXML(rootElem);
  518. rootElem.SetBool("local", selectedComponents[i].id >= FIRST_LOCAL_ID);
  519. sceneCopyBuffer.Push(xml);
  520. }
  521. }
  522. // Copy nodes
  523. else
  524. {
  525. for (uint i = 0; i < selectedNodes.length; ++i)
  526. {
  527. // Skip the root scene node as it cannot be copied
  528. if (selectedNodes[i] is editorScene)
  529. continue;
  530. XMLFile@ xml = XMLFile();
  531. XMLElement rootElem = xml.CreateRoot("node");
  532. selectedNodes[i].SaveXML(rootElem);
  533. rootElem.SetBool("local", selectedNodes[i].id >= FIRST_LOCAL_ID);
  534. sceneCopyBuffer.Push(xml);
  535. }
  536. }
  537. return true;
  538. }
  539. bool ScenePaste(bool pasteRoot = false, bool duplication = false)
  540. {
  541. ui.cursor.shape = CS_BUSY;
  542. // Group for storing undo actions
  543. EditActionGroup group;
  544. for (uint i = 0; i < sceneCopyBuffer.length; ++i)
  545. {
  546. XMLElement rootElem = sceneCopyBuffer[i].root;
  547. String mode = rootElem.name;
  548. if (mode == "component" && editNode !is null)
  549. {
  550. // If this is the root node, do not allow to create duplicate scene-global components
  551. if (editNode is editorScene && CheckForExistingGlobalComponent(editNode, rootElem.GetAttribute("type")))
  552. return false;
  553. // If copied component was local, make the new local too
  554. Component@ newComponent = editNode.CreateComponent(rootElem.GetAttribute("type"), rootElem.GetBool("local") ? LOCAL :
  555. REPLICATED);
  556. if (newComponent is null)
  557. return false;
  558. newComponent.LoadXML(rootElem);
  559. newComponent.ApplyAttributes();
  560. // Create an undo action
  561. CreateComponentAction action;
  562. action.Define(newComponent);
  563. group.actions.Push(action);
  564. }
  565. else if (mode == "node")
  566. {
  567. // If copied node was local, make the new local too
  568. Node@ newNode;
  569. // Are we pasting into the root node?
  570. if (pasteRoot)
  571. newNode = editorScene.CreateChild("", rootElem.GetBool("local") ? LOCAL : REPLICATED);
  572. else
  573. {
  574. // If we are duplicating or have the original node selected, paste into the selected nodes parent
  575. if (duplication || editNode is null || editNode.id == rootElem.GetUInt("id"))
  576. {
  577. if (editNode !is null && editNode.parent !is null)
  578. newNode = editNode.parent.CreateChild("", rootElem.GetBool("local") ? LOCAL : REPLICATED);
  579. else
  580. newNode = editorScene.CreateChild("", rootElem.GetBool("local") ? LOCAL : REPLICATED);
  581. }
  582. // If we aren't duplicating, paste into the selected node
  583. else
  584. {
  585. newNode = editNode.CreateChild("", rootElem.GetBool("local") ? LOCAL : REPLICATED);
  586. }
  587. }
  588. newNode.LoadXML(rootElem);
  589. // Create an undo action
  590. CreateNodeAction action;
  591. action.Define(newNode);
  592. group.actions.Push(action);
  593. }
  594. }
  595. SaveEditActionGroup(group);
  596. SetSceneModified();
  597. return true;
  598. }
  599. bool SceneDuplicate()
  600. {
  601. Array<XMLFile@> copy = sceneCopyBuffer;
  602. if (!SceneCopy())
  603. {
  604. sceneCopyBuffer = copy;
  605. return false;
  606. }
  607. if (!ScenePaste(false, true))
  608. {
  609. sceneCopyBuffer = copy;
  610. return false;
  611. }
  612. sceneCopyBuffer = copy;
  613. return true;
  614. }
  615. bool SceneUnparent()
  616. {
  617. if (!CheckHierarchyWindowFocus() || !selectedComponents.empty || selectedNodes.empty)
  618. return false;
  619. ui.cursor.shape = CS_BUSY;
  620. // Group for storing undo actions
  621. EditActionGroup group;
  622. // Parent selected nodes to root
  623. Array<Node@> changedNodes;
  624. for (uint i = 0; i < selectedNodes.length; ++i)
  625. {
  626. Node@ sourceNode = selectedNodes[i];
  627. if (sourceNode.parent is null || sourceNode.parent is editorScene)
  628. continue; // Root or already parented to root
  629. // Perform the reparenting, continue loop even if action fails
  630. ReparentNodeAction action;
  631. action.Define(sourceNode, editorScene);
  632. group.actions.Push(action);
  633. SceneChangeParent(sourceNode, editorScene, false);
  634. changedNodes.Push(sourceNode);
  635. }
  636. // Reselect the changed nodes at their new position in the list
  637. for (uint i = 0; i < changedNodes.length; ++i)
  638. hierarchyList.AddSelection(GetListIndex(changedNodes[i]));
  639. SaveEditActionGroup(group);
  640. SetSceneModified();
  641. return true;
  642. }
  643. bool NodesParentToLastSelected()
  644. {
  645. if (lastSelectedNode.Get() is null)
  646. return false;
  647. if (!CheckHierarchyWindowFocus() || !selectedComponents.empty || selectedNodes.empty)
  648. return false;
  649. ui.cursor.shape = CS_BUSY;
  650. // Group for storing undo actions
  651. EditActionGroup group;
  652. // Parent selected nodes to root
  653. Array<Node@> changedNodes;
  654. // Find new parent node it selected last
  655. Node@ lastNode = lastSelectedNode.Get(); //GetListNode(hierarchyList.selection);
  656. for (uint i = 0; i < selectedNodes.length; ++i)
  657. {
  658. Node@ sourceNode = selectedNodes[i];
  659. if ( sourceNode.id == lastNode.id)
  660. continue; // Skip last node it is parent
  661. if (sourceNode.parent.id == lastNode.id)
  662. continue; // Root or already parented to root
  663. // Perform the reparenting, continue loop even if action fails
  664. ReparentNodeAction action;
  665. action.Define(sourceNode, lastNode);
  666. group.actions.Push(action);
  667. SceneChangeParent(sourceNode, lastNode, false);
  668. changedNodes.Push(sourceNode);
  669. }
  670. // Reselect the changed nodes at their new position in the list
  671. for (uint i = 0; i < changedNodes.length; ++i)
  672. hierarchyList.AddSelection(GetListIndex(changedNodes[i]));
  673. SaveEditActionGroup(group);
  674. SetSceneModified();
  675. return true;
  676. }
  677. bool SceneSmartDuplicateNode()
  678. {
  679. const float minOffset = 0.1;
  680. if (!CheckHierarchyWindowFocus() || !selectedComponents.empty
  681. || selectedNodes.empty || lastSelectedNode.Get() is null)
  682. return false;
  683. Node@ node = lastSelectedNode.Get();
  684. Node@ parent = node.parent;
  685. Vector3 offset = Vector3(1,0,0); // default offset
  686. if (parent is editorScene) // if parent of selected node is Scene make empty parent for it and place in same position;
  687. {
  688. parent = CreateNode(LOCAL);
  689. SceneChangeParent(parent, editorScene, false);
  690. parent.worldPosition = node.worldPosition;
  691. parent.name = node.name + "Group";
  692. node.name = parent.name + "Instance" + String(parent.numChildren);
  693. SceneChangeParent(node, parent, false);
  694. parent = node.parent;
  695. SelectNode(node, false);
  696. }
  697. Vector3 size;
  698. BoundingBox bb;
  699. // get bb for offset
  700. Drawable@ drawable = GetFirstDrawable(node);
  701. if (drawable !is null)
  702. {
  703. bb = drawable.boundingBox;
  704. size = bb.size * drawable.node.worldScale;
  705. offset = Vector3(size.x, 0, 0);
  706. }
  707. // make offset on axis that select user by mouse
  708. if (gizmoAxisX.selected)
  709. {
  710. if (size.x < minOffset) size.x = minOffset;
  711. offset = node.worldRotation * Vector3(size.x,0,0);
  712. }
  713. else if (gizmoAxisY.selected)
  714. {
  715. if (size.y < minOffset) size.y = minOffset;
  716. offset = node.worldRotation * Vector3(0,size.y,0);
  717. }
  718. else if (gizmoAxisZ.selected)
  719. {
  720. if (size.z < minOffset) size.z = minOffset;
  721. offset = node.worldRotation * Vector3(0,0,size.z);
  722. }
  723. else
  724. offset = lastOffsetForSmartDuplicate;
  725. Vector3 lastInstancePosition = node.worldPosition;
  726. SelectNode(node, false);
  727. SceneDuplicate();
  728. Node@ newInstance = parent.children[parent.numChildren-1];
  729. SelectNode(newInstance, false);
  730. newInstance.worldPosition = lastInstancePosition;
  731. newInstance.Translate(offset, TS_WORLD);
  732. newInstance.name = parent.name + "Instance" + String(parent.numChildren-1);
  733. lastOffsetForSmartDuplicate = offset;
  734. UpdateNodeAttributes();
  735. return true;
  736. }
  737. bool ViewCloser()
  738. {
  739. return (viewCloser = true);
  740. }
  741. bool SceneToggleEnable()
  742. {
  743. if (!CheckHierarchyWindowFocus())
  744. return false;
  745. ui.cursor.shape = CS_BUSY;
  746. EditActionGroup group;
  747. // Toggle enabled state of nodes recursively
  748. for (uint i = 0; i < selectedNodes.length; ++i)
  749. {
  750. // Do not attempt to disable the Scene
  751. if (selectedNodes[i].typeName == "Node")
  752. {
  753. bool oldEnabled = selectedNodes[i].enabled;
  754. selectedNodes[i].SetEnabledRecursive(!oldEnabled);
  755. // Create undo action
  756. ToggleNodeEnabledAction action;
  757. action.Define(selectedNodes[i], oldEnabled);
  758. group.actions.Push(action);
  759. }
  760. }
  761. for (uint i = 0; i < selectedComponents.length; ++i)
  762. {
  763. // Some components purposefully do not expose the Enabled attribute, and it does not affect them in any way
  764. // (Octree, PhysicsWorld). Check that the first attribute is in fact called "Is Enabled"
  765. if (selectedComponents[i].numAttributes > 0 && selectedComponents[i].attributeInfos[0].name == "Is Enabled")
  766. {
  767. bool oldEnabled = selectedComponents[i].enabled;
  768. selectedComponents[i].enabled = !oldEnabled;
  769. // Create undo action
  770. EditAttributeAction action;
  771. action.Define(selectedComponents[i], 0, Variant(oldEnabled));
  772. group.actions.Push(action);
  773. }
  774. }
  775. SaveEditActionGroup(group);
  776. SetSceneModified();
  777. return true;
  778. }
  779. bool SceneEnableAllNodes()
  780. {
  781. if (!CheckHierarchyWindowFocus())
  782. return false;
  783. ui.cursor.shape = CS_BUSY;
  784. EditActionGroup group;
  785. // Toggle enabled state of nodes recursively
  786. Array<Node@> allNodes;
  787. allNodes = editorScene.GetChildren(true);
  788. for (uint i = 0; i < allNodes.length; ++i)
  789. {
  790. // Do not attempt to disable the Scene
  791. if (allNodes[i].typeName == "Node")
  792. {
  793. bool oldEnabled = allNodes[i].enabled;
  794. if (oldEnabled == false)
  795. allNodes[i].SetEnabledRecursive(true);
  796. // Create undo action
  797. ToggleNodeEnabledAction action;
  798. action.Define(allNodes[i], oldEnabled);
  799. group.actions.Push(action);
  800. }
  801. }
  802. Array<Component@> allComponents;
  803. allComponents = editorScene.GetComponents();
  804. for (uint i = 0; i < allComponents.length; ++i)
  805. {
  806. // Some components purposefully do not expose the Enabled attribute, and it does not affect them in any way
  807. // (Octree, PhysicsWorld). Check that the first attribute is in fact called "Is Enabled"
  808. if (allComponents[i].numAttributes > 0 && allComponents[i].attributeInfos[0].name == "Is Enabled")
  809. {
  810. bool oldEnabled = allComponents[i].enabled;
  811. allComponents[i].enabled = true;
  812. // Create undo action
  813. EditAttributeAction action;
  814. action.Define(allComponents[i], 0, Variant(oldEnabled));
  815. group.actions.Push(action);
  816. }
  817. }
  818. SaveEditActionGroup(group);
  819. SetSceneModified();
  820. return true;
  821. }
  822. bool SceneChangeParent(Node@ sourceNode, Node@ targetNode, bool createUndoAction = true)
  823. {
  824. // Create undo action if requested
  825. if (createUndoAction)
  826. {
  827. ReparentNodeAction action;
  828. action.Define(sourceNode, targetNode);
  829. SaveEditAction(action);
  830. }
  831. sourceNode.parent = targetNode;
  832. SetSceneModified();
  833. // Return true if success
  834. if (sourceNode.parent is targetNode)
  835. {
  836. UpdateNodeAttributes(); // Parent change may have changed local transform
  837. return true;
  838. }
  839. else
  840. return false;
  841. }
  842. bool SceneChangeParent(Node@ sourceNode, Array<Node@> sourceNodes, Node@ targetNode, bool createUndoAction = true)
  843. {
  844. // Create undo action if requested
  845. if (createUndoAction)
  846. {
  847. ReparentNodeAction action;
  848. action.Define(sourceNodes, targetNode);
  849. SaveEditAction(action);
  850. }
  851. for (uint i = 0; i < sourceNodes.length; ++i)
  852. {
  853. Node@ node = sourceNodes[i];
  854. node.parent = targetNode;
  855. }
  856. SetSceneModified();
  857. // Return true if success
  858. if (sourceNode.parent is targetNode)
  859. {
  860. UpdateNodeAttributes(); // Parent change may have changed local transform
  861. return true;
  862. }
  863. else
  864. return false;
  865. }
  866. bool SceneReorder(Node@ sourceNode, Node@ targetNode)
  867. {
  868. if (sourceNode is null || targetNode is null || sourceNode.parent is null || sourceNode.parent !is targetNode.parent)
  869. return false;
  870. if (sourceNode is targetNode)
  871. return true; // No-op
  872. Node@ parent = sourceNode.parent;
  873. uint destIndex = SceneFindChildIndex(parent, targetNode);
  874. ReorderNodeAction action;
  875. action.Define(sourceNode, destIndex);
  876. SaveEditAction(action);
  877. PerformReorder(parent, sourceNode, destIndex);
  878. return true;
  879. }
  880. bool SceneReorder(Component@ sourceComponent, Component@ targetComponent)
  881. {
  882. if (sourceComponent is null || targetComponent is null || sourceComponent.node !is targetComponent.node)
  883. return false;
  884. if (sourceComponent is targetComponent)
  885. return true; // No-op
  886. Node@ node = sourceComponent.node;
  887. uint destIndex = SceneFindComponentIndex(node, targetComponent);
  888. ReorderComponentAction action;
  889. action.Define(sourceComponent, destIndex);
  890. SaveEditAction(action);
  891. PerformReorder(node, sourceComponent, destIndex);
  892. return true;
  893. }
  894. void PerformReorder(Node@ parent, Node@ child, uint destIndex)
  895. {
  896. suppressSceneChanges = true;
  897. // Removal from scene zeroes the ID. Be prepared to restore it
  898. uint oldId = child.id;
  899. parent.RemoveChild(child);
  900. child.id = oldId;
  901. parent.AddChild(child, destIndex);
  902. UpdateHierarchyItem(parent); // Force update to make sure the order is current
  903. SetSceneModified();
  904. suppressSceneChanges = false;
  905. }
  906. void PerformReorder(Node@ node, Component@ component, uint destIndex)
  907. {
  908. suppressSceneChanges = true;
  909. node.ReorderComponent(component, destIndex);
  910. UpdateHierarchyItem(node); // Force update to make sure the order is current
  911. SetSceneModified();
  912. suppressSceneChanges = false;
  913. }
  914. uint SceneFindChildIndex(Node@ parent, Node@ child)
  915. {
  916. for (uint i = 0; i < parent.numChildren; ++i)
  917. {
  918. if (parent.children[i] is child)
  919. return i;
  920. }
  921. return -1;
  922. }
  923. uint SceneFindComponentIndex(Node@ node, Component@ component)
  924. {
  925. for (uint i = 0; i < node.numComponents; ++i)
  926. {
  927. if (node.components[i] is component)
  928. return i;
  929. }
  930. return -1;
  931. }
  932. bool SceneResetPosition()
  933. {
  934. if (editNode !is null)
  935. {
  936. Transform oldTransform;
  937. oldTransform.Define(editNode);
  938. editNode.position = Vector3(0.0, 0.0, 0.0);
  939. // Create undo action
  940. EditNodeTransformAction action;
  941. action.Define(editNode, oldTransform);
  942. SaveEditAction(action);
  943. SetSceneModified();
  944. UpdateNodeAttributes();
  945. return true;
  946. }
  947. else
  948. return false;
  949. }
  950. bool SceneResetRotation()
  951. {
  952. if (editNode !is null)
  953. {
  954. Transform oldTransform;
  955. oldTransform.Define(editNode);
  956. editNode.rotation = Quaternion();
  957. // Create undo action
  958. EditNodeTransformAction action;
  959. action.Define(editNode, oldTransform);
  960. SaveEditAction(action);
  961. SetSceneModified();
  962. UpdateNodeAttributes();
  963. return true;
  964. }
  965. else
  966. return false;
  967. }
  968. bool SceneResetScale()
  969. {
  970. if (editNode !is null)
  971. {
  972. Transform oldTransform;
  973. oldTransform.Define(editNode);
  974. editNode.scale = Vector3(1.0, 1.0, 1.0);
  975. // Create undo action
  976. EditNodeTransformAction action;
  977. action.Define(editNode, oldTransform);
  978. SaveEditAction(action);
  979. SetSceneModified();
  980. UpdateNodeAttributes();
  981. return true;
  982. }
  983. else
  984. return false;
  985. }
  986. bool SceneResetTransform()
  987. {
  988. if (editNode !is null)
  989. {
  990. Transform oldTransform;
  991. oldTransform.Define(editNode);
  992. editNode.position = Vector3(0.0, 0.0, 0.0);
  993. editNode.rotation = Quaternion();
  994. editNode.scale = Vector3(1.0, 1.0, 1.0);
  995. // Create undo action
  996. EditNodeTransformAction action;
  997. action.Define(editNode, oldTransform);
  998. SaveEditAction(action);
  999. SetSceneModified();
  1000. UpdateNodeAttributes();
  1001. return true;
  1002. }
  1003. else
  1004. return false;
  1005. }
  1006. bool SceneSelectAll()
  1007. {
  1008. BeginSelectionModify();
  1009. Array<Node@> rootLevelNodes = editorScene.GetChildren();
  1010. Array<uint> indices;
  1011. for (uint i = 0; i < rootLevelNodes.length; ++i)
  1012. indices.Push(GetListIndex(rootLevelNodes[i]));
  1013. hierarchyList.SetSelections(indices);
  1014. EndSelectionModify();
  1015. return true;
  1016. }
  1017. bool SceneResetToDefault()
  1018. {
  1019. ui.cursor.shape = CS_BUSY;
  1020. // Group for storing undo actions
  1021. EditActionGroup group;
  1022. // Reset selected component to their default
  1023. if (!selectedComponents.empty)
  1024. {
  1025. for (uint i = 0; i < selectedComponents.length; ++i)
  1026. {
  1027. Component@ component = selectedComponents[i];
  1028. ResetAttributesAction action;
  1029. action.Define(component);
  1030. group.actions.Push(action);
  1031. component.ResetToDefault();
  1032. component.ApplyAttributes();
  1033. for (uint j = 0; j < component.numAttributes; ++j)
  1034. PostEditAttribute(component, j);
  1035. }
  1036. }
  1037. // OR reset selected nodes to their default
  1038. else
  1039. {
  1040. for (uint i = 0; i < selectedNodes.length; ++i)
  1041. {
  1042. Node@ node = selectedNodes[i];
  1043. ResetAttributesAction action;
  1044. action.Define(node);
  1045. group.actions.Push(action);
  1046. node.ResetToDefault();
  1047. node.ApplyAttributes();
  1048. for (uint j = 0; j < node.numAttributes; ++j)
  1049. PostEditAttribute(node, j);
  1050. }
  1051. }
  1052. SaveEditActionGroup(group);
  1053. SetSceneModified();
  1054. attributesFullDirty = true;
  1055. return true;
  1056. }
  1057. bool SceneRebuildNavigation()
  1058. {
  1059. ui.cursor.shape = CS_BUSY;
  1060. Array<Component@>@ navMeshes = editorScene.GetComponents("NavigationMesh", true);
  1061. if (navMeshes.empty)
  1062. {
  1063. @navMeshes = editorScene.GetComponents("DynamicNavigationMesh", true);
  1064. if (navMeshes.empty)
  1065. {
  1066. MessageBox("No NavigationMesh components in the scene, nothing to rebuild.");
  1067. return false;
  1068. }
  1069. }
  1070. bool success = true;
  1071. for (uint i = 0; i < navMeshes.length; ++i)
  1072. {
  1073. NavigationMesh@ navMesh = navMeshes[i];
  1074. if (!navMesh.Build())
  1075. success = false;
  1076. }
  1077. return success;
  1078. }
  1079. bool SceneRenderZoneCubemaps()
  1080. {
  1081. bool success = false;
  1082. Array<Zone@> capturedThisCall;
  1083. bool alreadyCapturing = activeCubeCapture.length > 0; // May have managed to quickly queue up a second round of zones to render cubemaps for
  1084. for (uint i = 0; i < selectedNodes.length; ++i)
  1085. {
  1086. Array<Component@>@ zones = selectedNodes[i].GetComponents("Zone", true);
  1087. for (uint z = 0; z < zones.length; ++z)
  1088. {
  1089. Zone@ zone = cast<Zone>(zones[z]);
  1090. if (zone !is null)
  1091. {
  1092. activeCubeCapture.Push(EditorCubeCapture(zone));
  1093. capturedThisCall.Push(zone);
  1094. }
  1095. }
  1096. }
  1097. for (uint i = 0; i < selectedComponents.length; ++i)
  1098. {
  1099. Zone@ zone = cast<Zone>(selectedComponents[i]);
  1100. if (zone !is null)
  1101. {
  1102. if (capturedThisCall.FindByRef(zone) < 0)
  1103. {
  1104. activeCubeCapture.Push(EditorCubeCapture(zone));
  1105. capturedThisCall.Push(zone);
  1106. }
  1107. }
  1108. }
  1109. // Start rendering cubemaps if there are any to render and the queue isn't already running
  1110. if (activeCubeCapture.length > 0 && !alreadyCapturing)
  1111. activeCubeCapture[0].Start();
  1112. if (capturedThisCall.length <= 0)
  1113. {
  1114. MessageBox("No zones selected to render cubemaps for/");
  1115. }
  1116. return capturedThisCall.length > 0;
  1117. }
  1118. bool SceneAddChildrenStaticModelGroup()
  1119. {
  1120. StaticModelGroup@ smg = cast<StaticModelGroup>(editComponents.length > 0 ? editComponents[0] : null);
  1121. if (smg is null && editNode !is null)
  1122. smg = editNode.GetComponent("StaticModelGroup");
  1123. if (smg is null)
  1124. {
  1125. MessageBox("Must have a StaticModelGroup component selected.");
  1126. return false;
  1127. }
  1128. uint attrIndex = GetAttributeIndex(smg, "Instance Nodes");
  1129. Variant oldValue = smg.attributes[attrIndex];
  1130. Array<Node@> children = smg.node.GetChildren(true);
  1131. for (uint i = 0; i < children.length; ++i)
  1132. smg.AddInstanceNode(children[i]);
  1133. EditAttributeAction action;
  1134. action.Define(smg, attrIndex, oldValue);
  1135. SaveEditAction(action);
  1136. SetSceneModified();
  1137. FocusComponent(smg);
  1138. return true;
  1139. }
  1140. bool SceneSetChildrenSplinePath(bool makeCycle)
  1141. {
  1142. SplinePath@ sp = cast<SplinePath>(editComponents.length > 0 ? editComponents[0] : null);
  1143. if (sp is null && editNode !is null)
  1144. sp = editNode.GetComponent("SplinePath");
  1145. if (sp is null)
  1146. {
  1147. MessageBox("Must have a SplinePath component selected.");
  1148. return false;
  1149. }
  1150. uint attrIndex = GetAttributeIndex(sp, "Control Points");
  1151. Variant oldValue = sp.attributes[attrIndex];
  1152. Array<Node@> children = sp.node.GetChildren(true);
  1153. if (children.length >= 2)
  1154. {
  1155. sp.ClearControlPoints();
  1156. for (uint i = 0; i < children.length; ++i)
  1157. sp.AddControlPoint(children[i]);
  1158. }
  1159. else
  1160. {
  1161. MessageBox("You must have a minimum two children Nodes in selected Node.");
  1162. return false;
  1163. }
  1164. if (makeCycle)
  1165. sp.AddControlPoint(children[0]);
  1166. EditAttributeAction action;
  1167. action.Define(sp, attrIndex, oldValue);
  1168. SaveEditAction(action);
  1169. SetSceneModified();
  1170. FocusComponent(sp);
  1171. return true;
  1172. }
  1173. void AssignMaterial(StaticModel@ model, String materialPath)
  1174. {
  1175. Material@ material = cache.GetResource("Material", materialPath);
  1176. if (material is null)
  1177. return;
  1178. ResourceRefList materials = model.GetAttribute("Material").GetResourceRefList();
  1179. Array<String> oldMaterials;
  1180. for(uint i = 0; i < materials.length; ++i)
  1181. oldMaterials.Push(materials.names[i]);
  1182. model.material = material;
  1183. AssignMaterialAction action;
  1184. action.Define(model, oldMaterials, material);
  1185. SaveEditAction(action);
  1186. SetSceneModified();
  1187. FocusComponent(model);
  1188. }
  1189. void UpdateSceneMru(String filename)
  1190. {
  1191. while (uiRecentScenes.Find(filename) > -1)
  1192. uiRecentScenes.Erase(uiRecentScenes.Find(filename));
  1193. uiRecentScenes.Insert(0, filename);
  1194. for (uint i = uiRecentScenes.length - 1; i >= maxRecentSceneCount; i--)
  1195. uiRecentScenes.Erase(i);
  1196. PopulateMruScenes();
  1197. }
  1198. Drawable@ GetFirstDrawable(Node@ node)
  1199. {
  1200. Array<Node@> nodes = node.GetChildren(true);
  1201. nodes.Insert(0, node);
  1202. for (uint i = 0; i < nodes.length; ++i)
  1203. {
  1204. Array<Component@> components = nodes[i].GetComponents();
  1205. for (uint j = 0; j < components.length; ++j)
  1206. {
  1207. Drawable@ drawable = cast<Drawable>(components[j]);
  1208. if (drawable !is null)
  1209. return drawable;
  1210. }
  1211. }
  1212. return null;
  1213. }
  1214. void AssignModel(StaticModel@ assignee, String modelPath)
  1215. {
  1216. Model@ model = cache.GetResource("Model", modelPath);
  1217. if (model is null)
  1218. return;
  1219. Model@ oldModel = assignee.model;
  1220. assignee.model = model;
  1221. AssignModelAction action;
  1222. action.Define(assignee, oldModel, model);
  1223. SaveEditAction(action);
  1224. SetSceneModified();
  1225. FocusComponent(assignee);
  1226. }
  1227. void CreateModelWithStaticModel(String filepath, Node@ parent)
  1228. {
  1229. if (parent is null)
  1230. return;
  1231. /// \todo should be able to specify the createmode
  1232. if (parent is editorScene)
  1233. parent = CreateNode(REPLICATED);
  1234. Model@ model = cache.GetResource("Model", filepath);
  1235. if (model is null)
  1236. return;
  1237. StaticModel@ staticModel = parent.GetOrCreateComponent("StaticModel");
  1238. staticModel.model = model;
  1239. if (applyMaterialList)
  1240. staticModel.ApplyMaterialList();
  1241. CreateLoadedComponent(staticModel);
  1242. }
  1243. void CreateModelWithAnimatedModel(String filepath, Node@ parent)
  1244. {
  1245. if (parent is null)
  1246. return;
  1247. /// \todo should be able to specify the createmode
  1248. if (parent is editorScene)
  1249. parent = CreateNode(REPLICATED);
  1250. Model@ model = cache.GetResource("Model", filepath);
  1251. if (model is null)
  1252. return;
  1253. AnimatedModel@ animatedModel = parent.GetOrCreateComponent("AnimatedModel");
  1254. animatedModel.model = model;
  1255. if (applyMaterialList)
  1256. animatedModel.ApplyMaterialList();
  1257. CreateLoadedComponent(animatedModel);
  1258. }
  1259. bool ColorWheelSetupBehaviorForColoring()
  1260. {
  1261. Menu@ menu = GetEventSender();
  1262. if (menu is null)
  1263. return false;
  1264. coloringPropertyName = menu.name;
  1265. if (coloringPropertyName == "menuCancel") return false;
  1266. if (coloringComponent.typeName == "Light")
  1267. {
  1268. Light@ light = cast<Light>(coloringComponent);
  1269. if (light !is null)
  1270. {
  1271. if (coloringPropertyName == "menuLightColor")
  1272. {
  1273. coloringOldColor = light.color;
  1274. ShowColorWheelWithColor(coloringOldColor);
  1275. }
  1276. else if (coloringPropertyName == "menuSpecularIntensity")
  1277. {
  1278. // ColorWheel have only 0-1 range output of V-value(BW), and for huge-range values we divide in and multiply out
  1279. float scaledSpecular = light.specularIntensity * 0.1f;
  1280. coloringOldScalar = scaledSpecular;
  1281. ShowColorWheelWithColor(Color(scaledSpecular,scaledSpecular,scaledSpecular));
  1282. }
  1283. else if (coloringPropertyName == "menuBrightnessMultiplier")
  1284. {
  1285. float scaledBrightness = light.brightness * 0.1f;
  1286. coloringOldScalar = scaledBrightness;
  1287. ShowColorWheelWithColor(Color(scaledBrightness,scaledBrightness,scaledBrightness));
  1288. }
  1289. }
  1290. }
  1291. else if (coloringComponent.typeName == "StaticModel")
  1292. {
  1293. StaticModel@ model = cast<StaticModel>(coloringComponent);
  1294. if (model !is null)
  1295. {
  1296. Material@ mat = model.materials[0];
  1297. if (mat !is null)
  1298. {
  1299. if (coloringPropertyName == "menuDiffuseColor")
  1300. {
  1301. Variant oldValue = mat.shaderParameters["MatDiffColor"];
  1302. Array<String> values = oldValue.ToString().Split(' ');
  1303. coloringOldColor = Color(values[0].ToFloat(),values[1].ToFloat(),values[2].ToFloat(),values[3].ToFloat()); //RGBA
  1304. ShowColorWheelWithColor(coloringOldColor);
  1305. }
  1306. else if (coloringPropertyName == "menuSpecularColor")
  1307. {
  1308. Variant oldValue = mat.shaderParameters["MatSpecColor"];
  1309. Array<String> values = oldValue.ToString().Split(' ');
  1310. coloringOldColor = Color(values[0].ToFloat(),values[1].ToFloat(),values[2].ToFloat());
  1311. coloringOldScalar = values[3].ToFloat();
  1312. ShowColorWheelWithColor(Color(coloringOldColor.r, coloringOldColor.g, coloringOldColor.b, coloringOldScalar/128.0f)); //RGB + shine
  1313. }
  1314. else if (coloringPropertyName == "menuEmissiveColor")
  1315. {
  1316. Variant oldValue = mat.shaderParameters["MatEmissiveColor"];
  1317. Array<String> values = oldValue.ToString().Split(' ');
  1318. coloringOldColor = Color(values[0].ToFloat(),values[1].ToFloat(),values[2].ToFloat()); // RGB
  1319. ShowColorWheelWithColor(coloringOldColor);
  1320. }
  1321. else if (coloringPropertyName == "menuEnvironmentMapColor")
  1322. {
  1323. Variant oldValue = mat.shaderParameters["MatEnvMapColor"];
  1324. Array<String> values = oldValue.ToString().Split(' ');
  1325. coloringOldColor = Color(values[0].ToFloat(),values[1].ToFloat(),values[2].ToFloat()); //RGB
  1326. ShowColorWheelWithColor(coloringOldColor);
  1327. }
  1328. }
  1329. }
  1330. }
  1331. else if (coloringComponent.typeName == "Zone")
  1332. {
  1333. Zone@ zone = cast<Zone>(coloringComponent);
  1334. if (zone !is null)
  1335. {
  1336. if (coloringPropertyName == "menuAmbientColor")
  1337. {
  1338. coloringOldColor = zone.ambientColor;
  1339. }
  1340. else if (coloringPropertyName == "menuFogColor")
  1341. {
  1342. coloringOldColor = zone.fogColor;
  1343. }
  1344. ShowColorWheelWithColor(coloringOldColor);
  1345. }
  1346. }
  1347. return true;
  1348. }