EditorInspectorWindow.as 31 KB

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