AttributeEditor.as 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. // Attribute editor
  2. //
  3. // Functions that caller must implement:
  4. // - void SetAttributeEditorID(UIElement@ attrEdit, Array<Serializable@>@ serializables);
  5. // - bool PreEditAttribute(Array<Serializable@>@ serializables, uint index);
  6. // - void PostEditAttribute(Array<Serializable@>@ serializables, uint index, const Array<Variant>& oldValues);
  7. // - Array<Serializable@>@ GetAttributeEditorTargets(UIElement@ attrEdit);
  8. // - String GetVariableName(StringHash hash);
  9. const uint MIN_NODE_ATTRIBUTES = 4;
  10. const uint MAX_NODE_ATTRIBUTES = 8;
  11. const int ATTRNAME_WIDTH = 150;
  12. const int ATTR_HEIGHT = 19;
  13. const StringHash TEXT_CHANGED_EVENT_TYPE("TextChanged");
  14. bool inLoadAttributeEditor = false;
  15. bool inEditAttribute = false;
  16. bool showNonEditableAttribute = false;
  17. Color normalTextColor(1.0f, 1.0f, 1.0f);
  18. Color modifiedTextColor(1.0f, 0.8f, 0.5f);
  19. Color nonEditableTextColor(0.7f, 0.7f, 0.7f);
  20. String sceneResourcePath = AddTrailingSlash(fileSystem.programDir + "Data");
  21. bool rememberResourcePath = true;
  22. // Exceptions for string attributes that should not be continuously edited
  23. Array<String> noTextChangedAttrs = {"Script File", "Class Name", "Script Object Type", "Script File Name"};
  24. WeakHandle testAnimState;
  25. bool dragEditAttribute = false;
  26. UIElement@ SetEditable(UIElement@ element, bool editable)
  27. {
  28. if (element is null)
  29. return element;
  30. element.editable = editable;
  31. element.colors[C_TOPLEFT] = editable ? element.colors[C_BOTTOMRIGHT] : nonEditableTextColor;
  32. element.colors[C_BOTTOMLEFT] = element.colors[C_TOPLEFT];
  33. element.colors[C_TOPRIGHT] = element.colors[C_TOPLEFT];
  34. return element;
  35. }
  36. UIElement@ SetValue(LineEdit@ element, const String&in value, bool sameValue)
  37. {
  38. element.text = sameValue ? value : STRIKED_OUT;
  39. element.cursorPosition = 0;
  40. return element;
  41. }
  42. UIElement@ SetValue(CheckBox@ element, bool value, bool sameValue)
  43. {
  44. element.checked = sameValue ? value : false;
  45. return element;
  46. }
  47. UIElement@ SetValue(DropDownList@ element, int value, bool sameValue)
  48. {
  49. element.selection = sameValue ? value : M_MAX_UNSIGNED;
  50. return element;
  51. }
  52. UIElement@ CreateAttributeEditorParentWithSeparatedLabel(ListView@ list, const String&in name, uint index, uint subIndex, bool suppressedSeparatedLabel = false)
  53. {
  54. UIElement@ editorParent = UIElement("Edit" + String(index) + "_" + String(subIndex));
  55. editorParent.vars["Index"] = index;
  56. editorParent.vars["SubIndex"] = subIndex;
  57. editorParent.SetLayout(LM_VERTICAL, 2);
  58. list.AddItem(editorParent);
  59. if (suppressedSeparatedLabel)
  60. {
  61. UIElement@ placeHolder = UIElement(name);
  62. editorParent.AddChild(placeHolder);
  63. }
  64. else
  65. {
  66. Text@ attrNameText = Text();
  67. editorParent.AddChild(attrNameText);
  68. attrNameText.style = "EditorAttributeText";
  69. attrNameText.text = name;
  70. }
  71. return editorParent;
  72. }
  73. UIElement@ CreateAttributeEditorParentAsListChild(ListView@ list, const String&in name, uint index, uint subIndex)
  74. {
  75. UIElement@ editorParent = UIElement("Edit" + String(index) + "_" + String(subIndex));
  76. editorParent.vars["Index"] = index;
  77. editorParent.vars["SubIndex"] = subIndex;
  78. editorParent.SetLayout(LM_HORIZONTAL);
  79. list.AddChild(editorParent);
  80. UIElement@ placeHolder = UIElement(name);
  81. editorParent.AddChild(placeHolder);
  82. return editorParent;
  83. }
  84. UIElement@ CreateAttributeEditorParent(ListView@ list, const String&in name, uint index, uint subIndex)
  85. {
  86. UIElement@ editorParent = UIElement("Edit" + String(index) + "_" + String(subIndex));
  87. editorParent.vars["Index"] = index;
  88. editorParent.vars["SubIndex"] = subIndex;
  89. editorParent.SetLayout(LM_HORIZONTAL);
  90. editorParent.SetFixedHeight(ATTR_HEIGHT);
  91. list.AddItem(editorParent);
  92. Text@ attrNameText = Text();
  93. editorParent.AddChild(attrNameText);
  94. attrNameText.style = "EditorAttributeText";
  95. attrNameText.text = name;
  96. attrNameText.SetFixedWidth(ATTRNAME_WIDTH);
  97. return editorParent;
  98. }
  99. LineEdit@ CreateAttributeLineEdit(UIElement@ parent, Array<Serializable@>@ serializables, uint index, uint subIndex)
  100. {
  101. LineEdit@ attrEdit = LineEdit();
  102. parent.AddChild(attrEdit);
  103. attrEdit.dragDropMode = DD_TARGET;
  104. attrEdit.style = "EditorAttributeEdit";
  105. attrEdit.SetFixedHeight(ATTR_HEIGHT - 2);
  106. attrEdit.vars["Index"] = index;
  107. attrEdit.vars["SubIndex"] = subIndex;
  108. SetAttributeEditorID(attrEdit, serializables);
  109. return attrEdit;
  110. }
  111. UIElement@ CreateStringAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex)
  112. {
  113. UIElement@ parent = CreateAttributeEditorParent(list, info.name, index, subIndex);
  114. LineEdit@ attrEdit = CreateAttributeLineEdit(parent, serializables, index, subIndex);
  115. attrEdit.dragDropMode = DD_TARGET;
  116. // Do not subscribe to continuous edits of certain attributes (script class names) to prevent unnecessary errors getting printed
  117. if (noTextChangedAttrs.Find(info.name) == -1)
  118. SubscribeToEvent(attrEdit, "TextChanged", "EditAttribute");
  119. SubscribeToEvent(attrEdit, "TextFinished", "EditAttribute");
  120. return parent;
  121. }
  122. UIElement@ CreateBoolAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex)
  123. {
  124. bool isUIElement = cast<UIElement>(serializables[0]) !is null;
  125. UIElement@ parent;
  126. if (info.name == (isUIElement ? "Is Visible" : "Is Enabled"))
  127. parent = CreateAttributeEditorParentAsListChild(list, info.name, index, subIndex);
  128. else
  129. parent = CreateAttributeEditorParent(list, info.name, index, subIndex);
  130. CheckBox@ attrEdit = CheckBox();
  131. parent.AddChild(attrEdit);
  132. attrEdit.style = AUTO_STYLE;
  133. attrEdit.vars["Index"] = index;
  134. attrEdit.vars["SubIndex"] = subIndex;
  135. SetAttributeEditorID(attrEdit, serializables);
  136. SubscribeToEvent(attrEdit, "Toggled", "EditAttribute");
  137. return parent;
  138. }
  139. UIElement@ CreateNumAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex)
  140. {
  141. UIElement@ parent = CreateAttributeEditorParent(list, info.name, index, subIndex);
  142. VariantType type = info.type;
  143. uint numCoords = type - VAR_FLOAT + 1;
  144. if (type == VAR_QUATERNION)
  145. numCoords = 3;
  146. else if (type == VAR_COLOR || type == VAR_INTRECT)
  147. numCoords = 4;
  148. else if (type == VAR_INTVECTOR2)
  149. numCoords = 2;
  150. for (uint i = 0; i < numCoords; ++i)
  151. {
  152. LineEdit@ attrEdit = CreateAttributeLineEdit(parent, serializables, index, subIndex);
  153. attrEdit.vars["Coordinate"] = i;
  154. CreateDragSlider(attrEdit);
  155. SubscribeToEvent(attrEdit, "TextChanged", "EditAttribute");
  156. SubscribeToEvent(attrEdit, "TextFinished", "EditAttribute");
  157. }
  158. return parent;
  159. }
  160. UIElement@ CreateIntAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex)
  161. {
  162. UIElement@ parent = CreateAttributeEditorParent(list, info.name, index, subIndex);
  163. // Check for enums
  164. if (info.enumNames is null || info.enumNames.empty)
  165. {
  166. // No enums, create a numeric editor
  167. LineEdit@ attrEdit = CreateAttributeLineEdit(parent, serializables, index, subIndex);
  168. CreateDragSlider(attrEdit);
  169. SubscribeToEvent(attrEdit, "TextChanged", "EditAttribute");
  170. SubscribeToEvent(attrEdit, "TextFinished", "EditAttribute");
  171. // If the attribute is a node ID, make it a drag/drop target
  172. if (info.name.Contains("NodeID", false) || info.name.Contains("Node ID", false) || (info.mode & AM_NODEID) != 0)
  173. attrEdit.dragDropMode = DD_TARGET;
  174. }
  175. else
  176. {
  177. DropDownList@ attrEdit = DropDownList();
  178. parent.AddChild(attrEdit);
  179. attrEdit.style = AUTO_STYLE;
  180. attrEdit.SetFixedHeight(ATTR_HEIGHT - 2);
  181. attrEdit.resizePopup = true;
  182. attrEdit.placeholderText = STRIKED_OUT;
  183. attrEdit.vars["Index"] = index;
  184. attrEdit.vars["SubIndex"] = subIndex;
  185. attrEdit.SetLayout(LM_HORIZONTAL, 0, IntRect(4, 1, 4, 1));
  186. SetAttributeEditorID(attrEdit, serializables);
  187. for (uint i = 0; i < info.enumNames.length; ++i)
  188. {
  189. Text@ choice = Text();
  190. attrEdit.AddItem(choice);
  191. choice.style = "EditorEnumAttributeText";
  192. choice.text = info.enumNames[i];
  193. }
  194. SubscribeToEvent(attrEdit, "ItemSelected", "EditAttribute");
  195. }
  196. return parent;
  197. }
  198. UIElement@ CreateResourceRefAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex, bool suppressedSeparatedLabel = false)
  199. {
  200. UIElement@ parent;
  201. StringHash resourceType;
  202. // Get the real attribute info from the serializable for the correct resource type
  203. AttributeInfo attrInfo = serializables[0].attributeInfos[index];
  204. if (attrInfo.type == VAR_RESOURCEREF)
  205. resourceType = serializables[0].attributes[index].GetResourceRef().type;
  206. else if (attrInfo.type == VAR_RESOURCEREFLIST)
  207. resourceType = serializables[0].attributes[index].GetResourceRefList().type;
  208. else if (attrInfo.type == VAR_VARIANTVECTOR)
  209. resourceType = serializables[0].attributes[index].GetVariantVector()[subIndex].GetResourceRef().type;
  210. ResourcePicker@ picker = GetResourcePicker(resourceType);
  211. // Create the attribute name on a separate non-interactive line to allow for more space
  212. parent = CreateAttributeEditorParentWithSeparatedLabel(list, info.name, index, subIndex, suppressedSeparatedLabel);
  213. UIElement@ container = UIElement();
  214. container.SetLayout(LM_HORIZONTAL, 4, IntRect(info.name.StartsWith(" ") ? 20 : 10, 0, 4, 0)); // Left margin is indented more when the name is so
  215. container.SetFixedHeight(ATTR_HEIGHT);
  216. parent.AddChild(container);
  217. LineEdit@ attrEdit = CreateAttributeLineEdit(container, serializables, index, subIndex);
  218. attrEdit.vars[TYPE_VAR] = resourceType.value;
  219. SubscribeToEvent(attrEdit, "TextFinished", "EditAttribute");
  220. if (picker !is null)
  221. {
  222. if ((picker.actions & ACTION_PICK) != 0)
  223. {
  224. Button@ pickButton = CreateResourcePickerButton(container, serializables, index, subIndex, "Pick");
  225. SubscribeToEvent(pickButton, "Released", "PickResource");
  226. }
  227. if ((picker.actions & ACTION_OPEN) != 0)
  228. {
  229. Button@ openButton = CreateResourcePickerButton(container, serializables, index, subIndex, "Open");
  230. SubscribeToEvent(openButton, "Released", "OpenResource");
  231. }
  232. if ((picker.actions & ACTION_EDIT) != 0)
  233. {
  234. Button@ editButton = CreateResourcePickerButton(container, serializables, index, subIndex, "Edit");
  235. SubscribeToEvent(editButton, "Released", "EditResource");
  236. }
  237. if ((picker.actions & ACTION_TEST) != 0)
  238. {
  239. Button@ testButton = CreateResourcePickerButton(container, serializables, index, subIndex, "Test");
  240. SubscribeToEvent(testButton, "Released", "TestResource");
  241. }
  242. }
  243. return parent;
  244. }
  245. Button@ CreateResourcePickerButton(UIElement@ container, Array<Serializable@>@ serializables, uint index, uint subIndex, const String&in text)
  246. {
  247. Button@ button = Button();
  248. container.AddChild(button);
  249. button.style = AUTO_STYLE;
  250. button.SetFixedSize(36, ATTR_HEIGHT - 2);
  251. button.vars["Index"] = index;
  252. button.vars["SubIndex"] = subIndex;
  253. SetAttributeEditorID(button, serializables);
  254. Text@ buttonText = Text();
  255. button.AddChild(buttonText);
  256. buttonText.style = "EditorAttributeText";
  257. buttonText.SetAlignment(HA_CENTER, VA_CENTER);
  258. buttonText.text = text;
  259. return button;
  260. }
  261. UIElement@ CreateAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index, uint subIndex, bool suppressedSeparatedLabel = false)
  262. {
  263. UIElement@ parent;
  264. VariantType type = info.type;
  265. if (type == VAR_STRING || type == VAR_BUFFER)
  266. parent = CreateStringAttributeEditor(list, serializables, info, index, subIndex);
  267. else if (type == VAR_BOOL)
  268. parent = CreateBoolAttributeEditor(list, serializables, info, index, subIndex);
  269. else if ((type >= VAR_FLOAT && type <= VAR_VECTOR4) || type == VAR_QUATERNION || type == VAR_COLOR || type == VAR_INTVECTOR2 || type == VAR_INTRECT)
  270. parent = CreateNumAttributeEditor(list, serializables, info, index, subIndex);
  271. else if (type == VAR_INT)
  272. parent = CreateIntAttributeEditor(list, serializables, info, index, subIndex);
  273. else if (type == VAR_RESOURCEREF)
  274. parent = CreateResourceRefAttributeEditor(list, serializables, info, index, subIndex, suppressedSeparatedLabel);
  275. else if (type == VAR_RESOURCEREFLIST)
  276. {
  277. uint numRefs = serializables[0].attributes[index].GetResourceRefList().length;
  278. // Straightly speaking the individual resource reference in the list is not an attribute of the serializable
  279. // However, the AttributeInfo structure is used here to reduce the number of parameters being passed in the function
  280. AttributeInfo refInfo;
  281. refInfo.name = info.name;
  282. refInfo.type = VAR_RESOURCEREF;
  283. for (uint i = 0; i < numRefs; ++i)
  284. CreateAttributeEditor(list, serializables, refInfo, index, i, i > 0);
  285. }
  286. else if (type == VAR_VARIANTVECTOR)
  287. {
  288. VectorStruct@ vectorStruct = GetVectorStruct(serializables, index);
  289. if (vectorStruct is null)
  290. return null;
  291. uint nameIndex = 0;
  292. Array<Variant>@ vector = serializables[0].attributes[index].GetVariantVector();
  293. for (uint i = 0; i < vector.length; ++i)
  294. {
  295. // The individual variant in the vector is not an attribute of the serializable, the structure is reused for convenience
  296. AttributeInfo vectorInfo;
  297. vectorInfo.name = vectorStruct.variableNames[nameIndex];
  298. vectorInfo.type = vector[i].type;
  299. CreateAttributeEditor(list, serializables, vectorInfo, index, i);
  300. ++nameIndex;
  301. if (nameIndex >= vectorStruct.variableNames.length)
  302. nameIndex = vectorStruct.restartIndex;
  303. }
  304. }
  305. else if (type == VAR_VARIANTMAP)
  306. {
  307. VariantMap map = serializables[0].attributes[index].GetVariantMap();
  308. Array<StringHash>@ keys = map.keys;
  309. for (uint i = 0; i < keys.length; ++i)
  310. {
  311. String varName = GetVarName(keys[i]);
  312. if (varName.empty)
  313. {
  314. // UIElements will contain internal vars, which do not have known mappings. Skip these
  315. if (cast<UIElement>(serializables[0]) !is null)
  316. continue;
  317. // Else, for scene nodes, show as hexadecimal hashes if nothing else is available
  318. varName = keys[i].ToString();
  319. }
  320. Variant value = map[keys[i]];
  321. // The individual variant in the map is not an attribute of the serializable, the structure is reused for convenience
  322. AttributeInfo mapInfo;
  323. mapInfo.name = varName + " (Var)";
  324. mapInfo.type = value.type;
  325. parent = CreateAttributeEditor(list, serializables, mapInfo, index, i);
  326. // Add the variant key to the parent. We may fail to add the editor in case it is unsupported
  327. if (parent !is null)
  328. {
  329. parent.vars["Key"] = keys[i].value;
  330. // If variable name is not registered (i.e. it is an editor internal variable) then hide it
  331. if (varName.empty)
  332. parent.visible = false;
  333. }
  334. }
  335. }
  336. return parent;
  337. }
  338. uint GetAttributeEditorCount(Array<Serializable@>@ serializables)
  339. {
  340. uint count = 0;
  341. if (!serializables.empty)
  342. {
  343. /// \todo When multi-editing, this only counts the editor count of the first serializable
  344. bool isUIElement = cast<UIElement>(serializables[0]) !is null;
  345. for (uint i = 0; i < serializables[0].numAttributes; ++i)
  346. {
  347. AttributeInfo info = serializables[0].attributeInfos[i];
  348. if (!showNonEditableAttribute && info.mode & AM_NOEDIT != 0)
  349. continue;
  350. // "Is Enabled" is not inserted into the main attribute list, so do not count
  351. // Similarly, for UIElement, "Is Visible" is not inserted
  352. if (info.name == (isUIElement ? "Is Visible" : "Is Enabled"))
  353. continue;
  354. if (info.type == VAR_RESOURCEREFLIST)
  355. count += serializables[0].attributes[i].GetResourceRefList().length;
  356. else if (info.type == VAR_VARIANTVECTOR && GetVectorStruct(serializables, i) !is null)
  357. count += serializables[0].attributes[i].GetVariantVector().length;
  358. else if (info.type == VAR_VARIANTMAP)
  359. count += serializables[0].attributes[i].GetVariantMap().length;
  360. else
  361. ++count;
  362. }
  363. }
  364. return count;
  365. }
  366. UIElement@ GetAttributeEditorParent(UIElement@ parent, uint index, uint subIndex)
  367. {
  368. return parent.GetChild("Edit" + String(index) + "_" + String(subIndex), true);
  369. }
  370. void LoadAttributeEditor(ListView@ list, Array<Serializable@>@ serializables, const AttributeInfo&in info, uint index)
  371. {
  372. bool editable = info.mode & AM_NOEDIT == 0;
  373. UIElement@ parent = GetAttributeEditorParent(list, index, 0);
  374. if (parent is null)
  375. return;
  376. inLoadAttributeEditor = true;
  377. bool sameName = true;
  378. bool sameValue = true;
  379. Variant value = serializables[0].attributes[index];
  380. Array<Variant> values;
  381. for (uint i = 0; i < serializables.length; ++i)
  382. {
  383. if (index >= serializables[i].numAttributes || serializables[i].attributeInfos[index].name != info.name)
  384. {
  385. sameName = false;
  386. break;
  387. }
  388. Variant val = serializables[i].attributes[index];
  389. if (val != value)
  390. sameValue = false;
  391. values.Push(val);
  392. }
  393. // Attribute with different values from multiple-select is loaded with default/empty value and non-editable
  394. if (sameName)
  395. LoadAttributeEditor(parent, value, info, editable, sameValue, values);
  396. else
  397. parent.visible = false;
  398. inLoadAttributeEditor = false;
  399. }
  400. void LoadAttributeEditor(UIElement@ parent, const Variant&in value, const AttributeInfo&in info, bool editable, bool sameValue, const Array<Variant>&in values)
  401. {
  402. uint index = parent.vars["Index"].GetUInt();
  403. // Assume the first child is always a text label element or a container that containing a text label element
  404. UIElement@ label = parent.children[0];
  405. if (label.type == UI_ELEMENT_TYPE && label.numChildren > 0)
  406. label = label.children[0];
  407. if (label.type == TEXT_TYPE)
  408. {
  409. bool modified;
  410. if (info.defaultValue.type == VAR_NONE || info.defaultValue.type == VAR_RESOURCEREFLIST)
  411. modified = !value.zero;
  412. else
  413. modified = value != info.defaultValue;
  414. cast<Text>(label).color = (editable ? (modified ? modifiedTextColor : normalTextColor) : nonEditableTextColor);
  415. }
  416. VariantType type = info.type;
  417. if (type == VAR_FLOAT || type == VAR_STRING || type == VAR_BUFFER)
  418. SetEditable(SetValue(parent.children[1], value.ToString(), sameValue), editable && sameValue);
  419. else if (type == VAR_BOOL)
  420. SetEditable(SetValue(parent.children[1], value.GetBool(), sameValue), editable && sameValue);
  421. else if (type == VAR_INT)
  422. {
  423. if (info.enumNames is null || info.enumNames.empty)
  424. SetEditable(SetValue(parent.children[1], value.ToString(), sameValue), editable && sameValue);
  425. else
  426. SetEditable(SetValue(parent.children[1], value.GetInt(), sameValue), editable && sameValue);
  427. }
  428. else if (type == VAR_RESOURCEREF)
  429. {
  430. SetEditable(SetValue(parent.children[1].children[0], value.GetResourceRef().name, sameValue), editable && sameValue);
  431. SetEditable(parent.children[1].children[1], editable && sameValue); // If editable then can pick
  432. for (uint i = 2; i < parent.children[1].numChildren; ++i)
  433. SetEditable(parent.children[1].children[i], sameValue); // If same value then can open/edit/test
  434. }
  435. else if (type == VAR_RESOURCEREFLIST)
  436. {
  437. UIElement@ list = parent.parent;
  438. ResourceRefList refList = value.GetResourceRefList();
  439. for (uint subIndex = 0; subIndex < refList.length; ++subIndex)
  440. {
  441. parent = GetAttributeEditorParent(list, index, subIndex);
  442. if (parent is null)
  443. break;
  444. String firstName = refList.names[subIndex];
  445. bool nameSameValue = true;
  446. if (!sameValue)
  447. {
  448. // Reevaluate each name in the list
  449. for (uint i = 0; i < values.length; ++i)
  450. {
  451. ResourceRefList refList = values[i].GetResourceRefList();
  452. if (subIndex >= refList.length || refList.names[subIndex] != firstName)
  453. {
  454. nameSameValue = false;
  455. break;
  456. }
  457. }
  458. }
  459. SetEditable(SetValue(parent.children[1].children[0], firstName, nameSameValue), editable && nameSameValue);
  460. }
  461. }
  462. else if (type == VAR_VARIANTVECTOR)
  463. {
  464. UIElement@ list = parent.parent;
  465. Array<Variant>@ vector = value.GetVariantVector();
  466. for (uint subIndex = 0; subIndex < vector.length; ++subIndex)
  467. {
  468. parent = GetAttributeEditorParent(list, index, subIndex);
  469. if (parent is null)
  470. break;
  471. Variant firstValue = vector[subIndex];
  472. bool sameValue = true;
  473. Array<Variant> varValues;
  474. // Reevaluate each variant in the vector
  475. for (uint i = 0; i < values.length; ++i)
  476. {
  477. Array<Variant>@ vector = values[i].GetVariantVector();
  478. if (subIndex < vector.length)
  479. {
  480. Variant value = vector[subIndex];
  481. varValues.Push(value);
  482. if (value != firstValue)
  483. sameValue = false;
  484. }
  485. else
  486. sameValue = false;
  487. }
  488. // The individual variant in the list is not an attribute of the serializable, the structure is reused for convenience
  489. AttributeInfo info;
  490. info.type = firstValue.type;
  491. LoadAttributeEditor(parent, firstValue, info, editable, sameValue, varValues);
  492. }
  493. }
  494. else if (type == VAR_VARIANTMAP)
  495. {
  496. UIElement@ list = parent.parent;
  497. VariantMap map = value.GetVariantMap();
  498. Array<StringHash>@ keys = map.keys;
  499. for (uint subIndex = 0; subIndex < keys.length; ++subIndex)
  500. {
  501. parent = GetAttributeEditorParent(list, index, subIndex);
  502. if (parent is null)
  503. break;
  504. String varName = GetVarName(keys[subIndex]);
  505. if (varName.empty)
  506. varName = keys[subIndex].ToString(); // Use hexadecimal if nothing else is available
  507. Variant firstValue = map[keys[subIndex]];
  508. bool sameValue = true;
  509. Array<Variant> varValues;
  510. // Reevaluate each variant in the map
  511. for (uint i = 0; i < values.length; ++i)
  512. {
  513. VariantMap map = values[i].GetVariantMap();
  514. if (map.Contains(keys[subIndex]))
  515. {
  516. Variant value = map[keys[subIndex]];
  517. varValues.Push(value);
  518. if (value != firstValue)
  519. sameValue = false;
  520. }
  521. else
  522. sameValue = false;
  523. }
  524. // The individual variant in the map is not an attribute of the serializable, the structure is reused for convenience
  525. AttributeInfo info;
  526. info.type = firstValue.type;
  527. LoadAttributeEditor(parent, firstValue, info, editable, sameValue, varValues);
  528. }
  529. }
  530. else
  531. {
  532. Array<Array<String> > coordinates;
  533. for (uint i = 0; i < values.length; ++i)
  534. {
  535. Variant value = values[i];
  536. // Convert Quaternion value to Vector3 value first
  537. if (type == VAR_QUATERNION)
  538. value = value.GetQuaternion().eulerAngles;
  539. coordinates.Push(value.ToString().Split(' '));
  540. }
  541. for (uint i = 0; i < coordinates[0].length; ++i)
  542. {
  543. String value = coordinates[0][i];
  544. bool coordinateSameValue = true;
  545. if (!sameValue)
  546. {
  547. // Reevaluate each coordinate
  548. for (uint j = 1; j < coordinates.length; ++j)
  549. {
  550. if (coordinates[j][i] != value)
  551. {
  552. coordinateSameValue = false;
  553. break;
  554. }
  555. }
  556. }
  557. SetEditable(SetValue(parent.children[i + 1], value, coordinateSameValue), editable && coordinateSameValue);
  558. }
  559. }
  560. }
  561. void StoreAttributeEditor(UIElement@ parent, Array<Serializable@>@ serializables, uint index, uint subIndex, uint coordinate)
  562. {
  563. AttributeInfo info = serializables[0].attributeInfos[index];
  564. if (info.type == VAR_RESOURCEREFLIST)
  565. {
  566. for (uint i = 0; i < serializables.length; ++i)
  567. {
  568. ResourceRefList refList = serializables[i].attributes[index].GetResourceRefList();
  569. Variant[] values(1);
  570. GetEditorValue(parent, VAR_RESOURCEREF, null, coordinate, values);
  571. ResourceRef ref = values[0].GetResourceRef();
  572. refList.names[subIndex] = ref.name;
  573. serializables[i].attributes[index] = Variant(refList);
  574. }
  575. }
  576. else if (info.type == VAR_VARIANTVECTOR)
  577. {
  578. for (uint i = 0; i < serializables.length; ++i)
  579. {
  580. Array<Variant>@ vector = serializables[i].attributes[index].GetVariantVector();
  581. Variant[] values;
  582. values.Push(vector[subIndex]); // Each individual variant may have multiple coordinates itself
  583. GetEditorValue(parent, vector[subIndex].type, null, coordinate, values);
  584. vector[subIndex] = values[0];
  585. serializables[i].attributes[index] = Variant(vector);
  586. }
  587. }
  588. else if (info.type == VAR_VARIANTMAP)
  589. {
  590. VariantMap map = serializables[0].attributes[index].GetVariantMap();
  591. StringHash key(parent.vars["Key"].GetUInt());
  592. for (uint i = 0; i < serializables.length; ++i)
  593. {
  594. VariantMap map = serializables[i].attributes[index].GetVariantMap();
  595. Variant[] values;
  596. values.Push(map[key]); // Each individual variant may have multiple coordinates itself
  597. GetEditorValue(parent, map[key].type, null, coordinate, values);
  598. map[key] = values[0];
  599. serializables[i].attributes[index] = Variant(map);
  600. }
  601. }
  602. else
  603. {
  604. Array<Variant> values;
  605. for (uint i = 0; i < serializables.length; ++i)
  606. values.Push(serializables[i].attributes[index]);
  607. GetEditorValue(parent, info.type, info.enumNames, coordinate, values);
  608. for (uint i = 0; i < serializables.length; ++i)
  609. serializables[i].attributes[index] = values[i];
  610. }
  611. }
  612. void FillValue(Array<Variant>& values, const Variant&in value)
  613. {
  614. for (uint i = 0; i < values.length; ++i)
  615. values[i] = value;
  616. }
  617. void SanitizeNumericalValue(VariantType type, String& value)
  618. {
  619. if (type >= VAR_FLOAT && type <= VAR_COLOR)
  620. value = String(value.ToFloat());
  621. else if (type == VAR_INT || type == VAR_INTRECT || type == VAR_INTVECTOR2)
  622. value = String(value.ToInt());
  623. }
  624. void GetEditorValue(UIElement@ parent, VariantType type, Array<String>@ enumNames, uint coordinate, Array<Variant>& values)
  625. {
  626. LineEdit@ attrEdit = parent.children[coordinate + 1];
  627. if (type == VAR_STRING)
  628. FillValue(values, Variant(attrEdit.text.Trimmed()));
  629. else if (type == VAR_BOOL)
  630. {
  631. CheckBox@ attrEdit = parent.children[1];
  632. FillValue(values, Variant(attrEdit.checked));
  633. }
  634. else if (type == VAR_FLOAT)
  635. FillValue(values, Variant(attrEdit.text.ToFloat()));
  636. else if (type == VAR_QUATERNION)
  637. {
  638. float value = attrEdit.text.ToFloat();
  639. for (uint i = 0; i < values.length; ++i)
  640. {
  641. float[] data = values[i].GetQuaternion().eulerAngles.data;
  642. data[coordinate] = value;
  643. values[i] = Quaternion(Vector3(data));
  644. }
  645. }
  646. else if (type == VAR_INT)
  647. {
  648. if (enumNames is null || enumNames.empty)
  649. FillValue(values, Variant(attrEdit.text.ToInt()));
  650. else
  651. {
  652. DropDownList@ attrEdit = parent.children[1];
  653. FillValue(values, Variant(attrEdit.selection));
  654. }
  655. }
  656. else if (type == VAR_RESOURCEREF)
  657. {
  658. LineEdit@ attrEdit = parent.children[0];
  659. ResourceRef ref;
  660. ref.name = attrEdit.text.Trimmed();
  661. ref.type = StringHash(attrEdit.vars[TYPE_VAR].GetUInt());
  662. FillValue(values, Variant(ref));
  663. }
  664. else
  665. {
  666. String value = attrEdit.text;
  667. SanitizeNumericalValue(type, value);
  668. for (uint i = 0; i < values.length; ++i)
  669. {
  670. String[] data = values[i].ToString().Split(' ');
  671. data[coordinate] = value;
  672. values[i] = Variant(type, Join(data, " "));
  673. }
  674. }
  675. }
  676. void UpdateAttributes(Array<Serializable@>@ serializables, ListView@ list, bool& fullUpdate)
  677. {
  678. // If attributes have changed structurally, do a full update
  679. uint count = GetAttributeEditorCount(serializables);
  680. if (fullUpdate == false)
  681. {
  682. if (list.contentElement.numChildren != count)
  683. fullUpdate = true;
  684. }
  685. // Remember the old scroll position so that a full update does not feel as jarring
  686. IntVector2 oldViewPos = list.viewPosition;
  687. if (fullUpdate)
  688. {
  689. list.RemoveAllItems();
  690. Array<UIElement@> children = list.GetChildren();
  691. for (uint i = 0; i < children.length; ++i)
  692. {
  693. if (!children[i].internal)
  694. children[i].Remove();
  695. }
  696. }
  697. if (serializables.empty)
  698. return;
  699. // If there are many serializables, they must share same attribute structure (up to certain number if not all)
  700. for (uint i = 0; i < serializables[0].numAttributes; ++i)
  701. {
  702. AttributeInfo info = serializables[0].attributeInfos[i];
  703. if (!showNonEditableAttribute && info.mode & AM_NOEDIT != 0)
  704. continue;
  705. // Use the default value (could be instance's default value) of the first serializable as the default for all
  706. info.defaultValue = serializables[0].attributeDefaults[i];
  707. if (fullUpdate)
  708. CreateAttributeEditor(list, serializables, info, i, 0);
  709. LoadAttributeEditor(list, serializables, info, i);
  710. }
  711. if (fullUpdate)
  712. list.viewPosition = oldViewPos;
  713. }
  714. void EditScriptAttributes(Component@ component, uint index)
  715. {
  716. if (component !is null && component.typeName.Contains("ScriptInstance"))
  717. {
  718. String hash = GetComponentAttributeHash(component, index);
  719. if (!hash.empty)
  720. scriptAttributes[hash] = component.attributes[index];
  721. }
  722. }
  723. void CreateDragSlider(LineEdit@ parent)
  724. {
  725. Button@ dragSld = Button();
  726. dragSld.style = "EditorDragSlider";
  727. dragSld.SetFixedHeight(ATTR_HEIGHT - 3);
  728. dragSld.SetFixedWidth(dragSld.height);
  729. dragSld.SetAlignment(HA_RIGHT, VA_TOP);
  730. parent.AddChild(dragSld);
  731. SubscribeToEvent(dragSld, "DragBegin", "LineDragBegin");
  732. SubscribeToEvent(dragSld, "DragMove", "LineDragMove");
  733. SubscribeToEvent(dragSld, "DragEnd", "LineDragEnd");
  734. SubscribeToEvent(dragSld, "DragCancel", "LineDragCancel");
  735. }
  736. void EditAttribute(StringHash eventType, VariantMap& eventData)
  737. {
  738. // Changing elements programmatically may cause events to be sent. Stop possible infinite loop in that case.
  739. if (inLoadAttributeEditor)
  740. return;
  741. UIElement@ attrEdit = eventData["Element"].GetPtr();
  742. UIElement@ parent = attrEdit.parent;
  743. Array<Serializable@>@ serializables = GetAttributeEditorTargets(attrEdit);
  744. if (serializables.empty)
  745. return;
  746. uint index = attrEdit.vars["Index"].GetUInt();
  747. uint subIndex = attrEdit.vars["SubIndex"].GetUInt();
  748. uint coordinate = attrEdit.vars["Coordinate"].GetUInt();
  749. bool intermediateEdit = eventType == TEXT_CHANGED_EVENT_TYPE;
  750. // Do the editor pre logic before attribute is being modified
  751. if (!PreEditAttribute(serializables, index))
  752. return;
  753. inEditAttribute = true;
  754. Array<Variant> oldValues;
  755. if (!dragEditAttribute)
  756. {
  757. // Store old values so that PostEditAttribute can create undo actions
  758. for (uint i = 0; i < serializables.length; ++i)
  759. oldValues.Push(serializables[i].attributes[index]);
  760. }
  761. StoreAttributeEditor(parent, serializables, index, subIndex, coordinate);
  762. for (uint i = 0; i < serializables.length; ++i)
  763. serializables[i].ApplyAttributes();
  764. if (!dragEditAttribute)
  765. {
  766. // Do the editor post logic after attribute has been modified.
  767. PostEditAttribute(serializables, index, oldValues);
  768. }
  769. // Update the stored script attributes if this is a ScriptInstance
  770. EditScriptAttributes(serializables[0], index);
  771. inEditAttribute = false;
  772. // If not an intermediate edit, reload the editor fields with validated values
  773. // (attributes may have interactions; therefore we load everything, not just the value being edited)
  774. if (!intermediateEdit)
  775. attributesDirty = true;
  776. }
  777. void LineDragBegin(StringHash eventType, VariantMap& eventData)
  778. {
  779. UIElement@ label = eventData["Element"].GetPtr();
  780. int x = eventData["X"].GetInt();
  781. label.vars["posX"] = x;
  782. // Store the old value before dragging
  783. dragEditAttribute = false;
  784. LineEdit@ selectedNumEditor = label.parent;
  785. selectedNumEditor.vars["DragBeginValue"] = selectedNumEditor.text;
  786. selectedNumEditor.cursorPosition = 0;
  787. // Set mouse mode to user preference
  788. SetMouseMode(true);
  789. }
  790. void LineDragMove(StringHash eventTypem, VariantMap& eventData)
  791. {
  792. UIElement@ label = eventData["Element"].GetPtr();
  793. LineEdit@ selectedNumEditor = label.parent;
  794. // Prevent undo
  795. dragEditAttribute = true;
  796. int x = eventData["X"].GetInt();
  797. int posx = label.vars["posX"].GetInt();
  798. float val = input.mouseMoveX;
  799. float fieldVal = selectedNumEditor.text.ToFloat();
  800. fieldVal += val/100;
  801. label.vars["posX"] = x;
  802. selectedNumEditor.text = fieldVal;
  803. selectedNumEditor.cursorPosition = 0;
  804. }
  805. void LineDragEnd(StringHash eventType, VariantMap& eventData)
  806. {
  807. UIElement@ label = eventData["Element"].GetPtr();
  808. LineEdit@ selectedNumEditor = label.parent;
  809. // Prepare the attributes to store an undo with:
  810. // - old value = drag begin value
  811. // - new value = final value
  812. String finalValue = selectedNumEditor.text;
  813. // Reset attribute to begin value, and prevent undo
  814. dragEditAttribute = true;
  815. selectedNumEditor.text = selectedNumEditor.vars["DragBeginValue"].GetString();
  816. // Store final value, allow undo
  817. dragEditAttribute = false;
  818. selectedNumEditor.text = finalValue;
  819. selectedNumEditor.cursorPosition = 0;
  820. // Revert mouse to normal behaviour
  821. SetMouseMode(false);
  822. }
  823. void LineDragCancel(StringHash eventType, VariantMap& eventData)
  824. {
  825. UIElement@ label = eventData["Element"].GetPtr();
  826. // Reset value to what it was when drag edit began, preventing undo.
  827. dragEditAttribute = true;
  828. LineEdit@ selectedNumEditor = label.parent;
  829. selectedNumEditor.text = selectedNumEditor.vars["DragBeginValue"].GetString();
  830. selectedNumEditor.cursorPosition = 0;
  831. // Revert mouse to normal behaviour
  832. SetMouseMode(false);
  833. }
  834. // Resource picker functionality
  835. const uint ACTION_PICK = 1;
  836. const uint ACTION_OPEN = 2;
  837. const uint ACTION_EDIT = 4;
  838. const uint ACTION_TEST = 8;
  839. class ResourcePicker
  840. {
  841. String typeName;
  842. StringHash type;
  843. String lastPath;
  844. uint lastFilter;
  845. Array<String> filters;
  846. uint actions;
  847. ResourcePicker(const String&in typeName_, const String&in filter_, uint actions_ = ACTION_PICK | ACTION_OPEN)
  848. {
  849. typeName = typeName_;
  850. type = StringHash(typeName_);
  851. actions = actions_;
  852. filters.Push(filter_);
  853. filters.Push("*.*");
  854. lastFilter = 0;
  855. }
  856. ResourcePicker(const String&in typeName_, const Array<String>@ filters_, uint actions_ = ACTION_PICK | ACTION_OPEN)
  857. {
  858. typeName = typeName_;
  859. type = StringHash(typeName_);
  860. filters = filters_;
  861. actions = actions_;
  862. filters.Push("*.*");
  863. lastFilter = 0;
  864. }
  865. };
  866. Array<ResourcePicker@> resourcePickers;
  867. Array<Serializable@> resourceTargets;
  868. uint resourcePickIndex = 0;
  869. uint resourcePickSubIndex = 0;
  870. ResourcePicker@ resourcePicker = null;
  871. void InitResourcePicker()
  872. {
  873. // Fill resource picker data
  874. Array<String> fontFilters = {"*.ttf", "*.otf", "*.fnt", "*.xml"};
  875. Array<String> imageFilters = {"*.png", "*.jpg", "*.bmp", "*.tga"};
  876. Array<String> luaFileFilters = {"*.lua", "*.luc"};
  877. Array<String> scriptFilters = {"*.as", "*.asc"};
  878. Array<String> soundFilters = {"*.wav","*.ogg"};
  879. Array<String> textureFilters = {"*.dds", "*.png", "*.jpg", "*.bmp", "*.tga", "*.ktx", "*.pvr"};
  880. Array<String> materialFilters = {"*.xml", "*.material"};
  881. Array<String> anmSetFilters = {"*.scml"};
  882. Array<String> pexFilters = {"*.pex"};
  883. Array<String> tmxFilters = {"*.tmx"};
  884. resourcePickers.Push(ResourcePicker("Animation", "*.ani", ACTION_PICK | ACTION_TEST));
  885. resourcePickers.Push(ResourcePicker("Font", fontFilters));
  886. resourcePickers.Push(ResourcePicker("Image", imageFilters));
  887. resourcePickers.Push(ResourcePicker("LuaFile", luaFileFilters));
  888. resourcePickers.Push(ResourcePicker("Material", materialFilters, ACTION_PICK | ACTION_OPEN | ACTION_EDIT));
  889. resourcePickers.Push(ResourcePicker("Model", "*.mdl", ACTION_PICK));
  890. resourcePickers.Push(ResourcePicker("ParticleEffect", "*.xml", ACTION_PICK | ACTION_OPEN));
  891. resourcePickers.Push(ResourcePicker("ScriptFile", scriptFilters));
  892. resourcePickers.Push(ResourcePicker("Sound", soundFilters));
  893. resourcePickers.Push(ResourcePicker("Technique", "*.xml"));
  894. resourcePickers.Push(ResourcePicker("Texture2D", textureFilters));
  895. resourcePickers.Push(ResourcePicker("TextureCube", "*.xml"));
  896. resourcePickers.Push(ResourcePicker("Texture3D", "*.xml"));
  897. resourcePickers.Push(ResourcePicker("XMLFile", "*.xml"));
  898. resourcePickers.Push(ResourcePicker("Sprite2D", textureFilters, ACTION_PICK | ACTION_OPEN));
  899. resourcePickers.Push(ResourcePicker("AnimationSet2D", anmSetFilters, ACTION_PICK | ACTION_OPEN));
  900. resourcePickers.Push(ResourcePicker("ParticleEffect2D", pexFilters, ACTION_PICK | ACTION_OPEN));
  901. resourcePickers.Push(ResourcePicker("TmxFile2D", tmxFilters, ACTION_PICK | ACTION_OPEN));
  902. }
  903. ResourcePicker@ GetResourcePicker(StringHash resourceType)
  904. {
  905. for (uint i = 0; i < resourcePickers.length; ++i)
  906. {
  907. if (resourcePickers[i].type == resourceType)
  908. return resourcePickers[i];
  909. }
  910. return null;
  911. }
  912. void PickResource(StringHash eventType, VariantMap& eventData)
  913. {
  914. UIElement@ button = eventData["Element"].GetPtr();
  915. LineEdit@ attrEdit = button.parent.children[0];
  916. Array<Serializable@>@ targets = GetAttributeEditorTargets(attrEdit);
  917. if (targets.empty)
  918. return;
  919. resourcePickIndex = attrEdit.vars["Index"].GetUInt();
  920. resourcePickSubIndex = attrEdit.vars["SubIndex"].GetUInt();
  921. AttributeInfo info = targets[0].attributeInfos[resourcePickIndex];
  922. StringHash resourceType;
  923. if (info.type == VAR_RESOURCEREF)
  924. resourceType = targets[0].attributes[resourcePickIndex].GetResourceRef().type;
  925. else if (info.type == VAR_RESOURCEREFLIST)
  926. resourceType = targets[0].attributes[resourcePickIndex].GetResourceRefList().type;
  927. else if (info.type == VAR_VARIANTVECTOR)
  928. resourceType = targets[0].attributes[resourcePickIndex].GetVariantVector()[resourcePickSubIndex].GetResourceRef().type;
  929. @resourcePicker = GetResourcePicker(resourceType);
  930. if (resourcePicker is null)
  931. return;
  932. resourceTargets.Clear();
  933. for (uint i = 0; i < targets.length; ++i)
  934. resourceTargets.Push(targets[i]);
  935. String lastPath = resourcePicker.lastPath;
  936. if (lastPath.empty)
  937. lastPath = sceneResourcePath;
  938. CreateFileSelector("Pick " + resourcePicker.typeName, "OK", "Cancel", lastPath, resourcePicker.filters, resourcePicker.lastFilter);
  939. SubscribeToEvent(uiFileSelector, "FileSelected", "PickResourceDone");
  940. }
  941. void PickResourceDone(StringHash eventType, VariantMap& eventData)
  942. {
  943. StoreResourcePickerPath();
  944. CloseFileSelector();
  945. if (!eventData["OK"].GetBool())
  946. {
  947. resourceTargets.Clear();
  948. @resourcePicker = null;
  949. return;
  950. }
  951. if (resourcePicker is null)
  952. return;
  953. // Validate the resource. It must come from within a registered resource directory, and be loaded successfully
  954. String resourceName = eventData["FileName"].GetString();
  955. Resource@ res = GetPickedResource(resourceName);
  956. if (res is null)
  957. {
  958. @resourcePicker = null;
  959. return;
  960. }
  961. // Store old values so that PostEditAttribute can create undo actions
  962. Array<Variant> oldValues;
  963. for (uint i = 0; i < resourceTargets.length; ++i)
  964. oldValues.Push(resourceTargets[i].attributes[resourcePickIndex]);
  965. for (uint i = 0; i < resourceTargets.length; ++i)
  966. {
  967. Serializable@ target = resourceTargets[i];
  968. AttributeInfo info = target.attributeInfos[resourcePickIndex];
  969. if (info.type == VAR_RESOURCEREF)
  970. {
  971. ResourceRef ref = target.attributes[resourcePickIndex].GetResourceRef();
  972. ref.type = res.type;
  973. ref.name = res.name;
  974. target.attributes[resourcePickIndex] = Variant(ref);
  975. target.ApplyAttributes();
  976. }
  977. else if (info.type == VAR_RESOURCEREFLIST)
  978. {
  979. ResourceRefList refList = target.attributes[resourcePickIndex].GetResourceRefList();
  980. if (resourcePickSubIndex < refList.length)
  981. {
  982. refList.names[resourcePickSubIndex] = res.name;
  983. target.attributes[resourcePickIndex] = Variant(refList);
  984. target.ApplyAttributes();
  985. }
  986. }
  987. else if (info.type == VAR_VARIANTVECTOR)
  988. {
  989. Array<Variant>@ attrs = target.attributes[resourcePickIndex].GetVariantVector();
  990. ResourceRef ref = attrs[resourcePickSubIndex].GetResourceRef();
  991. ref.type = res.type;
  992. ref.name = res.name;
  993. attrs[resourcePickSubIndex] = ref;
  994. target.attributes[resourcePickIndex] = Variant(attrs);
  995. target.ApplyAttributes();
  996. }
  997. EditScriptAttributes(target, resourcePickIndex);
  998. }
  999. PostEditAttribute(resourceTargets, resourcePickIndex, oldValues);
  1000. UpdateAttributeInspector(false);
  1001. resourceTargets.Clear();
  1002. @resourcePicker = null;
  1003. }
  1004. void StoreResourcePickerPath()
  1005. {
  1006. // Store filter and directory for next time
  1007. if (resourcePicker !is null && uiFileSelector !is null)
  1008. {
  1009. resourcePicker.lastPath = uiFileSelector.path;
  1010. resourcePicker.lastFilter = uiFileSelector.filterIndex;
  1011. }
  1012. }
  1013. Resource@ GetPickedResource(String resourceName)
  1014. {
  1015. resourceName = GetResourceNameFromFullName(resourceName);
  1016. String type = resourcePicker.typeName;
  1017. // Cube and 3D textures both use .xml extension. In that case interrogate the proper resource type
  1018. // from the file itself
  1019. if (type == "Texture3D" || type == "TextureCube")
  1020. {
  1021. XMLFile@ xmlRes = cache.GetResource("XMLFile", resourceName);
  1022. if (xmlRes !is null)
  1023. {
  1024. if (xmlRes.root.name.Compare("cubemap", false) == 0 || xmlRes.root.name.Compare("texturecube", false) == 0)
  1025. type = "TextureCube";
  1026. else if (xmlRes.root.name.Compare("texture3d", false) == 0)
  1027. type = "Texture3D";
  1028. }
  1029. }
  1030. Resource@ res = cache.GetResource(type, resourceName);
  1031. if (res is null)
  1032. log.Warning("Cannot find resource type: " + type + " Name:" + resourceName);
  1033. return res;
  1034. }
  1035. String GetResourceNameFromFullName(const String&in resourceName)
  1036. {
  1037. Array<String>@ resourceDirs = cache.resourceDirs;
  1038. for (uint i = 0; i < resourceDirs.length; ++i)
  1039. {
  1040. if (!resourceName.ToLower().StartsWith(resourceDirs[i].ToLower()))
  1041. continue;
  1042. return resourceName.Substring(resourceDirs[i].length);
  1043. }
  1044. return ""; // Not found
  1045. }
  1046. void OpenResource(StringHash eventType, VariantMap& eventData)
  1047. {
  1048. UIElement@ button = eventData["Element"].GetPtr();
  1049. LineEdit@ attrEdit = button.parent.children[0];
  1050. String fileName = attrEdit.text.Trimmed();
  1051. if (fileName.empty)
  1052. return;
  1053. OpenResource(fileName);
  1054. }
  1055. void OpenResource(String fileName)
  1056. {
  1057. Array<String>@ resourceDirs = cache.resourceDirs;
  1058. for (uint i = 0; i < resourceDirs.length; ++i)
  1059. {
  1060. String fullPath = resourceDirs[i] + fileName;
  1061. if (fileSystem.FileExists(fullPath))
  1062. {
  1063. fileSystem.SystemOpen(fullPath, "");
  1064. return;
  1065. }
  1066. }
  1067. }
  1068. void EditResource(StringHash eventType, VariantMap& eventData)
  1069. {
  1070. UIElement@ button = eventData["Element"].GetPtr();
  1071. LineEdit@ attrEdit = button.parent.children[0];
  1072. String fileName = attrEdit.text.Trimmed();
  1073. if (fileName.empty)
  1074. return;
  1075. StringHash resourceType(attrEdit.vars[TYPE_VAR].GetUInt());
  1076. Resource@ resource = cache.GetResource(resourceType, fileName);
  1077. if (resource !is null)
  1078. {
  1079. // For now only Materials can be edited
  1080. if (resource.typeName == "Material")
  1081. EditMaterial(cast<Material>(resource));
  1082. }
  1083. }
  1084. void TestResource(StringHash eventType, VariantMap& eventData)
  1085. {
  1086. UIElement@ button = eventData["Element"].GetPtr();
  1087. LineEdit@ attrEdit = button.parent.children[0];
  1088. StringHash resourceType(attrEdit.vars[TYPE_VAR].GetUInt());
  1089. // For now only Animations can be tested
  1090. StringHash animType("Animation");
  1091. if (resourceType == animType)
  1092. TestAnimation(attrEdit);
  1093. }
  1094. void TestAnimation(UIElement@ attrEdit)
  1095. {
  1096. // Note: only supports the AnimationState array in AnimatedModel, and if only 1 model selected
  1097. Array<Serializable@>@ targets = GetAttributeEditorTargets(attrEdit);
  1098. if (targets.length != 1)
  1099. return;
  1100. AnimatedModel@ model = cast<AnimatedModel>(targets[0]);
  1101. if (model is null)
  1102. return;
  1103. uint animStateIndex = (attrEdit.vars["SubIndex"].GetUInt() - 1) / 6;
  1104. if (testAnimState.Get() is null)
  1105. {
  1106. testAnimState = model.GetAnimationState(animStateIndex);
  1107. AnimationState@ animState = testAnimState.Get();
  1108. if (animState !is null)
  1109. animState.time = 0; // Start from beginning
  1110. }
  1111. else
  1112. testAnimState = null;
  1113. }
  1114. void UpdateTestAnimation(float timeStep)
  1115. {
  1116. AnimationState@ animState = testAnimState.Get();
  1117. if (animState !is null)
  1118. {
  1119. // If has also an AnimationController, and scene update is enabled, check if it is also driving the animation
  1120. // and skip in that case (avoid double speed animation)
  1121. if (runUpdate)
  1122. {
  1123. AnimatedModel@ model = animState.model;
  1124. if (model !is null)
  1125. {
  1126. Node@ node = model.node;
  1127. if (node !is null)
  1128. {
  1129. AnimationController@ ctrl = node.GetComponent("AnimationController");
  1130. Animation@ anim = animState.animation;
  1131. if (ctrl !is null && anim !is null)
  1132. {
  1133. if (ctrl.IsPlaying(anim.name))
  1134. return;
  1135. }
  1136. }
  1137. }
  1138. }
  1139. animState.AddTime(timeStep);
  1140. }
  1141. }
  1142. // VariantVector decoding & editing for certain components
  1143. class VectorStruct
  1144. {
  1145. String componentTypeName;
  1146. String attributeName;
  1147. Array<String> variableNames;
  1148. uint restartIndex;
  1149. VectorStruct(const String&in componentTypeName_, const String&in attributeName_, const Array<String>@ variableNames_, uint restartIndex_)
  1150. {
  1151. componentTypeName = componentTypeName_;
  1152. attributeName = attributeName_;
  1153. variableNames = variableNames_;
  1154. restartIndex = restartIndex_;
  1155. }
  1156. };
  1157. Array<VectorStruct@> vectorStructs;
  1158. void InitVectorStructs()
  1159. {
  1160. // Fill vector structure data
  1161. Array<String> billboardVariables = {
  1162. "Billboard Count",
  1163. " Position",
  1164. " Size",
  1165. " UV Coordinates",
  1166. " Color",
  1167. " Rotation",
  1168. " Is Enabled"
  1169. };
  1170. vectorStructs.Push(VectorStruct("BillboardSet", "Billboards", billboardVariables, 1));
  1171. Array<String> animationStateVariables = {
  1172. "Anim State Count",
  1173. " Animation",
  1174. " Start Bone",
  1175. " Is Looped",
  1176. " Weight",
  1177. " Time",
  1178. " Layer"
  1179. };
  1180. vectorStructs.Push(VectorStruct("AnimatedModel", "Animation States", animationStateVariables, 1));
  1181. Array<String> staticModelGroupInstanceVariables = {
  1182. "Instance Count",
  1183. " NodeID"
  1184. };
  1185. vectorStructs.Push(VectorStruct("StaticModelGroup", "Instance Nodes", staticModelGroupInstanceVariables, 1));
  1186. Array<String> splinePathInstanceVariables = {
  1187. "Control Point Count",
  1188. " NodeID"
  1189. };
  1190. vectorStructs.Push(VectorStruct("SplinePath", "Control Points", splinePathInstanceVariables, 1));
  1191. }
  1192. VectorStruct@ GetVectorStruct(Array<Serializable@>@ serializables, uint index)
  1193. {
  1194. AttributeInfo info = serializables[0].attributeInfos[index];
  1195. for (uint i = 0; i < vectorStructs.length; ++i)
  1196. {
  1197. if (vectorStructs[i].componentTypeName == serializables[0].typeName && vectorStructs[i].attributeName == info.name)
  1198. return vectorStructs[i];
  1199. }
  1200. return null;
  1201. }
  1202. int GetAttributeIndex(Serializable@ serializable, const String&in attrName)
  1203. {
  1204. for (uint i = 0; i < serializable.numAttributes; ++i)
  1205. {
  1206. if (serializable.attributeInfos[i].name.Compare(attrName, false) == 0)
  1207. return i;
  1208. }
  1209. return -1;
  1210. }