EditorInspectorWindow.as 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. // Urho3D editor attribute inspector window handling
  2. #include "Scripts/Editor/AttributeEditor.as"
  3. Window@ attributeInspectorWindow;
  4. UIElement@ parentContainer;
  5. UIElement@ inspectorLockButton;
  6. bool applyMaterialList = true;
  7. bool attributesDirty = false;
  8. bool attributesFullDirty = false;
  9. const String STRIKED_OUT = "——"; // Two unicode EM DASH (U+2014)
  10. const StringHash NODE_IDS_VAR("NodeIDs");
  11. const StringHash COMPONENT_IDS_VAR("ComponentIDs");
  12. const StringHash UI_ELEMENT_IDS_VAR("UIElementIDs");
  13. const int LABEL_WIDTH = 30;
  14. // Constants for accessing xmlResources
  15. Array<XMLFile@> xmlResources;
  16. const uint ATTRIBUTE_RES = 0;
  17. const uint VARIABLE_RES = 1;
  18. const uint STYLE_RES = 2;
  19. const uint TAGS_RES = 3;
  20. uint nodeContainerIndex = M_MAX_UNSIGNED;
  21. uint componentContainerStartIndex = 0;
  22. uint elementContainerIndex = M_MAX_UNSIGNED;
  23. // Node or UIElement hash-to-varname reverse mapping
  24. VariantMap globalVarNames;
  25. bool inspectorLocked = false;
  26. void InitXMLResources()
  27. {
  28. String[] resources = { "UI/EditorInspector_Attribute.xml", "UI/EditorInspector_Variable.xml",
  29. "UI/EditorInspector_Style.xml", "UI/EditorInspector_Tags.xml" };
  30. for (uint i = 0; i < resources.length; ++i)
  31. xmlResources.Push(cache.GetResource("XMLFile", resources[i]));
  32. }
  33. /// Delete all child containers in the inspector list.
  34. void DeleteAllContainers()
  35. {
  36. parentContainer.RemoveAllChildren();
  37. nodeContainerIndex = M_MAX_UNSIGNED;
  38. componentContainerStartIndex = 0;
  39. elementContainerIndex = M_MAX_UNSIGNED;
  40. }
  41. /// Get container at the specified index in the inspector list, the container must be created before.
  42. UIElement@ GetContainer(uint index)
  43. {
  44. return parentContainer.children[index];
  45. }
  46. /// Get node container in the inspector list, create the container if it is not yet available.
  47. UIElement@ GetNodeContainer()
  48. {
  49. if (nodeContainerIndex != M_MAX_UNSIGNED)
  50. return GetContainer(nodeContainerIndex);
  51. nodeContainerIndex = parentContainer.numChildren;
  52. parentContainer.LoadChildXML(xmlResources[ATTRIBUTE_RES], uiStyle);
  53. UIElement@ container = GetContainer(nodeContainerIndex);
  54. container.LoadChildXML(xmlResources[VARIABLE_RES], uiStyle);
  55. SubscribeToEvent(container.GetChild("ResetToDefault", true), "Released", "HandleResetToDefault");
  56. SubscribeToEvent(container.GetChild("NewVarDropDown", true), "ItemSelected", "CreateNodeVariable");
  57. SubscribeToEvent(container.GetChild("DeleteVarButton", true), "Released", "DeleteNodeVariable");
  58. ++componentContainerStartIndex;
  59. parentContainer.LoadChildXML(xmlResources[TAGS_RES], uiStyle);
  60. parentContainer.GetChild("TagsLabel", true).SetFixedWidth(LABEL_WIDTH);
  61. LineEdit@ tagEdit = parentContainer.GetChild("TagsEdit", true);
  62. SubscribeToEvent(tagEdit, "TextChanged", "HandleTagsEdit");
  63. UIElement@ tagSelect = parentContainer.GetChild("TagsSelect", true);
  64. SubscribeToEvent(tagSelect, "Released", "HandleTagsSelect");
  65. ++componentContainerStartIndex;
  66. return container;
  67. }
  68. /// Get component container at the specified index, create the container if it is not yet available at the specified index.
  69. UIElement@ GetComponentContainer(uint index)
  70. {
  71. if (componentContainerStartIndex + index < parentContainer.numChildren)
  72. return GetContainer(componentContainerStartIndex + index);
  73. UIElement@ container;
  74. for (uint i = parentContainer.numChildren; i <= componentContainerStartIndex + index; ++i)
  75. {
  76. parentContainer.LoadChildXML(xmlResources[ATTRIBUTE_RES], uiStyle);
  77. container = GetContainer(i);
  78. SubscribeToEvent(container.GetChild("ResetToDefault", true), "Released", "HandleResetToDefault");
  79. }
  80. return container;
  81. }
  82. /// Get UI-element container, create the container if it is not yet available.
  83. UIElement@ GetUIElementContainer()
  84. {
  85. if (elementContainerIndex != M_MAX_UNSIGNED)
  86. return GetContainer(elementContainerIndex);
  87. elementContainerIndex = parentContainer.numChildren;
  88. parentContainer.LoadChildXML(xmlResources[ATTRIBUTE_RES], uiStyle);
  89. parentContainer.LoadChildXML(xmlResources[TAGS_RES], uiStyle);
  90. parentContainer.GetChild("TagsLabel", true).SetFixedWidth(LABEL_WIDTH);
  91. UIElement@ container = GetContainer(elementContainerIndex);
  92. container.LoadChildXML(xmlResources[VARIABLE_RES], uiStyle);
  93. container.LoadChildXML(xmlResources[STYLE_RES], uiStyle);
  94. DropDownList@ styleList = container.GetChild("StyleDropDown", true);
  95. styleList.placeholderText = STRIKED_OUT;
  96. styleList.parent.GetChild("StyleDropDownLabel").SetFixedWidth(LABEL_WIDTH);
  97. PopulateStyleList(styleList);
  98. SubscribeToEvent(container.GetChild("ResetToDefault", true), "Released", "HandleResetToDefault");
  99. SubscribeToEvent(container.GetChild("NewVarDropDown", true), "ItemSelected", "CreateUIElementVariable");
  100. SubscribeToEvent(container.GetChild("DeleteVarButton", true), "Released", "DeleteUIElementVariable");
  101. SubscribeToEvent(styleList, "ItemSelected", "HandleStyleItemSelected");
  102. LineEdit@ tagEdit = parentContainer.GetChild("TagsEdit", true);
  103. SubscribeToEvent(tagEdit, "TextChanged", "HandleTagsEdit");
  104. UIElement@ tagSelect = parentContainer.GetChild("TagsSelect", true);
  105. SubscribeToEvent(tagSelect, "Released", "HandleTagsSelect");
  106. return container;
  107. }
  108. void CreateAttributeInspectorWindow()
  109. {
  110. if (attributeInspectorWindow !is null)
  111. return;
  112. InitResourcePicker();
  113. InitVectorStructs();
  114. InitXMLResources();
  115. attributeInspectorWindow = LoadEditorUI("UI/EditorInspectorWindow.xml");
  116. parentContainer = attributeInspectorWindow.GetChild("ParentContainer");
  117. ui.root.AddChild(attributeInspectorWindow);
  118. int height = Min(ui.root.height - 60, 500);
  119. attributeInspectorWindow.SetSize(344, height);
  120. attributeInspectorWindow.SetPosition(ui.root.width - 10 - attributeInspectorWindow.width, 100);
  121. attributeInspectorWindow.opacity = uiMaxOpacity;
  122. attributeInspectorWindow.BringToFront();
  123. inspectorLockButton = attributeInspectorWindow.GetChild("LockButton", true);
  124. UpdateAttributeInspector();
  125. SubscribeToEvent(inspectorLockButton, "Pressed", "ToggleInspectorLock");
  126. SubscribeToEvent(attributeInspectorWindow.GetChild("CloseButton", true), "Pressed", "HideAttributeInspectorWindow");
  127. SubscribeToEvent(attributeInspectorWindow, "LayoutUpdated", "HandleWindowLayoutUpdated");
  128. }
  129. void DisableInspectorLock()
  130. {
  131. inspectorLocked = false;
  132. if (inspectorLockButton !is null)
  133. inspectorLockButton.style = "Button";
  134. UpdateAttributeInspector(true);
  135. }
  136. void EnableInspectorLock()
  137. {
  138. inspectorLocked = true;
  139. if (inspectorLockButton !is null)
  140. inspectorLockButton.style = "ToggledButton";
  141. }
  142. void ToggleInspectorLock()
  143. {
  144. if (inspectorLocked)
  145. DisableInspectorLock();
  146. else
  147. EnableInspectorLock();
  148. }
  149. bool ToggleAttributeInspectorWindow()
  150. {
  151. if (attributeInspectorWindow.visible == false)
  152. ShowAttributeInspectorWindow();
  153. else
  154. HideAttributeInspectorWindow();
  155. return true;
  156. }
  157. void ShowAttributeInspectorWindow()
  158. {
  159. attributeInspectorWindow.visible = true;
  160. attributeInspectorWindow.BringToFront();
  161. }
  162. void HideAttributeInspectorWindow()
  163. {
  164. attributeInspectorWindow.visible = false;
  165. }
  166. /// Handle main window layout updated event by positioning elements that needs manually-positioning (elements that are children of UI-element container with "Free" layout-mode).
  167. void HandleWindowLayoutUpdated()
  168. {
  169. // When window resize and so the list's width is changed, adjust the 'Is enabled' container width and icon panel width so that their children stay at the right most position
  170. for (uint i = 0; i < parentContainer.numChildren; ++i)
  171. {
  172. UIElement@ container = GetContainer(i);
  173. ListView@ list = container.GetChild("AttributeList");
  174. if (list is null)
  175. continue;
  176. int width = list.width;
  177. // Adjust the icon panel's width
  178. UIElement@ panel = container.GetChild("IconsPanel", true);
  179. if (panel !is null)
  180. panel.width = width;
  181. // At the moment, only 'Is Enabled' container (place-holder + check box) is being created as child of the list view instead of as list item
  182. for (uint j = 0; j < list.numChildren; ++j)
  183. {
  184. UIElement@ element = list.children[j];
  185. if (!element.internal)
  186. {
  187. element.SetFixedWidth(width);
  188. UIElement@ title = container.GetChild("TitleText");
  189. element.position = IntVector2(0, (title.screenPosition - list.screenPosition).y);
  190. // Adjust icon panel's width one more time to cater for the space occupied by 'Is Enabled' check box
  191. if (panel !is null)
  192. panel.width = width - element.children[1].width - panel.layoutSpacing;
  193. break;
  194. }
  195. }
  196. }
  197. }
  198. Array<Serializable@> ToSerializableArray(Array<Node@> nodes)
  199. {
  200. Array<Serializable@> serializables;
  201. for (uint i = 0; i < nodes.length; ++i)
  202. serializables.Push(nodes[i]);
  203. return serializables;
  204. }
  205. /// Update the whole attribute inspector window, when fullUpdate flag is set to true then first delete all the containers and repopulate them again from scratch.
  206. /// The fullUpdate flag is usually set to true when the structure of the attributes are different than the existing attributes in the list.
  207. void UpdateAttributeInspector(bool fullUpdate = true)
  208. {
  209. if (inspectorLocked)
  210. return;
  211. attributesDirty = false;
  212. if (fullUpdate)
  213. attributesFullDirty = false;
  214. // If full update delete all containers and add them back as necessary
  215. if (fullUpdate)
  216. DeleteAllContainers();
  217. if (!editNodes.empty)
  218. {
  219. UIElement@ container = GetNodeContainer();
  220. Text@ nodeTitle = container.GetChild("TitleText");
  221. String nodeType;
  222. if (editNode !is null)
  223. {
  224. String idStr;
  225. if (editNode.id >= FIRST_LOCAL_ID)
  226. idStr = " (Local ID " + String(editNode.id) + ")";
  227. else
  228. idStr = " (ID " + String(editNode.id) + ")";
  229. nodeType = editNode.typeName;
  230. nodeTitle.text = nodeType + idStr;
  231. LineEdit@ tagEdit = parentContainer.GetChild("TagsEdit", true);
  232. tagEdit.text = Join(editNode.tags, ";");
  233. }
  234. else
  235. {
  236. nodeType = editNodes[0].typeName;
  237. nodeTitle.text = nodeType + " (ID " + STRIKED_OUT + " : " + editNodes.length + "x)";
  238. }
  239. IconizeUIElement(nodeTitle, nodeType);
  240. ListView@ list = container.GetChild("AttributeList");
  241. Array<Serializable@> nodes = ToSerializableArray(editNodes);
  242. UpdateAttributes(nodes, list, fullUpdate);
  243. if (fullUpdate)
  244. {
  245. //\todo Avoid hardcoding
  246. // Resize the node editor according to the number of variables, up to a certain maximum
  247. uint maxAttrs = Clamp(list.contentElement.numChildren, MIN_NODE_ATTRIBUTES, MAX_NODE_ATTRIBUTES);
  248. list.SetFixedHeight(maxAttrs * ATTR_HEIGHT + 2);
  249. container.SetFixedHeight(maxAttrs * ATTR_HEIGHT + 58);
  250. }
  251. // Set icon's target in the icon panel
  252. SetAttributeEditorID(container.GetChild("ResetToDefault", true), nodes);
  253. }
  254. if (!editComponents.empty)
  255. {
  256. uint numEditableComponents = editComponents.length / numEditableComponentsPerNode;
  257. String multiplierText;
  258. if (numEditableComponents > 1)
  259. multiplierText = " (" + numEditableComponents + "x)";
  260. for (uint j = 0; j < numEditableComponentsPerNode; ++j)
  261. {
  262. UIElement@ container = GetComponentContainer(j);
  263. Text@ componentTitle = container.GetChild("TitleText");
  264. componentTitle.text = GetComponentTitle(editComponents[j * numEditableComponents]) + multiplierText;
  265. IconizeUIElement(componentTitle, editComponents[j * numEditableComponents].typeName);
  266. SetIconEnabledColor(componentTitle, editComponents[j * numEditableComponents].enabledEffective);
  267. Array<Serializable@> components;
  268. for (uint i = 0; i < numEditableComponents; ++i)
  269. {
  270. Component@ component = editComponents[j * numEditableComponents + i];
  271. components.Push(component);
  272. }
  273. UpdateAttributes(components, container.GetChild("AttributeList"), fullUpdate);
  274. SetAttributeEditorID(container.GetChild("ResetToDefault", true), components);
  275. }
  276. }
  277. if (!editUIElements.empty)
  278. {
  279. UIElement@ container = GetUIElementContainer();
  280. Text@ titleText = container.GetChild("TitleText");
  281. DropDownList@ styleList = container.GetChild("StyleDropDown", true);
  282. String elementType;
  283. if (editUIElement !is null)
  284. {
  285. elementType = editUIElement.typeName;
  286. titleText.text = elementType + " [ID " + GetUIElementID(editUIElement).ToString() + "]";
  287. SetStyleListSelection(styleList, editUIElement.style);
  288. LineEdit@ tagEdit = parentContainer.GetChild("TagsEdit", true);
  289. tagEdit.text = Join(editUIElement.tags, ";");
  290. }
  291. else
  292. {
  293. elementType = editUIElements[0].typeName;
  294. String appliedStyle = cast<UIElement>(editUIElements[0]).style;
  295. bool sameType = true;
  296. bool sameStyle = true;
  297. for (uint i = 1; i < editUIElements.length; ++i)
  298. {
  299. if (editUIElements[i].typeName != elementType)
  300. {
  301. sameType = false;
  302. sameStyle = false;
  303. break;
  304. }
  305. if (sameStyle && cast<UIElement>(editUIElements[i]).style != appliedStyle)
  306. sameStyle = false;
  307. }
  308. titleText.text = (sameType ? elementType : "Mixed type") + " [ID " + STRIKED_OUT + " : " + editUIElements.length + "x]";
  309. SetStyleListSelection(SetEditable(styleList, sameStyle), sameStyle ? appliedStyle : STRIKED_OUT);
  310. if (!sameType)
  311. elementType.Clear(); // No icon
  312. }
  313. IconizeUIElement(titleText, elementType);
  314. UpdateAttributes(editUIElements, container.GetChild("AttributeList"), fullUpdate);
  315. SetAttributeEditorID(container.GetChild("ResetToDefault", true), editUIElements);
  316. }
  317. if (parentContainer.numChildren > 0)
  318. UpdateAttributeInspectorIcons();
  319. else
  320. {
  321. // No editables, insert a dummy component container to show the information
  322. Text@ titleText = GetComponentContainer(0).GetChild("TitleText");
  323. titleText.text = "Select editable objects";
  324. titleText.autoLocalizable = true;
  325. UIElement@ panel = titleText.GetChild("IconsPanel");
  326. panel.visible = false;
  327. }
  328. // Adjust size and position of manual-layout UI-elements, e.g. icons panel
  329. if (fullUpdate)
  330. HandleWindowLayoutUpdated();
  331. }
  332. /// Update the attribute list of the node container.
  333. void UpdateNodeAttributes()
  334. {
  335. bool fullUpdate = false;
  336. UpdateAttributes(ToSerializableArray(editNodes), GetNodeContainer().GetChild("AttributeList"), fullUpdate);
  337. if (fullUpdate)
  338. HandleWindowLayoutUpdated();
  339. }
  340. /// Update the icons enabled color based on the internal state of the objects.
  341. /// For node and component, based on "enabled" property.
  342. /// For ui-element, based on "visible" property.
  343. void UpdateAttributeInspectorIcons()
  344. {
  345. if (!editNodes.empty)
  346. {
  347. Text@ nodeTitle = GetNodeContainer().GetChild("TitleText");
  348. if (editNode !is null)
  349. SetIconEnabledColor(nodeTitle, editNode.enabled);
  350. else if (editNodes.length > 0)
  351. {
  352. bool hasSameEnabledState = true;
  353. for (uint i = 1; i < editNodes.length; ++i)
  354. {
  355. if (editNodes[i].enabled != editNodes[0].enabled)
  356. {
  357. hasSameEnabledState = false;
  358. break;
  359. }
  360. }
  361. SetIconEnabledColor(nodeTitle, editNodes[0].enabled, !hasSameEnabledState);
  362. }
  363. }
  364. if (!editComponents.empty)
  365. {
  366. uint numEditableComponents = editComponents.length / numEditableComponentsPerNode;
  367. for (uint j = 0; j < numEditableComponentsPerNode; ++j)
  368. {
  369. Text@ componentTitle = GetComponentContainer(j).GetChild("TitleText");
  370. bool enabledEffective = editComponents[j * numEditableComponents].enabledEffective;
  371. bool hasSameEnabledState = true;
  372. for (uint i = 1; i < numEditableComponents; ++i)
  373. {
  374. if (editComponents[j * numEditableComponents + i].enabledEffective != enabledEffective)
  375. {
  376. hasSameEnabledState = false;
  377. break;
  378. }
  379. }
  380. SetIconEnabledColor(componentTitle, enabledEffective, !hasSameEnabledState);
  381. }
  382. }
  383. if (!editUIElements.empty)
  384. {
  385. Text@ elementTitle = GetUIElementContainer().GetChild("TitleText");
  386. if (editUIElement !is null)
  387. SetIconEnabledColor(elementTitle, editUIElement.visible);
  388. else if (editUIElements.length > 0)
  389. {
  390. bool hasSameVisibleState = true;
  391. bool visible = cast<UIElement>(editUIElements[0]).visible;
  392. for (uint i = 1; i < editUIElements.length; ++i)
  393. {
  394. if (cast<UIElement>(editUIElements[i]).visible != visible)
  395. {
  396. hasSameVisibleState = false;
  397. break;
  398. }
  399. }
  400. SetIconEnabledColor(elementTitle, visible, !hasSameVisibleState);
  401. }
  402. }
  403. }
  404. /// Return true if the edit attribute action should continue.
  405. bool PreEditAttribute(Array<Serializable@>@ serializables, uint index)
  406. {
  407. return true;
  408. }
  409. /// Call after the attribute values in the target serializables have been edited.
  410. void PostEditAttribute(Array<Serializable@>@ serializables, uint index, const Array<Variant>& oldValues)
  411. {
  412. // Create undo actions for the edits
  413. EditActionGroup group;
  414. for (uint i = 0; i < serializables.length; ++i)
  415. {
  416. EditAttributeAction action;
  417. action.Define(serializables[i], index, oldValues[i]);
  418. group.actions.Push(action);
  419. }
  420. SaveEditActionGroup(group);
  421. // If a UI-element changing its 'Is Modal' attribute, clear the hierarchy list selection
  422. int itemType = GetType(serializables[0]);
  423. if (itemType == ITEM_UI_ELEMENT && serializables[0].attributeInfos[index].name == "Is Modal")
  424. hierarchyList.ClearSelection();
  425. for (uint i = 0; i < serializables.length; ++i)
  426. {
  427. PostEditAttribute(serializables[i], index);
  428. if (itemType == ITEM_UI_ELEMENT)
  429. SetUIElementModified(serializables[i]);
  430. }
  431. if (itemType != ITEM_UI_ELEMENT)
  432. SetSceneModified();
  433. }
  434. /// Call after the attribute values in the target serializables have been edited.
  435. void PostEditAttribute(Serializable@ serializable, uint index)
  436. {
  437. // If a StaticModel/AnimatedModel/Skybox model was changed, apply a possibly different material list
  438. if (applyMaterialList && serializable.attributeInfos[index].name == "Model")
  439. {
  440. StaticModel@ staticModel = cast<StaticModel>(serializable);
  441. if (staticModel !is null)
  442. staticModel.ApplyMaterialList();
  443. }
  444. // If a CollisionShape changed the shape type to trimesh or convex, and a collision model is not set,
  445. // try to get it from a StaticModel in the same node
  446. if (serializable.typeName == "CollisionShape" && serializable.attributeInfos[index].name == "Shape Type")
  447. {
  448. int shapeType = serializable.GetAttribute("Shape Type").GetInt();
  449. if ((shapeType == 6 || shapeType == 7) && serializable.GetAttribute("CustomGeometry ComponentID").GetInt() == 0 &&
  450. serializable.GetAttribute("Model").GetResourceRef().name.Trimmed().length == 0)
  451. {
  452. Node@ ownerNode = cast<Component>(serializable).node;
  453. if (ownerNode !is null)
  454. {
  455. StaticModel@ staticModel = ownerNode.GetComponent("StaticModel");
  456. if (staticModel !is null)
  457. {
  458. serializable.SetAttribute("Model", staticModel.GetAttribute("Model"));
  459. serializable.ApplyAttributes();
  460. }
  461. }
  462. }
  463. }
  464. }
  465. /// Store the IDs of the actual serializable objects into user-defined variable of the 'attribute editor' (e.g. line-edit, drop-down-list, etc).
  466. void SetAttributeEditorID(UIElement@ attrEdit, Array<Serializable@>@ serializables)
  467. {
  468. if (serializables is null || serializables.length == 0)
  469. return;
  470. // All target serializables must be either nodes, ui-elements, or components
  471. Array<Variant> ids;
  472. switch (GetType(serializables[0]))
  473. {
  474. case ITEM_NODE:
  475. for (uint i = 0; i < serializables.length; ++i)
  476. ids.Push(cast<Node>(serializables[i]).id);
  477. attrEdit.vars[NODE_IDS_VAR] = ids;
  478. break;
  479. case ITEM_COMPONENT:
  480. for (uint i = 0; i < serializables.length; ++i)
  481. ids.Push(cast<Component>(serializables[i]).id);
  482. attrEdit.vars[COMPONENT_IDS_VAR] = ids;
  483. break;
  484. case ITEM_UI_ELEMENT:
  485. for (uint i = 0; i < serializables.length; ++i)
  486. ids.Push(GetUIElementID(cast<UIElement>(serializables[i])));
  487. attrEdit.vars[UI_ELEMENT_IDS_VAR] = ids;
  488. break;
  489. default:
  490. break;
  491. }
  492. }
  493. /// Return the actual serializable objects based on the IDs stored in the user-defined variable of the 'attribute editor'.
  494. Array<Serializable@>@ GetAttributeEditorTargets(UIElement@ attrEdit)
  495. {
  496. Array<Serializable@> ret;
  497. Variant variant = attrEdit.GetVar(NODE_IDS_VAR);
  498. if (!variant.empty)
  499. {
  500. Array<Variant>@ ids = variant.GetVariantVector();
  501. for (uint i = 0; i < ids.length; ++i)
  502. {
  503. Node@ node = editorScene.GetNode(ids[i].GetUInt());
  504. if (node !is null)
  505. ret.Push(node);
  506. }
  507. }
  508. else
  509. {
  510. variant = attrEdit.GetVar(COMPONENT_IDS_VAR);
  511. if (!variant.empty)
  512. {
  513. Array<Variant>@ ids = variant.GetVariantVector();
  514. for (uint i = 0; i < ids.length; ++i)
  515. {
  516. Component@ component = editorScene.GetComponent(ids[i].GetUInt());
  517. if (component !is null)
  518. ret.Push(component);
  519. }
  520. }
  521. else
  522. {
  523. variant = attrEdit.GetVar(UI_ELEMENT_IDS_VAR);
  524. if (!variant.empty)
  525. {
  526. Array<Variant>@ ids = variant.GetVariantVector();
  527. for (uint i = 0; i < ids.length; ++i)
  528. {
  529. UIElement@ element = editorUIElement.GetChild(UI_ELEMENT_ID_VAR, ids[i], true);
  530. if (element !is null)
  531. ret.Push(element);
  532. }
  533. }
  534. }
  535. }
  536. return ret;
  537. }
  538. void HandleTagsEdit(StringHash eventType, VariantMap& eventData)
  539. {
  540. LineEdit@ lineEdit = eventData["Element"].GetPtr();
  541. Array<String> tags = lineEdit.text.Split(';');
  542. if (editUIElement !is null)
  543. {
  544. editUIElement.RemoveAllTags();
  545. for (uint i = 0; i < tags.length; i++)
  546. editUIElement.AddTag(tags[i].Trimmed());
  547. }
  548. else if (editNode !is null)
  549. {
  550. editNode.RemoveAllTags();
  551. for (uint i = 0; i < tags.length; i++)
  552. editNode.AddTag(tags[i].Trimmed());
  553. }
  554. }
  555. void HandleTagsSelect(StringHash eventType, VariantMap& eventData)
  556. {
  557. UIElement@ tagSelect = eventData["Element"].GetPtr();
  558. Array<UIElement@> actions;
  559. String Indicator = "* ";
  560. // In first priority changes to UIElement
  561. if (editUIElement !is null)
  562. {
  563. // 1. Add established tags from current editable UIElement to menu
  564. Array<String> elementTags = editUIElement.tags;
  565. for (uint i = 0; i < elementTags.length; i++)
  566. {
  567. bool isHasTag = editUIElement.HasTag(elementTags[i]);
  568. String taggedIndicator = (isHasTag ? Indicator : "");
  569. actions.Push(CreateContextMenuItem(taggedIndicator + elementTags[i], "HandleTagsMenuSelection", elementTags[i]));
  570. }
  571. // 2. Add default tags
  572. Array<String> stdTags = defaultTags.Split(';');
  573. for (uint i= 0; i < stdTags.length; i++)
  574. {
  575. bool isHasTag = editUIElement.HasTag(stdTags[i]);
  576. // Add this tag into menu if only Node not tadded with it yet, otherwise it showed on step 1.
  577. if (!isHasTag)
  578. {
  579. String taggedIndicator = (isHasTag ? Indicator : "");
  580. actions.Push(CreateContextMenuItem(taggedIndicator + stdTags[i], "HandleTagsMenuSelection", stdTags[i]));
  581. }
  582. }
  583. }
  584. else if (editNode !is null)
  585. {
  586. // 1. Add established tags from Node to menu
  587. Array<String> nodeTags = editNode.tags;
  588. for (uint i = 0; i < nodeTags.length; i++)
  589. {
  590. bool isHasTag = editNode.HasTag(nodeTags[i]);
  591. String taggedIndicator = (isHasTag ? Indicator : "");
  592. actions.Push(CreateContextMenuItem(taggedIndicator + nodeTags[i], "HandleTagsMenuSelection", nodeTags[i]));
  593. }
  594. Array<String> sceneTags = editorScene.tags;
  595. // 2. Add tags from Scene.tags (In this scenario Scene.tags used as storage for frequently used tags in current Scene only)
  596. for (uint i = 0; i < sceneTags.length; i++)
  597. {
  598. bool isHasTag = editNode.HasTag(sceneTags[i]);
  599. // Add this tag into menu if only Node not tadded with it yet, otherwise it showed on step 1.
  600. if (!isHasTag)
  601. {
  602. String taggedIndicator = (isHasTag ? Indicator : "");
  603. actions.Push(CreateContextMenuItem(taggedIndicator + sceneTags[i], "HandleTagsMenuSelection", sceneTags[i]));
  604. }
  605. }
  606. // 3. Add default tags
  607. Array<String> stdTags = defaultTags.Split(';');
  608. for (uint i = 0; i < stdTags.length; i++)
  609. {
  610. bool isHasTag = editNode.HasTag(stdTags[i]);
  611. // Add this tag into menu if only Node not tadded with it yet, otherwise it showed on step 1.
  612. if (!isHasTag)
  613. {
  614. String taggedIndicator = (isHasTag ? Indicator : "");
  615. actions.Push(CreateContextMenuItem(taggedIndicator + stdTags[i], "HandleTagsMenuSelection", stdTags[i]));
  616. }
  617. }
  618. }
  619. // if any action has been added, add also Reset and Cancel and show menu
  620. if (actions.length > 0)
  621. {
  622. actions.Push(CreateContextMenuItem("Reset", "HandleTagsMenuSelection", "Reset"));
  623. actions.Push(CreateContextMenuItem("Cancel", "HandleTagsMenuSelectionDivisor"));
  624. ActivateContextMenu(actions);
  625. }
  626. }
  627. void HandleTagsMenuSelectionDivisor()
  628. {
  629. //do nothing
  630. }
  631. void HandleTagsMenuSelection()
  632. {
  633. Menu@ menu = GetEventSender();
  634. if (menu is null)
  635. return;
  636. String menuSelectedTag = menu.name;
  637. // In first priority changes to UIElement
  638. if (editUIElement !is null)
  639. {
  640. if (menuSelectedTag == "Reset")
  641. {
  642. editUIElement.RemoveAllTags();
  643. UpdateAttributeInspector();
  644. return;
  645. }
  646. if (!editUIElement.HasTag(menuSelectedTag))
  647. {
  648. editUIElement.AddTag(menuSelectedTag.Trimmed());
  649. }
  650. else
  651. {
  652. editUIElement.RemoveTag(menuSelectedTag.Trimmed());
  653. }
  654. }
  655. else if (editNode !is null)
  656. {
  657. if (menuSelectedTag == "Reset")
  658. {
  659. editNode.RemoveAllTags();
  660. UpdateAttributeInspector();
  661. return;
  662. }
  663. if (!editNode.HasTag(menuSelectedTag))
  664. {
  665. editNode.AddTag(menuSelectedTag.Trimmed());
  666. }
  667. else
  668. {
  669. editNode.RemoveTag(menuSelectedTag.Trimmed());
  670. }
  671. }
  672. UpdateAttributeInspector();
  673. }
  674. /// Handle reset to default event, sent when reset icon in the icon-panel is clicked.
  675. void HandleResetToDefault(StringHash eventType, VariantMap& eventData)
  676. {
  677. ui.cursor.shape = CS_BUSY;
  678. UIElement@ button = eventData["Element"].GetPtr();
  679. Array<Serializable@>@ serializables = GetAttributeEditorTargets(button);
  680. if (serializables.empty)
  681. return;
  682. // Group for storing undo actions
  683. EditActionGroup group;
  684. // Reset target serializables to their default values
  685. for (uint i = 0; i < serializables.length; ++i)
  686. {
  687. Serializable@ target = serializables[i];
  688. ResetAttributesAction action;
  689. action.Define(target);
  690. group.actions.Push(action);
  691. target.ResetToDefault();
  692. if (action.targetType == ITEM_UI_ELEMENT)
  693. {
  694. action.SetInternalVars(target);
  695. SetUIElementModified(target);
  696. }
  697. target.ApplyAttributes();
  698. for (uint j = 0; j < target.numAttributes; ++j)
  699. PostEditAttribute(target, j);
  700. }
  701. SaveEditActionGroup(group);
  702. if (GetType(serializables[0]) != ITEM_UI_ELEMENT)
  703. SetSceneModified();
  704. attributesFullDirty = true;
  705. }
  706. /// Handle create new user-defined variable event for node target.
  707. void CreateNodeVariable(StringHash eventType, VariantMap& eventData)
  708. {
  709. if (editNodes.empty)
  710. return;
  711. String newName = ExtractVariableName(eventData);
  712. if (newName.empty)
  713. return;
  714. // Create scene variable
  715. editorScene.RegisterVar(newName);
  716. globalVarNames[newName] = newName;
  717. Variant newValue = ExtractVariantType(eventData);
  718. // If we overwrite an existing variable, must recreate the attribute-editor(s) for the correct type
  719. bool overwrite = false;
  720. for (uint i = 0; i < editNodes.length; ++i)
  721. {
  722. overwrite = overwrite || editNodes[i].vars.Contains(newName);
  723. editNodes[i].vars[newName] = newValue;
  724. }
  725. if (overwrite)
  726. attributesFullDirty = true;
  727. else
  728. attributesDirty = true;
  729. }
  730. /// Handle delete existing user-defined variable event for node target.
  731. void DeleteNodeVariable(StringHash eventType, VariantMap& eventData)
  732. {
  733. if (editNodes.empty)
  734. return;
  735. String delName = ExtractVariableName(eventData);
  736. if (delName.empty)
  737. return;
  738. bool erased = false;
  739. for (uint i = 0; i < editNodes.length; ++i)
  740. {
  741. // \todo Should first check whether var in question is editable
  742. erased = editNodes[i].vars.Erase(delName) || erased;
  743. }
  744. if (erased)
  745. {
  746. attributesDirty = true;
  747. // If the attribute is not defined in any other node, unregister from the scene
  748. // to prevent it from being unnecessarily saved; the global var list will still hold it
  749. // to keep the hash-name mapping known in case it's in use in other scenes
  750. Array<Node@>@ allChildren = editorScene.GetChildren(true);
  751. StringHash delNameHash(delName);
  752. bool inUse = false;
  753. for (uint i = 0; i < allChildren.length; ++i)
  754. {
  755. if (allChildren[i].vars.Contains(delNameHash))
  756. {
  757. inUse = true;
  758. break;
  759. }
  760. }
  761. if (!inUse)
  762. editorScene.UnregisterVar(delName);
  763. }
  764. }
  765. /// Handle create new user-defined variable event for ui-element target.
  766. void CreateUIElementVariable(StringHash eventType, VariantMap& eventData)
  767. {
  768. if (editUIElements.empty)
  769. return;
  770. String newName = ExtractVariableName(eventData);
  771. if (newName.empty)
  772. return;
  773. // Create UIElement variable
  774. globalVarNames[newName] = newName;
  775. Variant newValue = ExtractVariantType(eventData);
  776. // If we overwrite an existing variable, must recreate the attribute-editor(s) for the correct type
  777. bool overwrite = false;
  778. for (uint i = 0; i < editUIElements.length; ++i)
  779. {
  780. UIElement@ element = cast<UIElement>(editUIElements[i]);
  781. overwrite = overwrite || element.vars.Contains(newName);
  782. element.vars[newName] = newValue;
  783. }
  784. if (overwrite)
  785. attributesFullDirty = true;
  786. else
  787. attributesDirty = true;
  788. }
  789. /// Handle delete existing user-defined variable event for ui-element target.
  790. void DeleteUIElementVariable(StringHash eventType, VariantMap& eventData)
  791. {
  792. if (editUIElements.empty)
  793. return;
  794. String delName = ExtractVariableName(eventData);
  795. if (delName.empty)
  796. return;
  797. // Note: intentionally do not unregister the variable name here as the same variable name may still be used by other attribute list
  798. bool erased = false;
  799. for (uint i = 0; i < editUIElements.length; ++i)
  800. {
  801. // \todo Should first check whether var in question is editable
  802. erased = cast<UIElement>(editUIElements[i]).vars.Erase(delName) || erased;
  803. }
  804. if (erased)
  805. attributesDirty = true;
  806. }
  807. String ExtractVariableName(VariantMap& eventData)
  808. {
  809. UIElement@ element = eventData["Element"].GetPtr();
  810. LineEdit@ nameEdit = element.parent.GetChild("VarNameEdit");
  811. return nameEdit.text.Trimmed();
  812. }
  813. Variant ExtractVariantType(VariantMap& eventData)
  814. {
  815. DropDownList@ dropDown = eventData["Element"].GetPtr();
  816. switch (dropDown.selection)
  817. {
  818. case 0:
  819. return int(0);
  820. case 1:
  821. return false;
  822. case 2:
  823. return float(0.0);
  824. case 3:
  825. return Variant(String());
  826. case 4:
  827. return Variant(Vector3());
  828. case 5:
  829. return Variant(Color());
  830. }
  831. return Variant(); // This should not happen
  832. }
  833. /// Get back the human-readable variable name from the StringHash.
  834. String GetVarName(StringHash hash)
  835. {
  836. // First try to get it from scene
  837. String name = editorScene.GetVarName(hash);
  838. // Then from the global variable reverse mappings
  839. if (name.empty && globalVarNames.Contains(hash))
  840. name = globalVarNames[hash].ToString();
  841. return name;
  842. }
  843. bool inSetStyleListSelection = false;
  844. /// Select/highlight the matching style in the style drop-down-list based on specified style.
  845. void SetStyleListSelection(DropDownList@ styleList, const String&in style)
  846. {
  847. // Prevent infinite loop upon initial style selection
  848. inSetStyleListSelection = true;
  849. uint selection = M_MAX_UNSIGNED;
  850. String styleName = style.empty ? "auto" : style;
  851. Array<UIElement@> items = styleList.GetItems();
  852. for (uint i = 0; i < items.length; ++i)
  853. {
  854. Text@ element = cast<Text>(items[i]);
  855. if (element is null)
  856. continue; // It may be a divider
  857. if (element.text == styleName)
  858. {
  859. selection = i;
  860. break;
  861. }
  862. }
  863. styleList.selection = selection;
  864. inSetStyleListSelection = false;
  865. }
  866. /// Handle the style change of the target ui-elements event when a new style is picked from the style drop-down-list.
  867. void HandleStyleItemSelected(StringHash eventType, VariantMap& eventData)
  868. {
  869. if (inSetStyleListSelection || editUIElements.empty)
  870. return;
  871. ui.cursor.shape = CS_BUSY;
  872. DropDownList@ styleList = eventData["Element"].GetPtr();
  873. Text@ text = cast<Text>(styleList.selectedItem);
  874. if (text is null)
  875. return;
  876. String newStyle = text.text;
  877. if (newStyle == "auto")
  878. newStyle.Clear();
  879. // Group for storing undo actions
  880. EditActionGroup group;
  881. // Apply new style to selected UI-elements
  882. for (uint i = 0; i < editUIElements.length; ++i)
  883. {
  884. UIElement@ element = editUIElements[i];
  885. ApplyUIElementStyleAction action;
  886. action.Define(element, newStyle);
  887. group.actions.Push(action);
  888. // Use the Redo() to actually do the action
  889. action.Redo();
  890. }
  891. SaveEditActionGroup(group);
  892. }