EditorInspectorWindow.as 27 KB

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