EditorHierarchyWindow.as 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. // Urho3D editor hierarchy window handling
  2. const int ITEM_NONE = 0;
  3. const int ITEM_NODE = 1;
  4. const int ITEM_COMPONENT = 2;
  5. const int ITEM_UI_ELEMENT = 3;
  6. const uint NO_ITEM = M_MAX_UNSIGNED;
  7. const StringHash SCENE_TYPE("Scene");
  8. const StringHash NODE_TYPE("Node");
  9. const StringHash STATICMODEL_TYPE("StaticModel");
  10. const StringHash ANIMATEDMODEL_TYPE("AnimatedModel");
  11. const StringHash STATICMODELGROUP_TYPE("StaticModelGroup");
  12. const StringHash SPLINEPATH_TYPE("SplinePath");
  13. const StringHash CONSTRAINT_TYPE("Constraint");
  14. const String NO_CHANGE(uint8(0));
  15. const StringHash TYPE_VAR("Type");
  16. const StringHash NODE_ID_VAR("NodeID");
  17. const StringHash COMPONENT_ID_VAR("ComponentID");
  18. const StringHash UI_ELEMENT_ID_VAR("UIElementID");
  19. const StringHash DRAGDROPCONTENT_VAR("DragDropContent");
  20. const StringHash[] ID_VARS = { StringHash(""), NODE_ID_VAR, COMPONENT_ID_VAR, UI_ELEMENT_ID_VAR };
  21. Color nodeTextColor(1.0f, 1.0f, 1.0f);
  22. Color componentTextColor(0.7f, 1.0f, 0.7f);
  23. Window@ hierarchyWindow;
  24. ListView@ hierarchyList;
  25. // UIElement does not have unique ID, so use a running number to generate a new ID each time an item is inserted into hierarchy list
  26. const uint UI_ELEMENT_BASE_ID = 1;
  27. uint uiElementNextID = UI_ELEMENT_BASE_ID;
  28. bool showInternalUIElement = false;
  29. bool showTemporaryObject = false;
  30. Array<uint> hierarchyUpdateSelections;
  31. Variant GetUIElementID(UIElement@ element)
  32. {
  33. Variant elementID = element.GetVar(UI_ELEMENT_ID_VAR);
  34. if (elementID.empty)
  35. {
  36. // Generate new ID
  37. elementID = uiElementNextID++;
  38. // Store the generated ID
  39. element.vars[UI_ELEMENT_ID_VAR] = elementID;
  40. }
  41. return elementID;
  42. }
  43. UIElement@ GetUIElementByID(const Variant&in id)
  44. {
  45. return id == UI_ELEMENT_BASE_ID ? editorUIElement : editorUIElement.GetChild(UI_ELEMENT_ID_VAR, id, true);
  46. }
  47. void CreateHierarchyWindow()
  48. {
  49. if (hierarchyWindow !is null)
  50. return;
  51. hierarchyWindow = ui.LoadLayout(cache.GetResource("XMLFile", "UI/EditorHierarchyWindow.xml"));
  52. hierarchyList = hierarchyWindow.GetChild("HierarchyList");
  53. ui.root.AddChild(hierarchyWindow);
  54. int height = Min(ui.root.height - 60, 460);
  55. hierarchyWindow.SetSize(300, height);
  56. hierarchyWindow.SetPosition(35, 100);
  57. hierarchyWindow.opacity = uiMaxOpacity;
  58. hierarchyWindow.BringToFront();
  59. UpdateHierarchyItem(editorScene);
  60. // Set selection to happen on click end, so that we can drag nodes to the inspector without resetting the inspector view
  61. hierarchyList.selectOnClickEnd = true;
  62. // Set drag & drop target mode on the node list background, which is used to parent nodes back to the root node
  63. hierarchyList.contentElement.dragDropMode = DD_TARGET;
  64. hierarchyList.scrollPanel.dragDropMode = DD_TARGET;
  65. SubscribeToEvent(hierarchyWindow.GetChild("CloseButton", true), "Released", "HideHierarchyWindow");
  66. SubscribeToEvent(hierarchyWindow.GetChild("ExpandButton", true), "Released", "ExpandCollapseHierarchy");
  67. SubscribeToEvent(hierarchyWindow.GetChild("CollapseButton", true), "Released", "ExpandCollapseHierarchy");
  68. SubscribeToEvent(hierarchyList, "SelectionChanged", "HandleHierarchyListSelectionChange");
  69. SubscribeToEvent(hierarchyList, "ItemDoubleClicked", "HandleHierarchyListDoubleClick");
  70. SubscribeToEvent(hierarchyList, "ItemClicked", "HandleHierarchyItemClick");
  71. SubscribeToEvent("DragDropTest", "HandleDragDropTest");
  72. SubscribeToEvent("DragDropFinish", "HandleDragDropFinish");
  73. SubscribeToEvent(editorScene, "NodeAdded", "HandleNodeAdded");
  74. SubscribeToEvent(editorScene, "NodeRemoved", "HandleNodeRemoved");
  75. SubscribeToEvent(editorScene, "ComponentAdded", "HandleComponentAdded");
  76. SubscribeToEvent(editorScene, "ComponentRemoved", "HandleComponentRemoved");
  77. SubscribeToEvent(editorScene, "NodeNameChanged", "HandleNodeNameChanged");
  78. SubscribeToEvent(editorScene, "NodeEnabledChanged", "HandleNodeEnabledChanged");
  79. SubscribeToEvent(editorScene, "ComponentEnabledChanged", "HandleComponentEnabledChanged");
  80. SubscribeToEvent("TemporaryChanged", "HandleTemporaryChanged");
  81. }
  82. bool ShowHierarchyWindow()
  83. {
  84. hierarchyWindow.visible = true;
  85. hierarchyWindow.BringToFront();
  86. return true;
  87. }
  88. void HideHierarchyWindow()
  89. {
  90. hierarchyWindow.visible = false;
  91. }
  92. void ExpandCollapseHierarchy(StringHash eventType, VariantMap& eventData)
  93. {
  94. Button@ button = eventData["Element"].GetPtr();
  95. bool enable = button.name == "ExpandButton";
  96. CheckBox@ checkBox = hierarchyWindow.GetChild("AllCheckBox", true);
  97. bool all = checkBox.checked;
  98. checkBox.checked = false; // Auto-reset
  99. Array<uint> selections = hierarchyList.selections;
  100. for (uint i = 0; i < selections.length; ++i)
  101. hierarchyList.Expand(selections[i], enable, all);
  102. }
  103. void EnableExpandCollapseButtons(bool enable)
  104. {
  105. String[] buttons = { "ExpandButton", "CollapseButton", "AllCheckBox" };
  106. for (uint i = 0; i < buttons.length; ++i)
  107. {
  108. UIElement@ element = hierarchyWindow.GetChild(buttons[i], true);
  109. element.enabled = enable;
  110. element.children[0].color = enable ? normalTextColor : nonEditableTextColor;
  111. }
  112. }
  113. void UpdateHierarchyItem(Serializable@ serializable, bool clear = false)
  114. {
  115. if (clear)
  116. {
  117. // Remove the current selection before updating the list item (in turn trigger an update on the attribute editor)
  118. hierarchyList.ClearSelection();
  119. // Clear copybuffer when whole window refreshed
  120. sceneCopyBuffer.Clear();
  121. uiElementCopyBuffer.Clear();
  122. }
  123. // In case of item's parent is not found in the hierarchy list then the item will be inserted at the list root level
  124. Serializable@ parent;
  125. switch (GetType(serializable))
  126. {
  127. case ITEM_NODE:
  128. parent = cast<Node>(serializable).parent;
  129. break;
  130. case ITEM_COMPONENT:
  131. parent = cast<Component>(serializable).node;
  132. break;
  133. case ITEM_UI_ELEMENT:
  134. parent = cast<UIElement>(serializable).parent;
  135. break;
  136. default:
  137. break;
  138. }
  139. UIElement@ parentItem = hierarchyList.items[GetListIndex(parent)];
  140. UpdateHierarchyItem(GetListIndex(serializable), serializable, parentItem);
  141. }
  142. uint UpdateHierarchyItem(uint itemIndex, Serializable@ serializable, UIElement@ parentItem)
  143. {
  144. // Whenever we're updating, disable layout update to optimize speed
  145. hierarchyList.contentElement.DisableLayoutUpdate();
  146. if (serializable is null)
  147. {
  148. hierarchyList.RemoveItem(itemIndex);
  149. hierarchyList.contentElement.EnableLayoutUpdate();
  150. hierarchyList.contentElement.UpdateLayout();
  151. return itemIndex;
  152. }
  153. int itemType = GetType(serializable);
  154. Variant id = GetID(serializable, itemType);
  155. // Remove old item if exists
  156. if (itemIndex < hierarchyList.numItems && MatchID(hierarchyList.items[itemIndex], id, itemType))
  157. hierarchyList.RemoveItem(itemIndex);
  158. Text@ text = Text();
  159. hierarchyList.InsertItem(itemIndex, text, parentItem);
  160. text.style = "FileSelectorListText";
  161. if (serializable.type == SCENE_TYPE || serializable is editorUIElement)
  162. // The root node (scene) and editor's root UIElement cannot be moved by drag and drop
  163. text.dragDropMode = DD_TARGET;
  164. else
  165. // Internal UIElement is not able to participate in drag and drop action
  166. text.dragDropMode = itemType == ITEM_UI_ELEMENT && cast<UIElement>(serializable).internal ? DD_DISABLED : DD_SOURCE_AND_TARGET;
  167. // Advance the index for the child items
  168. if (itemIndex == M_MAX_UNSIGNED)
  169. itemIndex = hierarchyList.numItems;
  170. else
  171. ++itemIndex;
  172. String iconType = serializable.typeName;
  173. if (serializable is editorUIElement)
  174. iconType = "Root" + iconType;
  175. IconizeUIElement(text, iconType);
  176. SetID(text, serializable, itemType);
  177. switch (itemType)
  178. {
  179. case ITEM_NODE:
  180. {
  181. Node@ node = cast<Node>(serializable);
  182. text.text = GetNodeTitle(node);
  183. text.color = nodeTextColor;
  184. SetIconEnabledColor(text, node.enabled);
  185. // Update components first
  186. for (uint i = 0; i < node.numComponents; ++i)
  187. {
  188. Component@ component = node.components[i];
  189. if (showTemporaryObject || !component.temporary)
  190. AddComponentItem(itemIndex++, component, text);
  191. }
  192. // Then update child nodes recursively
  193. for (uint i = 0; i < node.numChildren; ++i)
  194. {
  195. Node@ childNode = node.children[i];
  196. if (showTemporaryObject || !childNode.temporary)
  197. itemIndex = UpdateHierarchyItem(itemIndex, childNode, text);
  198. }
  199. break;
  200. }
  201. case ITEM_COMPONENT:
  202. {
  203. Component@ component = cast<Component>(serializable);
  204. text.text = GetComponentTitle(component);
  205. text.color = componentTextColor;
  206. SetIconEnabledColor(text, component.enabledEffective);
  207. break;
  208. }
  209. case ITEM_UI_ELEMENT:
  210. {
  211. UIElement@ element = cast<UIElement>(serializable);
  212. text.text = GetUIElementTitle(element);
  213. SetIconEnabledColor(text, element.visible);
  214. // Update child elements recursively
  215. for (uint i = 0; i < element.numChildren; ++i)
  216. {
  217. UIElement@ childElement = element.children[i];
  218. if ((showInternalUIElement || !childElement.internal) && (showTemporaryObject || !childElement.temporary))
  219. itemIndex = UpdateHierarchyItem(itemIndex, childElement, text);
  220. }
  221. break;
  222. }
  223. default:
  224. break;
  225. }
  226. // Re-enable layout update (and do manual layout) now
  227. hierarchyList.contentElement.EnableLayoutUpdate();
  228. hierarchyList.contentElement.UpdateLayout();
  229. return itemIndex;
  230. }
  231. void UpdateHierarchyItemText(uint itemIndex, bool iconEnabled, const String&in textTitle = NO_CHANGE)
  232. {
  233. Text@ text = hierarchyList.items[itemIndex];
  234. if (text is null)
  235. return;
  236. SetIconEnabledColor(text, iconEnabled);
  237. if (textTitle != NO_CHANGE)
  238. text.text = textTitle;
  239. }
  240. void AddComponentItem(uint compItemIndex, Component@ component, UIElement@ parentItem)
  241. {
  242. Text@ text = Text();
  243. hierarchyList.InsertItem(compItemIndex, text, parentItem);
  244. text.style = "FileSelectorListText";
  245. text.vars[TYPE_VAR] = ITEM_COMPONENT;
  246. text.vars[NODE_ID_VAR] = component.node.id;
  247. text.vars[COMPONENT_ID_VAR] = component.id;
  248. text.text = GetComponentTitle(component);
  249. text.color = componentTextColor;
  250. // Components currently act only as drag targets
  251. text.dragDropMode = DD_TARGET;
  252. IconizeUIElement(text, component.typeName);
  253. SetIconEnabledColor(text, component.enabledEffective);
  254. }
  255. int GetType(Serializable@ serializable)
  256. {
  257. if (cast<Node>(serializable) !is null)
  258. return ITEM_NODE;
  259. else if (cast<Component>(serializable) !is null)
  260. return ITEM_COMPONENT;
  261. else if (cast<UIElement>(serializable) !is null)
  262. return ITEM_UI_ELEMENT;
  263. else
  264. return ITEM_NONE;
  265. }
  266. void SetID(Text@ text, Serializable@ serializable, int itemType = ITEM_NONE)
  267. {
  268. // If item type is not provided, auto detect it
  269. if (itemType == ITEM_NONE)
  270. itemType = GetType(serializable);
  271. text.vars[TYPE_VAR] = itemType;
  272. text.vars[ID_VARS[itemType]] = GetID(serializable, itemType);
  273. // Set node ID as drag and drop content for node ID editing
  274. if (itemType == ITEM_NODE)
  275. text.vars[DRAGDROPCONTENT_VAR] = String(text.vars[NODE_ID_VAR].GetUInt());
  276. switch (itemType)
  277. {
  278. case ITEM_COMPONENT:
  279. text.vars[NODE_ID_VAR] = cast<Component>(serializable).node.id;
  280. break;
  281. case ITEM_UI_ELEMENT:
  282. // Subscribe to UI-element events
  283. SubscribeToEvent(serializable, "NameChanged", "HandleElementNameChanged");
  284. SubscribeToEvent(serializable, "VisibleChanged", "HandleElementVisibilityChanged");
  285. SubscribeToEvent(serializable, "Resized", "HandleElementAttributeChanged");
  286. SubscribeToEvent(serializable, "Positioned", "HandleElementAttributeChanged");
  287. break;
  288. default:
  289. break;
  290. }
  291. }
  292. uint GetID(Serializable@ serializable, int itemType = ITEM_NONE)
  293. {
  294. // If item type is not provided, auto detect it
  295. if (itemType == ITEM_NONE)
  296. itemType = GetType(serializable);
  297. switch (itemType)
  298. {
  299. case ITEM_NODE:
  300. return cast<Node>(serializable).id;
  301. case ITEM_COMPONENT:
  302. return cast<Component>(serializable).id;
  303. case ITEM_UI_ELEMENT:
  304. return GetUIElementID(cast<UIElement>(serializable)).GetUInt();
  305. }
  306. return M_MAX_UNSIGNED;
  307. }
  308. bool MatchID(UIElement@ element, const Variant&in id, int itemType)
  309. {
  310. return element.GetVar(TYPE_VAR).GetInt() == itemType && element.GetVar(ID_VARS[itemType]) == id;
  311. }
  312. uint GetListIndex(Serializable@ serializable)
  313. {
  314. if (serializable is null)
  315. return NO_ITEM;
  316. int itemType = GetType(serializable);
  317. Variant id = GetID(serializable, itemType);
  318. uint numItems = hierarchyList.numItems;
  319. for (uint i = 0; i < numItems; ++i)
  320. {
  321. if (MatchID(hierarchyList.items[i], id, itemType))
  322. return i;
  323. }
  324. return NO_ITEM;
  325. }
  326. UIElement@ GetListUIElement(uint index)
  327. {
  328. UIElement@ item = hierarchyList.items[index];
  329. if (item is null)
  330. return null;
  331. // Get the text item's ID and use it to retrieve the actual UIElement the text item is associated to
  332. return GetUIElementByID(GetUIElementID(item));
  333. }
  334. Node@ GetListNode(uint index)
  335. {
  336. UIElement@ item = hierarchyList.items[index];
  337. if (item is null)
  338. return null;
  339. return editorScene.GetNode(item.vars[NODE_ID_VAR].GetUInt());
  340. }
  341. Component@ GetListComponent(uint index)
  342. {
  343. UIElement@ item = hierarchyList.items[index];
  344. return GetListComponent(item);
  345. }
  346. Component@ GetListComponent(UIElement@ item)
  347. {
  348. if (item is null)
  349. return null;
  350. if (item.vars[TYPE_VAR].GetInt() != ITEM_COMPONENT)
  351. return null;
  352. return editorScene.GetComponent(item.vars[COMPONENT_ID_VAR].GetUInt());
  353. }
  354. uint GetComponentListIndex(Component@ component)
  355. {
  356. if (component is null)
  357. return NO_ITEM;
  358. uint numItems = hierarchyList.numItems;
  359. for (uint i = 0; i < numItems; ++i)
  360. {
  361. UIElement@ item = hierarchyList.items[i];
  362. if (item.vars[TYPE_VAR].GetInt() == ITEM_COMPONENT && item.vars[COMPONENT_ID_VAR].GetUInt() == component.id)
  363. return i;
  364. }
  365. return NO_ITEM;
  366. }
  367. String GetUIElementTitle(UIElement@ element)
  368. {
  369. String ret;
  370. // Only top level UI-element has this variable
  371. String modifiedStr = element.GetVar(MODIFIED_VAR).GetBool() ? "*" : "";
  372. ret = (element.name.empty ? element.typeName : element.name) + modifiedStr + " [" + GetUIElementID(element).ToString() + "]";
  373. if (element.temporary)
  374. ret += " (Temp)";
  375. return ret;
  376. }
  377. String GetNodeTitle(Node@ node)
  378. {
  379. String ret;
  380. if (node.name.empty)
  381. ret = node.typeName;
  382. else
  383. ret = node.name;
  384. if (node.id >= FIRST_LOCAL_ID)
  385. ret += " (Local " + String(node.id) + ")";
  386. else
  387. ret += " (" + String(node.id) + ")";
  388. if (node.temporary)
  389. ret += " (Temp)";
  390. return ret;
  391. }
  392. String GetComponentTitle(Component@ component)
  393. {
  394. String ret = component.typeName;
  395. if (component.id >= FIRST_LOCAL_ID)
  396. ret += " (Local)";
  397. if (component.temporary)
  398. ret += " (Temp)";
  399. return ret;
  400. }
  401. void SelectNode(Node@ node, bool multiselect)
  402. {
  403. if (node is null && !multiselect)
  404. {
  405. hierarchyList.ClearSelection();
  406. return;
  407. }
  408. uint index = GetListIndex(node);
  409. uint numItems = hierarchyList.numItems;
  410. if (index < numItems)
  411. {
  412. // Expand the node chain now
  413. if (!multiselect || !hierarchyList.IsSelected(index))
  414. {
  415. // Go in the parent chain up to make sure the chain is expanded
  416. Node@ current = node;
  417. do
  418. {
  419. hierarchyList.Expand(GetListIndex(current), true);
  420. current = current.parent;
  421. }
  422. while (current !is null);
  423. }
  424. // This causes an event to be sent, in response we set the node/component selections, and refresh editors
  425. if (!multiselect)
  426. hierarchyList.selection = index;
  427. else
  428. hierarchyList.ToggleSelection(index);
  429. }
  430. else if (!multiselect)
  431. hierarchyList.ClearSelection();
  432. }
  433. void SelectComponent(Component@ component, bool multiselect)
  434. {
  435. if (component is null && !multiselect)
  436. {
  437. hierarchyList.ClearSelection();
  438. return;
  439. }
  440. Node@ node = component.node;
  441. if (node is null && !multiselect)
  442. {
  443. hierarchyList.ClearSelection();
  444. return;
  445. }
  446. uint nodeIndex = GetListIndex(node);
  447. uint componentIndex = GetComponentListIndex(component);
  448. uint numItems = hierarchyList.numItems;
  449. if (nodeIndex < numItems && componentIndex < numItems)
  450. {
  451. // Expand the node chain now
  452. if (!multiselect || !hierarchyList.IsSelected(componentIndex))
  453. {
  454. // Go in the parent chain up to make sure the chain is expanded
  455. Node@ current = node;
  456. do
  457. {
  458. hierarchyList.Expand(GetListIndex(current), true);
  459. current = current.parent;
  460. }
  461. while (current !is null);
  462. }
  463. // This causes an event to be sent, in response we set the node/component selections, and refresh editors
  464. if (!multiselect)
  465. hierarchyList.selection = componentIndex;
  466. else
  467. hierarchyList.ToggleSelection(componentIndex);
  468. }
  469. else if (!multiselect)
  470. hierarchyList.ClearSelection();
  471. }
  472. void SelectUIElement(UIElement@ element, bool multiselect)
  473. {
  474. uint index = GetListIndex(element);
  475. uint numItems = hierarchyList.numItems;
  476. if (index < numItems)
  477. {
  478. // Expand the node chain now
  479. if (!multiselect || !hierarchyList.IsSelected(index))
  480. {
  481. // Go in the parent chain up to make sure the chain is expanded
  482. UIElement@ current = element;
  483. do
  484. {
  485. hierarchyList.Expand(GetListIndex(current), true);
  486. current = current.parent;
  487. }
  488. while (current !is null);
  489. }
  490. if (!multiselect)
  491. hierarchyList.selection = index;
  492. else
  493. hierarchyList.ToggleSelection(index);
  494. }
  495. else if (!multiselect)
  496. hierarchyList.ClearSelection();
  497. }
  498. void HandleHierarchyListSelectionChange()
  499. {
  500. if (inSelectionModify)
  501. return;
  502. ClearSceneSelection();
  503. ClearUIElementSelection();
  504. Array<uint> indices = hierarchyList.selections;
  505. // Enable Expand/Collapse button when there is selection
  506. EnableExpandCollapseButtons(indices.length > 0);
  507. for (uint i = 0; i < indices.length; ++i)
  508. {
  509. uint index = indices[i];
  510. UIElement@ item = hierarchyList.items[index];
  511. int type = item.vars[TYPE_VAR].GetInt();
  512. if (type == ITEM_COMPONENT)
  513. {
  514. Component@ comp = GetListComponent(index);
  515. if (comp !is null)
  516. selectedComponents.Push(comp);
  517. }
  518. else if (type == ITEM_NODE)
  519. {
  520. Node@ node = GetListNode(index);
  521. if (node !is null)
  522. selectedNodes.Push(node);
  523. }
  524. else if (type == ITEM_UI_ELEMENT)
  525. {
  526. UIElement@ element = GetListUIElement(index);
  527. if (element !is null && element !is editorUIElement)
  528. selectedUIElements.Push(element);
  529. }
  530. }
  531. // If only one node/UIElement selected, use it for editing
  532. if (selectedNodes.length == 1)
  533. editNode = selectedNodes[0];
  534. if (selectedUIElements.length == 1)
  535. editUIElement = selectedUIElements[0];
  536. // If selection contains only components, and they have a common node, use it for editing
  537. if (selectedNodes.empty && !selectedComponents.empty)
  538. {
  539. Node@ commonNode;
  540. for (uint i = 0; i < selectedComponents.length; ++i)
  541. {
  542. if (i == 0)
  543. commonNode = selectedComponents[i].node;
  544. else
  545. {
  546. if (selectedComponents[i].node !is commonNode)
  547. commonNode = null;
  548. }
  549. }
  550. editNode = commonNode;
  551. }
  552. // Now check if the component(s) can be edited. If many selected, must have same type or have same edit node
  553. if (!selectedComponents.empty)
  554. {
  555. if (editNode is null)
  556. {
  557. StringHash compType = selectedComponents[0].type;
  558. bool sameType = true;
  559. for (uint i = 1; i < selectedComponents.length; ++i)
  560. {
  561. if (selectedComponents[i].type != compType)
  562. {
  563. sameType = false;
  564. break;
  565. }
  566. }
  567. if (sameType)
  568. editComponents = selectedComponents;
  569. }
  570. else
  571. {
  572. editComponents = selectedComponents;
  573. numEditableComponentsPerNode = selectedComponents.length;
  574. }
  575. }
  576. // If just nodes selected, and no components, show as many matching components for editing as possible
  577. if (!selectedNodes.empty && selectedComponents.empty && selectedNodes[0].numComponents > 0)
  578. {
  579. uint count = 0;
  580. for (uint j = 0; j < selectedNodes[0].numComponents; ++j)
  581. {
  582. StringHash compType = selectedNodes[0].components[j].type;
  583. bool sameType = true;
  584. for (uint i = 1; i < selectedNodes.length; ++i)
  585. {
  586. if (selectedNodes[i].numComponents <= j || selectedNodes[i].components[j].type != compType)
  587. {
  588. sameType = false;
  589. break;
  590. }
  591. }
  592. if (sameType)
  593. {
  594. ++count;
  595. for (uint i = 0; i < selectedNodes.length; ++i)
  596. editComponents.Push(selectedNodes[i].components[j]);
  597. }
  598. }
  599. if (count > 1)
  600. numEditableComponentsPerNode = count;
  601. }
  602. if (selectedNodes.empty && editNode !is null)
  603. editNodes.Push(editNode);
  604. else
  605. {
  606. editNodes = selectedNodes;
  607. // Cannot multi-edit on scene and node(s) together as scene and node do not share identical attributes,
  608. // editing via gizmo does not make too much sense either
  609. if (editNodes.length > 1 && editNodes[0] is editorScene)
  610. editNodes.Erase(0);
  611. }
  612. if (selectedUIElements.empty && editUIElement !is null)
  613. editUIElements.Push(editUIElement);
  614. else
  615. editUIElements = selectedUIElements;
  616. PositionGizmo();
  617. UpdateAttributeInspector();
  618. UpdateCameraPreview();
  619. }
  620. void HandleHierarchyListDoubleClick(StringHash eventType, VariantMap& eventData)
  621. {
  622. UIElement@ item = eventData["Item"].GetPtr();
  623. int type = item.vars[TYPE_VAR].GetInt();
  624. // Locate nodes from the scene by double-clicking
  625. if (type == ITEM_NODE)
  626. {
  627. Node@ node = editorScene.GetNode(item.vars[NODE_ID_VAR].GetUInt());
  628. LocateNode(node);
  629. }
  630. }
  631. void HandleHierarchyItemClick(StringHash eventType, VariantMap& eventData)
  632. {
  633. if (eventData["Button"].GetInt() != MOUSEB_RIGHT)
  634. return;
  635. UIElement@ uiElement = eventData["Item"].GetPtr();
  636. int selectionIndex = eventData["Selection"].GetInt();
  637. Array<UIElement@> actions;
  638. int type = uiElement.vars[TYPE_VAR].GetInt();
  639. // Adds left clicked items to selection which is not normal listview behavior
  640. if (type == ITEM_COMPONENT || type == ITEM_NODE)
  641. {
  642. if (input.keyDown[KEY_LSHIFT])
  643. hierarchyList.AddSelection(selectionIndex);
  644. else
  645. {
  646. hierarchyList.ClearSelection();
  647. hierarchyList.AddSelection(selectionIndex);
  648. }
  649. }
  650. if (type == ITEM_COMPONENT)
  651. {
  652. Component@ targetComponent = editorScene.GetComponent(uiElement.vars[COMPONENT_ID_VAR].GetUInt());
  653. if (targetComponent is null)
  654. return;
  655. actions.Push(CreateContextMenuItem("Copy", "HandleHierarchyContextCopy"));
  656. actions.Push(CreateContextMenuItem("Cut", "HandleHierarchyContextCut"));
  657. actions.Push(CreateContextMenuItem("Delete", "HandleHierarchyContextDelete"));
  658. actions.Push(CreateContextMenuItem("Paste", "HandleHierarchyContextPaste"));
  659. actions.Push(CreateContextMenuItem("Enable/disable", "HandleHierarchyContextEnableDisable"));
  660. /* actions.Push(CreateBrowserFileActionMenu("Edit", "HandleBrowserEditResource", file)); */
  661. }
  662. else if (type == ITEM_NODE)
  663. {
  664. actions.Push(CreateContextMenuItem("Create Replicated Node", "HandleHierarchyContextCreateReplicatedNode"));
  665. actions.Push(CreateContextMenuItem("Create Local Node", "HandleHierarchyContextCreateLocalNode"));
  666. actions.Push(CreateContextMenuItem("Copy", "HandleHierarchyContextCopy"));
  667. actions.Push(CreateContextMenuItem("Cut", "HandleHierarchyContextCut"));
  668. actions.Push(CreateContextMenuItem("Delete", "HandleHierarchyContextDelete"));
  669. actions.Push(CreateContextMenuItem("Paste", "HandleHierarchyContextPaste"));
  670. actions.Push(CreateContextMenuItem("Reset to default", "HandleHierarchyContextResetToDefault"));
  671. actions.Push(CreateContextMenuItem("Reset position", "HandleHierarchyContextResetPosition"));
  672. actions.Push(CreateContextMenuItem("Reset rotation", "HandleHierarchyContextResetRotation"));
  673. actions.Push(CreateContextMenuItem("Reset scale", "HandleHierarchyContextResetScale"));
  674. actions.Push(CreateContextMenuItem("Enable/disable", "HandleHierarchyContextEnableDisable"));
  675. actions.Push(CreateContextMenuItem("Unparent", "HandleHierarchyContextUnparent"));
  676. }
  677. else if (type == ITEM_UI_ELEMENT)
  678. {
  679. // close ui element
  680. actions.Push(CreateContextMenuItem("Close UI-Layout", "HandleHierarchyContextUIElementCloseUILayout"));
  681. actions.Push(CreateContextMenuItem("Close all UI-layouts", "HandleHierarchyContextUIElementCloseAllUILayouts"));
  682. }
  683. if (actions.length > 0)
  684. ActivateContextMenu(actions);
  685. }
  686. void HandleDragDropTest(StringHash eventType, VariantMap& eventData)
  687. {
  688. UIElement@ source = eventData["Source"].GetPtr();
  689. UIElement@ target = eventData["Target"].GetPtr();
  690. int itemType;
  691. eventData["Accept"] = TestDragDrop(source, target, itemType);
  692. }
  693. void HandleDragDropFinish(StringHash eventType, VariantMap& eventData)
  694. {
  695. UIElement@ source = eventData["Source"].GetPtr();
  696. UIElement@ target = eventData["Target"].GetPtr();
  697. int itemType = ITEM_NONE;
  698. bool accept = TestDragDrop(source, target, itemType);
  699. eventData["Accept"] = accept;
  700. if (!accept)
  701. return;
  702. // resource browser
  703. if (source !is null && source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt() > 0)
  704. {
  705. int type = source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt();
  706. BrowserFile@ browserFile = GetBrowserFileFromId(source.vars[TEXT_VAR_FILE_ID].GetUInt());
  707. if (browserFile is null)
  708. return;
  709. Component@ createdComponent;
  710. if (itemType == ITEM_NODE)
  711. {
  712. Node@ targetNode = editorScene.GetNode(target.vars[NODE_ID_VAR].GetUInt());
  713. if (targetNode is null)
  714. return;
  715. // editNode = targetNode;
  716. if (type == RESOURCE_TYPE_PREFAB)
  717. {
  718. LoadNode(browserFile.GetFullPath(), targetNode);
  719. }
  720. else if(type == RESOURCE_TYPE_SCRIPTFILE)
  721. {
  722. // TODO: not sure what to do here. lots of choices.
  723. }
  724. else if(type == RESOURCE_TYPE_MODEL)
  725. {
  726. CreateModelWithStaticModel(browserFile.resourceKey, targetNode);
  727. return;
  728. }
  729. else if (type == RESOURCE_TYPE_PARTICLEEFFECT)
  730. {
  731. if (browserFile.extension == "xml")
  732. {
  733. ParticleEffect@ effect = cache.GetResource("ParticleEffect", browserFile.resourceKey);
  734. if (effect is null)
  735. return;
  736. ParticleEmitter@ emitter = targetNode.CreateComponent("ParticleEmitter");
  737. emitter.effect = effect;
  738. createdComponent = emitter;
  739. }
  740. }
  741. else if (type == RESOURCE_TYPE_2D_PARTICLE_EFFECT)
  742. {
  743. if (browserFile.extension == "xml")
  744. {
  745. ParticleEffect2D@ effect = cache.GetResource("ParticleEffect2D", browserFile.resourceKey);
  746. if (effect is null)
  747. return;
  748. ParticleEmitter2D@ emitter = targetNode.CreateComponent("ParticleEmitter2D");
  749. emitter.effect = effect;
  750. createdComponent = emitter;
  751. }
  752. }
  753. }
  754. else if (itemType == ITEM_COMPONENT)
  755. {
  756. Component@ targetComponent = editorScene.GetComponent(target.vars[COMPONENT_ID_VAR].GetUInt());
  757. if (targetComponent is null)
  758. return;
  759. if (type == RESOURCE_TYPE_MATERIAL)
  760. {
  761. StaticModel@ model = cast<StaticModel>(targetComponent);
  762. if (model is null)
  763. return;
  764. AssignMaterial(model, browserFile.resourceKey);
  765. }
  766. else if (type == RESOURCE_TYPE_MODEL)
  767. {
  768. StaticModel@ staticModel = cast<StaticModel>(targetComponent);
  769. if (staticModel is null)
  770. return;
  771. AssignModel(staticModel, browserFile.resourceKey);
  772. }
  773. }
  774. else
  775. {
  776. LineEdit@ text = cast<LineEdit>(target);
  777. if (text is null)
  778. return;
  779. text.text = browserFile.resourceKey;
  780. VariantMap data();
  781. data["Element"] = text;
  782. data["Text"] = text.text;
  783. text.SendEvent("TextFinished", data);
  784. }
  785. if (createdComponent !is null)
  786. {
  787. CreateLoadedComponent(createdComponent);
  788. }
  789. return;
  790. }
  791. if (itemType == ITEM_NODE)
  792. {
  793. Node@ targetNode = editorScene.GetNode(target.vars[NODE_ID_VAR].GetUInt());
  794. // If target is null, parent to scene
  795. if (targetNode is null)
  796. targetNode = editorScene;
  797. Array<Node@> sourceNodes = GetMultipleSourceNodes(source);
  798. if (sourceNodes.length > 0)
  799. {
  800. if (sourceNodes.length > 1)
  801. SceneChangeParent(sourceNodes[0], sourceNodes, targetNode);
  802. else
  803. SceneChangeParent(sourceNodes[0], targetNode);
  804. // Focus the node at its new position in the list which in turn should trigger a refresh in attribute inspector
  805. FocusNode(sourceNodes[0]);
  806. }
  807. }
  808. else if (itemType == ITEM_UI_ELEMENT)
  809. {
  810. UIElement@ sourceElement = GetUIElementByID(source.vars[UI_ELEMENT_ID_VAR].GetUInt());
  811. UIElement@ targetElement = GetUIElementByID(target.vars[UI_ELEMENT_ID_VAR].GetUInt());
  812. // If target is null, cannot proceed
  813. if (targetElement is null)
  814. return;
  815. // Perform the reparenting
  816. if (!UIElementChangeParent(sourceElement, targetElement))
  817. return;
  818. // Focus the element at its new position in the list which in turn should trigger a refresh in attribute inspector
  819. FocusUIElement(sourceElement);
  820. }
  821. else if (itemType == ITEM_COMPONENT)
  822. {
  823. Array<Node@> sourceNodes = GetMultipleSourceNodes(source);
  824. Component@ targetComponent = editorScene.GetComponent(target.vars[COMPONENT_ID_VAR].GetUInt());
  825. if (targetComponent !is null && sourceNodes.length > 0)
  826. {
  827. // Drag node to StaticModelGroup to make it an instance
  828. StaticModelGroup@ smg = cast<StaticModelGroup>(targetComponent);
  829. if (smg !is null)
  830. {
  831. // Save undo action
  832. EditAttributeAction action;
  833. uint attrIndex = GetAttributeIndex(smg, "Instance Nodes");
  834. Variant oldIDs = smg.attributes[attrIndex];
  835. for (uint i = 0; i < sourceNodes.length; ++i)
  836. smg.AddInstanceNode(sourceNodes[i]);
  837. action.Define(smg, attrIndex, oldIDs);
  838. SaveEditAction(action);
  839. SetSceneModified();
  840. }
  841. // Drag node to SplinePath to make it a control point
  842. SplinePath@ spline = cast<SplinePath>(targetComponent);
  843. if (spline !is null)
  844. {
  845. // Save undo action
  846. EditAttributeAction action;
  847. uint attrIndex = GetAttributeIndex(spline, "Control Points");
  848. Variant oldIDs = spline.attributes[attrIndex];
  849. for (uint i = 0; i < sourceNodes.length; ++i)
  850. spline.AddControlPoint(sourceNodes[i]);
  851. action.Define(spline, attrIndex, oldIDs);
  852. SaveEditAction(action);
  853. SetSceneModified();
  854. }
  855. // Drag a node to Constraint to make it the remote end of the constraint
  856. Constraint@ constraint = cast<Constraint>(targetComponent);
  857. RigidBody@ rigidBody = sourceNodes[0].GetComponent("RigidBody");
  858. if (constraint !is null && rigidBody !is null)
  859. {
  860. // Save undo action
  861. EditAttributeAction action;
  862. uint attrIndex = GetAttributeIndex(constraint, "Other Body NodeID");
  863. Variant oldID = constraint.attributes[attrIndex];
  864. constraint.otherBody = rigidBody;
  865. action.Define(constraint, attrIndex, oldID);
  866. SaveEditAction(action);
  867. SetSceneModified();
  868. }
  869. }
  870. }
  871. }
  872. Array<Node@> GetMultipleSourceNodes(UIElement@ source)
  873. {
  874. Array<Node@> nodeList;
  875. Node@ node = editorScene.GetNode(source.vars[NODE_ID_VAR].GetUInt());
  876. if (node !is null)
  877. nodeList.Push(node);
  878. // Handle additional selected children from a ListView
  879. if (source.parent !is null && source.parent.typeName == "HierarchyContainer")
  880. {
  881. ListView@ listView_ = cast<ListView>(source.parent.parent.parent);
  882. if (listView_ is null)
  883. return nodeList;
  884. bool sourceIsSelected = false;
  885. for (uint i = 0; i < listView_.selectedItems.length; ++i)
  886. {
  887. if (listView_.selectedItems[i] is source)
  888. {
  889. sourceIsSelected = true;
  890. break;
  891. }
  892. }
  893. if (sourceIsSelected)
  894. {
  895. for (uint i = 0; i < listView_.selectedItems.length; ++i)
  896. {
  897. UIElement@ item_ = listView_.selectedItems[i];
  898. // The source item is already added
  899. if (item_ is source)
  900. continue;
  901. if (item_.vars[TYPE_VAR] == ITEM_NODE)
  902. {
  903. Node@ node = editorScene.GetNode(item_.vars[NODE_ID_VAR].GetUInt());
  904. if (node !is null)
  905. nodeList.Push(node);
  906. }
  907. }
  908. }
  909. }
  910. return nodeList;
  911. }
  912. bool TestDragDrop(UIElement@ source, UIElement@ target, int& itemType)
  913. {
  914. int targetItemType = target.GetVar(TYPE_VAR).GetInt();
  915. if (targetItemType == ITEM_NODE)
  916. {
  917. Node@ sourceNode;
  918. Node@ targetNode;
  919. Variant variant = source.GetVar(NODE_ID_VAR);
  920. if (!variant.empty)
  921. sourceNode = editorScene.GetNode(variant.GetUInt());
  922. variant = target.GetVar(NODE_ID_VAR);
  923. if (!variant.empty)
  924. targetNode = editorScene.GetNode(variant.GetUInt());
  925. if (sourceNode !is null && targetNode !is null)
  926. {
  927. itemType = ITEM_NODE;
  928. if (sourceNode.parent is targetNode)
  929. return false;
  930. if (targetNode.parent is sourceNode)
  931. return false;
  932. }
  933. // Resource browser
  934. if (sourceNode is null && targetNode !is null)
  935. {
  936. itemType = ITEM_NODE;
  937. int type = source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt();
  938. return type == RESOURCE_TYPE_PREFAB ||
  939. type == RESOURCE_TYPE_SCRIPTFILE ||
  940. type == RESOURCE_TYPE_MODEL ||
  941. type == RESOURCE_TYPE_PARTICLEEFFECT ||
  942. type == RESOURCE_TYPE_2D_PARTICLE_EFFECT;
  943. }
  944. return true;
  945. }
  946. else if (targetItemType == ITEM_UI_ELEMENT)
  947. {
  948. UIElement@ sourceElement;
  949. UIElement@ targetElement;
  950. Variant variant = source.GetVar(UI_ELEMENT_ID_VAR);
  951. if (!variant.empty)
  952. sourceElement = GetUIElementByID(variant.GetUInt());
  953. variant = target.GetVar(UI_ELEMENT_ID_VAR);
  954. if (!variant.empty)
  955. targetElement = GetUIElementByID(variant.GetUInt());
  956. if (sourceElement !is null && targetElement !is null)
  957. {
  958. itemType = ITEM_UI_ELEMENT;
  959. if (sourceElement.parent is targetElement)
  960. return false;
  961. if (targetElement.parent is sourceElement)
  962. return false;
  963. }
  964. return true;
  965. }
  966. else if (targetItemType == ITEM_COMPONENT)
  967. {
  968. // Now only support dragging of nodes to StaticModelGroup, SplinePath or Constraint. Can be expanded to support others
  969. Node@ sourceNode;
  970. Component@ targetComponent;
  971. Variant variant = source.GetVar(NODE_ID_VAR);
  972. if (!variant.empty)
  973. sourceNode = editorScene.GetNode(variant.GetUInt());
  974. variant = target.GetVar(COMPONENT_ID_VAR);
  975. if (!variant.empty)
  976. targetComponent = editorScene.GetComponent(variant.GetUInt());
  977. itemType = ITEM_COMPONENT;
  978. if (sourceNode !is null && targetComponent !is null && (targetComponent.type == STATICMODELGROUP_TYPE ||
  979. targetComponent.type == CONSTRAINT_TYPE || targetComponent.type == SPLINEPATH_TYPE))
  980. return true;
  981. // resource browser
  982. int type = source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt();
  983. if (targetComponent.type == STATICMODEL_TYPE || targetComponent.type == ANIMATEDMODEL_TYPE)
  984. return type == RESOURCE_TYPE_MATERIAL || type == RESOURCE_TYPE_MODEL;
  985. return false;
  986. }
  987. else if (source.vars.Contains(TEXT_VAR_RESOURCE_TYPE)) // only testing resource browser ui elements
  988. {
  989. int type = source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt();
  990. // test against resource pickers
  991. LineEdit@ lineEdit = cast<LineEdit>(target);
  992. if (lineEdit !is null)
  993. {
  994. StringHash resourceType = GetResourceTypeFromPickerLineEdit(lineEdit);
  995. if (resourceType == StringHash("Material") && type == RESOURCE_TYPE_MATERIAL)
  996. return true;
  997. else if (resourceType == StringHash("Model") && type == RESOURCE_TYPE_MODEL)
  998. return true;
  999. else if (resourceType == StringHash("Animation") && type == RESOURCE_TYPE_ANIMATION)
  1000. return true;
  1001. }
  1002. }
  1003. return true;
  1004. }
  1005. StringHash GetResourceTypeFromPickerLineEdit(UIElement@ lineEdit)
  1006. {
  1007. Array<Serializable@>@ targets = GetAttributeEditorTargets(lineEdit);
  1008. if (!targets.empty)
  1009. {
  1010. resourcePickIndex = lineEdit.vars["Index"].GetUInt();
  1011. resourcePickSubIndex = lineEdit.vars["SubIndex"].GetUInt();
  1012. AttributeInfo info = targets[0].attributeInfos[resourcePickIndex];
  1013. StringHash resourceType;
  1014. if (info.type == VAR_RESOURCEREF)
  1015. return targets[0].attributes[resourcePickIndex].GetResourceRef().type;
  1016. else if (info.type == VAR_RESOURCEREFLIST)
  1017. return targets[0].attributes[resourcePickIndex].GetResourceRefList().type;
  1018. else if (info.type == VAR_VARIANTVECTOR)
  1019. return targets[0].attributes[resourcePickIndex].GetVariantVector()[resourcePickSubIndex].GetResourceRef().type;
  1020. }
  1021. return StringHash();
  1022. }
  1023. void FocusNode(Node@ node)
  1024. {
  1025. uint index = GetListIndex(node);
  1026. hierarchyList.selection = index;
  1027. }
  1028. void FocusComponent(Component@ component)
  1029. {
  1030. uint index = GetComponentListIndex(component);
  1031. hierarchyList.selection = index;
  1032. }
  1033. void FocusUIElement(UIElement@ element)
  1034. {
  1035. uint index = GetListIndex(element);
  1036. hierarchyList.selection = index;
  1037. }
  1038. void CreateBuiltinObject(const String& name)
  1039. {
  1040. Node@ newNode = editorScene.CreateChild(name, REPLICATED);
  1041. // Set the new node a certain distance from the camera
  1042. newNode.position = GetNewNodePosition();
  1043. StaticModel@ object = newNode.CreateComponent("StaticModel");
  1044. object.model = cache.GetResource("Model", "Models/" + name + ".mdl");
  1045. // Create an undo action for the create
  1046. CreateNodeAction action;
  1047. action.Define(newNode);
  1048. SaveEditAction(action);
  1049. SetSceneModified();
  1050. FocusNode(newNode);
  1051. }
  1052. bool CheckHierarchyWindowFocus()
  1053. {
  1054. // When we do edit operations based on key shortcuts, make sure the hierarchy list is focused
  1055. return ui.focusElement is hierarchyList || ui.focusElement is null;
  1056. }
  1057. bool CheckForExistingGlobalComponent(Node@ node, const String&in typeName)
  1058. {
  1059. if (typeName != "Octree" && typeName != "PhysicsWorld" && typeName != "DebugRenderer")
  1060. return false;
  1061. else
  1062. return node.HasComponent(typeName);
  1063. }
  1064. void HandleNodeAdded(StringHash eventType, VariantMap& eventData)
  1065. {
  1066. if (suppressSceneChanges)
  1067. return;
  1068. Node@ node = eventData["Node"].GetPtr();
  1069. if (showTemporaryObject || !node.temporary)
  1070. UpdateHierarchyItem(node);
  1071. }
  1072. void HandleNodeRemoved(StringHash eventType, VariantMap& eventData)
  1073. {
  1074. if (suppressSceneChanges)
  1075. return;
  1076. Node@ node = eventData["Node"].GetPtr();
  1077. uint index = GetListIndex(node);
  1078. UpdateHierarchyItem(index, null, null);
  1079. }
  1080. void HandleComponentAdded(StringHash eventType, VariantMap& eventData)
  1081. {
  1082. if (suppressSceneChanges)
  1083. return;
  1084. // Insert the newly added component at last component position but before the first child node position of the parent node
  1085. Node@ node = eventData["Node"].GetPtr();
  1086. Component@ component = eventData["Component"].GetPtr();
  1087. if (showTemporaryObject || !component.temporary)
  1088. {
  1089. uint nodeIndex = GetListIndex(node);
  1090. if (nodeIndex != NO_ITEM)
  1091. {
  1092. uint index = node.numChildren > 0 ? GetListIndex(node.children[0]) : M_MAX_UNSIGNED;
  1093. UpdateHierarchyItem(index, component, hierarchyList.items[nodeIndex]);
  1094. }
  1095. }
  1096. }
  1097. void HandleComponentRemoved(StringHash eventType, VariantMap& eventData)
  1098. {
  1099. if (suppressSceneChanges)
  1100. return;
  1101. Component@ component = eventData["Component"].GetPtr();
  1102. uint index = GetComponentListIndex(component);
  1103. if (index != NO_ITEM)
  1104. hierarchyList.RemoveItem(index);
  1105. }
  1106. void HandleNodeNameChanged(StringHash eventType, VariantMap& eventData)
  1107. {
  1108. if (suppressSceneChanges)
  1109. return;
  1110. Node@ node = eventData["Node"].GetPtr();
  1111. UpdateHierarchyItemText(GetListIndex(node), node.enabled, GetNodeTitle(node));
  1112. }
  1113. void HandleNodeEnabledChanged(StringHash eventType, VariantMap& eventData)
  1114. {
  1115. if (suppressSceneChanges)
  1116. return;
  1117. Node@ node = eventData["Node"].GetPtr();
  1118. UpdateHierarchyItemText(GetListIndex(node), node.enabled);
  1119. attributesDirty = true;
  1120. }
  1121. void HandleComponentEnabledChanged(StringHash eventType, VariantMap& eventData)
  1122. {
  1123. if (suppressSceneChanges)
  1124. return;
  1125. Component@ component = eventData["Component"].GetPtr();
  1126. UpdateHierarchyItemText(GetComponentListIndex(component), component.enabledEffective);
  1127. attributesDirty = true;
  1128. }
  1129. void HandleUIElementAdded(StringHash eventType, VariantMap& eventData)
  1130. {
  1131. if (suppressUIElementChanges)
  1132. return;
  1133. UIElement@ element = eventData["Element"].GetPtr();
  1134. if ((showInternalUIElement || !element.internal) && (showTemporaryObject || !element.temporary))
  1135. UpdateHierarchyItem(element);
  1136. }
  1137. void HandleUIElementRemoved(StringHash eventType, VariantMap& eventData)
  1138. {
  1139. if (suppressUIElementChanges)
  1140. return;
  1141. UIElement@ element = eventData["Element"].GetPtr();
  1142. UpdateHierarchyItem(GetListIndex(element), null, null);
  1143. }
  1144. void HandleElementNameChanged(StringHash eventType, VariantMap& eventData)
  1145. {
  1146. if (suppressUIElementChanges)
  1147. return;
  1148. UIElement@ element = eventData["Element"].GetPtr();
  1149. UpdateHierarchyItemText(GetListIndex(element), element.visible, GetUIElementTitle(element));
  1150. }
  1151. void HandleElementVisibilityChanged(StringHash eventType, VariantMap& eventData)
  1152. {
  1153. if (suppressUIElementChanges)
  1154. return;
  1155. UIElement@ element = eventData["Element"].GetPtr();
  1156. UpdateHierarchyItemText(GetListIndex(element), element.visible);
  1157. }
  1158. void HandleElementAttributeChanged(StringHash eventType, VariantMap& eventData)
  1159. {
  1160. // Do not refresh the attribute inspector while the attribute is being edited via the attribute-editors
  1161. if (suppressUIElementChanges || inEditAttribute)
  1162. return;
  1163. UIElement@ element = eventData["Element"].GetPtr();
  1164. for (uint i = 0; i < editUIElements.length; ++i)
  1165. {
  1166. if (editUIElements[i] is element)
  1167. attributesDirty = true;
  1168. }
  1169. }
  1170. void HandleTemporaryChanged(StringHash eventType, VariantMap& eventData)
  1171. {
  1172. if (suppressSceneChanges || suppressUIElementChanges)
  1173. return;
  1174. Serializable@ serializable = cast<Serializable>(GetEventSender());
  1175. Node@ node = cast<Node>(serializable);
  1176. if (node !is null && node.scene is editorScene)
  1177. {
  1178. if (showTemporaryObject)
  1179. UpdateHierarchyItemText(GetListIndex(node), node.enabled);
  1180. else if (!node.temporary && GetListIndex(node) == NO_ITEM)
  1181. UpdateHierarchyItem(node);
  1182. else if (node.temporary)
  1183. UpdateHierarchyItem(GetListIndex(node), null, null);
  1184. return;
  1185. }
  1186. Component@ component = cast<Component>(serializable);
  1187. if (component !is null && component.node !is null && component.node.scene is editorScene)
  1188. {
  1189. if (showTemporaryObject)
  1190. UpdateHierarchyItemText(GetComponentListIndex(component), node.enabled);
  1191. else if (!component.temporary && GetComponentListIndex(component) == NO_ITEM)
  1192. {
  1193. uint nodeIndex = GetListIndex(node);
  1194. if (nodeIndex != NO_ITEM)
  1195. {
  1196. uint index = node.numChildren > 0 ? GetListIndex(node.children[0]) : M_MAX_UNSIGNED;
  1197. UpdateHierarchyItem(index, component, hierarchyList.items[nodeIndex]);
  1198. }
  1199. }
  1200. else if (component.temporary)
  1201. {
  1202. uint index = GetComponentListIndex(component);
  1203. if (index != NO_ITEM)
  1204. hierarchyList.RemoveItem(index);
  1205. }
  1206. return;
  1207. }
  1208. UIElement@ element = cast<UIElement>(serializable);
  1209. if (element !is null)
  1210. {
  1211. if (showTemporaryObject)
  1212. UpdateHierarchyItemText(GetListIndex(element), element.visible);
  1213. else if (!element.temporary && GetListIndex(element) == NO_ITEM)
  1214. UpdateHierarchyItem(element);
  1215. else if (element.temporary)
  1216. UpdateHierarchyItem(GetListIndex(element), null, null);
  1217. return;
  1218. }
  1219. }
  1220. // Hierarchy window edit functions
  1221. bool Undo()
  1222. {
  1223. if (undoStackPos > 0)
  1224. {
  1225. --undoStackPos;
  1226. // Undo commands in reverse order
  1227. for (int i = int(undoStack[undoStackPos].actions.length - 1); i >= 0; --i)
  1228. undoStack[undoStackPos].actions[i].Undo();
  1229. }
  1230. return true;
  1231. }
  1232. bool Redo()
  1233. {
  1234. if (undoStackPos < undoStack.length)
  1235. {
  1236. // Redo commands in same order as stored
  1237. for (uint i = 0; i < undoStack[undoStackPos].actions.length; ++i)
  1238. undoStack[undoStackPos].actions[i].Redo();
  1239. ++undoStackPos;
  1240. }
  1241. return true;
  1242. }
  1243. bool Cut()
  1244. {
  1245. if (CheckHierarchyWindowFocus())
  1246. {
  1247. bool ret = true;
  1248. if (!selectedNodes.empty || !selectedComponents.empty)
  1249. ret = ret && SceneCut();
  1250. // Not mutually exclusive
  1251. if (!selectedUIElements.empty)
  1252. ret = ret && UIElementCut();
  1253. return ret;
  1254. }
  1255. return false;
  1256. }
  1257. bool Copy()
  1258. {
  1259. if (CheckHierarchyWindowFocus())
  1260. {
  1261. bool ret = true;
  1262. if (!selectedNodes.empty || !selectedComponents.empty)
  1263. ret = ret && (selectedNodes.empty || selectedComponents.empty ? SceneCopy() : false); // Node and component is mutually exclusive for copy action
  1264. // Not mutually exclusive
  1265. if (!selectedUIElements.empty)
  1266. ret = ret && UIElementCopy();
  1267. return ret;
  1268. }
  1269. return false;
  1270. }
  1271. bool Paste()
  1272. {
  1273. if (CheckHierarchyWindowFocus())
  1274. {
  1275. bool ret = true;
  1276. if (editNode !is null && !sceneCopyBuffer.empty)
  1277. ret = ret && ScenePaste();
  1278. // Not mutually exclusive
  1279. if (editUIElement !is null && !uiElementCopyBuffer.empty)
  1280. ret = ret && UIElementPaste();
  1281. return ret;
  1282. }
  1283. return false;
  1284. }
  1285. bool Delete()
  1286. {
  1287. if (CheckHierarchyWindowFocus())
  1288. {
  1289. bool ret = true;
  1290. if (!selectedNodes.empty || !selectedComponents.empty)
  1291. ret = ret && SceneDelete();
  1292. // Not mutually exclusive
  1293. if (!selectedUIElements.empty)
  1294. ret = ret && UIElementDelete();
  1295. return ret;
  1296. }
  1297. return false;
  1298. }
  1299. bool SelectAll()
  1300. {
  1301. if (CheckHierarchyWindowFocus())
  1302. {
  1303. if (!selectedNodes.empty || !selectedComponents.empty)
  1304. return SceneSelectAll();
  1305. else if (!selectedUIElements.empty || hierarchyList.items[GetListIndex(editorUIElement)].selected)
  1306. return UIElementSelectAll();
  1307. else
  1308. return SceneSelectAll(); // If nothing is selected yet, fall back to scene select all
  1309. }
  1310. return false;
  1311. }
  1312. bool ResetToDefault()
  1313. {
  1314. if (CheckHierarchyWindowFocus())
  1315. {
  1316. bool ret = true;
  1317. if (!selectedNodes.empty || !selectedComponents.empty)
  1318. ret = ret && (selectedNodes.empty || selectedComponents.empty ? SceneResetToDefault() : false); // Node and component is mutually exclusive for reset-to-default action
  1319. // Not mutually exclusive
  1320. if (!selectedUIElements.empty)
  1321. ret = ret && UIElementResetToDefault();
  1322. return ret;
  1323. }
  1324. return false;
  1325. }
  1326. void ClearEditActions()
  1327. {
  1328. undoStack.Clear();
  1329. undoStackPos = 0;
  1330. }
  1331. void SaveEditAction(EditAction@ action)
  1332. {
  1333. // Create a group with 1 action
  1334. EditActionGroup group;
  1335. group.actions.Push(action);
  1336. SaveEditActionGroup(group);
  1337. }
  1338. void SaveEditActionGroup(EditActionGroup@ group)
  1339. {
  1340. if (group.actions.empty)
  1341. return;
  1342. // Truncate the stack first to current pos
  1343. undoStack.Resize(undoStackPos);
  1344. undoStack.Push(group);
  1345. ++undoStackPos;
  1346. // Limit maximum undo steps
  1347. if (undoStack.length > MAX_UNDOSTACK_SIZE)
  1348. {
  1349. undoStack.Erase(0);
  1350. --undoStackPos;
  1351. }
  1352. }
  1353. void BeginSelectionModify()
  1354. {
  1355. // A large operation on selected nodes is about to begin. Disable intermediate selection updates
  1356. inSelectionModify = true;
  1357. // Cursor shape reverts back to normal automatically after the large operation is completed
  1358. ui.cursor.shape = CS_BUSY;
  1359. }
  1360. void EndSelectionModify()
  1361. {
  1362. // The large operation on selected nodes has ended. Update node/component selection now
  1363. inSelectionModify = false;
  1364. HandleHierarchyListSelectionChange();
  1365. }
  1366. void HandleHierarchyContextCreateReplicatedNode()
  1367. {
  1368. CreateNode(REPLICATED);
  1369. }
  1370. void HandleHierarchyContextCreateLocalNode()
  1371. {
  1372. CreateNode(LOCAL);
  1373. }
  1374. void HandleHierarchyContextCopy()
  1375. {
  1376. Copy();
  1377. }
  1378. void HandleHierarchyContextCut()
  1379. {
  1380. Cut();
  1381. }
  1382. void HandleHierarchyContextDelete()
  1383. {
  1384. Delete();
  1385. }
  1386. void HandleHierarchyContextPaste()
  1387. {
  1388. Paste();
  1389. }
  1390. void HandleHierarchyContextResetToDefault()
  1391. {
  1392. ResetToDefault();
  1393. }
  1394. void HandleHierarchyContextResetPosition()
  1395. {
  1396. SceneResetPosition();
  1397. }
  1398. void HandleHierarchyContextResetRotation()
  1399. {
  1400. SceneResetRotation();
  1401. }
  1402. void HandleHierarchyContextResetScale()
  1403. {
  1404. SceneResetScale();
  1405. }
  1406. void HandleHierarchyContextEnableDisable()
  1407. {
  1408. SceneToggleEnable();
  1409. }
  1410. void HandleHierarchyContextUnparent()
  1411. {
  1412. SceneUnparent();
  1413. }
  1414. void HandleHierarchyContextUIElementCloseUILayout()
  1415. {
  1416. CloseUILayout();
  1417. }
  1418. void HandleHierarchyContextUIElementCloseAllUILayouts()
  1419. {
  1420. CloseAllUILayouts();
  1421. }