2
0

EditorHierarchyWindow.as 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  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 = LoadEditorUI("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("Duplicate", "HandleHierarchyContextDuplicate"));
  667. actions.Push(CreateContextMenuItem("Copy", "HandleHierarchyContextCopy"));
  668. actions.Push(CreateContextMenuItem("Cut", "HandleHierarchyContextCut"));
  669. actions.Push(CreateContextMenuItem("Delete", "HandleHierarchyContextDelete"));
  670. actions.Push(CreateContextMenuItem("Paste", "HandleHierarchyContextPaste"));
  671. actions.Push(CreateContextMenuItem("Reset to default", "HandleHierarchyContextResetToDefault"));
  672. actions.Push(CreateContextMenuItem("Reset position", "HandleHierarchyContextResetPosition"));
  673. actions.Push(CreateContextMenuItem("Reset rotation", "HandleHierarchyContextResetRotation"));
  674. actions.Push(CreateContextMenuItem("Reset scale", "HandleHierarchyContextResetScale"));
  675. actions.Push(CreateContextMenuItem("Enable/disable", "HandleHierarchyContextEnableDisable"));
  676. actions.Push(CreateContextMenuItem("Unparent", "HandleHierarchyContextUnparent"));
  677. }
  678. else if (type == ITEM_UI_ELEMENT)
  679. {
  680. // close ui element
  681. actions.Push(CreateContextMenuItem("Close UI-Layout", "HandleHierarchyContextUIElementCloseUILayout"));
  682. actions.Push(CreateContextMenuItem("Close all UI-layouts", "HandleHierarchyContextUIElementCloseAllUILayouts"));
  683. }
  684. if (actions.length > 0)
  685. ActivateContextMenu(actions);
  686. }
  687. void HandleDragDropTest(StringHash eventType, VariantMap& eventData)
  688. {
  689. UIElement@ source = eventData["Source"].GetPtr();
  690. UIElement@ target = eventData["Target"].GetPtr();
  691. int itemType;
  692. eventData["Accept"] = TestDragDrop(source, target, itemType);
  693. }
  694. void HandleDragDropFinish(StringHash eventType, VariantMap& eventData)
  695. {
  696. UIElement@ source = eventData["Source"].GetPtr();
  697. UIElement@ target = eventData["Target"].GetPtr();
  698. int itemType = ITEM_NONE;
  699. bool accept = TestDragDrop(source, target, itemType);
  700. eventData["Accept"] = accept;
  701. if (!accept)
  702. return;
  703. // resource browser
  704. if (source !is null && source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt() > 0)
  705. {
  706. int type = source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt();
  707. BrowserFile@ browserFile = GetBrowserFileFromId(source.vars[TEXT_VAR_FILE_ID].GetUInt());
  708. if (browserFile is null)
  709. return;
  710. Component@ createdComponent;
  711. if (itemType == ITEM_NODE)
  712. {
  713. Node@ targetNode = editorScene.GetNode(target.vars[NODE_ID_VAR].GetUInt());
  714. if (targetNode is null)
  715. return;
  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. Resource@ effect = cache.GetResource("ParticleEffect2D", browserFile.resourceKey);
  746. if (effect is null)
  747. return;
  748. ResourceRef effectRef;
  749. effectRef.type = effect.type;
  750. effectRef.name = effect.name;
  751. Component@ emitter = targetNode.CreateComponent("ParticleEmitter2D");
  752. emitter.SetAttribute("Particle Effect", Variant(effectRef));
  753. createdComponent = emitter;
  754. }
  755. }
  756. }
  757. else if (itemType == ITEM_COMPONENT)
  758. {
  759. Component@ targetComponent = editorScene.GetComponent(target.vars[COMPONENT_ID_VAR].GetUInt());
  760. if (targetComponent is null)
  761. return;
  762. if (type == RESOURCE_TYPE_MATERIAL)
  763. {
  764. StaticModel@ model = cast<StaticModel>(targetComponent);
  765. if (model is null)
  766. return;
  767. AssignMaterial(model, browserFile.resourceKey);
  768. }
  769. else if (type == RESOURCE_TYPE_MODEL)
  770. {
  771. StaticModel@ staticModel = cast<StaticModel>(targetComponent);
  772. if (staticModel is null)
  773. return;
  774. AssignModel(staticModel, browserFile.resourceKey);
  775. }
  776. }
  777. else
  778. {
  779. LineEdit@ text = cast<LineEdit>(target);
  780. if (text is null)
  781. return;
  782. text.text = browserFile.resourceKey;
  783. VariantMap data();
  784. data["Element"] = text;
  785. data["Text"] = text.text;
  786. text.SendEvent("TextFinished", data);
  787. }
  788. if (createdComponent !is null)
  789. {
  790. CreateLoadedComponent(createdComponent);
  791. }
  792. return;
  793. }
  794. if (itemType == ITEM_NODE)
  795. {
  796. Node@ targetNode = editorScene.GetNode(target.vars[NODE_ID_VAR].GetUInt());
  797. // If target is null, parent to scene
  798. if (targetNode is null)
  799. targetNode = editorScene;
  800. Array<Node@> sourceNodes = GetMultipleSourceNodes(source);
  801. if (sourceNodes.length > 0)
  802. {
  803. if (sourceNodes.length > 1)
  804. SceneChangeParent(sourceNodes[0], sourceNodes, targetNode);
  805. else
  806. SceneChangeParent(sourceNodes[0], targetNode);
  807. // Focus the node at its new position in the list which in turn should trigger a refresh in attribute inspector
  808. FocusNode(sourceNodes[0]);
  809. }
  810. }
  811. else if (itemType == ITEM_UI_ELEMENT)
  812. {
  813. UIElement@ sourceElement = GetUIElementByID(source.vars[UI_ELEMENT_ID_VAR].GetUInt());
  814. UIElement@ targetElement = GetUIElementByID(target.vars[UI_ELEMENT_ID_VAR].GetUInt());
  815. // If target is null, cannot proceed
  816. if (targetElement is null)
  817. return;
  818. // Perform the reparenting
  819. if (!UIElementChangeParent(sourceElement, targetElement))
  820. return;
  821. // Focus the element at its new position in the list which in turn should trigger a refresh in attribute inspector
  822. FocusUIElement(sourceElement);
  823. }
  824. else if (itemType == ITEM_COMPONENT)
  825. {
  826. Array<Node@> sourceNodes = GetMultipleSourceNodes(source);
  827. Component@ targetComponent = editorScene.GetComponent(target.vars[COMPONENT_ID_VAR].GetUInt());
  828. if (targetComponent !is null && sourceNodes.length > 0)
  829. {
  830. // Drag node to StaticModelGroup to make it an instance
  831. StaticModelGroup@ smg = cast<StaticModelGroup>(targetComponent);
  832. if (smg !is null)
  833. {
  834. // Save undo action
  835. EditAttributeAction action;
  836. uint attrIndex = GetAttributeIndex(smg, "Instance Nodes");
  837. Variant oldIDs = smg.attributes[attrIndex];
  838. for (uint i = 0; i < sourceNodes.length; ++i)
  839. smg.AddInstanceNode(sourceNodes[i]);
  840. action.Define(smg, attrIndex, oldIDs);
  841. SaveEditAction(action);
  842. SetSceneModified();
  843. }
  844. // Drag node to SplinePath to make it a control point
  845. SplinePath@ spline = cast<SplinePath>(targetComponent);
  846. if (spline !is null)
  847. {
  848. // Save undo action
  849. EditAttributeAction action;
  850. uint attrIndex = GetAttributeIndex(spline, "Control Points");
  851. Variant oldIDs = spline.attributes[attrIndex];
  852. for (uint i = 0; i < sourceNodes.length; ++i)
  853. spline.AddControlPoint(sourceNodes[i]);
  854. action.Define(spline, attrIndex, oldIDs);
  855. SaveEditAction(action);
  856. SetSceneModified();
  857. }
  858. // Drag a node to Constraint to make it the remote end of the constraint
  859. Constraint@ constraint = cast<Constraint>(targetComponent);
  860. RigidBody@ rigidBody = sourceNodes[0].GetComponent("RigidBody");
  861. if (constraint !is null && rigidBody !is null)
  862. {
  863. // Save undo action
  864. EditAttributeAction action;
  865. uint attrIndex = GetAttributeIndex(constraint, "Other Body NodeID");
  866. Variant oldID = constraint.attributes[attrIndex];
  867. constraint.otherBody = rigidBody;
  868. action.Define(constraint, attrIndex, oldID);
  869. SaveEditAction(action);
  870. SetSceneModified();
  871. }
  872. }
  873. }
  874. }
  875. Array<Node@> GetMultipleSourceNodes(UIElement@ source)
  876. {
  877. Array<Node@> nodeList;
  878. Node@ node = editorScene.GetNode(source.vars[NODE_ID_VAR].GetUInt());
  879. if (node !is null)
  880. nodeList.Push(node);
  881. // Handle additional selected children from a ListView
  882. if (source.parent !is null && source.parent.typeName == "HierarchyContainer")
  883. {
  884. ListView@ listView_ = cast<ListView>(source.parent.parent.parent);
  885. if (listView_ is null)
  886. return nodeList;
  887. bool sourceIsSelected = false;
  888. for (uint i = 0; i < listView_.selectedItems.length; ++i)
  889. {
  890. if (listView_.selectedItems[i] is source)
  891. {
  892. sourceIsSelected = true;
  893. break;
  894. }
  895. }
  896. if (sourceIsSelected)
  897. {
  898. for (uint i = 0; i < listView_.selectedItems.length; ++i)
  899. {
  900. UIElement@ item_ = listView_.selectedItems[i];
  901. // The source item is already added
  902. if (item_ is source)
  903. continue;
  904. if (item_.vars[TYPE_VAR] == ITEM_NODE)
  905. {
  906. Node@ node = editorScene.GetNode(item_.vars[NODE_ID_VAR].GetUInt());
  907. if (node !is null)
  908. nodeList.Push(node);
  909. }
  910. }
  911. }
  912. }
  913. return nodeList;
  914. }
  915. bool TestDragDrop(UIElement@ source, UIElement@ target, int& itemType)
  916. {
  917. int targetItemType = target.GetVar(TYPE_VAR).GetInt();
  918. if (targetItemType == ITEM_NODE)
  919. {
  920. Node@ sourceNode;
  921. Node@ targetNode;
  922. Variant variant = source.GetVar(NODE_ID_VAR);
  923. if (!variant.empty)
  924. sourceNode = editorScene.GetNode(variant.GetUInt());
  925. variant = target.GetVar(NODE_ID_VAR);
  926. if (!variant.empty)
  927. targetNode = editorScene.GetNode(variant.GetUInt());
  928. if (sourceNode !is null && targetNode !is null)
  929. {
  930. itemType = ITEM_NODE;
  931. if (sourceNode.parent is targetNode)
  932. return false;
  933. if (targetNode.parent is sourceNode)
  934. return false;
  935. }
  936. // Resource browser
  937. if (sourceNode is null && targetNode !is null)
  938. {
  939. itemType = ITEM_NODE;
  940. int type = source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt();
  941. return type == RESOURCE_TYPE_PREFAB ||
  942. type == RESOURCE_TYPE_SCRIPTFILE ||
  943. type == RESOURCE_TYPE_MODEL ||
  944. type == RESOURCE_TYPE_PARTICLEEFFECT ||
  945. type == RESOURCE_TYPE_2D_PARTICLE_EFFECT;
  946. }
  947. return true;
  948. }
  949. else if (targetItemType == ITEM_UI_ELEMENT)
  950. {
  951. UIElement@ sourceElement;
  952. UIElement@ targetElement;
  953. Variant variant = source.GetVar(UI_ELEMENT_ID_VAR);
  954. if (!variant.empty)
  955. sourceElement = GetUIElementByID(variant.GetUInt());
  956. variant = target.GetVar(UI_ELEMENT_ID_VAR);
  957. if (!variant.empty)
  958. targetElement = GetUIElementByID(variant.GetUInt());
  959. if (sourceElement !is null && targetElement !is null)
  960. {
  961. itemType = ITEM_UI_ELEMENT;
  962. if (sourceElement.parent is targetElement)
  963. return false;
  964. if (targetElement.parent is sourceElement)
  965. return false;
  966. }
  967. return true;
  968. }
  969. else if (targetItemType == ITEM_COMPONENT)
  970. {
  971. // Now only support dragging of nodes to StaticModelGroup, SplinePath or Constraint. Can be expanded to support others
  972. Node@ sourceNode;
  973. Component@ targetComponent;
  974. Variant variant = source.GetVar(NODE_ID_VAR);
  975. if (!variant.empty)
  976. sourceNode = editorScene.GetNode(variant.GetUInt());
  977. variant = target.GetVar(COMPONENT_ID_VAR);
  978. if (!variant.empty)
  979. targetComponent = editorScene.GetComponent(variant.GetUInt());
  980. itemType = ITEM_COMPONENT;
  981. if (sourceNode !is null && targetComponent !is null && (targetComponent.type == STATICMODELGROUP_TYPE ||
  982. targetComponent.type == CONSTRAINT_TYPE || targetComponent.type == SPLINEPATH_TYPE))
  983. return true;
  984. // resource browser
  985. int type = source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt();
  986. if (targetComponent.type == STATICMODEL_TYPE || targetComponent.type == ANIMATEDMODEL_TYPE)
  987. return type == RESOURCE_TYPE_MATERIAL || type == RESOURCE_TYPE_MODEL;
  988. return false;
  989. }
  990. else if (source.vars.Contains(TEXT_VAR_RESOURCE_TYPE)) // only testing resource browser ui elements
  991. {
  992. int type = source.GetVar(TEXT_VAR_RESOURCE_TYPE).GetInt();
  993. // test against resource pickers
  994. LineEdit@ lineEdit = cast<LineEdit>(target);
  995. if (lineEdit !is null)
  996. {
  997. StringHash resourceType = GetResourceTypeFromPickerLineEdit(lineEdit);
  998. if (resourceType == StringHash("Material") && type == RESOURCE_TYPE_MATERIAL)
  999. return true;
  1000. else if (resourceType == StringHash("Model") && type == RESOURCE_TYPE_MODEL)
  1001. return true;
  1002. else if (resourceType == StringHash("Animation") && type == RESOURCE_TYPE_ANIMATION)
  1003. return true;
  1004. }
  1005. }
  1006. return true;
  1007. }
  1008. StringHash GetResourceTypeFromPickerLineEdit(UIElement@ lineEdit)
  1009. {
  1010. Array<Serializable@>@ targets = GetAttributeEditorTargets(lineEdit);
  1011. if (!targets.empty)
  1012. {
  1013. resourcePickIndex = lineEdit.vars["Index"].GetUInt();
  1014. resourcePickSubIndex = lineEdit.vars["SubIndex"].GetUInt();
  1015. AttributeInfo info = targets[0].attributeInfos[resourcePickIndex];
  1016. StringHash resourceType;
  1017. if (info.type == VAR_RESOURCEREF)
  1018. return targets[0].attributes[resourcePickIndex].GetResourceRef().type;
  1019. else if (info.type == VAR_RESOURCEREFLIST)
  1020. return targets[0].attributes[resourcePickIndex].GetResourceRefList().type;
  1021. else if (info.type == VAR_VARIANTVECTOR)
  1022. return targets[0].attributes[resourcePickIndex].GetVariantVector()[resourcePickSubIndex].GetResourceRef().type;
  1023. }
  1024. return StringHash();
  1025. }
  1026. void FocusNode(Node@ node)
  1027. {
  1028. uint index = GetListIndex(node);
  1029. hierarchyList.selection = index;
  1030. }
  1031. void FocusComponent(Component@ component)
  1032. {
  1033. uint index = GetComponentListIndex(component);
  1034. hierarchyList.selection = index;
  1035. }
  1036. void FocusUIElement(UIElement@ element)
  1037. {
  1038. uint index = GetListIndex(element);
  1039. hierarchyList.selection = index;
  1040. }
  1041. void CreateBuiltinObject(const String& name)
  1042. {
  1043. Node@ newNode = editorScene.CreateChild(name, REPLICATED);
  1044. // Set the new node a certain distance from the camera
  1045. newNode.position = GetNewNodePosition();
  1046. StaticModel@ object = newNode.CreateComponent("StaticModel");
  1047. object.model = cache.GetResource("Model", "Models/" + name + ".mdl");
  1048. // Create an undo action for the create
  1049. CreateNodeAction action;
  1050. action.Define(newNode);
  1051. SaveEditAction(action);
  1052. SetSceneModified();
  1053. FocusNode(newNode);
  1054. }
  1055. bool CheckHierarchyWindowFocus()
  1056. {
  1057. // When we do edit operations based on key shortcuts, make sure the hierarchy list is focused
  1058. return ui.focusElement is hierarchyList || ui.focusElement is null;
  1059. }
  1060. bool CheckForExistingGlobalComponent(Node@ node, const String&in typeName)
  1061. {
  1062. if (typeName != "Octree" && typeName != "PhysicsWorld" && typeName != "DebugRenderer")
  1063. return false;
  1064. else
  1065. return node.HasComponent(typeName);
  1066. }
  1067. void HandleNodeAdded(StringHash eventType, VariantMap& eventData)
  1068. {
  1069. if (suppressSceneChanges)
  1070. return;
  1071. Node@ node = eventData["Node"].GetPtr();
  1072. if (showTemporaryObject || !node.temporary)
  1073. UpdateHierarchyItem(node);
  1074. }
  1075. void HandleNodeRemoved(StringHash eventType, VariantMap& eventData)
  1076. {
  1077. if (suppressSceneChanges)
  1078. return;
  1079. Node@ node = eventData["Node"].GetPtr();
  1080. uint index = GetListIndex(node);
  1081. UpdateHierarchyItem(index, null, null);
  1082. }
  1083. void HandleComponentAdded(StringHash eventType, VariantMap& eventData)
  1084. {
  1085. if (suppressSceneChanges)
  1086. return;
  1087. // Insert the newly added component at last component position but before the first child node position of the parent node
  1088. Node@ node = eventData["Node"].GetPtr();
  1089. Component@ component = eventData["Component"].GetPtr();
  1090. if (showTemporaryObject || !component.temporary)
  1091. {
  1092. uint nodeIndex = GetListIndex(node);
  1093. if (nodeIndex != NO_ITEM)
  1094. {
  1095. uint index = node.numChildren > 0 ? GetListIndex(node.children[0]) : M_MAX_UNSIGNED;
  1096. UpdateHierarchyItem(index, component, hierarchyList.items[nodeIndex]);
  1097. }
  1098. }
  1099. }
  1100. void HandleComponentRemoved(StringHash eventType, VariantMap& eventData)
  1101. {
  1102. if (suppressSceneChanges)
  1103. return;
  1104. Component@ component = eventData["Component"].GetPtr();
  1105. uint index = GetComponentListIndex(component);
  1106. if (index != NO_ITEM)
  1107. hierarchyList.RemoveItem(index);
  1108. }
  1109. void HandleNodeNameChanged(StringHash eventType, VariantMap& eventData)
  1110. {
  1111. if (suppressSceneChanges)
  1112. return;
  1113. Node@ node = eventData["Node"].GetPtr();
  1114. UpdateHierarchyItemText(GetListIndex(node), node.enabled, GetNodeTitle(node));
  1115. }
  1116. void HandleNodeEnabledChanged(StringHash eventType, VariantMap& eventData)
  1117. {
  1118. if (suppressSceneChanges)
  1119. return;
  1120. Node@ node = eventData["Node"].GetPtr();
  1121. UpdateHierarchyItemText(GetListIndex(node), node.enabled);
  1122. attributesDirty = true;
  1123. }
  1124. void HandleComponentEnabledChanged(StringHash eventType, VariantMap& eventData)
  1125. {
  1126. if (suppressSceneChanges)
  1127. return;
  1128. Component@ component = eventData["Component"].GetPtr();
  1129. UpdateHierarchyItemText(GetComponentListIndex(component), component.enabledEffective);
  1130. attributesDirty = true;
  1131. }
  1132. void HandleUIElementAdded(StringHash eventType, VariantMap& eventData)
  1133. {
  1134. if (suppressUIElementChanges)
  1135. return;
  1136. UIElement@ element = eventData["Element"].GetPtr();
  1137. if ((showInternalUIElement || !element.internal) && (showTemporaryObject || !element.temporary))
  1138. UpdateHierarchyItem(element);
  1139. }
  1140. void HandleUIElementRemoved(StringHash eventType, VariantMap& eventData)
  1141. {
  1142. if (suppressUIElementChanges)
  1143. return;
  1144. UIElement@ element = eventData["Element"].GetPtr();
  1145. UpdateHierarchyItem(GetListIndex(element), null, null);
  1146. }
  1147. void HandleElementNameChanged(StringHash eventType, VariantMap& eventData)
  1148. {
  1149. if (suppressUIElementChanges)
  1150. return;
  1151. UIElement@ element = eventData["Element"].GetPtr();
  1152. UpdateHierarchyItemText(GetListIndex(element), element.visible, GetUIElementTitle(element));
  1153. }
  1154. void HandleElementVisibilityChanged(StringHash eventType, VariantMap& eventData)
  1155. {
  1156. if (suppressUIElementChanges)
  1157. return;
  1158. UIElement@ element = eventData["Element"].GetPtr();
  1159. UpdateHierarchyItemText(GetListIndex(element), element.visible);
  1160. }
  1161. void HandleElementAttributeChanged(StringHash eventType, VariantMap& eventData)
  1162. {
  1163. // Do not refresh the attribute inspector while the attribute is being edited via the attribute-editors
  1164. if (suppressUIElementChanges || inEditAttribute)
  1165. return;
  1166. UIElement@ element = eventData["Element"].GetPtr();
  1167. for (uint i = 0; i < editUIElements.length; ++i)
  1168. {
  1169. if (editUIElements[i] is element)
  1170. attributesDirty = true;
  1171. }
  1172. }
  1173. void HandleTemporaryChanged(StringHash eventType, VariantMap& eventData)
  1174. {
  1175. if (suppressSceneChanges || suppressUIElementChanges)
  1176. return;
  1177. Serializable@ serializable = cast<Serializable>(GetEventSender());
  1178. Node@ node = cast<Node>(serializable);
  1179. if (node !is null && node.scene is editorScene)
  1180. {
  1181. if (showTemporaryObject)
  1182. UpdateHierarchyItemText(GetListIndex(node), node.enabled);
  1183. else if (!node.temporary && GetListIndex(node) == NO_ITEM)
  1184. UpdateHierarchyItem(node);
  1185. else if (node.temporary)
  1186. UpdateHierarchyItem(GetListIndex(node), null, null);
  1187. return;
  1188. }
  1189. Component@ component = cast<Component>(serializable);
  1190. if (component !is null && component.node !is null && component.node.scene is editorScene)
  1191. {
  1192. if (showTemporaryObject)
  1193. UpdateHierarchyItemText(GetComponentListIndex(component), node.enabled);
  1194. else if (!component.temporary && GetComponentListIndex(component) == NO_ITEM)
  1195. {
  1196. uint nodeIndex = GetListIndex(node);
  1197. if (nodeIndex != NO_ITEM)
  1198. {
  1199. uint index = node.numChildren > 0 ? GetListIndex(node.children[0]) : M_MAX_UNSIGNED;
  1200. UpdateHierarchyItem(index, component, hierarchyList.items[nodeIndex]);
  1201. }
  1202. }
  1203. else if (component.temporary)
  1204. {
  1205. uint index = GetComponentListIndex(component);
  1206. if (index != NO_ITEM)
  1207. hierarchyList.RemoveItem(index);
  1208. }
  1209. return;
  1210. }
  1211. UIElement@ element = cast<UIElement>(serializable);
  1212. if (element !is null)
  1213. {
  1214. if (showTemporaryObject)
  1215. UpdateHierarchyItemText(GetListIndex(element), element.visible);
  1216. else if (!element.temporary && GetListIndex(element) == NO_ITEM)
  1217. UpdateHierarchyItem(element);
  1218. else if (element.temporary)
  1219. UpdateHierarchyItem(GetListIndex(element), null, null);
  1220. return;
  1221. }
  1222. }
  1223. // Hierarchy window edit functions
  1224. bool Undo()
  1225. {
  1226. if (undoStackPos > 0)
  1227. {
  1228. --undoStackPos;
  1229. // Undo commands in reverse order
  1230. for (int i = int(undoStack[undoStackPos].actions.length - 1); i >= 0; --i)
  1231. undoStack[undoStackPos].actions[i].Undo();
  1232. }
  1233. return true;
  1234. }
  1235. bool Redo()
  1236. {
  1237. if (undoStackPos < undoStack.length)
  1238. {
  1239. // Redo commands in same order as stored
  1240. for (uint i = 0; i < undoStack[undoStackPos].actions.length; ++i)
  1241. undoStack[undoStackPos].actions[i].Redo();
  1242. ++undoStackPos;
  1243. }
  1244. return true;
  1245. }
  1246. bool Cut()
  1247. {
  1248. if (CheckHierarchyWindowFocus())
  1249. {
  1250. bool ret = true;
  1251. if (!selectedNodes.empty || !selectedComponents.empty)
  1252. ret = ret && SceneCut();
  1253. // Not mutually exclusive
  1254. if (!selectedUIElements.empty)
  1255. ret = ret && UIElementCut();
  1256. return ret;
  1257. }
  1258. return false;
  1259. }
  1260. bool Duplicate()
  1261. {
  1262. if (CheckHierarchyWindowFocus())
  1263. {
  1264. bool ret = true;
  1265. if (!selectedNodes.empty || !selectedComponents.empty)
  1266. ret = ret && (selectedNodes.empty || selectedComponents.empty ? SceneDuplicate() : false); // Node and component is mutually exclusive for copy action
  1267. // Not mutually exclusive
  1268. if (!selectedUIElements.empty)
  1269. ret = ret && UIElementDuplicate();
  1270. return ret;
  1271. }
  1272. return false;
  1273. }
  1274. bool Copy()
  1275. {
  1276. if (CheckHierarchyWindowFocus())
  1277. {
  1278. bool ret = true;
  1279. if (!selectedNodes.empty || !selectedComponents.empty)
  1280. ret = ret && (selectedNodes.empty || selectedComponents.empty ? SceneCopy() : false); // Node and component is mutually exclusive for copy action
  1281. // Not mutually exclusive
  1282. if (!selectedUIElements.empty)
  1283. ret = ret && UIElementCopy();
  1284. return ret;
  1285. }
  1286. return false;
  1287. }
  1288. bool Paste()
  1289. {
  1290. if (CheckHierarchyWindowFocus())
  1291. {
  1292. bool ret = true;
  1293. if (editNode !is null && !sceneCopyBuffer.empty)
  1294. ret = ret && ScenePaste();
  1295. // Not mutually exclusive
  1296. if (editUIElement !is null && !uiElementCopyBuffer.empty)
  1297. ret = ret && UIElementPaste();
  1298. return ret;
  1299. }
  1300. return false;
  1301. }
  1302. bool Delete()
  1303. {
  1304. if (CheckHierarchyWindowFocus())
  1305. {
  1306. bool ret = true;
  1307. if (!selectedNodes.empty || !selectedComponents.empty)
  1308. ret = ret && SceneDelete();
  1309. // Not mutually exclusive
  1310. if (!selectedUIElements.empty)
  1311. ret = ret && UIElementDelete();
  1312. return ret;
  1313. }
  1314. return false;
  1315. }
  1316. bool SelectAll()
  1317. {
  1318. if (CheckHierarchyWindowFocus())
  1319. {
  1320. if (!selectedNodes.empty || !selectedComponents.empty)
  1321. return SceneSelectAll();
  1322. else if (!selectedUIElements.empty || hierarchyList.items[GetListIndex(editorUIElement)].selected)
  1323. return UIElementSelectAll();
  1324. else
  1325. return SceneSelectAll(); // If nothing is selected yet, fall back to scene select all
  1326. }
  1327. return false;
  1328. }
  1329. bool DeselectAll()
  1330. {
  1331. if (CheckHierarchyWindowFocus())
  1332. {
  1333. BeginSelectionModify();
  1334. hierarchyList.ClearSelection();
  1335. EndSelectionModify();
  1336. return true;
  1337. }
  1338. return false;
  1339. }
  1340. bool ResetToDefault()
  1341. {
  1342. if (CheckHierarchyWindowFocus())
  1343. {
  1344. bool ret = true;
  1345. if (!selectedNodes.empty || !selectedComponents.empty)
  1346. ret = ret && (selectedNodes.empty || selectedComponents.empty ? SceneResetToDefault() : false); // Node and component is mutually exclusive for reset-to-default action
  1347. // Not mutually exclusive
  1348. if (!selectedUIElements.empty)
  1349. ret = ret && UIElementResetToDefault();
  1350. return ret;
  1351. }
  1352. return false;
  1353. }
  1354. void ClearEditActions()
  1355. {
  1356. undoStack.Clear();
  1357. undoStackPos = 0;
  1358. }
  1359. void SaveEditAction(EditAction@ action)
  1360. {
  1361. // Create a group with 1 action
  1362. EditActionGroup group;
  1363. group.actions.Push(action);
  1364. SaveEditActionGroup(group);
  1365. }
  1366. void SaveEditActionGroup(EditActionGroup@ group)
  1367. {
  1368. if (group.actions.empty)
  1369. return;
  1370. // Truncate the stack first to current pos
  1371. undoStack.Resize(undoStackPos);
  1372. undoStack.Push(group);
  1373. ++undoStackPos;
  1374. // Limit maximum undo steps
  1375. if (undoStack.length > MAX_UNDOSTACK_SIZE)
  1376. {
  1377. undoStack.Erase(0);
  1378. --undoStackPos;
  1379. }
  1380. }
  1381. void BeginSelectionModify()
  1382. {
  1383. // A large operation on selected nodes is about to begin. Disable intermediate selection updates
  1384. inSelectionModify = true;
  1385. // Cursor shape reverts back to normal automatically after the large operation is completed
  1386. ui.cursor.shape = CS_BUSY;
  1387. }
  1388. void EndSelectionModify()
  1389. {
  1390. // The large operation on selected nodes has ended. Update node/component selection now
  1391. inSelectionModify = false;
  1392. HandleHierarchyListSelectionChange();
  1393. }
  1394. void HandleHierarchyContextCreateReplicatedNode()
  1395. {
  1396. CreateNode(REPLICATED);
  1397. }
  1398. void HandleHierarchyContextCreateLocalNode()
  1399. {
  1400. CreateNode(LOCAL);
  1401. }
  1402. void HandleHierarchyContextDuplicate()
  1403. {
  1404. Duplicate();
  1405. }
  1406. void HandleHierarchyContextCopy()
  1407. {
  1408. Copy();
  1409. }
  1410. void HandleHierarchyContextCut()
  1411. {
  1412. Cut();
  1413. }
  1414. void HandleHierarchyContextDelete()
  1415. {
  1416. Delete();
  1417. }
  1418. void HandleHierarchyContextPaste()
  1419. {
  1420. Paste();
  1421. }
  1422. void HandleHierarchyContextResetToDefault()
  1423. {
  1424. ResetToDefault();
  1425. }
  1426. void HandleHierarchyContextResetPosition()
  1427. {
  1428. SceneResetPosition();
  1429. }
  1430. void HandleHierarchyContextResetRotation()
  1431. {
  1432. SceneResetRotation();
  1433. }
  1434. void HandleHierarchyContextResetScale()
  1435. {
  1436. SceneResetScale();
  1437. }
  1438. void HandleHierarchyContextEnableDisable()
  1439. {
  1440. SceneToggleEnable();
  1441. }
  1442. void HandleHierarchyContextUnparent()
  1443. {
  1444. SceneUnparent();
  1445. }
  1446. void HandleHierarchyContextUIElementCloseUILayout()
  1447. {
  1448. CloseUILayout();
  1449. }
  1450. void HandleHierarchyContextUIElementCloseAllUILayouts()
  1451. {
  1452. CloseAllUILayouts();
  1453. }