EditorInspectorWindow.as 36 KB

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