EditorScene.as 45 KB

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