EditorInspectorWindow.as 29 KB

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