AttributeEditor.as 46 KB

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