EditorInspectorWindow.as 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. titleText.autoLocalizable = true;
  305. UIElement@ panel = titleText.GetChild("IconsPanel");
  306. panel.visible = false;
  307. }
  308. // Adjust size and position of manual-layout UI-elements, e.g. icons panel
  309. if (fullUpdate)
  310. HandleWindowLayoutUpdated();
  311. }
  312. void UpdateScriptInstances()
  313. {
  314. Array<Component@>@ components = scene.GetComponents("ScriptInstance", true);
  315. for (uint i = 0; i < components.length; i++)
  316. UpdateScriptAttributes(components[i]);
  317. components = scene.GetComponents("LuaScriptInstance", true);
  318. for (uint i = 0; i < components.length; i++)
  319. UpdateScriptAttributes(components[i]);
  320. }
  321. String GetComponentAttributeHash(Component@ component, uint index)
  322. {
  323. // We won't consider the main attributes, as they won't reset when an error occurs.
  324. if (component.typeName == "ScriptInstance")
  325. {
  326. if (index <= SCRIPTINSTANCE_ATTRIBUTE_IGNORE)
  327. return "";
  328. }
  329. else
  330. {
  331. if (index <= LUASCRIPTINSTANCE_ATTRIBUTE_IGNORE)
  332. return "";
  333. }
  334. AttributeInfo attributeInfo = component.attributeInfos[index];
  335. Variant attribute = component.attributes[index];
  336. return String(component.id) + "-" + attributeInfo.name + "-" + attribute.typeName;
  337. }
  338. void UpdateScriptAttributes(Component@ component)
  339. {
  340. for (uint i = Min(SCRIPTINSTANCE_ATTRIBUTE_IGNORE, LUASCRIPTINSTANCE_ATTRIBUTE_IGNORE) + 1; i < component.numAttributes; i++)
  341. {
  342. Variant attribute = component.attributes[i];
  343. // Component/node ID's are always unique within a scene, based on a simple increment.
  344. // This makes for a simple method of mapping a components attributes unique and consistent.
  345. // We will also use the type name in the hash to be able to recall and differentiate type changes.
  346. String hash = GetComponentAttributeHash(component, i);
  347. if (hash.empty)
  348. continue;
  349. if (!scriptAttributes.Contains(hash))
  350. {
  351. // set the initial value to the default value.
  352. scriptAttributes[hash] = attribute;
  353. }
  354. else
  355. {
  356. // recall the previously stored value
  357. component.attributes[i] = scriptAttributes[hash];
  358. }
  359. }
  360. component.ApplyAttributes();
  361. }
  362. /// Update the attribute list of the node container.
  363. void UpdateNodeAttributes()
  364. {
  365. bool fullUpdate = false;
  366. UpdateAttributes(ToSerializableArray(editNodes), GetNodeContainer().GetChild("AttributeList"), fullUpdate);
  367. }
  368. /// Update the icons enabled color based on the internal state of the objects.
  369. /// For node and component, based on "enabled" property.
  370. /// For ui-element, based on "visible" property.
  371. void UpdateAttributeInspectorIcons()
  372. {
  373. if (!editNodes.empty)
  374. {
  375. Text@ nodeTitle = GetNodeContainer().GetChild("TitleText");
  376. if (editNode !is null)
  377. SetIconEnabledColor(nodeTitle, editNode.enabled);
  378. else if (editNodes.length > 0)
  379. {
  380. bool hasSameEnabledState = true;
  381. for (uint i = 1; i < editNodes.length; ++i)
  382. {
  383. if (editNodes[i].enabled != editNodes[0].enabled)
  384. {
  385. hasSameEnabledState = false;
  386. break;
  387. }
  388. }
  389. SetIconEnabledColor(nodeTitle, editNodes[0].enabled, !hasSameEnabledState);
  390. }
  391. }
  392. if (!editComponents.empty)
  393. {
  394. uint numEditableComponents = editComponents.length / numEditableComponentsPerNode;
  395. for (uint j = 0; j < numEditableComponentsPerNode; ++j)
  396. {
  397. Text@ componentTitle = GetComponentContainer(j).GetChild("TitleText");
  398. bool enabledEffective = editComponents[j * numEditableComponents].enabledEffective;
  399. bool hasSameEnabledState = true;
  400. for (uint i = 1; i < numEditableComponents; ++i)
  401. {
  402. if (editComponents[j * numEditableComponents + i].enabledEffective != enabledEffective)
  403. {
  404. hasSameEnabledState = false;
  405. break;
  406. }
  407. }
  408. SetIconEnabledColor(componentTitle, enabledEffective, !hasSameEnabledState);
  409. }
  410. }
  411. if (!editUIElements.empty)
  412. {
  413. Text@ elementTitle = GetUIElementContainer().GetChild("TitleText");
  414. if (editUIElement !is null)
  415. SetIconEnabledColor(elementTitle, editUIElement.visible);
  416. else if (editUIElements.length > 0)
  417. {
  418. bool hasSameVisibleState = true;
  419. bool visible = cast<UIElement>(editUIElements[0]).visible;
  420. for (uint i = 1; i < editUIElements.length; ++i)
  421. {
  422. if (cast<UIElement>(editUIElements[i]).visible != visible)
  423. {
  424. hasSameVisibleState = false;
  425. break;
  426. }
  427. }
  428. SetIconEnabledColor(elementTitle, visible, !hasSameVisibleState);
  429. }
  430. }
  431. }
  432. /// Return true if the edit attribute action should continue.
  433. bool PreEditAttribute(Array<Serializable@>@ serializables, uint index)
  434. {
  435. return true;
  436. }
  437. /// Call after the attribute values in the target serializables have been edited.
  438. void PostEditAttribute(Array<Serializable@>@ serializables, uint index, const Array<Variant>& oldValues)
  439. {
  440. // Create undo actions for the edits
  441. EditActionGroup group;
  442. for (uint i = 0; i < serializables.length; ++i)
  443. {
  444. EditAttributeAction action;
  445. action.Define(serializables[i], index, oldValues[i]);
  446. group.actions.Push(action);
  447. }
  448. SaveEditActionGroup(group);
  449. // If a UI-element changing its 'Is Modal' attribute, clear the hierarchy list selection
  450. int itemType = GetType(serializables[0]);
  451. if (itemType == ITEM_UI_ELEMENT && serializables[0].attributeInfos[index].name == "Is Modal")
  452. hierarchyList.ClearSelection();
  453. for (uint i = 0; i < serializables.length; ++i)
  454. {
  455. PostEditAttribute(serializables[i], index);
  456. if (itemType == ITEM_UI_ELEMENT)
  457. SetUIElementModified(serializables[i]);
  458. }
  459. if (itemType != ITEM_UI_ELEMENT)
  460. SetSceneModified();
  461. }
  462. /// Call after the attribute values in the target serializables have been edited.
  463. void PostEditAttribute(Serializable@ serializable, uint index)
  464. {
  465. // If a StaticModel/AnimatedModel/Skybox model was changed, apply a possibly different material list
  466. if (applyMaterialList && serializable.attributeInfos[index].name == "Model")
  467. {
  468. StaticModel@ staticModel = cast<StaticModel>(serializable);
  469. if (staticModel !is null)
  470. staticModel.ApplyMaterialList();
  471. }
  472. // If a CollisionShape changed the shape type to trimesh or convex, and a collision model is not set,
  473. // try to get it from a StaticModel in the same node
  474. if (serializable.typeName == "CollisionShape" && serializable.attributeInfos[index].name == "Shape Type")
  475. {
  476. int shapeType = serializable.GetAttribute("Shape Type").GetInt();
  477. if ((shapeType == 6 || shapeType == 7) && serializable.GetAttribute("CustomGeometry NodeID").GetInt() == 0 &&
  478. serializable.GetAttribute("Model").GetResourceRef().name.Trimmed().length == 0)
  479. {
  480. Node@ ownerNode = cast<Component>(serializable).node;
  481. if (ownerNode !is null)
  482. {
  483. StaticModel@ staticModel = ownerNode.GetComponent("StaticModel");
  484. if (staticModel !is null)
  485. {
  486. serializable.SetAttribute("Model", staticModel.GetAttribute("Model"));
  487. serializable.ApplyAttributes();
  488. }
  489. }
  490. }
  491. }
  492. }
  493. /// Store the IDs of the actual serializable objects into user-defined variable of the 'attribute editor' (e.g. line-edit, drop-down-list, etc).
  494. void SetAttributeEditorID(UIElement@ attrEdit, Array<Serializable@>@ serializables)
  495. {
  496. if (serializables is null || serializables.length == 0)
  497. return;
  498. // All target serializables must be either nodes, ui-elements, or components
  499. Array<Variant> ids;
  500. switch (GetType(serializables[0]))
  501. {
  502. case ITEM_NODE:
  503. for (uint i = 0; i < serializables.length; ++i)
  504. ids.Push(cast<Node>(serializables[i]).id);
  505. attrEdit.vars[NODE_IDS_VAR] = ids;
  506. break;
  507. case ITEM_COMPONENT:
  508. for (uint i = 0; i < serializables.length; ++i)
  509. ids.Push(cast<Component>(serializables[i]).id);
  510. attrEdit.vars[COMPONENT_IDS_VAR] = ids;
  511. break;
  512. case ITEM_UI_ELEMENT:
  513. for (uint i = 0; i < serializables.length; ++i)
  514. ids.Push(GetUIElementID(cast<UIElement>(serializables[i])));
  515. attrEdit.vars[UI_ELEMENT_IDS_VAR] = ids;
  516. break;
  517. default:
  518. break;
  519. }
  520. }
  521. /// Return the actual serializable objects based on the IDs stored in the user-defined variable of the 'attribute editor'.
  522. Array<Serializable@>@ GetAttributeEditorTargets(UIElement@ attrEdit)
  523. {
  524. Array<Serializable@> ret;
  525. Variant variant = attrEdit.GetVar(NODE_IDS_VAR);
  526. if (!variant.empty)
  527. {
  528. Array<Variant>@ ids = variant.GetVariantVector();
  529. for (uint i = 0; i < ids.length; ++i)
  530. {
  531. Node@ node = editorScene.GetNode(ids[i].GetUInt());
  532. if (node !is null)
  533. ret.Push(node);
  534. }
  535. }
  536. else
  537. {
  538. variant = attrEdit.GetVar(COMPONENT_IDS_VAR);
  539. if (!variant.empty)
  540. {
  541. Array<Variant>@ ids = variant.GetVariantVector();
  542. for (uint i = 0; i < ids.length; ++i)
  543. {
  544. Component@ component = editorScene.GetComponent(ids[i].GetUInt());
  545. if (component !is null)
  546. ret.Push(component);
  547. }
  548. }
  549. else
  550. {
  551. variant = attrEdit.GetVar(UI_ELEMENT_IDS_VAR);
  552. if (!variant.empty)
  553. {
  554. Array<Variant>@ ids = variant.GetVariantVector();
  555. for (uint i = 0; i < ids.length; ++i)
  556. {
  557. UIElement@ element = editorUIElement.GetChild(UI_ELEMENT_ID_VAR, ids[i], true);
  558. if (element !is null)
  559. ret.Push(element);
  560. }
  561. }
  562. }
  563. }
  564. return ret;
  565. }
  566. /// Handle reset to default event, sent when reset icon in the icon-panel is clicked.
  567. void HandleResetToDefault(StringHash eventType, VariantMap& eventData)
  568. {
  569. ui.cursor.shape = CS_BUSY;
  570. UIElement@ button = eventData["Element"].GetPtr();
  571. Array<Serializable@>@ serializables = GetAttributeEditorTargets(button);
  572. if (serializables.empty)
  573. return;
  574. // Group for storing undo actions
  575. EditActionGroup group;
  576. // Reset target serializables to their default values
  577. for (uint i = 0; i < serializables.length; ++i)
  578. {
  579. Serializable@ target = serializables[i];
  580. ResetAttributesAction action;
  581. action.Define(target);
  582. group.actions.Push(action);
  583. target.ResetToDefault();
  584. if (action.targetType == ITEM_UI_ELEMENT)
  585. {
  586. action.SetInternalVars(target);
  587. SetUIElementModified(target);
  588. }
  589. target.ApplyAttributes();
  590. for (uint j = 0; j < target.numAttributes; ++j)
  591. PostEditAttribute(target, j);
  592. }
  593. SaveEditActionGroup(group);
  594. if (GetType(serializables[0]) != ITEM_UI_ELEMENT)
  595. SetSceneModified();
  596. attributesFullDirty = true;
  597. }
  598. /// Handle create new user-defined variable event for node target.
  599. void CreateNodeVariable(StringHash eventType, VariantMap& eventData)
  600. {
  601. if (editNodes.empty)
  602. return;
  603. String newName = ExtractVariableName(eventData);
  604. if (newName.empty)
  605. return;
  606. // Create scene variable
  607. editorScene.RegisterVar(newName);
  608. globalVarNames[newName] = newName;
  609. Variant newValue = ExtractVariantType(eventData);
  610. // If we overwrite an existing variable, must recreate the attribute-editor(s) for the correct type
  611. bool overwrite = false;
  612. for (uint i = 0; i < editNodes.length; ++i)
  613. {
  614. overwrite = overwrite || editNodes[i].vars.Contains(newName);
  615. editNodes[i].vars[newName] = newValue;
  616. }
  617. if (overwrite)
  618. attributesFullDirty = true;
  619. else
  620. attributesDirty = true;
  621. }
  622. /// Handle delete existing user-defined variable event for node target.
  623. void DeleteNodeVariable(StringHash eventType, VariantMap& eventData)
  624. {
  625. if (editNodes.empty)
  626. return;
  627. String delName = ExtractVariableName(eventData);
  628. if (delName.empty)
  629. return;
  630. // Note: intentionally do not unregister the variable name here as the same variable name may still be used by other attribute list
  631. bool erased = false;
  632. for (uint i = 0; i < editNodes.length; ++i)
  633. {
  634. // \todo Should first check whether var in question is editable
  635. erased = editNodes[i].vars.Erase(delName) || erased;
  636. }
  637. if (erased)
  638. attributesDirty = true;
  639. }
  640. /// Handle create new user-defined variable event for ui-element target.
  641. void CreateUIElementVariable(StringHash eventType, VariantMap& eventData)
  642. {
  643. if (editUIElements.empty)
  644. return;
  645. String newName = ExtractVariableName(eventData);
  646. if (newName.empty)
  647. return;
  648. // Create UIElement variable
  649. globalVarNames[newName] = newName;
  650. Variant newValue = ExtractVariantType(eventData);
  651. // If we overwrite an existing variable, must recreate the attribute-editor(s) for the correct type
  652. bool overwrite = false;
  653. for (uint i = 0; i < editUIElements.length; ++i)
  654. {
  655. UIElement@ element = cast<UIElement>(editUIElements[i]);
  656. overwrite = overwrite || element.vars.Contains(newName);
  657. element.vars[newName] = newValue;
  658. }
  659. if (overwrite)
  660. attributesFullDirty = true;
  661. else
  662. attributesDirty = true;
  663. }
  664. /// Handle delete existing user-defined variable event for ui-element target.
  665. void DeleteUIElementVariable(StringHash eventType, VariantMap& eventData)
  666. {
  667. if (editUIElements.empty)
  668. return;
  669. String delName = ExtractVariableName(eventData);
  670. if (delName.empty)
  671. return;
  672. // Note: intentionally do not unregister the variable name here as the same variable name may still be used by other attribute list
  673. bool erased = false;
  674. for (uint i = 0; i < editUIElements.length; ++i)
  675. {
  676. // \todo Should first check whether var in question is editable
  677. erased = cast<UIElement>(editUIElements[i]).vars.Erase(delName) || erased;
  678. }
  679. if (erased)
  680. attributesDirty = true;
  681. }
  682. String ExtractVariableName(VariantMap& eventData)
  683. {
  684. UIElement@ element = eventData["Element"].GetPtr();
  685. LineEdit@ nameEdit = element.parent.GetChild("VarNameEdit");
  686. return nameEdit.text.Trimmed();
  687. }
  688. Variant ExtractVariantType(VariantMap& eventData)
  689. {
  690. DropDownList@ dropDown = eventData["Element"].GetPtr();
  691. switch (dropDown.selection)
  692. {
  693. case 0:
  694. return int(0);
  695. case 1:
  696. return false;
  697. case 2:
  698. return float(0.0);
  699. case 3:
  700. return Variant(String());
  701. case 4:
  702. return Variant(Vector3());
  703. case 5:
  704. return Variant(Color());
  705. }
  706. return Variant(); // This should not happen
  707. }
  708. /// Get back the human-readable variable name from the StringHash.
  709. String GetVarName(StringHash hash)
  710. {
  711. // First try to get it from scene
  712. String name = editorScene.GetVarName(hash);
  713. // Then from the global variable reverse mappings
  714. if (name.empty && globalVarNames.Contains(hash))
  715. name = globalVarNames[hash].ToString();
  716. return name;
  717. }
  718. bool inSetStyleListSelection = false;
  719. /// Select/highlight the matching style in the style drop-down-list based on specified style.
  720. void SetStyleListSelection(DropDownList@ styleList, const String&in style)
  721. {
  722. // Prevent infinite loop upon initial style selection
  723. inSetStyleListSelection = true;
  724. uint selection = M_MAX_UNSIGNED;
  725. String styleName = style.empty ? "auto" : style;
  726. Array<UIElement@> items = styleList.GetItems();
  727. for (uint i = 0; i < items.length; ++i)
  728. {
  729. Text@ element = cast<Text>(items[i]);
  730. if (element is null)
  731. continue; // It may be a divider
  732. if (element.text == styleName)
  733. {
  734. selection = i;
  735. break;
  736. }
  737. }
  738. styleList.selection = selection;
  739. inSetStyleListSelection = false;
  740. }
  741. /// Handle the style change of the target ui-elements event when a new style is picked from the style drop-down-list.
  742. void HandleStyleItemSelected(StringHash eventType, VariantMap& eventData)
  743. {
  744. if (inSetStyleListSelection || editUIElements.empty)
  745. return;
  746. ui.cursor.shape = CS_BUSY;
  747. DropDownList@ styleList = eventData["Element"].GetPtr();
  748. Text@ text = cast<Text>(styleList.selectedItem);
  749. if (text is null)
  750. return;
  751. String newStyle = text.text;
  752. if (newStyle == "auto")
  753. newStyle.Clear();
  754. // Group for storing undo actions
  755. EditActionGroup group;
  756. // Apply new style to selected UI-elements
  757. for (uint i = 0; i < editUIElements.length; ++i)
  758. {
  759. UIElement@ element = editUIElements[i];
  760. ApplyUIElementStyleAction action;
  761. action.Define(element, newStyle);
  762. group.actions.Push(action);
  763. // Use the Redo() to actually do the action
  764. action.Redo();
  765. }
  766. SaveEditActionGroup(group);
  767. }