EditorHierarchyWindow.as 56 KB

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