EditorHierarchyWindow.as 60 KB

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