EditorSceneWindow.as 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. // Urho3D editor scene hierarchy window handling
  2. const int ITEM_NONE = 0;
  3. const int ITEM_NODE = 1;
  4. const int ITEM_COMPONENT = 2;
  5. const uint NO_ITEM = 0xffffffff;
  6. Window@ sceneWindow;
  7. void CreateSceneWindow()
  8. {
  9. if (sceneWindow !is null)
  10. return;
  11. @sceneWindow = ui.LoadLayout(cache.GetResource("XMLFile", "UI/EditorSceneWindow.xml"), uiStyle);
  12. ui.root.AddChild(sceneWindow);
  13. int height = Min(ui.root.height - 60, 500);
  14. sceneWindow.SetSize(300, height);
  15. sceneWindow.SetPosition(20, 40);
  16. sceneWindow.opacity = uiMaxOpacity;
  17. sceneWindow.BringToFront();
  18. UpdateSceneWindow();
  19. DropDownList@ newNodeList = sceneWindow.GetChild("NewNodeList", true);
  20. Array<String> newNodeChoices = {"Replicated", "Local"};
  21. for (uint i = 0; i < newNodeChoices.length; ++i)
  22. {
  23. Text@ choice = Text();
  24. choice.SetStyle(uiStyle, "FileSelectorFilterText");
  25. choice.text = newNodeChoices[i];
  26. newNodeList.AddItem(choice);
  27. }
  28. DropDownList@ newComponentList = sceneWindow.GetChild("NewComponentList", true);
  29. Array<String> componentTypes = GetAvailableComponents();
  30. for (uint i = 0; i < componentTypes.length; ++i)
  31. {
  32. Text@ choice = Text();
  33. choice.SetStyle(uiStyle, "FileSelectorFilterText");
  34. choice.text = componentTypes[i];
  35. newComponentList.AddItem(choice);
  36. }
  37. // Set drag & drop target mode on the node list background, which is used to parent
  38. // nodes back to the root node
  39. ListView@ list = sceneWindow.GetChild("NodeList");
  40. list.contentElement.dragDropMode = DD_TARGET;
  41. list.scrollPanel.dragDropMode = DD_TARGET;
  42. SubscribeToEvent(sceneWindow.GetChild("CloseButton", true), "Released", "HideSceneWindow");
  43. SubscribeToEvent(sceneWindow.GetChild("ExpandAllButton", true), "Released", "ExpandSceneHierarchy");
  44. SubscribeToEvent(sceneWindow.GetChild("CollapseAllButton", true), "Released", "CollapseSceneHierarchy");
  45. SubscribeToEvent(sceneWindow.GetChild("NodeList", true), "SelectionChanged", "HandleSceneWindowSelectionChange");
  46. SubscribeToEvent(sceneWindow.GetChild("NodeList", true), "ItemDoubleClicked", "HandleSceneWindowItemDoubleClick");
  47. SubscribeToEvent(sceneWindow.GetChild("NodeList", true), "UnhandledKey", "HandleSceneWindowKey");
  48. SubscribeToEvent(newNodeList, "ItemSelected", "HandleCreateNode");
  49. SubscribeToEvent(newComponentList, "ItemSelected", "HandleCreateComponent");
  50. SubscribeToEvent("DragDropTest", "HandleDragDropTest");
  51. SubscribeToEvent("DragDropFinish", "HandleDragDropFinish");
  52. SubscribeToEvent("BoneHierarchyCreated", "HandleBoneHierarchyCreated");
  53. SubscribeToEvent("TerrainCreated", "HandleTerrainCreated");
  54. }
  55. void ShowSceneWindow()
  56. {
  57. sceneWindow.visible = true;
  58. sceneWindow.BringToFront();
  59. }
  60. void HideSceneWindow()
  61. {
  62. sceneWindow.visible = false;
  63. }
  64. void ExpandSceneHierarchy()
  65. {
  66. ListView@ list = sceneWindow.GetChild("NodeList", true);
  67. list.SetChildItemsVisible(true);
  68. }
  69. void CollapseSceneHierarchy()
  70. {
  71. ListView@ list = sceneWindow.GetChild("NodeList", true);
  72. list.contentElement.DisableLayoutUpdate();
  73. // Show root-level nodes, but no components
  74. for (uint i = 0; i < list.numItems; ++i)
  75. {
  76. UIElement@ item = list.items[i];
  77. int indent = item.vars["Indent"].GetInt();
  78. int type = item.vars["Type"].GetInt();
  79. if (type == ITEM_COMPONENT || indent > 1)
  80. item.visible = false;
  81. else
  82. item.visible = true;
  83. }
  84. list.contentElement.EnableLayoutUpdate();
  85. list.contentElement.UpdateLayout();
  86. }
  87. void ClearSceneWindow()
  88. {
  89. if (sceneWindow is null)
  90. return;
  91. ListView@ list = sceneWindow.GetChild("NodeList", true);
  92. list.RemoveAllItems();
  93. }
  94. void UpdateSceneWindow()
  95. {
  96. ClearSceneWindow();
  97. UpdateSceneWindowNode(0, editorScene);
  98. // Clear copybuffer when whole window refreshed
  99. copyBuffer.Clear();
  100. }
  101. uint UpdateSceneWindowNode(uint itemIndex, Node@ node)
  102. {
  103. ListView@ list = sceneWindow.GetChild("NodeList", true);
  104. // Whenever we're updating the root, disable layout update to optimize speed
  105. if (node is editorScene)
  106. list.contentElement.DisableLayoutUpdate();
  107. // Remove old item if exists
  108. if (itemIndex < list.numItems && (node is null || (list.items[itemIndex].vars["Type"].GetInt() == ITEM_NODE &&
  109. list.items[itemIndex].vars["NodeID"].GetUInt() == node.id)))
  110. list.RemoveItem(itemIndex);
  111. if (node is null)
  112. return itemIndex;
  113. int indent = GetNodeIndent(node);
  114. Text@ text = Text();
  115. text.SetStyle(uiStyle, "FileSelectorListText");
  116. text.vars["Type"] = ITEM_NODE;
  117. text.vars["NodeID"] = node.id;
  118. text.vars["Indent"] = indent;
  119. text.text = GetNodeTitle(node, indent);
  120. // Nodes can be moved by drag and drop. The root node (scene) can not.
  121. if (node.typeName == "Node")
  122. text.dragDropMode = DD_SOURCE_AND_TARGET;
  123. else
  124. text.dragDropMode = DD_TARGET;
  125. list.InsertItem(itemIndex, text);
  126. ++itemIndex;
  127. // Update components first
  128. for (uint j = 0; j < node.numComponents; ++j)
  129. {
  130. Component@ component = node.components[j];
  131. AddComponentToSceneWindow(component, indent + 1, itemIndex);
  132. ++itemIndex;
  133. }
  134. // Then update child nodes recursively
  135. for (uint i = 0; i < node.numChildren; ++i)
  136. {
  137. Node@ childNode = node.children[i];
  138. itemIndex = UpdateSceneWindowNode(itemIndex, childNode);
  139. }
  140. // Re-enable layout update (and do manual layout) now
  141. if (node is editorScene)
  142. {
  143. list.contentElement.EnableLayoutUpdate();
  144. list.contentElement.UpdateLayout();
  145. }
  146. return itemIndex;
  147. }
  148. void UpdateSceneWindowNodeOnly(uint itemIndex, Node@ node)
  149. {
  150. ListView@ list = sceneWindow.GetChild("NodeList", true);
  151. int indent = GetNodeIndent(node);
  152. Text@ text = list.items[itemIndex];
  153. if (text is null)
  154. return;
  155. text.text = GetNodeTitle(node, indent);
  156. }
  157. void UpdateSceneWindowNode(Node@ node)
  158. {
  159. uint index = GetNodeListIndex(node);
  160. if (index == NO_ITEM)
  161. index = GetParentAddIndex(node);
  162. UpdateSceneWindowNode(index, node);
  163. }
  164. void UpdateSceneWindowNodeOnly(Node@ node)
  165. {
  166. uint index = GetNodeListIndex(node);
  167. UpdateSceneWindowNodeOnly(index, node);
  168. }
  169. void AddComponentToSceneWindow(Component@ component, int indent, uint compItemIndex)
  170. {
  171. ListView@ list = sceneWindow.GetChild("NodeList", true);
  172. Text@ text = Text();
  173. text.SetStyle(uiStyle, "FileSelectorListText");
  174. text.vars["Type"] = ITEM_COMPONENT;
  175. text.vars["NodeID"] = component.node.id;
  176. text.vars["ComponentID"] = component.id;
  177. text.vars["Indent"] = indent;
  178. text.text = GetComponentTitle(component, indent);
  179. list.InsertItem(compItemIndex, text);
  180. }
  181. uint GetNodeListIndex(Node@ node)
  182. {
  183. if (node is null)
  184. return NO_ITEM;
  185. ListView@ list = sceneWindow.GetChild("NodeList", true);
  186. uint numItems = list.numItems;
  187. uint nodeID = node.id;
  188. for (uint i = 0; i < numItems; ++i)
  189. {
  190. UIElement@ item = list.items[i];
  191. if (item.vars["Type"].GetInt() == ITEM_NODE && item.vars["NodeID"].GetUInt() == nodeID)
  192. return i;
  193. }
  194. return NO_ITEM;
  195. }
  196. uint GetParentAddIndex(Node@ node)
  197. {
  198. if (node is null || node.parent is null)
  199. return NO_ITEM;
  200. ListView@ list = sceneWindow.GetChild("NodeList", true);
  201. uint numItems = list.numItems;
  202. uint parentID = node.parent.id;
  203. for (uint i = 0; i < numItems; ++i)
  204. {
  205. UIElement@ item = list.items[i];
  206. if (item.vars["Type"].GetInt() == ITEM_NODE && item.vars["NodeID"].GetUInt() == parentID)
  207. {
  208. int indent = item.vars["Indent"].GetInt();
  209. for (uint j = i + 1; j < numItems; ++j)
  210. {
  211. // Scan for the next node on this or lower level; that is the place to insert the new child node
  212. if (list.items[j].vars["Indent"].GetInt() <= indent)
  213. return j;
  214. }
  215. return numItems;
  216. }
  217. }
  218. return NO_ITEM;
  219. }
  220. uint GetNodeListIndex(Node@ node, uint startPos)
  221. {
  222. if (node is null)
  223. return NO_ITEM;
  224. ListView@ list = sceneWindow.GetChild("NodeList", true);
  225. uint numItems = list.numItems;
  226. uint nodeID = node.id;
  227. for (uint i = startPos; i < numItems; --i)
  228. {
  229. UIElement@ item = list.items[i];
  230. if (item.vars["Type"].GetInt() == ITEM_NODE && item.vars["NodeID"].GetInt() == int(nodeID))
  231. return i;
  232. }
  233. return NO_ITEM;
  234. }
  235. Node@ GetListNode(uint index)
  236. {
  237. ListView@ list = sceneWindow.GetChild("NodeList", true);
  238. UIElement@ item = list.items[index];
  239. if (item is null)
  240. return null;
  241. return editorScene.GetNode(item.vars["NodeID"].GetUInt());
  242. }
  243. Component@ GetListComponent(uint index)
  244. {
  245. ListView@ list = sceneWindow.GetChild("NodeList", true);
  246. UIElement@ item = list.items[index];
  247. return GetListComponent(item);
  248. }
  249. Component@ GetListComponent(UIElement@ item)
  250. {
  251. if (item is null)
  252. return null;
  253. if (item.vars["Type"].GetInt() != ITEM_COMPONENT)
  254. return null;
  255. return editorScene.GetComponent(item.vars["ComponentID"].GetUInt());
  256. }
  257. uint GetComponentListIndex(Component@ component)
  258. {
  259. if (component is null)
  260. return NO_ITEM;
  261. ListView@ list = sceneWindow.GetChild("NodeList", true);
  262. uint numItems = list.numItems;
  263. for (uint i = 0; i < numItems; ++i)
  264. {
  265. UIElement@ item = list.items[i];
  266. if (item.vars["Type"].GetInt() == ITEM_COMPONENT && item.vars["ComponentID"].GetUInt() == component.id)
  267. return i;
  268. }
  269. return NO_ITEM;
  270. }
  271. int GetNodeIndent(Node@ node)
  272. {
  273. int indent = 0;
  274. for (;;)
  275. {
  276. if (node.parent is null)
  277. break;
  278. ++indent;
  279. node = node.parent;
  280. }
  281. return indent;
  282. }
  283. String GetNodeTitle(Node@ node, int indent)
  284. {
  285. String indentStr;
  286. indentStr.Resize(indent);
  287. for (int i = 0; i < indent; ++i)
  288. indentStr[i] = ' ';
  289. String idStr;
  290. if (node.id >= FIRST_LOCAL_ID)
  291. idStr = "Local " + String(node.id - FIRST_LOCAL_ID);
  292. else
  293. idStr = String(node.id);
  294. if (node.name.empty)
  295. return indentStr + node.typeName + " (" + idStr + ")";
  296. else
  297. return indentStr + node.name + " (" + idStr + ")";
  298. }
  299. String GetComponentTitle(Component@ component, int indent)
  300. {
  301. String indentStr;
  302. String localStr;
  303. indentStr.Resize(indent);
  304. for (int i = 0; i < indent; ++i)
  305. indentStr[i] = ' ';
  306. if (component.id >= FIRST_LOCAL_ID)
  307. localStr = " (Local)";
  308. return indentStr + component.typeName + localStr;
  309. }
  310. void SelectNode(Node@ node, bool multiselect)
  311. {
  312. ListView@ list = sceneWindow.GetChild("NodeList", true);
  313. if (node is null && !multiselect)
  314. {
  315. list.ClearSelection();
  316. return;
  317. }
  318. uint nodeItem = GetNodeListIndex(node);
  319. // Go in the parent chain up to the first non-root level to make sure the chain is expanded
  320. for (;;)
  321. {
  322. Node@ parent = node.parent;
  323. if (node is editorScene || parent is editorScene || parent is null)
  324. break;
  325. node = parent;
  326. }
  327. uint numItems = list.numItems;
  328. uint parentItem = GetNodeListIndex(node);
  329. if (nodeItem < numItems)
  330. {
  331. // Expand the node chain now, but do not expand the whole scene in case the component was in the root
  332. if (!multiselect || !list.IsSelected(nodeItem))
  333. {
  334. list.items[nodeItem].visible = true;
  335. if (parentItem != 0 && parentItem < numItems)
  336. list.SetChildItemsVisible(parentItem, true);
  337. }
  338. // This causes an event to be sent, in response we set the node/component selections, and refresh editors
  339. if (!multiselect)
  340. list.selection = nodeItem;
  341. else
  342. list.ToggleSelection(nodeItem);
  343. }
  344. else
  345. {
  346. if (!multiselect)
  347. list.ClearSelection();
  348. }
  349. }
  350. void SelectComponent(Component@ component, bool multiselect)
  351. {
  352. ListView@ list = sceneWindow.GetChild("NodeList", true);
  353. if (component is null)
  354. {
  355. if (!multiselect)
  356. list.ClearSelection();
  357. return;
  358. }
  359. Node@ node = component.node;
  360. if (node is null)
  361. {
  362. if (!multiselect)
  363. list.ClearSelection();
  364. return;
  365. }
  366. // Go in the parent chain up to the first non-root level to make sure the chain is expanded
  367. for (;;)
  368. {
  369. Node@ parent = node.parent;
  370. if (node is editorScene || parent is editorScene || parent is null)
  371. break;
  372. node = parent;
  373. }
  374. uint numItems = list.numItems;
  375. uint nodeItem = GetNodeListIndex(node);
  376. uint componentItem = GetComponentListIndex(component);
  377. if (nodeItem >= list.numItems)
  378. {
  379. if (!multiselect)
  380. list.ClearSelection();
  381. return;
  382. }
  383. if (nodeItem < numItems && componentItem < numItems)
  384. {
  385. // Expand the node chain now, but do not expand the whole scene in case the component was in the root
  386. if (!multiselect || !list.IsSelected(componentItem))
  387. {
  388. list.items[nodeItem].visible = true;
  389. if (nodeItem != 0)
  390. list.SetChildItemsVisible(nodeItem, true);
  391. list.items[componentItem].visible = true;
  392. }
  393. // This causes an event to be sent, in response we set the node/component selections, and refresh editors
  394. if (!multiselect)
  395. list.selection = componentItem;
  396. else
  397. list.ToggleSelection(componentItem);
  398. }
  399. else
  400. {
  401. if (!multiselect)
  402. list.ClearSelection();
  403. }
  404. }
  405. void HandleSceneWindowSelectionChange()
  406. {
  407. if (inSelectionModify)
  408. return;
  409. ClearSelection();
  410. ListView@ list = sceneWindow.GetChild("NodeList", true);
  411. Array<uint> indices = list.selections;
  412. for (uint i = 0; i < indices.length; ++i)
  413. {
  414. uint index = indices[i];
  415. UIElement@ item = list.items[index];
  416. int type = item.vars["Type"].GetInt();
  417. if (type == ITEM_COMPONENT)
  418. {
  419. Component@ comp = GetListComponent(index);
  420. if (comp !is null)
  421. selectedComponents.Push(comp);
  422. }
  423. else if (type == ITEM_NODE)
  424. {
  425. Node@ node = GetListNode(index);
  426. if (node !is null)
  427. selectedNodes.Push(node);
  428. }
  429. }
  430. // If only one node selected, use it for editing
  431. if (selectedNodes.length == 1)
  432. editNode = selectedNodes[0];
  433. // If selection contains only components, and they have a common node, use it for editing
  434. if (selectedNodes.empty && !selectedComponents.empty)
  435. {
  436. Node@ commonNode;
  437. for (uint i = 0; i < selectedComponents.length; ++i)
  438. {
  439. if (i == 0)
  440. commonNode = selectedComponents[i].node;
  441. else
  442. {
  443. if (selectedComponents[i].node !is commonNode)
  444. commonNode = null;
  445. }
  446. }
  447. editNode = commonNode;
  448. }
  449. // Now check if the component(s) can be edited. If many selected, must have same type
  450. if (!selectedComponents.empty)
  451. {
  452. ShortStringHash compType = selectedComponents[0].type;
  453. bool sameType = true;
  454. for (uint i = 1; i < selectedComponents.length; ++i)
  455. {
  456. if (selectedComponents[i].type != compType)
  457. {
  458. sameType = false;
  459. break;
  460. }
  461. }
  462. if (sameType)
  463. editComponents = selectedComponents;
  464. }
  465. // If just nodes selected, and no components, show the first component(s) for editing if possible
  466. if (!selectedNodes.empty && selectedComponents.empty && selectedNodes[0].numComponents > 0)
  467. {
  468. ShortStringHash compType = selectedNodes[0].components[0].type;
  469. bool sameType = true;
  470. for (uint i = 1; i < selectedNodes.length; ++i)
  471. {
  472. if (selectedNodes[i].numComponents == 0 || selectedNodes[i].components[0].type != compType)
  473. {
  474. sameType = false;
  475. break;
  476. }
  477. }
  478. if (sameType)
  479. {
  480. for (uint i = 0; i < selectedNodes.length; ++i)
  481. editComponents.Push(selectedNodes[i].components[0]);
  482. }
  483. }
  484. if (selectedNodes.empty && editNode !is null)
  485. editNodes.Push(editNode);
  486. else
  487. editNodes = selectedNodes;
  488. PositionGizmo();
  489. UpdateNodeWindow();
  490. }
  491. void HandleSceneWindowItemDoubleClick(StringHash eventType, VariantMap& eventData)
  492. {
  493. ListView@ list = sceneWindow.GetChild("NodeList", true);
  494. uint index = eventData["Selection"].GetUInt();
  495. if (index == 0)
  496. {
  497. if (list.numItems > 1 && list.items[1].visible == true)
  498. CollapseSceneHierarchy();
  499. else
  500. list.ToggleChildItemsVisible(index);
  501. }
  502. else
  503. list.ToggleChildItemsVisible(index);
  504. }
  505. void HandleSceneWindowKey(StringHash eventType, VariantMap& eventData)
  506. {
  507. int key = eventData["Key"].GetInt();
  508. }
  509. void HandleDragDropTest(StringHash eventType, VariantMap& eventData)
  510. {
  511. UIElement@ source = eventData["Source"].GetUIElement();
  512. UIElement@ target = eventData["Target"].GetUIElement();
  513. eventData["Accept"] = TestSceneWindowElements(source, target);
  514. }
  515. void HandleDragDropFinish(StringHash eventType, VariantMap& eventData)
  516. {
  517. UIElement@ source = eventData["Source"].GetUIElement();
  518. UIElement@ target = eventData["Target"].GetUIElement();
  519. bool accept = TestSceneWindowElements(source, target);
  520. eventData["Accept"] = accept;
  521. if (!accept)
  522. return;
  523. Node@ sourceNode = editorScene.GetNode(source.vars["NodeID"].GetUInt());
  524. Node@ targetNode = editorScene.GetNode(target.vars["NodeID"].GetUInt());
  525. // If target is null, parent to scene
  526. if (targetNode is null)
  527. targetNode = editorScene;
  528. // Perform the reparenting
  529. // Set transform so that the world transform stays through the parent change
  530. BeginModify(targetNode.id);
  531. BeginModify(sourceNode.id);
  532. sourceNode.parent = targetNode;
  533. EndModify(sourceNode.id);
  534. EndModify(targetNode.id);
  535. // Verify success
  536. if (sourceNode.parent !is targetNode)
  537. return;
  538. // Update the node list now. If a node was moved into the root, this potentially refreshes the whole scene window.
  539. // Therefore disable layout update first
  540. ListView@ list = sceneWindow.GetChild("NodeList", true);
  541. uint sourceIndex = GetNodeListIndex(sourceNode);
  542. bool expanded = SaveExpandedStatus(sourceIndex);
  543. list.RemoveItem(sourceIndex);
  544. uint addIndex = GetParentAddIndex(sourceNode);
  545. UpdateSceneWindowNode(addIndex, sourceNode);
  546. UpdateNodeAttributes();
  547. RestoreExpandedStatus(addIndex, expanded);
  548. }
  549. bool TestSceneWindowElements(UIElement@ source, UIElement@ target)
  550. {
  551. // Test for validity of reparenting by drag and drop
  552. Node@ sourceNode;
  553. Node@ targetNode;
  554. if (source.vars.Contains("NodeID"))
  555. sourceNode = editorScene.GetNode(source.vars["NodeID"].GetUInt());
  556. if (target.vars.Contains("NodeID"))
  557. editorScene.GetNode(target.vars["NodeID"].GetUInt());
  558. if (sourceNode is null)
  559. return false;
  560. if (sourceNode is editorScene)
  561. return false;
  562. if (targetNode !is null)
  563. {
  564. if (sourceNode.parent is targetNode)
  565. return false;
  566. if (targetNode.parent is sourceNode)
  567. return false;
  568. }
  569. return true;
  570. }
  571. void UpdateAndFocusNode(Node@ node)
  572. {
  573. UpdateSceneWindowNode(node);
  574. uint index = GetNodeListIndex(node);
  575. ListView@ list = sceneWindow.GetChild("NodeList", true);
  576. list.selection = index;
  577. }
  578. void UpdateAndFocusComponent(Component@ component)
  579. {
  580. UpdateSceneWindowNode(component.node);
  581. uint index = GetComponentListIndex(component);
  582. ListView@ list = sceneWindow.GetChild("NodeList", true);
  583. list.selection = index;
  584. }
  585. void HandleCreateNode(StringHash eventType, VariantMap& eventData)
  586. {
  587. DropDownList@ list = eventData["Element"].GetUIElement();
  588. uint mode = list.selection;
  589. if (mode >= list.numItems)
  590. return;
  591. Node@ newNode = editorScene.CreateChild("", mode == 0 ? REPLICATED : LOCAL);
  592. // Set the new node a certain distance from the camera
  593. newNode.position = GetNewNodePosition();
  594. UpdateAndFocusNode(newNode);
  595. }
  596. void HandleCreateComponent(StringHash eventType, VariantMap& eventData)
  597. {
  598. if (editNode is null)
  599. return;
  600. DropDownList@ list = eventData["Element"].GetUIElement();
  601. Text@ text = list.selectedItem;
  602. if (text is null)
  603. return;
  604. // If this is the root node, do not allow to create duplicate scene-global components
  605. if (editNode is editorScene && CheckForExistingGlobalComponent(editNode, text.text))
  606. return;
  607. // For now, make a local node's all components local
  608. /// \todo Allow to specify the createmode
  609. Component@ newComponent = editNode.CreateComponent(text.text, editNode.id < FIRST_LOCAL_ID ? REPLICATED : LOCAL);
  610. UpdateAndFocusComponent(newComponent);
  611. }
  612. void HandleBoneHierarchyCreated(StringHash eventType, VariantMap& eventData)
  613. {
  614. Node@ node = eventData["Node"].GetNode();
  615. ListView@ list = sceneWindow.GetChild("NodeList", true);
  616. if (list.numItems > 0 && GetNodeListIndex(node) != NO_ITEM)
  617. UpdateSceneWindowNode(node);
  618. }
  619. void HandleTerrainCreated(StringHash eventType, VariantMap& eventData)
  620. {
  621. Node@ node = eventData["Node"].GetNode();
  622. ListView@ list = sceneWindow.GetChild("NodeList", true);
  623. if (list.numItems > 0 && GetNodeListIndex(node) != NO_ITEM)
  624. UpdateSceneWindowNode(node);
  625. }
  626. void CreateBuiltinObject(const String& name)
  627. {
  628. Node@ newNode = editorScene.CreateChild(name, REPLICATED);
  629. // Set the new node a certain distance from the camera
  630. newNode.position = GetNewNodePosition();
  631. StaticModel@ object = newNode.CreateComponent("StaticModel");
  632. object.model = cache.GetResource("Model", "Models/" + name + ".mdl");
  633. UpdateAndFocusNode(newNode);
  634. }
  635. bool CheckSceneWindowFocus()
  636. {
  637. // When we do scene operations based on key shortcuts, make sure either the 3D scene or the node list is focused,
  638. // not for example a file selector
  639. ListView@ list = sceneWindow.GetChild("NodeList", true);
  640. if (ui.focusElement is list || ui.focusElement is null)
  641. return true;
  642. else
  643. return false;
  644. }
  645. bool CheckForExistingGlobalComponent(Node@ node, const String&in typeName)
  646. {
  647. if (typeName != "Octree" && typeName != "PhysicsWorld" && typeName != "DebugRenderer")
  648. return false;
  649. else
  650. return node.HasComponent(typeName);
  651. }
  652. bool SaveExpandedStatus(uint itemIndex)
  653. {
  654. ListView@ list = sceneWindow.GetChild("NodeList", true);
  655. uint nextIndex = itemIndex + 1;
  656. if (nextIndex < list.numItems && list.items[nextIndex].vars["Indent"].GetInt() > list.items[itemIndex].vars["Indent"].GetInt()
  657. && list.items[nextIndex].visible == false)
  658. return false;
  659. else
  660. return true;
  661. }
  662. void RestoreExpandedStatus(uint itemIndex, bool expanded)
  663. {
  664. ListView@ list = sceneWindow.GetChild("NodeList", true);
  665. list.SetChildItemsVisible(itemIndex, expanded);
  666. }