EditorHierarchyWindow.as 54 KB

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