EditorScene.as 48 KB

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