EditorNodeWindow.as 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Urho3D editor node/component edit window handling
  2. #include "Scripts/Editor/AttributeEditor.as"
  3. Window@ nodeWindow;
  4. bool applyMaterialList = true;
  5. void CreateNodeWindow()
  6. {
  7. if (nodeWindow !is null)
  8. return;
  9. InitResourcePicker();
  10. InitVectorStructs();
  11. nodeWindow = ui.LoadLayout(cache.GetResource("XMLFile", "UI/EditorNodeWindow.xml"), uiStyle);
  12. ui.root.AddChild(nodeWindow);
  13. int height = Min(ui.root.height - 60, 500);
  14. nodeWindow.SetSize(300, height);
  15. nodeWindow.SetPosition(ui.root.width - 20 - nodeWindow.width, 40);
  16. nodeWindow.opacity = uiMaxOpacity;
  17. nodeWindow.BringToFront();
  18. UpdateNodeWindow();
  19. SubscribeToEvent(nodeWindow.GetChild("CloseButton", true), "Released", "HideNodeWindow");
  20. SubscribeToEvent(nodeWindow.GetChild("NewVarDropDown", true), "ItemSelected", "CreateNewVariable");
  21. SubscribeToEvent(nodeWindow.GetChild("DeleteVarButton", true), "Released", "DeleteVariable");
  22. }
  23. void HideNodeWindow()
  24. {
  25. nodeWindow.visible = false;
  26. }
  27. void ShowNodeWindow()
  28. {
  29. nodeWindow.visible = true;
  30. nodeWindow.BringToFront();
  31. }
  32. void UpdateNodeWindow()
  33. {
  34. // If a resource pick was in progress, it cannot be completed now, as component was changed
  35. PickResourceCanceled();
  36. Text@ nodeTitle = nodeWindow.GetChild("NodeTitle", true);
  37. Text@ componentTitle = nodeWindow.GetChild("ComponentTitle", true);
  38. if (editNode is null)
  39. {
  40. if (selectedNodes.length <= 1)
  41. nodeTitle.text = "No node";
  42. else
  43. nodeTitle.text = selectedNodes.length + " nodes";
  44. }
  45. else
  46. {
  47. String idStr;
  48. if (editNode.id >= FIRST_LOCAL_ID)
  49. idStr = "Local ID " + String(editNode.id - FIRST_LOCAL_ID);
  50. else
  51. idStr = "ID " + String(editNode.id);
  52. nodeTitle.text = editNode.typeName + " (" + idStr + ")";
  53. }
  54. if (editComponents.empty)
  55. {
  56. if (selectedComponents.length <= 1)
  57. componentTitle.text = "No component";
  58. else
  59. componentTitle.text = selectedComponents.length + " components";
  60. }
  61. else
  62. {
  63. String multiplierText;
  64. if (editComponents.length > 1)
  65. multiplierText = " (" + editComponents.length + "x)";
  66. componentTitle.text = GetComponentTitle(editComponents[0], 0) + multiplierText;
  67. }
  68. UpdateAttributes(true);
  69. }
  70. void UpdateAttributes(bool fullUpdate)
  71. {
  72. if (nodeWindow !is null)
  73. {
  74. Array<Serializable@> nodes;
  75. Array<Serializable@> components;
  76. if (editNode !is null)
  77. nodes.Push(editNode);
  78. for (uint i = 0; i < editComponents.length; ++i)
  79. components.Push(editComponents[i]);
  80. UpdateAttributes(nodes, nodeWindow.GetChild("NodeAttributeList", true), fullUpdate);
  81. UpdateAttributes(components, nodeWindow.GetChild("ComponentAttributeList", true), fullUpdate);
  82. }
  83. }
  84. void UpdateNodeAttributes()
  85. {
  86. if (nodeWindow !is null)
  87. {
  88. Array<Serializable@> nodes;
  89. if (editNode !is null)
  90. nodes.Push(editNode);
  91. UpdateAttributes(nodes, nodeWindow.GetChild("NodeAttributeList", true), false);
  92. }
  93. }
  94. void PostEditAttribute(Array<Serializable@>@ serializables, uint index)
  95. {
  96. // If node name changed, update it in the scene window also
  97. if (serializables[0] is editNode && serializables[0].attributeInfos[index].name == "Name")
  98. UpdateSceneWindowNodeOnly(editNode);
  99. // If a StaticModel/AnimatedModel/Skybox model was changed, apply a possibly different material list
  100. if (applyMaterialList && serializables[0].attributeInfos[index].name == "Model")
  101. {
  102. for (uint i = 0; i < serializables.length; ++i)
  103. {
  104. StaticModel@ staticModel = cast<StaticModel>(serializables[i]);
  105. if (staticModel !is null)
  106. ApplyMaterialList(staticModel);
  107. }
  108. }
  109. }
  110. void SetAttributeEditorID(UIElement@ attrEdit, Array<Serializable@>@ serializables)
  111. {
  112. // All target serializables must be either nodes or components, so can check the first for the type
  113. Node@ node = cast<Node>(serializables[0]);
  114. Array<Variant> ids;
  115. if (node !is null)
  116. {
  117. for (uint i = 0; i < serializables.length; ++i)
  118. ids.Push(Variant(cast<Node>(serializables[i]).id));
  119. attrEdit.vars["NodeIDs"] = ids;
  120. }
  121. else
  122. {
  123. for (uint i = 0; i < serializables.length; ++i)
  124. ids.Push(Variant(cast<Component>(serializables[i]).id));
  125. attrEdit.vars["ComponentIDs"] = ids;
  126. }
  127. }
  128. Array<Serializable@>@ GetAttributeEditorTargets(UIElement@ attrEdit)
  129. {
  130. Array<Serializable@> ret;
  131. if (attrEdit.vars.Contains("NodeIDs"))
  132. {
  133. Array<Variant>@ ids = attrEdit.vars["NodeIDs"].GetVariantVector();
  134. for (uint i = 0; i < ids.length; ++i)
  135. {
  136. Node@ node = editorScene.GetNode(ids[i].GetUInt());
  137. if (node !is null)
  138. ret.Push(node);
  139. }
  140. }
  141. if (attrEdit.vars.Contains("ComponentIDs"))
  142. {
  143. Array<Variant>@ ids = attrEdit.vars["ComponentIDs"].GetVariantVector();
  144. for (uint i = 0; i < ids.length; ++i)
  145. {
  146. Component@ component = editorScene.GetComponent(ids[i].GetUInt());
  147. if (component !is null)
  148. ret.Push(component);
  149. }
  150. }
  151. return ret;
  152. }
  153. void CreateNewVariable(StringHash eventType, VariantMap& eventData)
  154. {
  155. if (editNode is null)
  156. return;
  157. DropDownList@ dropDown = eventData["Element"].GetUIElement();
  158. LineEdit@ nameEdit = nodeWindow.GetChild("VarNameEdit", true);
  159. String sanitatedVarName = nameEdit.text.Trimmed().Replaced(";", "");
  160. if (sanitatedVarName.empty)
  161. return;
  162. editorScene.RegisterVar(sanitatedVarName);
  163. Variant newValue;
  164. switch (dropDown.selection)
  165. {
  166. case 0:
  167. newValue = int(0);
  168. break;
  169. case 1:
  170. newValue = false;
  171. break;
  172. case 2:
  173. newValue = float(0.0);
  174. break;
  175. case 3:
  176. newValue = String();
  177. break;
  178. case 4:
  179. newValue = Vector3();
  180. break;
  181. case 5:
  182. newValue = Color();
  183. break;
  184. }
  185. // If we overwrite an existing variable, must recreate the editor(s) for the correct type
  186. bool overwrite = editNode.vars.Contains(sanitatedVarName);
  187. editNode.vars[sanitatedVarName] = newValue;
  188. UpdateAttributes(overwrite);
  189. }
  190. void DeleteVariable(StringHash eventType, VariantMap& eventData)
  191. {
  192. if (editNode is null)
  193. return;
  194. LineEdit@ nameEdit = nodeWindow.GetChild("VarNameEdit", true);
  195. String sanitatedVarName = nameEdit.text.Trimmed().Replaced(";", "");
  196. if (sanitatedVarName.empty)
  197. return;
  198. editNode.vars.Erase(sanitatedVarName);
  199. UpdateAttributes(false);
  200. }