AttributeEditor.as 56 KB

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