EditorInspectorWindow.as 31 KB

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