EditorHierarchyWindow.as 56 KB

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