EditorHierarchyWindow.as 61 KB

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