EditorInspectorWindow.as 27 KB

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